PER Firmware
Loading...
Searching...
No Matches
usart.h
1#ifndef _PHAL_USART_H_
2#define _PHAL_USART_H_
3
4// Includes
5#define TOTAL_NUM_UART 8
6
7// Active Transfer list indexes (Add to this list if updating TOTAL_NUM_UART)
8#define USART1_ACTIVE_IDX 0
9#define USART2_ACTIVE_IDX 1
10#define USART3_ACTIVE_IDX 2
11#define UART4_ACTIVE_IDX 3
12#define LPUART1_ACTIVE_IDX 4
13#define USART5_ACTIVE_IDX 5
14#define USART6_ACTIVE_IDX 6
15#define USART7_ACTIVE_IDX 7
16
18#include "common/phal_G4/phal_G4.h"
19
20typedef uint32_t ptr_int;
21
22// Enumerations
23// See USART section of Family reference manual for configuration information
24
25typedef enum {
26 PT_EVEN = 0b010,
27 PT_ODD = 0b100,
28 PT_NONE = 0b000
29} parity_t;
30
31typedef enum {
32 WORD_7,
33 WORD_8,
34 WORD_9,
36
37typedef enum {
38 SB_ONE = 0b00,
39 SB_TWO = 0b01,
40 SB_HALF = 0b10,
41 SB_ONE_HALF = 0b11
43
44typedef enum {
46 CTS,
47 RTS,
48 CTS_RTS
50
51typedef enum {
52 OV_16 = 0,
53 OV_8 = 1
55
56typedef enum {
60
61typedef struct {
62 bool overrun;
63 bool noise_detected;
64 bool framing_error;
65 bool parity_error;
66
67 // DMA Errors
68 bool dma_transfer_error;
69 bool dma_direct_mode_error;
70 bool dma_fifo_overrun;
72
73typedef struct {
74 bool dma_transfer_error;
75 bool dma_direct_mode_error;
76 bool dma_fifo_overrun;
78
79// Structures
80typedef struct {
81 // Required parameters
82 uint32_t baud_rate;
83 word_length_t word_length;
84 stop_bits_t stop_bits;
85 parity_t parity;
86 hw_flow_ctl_t hw_flow_ctl;
87 ovsample_t ovsample;
88 obsample_t obsample;
89 bool wake_addr;
90 uint8_t address;
91 uint8_t usart_active_num;
92
93 // DMA configurations
94 dma_init_t* tx_dma_cfg;
95 dma_init_t* rx_dma_cfg;
96 USART_TypeDef* periph;
97
98 // Structs to communicate errors to user
99 volatile usart_tx_errors_t tx_errors;
100 volatile usart_rx_errors_t rx_errors;
102
103// Function Prototypes
104
113bool PHAL_initUSART(usart_init_t* handle, const uint32_t fck);
114
122void PHAL_usartTxBl(usart_init_t* handle, uint8_t* data, uint32_t len);
123
131void PHAL_usartRxBl(usart_init_t* handle, uint8_t* data, uint32_t len);
132
140bool PHAL_usartTxDma(usart_init_t* handle, uint8_t* data, uint32_t len);
141
150bool PHAL_usartRxDma(usart_init_t* handle, uint8_t* data, uint32_t len, bool cont);
151
158
166volatile bool PHAL_usartTxBusy(usart_init_t* handle);
167
175bool PHAL_usartRxBusy(usart_init_t* handle);
176
185extern void usart_receive_complete_callback(usart_init_t* handle);
186
187
188#define _DEF_USART_RXDMA_CONFIG(rx_addr_, priority_, USARTx, dma_num, channel_num, req_id) \
189 { \
190 .periph_addr = (uint32_t)&((USARTx)->RDR), \
191 .mem_addr = (uint32_t)(rx_addr_), \
192 .tx_size = 1, \
193 .circular = false, \
194 .mem_inc = true, \
195 .periph_inc = false, \
196 .mem_to_mem = false, \
197 .priority = (priority_), \
198 .dir = 0, \
199 .mem_size = DMA_SIZE_8BIT, \
200 .periph_size = DMA_SIZE_8BIT, \
201 .tx_isr_en = true, \
202 .mux_request = (req_id), \
203 .channel_idx = (channel_num), \
204 .periph = DMA##dma_num, \
205 }
206
207#define _DEF_USART_TXDMA_CONFIG(tx_addr_, priority_, USARTx, dma_num, channel_num, req_id) \
208 { \
209 .periph_addr = (uint32_t)&((USARTx)->TDR), \
210 .mem_addr = (uint32_t)(tx_addr_), \
211 .tx_size = 1, \
212 .circular = false, \
213 .mem_inc = true, \
214 .periph_inc = false, \
215 .mem_to_mem = false, \
216 .priority = (priority_), \
217 .dir = 1, \
218 .mem_size = DMA_SIZE_8BIT, \
219 .periph_size = DMA_SIZE_8BIT, \
220 .tx_isr_en = true, \
221 .mux_request = (req_id), \
222 .channel_idx = (channel_num), \
223 .periph = DMA##dma_num, \
224 }
225
226#define USART1_RXDMA_CONT_CONFIG(a, p) \
227 _DEF_USART_RXDMA_CONFIG(a, p, USART1, 1, 5, DMA_REQUEST_USART1_RX)
228#define USART1_TXDMA_CONT_CONFIG(a, p) \
229 _DEF_USART_TXDMA_CONFIG(a, p, USART1, 1, 7, DMA_REQUEST_USART1_TX)
230
231#define USART2_RXDMA_CONT_CONFIG(a, p) \
232 _DEF_USART_RXDMA_CONFIG(a, p, USART2, 1, 3, DMA_REQUEST_USART2_RX)
233#define USART2_TXDMA_CONT_CONFIG(a, p) \
234 _DEF_USART_TXDMA_CONFIG(a, p, USART2, 1, 4, DMA_REQUEST_USART2_TX)
235
236#define USART3_RXDMA_CONT_CONFIG(a, p) \
237 _DEF_USART_RXDMA_CONFIG(a, p, USART3, 1, 1, DMA_REQUEST_USART3_RX)
238#define USART3_TXDMA_CONT_CONFIG(a, p) \
239 _DEF_USART_TXDMA_CONFIG(a, p, USART3, 1, 2, DMA_REQUEST_USART3_TX)
240
241#define UART4_RXDMA_CONT_CONFIG(a, p) \
242 _DEF_USART_RXDMA_CONFIG(a, p, UART4, 2, 5, DMA_REQUEST_UART4_RX)
243#define UART4_TXDMA_CONT_CONFIG(a, p) \
244 _DEF_USART_TXDMA_CONFIG(a, p, UART4, 2, 3, DMA_REQUEST_UART4_TX)
245
246#define LPUART1_RXDMA_CONT_CONFIG(a, p) \
247 _DEF_USART_RXDMA_CONFIG(a, p, LPUART1, 2, 6, DMA_REQUEST_LPUART1_RX)
248#define LPUART1_TXDMA_CONT_CONFIG(a, p) \
249 _DEF_USART_TXDMA_CONFIG(a, p, LPUART1, 2, 7, DMA_REQUEST_LPUART1_TX)
250
251#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
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
Definition usart.h:94
Definition usart.h:107