uart.h
Go to the documentation of this file.
1 /*
2  * KubOS HAL
3  * Copyright (C) 2016 Kubos Corporation
4  *
5  * Licensed under the Apache License, Version 2.0 (the "License");
6  * you may not use this file except in compliance with the License.
7  * You may obtain a copy of the License at
8  *
9  * http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  */
27 #if (defined YOTTA_CFG_HARDWARE_UART) && (YOTTA_CFG_HARDWARE_UART_COUNT > 0)
28 #ifndef K_UART_H
29 #define K_UART_H
30 
31 #include "FreeRTOS.h"
32 #include "queue.h"
33 #include <stdint.h>
34 
35 #include "pins.h"
36 
49 #ifndef K_NUM_UARTS
50 #define K_NUM_UARTS YOTTA_CFG_HARDWARE_UART_COUNT
51 #endif
52 
65 #ifndef K_UART_CONSOLE
66 #define K_UART_CONSOLE YOTTA_CFG_HARDWARE_CONSOLE_UART
67 #endif
68 
81 #ifndef K_UART_CONSOLE_BAUDRATE
82 #define K_UART_CONSOLE_BAUDRATE YOTTA_CFG_HARDWARE_CONSOLE_BAUDRATE
83 #endif
84 
88 typedef enum {
89 #ifdef YOTTA_CFG_HARDWARE_UART_UART1
90  K_UART1 = 0,
91 #endif
92 #ifdef YOTTA_CFG_HARDWARE_UART_UART2
94 #endif
95 #ifdef YOTTA_CFG_HARDWARE_UART_UART3
97 #endif
98 #ifdef YOTTA_CFG_HARDWARE_UART_UART4
100 #endif
101 #ifdef YOTTA_CFG_HARDWARE_UART_UART5
103 #endif
104 #ifdef YOTTA_CFG_HARDWARE_UART_UART6
106 #endif
107 } KUARTNum;
108 
112 typedef enum {
116 } KWordLen;
117 
121 typedef enum {
124 } KStopBits;
125 
129 typedef enum {
133 } KParity;
134 
138 typedef struct {
139  const char *dev_path;
140  uint32_t baud_rate;
144  uint8_t rx_queue_len;
145  uint8_t tx_queue_len;
146 } KUARTConf;
147 
151 typedef struct {
152  int dev;
154  QueueHandle_t rx_queue;
155  QueueHandle_t tx_queue;
156 } KUART;
157 
163 
169 void k_uart_init(KUARTNum uart, KUARTConf *conf);
170 
174 void k_uart_console_init(void);
175 
186 int k_uart_read(KUARTNum uart, char *ptr, int len);
187 
198 int k_uart_write(KUARTNum uart, char *ptr, int len);
199 
205 void k_uart_write_immediate(KUARTNum uart, char c);
206 
211 int k_uart_rx_queue_len(KUARTNum uart);
212 
219 void k_uart_rx_queue_push(KUARTNum uart, char c, void *task_woken);
220 
226 inline int k_uart_rx_pin(KUARTNum uart) {
227  switch (uart) {
228 #ifdef YOTTA_CFG_HARDWARE_UART_UART1_RX
229  case K_UART1: return YOTTA_CFG_HARDWARE_UART_UART1_RX;
230 #endif
231 #ifdef YOTTA_CFG_HARDWARE_UART_UART2_RX
232  case K_UART2: return YOTTA_CFG_HARDWARE_UART_UART2_RX;
233 #endif
234 #ifdef YOTTA_CFG_HARDWARE_UART_UART3_RX
235  case K_UART3: return YOTTA_CFG_HARDWARE_UART_UART3_RX;
236 #endif
237 #ifdef YOTTA_CFG_HARDWARE_UART_UART4_RX
238  case K_UART4: return YOTTA_CFG_HARDWARE_UART_UART4_RX;
239 #endif
240 #ifdef YOTTA_CFG_HARDWARE_UART_UART5_RX
241  case K_UART5: return YOTTA_CFG_HARDWARE_UART_UART5_RX;
242 #endif
243 #ifdef YOTTA_CFG_HARDWARE_UART_UART6_RX
244  case K_UART6: return YOTTA_CFG_HARDWARE_UART_UART6_RX;
245 #endif
246  }
247  return -1;
248 }
249 
255 inline int k_uart_tx_pin(KUARTNum uart) {
256  switch (uart) {
257 #ifdef YOTTA_CFG_HARDWARE_UART_UART1_TX
258  case K_UART1: return YOTTA_CFG_HARDWARE_UART_UART1_TX;
259 #endif
260 #ifdef YOTTA_CFG_HARDWARE_UART_UART2_TX
261  case K_UART2: return YOTTA_CFG_HARDWARE_UART_UART2_TX;
262 #endif
263 #ifdef YOTTA_CFG_HARDWARE_UART_UART3_TX
264  case K_UART3: return YOTTA_CFG_HARDWARE_UART_UART3_TX;
265 #endif
266 #ifdef YOTTA_CFG_HARDWARE_UART_UART4_TX
267  case K_UART4: return YOTTA_CFG_HARDWARE_UART_UART4_TX;
268 #endif
269 #ifdef YOTTA_CFG_HARDWARE_UART_UART5_TX
270  case K_UART5: return YOTTA_CFG_HARDWARE_UART_UART5_TX;
271 #endif
272 #ifdef YOTTA_CFG_HARDWARE_UART_UART6_TX
273  case K_UART6: return YOTTA_CFG_HARDWARE_UART_UART6_TX;
274 #endif
275  }
276  return -1;
277 }
278 
279 // private APIs
286 
291 void kprv_uart_dev_init(KUARTNum uart);
292 
298 #endif // #ifndef K_UART_H
299 #endif // #ifdef YOTTA_CFG_HARDWARE_UART && YOTTA_CFG_HARDE_UART_COUNT > 0
300 /* @} */
KStopBits
Number of stop bits.
Definition: uart.h:121
int dev
Definition: uart.h:152
KUART * kprv_uart_get(KUARTNum uart)
Returns uart data structure for specified interface.
KStopBits stop_bits
Definition: uart.h:142
Definition: uart.h:113
void kprv_uart_enable_tx_int(KUARTNum uart)
Enables uart transmit interrupt.
KParity
Parity setting.
Definition: uart.h:129
Uart configuration structure.
Definition: uart.h:138
Definition: uart.h:96
int k_uart_write(KUARTNum uart, char *ptr, int len)
Interrupt driven function for writing data to a uart interface.
Definition: uart.h:115
void k_uart_console_init(void)
Setup and enable console uart interface.
Definition: uart.h:93
void kprv_uart_dev_init(KUARTNum uart)
Performs low level uart hardware initialization.
Definition: uart.h:99
Definition: uart.h:90
Definition: uart.h:122
Definition: uart.h:131
KUARTNum
Available uart interfaces.
Definition: uart.h:88
Definition: uart.h:130
KUARTConf conf
Definition: uart.h:153
Definition: uart.h:105
uint8_t tx_queue_len
Definition: uart.h:145
QueueHandle_t rx_queue
Definition: uart.h:154
Definition: uart.h:114
KWordLen
Word length.
Definition: uart.h:112
Definition: uart.h:102
Definition: uart.h:132
Definition: uart.h:123
uint8_t rx_queue_len
Definition: uart.h:144
int k_uart_read(KUARTNum uart, char *ptr, int len)
Interrupt driven function for reading data from a uart interface.
int k_uart_rx_queue_len(KUARTNum uart)
Returns the number of characters currently in the uart rx queue.
int k_uart_rx_pin(KUARTNum uart)
Returns rx pin for specified uart interface.
Definition: uart.h:226
QueueHandle_t tx_queue
Definition: uart.h:155
void k_uart_init(KUARTNum uart, KUARTConf *conf)
Setup and enable uart interface.
KParity parity
Definition: uart.h:143
uint32_t baud_rate
Definition: uart.h:140
KWordLen word_len
Definition: uart.h:141
KUARTConf k_uart_conf_defaults(void)
Generate KUARTConf with default uart values.
const char * dev_path
Definition: uart.h:139
void k_uart_write_immediate(KUARTNum uart, char c)
Write data directly to a uart interface.
Uart interface data structure.
Definition: uart.h:151
void k_uart_rx_queue_push(KUARTNum uart, char c, void *task_woken)
Pushes a character into the uart rx queue.
int k_uart_tx_pin(KUARTNum uart)
Returns tx pin for specified uart interface.
Definition: uart.h:255