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