1 /*
2 * Copyright (c) 2013-2018 Arm Limited. All rights reserved.
3 * Copyright (c) 2020 Nuvoton Technology Corp. All rights reserved.
4 *
5 * SPDX-License-Identifier: Apache-2.0
6 *
7 * Licensed under the Apache License, Version 2.0 (the License); you may
8 * not use this file except in compliance with the License.
9 * You may obtain a copy of the License at
10 *
11 * http://www.apache.org/licenses/LICENSE-2.0
12 *
13 * Unless required by applicable law or agreed to in writing, software
14 * distributed under the License is distributed on an AS IS BASIS, WITHOUT
15 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 * See the License for the specific language governing permissions and
17 * limitations under the License.
18 */
19
20 #include "Driver_USART.h"
21 #include "NuMicro.h"
22 #include "RTE_Device.h"
23
24
25 #ifndef ARG_UNUSED
26 #define ARG_UNUSED(arg) (void)arg
27 #endif
28
29 /* Driver version */
30 #define ARM_USART_DRV_VERSION ARM_DRIVER_VERSION_MAJOR_MINOR(2, 2)
31
32 /* Driver Version */
33 static const ARM_DRIVER_VERSION DriverVersion =
34 {
35 ARM_USART_API_VERSION,
36 ARM_USART_DRV_VERSION
37 };
38
39 /* Driver Capabilities */
40 static const ARM_USART_CAPABILITIES DriverCapabilities =
41 {
42 1, /* supports UART (Asynchronous) mode */
43 0, /* supports Synchronous Master mode */
44 0, /* supports Synchronous Slave mode */
45 0, /* supports UART Single-wire mode */
46 0, /* supports UART IrDA mode */
47 0, /* supports UART Smart Card mode */
48 0, /* Smart Card Clock generator available */
49 0, /* RTS Flow Control available */
50 0, /* CTS Flow Control available */
51 0, /* Transmit completed event: \ref ARM_USARTx_EVENT_TX_COMPLETE */
52 0, /* Signal receive character timeout event: \ref ARM_USARTx_EVENT_RX_TIMEOUT */
53 0, /* RTS Line: 0=not available, 1=available */
54 0, /* CTS Line: 0=not available, 1=available */
55 0, /* DTR Line: 0=not available, 1=available */
56 0, /* DSR Line: 0=not available, 1=available */
57 0, /* DCD Line: 0=not available, 1=available */
58 0, /* RI Line: 0=not available, 1=available */
59 0, /* Signal CTS change event: \ref ARM_USARTx_EVENT_CTS */
60 0, /* Signal DSR change event: \ref ARM_USARTx_EVENT_DSR */
61 0, /* Signal DCD change event: \ref ARM_USARTx_EVENT_DCD */
62 0, /* Signal RI change event: \ref ARM_USARTx_EVENT_RI */
63 0 /* Reserved */
64 };
65
ARM_USART_GetVersion(void)66 static ARM_DRIVER_VERSION ARM_USART_GetVersion(void)
67 {
68 return DriverVersion;
69 }
70
ARM_USART_GetCapabilities(void)71 static ARM_USART_CAPABILITIES ARM_USART_GetCapabilities(void)
72 {
73 return DriverCapabilities;
74 }
75
76 typedef struct
77 {
78 UART_T* dev; /* UART device structure */
79 uint32_t tx_nbr_bytes; /* Number of bytes transfered */
80 uint32_t rx_nbr_bytes; /* Number of bytes recevied */
81 ARM_USART_SignalEvent_t cb_event; /* Callback function for events */
82 } UARTx_Resources;
83
ARM_USARTx_Initialize(UARTx_Resources * uart_dev)84 static int32_t ARM_USARTx_Initialize(UARTx_Resources* uart_dev)
85 {
86 /* Initializes generic UART driver */
87 return ARM_DRIVER_OK;
88 }
89
ARM_USARTx_PowerControl(UARTx_Resources * uart_dev,ARM_POWER_STATE state)90 static int32_t ARM_USARTx_PowerControl(UARTx_Resources* uart_dev,
91 ARM_POWER_STATE state)
92 {
93 ARG_UNUSED(uart_dev);
94
95 switch(state)
96 {
97 case ARM_POWER_OFF:
98 case ARM_POWER_LOW:
99 return ARM_DRIVER_ERROR_UNSUPPORTED;
100 case ARM_POWER_FULL:
101 /* Nothing to be done */
102 return ARM_DRIVER_OK;
103 /* default: The default is not defined intentionally to force the
104 * compiler to check that all the enumeration values are
105 * covered in the switch.*/
106 }
107 }
108
ARM_USARTx_Send(UARTx_Resources * uart_dev,const void * data,uint32_t num)109 static int32_t ARM_USARTx_Send(UARTx_Resources* uart_dev, const void *data,
110 uint32_t num)
111 {
112 const uint8_t* p_data = (const uint8_t*)data;
113
114 if((data == NULL) || (num == 0U))
115 {
116 /* Invalid parameters */
117 return ARM_DRIVER_ERROR_PARAMETER;
118 }
119
120 /* Resets previous TX counter */
121 uart_dev->tx_nbr_bytes = 0;
122
123 while(uart_dev->tx_nbr_bytes != num)
124 {
125 /* Waits until UART is ready to transmit */
126 while(uart_dev->dev->FIFOSTS & UART_FIFOSTS_TXFULL_Msk);
127
128 /* As UART is ready to transmit at this point, the write function can
129 * not return any transmit error */
130 uart_dev->dev->DAT = *p_data;
131
132 uart_dev->tx_nbr_bytes++;
133 p_data++;
134 }
135
136 /* Waits until character is transmited */
137 while((uart_dev->dev->FIFOSTS & UART_FIFOSTS_TXEMPTY_Msk) == 0);
138
139 return ARM_DRIVER_OK;
140 }
141
ARM_USARTx_Receive(UARTx_Resources * uart_dev,void * data,uint32_t num)142 static int32_t ARM_USARTx_Receive(UARTx_Resources* uart_dev,
143 void *data, uint32_t num)
144 {
145 uint8_t* p_data = (uint8_t*)data;
146
147 if((data == NULL) || (num == 0U))
148 {
149 // Invalid parameters
150 return ARM_DRIVER_ERROR_PARAMETER;
151 }
152
153 /* Resets previous RX counter */
154 uart_dev->rx_nbr_bytes = 0;
155
156 while(uart_dev->rx_nbr_bytes != num)
157 {
158 /* Waits until one character is received */
159 while(uart_dev->dev->FIFOSTS & UART_FIFOSTS_RXEMPTY_Msk);
160
161 /* As UART has received one byte, the read can not
162 * return any receive error at this point */
163 *p_data = uart_dev->dev->DAT;
164
165 uart_dev->rx_nbr_bytes++;
166 p_data++;
167 }
168
169 return ARM_DRIVER_OK;
170 }
171
ARM_USARTx_GetTxCount(UARTx_Resources * uart_dev)172 static uint32_t ARM_USARTx_GetTxCount(UARTx_Resources* uart_dev)
173 {
174 return uart_dev->tx_nbr_bytes;
175 }
176
ARM_USARTx_GetRxCount(UARTx_Resources * uart_dev)177 static uint32_t ARM_USARTx_GetRxCount(UARTx_Resources* uart_dev)
178 {
179 return uart_dev->rx_nbr_bytes;
180 }
181
ARM_USARTx_Control(UARTx_Resources * uart_dev,uint32_t control,uint32_t arg)182 static int32_t ARM_USARTx_Control(UARTx_Resources* uart_dev, uint32_t control,
183 uint32_t arg)
184 {
185 switch(control & ARM_USART_CONTROL_Msk)
186 {
187 case ARM_USART_MODE_ASYNCHRONOUS:
188 uart_dev->dev->LINE = UART_PARITY_NONE | UART_STOP_BIT_1 | UART_WORD_LEN_8;
189 uart_dev->dev->BAUD = UART_BAUD_MODE2 | UART_BAUD_MODE2_DIVIDER(__HIRC, arg);
190
191 /* avoid noise in reset state */
192 uart_dev->dev->DAT = '\r';
193
194 break;
195 /* Unsupported command */
196 default:
197 return ARM_DRIVER_ERROR_UNSUPPORTED;
198 }
199
200 /* UART Data bits */
201 if(control & ARM_USART_DATA_BITS_Msk)
202 {
203 /* Data bit is not configurable */
204 return ARM_DRIVER_ERROR_UNSUPPORTED;
205 }
206
207 /* UART Parity */
208 if(control & ARM_USART_PARITY_Msk)
209 {
210 /* Parity is not configurable */
211 return ARM_USART_ERROR_PARITY;
212 }
213
214 /* USART Stop bits */
215 if(control & ARM_USART_STOP_BITS_Msk)
216 {
217 /* Stop bit is not configurable */
218 return ARM_USART_ERROR_STOP_BITS;
219 }
220
221 return ARM_DRIVER_OK;
222 }
223
224 #if (RTE_USART0)
225 /* USART0 Driver wrapper functions */
226 static UARTx_Resources USART0_DEV =
227 {
228 .dev = UART0_NS,
229 .tx_nbr_bytes = 0,
230 .rx_nbr_bytes = 0,
231 .cb_event = NULL,
232 };
233
ARM_USART0_Initialize(ARM_USART_SignalEvent_t cb_event)234 static int32_t ARM_USART0_Initialize(ARM_USART_SignalEvent_t cb_event)
235 {
236 USART0_DEV.cb_event = cb_event;
237
238 return ARM_USARTx_Initialize(&USART0_DEV);
239 }
240
ARM_USART0_Uninitialize(void)241 static int32_t ARM_USART0_Uninitialize(void)
242 {
243
244 /* Nothing to be done */
245 return ARM_DRIVER_OK;
246 }
247
ARM_USART0_PowerControl(ARM_POWER_STATE state)248 static int32_t ARM_USART0_PowerControl(ARM_POWER_STATE state)
249 {
250 return ARM_USARTx_PowerControl(&USART0_DEV, state);
251 }
252
ARM_USART0_Send(const void * data,uint32_t num)253 static int32_t ARM_USART0_Send(const void *data, uint32_t num)
254 {
255 return ARM_USARTx_Send(&USART0_DEV, data, num);
256 }
257
ARM_USART0_Receive(void * data,uint32_t num)258 static int32_t ARM_USART0_Receive(void *data, uint32_t num)
259 {
260 return ARM_USARTx_Receive(&USART0_DEV, data, num);
261 }
262
ARM_USART0_Transfer(const void * data_out,void * data_in,uint32_t num)263 static int32_t ARM_USART0_Transfer(const void *data_out, void *data_in,
264 uint32_t num)
265 {
266 ARG_UNUSED(data_out);
267 ARG_UNUSED(data_in);
268 ARG_UNUSED(num);
269
270 return ARM_DRIVER_ERROR_UNSUPPORTED;
271 }
272
ARM_USART0_GetTxCount(void)273 static uint32_t ARM_USART0_GetTxCount(void)
274 {
275 return ARM_USARTx_GetTxCount(&USART0_DEV);
276 }
277
ARM_USART0_GetRxCount(void)278 static uint32_t ARM_USART0_GetRxCount(void)
279 {
280 return ARM_USARTx_GetRxCount(&USART0_DEV);
281 }
ARM_USART0_Control(uint32_t control,uint32_t arg)282 static int32_t ARM_USART0_Control(uint32_t control, uint32_t arg)
283 {
284 return ARM_USARTx_Control(&USART0_DEV, control, arg);
285 }
286
ARM_USART0_GetStatus(void)287 static ARM_USART_STATUS ARM_USART0_GetStatus(void)
288 {
289 ARM_USART_STATUS status = {0, 0, 0, 0, 0, 0, 0, 0};
290 return status;
291 }
292
ARM_USART0_SetModemControl(ARM_USART_MODEM_CONTROL control)293 static int32_t ARM_USART0_SetModemControl(ARM_USART_MODEM_CONTROL control)
294 {
295 ARG_UNUSED(control);
296 return ARM_DRIVER_ERROR_UNSUPPORTED;
297 }
298
ARM_USART0_GetModemStatus(void)299 static ARM_USART_MODEM_STATUS ARM_USART0_GetModemStatus(void)
300 {
301 ARM_USART_MODEM_STATUS modem_status = {0, 0, 0, 0, 0};
302 return modem_status;
303 }
304
305 extern ARM_DRIVER_USART Driver_USART0;
306 ARM_DRIVER_USART Driver_USART0 =
307 {
308 ARM_USART_GetVersion,
309 ARM_USART_GetCapabilities,
310 ARM_USART0_Initialize,
311 ARM_USART0_Uninitialize,
312 ARM_USART0_PowerControl,
313 ARM_USART0_Send,
314 ARM_USART0_Receive,
315 ARM_USART0_Transfer,
316 ARM_USART0_GetTxCount,
317 ARM_USART0_GetRxCount,
318 ARM_USART0_Control,
319 ARM_USART0_GetStatus,
320 ARM_USART0_SetModemControl,
321 ARM_USART0_GetModemStatus
322 };
323 #endif /* RTE_USART0 */
324
325 #if (RTE_USART1)
326 /* USART1 Driver wrapper functions */
327 static UARTx_Resources USART1_DEV =
328 {
329 .dev = UART1,
330 .tx_nbr_bytes = 0,
331 .rx_nbr_bytes = 0,
332 .cb_event = NULL,
333 };
334
ARM_USART1_Initialize(ARM_USART_SignalEvent_t cb_event)335 static int32_t ARM_USART1_Initialize(ARM_USART_SignalEvent_t cb_event)
336 {
337 USART1_DEV.cb_event = cb_event;
338
339 return ARM_USARTx_Initialize(&USART1_DEV);
340 }
341
ARM_USART1_Uninitialize(void)342 static int32_t ARM_USART1_Uninitialize(void)
343 {
344 /* Nothing to be done */
345 return ARM_DRIVER_OK;
346 }
347
ARM_USART1_PowerControl(ARM_POWER_STATE state)348 static int32_t ARM_USART1_PowerControl(ARM_POWER_STATE state)
349 {
350 return ARM_USARTx_PowerControl(&USART1_DEV, state);
351 }
352
ARM_USART1_Send(const void * data,uint32_t num)353 static int32_t ARM_USART1_Send(const void *data, uint32_t num)
354 {
355 return ARM_USARTx_Send(&USART1_DEV, data, num);
356 }
357
ARM_USART1_Receive(void * data,uint32_t num)358 static int32_t ARM_USART1_Receive(void *data, uint32_t num)
359 {
360 return ARM_USARTx_Receive(&USART1_DEV, data, num);
361 }
362
ARM_USART1_Transfer(const void * data_out,void * data_in,uint32_t num)363 static int32_t ARM_USART1_Transfer(const void *data_out, void *data_in,
364 uint32_t num)
365 {
366 ARG_UNUSED(data_out);
367 ARG_UNUSED(data_in);
368 ARG_UNUSED(num);
369
370 return ARM_DRIVER_ERROR_UNSUPPORTED;
371 }
372
ARM_USART1_GetTxCount(void)373 static uint32_t ARM_USART1_GetTxCount(void)
374 {
375 return ARM_USARTx_GetTxCount(&USART1_DEV);
376 }
377
ARM_USART1_GetRxCount(void)378 static uint32_t ARM_USART1_GetRxCount(void)
379 {
380 return ARM_USARTx_GetRxCount(&USART1_DEV);
381 }
ARM_USART1_Control(uint32_t control,uint32_t arg)382 static int32_t ARM_USART1_Control(uint32_t control, uint32_t arg)
383 {
384 return ARM_USARTx_Control(&USART1_DEV, control, arg);
385 }
386
ARM_USART1_GetStatus(void)387 static ARM_USART_STATUS ARM_USART1_GetStatus(void)
388 {
389 ARM_USART_STATUS status = {0, 0, 0, 0, 0, 0, 0, 0};
390 return status;
391 }
392
ARM_USART1_SetModemControl(ARM_USART_MODEM_CONTROL control)393 static int32_t ARM_USART1_SetModemControl(ARM_USART_MODEM_CONTROL control)
394 {
395 ARG_UNUSED(control);
396 return ARM_DRIVER_ERROR_UNSUPPORTED;
397 }
398
ARM_USART1_GetModemStatus(void)399 static ARM_USART_MODEM_STATUS ARM_USART1_GetModemStatus(void)
400 {
401 ARM_USART_MODEM_STATUS modem_status = {0, 0, 0, 0, 0};
402 return modem_status;
403 }
404
405 extern ARM_DRIVER_USART Driver_USART1;
406 ARM_DRIVER_USART Driver_USART1 =
407 {
408 ARM_USART_GetVersion,
409 ARM_USART_GetCapabilities,
410 ARM_USART1_Initialize,
411 ARM_USART1_Uninitialize,
412 ARM_USART1_PowerControl,
413 ARM_USART1_Send,
414 ARM_USART1_Receive,
415 ARM_USART1_Transfer,
416 ARM_USART1_GetTxCount,
417 ARM_USART1_GetRxCount,
418 ARM_USART1_Control,
419 ARM_USART1_GetStatus,
420 ARM_USART1_SetModemControl,
421 ARM_USART1_GetModemStatus
422 };
423 #endif /* RTE_USART1 */
424