brazos-trapezoidal
CustomUart.c
Go to the documentation of this file.
1
14//-------------------------------------- Include Files ----------------------------------------------------------------
15
16#include "CustomUart.h"
17
18//-------------------------------------- PUBLIC (Variables) -----------------------------------------------------------
19
20//-------------------------------------- PRIVATE (Variables, Constants & Defines) -------------------------------------
21
22typedef struct
23{
24 bool b_initialized;
25 USART_Type *p_base_reg;
26 USART_SignalEvent_t cb_event;
27 USART_PARITY_Enum parity;
28 uint32_t status;
29 bool b_block_rx;
30 uint8_t *p_rx_data;
31 uint16_t rx_expected;
32 uint16_t rx_bytes;
33 bool b_block_tx;
34 const uint8_t *p_tx_data;
35 uint16_t tx_expected;
36 uint16_t tx_bytes;
38
39static usart_device_t customusart0 =
40{
41 .b_initialized = false,
42 .p_base_reg = USART0,
43 .cb_event = NULL,
44 .parity = UART_PARITY_NONE,
45 .status = DRV_OK,
46 .b_block_rx = true,
47 .p_rx_data = NULL,
48 .rx_expected = 0,
49 .rx_bytes = 0,
50 .b_block_tx = true,
51 .p_tx_data = NULL,
52 .tx_expected = 0,
53 .tx_bytes = 0
54};
55
56//-------------------------------------- PRIVATE (Function Prototypes) ------------------------------------------------
57
58uint16_t customAddParity(usart_device_pt pUart, uint8_t data);
59bool customCheckParity(usart_device_pt pUart, uint16_t data);
60
61//=====================================================================================================================
62//-------------------------------------- Public Functions -------------------------------------------------------------
63//=====================================================================================================================
64
66uint32_t customUart_initialize( UsartInstance_Enum e_uart, USART_SignalEvent_t cb_event, void **pp_uart )
67{
68 usart_device_pt pUart = NULL;
69
70 VERIFY_UART_INST_VALID(e_uart);
71 VERIFY_UART_VOIDPTR_IS_NULL(pp_uart);
72
73 /* Select UART */
74 if ( e_uart == eUsart0 )
75 {
76 pUart = &customusart0;
77
78 /* Enable USART 0 IRQ */
79 NVIC_ClearPendingIRQ(USART0_IRQn);
80 NVIC_EnableIRQ(USART0_IRQn);
81
82
83 }
84 else
85 {
86 return ( DRV_ERROR_PARAMETER );
87 }
88
89 VERIFY_INIT(pUart->b_initialized);
90
91 /* Reset default values of USART */
92 hw_uart_reset(pUart->p_base_reg);
93
94 /* Enable transmitter (receiver is only enabled when read is requested) */
95 hw_uart_write_tx_en(pUart->p_base_reg, ENABLE);
96
97 /* Set UART device content */
98 pUart->cb_event = cb_event;
99 pUart->b_block_rx = true;
100 pUart->b_block_tx = true;
101 pUart->b_initialized = true;
102
103 *pp_uart = pUart;
104
105 return (DRV_OK);
106}
107
109uint32_t customUart_getTxExpected( void* p_uart )
110{
111 usart_device_pt pUart = (usart_device_pt)p_uart;
112
113 VERIFY_UART_IS_NULL(p_uart);
114
115 VERIFY_UART_BASE_VALID(pUart->p_base_reg);
116
117 VERIFY_UNINIT(pUart->b_initialized);
118
119 return (pUart->tx_expected);
120}
121
122/* ----------------------------------------------------------------------------
123 * Function : void USART0_Exception_Handler ( void )
124 * ----------------------------------------------------------------------------
125 * Description : This function is the weak link for USART0 peripheral.
126 * Inputs : None
127 * Outputs : None
128 * Assumptions : None
129 * ------------------------------------------------------------------------- */
130void USART0_Exception_Handler( void )
131{
132 uint16_t regData;
133
134 if (customusart0.p_base_reg->STS & USART_STS_RX_FE_Msk)
135 {
136 customusart0.p_base_reg->STS_b.RX_FE = CLEAR;
137
138 hw_uart_write_rx_int_en(customusart0.p_base_reg, DISABLE);
139 hw_uart_write_rx_en(customusart0.p_base_reg, DISABLE);
140
141 if (customusart0.cb_event != NULL)
142 {
143 customusart0.cb_event(UART_EVT_RX_FE);
144 }
145 return;
146 }
147
148 if (customusart0.p_base_reg->STS & USART_STS_RX_FULL_Msk)
149 {
150 /* Read out DATA register to clear the status bit */
151
152 if ( customusart0.rx_bytes < customusart0.rx_expected )
153 {
154 regData = customusart0.p_base_reg->DATA;
155 if (customCheckParity(&customusart0, regData))
156 {
157 *customusart0.p_rx_data++ = (uint8_t)regData;
158 customusart0.rx_bytes++;
159 }
160 else
161 {
162 hw_uart_write_rx_int_en(customusart0.p_base_reg, DISABLE);
163 hw_uart_write_rx_en(customusart0.p_base_reg, DISABLE);
164
165 if (customusart0.cb_event != NULL)
166 {
167 customusart0.cb_event(UART_EVT_RX_PARITY_ERROR);
168 }
169 }
170 }
171 if ( customusart0.rx_bytes == customusart0.rx_expected ) /* done */
172 {
173 //Do not disable UARTRX here, use uart_control to make this in another point of code
174// hw_uart_write_rx_int_en(customusart0.p_base_reg, DISABLE);
175// hw_uart_write_rx_en(customusart0.p_base_reg, DISABLE);
176
177 if (customusart0.cb_event != NULL)
178 {
179 customusart0.cb_event(UART_EVT_RX_COMPLETE);
180 }
181 }
182
183 return;
184 } /* Rx Full */
185 else if (customusart0.p_base_reg->STS & USART_STS_TX_EMPTY_Msk)
186 {
187 /* Write the DATA register to clear the status bit */
188
189 if (customusart0.tx_bytes == customusart0.tx_expected) /* done */
190 {
191 hw_uart_write_tx_int_en(customusart0.p_base_reg, DISABLE);
192
193 if (customusart0.cb_event != NULL)
194 {
195 customusart0.cb_event(UART_EVT_TX_COMPLETE);
196 }
197 }
198 else if (customusart0.tx_bytes < customusart0.tx_expected)
199 {
200 customusart0.p_base_reg->DATA = customAddParity(&customusart0, (uint8_t)*customusart0.p_tx_data++);
201 customusart0.tx_bytes++;
202 }
203
204 return;
205 } /* Tx Empty */
206 if (customusart0.p_base_reg->STS & USART_STS_RX_OVER_Msk)
207 {
208 customusart0.p_base_reg->STS_b.RX_OVER = CLEAR;
209
210 if (customusart0.cb_event != NULL)
211 {
212 customusart0.cb_event (UART_EVT_RX_OVER);
213 }
214 return;
215 }
216 if (customusart0.p_base_reg->STS & USART_STS_TX_IDLE_Msk)
217 {
218 /* received a TX idle interrupt */
219 if (customusart0.cb_event != NULL)
220 {
221 customusart0.cb_event(UART_EVT_TX_IDLE);
222 }
223 }
224 if (customusart0.p_base_reg->STS & USART_STS_RX_BRK_Msk)
225 {
226 /* received a RX break interrupt */
227 if (customusart0.cb_event != NULL)
228 {
229 customusart0.cb_event(UART_EVT_RX_BRK);
230 }
231 }
232
233 else
234 return;
235}
236
237/* ----------------------------------------------------------------------------
238 * Function : void USART1_Exception_Handler ( void )
239 * ----------------------------------------------------------------------------
240 * Description : This function is the weak link for USART1 peripheral.
241 * Inputs : None
242 * Outputs : None
243 * Assumptions : None
244 * ------------------------------------------------------------------------- */
245void USART1_Exception_Handler( void )
246{
247 return;
248}
249
250uint16_t customAddParity(usart_device_pt pUart, uint8_t data) {
251 uint8_t computedData = data;
252 bool parity;
253
254 if (pUart->parity == UART_PARITY_NONE)
255 return (uint16_t)data;
256 else {
257 computedData ^= computedData >> 4;
258 computedData ^= computedData >> 2;
259 computedData ^= computedData >> 1;
260 parity = computedData & 1;
261 if (pUart->parity == UART_PARITY_ODD)
262 return ((uint16_t)data | ((uint16_t)(!parity) << 8));
263 else if (pUart->parity == UART_PARITY_EVEN)
264 return ((uint16_t)data | ((uint16_t)(parity) << 8));
265 else
266 assert(false);
267 }
268}
269
281bool customCheckParity(usart_device_pt pUart, uint16_t data) {
282 uint8_t computedData = (uint8_t)data;
283 bool parity = (bool)(data >> 8);
284
285 if (pUart->parity == UART_PARITY_NONE)
286 return true;
287 else {
288 computedData ^= computedData >> 4;
289 computedData ^= computedData >> 2;
290 computedData ^= computedData >> 1;
291 if (pUart->parity == UART_PARITY_ODD)
292 return ((bool)(computedData & 1) == (!parity));
293 else if (pUart->parity == UART_PARITY_EVEN)
294 return ((bool)(computedData & 1) == parity);
295 else
296 assert(false);
297 }
298}
#define ENABLE
#define CLEAR
#define DISABLE
uint32_t customUart_getTxExpected(void *p_uart)
Definition: CustomUart.c:109
uint32_t customUart_initialize(UsartInstance_Enum e_uart, USART_SignalEvent_t cb_event, void **pp_uart)
Definition: CustomUart.c:66
bool customCheckParity(usart_device_pt pUart, uint16_t data)
Definition: CustomUart.c:281
Header file for CustomUart.c.