1 /*
2  * Copyright (c) 2015, Freescale Semiconductor, Inc.
3  * Copyright 2016-2020 NXP
4  * All rights reserved.
5  *
6  * SPDX-License-Identifier: BSD-3-Clause
7  */
8 #ifndef __FSL_LPUART_RTOS_H__
9 #define __FSL_LPUART_RTOS_H__
10 
11 #include "fsl_lpuart.h"
12 #include <FreeRTOS.h>
13 #include <event_groups.h>
14 #include <semphr.h>
15 
16 /*!
17  * @addtogroup lpuart_freertos_driver
18  * @{
19  */
20 
21 /*******************************************************************************
22  * Definitions
23  ******************************************************************************/
24 
25 /*! @name Driver version */
26 /*@{*/
27 /*! @brief LPUART FreeRTOS driver version. */
28 #define FSL_LPUART_FREERTOS_DRIVER_VERSION (MAKE_VERSION(2, 6, 0))
29 /*@}*/
30 
31 /*! @brief LPUART RTOS configuration structure. */
32 typedef struct _lpuart_rtos_config
33 {
34     LPUART_Type *base;                /*!< UART base address */
35     uint32_t srcclk;                  /*!< UART source clock in Hz*/
36     uint32_t baudrate;                /*!< Desired communication speed */
37     lpuart_parity_mode_t parity;      /*!< Parity setting */
38     lpuart_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     /* Zero in constant and multiplier is interpreted as infinit timeout. */
42     uint32_t rx_timeout_constant_ms;   /*!< RX timeout applied per receive */
43     uint32_t rx_timeout_multiplier_ms; /*!< RX timeout added for each byte of the receive. */
44     uint32_t tx_timeout_constant_ms;   /*!< TX timeout applied per transmition */
45     uint32_t tx_timeout_multiplier_ms; /*!< TX timeout added for each byte of the transmition. */
46 #if defined(FSL_FEATURE_LPUART_HAS_MODEM_SUPPORT) && FSL_FEATURE_LPUART_HAS_MODEM_SUPPORT
47     bool enableRxRTS;                         /*!< RX RTS enable */
48     bool enableTxCTS;                         /*!< TX CTS enable */
49     lpuart_transmit_cts_source_t txCtsSource; /*!< TX CTS source */
50     lpuart_transmit_cts_config_t txCtsConfig; /*!< TX CTS configure */
51 #endif
52 } lpuart_rtos_config_t;
53 
54 /*!
55  * @cond RTOS_PRIVATE
56  * @name LPUART event flags
57  *
58  * This are only valid states for txEvent and rxEvent (lpuart_rtos_handle_t).
59  */
60 /*@{*/
61 /*! @brief Event flag - uart transmit complete. */
62 #define RTOS_LPUART_TX_COMPLETE 0x1U
63 /*! @brief Event flag - uart receive complete. */
64 #define RTOS_LPUART_RX_COMPLETE 0x2U
65 /*! @brief Event flag - ring buffer overrun. */
66 #define RTOS_LPUART_RING_BUFFER_OVERRUN 0x4U
67 /*! @brief Event flag - hardware buffer overrun. */
68 #define RTOS_LPUART_HARDWARE_BUFFER_OVERRUN 0x8U
69 /*@}*/
70 
71 /*! @brief LPUART FreeRTOS transfer structure. */
72 typedef struct _lpuart_rtos_handle
73 {
74     LPUART_Type *base;                 /*!< UART base address */
75     lpuart_transfer_t txTransfer;      /*!< TX transfer structure */
76     lpuart_transfer_t rxTransfer;      /*!< RX transfer structure */
77     SemaphoreHandle_t rxSemaphore;     /*!< RX semaphore for resource sharing */
78     SemaphoreHandle_t txSemaphore;     /*!< TX semaphore for resource sharing */
79     EventGroupHandle_t rxEvent;        /*!< RX completion event */
80     EventGroupHandle_t txEvent;        /*!< TX completion event */
81     uint32_t rx_timeout_constant_ms;   /*!< RX Timeout applied per transfer */
82     uint32_t rx_timeout_multiplier_ms; /*!< RX Timeout added for each byte of the transfer. */
83     uint32_t tx_timeout_constant_ms;   /*!< TX Timeout applied per transfer */
84     uint32_t tx_timeout_multiplier_ms; /*!< TX Timeout added for each byte of the transfer. */
85     void *t_state;                     /*!< Transactional state of the underlying driver */
86 #if (configSUPPORT_STATIC_ALLOCATION == 1)
87     StaticSemaphore_t txSemaphoreBuffer; /*!< Statically allocated memory for txSemaphore */
88     StaticSemaphore_t rxSemaphoreBuffer; /*!< Statically allocated memory for rxSemaphore */
89     StaticEventGroup_t txEventBuffer;    /*!< Statically allocated memory for txEvent */
90     StaticEventGroup_t rxEventBuffer;    /*!< Statically allocated memory for rxEvent */
91 #endif
92 } lpuart_rtos_handle_t;
93 /*! \endcond */
94 
95 /*******************************************************************************
96  * API
97  ******************************************************************************/
98 
99 #if defined(__cplusplus)
100 extern "C" {
101 #endif
102 
103 /*!
104  * @name LPUART RTOS Operation
105  * @{
106  */
107 
108 /*!
109  * @brief Initializes an LPUART instance for operation in RTOS.
110  *
111  * @param handle The RTOS LPUART handle, the pointer to an allocated space for RTOS context.
112  * @param t_handle The pointer to an allocated space to store the transactional layer internal state.
113  * @param cfg The pointer to the parameters required to configure the LPUART after initialization.
114  * @return 0 succeed, others failed
115  */
116 int LPUART_RTOS_Init(lpuart_rtos_handle_t *handle, lpuart_handle_t *t_handle, const lpuart_rtos_config_t *cfg);
117 
118 /*!
119  * @brief Deinitializes an LPUART instance for operation.
120  *
121  * This function deinitializes the LPUART module, sets all register value to the reset value,
122  * and releases the resources.
123  *
124  * @param handle The RTOS LPUART handle.
125  */
126 int LPUART_RTOS_Deinit(lpuart_rtos_handle_t *handle);
127 
128 /*!
129  * @name LPUART transactional Operation
130  * @{
131  */
132 
133 /*!
134  * @brief Sends data in the background.
135  *
136  * This function sends data. It is an synchronous API.
137  * If the hardware buffer is full, the task is in the blocked state.
138  *
139  * @param handle The RTOS LPUART handle.
140  * @param buffer The pointer to buffer to send.
141  * @param length The number of bytes to send.
142  */
143 int LPUART_RTOS_Send(lpuart_rtos_handle_t *handle, uint8_t *buffer, uint32_t length);
144 
145 /*!
146  * @brief Receives data.
147  *
148  * This function receives data from LPUART. It is an synchronous API. If any data is immediately available
149  * it is returned immediately and the number of bytes received.
150  *
151  * @param handle The RTOS LPUART handle.
152  * @param buffer The pointer to buffer where to write received data.
153  * @param length The number of bytes to receive.
154  * @param received The pointer to a variable of size_t where the number of received data is filled.
155  */
156 int LPUART_RTOS_Receive(lpuart_rtos_handle_t *handle, uint8_t *buffer, uint32_t length, size_t *received);
157 
158 /*!
159  * @brief Set RX timeout in runtime
160  *
161  * This function can modify RX timeout between initialization and receive.
162  *
163  * param handle The RTOS LPUART handle.
164  * param rx_timeout_constant_ms RX timeout applied per receive.
165  * param rx_timeout_multiplier_ms RX timeout added for each byte of the receive.
166  */
167 int LPUART_RTOS_SetRxTimeout(lpuart_rtos_handle_t *handle,
168                              uint32_t rx_timeout_constant_ms,
169                              uint32_t rx_timeout_multiplier_ms);
170 
171 /*!
172  * @brief Set TX timeout in runtime
173  *
174  * This function can modify TX timeout between initialization and send.
175  *
176  * param handle The RTOS LPUART handle.
177  * param tx_timeout_constant_ms TX timeout applied per transmition.
178  * param tx_timeout_multiplier_ms TX timeout added for each byte of the transmition.
179  */
180 int LPUART_RTOS_SetTxTimeout(lpuart_rtos_handle_t *handle,
181                              uint32_t tx_timeout_constant_ms,
182                              uint32_t tx_timeout_multiplier_ms);
183 
184 /* @} */
185 
186 #if defined(__cplusplus)
187 }
188 #endif
189 
190 /*! @}*/
191 
192 #endif /* __FSL_LPUART_RTOS_H__ */
193