1 /*
2  * Copyright (c) 2016, Freescale Semiconductor, Inc.
3  * Copyright 2016-2020 NXP
4  * All rights reserved.
5  *
6  * SPDX-License-Identifier: BSD-3-Clause
7  */
8 #ifndef __FSL_UART_RTOS_H__
9 #define __FSL_UART_RTOS_H__
10 
11 #include "fsl_uart.h"
12 #include <FreeRTOS.h>
13 #include <event_groups.h>
14 #include <semphr.h>
15 
16 /*!
17  * @addtogroup uart_freertos_driver
18  * @{
19  */
20 
21 /*******************************************************************************
22  * Definitions
23  ******************************************************************************/
24 
25 /*! @name Driver version */
26 /*@{*/
27 /*! @brief UART FreeRTOS driver version 2.1.1. */
28 #define FSL_UART_FREERTOS_DRIVER_VERSION (MAKE_VERSION(2, 1, 1))
29 /*@}*/
30 
31 /*! @brief UART configuration structure */
32 typedef struct _uart_rtos_config
33 {
34     UART_Type *base;                /*!< UART base address */
35     uint32_t srcclk;                /*!< UART source clock in Hz*/
36     uint32_t baudrate;              /*!< Desired communication speed */
37     uart_parity_mode_t parity;      /*!< Parity setting */
38     uart_stop_bit_count_t stopbits; /*!< Number of stop bits to use */
39     uint8_t *buffer;                /*!< Buffer for background reception */
40     uint32_t buffer_size;           /*!< Size of buffer for background reception */
41 } uart_rtos_config_t;
42 
43 /*!
44  * @cond RTOS_PRIVATE
45  * @name UART FreeRTOS handler
46  *
47  * These are the only valid states for txEvent and rxEvent (uart_rtos_handle_t).
48  */
49 /*@{*/
50 /*! @brief Event flag - transfer complete. */
51 #define RTOS_UART_COMPLETE 0x1U
52 /*! @brief Event flag - ring buffer overrun. */
53 #define RTOS_UART_RING_BUFFER_OVERRUN 0x2U
54 /*! @brief Event flag - hardware buffer overrun. */
55 #define RTOS_UART_HARDWARE_BUFFER_OVERRUN 0x4U
56 /*@}*/
57 
58 /*! @brief UART FreeRTOS transfer structure. */
59 typedef struct _uart_rtos_handle
60 {
61     UART_Type *base;               /*!< UART base address */
62     uart_transfer_t txTransfer;    /*!< TX transfer structure */
63     uart_transfer_t rxTransfer;    /*!< RX transfer structure */
64     SemaphoreHandle_t rxSemaphore; /*!< RX semaphore for resource sharing */
65     SemaphoreHandle_t txSemaphore; /*!< TX semaphore for resource sharing */
66     EventGroupHandle_t rxEvent;    /*!< RX completion event */
67     EventGroupHandle_t txEvent;    /*!< TX completion event */
68     void *t_state;                 /*!< Transactional state of the underlying driver */
69 #if (configSUPPORT_STATIC_ALLOCATION == 1)
70     StaticSemaphore_t txSemaphoreBuffer; /*!< Statically allocated memory for txSemaphore */
71     StaticSemaphore_t rxSemaphoreBuffer; /*!< Statically allocated memory for rxSemaphore */
72     StaticEventGroup_t txEventBuffer;    /*!< Statically allocated memory for txEvent */
73     StaticEventGroup_t rxEventBuffer;    /*!< Statically allocated memory for rxEvent */
74 #endif
75 } uart_rtos_handle_t;
76 /*! \endcond */
77 
78 /*******************************************************************************
79  * API
80  ******************************************************************************/
81 
82 #if defined(__cplusplus)
83 extern "C" {
84 #endif
85 
86 /*!
87  * @name UART RTOS Operation
88  * @{
89  */
90 
91 /*!
92  * @brief Initializes a UART instance for operation in RTOS.
93  *
94  * @param handle The RTOS UART handle, the pointer to an allocated space for RTOS context.
95  * @param t_handle The pointer to the allocated space to store the transactional layer internal state.
96  * @param cfg The pointer to the parameters required to configure the UART after initialization.
97  * @return 0 succeed; otherwise fail.
98  */
99 int UART_RTOS_Init(uart_rtos_handle_t *handle, uart_handle_t *t_handle, const uart_rtos_config_t *cfg);
100 
101 /*!
102  * @brief Deinitializes a UART instance for operation.
103  *
104  * This function deinitializes the UART module, sets all register values to reset value,
105  * and frees the resources.
106  *
107  * @param handle The RTOS UART handle.
108  */
109 int UART_RTOS_Deinit(uart_rtos_handle_t *handle);
110 
111 /*!
112  * @name UART transactional Operation
113  * @{
114  */
115 
116 /*!
117  * @brief Sends data in the background.
118  *
119  * This function sends data. It is a synchronous API.
120  * If the hardware buffer is full, the task is in the blocked state.
121  *
122  * @param handle The RTOS UART handle.
123  * @param buffer The pointer to the buffer to send.
124  * @param length The number of bytes to send.
125  */
126 int UART_RTOS_Send(uart_rtos_handle_t *handle, uint8_t *buffer, uint32_t length);
127 
128 /*!
129  * @brief Receives data.
130  *
131  * This function receives data from UART. It is a synchronous API. If data is immediately available,
132  * it is returned immediately and the number of bytes received.
133  *
134  * @param handle The RTOS UART handle.
135  * @param buffer The pointer to the buffer to write received data.
136  * @param length The number of bytes to receive.
137  * @param received The pointer to a variable of size_t where the number of received data is filled.
138  */
139 int UART_RTOS_Receive(uart_rtos_handle_t *handle, uint8_t *buffer, uint32_t length, size_t *received);
140 
141 /* @} */
142 
143 #if defined(__cplusplus)
144 }
145 #endif
146 
147 /*! @}*/
148 
149 #endif /* __FSL_UART_RTOS_H__ */
150