PER Firmware
Loading...
Searching...
No Matches
usart.h
Go to the documentation of this file.
1
12#ifndef _PHAL_USART_H_
13#define _PHAL_USART_H_
14
15// Includes
16#if defined(STM32F407xx)
17#define TOTAL_NUM_UART 8
18
19// Active Transfer list indexes (Add to this list if updating TOTAL_NUM_UART)
20#define USART1_ACTIVE_IDX 0
21#define USART2_ACTIVE_IDX 1
22#define USART3_ACTIVE_IDX 2
23#define USART4_ACTIVE_IDX 3
24#define USART5_ACTIVE_IDX 4
25#define USART6_ACTIVE_IDX 5
26#define USART7_ACTIVE_IDX 6
27#define USART8_ACTIVE_IDX 7
28
29#elif defined(STM32F732xx)
30#define TOTAL_NUM_UART 8
31
32// Active Transfer list indexes (Add to this list if updating TOTAL_NUM_UART)
33#define USART1_ACTIVE_IDX 0
34#define USART2_ACTIVE_IDX 1
35#define USART3_ACTIVE_IDX 2
36#define USART4_ACTIVE_IDX 3
37#define USART5_ACTIVE_IDX 4
38#define USART6_ACTIVE_IDX 5
39#define USART7_ACTIVE_IDX 6
40#define USART8_ACTIVE_IDX 7
41
42// Defines that mean the same thing but are phrased differently in stm32f4xx and stm32f7xx
43#define USART_BRR_DIV_Fraction_Pos USART_BRR_DIV_FRACTION_Pos
44#define USART_BRR_DIV_Mantissa_Pos USART_BRR_DIV_MANTISSA_Pos
45#endif
46
48#include "common/phal_F4_F7/phal_F4_F7.h"
49
50typedef uint32_t ptr_int;
51
52// See USART section of Family reference manual (RM0090 for F4, or RM0431 for F7) for configuration information
53
54// Enumerations
55// See Table 146 in RM. 0090
56// By enabling Parity, you sacrifice one bit from your total word length
57// (eg. if WORD_8 is selected, you now have 7 data bits and 1 parity bit)
58typedef enum {
59 PT_EVEN = 0b010,
60 PT_ODD = 0b100,
61 PT_NONE = 0b000
63
64typedef enum {
68
69typedef enum {
70 SB_ONE = 0b00,
71 SB_TWO = 0b01,
72 SB_HALF = 0b10,
73 SB_ONE_HALF = 0b11
75
82
83typedef enum {
84 OV_16 = 0,
85 OV_8 = 1
87
92
105
112
113// Structures
114typedef struct
115{
116 // Required parameters
117 uint32_t baud_rate;
118 word_length_t word_length;
119 stop_bits_t stop_bits;
120 parity_t parity;
121 hw_flow_ctl_t hw_flow_ctl;
122 ovsample_t ovsample;
123 obsample_t obsample;
125 uint8_t address;
127
128 // DMA configurations
131 USART_TypeDef* periph;
132
133 // Structs to communicate errors to user
137
138// Function Prototypes
139
148bool PHAL_initUSART(usart_init_t* handle, const uint32_t fck);
149
156void PHAL_usartTxBl(usart_init_t* handle, uint8_t* data, uint32_t len);
157
164void PHAL_usartRxBl(usart_init_t* handle, uint8_t* data, uint32_t len);
165
173bool PHAL_usartTxDma(usart_init_t* handle, uint16_t* data, uint32_t len);
174
183bool PHAL_usartRxDma(usart_init_t* handle, uint16_t* data, uint32_t len, bool cont);
184
191
199bool PHAL_usartTxBusy(usart_init_t* handle);
200
208bool PHAL_usartRxBusy(usart_init_t* handle);
209
219
220#ifdef STM32F407xx
221// 4,5,7,8 are UART, rest are USART
222#define PHAL_USART1_RXDMA_STREAM DMA2_Stream5
223#define PHAL_USART1_TXDMA_STREAM DMA2_Stream7
224
225#define PHAL_USART2_RXDMA_STREAM DMA1_Stream5
226#define PHAL_USART2_TXDMA_STREAM DMA1_Stream6
227
228#define PHAL_USART3_RXDMA_STREAM DMA1_Stream1
229#define PHAL_USART3_TXDMA_STREAM DMA1_Stream3
230
231#define PHAL_USART4_RXDMA_STREAM DMA1_Stream2 // UART
232#define PHAL_USART4_TXDMA_STREAM DMA1_Stream4
233
234#define PHAL_USART5_RXDMA_STREAM DMA1_Stream0 // UART
235#define PHAL_USART5_TXDMA_STREAM DMA1_Stream7
236
237#define PHAL_USART6_RXDMA_STREAM DMA2_Stream1
238#define PHAL_USART6_TXDMA_STREAM DMA2_Stream6
239
240#define PHAL_USART7_RXDMA_STREAM DMA1_Stream3 // UART
241#define PHAL_USART7_TXDMA_STREAM DMA1_Stream1
242
243#define PHAL_USART8_RXDMA_STREAM DMA1_Stream6 // UART
244#define PHAL_USART8_TXDMA_STREAM DMA1_Stream0
245// dm00031020 311
246#define _DEF_USART_RXDMA_CONFIG(rx_addr_, priority_, UXART, dmanum, streamnum, channum) \
247 { \
248 .periph_addr = (uint32_t)&((UXART)->DR), \
249 .mem_addr = (uint32_t)(rx_addr_), \
250 .tx_size = 1, \
251 .increment = false, \
252 .circular = false, \
253 .dir = 0b0, \
254 .mem_inc = true, \
255 .periph_inc = false, \
256 .mem_to_mem = false, \
257 .priority = (priority_), \
258 .mem_size = 0b00, \
259 .periph_size = 0b00, \
260 .tx_isr_en = true, \
261 .dma_chan_request = channum, \
262 .stream_idx = streamnum, \
263 .periph = DMA##dmanum, \
264 .stream = DMA##dmanum##_Stream##streamnum, \
265 }
266
267#define _DEF_USART_TXDMA_CONFIG(tx_addr_, priority_, UXART, dmanum, streamnum, channum) \
268 { \
269 .periph_addr = (uint32_t)&((UXART)->DR), \
270 .mem_addr = (uint32_t)(tx_addr_), \
271 .tx_size = 1, \
272 .increment = false, \
273 .circular = false, \
274 .dir = 0b1, \
275 .mem_inc = true, \
276 .periph_inc = false, \
277 .mem_to_mem = false, \
278 .priority = (priority_), \
279 .mem_size = 0b00, \
280 .periph_size = 0b00, \
281 .tx_isr_en = true, \
282 .dma_chan_request = channum, \
283 .stream_idx = streamnum, \
284 .periph = DMA##dmanum, \
285 .stream = DMA##dmanum##_Stream##streamnum, \
286 }
287
288#define USART1_RXDMA_CONT_CONFIG(a, p) _DEF_USART_RXDMA_CONFIG(a, p, USART1, 2, 5, 4)
289#define USART1_TXDMA_CONT_CONFIG(a, p) _DEF_USART_TXDMA_CONFIG(a, p, USART1, 2, 7, 4)
290#define USART2_RXDMA_CONT_CONFIG(a, p) _DEF_USART_RXDMA_CONFIG(a, p, USART2, 1, 5, 4)
291#define USART2_TXDMA_CONT_CONFIG(a, p) _DEF_USART_TXDMA_CONFIG(a, p, USART2, 1, 6, 4)
292#define USART3_RXDMA_CONT_CONFIG(a, p) _DEF_USART_RXDMA_CONFIG(a, p, USART3, 1, 1, 4)
293#define USART3_TXDMA_CONT_CONFIG(a, p) _DEF_USART_TXDMA_CONFIG(a, p, USART3, 1, 3, 4)
294#define USART4_RXDMA_CONT_CONFIG(a, p) _DEF_USART_RXDMA_CONFIG(a, p, UART4, 1, 2, 4)
295#define USART4_TXDMA_CONT_CONFIG(a, p) _DEF_USART_TXDMA_CONFIG(a, p, UART4, 1, 4, 4)
296#define USART5_RXDMA_CONT_CONFIG(a, p) _DEF_USART_RXDMA_CONFIG(a, p, UART5, 1, 0, 4)
297#define USART5_TXDMA_CONT_CONFIG(a, p) _DEF_USART_TXDMA_CONFIG(a, p, UART5, 1, 7, 4)
298#define USART6_RXDMA_CONT_CONFIG(a, p) _DEF_USART_RXDMA_CONFIG(a, p, USART6, 2, 1, 5)
299#define USART6_TXDMA_CONT_CONFIG(a, p) _DEF_USART_TXDMA_CONFIG(a, p, USART6, 2, 6, 5)
300
301#else
302
303#define USART1_RXDMA_CONT_CONFIG(rx_addr_, priority_) \
304 { \
305 .periph_addr = (uint32_t)&(USART1->RDR), \
306 .mem_addr = (uint32_t)(rx_addr_), \
307 .tx_size = 1, \
308 .increment = false, \
309 .circular = false, \
310 .dir = 0b0, \
311 .mem_inc = true, \
312 .periph_inc = false, \
313 .mem_to_mem = false, \
314 .priority = (priority_), \
315 .mem_size = 0b00, \
316 .periph_size = 0b00, \
317 .tx_isr_en = true, \
318 .dma_chan_request = 0b0100, \
319 .stream_idx = 5, \
320 .periph = DMA2, \
321 .stream = DMA2_Stream5}
322
323#define USART1_TXDMA_CONT_CONFIG(tx_addr_, priority_) \
324 { \
325 .periph_addr = (uint32_t)&(USART1->TDR), \
326 .mem_addr = (uint32_t)(tx_addr_), \
327 .tx_size = 1, \
328 .increment = false, \
329 .circular = false, \
330 .dir = 0b1, \
331 .mem_inc = true, \
332 .periph_inc = false, \
333 .mem_to_mem = false, \
334 .priority = (priority_), \
335 .mem_size = 0b00, \
336 .periph_size = 0b00, \
337 .tx_isr_en = true, \
338 .dma_chan_request = 0b0100, \
339 .stream_idx = 7, \
340 .periph = DMA2, \
341 .stream = DMA2_Stream7}
342
343#define USART4_RXDMA_CONT_CONFIG(rx_addr_, priority_) \
344 { \
345 .periph_addr = (uint32_t)&(UART4->RDR), \
346 .mem_addr = (uint32_t)(rx_addr_), \
347 .tx_size = 1, \
348 .increment = false, \
349 .circular = false, \
350 .dir = 0b0, \
351 .mem_inc = true, \
352 .periph_inc = false, \
353 .mem_to_mem = false, \
354 .priority = (priority_), \
355 .mem_size = 0b00, \
356 .periph_size = 0b00, \
357 .tx_isr_en = true, \
358 .dma_chan_request = 0b0100, \
359 .stream_idx = 2, \
360 .periph = DMA1, \
361 .stream = DMA1_Stream2}
362
363#define USART4_TXDMA_CONT_CONFIG(tx_addr_, priority_) \
364 { \
365 .periph_addr = (uint32_t)&(UART4->TDR), \
366 .mem_addr = (uint32_t)(tx_addr_), \
367 .tx_size = 1, \
368 .increment = false, \
369 .circular = false, \
370 .dir = 0b1, \
371 .mem_inc = true, \
372 .periph_inc = false, \
373 .mem_to_mem = false, \
374 .priority = (priority_), \
375 .mem_size = 0b00, \
376 .periph_size = 0b00, \
377 .tx_isr_en = true, \
378 .dma_chan_request = 0b0100, \
379 .stream_idx = 4, \
380 .periph = DMA1, \
381 .stream = DMA1_Stream4}
382
383#define UART5_RXDMA_CONT_CONFIG(rx_addr_, priority_) \
384 { \
385 .periph_addr = (uint32_t)&(UART5->RDR), \
386 .mem_addr = (uint32_t)(rx_addr_), \
387 .tx_size = 1, \
388 .increment = false, \
389 .circular = false, \
390 .dir = 0b0, \
391 .mem_inc = true, \
392 .periph_inc = false, \
393 .mem_to_mem = false, \
394 .priority = (priority_), \
395 .mem_size = 0b00, \
396 .periph_size = 0b00, \
397 .tx_isr_en = true, \
398 .dma_chan_request = 0b0100, \
399 .stream_idx = 0, \
400 .periph = DMA1, \
401 .stream = DMA1_Stream0}
402#define UART5_TXDMA_CONT_CONFIG(tx_addr_, priority_) \
403 { \
404 .periph_addr = (uint32_t)&(UART5->TDR), \
405 .mem_addr = (uint32_t)(tx_addr_), \
406 .tx_size = 1, \
407 .increment = false, \
408 .circular = false, \
409 .dir = 0b1, \
410 .mem_inc = true, \
411 .periph_inc = false, \
412 .mem_to_mem = false, \
413 .priority = (priority_), \
414 .mem_size = 0b00, \
415 .periph_size = 0b00, \
416 .tx_isr_en = true, \
417 .dma_chan_request = 0b0100, \
418 .stream_idx = 7, \
419 .periph = DMA1, \
420 .stream = DMA1_Stream7}
421#endif
422#endif
void PHAL_usartTxBl(usart_init_t *handle, uint8_t *data, uint32_t len)
TX using no DMA (blocks until complete)
Definition usart.c:150
word_length_t
Definition usart.h:64
@ WORD_8
8-Bit word length
Definition usart.h:65
@ WORD_9
9-Bit word length
Definition usart.h:66
bool PHAL_initUSART(usart_init_t *handle, const uint32_t fck)
Initialize a USART Peripheral with desired settings.
Definition usart.c:34
bool PHAL_disableContinousRxDMA(usart_init_t *handle)
Disables the Continous RX that was previously used.
Definition usart.c:337
bool PHAL_usartRxBusy(usart_init_t *handle)
Returns whether USART peripheral is currently receiving data.
Definition usart.c:396
void PHAL_usartRxBl(usart_init_t *handle, uint8_t *data, uint32_t len)
RX using no DMA (blocks until complete)
Definition usart.c:182
bool PHAL_usartTxBusy(usart_init_t *handle)
Returns whether USART peripheral is currently transmitting data.
Definition usart.c:273
void usart_recieve_complete_callback(usart_init_t *handle)
Callback function called immediately after reception of a USART RX message Uses USART IDLE line inter...
Definition usart.c:723
bool PHAL_usartRxDma(usart_init_t *handle, uint16_t *data, uint32_t len, bool cont)
Starts an rx using dma of a specific length.
Definition usart.c:277
hw_flow_ctl_t
Definition usart.h:76
@ CTS_RTS
Enable both CTS and RTS.
Definition usart.h:80
@ HW_DISABLE
Hardware flow control disable.
Definition usart.h:77
@ CTS
Enable Clear to send control.
Definition usart.h:78
@ RTS
Enable Request to send control.
Definition usart.h:79
obsample_t
Definition usart.h:88
@ OB_ENABLE
Enable one bit sampling.
Definition usart.h:90
@ OB_DISABLE
Disable one bit sampling.
Definition usart.h:89
parity_t
Definition usart.h:58
@ PT_ODD
Odd Parity.
Definition usart.h:60
@ PT_EVEN
Even Parity.
Definition usart.h:59
@ PT_NONE
Disable Parity bits.
Definition usart.h:61
stop_bits_t
Definition usart.h:69
@ SB_ONE_HALF
1.5 Stop bits
Definition usart.h:73
@ SB_TWO
Two Stop bits.
Definition usart.h:71
@ SB_HALF
One half stop bit.
Definition usart.h:72
@ SB_ONE
One Stop bit.
Definition usart.h:70
ovsample_t
Definition usart.h:83
@ OV_16
Oversample by 16.
Definition usart.h:84
@ OV_8
Oversample by 8.
Definition usart.h:85
bool PHAL_usartTxDma(usart_init_t *handle, uint16_t *data, uint32_t len)
Starts a tx using dma, use PHAL_usartTxDmaComplete to ensure the previous transmission is complete.
Definition usart.c:207
Definition dma.h:17
Definition usart.h:115
uint32_t baud_rate
Baud rate for communication.
Definition usart.h:117
word_length_t word_length
Word length for tx/rx (8 default)
Definition usart.h:118
volatile usart_tx_errors_t tx_errors
Any TX error flags set during transmission.
Definition usart.h:134
uint8_t usart_active_num
Index of USART in active array (see USARTx_ACTIVE_IDX)
Definition usart.h:126
dma_init_t * tx_dma_cfg
TX configuration.
Definition usart.h:129
stop_bits_t stop_bits
Number of stop bits to use (1 default)
Definition usart.h:119
volatile usart_rx_errors_t rx_errors
Any RX error flags set during reception.
Definition usart.h:135
ovsample_t ovsample
8x or 16x oversample (16x default)
Definition usart.h:122
bool wake_addr
Wake up when given a specific address.
Definition usart.h:124
USART_TypeDef * periph
USART Peripheral to be used.
Definition usart.h:131
uint8_t address
Address to wake up to when addr_mode is enabled.
Definition usart.h:125
obsample_t obsample
One bit sampling enable (off default)
Definition usart.h:123
hw_flow_ctl_t hw_flow_ctl
Special hardware modes (none default)
Definition usart.h:121
dma_init_t * rx_dma_cfg
RX configuration.
Definition usart.h:130
parity_t parity
Parity of communication (none default)
Definition usart.h:120
Definition usart.h:94
bool dma_direct_mode_error
DMA error while attempting to operate in direct mode.
Definition usart.h:102
bool framing_error
Unable to understand USART frame.
Definition usart.h:97
bool dma_transfer_error
DMA transfer error.
Definition usart.h:101
bool dma_fifo_overrun
DMA FIFO has been overrun - apparently this can be ignored on USART peripherals AS LONG AS YOU AREN'T...
Definition usart.h:103
bool noise_detected
Oversampling detected a possible bit flip due to noise in usart frame.
Definition usart.h:96
bool parity_error
USART Parity bit incorrect (Only when parity is enabled)
Definition usart.h:98
bool overrun
USART unable to parse data in time.
Definition usart.h:95
Definition usart.h:107
bool dma_direct_mode_error
DMA error while attempting to operate in direct mode.
Definition usart.h:109
bool dma_fifo_overrun
DMA FIFO has been overrun - apparently this can be ignored on USART peripherals AS LONG AS YOU AREN'T...
Definition usart.h:110
bool dma_transfer_error
DMA transfer error.
Definition usart.h:108