]> git.sur5r.net Git - freertos/blob - FreeRTOS/Demo/CORTEX_M0+_LPC51U68_LPCXpresso/component/uart/uart.h
8fe377790f9edd9e092943752e34fcdf667a4a84
[freertos] / FreeRTOS / Demo / CORTEX_M0+_LPC51U68_LPCXpresso / component / uart / uart.h
1 /*\r
2  * Copyright 2018 NXP\r
3  * All rights reserved.\r
4  *\r
5  *\r
6  * SPDX-License-Identifier: BSD-3-Clause\r
7  */\r
8 \r
9 #ifndef __HAL_UART_ADAPTER_H__\r
10 #define __HAL_UART_ADAPTER_H__\r
11 \r
12 #if defined(FSL_RTOS_FREE_RTOS)\r
13 #include "FreeRTOS.h"\r
14 #endif\r
15 \r
16 /*!\r
17  * @addtogroup UART_Adapter\r
18  * @{\r
19  */\r
20 \r
21 /*******************************************************************************\r
22  * Definitions\r
23  ******************************************************************************/\r
24 \r
25 /*! @brief Enable or disable UART adapter non-blocking mode (1 - enable, 0 - disable) */\r
26 #ifdef DEBUG_CONSOLE_TRANSFER_NON_BLOCKING\r
27 #define UART_ADAPTER_NON_BLOCKING_MODE (1U)\r
28 #else\r
29 #ifndef SERIAL_MANAGER_NON_BLOCKING_MODE\r
30 #define UART_ADAPTER_NON_BLOCKING_MODE (0U)\r
31 #else\r
32 #define UART_ADAPTER_NON_BLOCKING_MODE SERIAL_MANAGER_NON_BLOCKING_MODE\r
33 #endif\r
34 #endif\r
35 \r
36 #if defined(__GIC_PRIO_BITS)\r
37 #define HAL_UART_ISR_PRIORITY (25U)\r
38 #else\r
39 #if defined(configLIBRARY_MAX_SYSCALL_INTERRUPT_PRIORITY)\r
40 #define HAL_UART_ISR_PRIORITY (configLIBRARY_MAX_SYSCALL_INTERRUPT_PRIORITY)\r
41 #else\r
42 /* The default value 3 is used to support different ARM Core, such as CM0P, CM4, CM7, and CM33, etc.\r
43  * The minimum number of priority bits implemented in the NVIC is 2 on these SOCs. The value of mininum\r
44  * priority is 3 (2^2 - 1). So, the default value is 3.\r
45  */\r
46 #define HAL_UART_ISR_PRIORITY (3U)\r
47 #endif\r
48 #endif\r
49 \r
50 #if (defined(UART_ADAPTER_NON_BLOCKING_MODE) && (UART_ADAPTER_NON_BLOCKING_MODE > 0U))\r
51 #define HAL_UART_HANDLE_SIZE (90U)\r
52 #else\r
53 #define HAL_UART_HANDLE_SIZE (4U)\r
54 #endif\r
55 \r
56 /*! @brief Whether enable transactional function of the UART. (0 - disable, 1 - enable) */\r
57 #define HAL_UART_TRANSFER_MODE (0U)\r
58 \r
59 typedef void *hal_uart_handle_t;\r
60 \r
61 /*! @brief UART status */\r
62 typedef enum _hal_uart_status\r
63 {\r
64     kStatus_HAL_UartSuccess = kStatus_Success,                       /*!< Successfully */\r
65     kStatus_HAL_UartTxBusy  = MAKE_STATUS(kStatusGroup_HAL_UART, 1), /*!< TX busy */\r
66     kStatus_HAL_UartRxBusy  = MAKE_STATUS(kStatusGroup_HAL_UART, 2), /*!< RX busy */\r
67     kStatus_HAL_UartTxIdle  = MAKE_STATUS(kStatusGroup_HAL_UART, 3), /*!< HAL UART transmitter is idle. */\r
68     kStatus_HAL_UartRxIdle  = MAKE_STATUS(kStatusGroup_HAL_UART, 4), /*!< HAL UART receiver is idle */\r
69     kStatus_HAL_UartBaudrateNotSupport =\r
70         MAKE_STATUS(kStatusGroup_HAL_UART, 5), /*!< Baudrate is not support in current clock source */\r
71     kStatus_HAL_UartProtocolError = MAKE_STATUS(\r
72         kStatusGroup_HAL_UART,\r
73         6),                                                        /*!< Error occurs for Noise, Framing, Parity, etc.\r
74                                                                         For transactional transfer, The up layer needs to abort the transfer and then starts again */\r
75     kStatus_HAL_UartError = MAKE_STATUS(kStatusGroup_HAL_UART, 7), /*!< Error occurs on HAL UART */\r
76 } hal_uart_status_t;\r
77 \r
78 /*! @brief UART parity mode. */\r
79 typedef enum _hal_uart_parity_mode\r
80 {\r
81     kHAL_UartParityDisabled = 0x0U, /*!< Parity disabled */\r
82     kHAL_UartParityEven     = 0x1U, /*!< Parity even enabled */\r
83     kHAL_UartParityOdd      = 0x2U, /*!< Parity odd enabled */\r
84 } hal_uart_parity_mode_t;\r
85 \r
86 /*! @brief UART stop bit count. */\r
87 typedef enum _hal_uart_stop_bit_count\r
88 {\r
89     kHAL_UartOneStopBit = 0U, /*!< One stop bit */\r
90     kHAL_UartTwoStopBit = 1U, /*!< Two stop bits */\r
91 } hal_uart_stop_bit_count_t;\r
92 \r
93 /*! @brief UART configuration structure. */\r
94 typedef struct _hal_uart_config\r
95 {\r
96     uint32_t srcClock_Hz;                   /*!< Source clock */\r
97     uint32_t baudRate_Bps;                  /*!< Baud rate  */\r
98     hal_uart_parity_mode_t parityMode;      /*!< Parity mode, disabled (default), even, odd */\r
99     hal_uart_stop_bit_count_t stopBitCount; /*!< Number of stop bits, 1 stop bit (default) or 2 stop bits  */\r
100     uint8_t enableRx;                       /*!< Enable RX */\r
101     uint8_t enableTx;                       /*!< Enable TX */\r
102     uint8_t instance; /*!< Instance (0 - UART0, 1 - UART1, ...), detail information please refer to the\r
103                            SOC corresponding RM.\r
104                            Invalid instance value will cause initialization failure. */\r
105 } hal_uart_config_t;\r
106 \r
107 /*! @brief UART transfer callback function. */\r
108 typedef void (*hal_uart_transfer_callback_t)(hal_uart_handle_t handle, hal_uart_status_t status, void *callbackParam);\r
109 \r
110 /*! @brief UART transfer structure. */\r
111 typedef struct _hal_uart_transfer\r
112 {\r
113     uint8_t *data;   /*!< The buffer of data to be transfer.*/\r
114     size_t dataSize; /*!< The byte count to be transfer. */\r
115 } hal_uart_transfer_t;\r
116 \r
117 /*******************************************************************************\r
118  * API\r
119  ******************************************************************************/\r
120 \r
121 #if defined(__cplusplus)\r
122 extern "C" {\r
123 #endif /* _cplusplus */\r
124 \r
125 /*!\r
126  * @name Initialization and deinitialization\r
127  * @{\r
128  */\r
129 \r
130 /*!\r
131  * @brief Initializes a UART instance with the UART handle and the user configuration structure.\r
132  *\r
133  * This function configures the UART module with user-defined settings. The user can configure the configuration\r
134  * structure. The parameter handle is a pointer to point to a memory space of size #HAL_UART_HANDLE_SIZE allocated by\r
135  * the caller. Example below shows how to use this API to configure the UART.\r
136  *  @code\r
137  *   uint8_t g_UartHandleBuffer[HAL_UART_HANDLE_SIZE];\r
138  *   hal_uart_handle_t g_UartHandle = &g_UartHandleBuffer[0];\r
139  *   hal_uart_config_t config;\r
140  *   config.srcClock_Hz = 48000000;\r
141  *   config.baudRate_Bps = 115200U;\r
142  *   config.parityMode = kHAL_UartParityDisabled;\r
143  *   config.stopBitCount = kHAL_UartOneStopBit;\r
144  *   config.enableRx = 1;\r
145  *   config.enableTx = 1;\r
146  *   config.instance = 0;\r
147  *   HAL_UartInit(g_UartHandle, &config);\r
148  *  @endcode\r
149  *\r
150  * @param handle Pointer to point to a memory space of size #HAL_UART_HANDLE_SIZE allocated by the caller.\r
151  * @param config Pointer to user-defined configuration structure.\r
152  * @retval kStatus_HAL_UartBaudrateNotSupport Baudrate is not support in current clock source.\r
153  * @retval kStatus_HAL_UartSuccess UART initialization succeed\r
154  */\r
155 hal_uart_status_t HAL_UartInit(hal_uart_handle_t handle, hal_uart_config_t *config);\r
156 \r
157 /*!\r
158  * @brief Deinitializes a UART instance.\r
159  *\r
160  * This function waits for TX complete, disables TX and RX, and disables the UART clock.\r
161  *\r
162  * @param handle UART handle pointer.\r
163  * @retval kStatus_HAL_UartSuccess UART de-initialization succeed\r
164  */\r
165 hal_uart_status_t HAL_UartDeinit(hal_uart_handle_t handle);\r
166 \r
167 /*! @}*/\r
168 \r
169 /*!\r
170  * @name Blocking bus Operations\r
171  * @{\r
172  */\r
173 \r
174 /*!\r
175  * @brief Reads RX data register using a blocking method.\r
176  *\r
177  * This function polls the RX register, waits for the RX register to be full or for RX FIFO to\r
178  * have data, and reads data from the RX register.\r
179  *\r
180  * @note The function #HAL_UartReceiveBlocking and the function #HAL_UartTransferReceiveNonBlocking\r
181  * cannot be used at the same time.\r
182  * And, the function #HAL_UartTransferAbortReceive cannot be used to abort the transmission of this function.\r
183  *\r
184  * @param handle UART handle pointer.\r
185  * @param data Start address of the buffer to store the received data.\r
186  * @param length Size of the buffer.\r
187  * @retval kStatus_HAL_UartError An error occurred while receiving data.\r
188  * @retval kStatus_HAL_UartParityError A parity error occurred while receiving data.\r
189  * @retval kStatus_HAL_UartSuccess Successfully received all data.\r
190  */\r
191 hal_uart_status_t HAL_UartReceiveBlocking(hal_uart_handle_t handle, uint8_t *data, size_t length);\r
192 \r
193 /*!\r
194  * @brief Writes to the TX register using a blocking method.\r
195  *\r
196  * This function polls the TX register, waits for the TX register to be empty or for the TX FIFO\r
197  * to have room and writes data to the TX buffer.\r
198  *\r
199  * @note The function #HAL_UartSendBlocking and the function #HAL_UartTransferSendNonBlocking\r
200  * cannot be used at the same time.\r
201  * And, the function #HAL_UartTransferAbortSend cannot be used to abort the transmission of this function.\r
202  *\r
203  * @param handle UART handle pointer.\r
204  * @param data Start address of the data to write.\r
205  * @param length Size of the data to write.\r
206  * @retval kStatus_HAL_UartSuccess Successfully sent all data.\r
207  */\r
208 hal_uart_status_t HAL_UartSendBlocking(hal_uart_handle_t handle, const uint8_t *data, size_t length);\r
209 \r
210 /*! @}*/\r
211 \r
212 #if (defined(UART_ADAPTER_NON_BLOCKING_MODE) && (UART_ADAPTER_NON_BLOCKING_MODE > 0U))\r
213 #if (defined(HAL_UART_TRANSFER_MODE) && (HAL_UART_TRANSFER_MODE > 0U))\r
214 \r
215 /*!\r
216  * @name Transactional\r
217  * @note The transactional API and the functional API cannot be used at the same time. The macro\r
218  * #HAL_UART_TRANSFER_MODE is used to set which one will be used. If #HAL_UART_TRANSFER_MODE is zero, the\r
219  * functional API with non-blocking mode will be used. Otherwise, transactional API will be used.\r
220  * @{\r
221  */\r
222 \r
223 /*!\r
224  * @brief Installs a callback and callback parameter.\r
225  *\r
226  * This function is used to install the callback and callback parameter for UART module.\r
227  * When any status of the UART changed, the driver will notify the upper layer by the installed callback\r
228  * function. And the status is also passed as status parameter when the callback is called.\r
229  *\r
230  * @param handle UART handle pointer.\r
231  * @param callback The callback function.\r
232  * @param callbackParam The parameter of the callback function.\r
233  * @retval kStatus_HAL_UartSuccess Successfully install the callback.\r
234  */\r
235 hal_uart_status_t HAL_UartTransferInstallCallback(hal_uart_handle_t handle,\r
236                                                   hal_uart_transfer_callback_t callback,\r
237                                                   void *callbackParam);\r
238 \r
239 /*!\r
240  * @brief Receives a buffer of data using an interrupt method.\r
241  *\r
242  * This function receives data using an interrupt method. This is a non-blocking function, which\r
243  * returns directly without waiting for all data to be received.\r
244  * The receive request is saved by the UART driver.\r
245  * When the new data arrives, the receive request is serviced first.\r
246  * When all data is received, the UART driver notifies the upper layer\r
247  * through a callback function and passes the status parameter @ref kStatus_UART_RxIdle.\r
248  *\r
249  * @note The function #HAL_UartReceiveBlocking and the function #HAL_UartTransferReceiveNonBlocking\r
250  * cannot be used at the same time.\r
251  *\r
252  * @param handle UART handle pointer.\r
253  * @param transfer UART transfer structure, see #hal_uart_transfer_t.\r
254  * @retval kStatus_HAL_UartSuccess Successfully queue the transfer into transmit queue.\r
255  * @retval kStatus_HAL_UartRxBusy Previous receive request is not finished.\r
256  * @retval kStatus_HAL_UartError An error occurred.\r
257  */\r
258 hal_uart_status_t HAL_UartTransferReceiveNonBlocking(hal_uart_handle_t handle, hal_uart_transfer_t *transfer);\r
259 \r
260 /*!\r
261  * @brief Transmits a buffer of data using the interrupt method.\r
262  *\r
263  * This function sends data using an interrupt method. This is a non-blocking function, which\r
264  * returns directly without waiting for all data to be written to the TX register. When\r
265  * all data is written to the TX register in the ISR, the UART driver calls the callback\r
266  * function and passes the @ref kStatus_UART_TxIdle as status parameter.\r
267  *\r
268  * @note The function #HAL_UartSendBlocking and the function #HAL_UartTransferSendNonBlocking\r
269  * cannot be used at the same time.\r
270  *\r
271  * @param handle UART handle pointer.\r
272  * @param transfer UART transfer structure. See #hal_uart_transfer_t.\r
273  * @retval kStatus_HAL_UartSuccess Successfully start the data transmission.\r
274  * @retval kStatus_HAL_UartTxBusy Previous transmission still not finished; data not all written to TX register yet.\r
275  * @retval kStatus_HAL_UartError An error occurred.\r
276  */\r
277 hal_uart_status_t HAL_UartTransferSendNonBlocking(hal_uart_handle_t handle, hal_uart_transfer_t *transfer);\r
278 \r
279 /*!\r
280  * @brief Gets the number of bytes that have been received.\r
281  *\r
282  * This function gets the number of bytes that have been received.\r
283  *\r
284  * @param handle UART handle pointer.\r
285  * @param count Receive bytes count.\r
286  * @retval kStatus_HAL_UartError An error occurred.\r
287  * @retval kStatus_Success Get successfully through the parameter \p count.\r
288  */\r
289 hal_uart_status_t HAL_UartTransferGetReceiveCount(hal_uart_handle_t handle, uint32_t *count);\r
290 \r
291 /*!\r
292  * @brief Gets the number of bytes written to the UART TX register.\r
293  *\r
294  * This function gets the number of bytes written to the UART TX\r
295  * register by using the interrupt method.\r
296  *\r
297  * @param handle UART handle pointer.\r
298  * @param count Send bytes count.\r
299  * @retval kStatus_HAL_UartError An error occurred.\r
300  * @retval kStatus_Success Get successfully through the parameter \p count.\r
301  */\r
302 hal_uart_status_t HAL_UartTransferGetSendCount(hal_uart_handle_t handle, uint32_t *count);\r
303 \r
304 /*!\r
305  * @brief Aborts the interrupt-driven data receiving.\r
306  *\r
307  * This function aborts the interrupt-driven data receiving. The user can get the remainBytes to know\r
308  * how many bytes are not received yet.\r
309  *\r
310  * @note The function #HAL_UartTransferAbortReceive cannot be used to abort the transmission of\r
311  * the function #HAL_UartReceiveBlocking.\r
312  *\r
313  * @param handle UART handle pointer.\r
314  * @retval kStatus_Success Get successfully abort the receiving.\r
315  */\r
316 hal_uart_status_t HAL_UartTransferAbortReceive(hal_uart_handle_t handle);\r
317 \r
318 /*!\r
319  * @brief Aborts the interrupt-driven data sending.\r
320  *\r
321  * This function aborts the interrupt-driven data sending. The user can get the remainBytes to find out\r
322  * how many bytes are not sent out.\r
323  *\r
324  * @note The function #HAL_UartTransferAbortSend cannot be used to abort the transmission of\r
325  * the function #HAL_UartSendBlocking.\r
326  *\r
327  * @param handle UART handle pointer.\r
328  * @retval kStatus_Success Get successfully abort the sending.\r
329  */\r
330 hal_uart_status_t HAL_UartTransferAbortSend(hal_uart_handle_t handle);\r
331 \r
332 /*! @}*/\r
333 \r
334 #else\r
335 \r
336 /*!\r
337  * @name Functional API with non-blocking mode.\r
338  * @note The functional API and the transactional API cannot be used at the same time. The macro\r
339  * #HAL_UART_TRANSFER_MODE is used to set which one will be used. If #HAL_UART_TRANSFER_MODE is zero, the\r
340  * functional API with non-blocking mode will be used. Otherwise, transactional API will be used.\r
341  * @{\r
342  */\r
343 \r
344 /*!\r
345  * @brief Installs a callback and callback parameter.\r
346  *\r
347  * This function is used to install the callback and callback parameter for UART module.\r
348  * When non-blocking sending or receiving finished, the adapter will notify the upper layer by the installed callback\r
349  * function. And the status is also passed as status parameter when the callback is called.\r
350  *\r
351  * @param handle UART handle pointer.\r
352  * @param callback The callback function.\r
353  * @param callbackParam The parameter of the callback function.\r
354  * @retval kStatus_HAL_UartSuccess Successfully install the callback.\r
355  */\r
356 hal_uart_status_t HAL_UartInstallCallback(hal_uart_handle_t handle,\r
357                                           hal_uart_transfer_callback_t callback,\r
358                                           void *callbackParam);\r
359 \r
360 /*!\r
361  * @brief Receives a buffer of data using an interrupt method.\r
362  *\r
363  * This function receives data using an interrupt method. This is a non-blocking function, which\r
364  * returns directly without waiting for all data to be received.\r
365  * The receive request is saved by the UART adapter.\r
366  * When the new data arrives, the receive request is serviced first.\r
367  * When all data is received, the UART adapter notifies the upper layer\r
368  * through a callback function and passes the status parameter @ref kStatus_UART_RxIdle.\r
369  *\r
370  * @note The function #HAL_UartReceiveBlocking and the function #HAL_UartReceiveNonBlocking\r
371  * cannot be used at the same time.\r
372  *\r
373  * @param handle UART handle pointer.\r
374  * @param data Start address of the data to write.\r
375  * @param length Size of the data to write.\r
376  * @retval kStatus_HAL_UartSuccess Successfully queue the transfer into transmit queue.\r
377  * @retval kStatus_HAL_UartRxBusy Previous receive request is not finished.\r
378  * @retval kStatus_HAL_UartError An error occurred.\r
379  */\r
380 hal_uart_status_t HAL_UartReceiveNonBlocking(hal_uart_handle_t handle, uint8_t *data, size_t length);\r
381 \r
382 /*!\r
383  * @brief Transmits a buffer of data using the interrupt method.\r
384  *\r
385  * This function sends data using an interrupt method. This is a non-blocking function, which\r
386  * returns directly without waiting for all data to be written to the TX register. When\r
387  * all data is written to the TX register in the ISR, the UART driver calls the callback\r
388  * function and passes the @ref kStatus_UART_TxIdle as status parameter.\r
389  *\r
390  * @note The function #HAL_UartSendBlocking and the function #HAL_UartSendNonBlocking\r
391  * cannot be used at the same time.\r
392  *\r
393  * @param handle UART handle pointer.\r
394  * @param data Start address of the data to write.\r
395  * @param length Size of the data to write.\r
396  * @retval kStatus_HAL_UartSuccess Successfully start the data transmission.\r
397  * @retval kStatus_HAL_UartTxBusy Previous transmission still not finished; data not all written to TX register yet.\r
398  * @retval kStatus_HAL_UartError An error occurred.\r
399  */\r
400 hal_uart_status_t HAL_UartSendNonBlocking(hal_uart_handle_t handle, uint8_t *data, size_t length);\r
401 \r
402 /*!\r
403  * @brief Gets the number of bytes that have been received.\r
404  *\r
405  * This function gets the number of bytes that have been received.\r
406  *\r
407  * @param handle UART handle pointer.\r
408  * @param count Receive bytes count.\r
409  * @retval kStatus_HAL_UartError An error occurred.\r
410  * @retval kStatus_Success Get successfully through the parameter \p count.\r
411  */\r
412 hal_uart_status_t HAL_UartGetReceiveCount(hal_uart_handle_t handle, uint32_t *reCount);\r
413 \r
414 /*!\r
415  * @brief Gets the number of bytes written to the UART TX register.\r
416  *\r
417  * This function gets the number of bytes written to the UART TX\r
418  * register by using the interrupt method.\r
419  *\r
420  * @param handle UART handle pointer.\r
421  * @param count Send bytes count.\r
422  * @retval kStatus_HAL_UartError An error occurred.\r
423  * @retval kStatus_Success Get successfully through the parameter \p count.\r
424  */\r
425 hal_uart_status_t HAL_UartGetSendCount(hal_uart_handle_t handle, uint32_t *seCount);\r
426 \r
427 /*!\r
428  * @brief Aborts the interrupt-driven data receiving.\r
429  *\r
430  * This function aborts the interrupt-driven data receiving. The user can get the remainBytes to know\r
431  * how many bytes are not received yet.\r
432  *\r
433  * @note The function #HAL_UartAbortReceive cannot be used to abort the transmission of\r
434  * the function #HAL_UartReceiveBlocking.\r
435  *\r
436  * @param handle UART handle pointer.\r
437  * @retval kStatus_Success Get successfully abort the receiving.\r
438  */\r
439 hal_uart_status_t HAL_UartAbortReceive(hal_uart_handle_t handle);\r
440 \r
441 /*!\r
442  * @brief Aborts the interrupt-driven data sending.\r
443  *\r
444  * This function aborts the interrupt-driven data sending. The user can get the remainBytes to find out\r
445  * how many bytes are not sent out.\r
446  *\r
447  * @note The function #HAL_UartAbortSend cannot be used to abort the transmission of\r
448  * the function #HAL_UartSendBlocking.\r
449  *\r
450  * @param handle UART handle pointer.\r
451  * @retval kStatus_Success Get successfully abort the sending.\r
452  */\r
453 hal_uart_status_t HAL_UartAbortSend(hal_uart_handle_t handle);\r
454 \r
455 /*! @}*/\r
456 \r
457 #endif\r
458 #endif\r
459 \r
460 #if (defined(UART_ADAPTER_NON_BLOCKING_MODE) && (UART_ADAPTER_NON_BLOCKING_MODE > 0U))\r
461 /*!\r
462  * @brief UART IRQ handle function.\r
463  *\r
464  * This function handles the UART transmit and receive IRQ request.\r
465  *\r
466  * @param handle UART handle pointer.\r
467  */\r
468 void HAL_UartIsrFunction(hal_uart_handle_t handle);\r
469 #endif\r
470 \r
471 #if defined(__cplusplus)\r
472 }\r
473 #endif\r
474 /*! @}*/\r
475 #endif /* __HAL_UART_ADAPTER_H__ */\r