1 /*
2 * Copyright (c) 2013-2021 Arm Limited. All rights reserved.
3 * Copyright 2019-2022 NXP. 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 "fsl_common.h"
21 #include "fsl_usart.h"
22 #include "fsl_clock.h"
23
24 #include "Driver_USART.h"
25 #include "board.h"
26 #include "platform_base_address.h"
27
28 #ifndef ARG_UNUSED
29 #define ARG_UNUSED(arg) (void)arg
30 #endif
31
32 /*******************************************************************************
33 * Variables
34 ******************************************************************************/
35
36 typedef struct {
37 USART_Type *base; /* USART base */
38 usart_config_t config; /* USART configuration structure */
39 uint32_t tx_nbr_bytes; /* Number of bytes transfered */
40 uint32_t rx_nbr_bytes; /* Number of bytes recevied */
41 bool is_initialized; /* true if initialized */
42 } UARTx_Resources;
43
44 /* Driver version */
45 #define ARM_USART_DRV_VERSION ARM_DRIVER_VERSION_MAJOR_MINOR(2, 2)
46
47 /* Driver Version */
48 static const ARM_DRIVER_VERSION DriverVersion = {
49 ARM_USART_API_VERSION,
50 ARM_USART_DRV_VERSION
51 };
52
53 /* Driver Capabilities */
54 static const ARM_USART_CAPABILITIES DriverCapabilities = {
55 1, /* supports UART (Asynchronous) mode */
56 0, /* supports Synchronous Master mode */
57 0, /* supports Synchronous Slave mode */
58 0, /* supports UART Single-wire mode */
59 0, /* supports UART IrDA mode */
60 0, /* supports UART Smart Card mode */
61 0, /* Smart Card Clock generator available */
62 0, /* RTS Flow Control available */
63 0, /* CTS Flow Control available */
64 0, /* Transmit completed event: \ref ARM_USARTx_EVENT_TX_COMPLETE */
65 0, /* Signal receive character timeout event: \ref ARM_USARTx_EVENT_RX_TIMEOUT */
66 0, /* RTS Line: 0=not available, 1=available */
67 0, /* CTS Line: 0=not available, 1=available */
68 0, /* DTR Line: 0=not available, 1=available */
69 0, /* DSR Line: 0=not available, 1=available */
70 0, /* DCD Line: 0=not available, 1=available */
71 0, /* RI Line: 0=not available, 1=available */
72 0, /* Signal CTS change event: \ref ARM_USARTx_EVENT_CTS */
73 0, /* Signal DSR change event: \ref ARM_USARTx_EVENT_DSR */
74 0, /* Signal DCD change event: \ref ARM_USARTx_EVENT_DCD */
75 0, /* Signal RI change event: \ref ARM_USARTx_EVENT_RI */
76 0 /* Reserved */
77 };
78
ARM_USART_GetVersion(void)79 static ARM_DRIVER_VERSION ARM_USART_GetVersion(void)
80 {
81 return DriverVersion;
82 }
83
ARM_USART_GetCapabilities(void)84 static ARM_USART_CAPABILITIES ARM_USART_GetCapabilities(void)
85 {
86 return DriverCapabilities;
87 }
88
ARM_USARTx_Initialize(UARTx_Resources * uart_dev)89 static int32_t ARM_USARTx_Initialize(UARTx_Resources* uart_dev)
90 {
91 uint32_t usartClkFreq;
92
93 usartClkFreq = BOARD_DEBUG_UART_CLK_FREQ;
94
95 if (USART_Init(uart_dev->base, &uart_dev->config, usartClkFreq) == kStatus_Success) {
96 uart_dev->is_initialized = true;
97 return ARM_DRIVER_OK;
98 } else {
99 return ARM_DRIVER_ERROR;
100 }
101 }
102
ARM_USARTx_PowerControl(UARTx_Resources * uart_dev,ARM_POWER_STATE state)103 static int32_t ARM_USARTx_PowerControl(UARTx_Resources* uart_dev,
104 ARM_POWER_STATE state)
105 {
106 ARG_UNUSED(uart_dev);
107
108 switch (state) {
109 case ARM_POWER_OFF:
110 case ARM_POWER_LOW:
111 return ARM_DRIVER_ERROR_UNSUPPORTED;
112 case ARM_POWER_FULL:
113 /* Nothing to be done */
114 return ARM_DRIVER_OK;
115 default:
116 return ARM_DRIVER_ERROR_PARAMETER;
117 }
118 }
119
ARM_USARTx_Deinitialize(UARTx_Resources * uart_dev)120 static int32_t ARM_USARTx_Deinitialize(UARTx_Resources* uart_dev)
121 {
122 if(uart_dev->is_initialized) {
123 USART_Deinit(uart_dev->base);
124 uart_dev->is_initialized = false;
125 }
126
127 return ARM_DRIVER_OK;
128 }
129
ARM_USARTx_Send(UARTx_Resources * uart_dev,const uint8_t * data,size_t length)130 static int32_t ARM_USARTx_Send(UARTx_Resources* uart_dev, const uint8_t *data, size_t length)
131 {
132 USART_WriteBlocking(uart_dev->base, data, length);
133
134 uart_dev->tx_nbr_bytes = length;
135
136 return ARM_DRIVER_OK;
137 }
138
ARM_USARTx_Receive(UARTx_Resources * uart_dev,uint8_t * data,size_t length)139 static int32_t ARM_USARTx_Receive(UARTx_Resources* uart_dev, uint8_t *data, size_t length)
140 {
141 status_t status;
142
143 status = USART_ReadBlocking(uart_dev->base, data, length);
144
145 if (status == kStatus_Success)
146 {
147 uart_dev->rx_nbr_bytes = length;
148 return ARM_DRIVER_OK;
149 }
150 else
151 {
152 return ARM_DRIVER_ERROR;
153 }
154 }
155
ARM_USARTx_GetTxCount(UARTx_Resources * uart_dev)156 static uint32_t ARM_USARTx_GetTxCount(UARTx_Resources* uart_dev)
157 {
158 return uart_dev->tx_nbr_bytes;
159 }
160
ARM_USARTx_GetRxCount(UARTx_Resources * uart_dev)161 static uint32_t ARM_USARTx_GetRxCount(UARTx_Resources* uart_dev)
162 {
163 return uart_dev->rx_nbr_bytes;
164 }
165
166
167 /* USART_BASE Driver wrapper functions */
168 static UARTx_Resources USART_DEV = {
169 .base = USART_BASE,
170 };
ARM_USART_Initialize(ARM_USART_SignalEvent_t cb_event)171 static int32_t ARM_USART_Initialize(ARM_USART_SignalEvent_t cb_event)
172 {
173 ARG_UNUSED(cb_event);
174
175 /*
176 * usartConfig->baudRate_Bps = 115200U;
177 * usartConfig->parityMode = kUSART_ParityDisabled;
178 * usartConfig->stopBitCount = kUSART_OneStopBit;
179 * usartConfig->bitCountPerChar = kUSART_8BitsPerChar;
180 * usartConfig->loopback = false;
181 * usartConfig->enableTx = false;
182 * usartConfig->enableRx = false;
183 */
184 USART_GetDefaultConfig(&USART_DEV.config);
185 USART_DEV.config.enableRx = true;
186 USART_DEV.config.enableTx = true;
187
188 return ARM_USARTx_Initialize(&USART_DEV);
189 }
190
ARM_USART_Uninitialize(void)191 static int32_t ARM_USART_Uninitialize(void)
192 {
193 return ARM_USARTx_Deinitialize(&USART_DEV);
194 }
195
ARM_USART_PowerControl(ARM_POWER_STATE state)196 static int32_t ARM_USART_PowerControl(ARM_POWER_STATE state)
197 {
198 return ARM_USARTx_PowerControl(&USART_DEV, state);
199 }
200
ARM_USART_Send(const void * data,uint32_t num)201 static int32_t ARM_USART_Send(const void *data, uint32_t num)
202 {
203 return ARM_USARTx_Send(&USART_DEV, data, num);
204 }
205
ARM_USART_Receive(void * data,uint32_t num)206 static int32_t ARM_USART_Receive(void *data, uint32_t num)
207 {
208 return ARM_USARTx_Receive(&USART_DEV, data, num);
209 }
210
ARM_USART_Transfer(const void * data_out,void * data_in,uint32_t num)211 static int32_t ARM_USART_Transfer(const void *data_out, void *data_in,
212 uint32_t num)
213 {
214 ARG_UNUSED(data_out);
215 ARG_UNUSED(data_in);
216 ARG_UNUSED(num);
217
218 return ARM_DRIVER_ERROR_UNSUPPORTED;
219 }
220
ARM_USART_GetTxCount(void)221 static uint32_t ARM_USART_GetTxCount(void)
222 {
223 return ARM_USARTx_GetTxCount(&USART_DEV);
224 }
225
ARM_USART_GetRxCount(void)226 static uint32_t ARM_USART_GetRxCount(void)
227 {
228 return ARM_USARTx_GetRxCount(&USART_DEV);
229 }
230
ARM_USART_Control(uint32_t control,uint32_t arg)231 static int32_t ARM_USART_Control(uint32_t control, uint32_t arg)
232 {
233 return ARM_DRIVER_OK; //ARM_USARTx_Control(&USART_DEV, control, arg);
234 }
235
ARM_USART_GetStatus(void)236 static ARM_USART_STATUS ARM_USART_GetStatus(void)
237 {
238 ARM_USART_STATUS status = {0, 0, 0, 0, 0, 0, 0, 0};
239 return status;
240 }
241
ARM_USART_SetModemControl(ARM_USART_MODEM_CONTROL control)242 static int32_t ARM_USART_SetModemControl(ARM_USART_MODEM_CONTROL control)
243 {
244 ARG_UNUSED(control);
245 return ARM_DRIVER_ERROR_UNSUPPORTED;
246 }
247
ARM_USART_GetModemStatus(void)248 static ARM_USART_MODEM_STATUS ARM_USART_GetModemStatus(void)
249 {
250 ARM_USART_MODEM_STATUS modem_status = {0, 0, 0, 0, 0};
251 return modem_status;
252 }
253
254 extern ARM_DRIVER_USART Driver_USART;
255 ARM_DRIVER_USART Driver_USART = {
256 .GetVersion = ARM_USART_GetVersion,
257 .GetCapabilities = ARM_USART_GetCapabilities,
258 .Initialize = ARM_USART_Initialize,
259 .Uninitialize = ARM_USART_Uninitialize,
260 .PowerControl = ARM_USART_PowerControl,
261 .Send = ARM_USART_Send,
262 .Receive = ARM_USART_Receive,
263 .Transfer = ARM_USART_Transfer,
264 .GetTxCount = ARM_USART_GetTxCount,
265 .GetRxCount = ARM_USART_GetRxCount,
266 .Control = ARM_USART_Control,
267 .GetStatus = ARM_USART_GetStatus,
268 .SetModemControl = ARM_USART_SetModemControl,
269 .GetModemStatus = ARM_USART_GetModemStatus
270 };
271