serial_port_uart.c
Go to the documentation of this file.
1 /*
2  * Copyright 2018 NXP
3  * All rights reserved.
4  *
5  *
6  * SPDX-License-Identifier: BSD-3-Clause
7  */
8 
9 #include "fsl_common.h"
10 #include "serial_manager.h"
11 #include "serial_port_internal.h"
12 
13 #if (defined(SERIAL_PORT_TYPE_UART) && (SERIAL_PORT_TYPE_UART > 0U))
14 #include "uart.h"
15 
16 #include "serial_port_uart.h"
17 
18 /*******************************************************************************
19  * Definitions
20  ******************************************************************************/
21 #ifndef NDEBUG
22 #if (defined(DEBUG_CONSOLE_ASSERT_DISABLE) && (DEBUG_CONSOLE_ASSERT_DISABLE > 0U))
23 #undef assert
24 #define assert(n)
25 #endif
26 #endif
27 
28 #if (defined(SERIAL_MANAGER_NON_BLOCKING_MODE) && (SERIAL_MANAGER_NON_BLOCKING_MODE > 0U))
29 #define SERIAL_PORT_UART_RECEIVE_DATA_LENGTH 1U
30 
31 typedef struct _serial_uart_send_state
32 {
34  void *callbackParam;
35  uint8_t *buffer;
36  uint32_t length;
37  volatile uint8_t busy;
38 } serial_uart_send_state_t;
39 
40 typedef struct _serial_uart_recv_state
41 {
43  void *callbackParam;
44  volatile uint8_t busy;
45  uint8_t readBuffer[SERIAL_PORT_UART_RECEIVE_DATA_LENGTH];
46 } serial_uart_recv_state_t;
47 #endif
48 
49 typedef struct _serial_uart_state
50 {
51 #if (defined(SERIAL_MANAGER_NON_BLOCKING_MODE) && (SERIAL_MANAGER_NON_BLOCKING_MODE > 0U))
52  serial_uart_send_state_t tx;
53  serial_uart_recv_state_t rx;
54 #endif
55  UART_HANDLE_DEFINE(usartHandleBuffer);
56 } serial_uart_state_t;
57 
58 /*******************************************************************************
59  * Prototypes
60  ******************************************************************************/
61 
62 /*******************************************************************************
63  * Code
64  ******************************************************************************/
65 
66 #if (defined(SERIAL_MANAGER_NON_BLOCKING_MODE) && (SERIAL_MANAGER_NON_BLOCKING_MODE > 0U))
67 /* UART user callback */
68 static void Serial_UartCallback(hal_uart_handle_t handle, hal_uart_status_t status, void *userData)
69 {
70  serial_uart_state_t *serialUartHandle;
72 #if (defined(HAL_UART_TRANSFER_MODE) && (HAL_UART_TRANSFER_MODE > 0U))
73  hal_uart_transfer_t transfer;
74 #endif
75 
76  if (NULL == userData)
77  {
78  return;
79  }
80 
81  serialUartHandle = (serial_uart_state_t *)userData;
82 
84  {
85  if ((NULL != serialUartHandle->rx.callback))
86  {
87  msg.buffer = &serialUartHandle->rx.readBuffer[0];
88  msg.length = sizeof(serialUartHandle->rx.readBuffer);
89  serialUartHandle->rx.callback(serialUartHandle->rx.callbackParam, &msg, kStatus_SerialManager_Success);
90  }
91 #if (defined(HAL_UART_TRANSFER_MODE) && (HAL_UART_TRANSFER_MODE > 0U))
92  transfer.data = &serialUartHandle->rx.readBuffer[0];
93  transfer.dataSize = sizeof(serialUartHandle->rx.readBuffer);
95  HAL_UartTransferReceiveNonBlocking(((hal_uart_handle_t)&serialUartHandle->usartHandleBuffer[0]), &transfer))
96 #else
98  HAL_UartReceiveNonBlocking(((hal_uart_handle_t)&serialUartHandle->usartHandleBuffer[0]),
99  &serialUartHandle->rx.readBuffer[0], sizeof(serialUartHandle->rx.readBuffer)))
100 #endif
101  {
102  serialUartHandle->rx.busy = 1U;
103  }
104  else
105  {
106  serialUartHandle->rx.busy = 0U;
107  }
108  }
109  else if ((hal_uart_status_t)kStatus_HAL_UartTxIdle == status)
110  {
111  if (0U != serialUartHandle->tx.busy)
112  {
113  serialUartHandle->tx.busy = 0U;
114  if ((NULL != serialUartHandle->tx.callback))
115  {
116  msg.buffer = serialUartHandle->tx.buffer;
117  msg.length = serialUartHandle->tx.length;
118  serialUartHandle->tx.callback(serialUartHandle->tx.callbackParam, &msg, kStatus_SerialManager_Success);
119  }
120  }
121  }
122  else
123  {
124  }
125 }
126 #endif
127 
128 serial_manager_status_t Serial_UartInit(serial_handle_t serialHandle, void *serialConfig)
129 {
130  serial_uart_state_t *serialUartHandle;
131  serial_port_uart_config_t *uartConfig;
133 #if (defined(SERIAL_MANAGER_NON_BLOCKING_MODE) && (SERIAL_MANAGER_NON_BLOCKING_MODE > 0U))
134 #if (defined(HAL_UART_TRANSFER_MODE) && (HAL_UART_TRANSFER_MODE > 0U))
135  hal_uart_transfer_t transfer;
136 #endif
137 #endif
138 
139  assert(serialConfig);
140  assert(serialHandle);
141  assert(SERIAL_PORT_UART_HANDLE_SIZE >= sizeof(serial_uart_state_t));
142 
143  uartConfig = (serial_port_uart_config_t *)serialConfig;
144  serialUartHandle = (serial_uart_state_t *)serialHandle;
145 
146  config.baudRate_Bps = uartConfig->baudRate;
147  config.parityMode = (hal_uart_parity_mode_t)uartConfig->parityMode;
148  config.stopBitCount = (hal_uart_stop_bit_count_t)uartConfig->stopBitCount;
149  config.enableRx = uartConfig->enableRx;
150  config.enableTx = uartConfig->enableTx;
151  config.srcClock_Hz = uartConfig->clockRate;
152  config.instance = uartConfig->instance;
153 
154  if (kStatus_HAL_UartSuccess != HAL_UartInit(((hal_uart_handle_t)&serialUartHandle->usartHandleBuffer[0]), &config))
155  {
157  }
158 
159 #if (defined(SERIAL_MANAGER_NON_BLOCKING_MODE) && (SERIAL_MANAGER_NON_BLOCKING_MODE > 0U))
160 
161 #if (defined(HAL_UART_TRANSFER_MODE) && (HAL_UART_TRANSFER_MODE > 0U))
163  HAL_UartTransferInstallCallback(((hal_uart_handle_t)&serialUartHandle->usartHandleBuffer[0]),
164  Serial_UartCallback, serialUartHandle))
165 #else
166  if (kStatus_HAL_UartSuccess != HAL_UartInstallCallback(((hal_uart_handle_t)&serialUartHandle->usartHandleBuffer[0]),
167  Serial_UartCallback, serialUartHandle))
168 #endif
169  {
171  }
172 
173  if (0U != uartConfig->enableRx)
174  {
175  serialUartHandle->rx.busy = 1U;
176 #if (defined(HAL_UART_TRANSFER_MODE) && (HAL_UART_TRANSFER_MODE > 0U))
177  transfer.data = &serialUartHandle->rx.readBuffer[0];
178  transfer.dataSize = sizeof(serialUartHandle->rx.readBuffer);
180  HAL_UartTransferReceiveNonBlocking(((hal_uart_handle_t)&serialUartHandle->usartHandleBuffer[0]), &transfer))
181 #else
183  HAL_UartReceiveNonBlocking(((hal_uart_handle_t)&serialUartHandle->usartHandleBuffer[0]),
184  &serialUartHandle->rx.readBuffer[0], sizeof(serialUartHandle->rx.readBuffer)))
185 #endif
186  {
187  serialUartHandle->rx.busy = 0U;
189  }
190  }
191 #endif
192 
194 }
195 
196 serial_manager_status_t Serial_UartDeinit(serial_handle_t serialHandle)
197 {
198  serial_uart_state_t *serialUartHandle;
199 
200  assert(serialHandle);
201 
202  serialUartHandle = (serial_uart_state_t *)serialHandle;
203 
204 #if (defined(SERIAL_MANAGER_NON_BLOCKING_MODE) && (SERIAL_MANAGER_NON_BLOCKING_MODE > 0U))
205 #if (defined(HAL_UART_TRANSFER_MODE) && (HAL_UART_TRANSFER_MODE > 0U))
206  (void)HAL_UartTransferAbortReceive(((hal_uart_handle_t)&serialUartHandle->usartHandleBuffer[0]));
207 #else
208  (void)HAL_UartAbortReceive(((hal_uart_handle_t)&serialUartHandle->usartHandleBuffer[0]));
209 #endif
210 #endif
211  (void)HAL_UartDeinit(((hal_uart_handle_t)&serialUartHandle->usartHandleBuffer[0]));
212 
213 #if (defined(SERIAL_MANAGER_NON_BLOCKING_MODE) && (SERIAL_MANAGER_NON_BLOCKING_MODE > 0U))
214  serialUartHandle->tx.busy = 0U;
215  serialUartHandle->rx.busy = 0U;
216 #endif
217 
219 }
220 
221 #if (defined(SERIAL_MANAGER_NON_BLOCKING_MODE) && (SERIAL_MANAGER_NON_BLOCKING_MODE > 0U))
222 
223 serial_manager_status_t Serial_UartWrite(serial_handle_t serialHandle, uint8_t *buffer, uint32_t length)
224 {
225  serial_uart_state_t *serialUartHandle;
226 #if (defined(HAL_UART_TRANSFER_MODE) && (HAL_UART_TRANSFER_MODE > 0U))
227  hal_uart_transfer_t transfer;
228 #endif
229 
230  assert(serialHandle);
231  assert(buffer);
232  assert(length);
233 
234  serialUartHandle = (serial_uart_state_t *)serialHandle;
235 
236  if (0U != serialUartHandle->tx.busy)
237  {
239  }
240  serialUartHandle->tx.busy = 1U;
241 
242  serialUartHandle->tx.buffer = buffer;
243  serialUartHandle->tx.length = length;
244 
245 #if (defined(HAL_UART_TRANSFER_MODE) && (HAL_UART_TRANSFER_MODE > 0U))
246  transfer.data = buffer;
247  transfer.dataSize = length;
249  HAL_UartTransferSendNonBlocking(((hal_uart_handle_t)&serialUartHandle->usartHandleBuffer[0]), &transfer))
250 #else
252  HAL_UartSendNonBlocking(((hal_uart_handle_t)&serialUartHandle->usartHandleBuffer[0]), buffer, length))
253 #endif
254  {
255  serialUartHandle->tx.busy = 0U;
257  }
259 }
260 
261 #else
262 
263 serial_manager_status_t Serial_UartWrite(serial_handle_t serialHandle, uint8_t *buffer, uint32_t length)
264 {
265  serial_uart_state_t *serialUartHandle;
266 
267  assert(serialHandle);
268  assert(buffer);
269  assert(length);
270 
271  serialUartHandle = (serial_uart_state_t *)serialHandle;
272 
273  return (serial_manager_status_t)HAL_UartSendBlocking(((hal_uart_handle_t)&serialUartHandle->usartHandleBuffer[0]),
274  buffer, length);
275 }
276 
277 serial_manager_status_t Serial_UartRead(serial_handle_t serialHandle, uint8_t *buffer, uint32_t length)
278 {
279  serial_uart_state_t *serialUartHandle;
280 
281  assert(serialHandle);
282  assert(buffer);
283  assert(length);
284 
285  serialUartHandle = (serial_uart_state_t *)serialHandle;
286 
288  ((hal_uart_handle_t)&serialUartHandle->usartHandleBuffer[0]), buffer, length);
289 }
290 
291 #endif
292 
293 #if (defined(SERIAL_MANAGER_NON_BLOCKING_MODE) && (SERIAL_MANAGER_NON_BLOCKING_MODE > 0U))
294 serial_manager_status_t Serial_UartCancelWrite(serial_handle_t serialHandle)
295 {
296  serial_uart_state_t *serialUartHandle;
298  uint32_t primask;
299  uint8_t isBusy = 0U;
300 
301  assert(serialHandle);
302 
303  serialUartHandle = (serial_uart_state_t *)serialHandle;
304 
305  primask = DisableGlobalIRQ();
306  isBusy = serialUartHandle->tx.busy;
307  serialUartHandle->tx.busy = 0U;
308  EnableGlobalIRQ(primask);
309 
310 #if (defined(HAL_UART_TRANSFER_MODE) && (HAL_UART_TRANSFER_MODE > 0U))
311  (void)HAL_UartTransferAbortSend(((hal_uart_handle_t)&serialUartHandle->usartHandleBuffer[0]));
312 #else
313  (void)HAL_UartAbortSend(((hal_uart_handle_t)&serialUartHandle->usartHandleBuffer[0]));
314 #endif
315  if (0U != isBusy)
316  {
317  if ((NULL != serialUartHandle->tx.callback))
318  {
319  msg.buffer = serialUartHandle->tx.buffer;
320  msg.length = serialUartHandle->tx.length;
321  serialUartHandle->tx.callback(serialUartHandle->tx.callbackParam, &msg, kStatus_SerialManager_Canceled);
322  }
323  }
325 }
326 
327 serial_manager_status_t Serial_UartInstallTxCallback(serial_handle_t serialHandle,
328  serial_manager_callback_t callback,
329  void *callbackParam)
330 {
331  serial_uart_state_t *serialUartHandle;
332 
333  assert(serialHandle);
334 
335  serialUartHandle = (serial_uart_state_t *)serialHandle;
336 
337  serialUartHandle->tx.callback = callback;
338  serialUartHandle->tx.callbackParam = callbackParam;
339 
341 }
342 
343 serial_manager_status_t Serial_UartInstallRxCallback(serial_handle_t serialHandle,
344  serial_manager_callback_t callback,
345  void *callbackParam)
346 {
347  serial_uart_state_t *serialUartHandle;
348 
349  assert(serialHandle);
350 
351  serialUartHandle = (serial_uart_state_t *)serialHandle;
352 
353  serialUartHandle->rx.callback = callback;
354  serialUartHandle->rx.callbackParam = callbackParam;
355 
357 }
358 
359 void Serial_UartIsrFunction(serial_handle_t serialHandle)
360 {
361  serial_uart_state_t *serialUartHandle;
362 
363  assert(serialHandle);
364 
365  serialUartHandle = (serial_uart_state_t *)serialHandle;
366 
367  HAL_UartIsrFunction(((hal_uart_handle_t)&serialUartHandle->usartHandleBuffer[0]));
368 }
369 #endif
370 
371 serial_manager_status_t Serial_UartEnterLowpower(serial_handle_t serialHandle)
372 {
373  serial_uart_state_t *serialUartHandle;
374 
375  assert(serialHandle);
376 
377  serialUartHandle = (serial_uart_state_t *)serialHandle;
378 
379  if (kStatus_HAL_UartSuccess != HAL_UartEnterLowpower(((hal_uart_handle_t)&serialUartHandle->usartHandleBuffer[0])))
380  {
382  }
383 
385 }
386 
387 serial_manager_status_t Serial_UartExitLowpower(serial_handle_t serialHandle)
388 {
389  serial_uart_state_t *serialUartHandle;
390 
391  assert(serialHandle);
392 
393  serialUartHandle = (serial_uart_state_t *)serialHandle;
394 
395  if (kStatus_HAL_UartSuccess != HAL_UartExitLowpower(((hal_uart_handle_t)&serialUartHandle->usartHandleBuffer[0])))
396  {
398  }
399 
401 }
402 
403 #endif
fsl_common.h
_serial_port_uart_config::stopBitCount
serial_port_uart_stop_bit_count_t stopBitCount
Definition: serial_port_uart.h:50
HAL_UartInit
hal_uart_status_t HAL_UartInit(hal_uart_handle_t handle, hal_uart_config_t *config)
Initializes a UART instance with the UART handle and the user configuration structure.
Definition: lpuart_adapter.c:226
serial_manager_status_t
enum _serial_manager_status serial_manager_status_t
serial manager error code
_serial_manager_callback_message::length
uint32_t length
Definition: serial_manager.h:255
NULL
#define NULL
Definition: porcupine/demo/c/dr_libs/tests/external/miniaudio/extras/speex_resampler/thirdparty/resample.c:92
hal_uart_handle_t
void * hal_uart_handle_t
The handle of uart adapter.
Definition: uart.h:97
_hal_uart_config
UART configuration structure.
Definition: uart.h:132
_serial_port_uart_config::baudRate
uint32_t baudRate
Definition: serial_port_uart.h:48
_serial_manager_callback_message::buffer
uint8_t * buffer
Definition: serial_manager.h:254
kStatus_SerialManager_Canceled
@ kStatus_SerialManager_Canceled
Definition: serial_manager.h:243
serial_handle_t
void * serial_handle_t
The handle of the serial manager module.
Definition: serial_manager.h:206
serial_port_internal.h
kStatus_SerialManager_Error
@ kStatus_SerialManager_Error
Definition: serial_manager.h:240
_serial_port_uart_config::enableRx
uint8_t enableRx
Definition: serial_port_uart.h:53
HAL_UartDeinit
hal_uart_status_t HAL_UartDeinit(hal_uart_handle_t handle)
Deinitializes a UART instance.
Definition: lpuart_adapter.c:310
_serial_port_uart_config::enableTx
uint8_t enableTx
Definition: serial_port_uart.h:54
serial_manager.h
HAL_UartSendBlocking
hal_uart_status_t HAL_UartSendBlocking(hal_uart_handle_t handle, const uint8_t *data, size_t length)
Writes to the TX register using a blocking method.
Definition: lpuart_adapter.c:353
uart.h
_hal_uart_transfer
UART transfer structure.
Definition: uart.h:149
kStatus_SerialManager_Success
@ kStatus_SerialManager_Success
Definition: serial_manager.h:239
_serial_port_uart_config
serial port uart config struct
Definition: serial_port_uart.h:45
kStatus_HAL_UartTxIdle
@ kStatus_HAL_UartTxIdle
Definition: uart.h:105
_serial_manager_callback_message
Callback message structure.
Definition: serial_manager.h:252
HAL_UartEnterLowpower
hal_uart_status_t HAL_UartEnterLowpower(hal_uart_handle_t handle)
Prepares to enter low power consumption.
Definition: lpuart_adapter.c:374
_hal_uart_transfer::data
uint8_t * data
Definition: uart.h:151
_hal_uart_transfer::dataSize
size_t dataSize
Definition: uart.h:152
UART_HANDLE_DEFINE
#define UART_HANDLE_DEFINE(name)
Defines the uart handle.
Definition: uart.h:89
serial_port_uart.h
_serial_port_uart_config::parityMode
serial_port_uart_parity_mode_t parityMode
Definition: serial_port_uart.h:49
kStatus_SerialManager_Busy
@ kStatus_SerialManager_Busy
Definition: serial_manager.h:241
hal_uart_parity_mode_t
enum _hal_uart_parity_mode hal_uart_parity_mode_t
UART parity mode.
HAL_UartReceiveBlocking
hal_uart_status_t HAL_UartReceiveBlocking(hal_uart_handle_t handle, uint8_t *data, size_t length)
Reads RX data register using a blocking method.
Definition: lpuart_adapter.c:331
_serial_port_uart_config::instance
uint8_t instance
Definition: serial_port_uart.h:51
DisableGlobalIRQ
static uint32_t DisableGlobalIRQ(void)
Disable the global IRQ.
Definition: fsl_common.h:552
HAL_UartExitLowpower
hal_uart_status_t HAL_UartExitLowpower(hal_uart_handle_t handle)
Restores from low power consumption.
Definition: lpuart_adapter.c:381
_serial_port_uart_config::clockRate
uint32_t clockRate
Definition: serial_port_uart.h:47
SERIAL_PORT_UART_HANDLE_SIZE
#define SERIAL_PORT_UART_HANDLE_SIZE
serial port uart handle size
Definition: serial_port_uart.h:26
kStatus_HAL_UartSuccess
@ kStatus_HAL_UartSuccess
Definition: uart.h:102
serial_manager_callback_t
void(* serial_manager_callback_t)(void *callbackParam, serial_manager_callback_message_t *message, serial_manager_status_t status)
callback function
Definition: serial_manager.h:259
config
static sai_transceiver_t config
Definition: imxrt1050/imxrt1050-evkb/source/pv_audio_rec.c:75
hal_uart_status_t
enum _hal_uart_status hal_uart_status_t
UART status.
kStatus_HAL_UartRxIdle
@ kStatus_HAL_UartRxIdle
Definition: uart.h:106
hal_uart_stop_bit_count_t
enum _hal_uart_stop_bit_count hal_uart_stop_bit_count_t
UART stop bit count.
EnableGlobalIRQ
static void EnableGlobalIRQ(uint32_t primask)
Enable the global IRQ.
Definition: fsl_common.h:583


picovoice_driver
Author(s):
autogenerated on Fri Apr 1 2022 02:14:50