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#endif
29
32
33typedef uint32_t ptr_int;
34
35// See USART section of Family reference manual (RM0090 for F4, or RM0431 for F7) for configuration information
36
37// Enumerations
38// See Table 146 in RM. 0090
39// By enabling Parity, you sacrifice one bit from your total word length
40// (eg. if WORD_8 is selected, you now have 7 data bits and 1 parity bit)
41typedef enum {
42 PT_EVEN = 0b010,
43 PT_ODD = 0b100,
44 PT_NONE = 0b000
46
47typedef enum {
51
52typedef enum {
53 SB_ONE = 0b00,
54 SB_TWO = 0b01,
55 SB_HALF = 0b10,
56 SB_ONE_HALF = 0b11
58
65
66typedef enum {
67 OV_16 = 0,
68 OV_8 = 1
70
75
88
95
96// Structures
97typedef struct
98{
99 // Required parameters
100 uint32_t baud_rate;
108 uint8_t address;
110
111 // DMA configurations
114 USART_TypeDef* periph;
115
116 // Structs to communicate errors to user
120
121// Function Prototypes
122
131bool PHAL_initUSART(usart_init_t* handle, const uint32_t fck);
132
139void PHAL_usartTxBl(usart_init_t* handle, uint8_t* data, uint32_t len);
140
147void PHAL_usartRxBl(usart_init_t* handle, uint8_t* data, uint32_t len);
148
156bool PHAL_usartTxDma(usart_init_t* handle, uint16_t* data, uint32_t len);
157
166bool PHAL_usartRxDma(usart_init_t* handle, uint16_t* data, uint32_t len, bool cont);
167
174
182bool PHAL_usartTxBusy(usart_init_t* handle);
183
191bool PHAL_usartRxBusy(usart_init_t* handle);
192
202
203#ifdef STM32F407xx
204// 4,5,7,8 are UART, rest are USART
205#define PHAL_USART1_RXDMA_STREAM DMA2_Stream5
206#define PHAL_USART1_TXDMA_STREAM DMA2_Stream7
207
208#define PHAL_USART2_RXDMA_STREAM DMA1_Stream5
209#define PHAL_USART2_TXDMA_STREAM DMA1_Stream6
210
211#define PHAL_USART3_RXDMA_STREAM DMA1_Stream1
212#define PHAL_USART3_TXDMA_STREAM DMA1_Stream3
213
214#define PHAL_USART4_RXDMA_STREAM DMA1_Stream2 // UART
215#define PHAL_USART4_TXDMA_STREAM DMA1_Stream4
216
217#define PHAL_USART5_RXDMA_STREAM DMA1_Stream0 // UART
218#define PHAL_USART5_TXDMA_STREAM DMA1_Stream7
219
220#define PHAL_USART6_RXDMA_STREAM DMA2_Stream1
221#define PHAL_USART6_TXDMA_STREAM DMA2_Stream6
222
223#define PHAL_USART7_RXDMA_STREAM DMA1_Stream3 // UART
224#define PHAL_USART7_TXDMA_STREAM DMA1_Stream1
225
226#define PHAL_USART8_RXDMA_STREAM DMA1_Stream6 // UART
227#define PHAL_USART8_TXDMA_STREAM DMA1_Stream0
228// dm00031020 311
229#define _DEF_USART_RXDMA_CONFIG(rx_addr_, priority_, UXART, dmanum, streamnum, channum) \
230 { \
231 .periph_addr = (uint32_t)&((UXART)->DR), \
232 .mem_addr = (uint32_t)(rx_addr_), \
233 .tx_size = 1, \
234 .increment = false, \
235 .circular = false, \
236 .dir = 0b0, \
237 .mem_inc = true, \
238 .periph_inc = false, \
239 .mem_to_mem = false, \
240 .priority = (priority_), \
241 .mem_size = 0b00, \
242 .periph_size = 0b00, \
243 .tx_isr_en = true, \
244 .dma_chan_request = channum, \
245 .stream_idx = streamnum, \
246 .periph = DMA##dmanum, \
247 .stream = DMA##dmanum##_Stream##streamnum, \
248 }
249
250#define _DEF_USART_TXDMA_CONFIG(tx_addr_, priority_, UXART, dmanum, streamnum, channum) \
251 { \
252 .periph_addr = (uint32_t)&((UXART)->DR), \
253 .mem_addr = (uint32_t)(tx_addr_), \
254 .tx_size = 1, \
255 .increment = false, \
256 .circular = false, \
257 .dir = 0b1, \
258 .mem_inc = true, \
259 .periph_inc = false, \
260 .mem_to_mem = false, \
261 .priority = (priority_), \
262 .mem_size = 0b00, \
263 .periph_size = 0b00, \
264 .tx_isr_en = true, \
265 .dma_chan_request = channum, \
266 .stream_idx = streamnum, \
267 .periph = DMA##dmanum, \
268 .stream = DMA##dmanum##_Stream##streamnum, \
269 }
270
271#define USART1_RXDMA_CONT_CONFIG(a, p) _DEF_USART_RXDMA_CONFIG(a, p, USART1, 2, 5, 4)
272#define USART1_TXDMA_CONT_CONFIG(a, p) _DEF_USART_TXDMA_CONFIG(a, p, USART1, 2, 7, 4)
273#define USART2_RXDMA_CONT_CONFIG(a, p) _DEF_USART_RXDMA_CONFIG(a, p, USART2, 1, 5, 4)
274#define USART2_TXDMA_CONT_CONFIG(a, p) _DEF_USART_TXDMA_CONFIG(a, p, USART2, 1, 6, 4)
275#define USART3_RXDMA_CONT_CONFIG(a, p) _DEF_USART_RXDMA_CONFIG(a, p, USART3, 1, 1, 4)
276#define USART3_TXDMA_CONT_CONFIG(a, p) _DEF_USART_TXDMA_CONFIG(a, p, USART3, 1, 3, 4)
277#define USART4_RXDMA_CONT_CONFIG(a, p) _DEF_USART_RXDMA_CONFIG(a, p, UART4, 1, 2, 4)
278#define USART4_TXDMA_CONT_CONFIG(a, p) _DEF_USART_TXDMA_CONFIG(a, p, UART4, 1, 4, 4)
279#define USART5_RXDMA_CONT_CONFIG(a, p) _DEF_USART_RXDMA_CONFIG(a, p, UART5, 1, 0, 4)
280#define USART5_TXDMA_CONT_CONFIG(a, p) _DEF_USART_TXDMA_CONFIG(a, p, UART5, 1, 7, 4)
281#define USART6_RXDMA_CONT_CONFIG(a, p) _DEF_USART_RXDMA_CONFIG(a, p, USART6, 2, 1, 5)
282#define USART6_TXDMA_CONT_CONFIG(a, p) _DEF_USART_TXDMA_CONFIG(a, p, USART6, 2, 6, 5)
283
284#else
285
286#define USART1_RXDMA_CONT_CONFIG(rx_addr_, priority_) \
287 { \
288 .periph_addr = (uint32_t)&(USART1->RDR), \
289 .mem_addr = (uint32_t)(rx_addr_), \
290 .tx_size = 1, \
291 .increment = false, \
292 .circular = false, \
293 .dir = 0b0, \
294 .mem_inc = true, \
295 .periph_inc = false, \
296 .mem_to_mem = false, \
297 .priority = (priority_), \
298 .mem_size = 0b00, \
299 .periph_size = 0b00, \
300 .tx_isr_en = true, \
301 .dma_chan_request = 0b0100, \
302 .stream_idx = 5, \
303 .periph = DMA2, \
304 .stream = DMA2_Stream5}
305
306#define USART1_TXDMA_CONT_CONFIG(tx_addr_, priority_) \
307 { \
308 .periph_addr = (uint32_t)&(USART1->TDR), \
309 .mem_addr = (uint32_t)(tx_addr_), \
310 .tx_size = 1, \
311 .increment = false, \
312 .circular = false, \
313 .dir = 0b1, \
314 .mem_inc = true, \
315 .periph_inc = false, \
316 .mem_to_mem = false, \
317 .priority = (priority_), \
318 .mem_size = 0b00, \
319 .periph_size = 0b00, \
320 .tx_isr_en = true, \
321 .dma_chan_request = 0b0100, \
322 .stream_idx = 7, \
323 .periph = DMA2, \
324 .stream = DMA2_Stream7}
325
326#define USART4_RXDMA_CONT_CONFIG(rx_addr_, priority_) \
327 { \
328 .periph_addr = (uint32_t)&(UART4->RDR), \
329 .mem_addr = (uint32_t)(rx_addr_), \
330 .tx_size = 1, \
331 .increment = false, \
332 .circular = false, \
333 .dir = 0b0, \
334 .mem_inc = true, \
335 .periph_inc = false, \
336 .mem_to_mem = false, \
337 .priority = (priority_), \
338 .mem_size = 0b00, \
339 .periph_size = 0b00, \
340 .tx_isr_en = true, \
341 .dma_chan_request = 0b0100, \
342 .stream_idx = 2, \
343 .periph = DMA1, \
344 .stream = DMA1_Stream2}
345
346#define USART4_TXDMA_CONT_CONFIG(tx_addr_, priority_) \
347 { \
348 .periph_addr = (uint32_t)&(UART4->TDR), \
349 .mem_addr = (uint32_t)(tx_addr_), \
350 .tx_size = 1, \
351 .increment = false, \
352 .circular = false, \
353 .dir = 0b1, \
354 .mem_inc = true, \
355 .periph_inc = false, \
356 .mem_to_mem = false, \
357 .priority = (priority_), \
358 .mem_size = 0b00, \
359 .periph_size = 0b00, \
360 .tx_isr_en = true, \
361 .dma_chan_request = 0b0100, \
362 .stream_idx = 4, \
363 .periph = DMA1, \
364 .stream = DMA1_Stream4}
365
366#define UART5_RXDMA_CONT_CONFIG(rx_addr_, priority_) \
367 { \
368 .periph_addr = (uint32_t)&(UART5->RDR), \
369 .mem_addr = (uint32_t)(rx_addr_), \
370 .tx_size = 1, \
371 .increment = false, \
372 .circular = false, \
373 .dir = 0b0, \
374 .mem_inc = true, \
375 .periph_inc = false, \
376 .mem_to_mem = false, \
377 .priority = (priority_), \
378 .mem_size = 0b00, \
379 .periph_size = 0b00, \
380 .tx_isr_en = true, \
381 .dma_chan_request = 0b0100, \
382 .stream_idx = 0, \
383 .periph = DMA1, \
384 .stream = DMA1_Stream0}
385#define UART5_TXDMA_CONT_CONFIG(tx_addr_, priority_) \
386 { \
387 .periph_addr = (uint32_t)&(UART5->TDR), \
388 .mem_addr = (uint32_t)(tx_addr_), \
389 .tx_size = 1, \
390 .increment = false, \
391 .circular = false, \
392 .dir = 0b1, \
393 .mem_inc = true, \
394 .periph_inc = false, \
395 .mem_to_mem = false, \
396 .priority = (priority_), \
397 .mem_size = 0b00, \
398 .periph_size = 0b00, \
399 .tx_isr_en = true, \
400 .dma_chan_request = 0b0100, \
401 .stream_idx = 7, \
402 .periph = DMA1, \
403 .stream = DMA1_Stream7}
404#endif
405#endif
void PHAL_usartTxBl(usart_init_t *handle, uint8_t *data, uint32_t len)
TX using no DMA (blocks until complete)
Definition usart.c:143
word_length_t
Definition usart.h:47
@ WORD_8
8-Bit word length
Definition usart.h:48
@ WORD_9
9-Bit word length
Definition usart.h:49
bool PHAL_initUSART(usart_init_t *handle, const uint32_t fck)
Initialize a USART Peripheral with desired settings.
Definition usart.c:39
bool PHAL_disableContinousRxDMA(usart_init_t *handle)
Disables the Continous RX that was previously used.
Definition usart.c:278
uint32_t ptr_int
Definition usart.h:33
bool PHAL_usartRxBusy(usart_init_t *handle)
Returns whether USART peripheral is currently receiving data.
Definition usart.c:323
void PHAL_usartRxBl(usart_init_t *handle, uint8_t *data, uint32_t len)
RX using no DMA (blocks until complete)
Definition usart.c:163
bool PHAL_usartTxBusy(usart_init_t *handle)
Returns whether USART peripheral is currently transmitting data.
Definition usart.c:228
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:650
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:232
hw_flow_ctl_t
Definition usart.h:59
@ CTS_RTS
Enable both CTS and RTS.
Definition usart.h:63
@ HW_DISABLE
Hardware flow control disable.
Definition usart.h:60
@ CTS
Enable Clear to send control.
Definition usart.h:61
@ RTS
Enable Request to send control.
Definition usart.h:62
obsample_t
Definition usart.h:71
@ OB_ENABLE
Enable one bit sampling.
Definition usart.h:73
@ OB_DISABLE
Disable one bit sampling.
Definition usart.h:72
parity_t
Definition usart.h:41
@ PT_ODD
Odd Parity.
Definition usart.h:43
@ PT_EVEN
Even Parity.
Definition usart.h:42
@ PT_NONE
Disable Parity bits.
Definition usart.h:44
stop_bits_t
Definition usart.h:52
@ SB_ONE_HALF
1.5 Stop bits
Definition usart.h:56
@ SB_TWO
Two Stop bits.
Definition usart.h:54
@ SB_HALF
One half stop bit.
Definition usart.h:55
@ SB_ONE
One Stop bit.
Definition usart.h:53
ovsample_t
Definition usart.h:66
@ OV_16
Oversample by 16.
Definition usart.h:67
@ OV_8
Oversample by 8.
Definition usart.h:68
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:179
Definition dma.h:17
Definition usart.h:98
uint32_t baud_rate
Baud rate for communication.
Definition usart.h:100
word_length_t word_length
Word length for tx/rx (8 default)
Definition usart.h:101
volatile usart_tx_errors_t tx_errors
Any TX error flags set during transmission.
Definition usart.h:117
uint8_t usart_active_num
Index of USART in active array (see USARTx_ACTIVE_IDX)
Definition usart.h:109
stop_bits_t stop_bits
Number of stop bits to use (1 default)
Definition usart.h:102
volatile usart_rx_errors_t rx_errors
Any RX error flags set during reception.
Definition usart.h:118
ovsample_t ovsample
8x or 16x oversample (16x default)
Definition usart.h:105
dma_init_t * rx_dma_cfg
RX configuration.
Definition usart.h:113
bool wake_addr
Wake up when given a specific address.
Definition usart.h:107
uint8_t address
Address to wake up to when addr_mode is enabled.
Definition usart.h:108
obsample_t obsample
One bit sampling enable (off default)
Definition usart.h:106
USART_TypeDef * periph
USART Peripheral to be used.
Definition usart.h:114
dma_init_t * tx_dma_cfg
TX configuration.
Definition usart.h:112
hw_flow_ctl_t hw_flow_ctl
Special hardware modes (none default)
Definition usart.h:104
parity_t parity
Parity of communication (none default)
Definition usart.h:103
Definition usart.h:77
bool dma_direct_mode_error
DMA error while attempting to operate in direct mode.
Definition usart.h:85
bool framing_error
Unable to understand USART frame.
Definition usart.h:80
bool dma_transfer_error
DMA transfer error.
Definition usart.h:84
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:86
bool noise_detected
Oversampling detected a possible bit flip due to noise in usart frame.
Definition usart.h:79
bool parity_error
USART Parity bit incorrect (Only when parity is enabled)
Definition usart.h:81
bool overrun
USART unable to parse data in time.
Definition usart.h:78
Definition usart.h:90
bool dma_direct_mode_error
DMA error while attempting to operate in direct mode.
Definition usart.h:92
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:93
bool dma_transfer_error
DMA transfer error.
Definition usart.h:91