APM:Libraries
usart.h
Go to the documentation of this file.
1 #ifndef _USART_H
2 #define _USART_H
3 
4 #include <hal_types.h>
5 #include "ring_buffer.h"
6 
7 /*
8  * Devices
9  */
10 
11 #ifndef USART_RX_BUF_SIZE
12 #define USART_RX_BUF_SIZE 512
13 #endif
14 
15 #ifndef USART_TX_BUF_SIZE
16 #define USART_TX_BUF_SIZE 1024
17 #endif
18 
19 typedef void (* usart_cb)();
20 
21 typedef struct usart_state {
23 
24  uint8_t txbusy;
25 
28 
29  uint8_t is_used; // flag that USART is used
30 
31 } usart_state;
32 
34 typedef struct usart_dev {
35  USART_TypeDef* USARTx;
36  uint32_t clk;
37  IRQn_Type irq;
38  uint8_t rx_pin;
39  uint8_t tx_pin;
40  uint8_t gpio_af;
44 } usart_dev;
45 
46 
47 extern const usart_dev * const UARTS[];
48 
49 #ifdef __cplusplus
50  extern "C" {
51 #endif
52 
53 extern const usart_dev * const _USART1;
54 extern const usart_dev * const _USART2;
55 extern const usart_dev * const _USART3;
56 extern const usart_dev * const _UART4;
57 extern const usart_dev * const _UART5;
58 extern const usart_dev * const _USART6;
59 
60 #define USART_F_RXNE 0x20
61 #define USART_F_TXE 0x80
62 #define USART_F_ORE 0x8
63 
64 #define USART_MASK_IDLEIE 0x10
65 #define USART_MASK_RXNEIE 0x20
66 #define USART_MASK_TCEIE 0x40
67 #define USART_MASK_TXEIE 0x80
68 #define USART_MASK_PEIE 0x100
69 
70 #define USART_MASK2_LBDIE 0x40
71 
72 #define USART_MASK3_CTSIE 0x400
73 #define USART_MASK3_EIE 0x1
74 
75 #define UART_Mode_Rx (0x0004)
76 #define UART_Mode_Tx (0x0008)
77 
78 #define UART_HardwareFlowControl_None (0x0000)
79 #define UART_HardwareFlowControl_RTS (0x0100)
80 #define UART_HardwareFlowControl_CTS (0x0200)
81 #define UART_HardwareFlowControl_RTS_CTS (0x0300)
82 
83 #define UART_Word_8b ((uint16_t)0x0000)
84 #define UART_Word_9b ((uint16_t)0x1000)
85 
90 void usart_init(const usart_dev *dev);
91 
92 
93 /*
94  USART_Word_Length
95  -----------------
96  USART_WordLength_8b
97  USART_WordLength_9b
98 
99  USART_Stop_Bits
100  ---------------
101  USART_StopBits_1
102  USART_StopBits_0_5
103  USART_StopBits_2
104  USART_StopBits_1_5
105 
106  USART_Parity
107  ------------
108  USART_Parity_No
109  USART_Parity_Even
110  USART_Parity_Odd
111 
112  USART_Mode
113  ----------
114  USART_Mode_Rx
115  USART_Mode_Tx
116 
117  USART_Hardware_Flow_Control
118  ---------------------------
119  USART_HardwareFlowControl_None
120  USART_HardwareFlowControl_RTS
121  USART_HardwareFlowControl_CTS
122  USART_HardwareFlowControl_RTS_CTS
123 */
124 
136 void usart_setup(const usart_dev *dev,
137  uint32_t baudRate,
138  uint16_t wordLength,
139  uint16_t stopBits,
140  uint16_t parity,
141  uint16_t mode,
142  uint16_t hardwareFlowControl);
143 
155 static inline void usart_enable(const usart_dev *dev)
156 {
157  dev->state->is_used=true;
158 
159  /* Check the parameters */
160  assert_param(IS_USART_ALL_PERIPH(dev->USARTx));
161 
162  /* Enable USART */
163 // USART_Cmd(dev->USARTx, ENABLE);
164 
165  dev->USARTx->CR1 |= USART_CR1_UE; /* Enable the selected USART by setting the UE bit in the CR1 register */
166 
167 }
168 
169 
170 static inline uint8_t usart_is_used(const usart_dev *dev)
171 {
172  return dev->state->is_used;
173 }
174 
175 
180 static inline void usart_disable(const usart_dev *dev){
181  /* Disable the selected USART by clearing the UE bit in the CR1 register */
182  dev->USARTx->CR1 &= (uint16_t)~((uint16_t)USART_CR1_UE);
183 
184  /* Clean up buffer */
185  dev->state->is_used=false;
186 }
187 
192 void usart_foreach(void (*fn)(const usart_dev*));
193 
194 
195 static inline uint32_t usart_txfifo_nbytes(const usart_dev *dev) {
196  return rb_full_count(dev->txrb);
197 }
198 static inline uint32_t usart_txfifo_freebytes(const usart_dev *dev) {
199  uint32_t used = rb_full_count(dev->txrb);
200  if(used >= USART_TX_BUF_SIZE/2) return 0; // leave half for dirty writes without check - thanks s_s
201  return (USART_TX_BUF_SIZE/2-used);
202 }
206 static inline void usart_disable_all(void) {
208 }
209 
217 uint32_t usart_tx(const usart_dev *dev, const uint8_t *buf, uint32_t len);
218 
229 void usart_putudec(const usart_dev *dev, uint32_t val);
230 
240 static inline uint32_t usart_putc(const usart_dev* dev, uint8_t bt) {
241  //uint32_t ret=0;
242  //uint32_t cnt=0;
243  //if (!usart_tx(dev, &byte, 1))
244  //;
245  return usart_tx(dev, &bt, 1);
246 }
247 
260 static inline void usart_putstr(const usart_dev *dev, const char* str) {
261  uint32_t i = 0;
262  uint8_t c;
263  while ( (c=str[i++]) ) usart_putc(dev, c);
264 
265 }
266 
277 static inline uint8_t usart_getc(const usart_dev *dev) {
278  return rb_remove(dev->rxrb);
279 }
280 
286 static inline uint32_t usart_data_available(const usart_dev *dev) {
287  return rb_full_count(dev->rxrb);
288 }
289 
290 
295 static inline void usart_reset_rx(const usart_dev *dev) {
296  rb_reset(dev->rxrb);
297 }
298 
299 static inline void usart_reset_tx(const usart_dev *dev) {
300  rb_reset(dev->txrb);
301  dev->state->txbusy = 0;
302 }
303 
304 static inline void usart_set_callback(const usart_dev *dev, Handler cb)
305 {
306  dev->state->callback = cb;
307 }
308 
309 
310 void USART1_IRQHandler(void);
311 void USART2_IRQHandler(void);
312 void USART3_IRQHandler(void);
313 void USART4_IRQHandler(void);
314 void USART5_IRQHandler(void);
315 void USART6_IRQHandler(void);
316 
317 void UART4_IRQHandler(void);
318 void UART5_IRQHandler(void);
319 
320 
321 #ifdef __cplusplus
322  }
323 #endif
324 
325 #endif
static uint32_t usart_data_available(const usart_dev *dev)
Return the amount of data available in a serial port&#39;s RX buffer.
Definition: usart.h:286
void USART5_IRQHandler(void)
static void usart_disable_all(void)
Disable all serial ports.
Definition: usart.h:206
struct usart_dev usart_dev
uint8_t tx_pin
Definition: usart.h:39
static uint32_t usart_txfifo_nbytes(const usart_dev *dev)
Definition: usart.h:195
uint8_t gpio_af
Definition: usart.h:40
IRQn_Type irq
Definition: usart.h:37
void usart_init(const usart_dev *dev)
Initialize a serial port.
Definition: usart.c:187
#define assert_param(expr)
static void usart_enable(const usart_dev *dev)
Enable a serial port.
Definition: usart.h:155
uint8_t tx_buf[USART_TX_BUF_SIZE]
Definition: usart.h:27
static uint16_t rb_full_count(ring_buffer *rb)
Return the number of elements stored in the ring buffer.
Definition: ring_buffer.h:84
static uint8_t rb_remove(ring_buffer *rb)
Remove and return the first item from a ring buffer.
Definition: ring_buffer.h:128
void UART5_IRQHandler(void)
void USART2_IRQHandler(void)
void UART4_IRQHandler(void)
const usart_dev *const _UART4
#define USART_RX_BUF_SIZE
Definition: usart.h:12
const usart_dev *const _USART2
const usart_dev *const UARTS[]
Definition: usart.c:136
ring_buffer * rxrb
Definition: usart.h:42
const usart_dev *const _USART1
Definition: usart.c:34
Simple circular buffer.
void(* usart_cb)()
Definition: usart.h:19
usart_state * state
Definition: usart.h:41
void USART4_IRQHandler(void)
static AP_HAL::OwnPtr< AP_HAL::Device > dev
Definition: ICM20789.cpp:16
static uint32_t usart_putc(const usart_dev *dev, uint8_t bt)
Transmit one character on a serial port.
Definition: usart.h:240
void usart_putudec(const usart_dev *dev, uint32_t val)
Transmit an unsigned integer to the specified serial port in decimal format.
Definition: usart.c:282
static void usart_set_callback(const usart_dev *dev, Handler cb)
Definition: usart.h:304
#define USART_TX_BUF_SIZE
Definition: usart.h:16
uint8_t rx_buf[USART_RX_BUF_SIZE]
Definition: usart.h:26
void USART3_IRQHandler(void)
Definition: usart.c:376
void USART1_IRQHandler(void)
Definition: usart.c:362
static uint8_t usart_is_used(const usart_dev *dev)
Definition: usart.h:170
USART_TypeDef * USARTx
Definition: usart.h:35
void usart_foreach(void(*fn)(const usart_dev *))
Call a function on each USART.
Definition: usart.c:163
const usart_dev *const _USART3
Definition: usart.c:74
static void usart_disable(const usart_dev *dev)
Turn off a serial port.
Definition: usart.h:180
const usart_dev *const _UART5
static void usart_reset_rx(const usart_dev *dev)
Discard the contents of a serial port&#39;s RX buffer.
Definition: usart.h:295
void usart_setup(const usart_dev *dev, uint32_t baudRate, uint16_t wordLength, uint16_t stopBits, uint16_t parity, uint16_t mode, uint16_t hardwareFlowControl)
Configure a serial port&#39;s baud rate.
Definition: usart.c:199
uint32_t usart_tx(const usart_dev *dev, const uint8_t *buf, uint32_t len)
Nonblocking USART transmit.
Definition: usart.c:258
uint8_t is_used
Definition: usart.h:29
static void rb_reset(ring_buffer *rb)
Discard all items from a ring buffer.
Definition: ring_buffer.h:186
Handler callback
Definition: usart.h:22
uint64_t Handler
Definition: hal_types.h:19
void USART6_IRQHandler(void)
struct usart_state usart_state
ring_buffer * txrb
Definition: usart.h:43
static uint8_t usart_getc(const usart_dev *dev)
Read one character from a serial port.
Definition: usart.h:277
uint8_t txbusy
Definition: usart.h:24
static void usart_reset_tx(const usart_dev *dev)
Definition: usart.h:299
static void usart_putstr(const usart_dev *dev, const char *str)
Transmit a character string on a serial port.
Definition: usart.h:260
static uint32_t usart_txfifo_freebytes(const usart_dev *dev)
Definition: usart.h:198
uint32_t clk
Definition: usart.h:36
uint8_t rx_pin
Definition: usart.h:38
const usart_dev *const _USART6