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_USART_FREERTOS_H__
9 #define FSL_USART_FREERTOS_H__
10 
11 #include "fsl_usart.h"
12 #include <FreeRTOS.h>
13 #include <event_groups.h>
14 #include <semphr.h>
15 
16 /*!
17  * @addtogroup usart_freertos_driver
18  * @{
19  */
20 
21 /*! @file*/
22 
23 /*******************************************************************************
24  * Definitions
25  ******************************************************************************/
26 
27 /*! @name Driver version */
28 /*! @{ */
29 /*! @brief USART FreeRTOS driver version. */
30 #define FSL_USART_FREERTOS_DRIVER_VERSION (MAKE_VERSION(2, 6, 0))
31 /*! @} */
32 
33 /*! @brief FLEX USART configuration structure */
34 struct rtos_usart_config
35 {
36     USART_Type *base;                /*!< USART base address */
37     uint32_t srcclk;                 /*!< USART source clock in Hz*/
38     uint32_t baudrate;               /*!< Desired communication speed */
39     usart_parity_mode_t parity;      /*!< Parity setting */
40     usart_stop_bit_count_t stopbits; /*!< Number of stop bits to use */
41     uint8_t *buffer;                 /*!< Buffer for background reception */
42     uint32_t buffer_size;            /*!< Size of buffer for background reception */
43 };
44 
45 /*! @brief FLEX USART FreeRTOS handle */
46 typedef struct _usart_rtos_handle
47 {
48     USART_Type *base;              /*!< USART base address */
49     usart_transfer_t txTransfer;   /*!< TX transfer structure */
50     usart_transfer_t rxTransfer;   /*!< RX transfer structure */
51     SemaphoreHandle_t rxSemaphore; /*!< RX semaphore for resource sharing */
52     SemaphoreHandle_t txSemaphore; /*!< TX semaphore for resource sharing */
53 #define RTOS_USART_COMPLETE                0x1U
54 #define RTOS_USART_RING_BUFFER_OVERRUN     0x2U
55 #define RTOS_USART_HARDWARE_BUFFER_OVERRUN 0x4U
56     EventGroupHandle_t rxEvent; /*!< RX completion event */
57     EventGroupHandle_t txEvent; /*!< TX completion event */
58     void *t_state;              /*!< Transactional state of the underlying driver */
59 } usart_rtos_handle_t;
60 
61 /*******************************************************************************
62  * API
63  ******************************************************************************/
64 
65 #if defined(__cplusplus)
66 extern "C" {
67 #endif
68 
69 /*!
70  * @name USART RTOS Operation
71  * @{
72  */
73 
74 /*!
75  * @brief Initializes a USART instance for operation in RTOS.
76  *
77  * @param handle The RTOS USART handle, the pointer to allocated space for RTOS context.
78  * @param t_handle The pointer to allocated space where to store transactional layer internal state.
79  * @param cfg The pointer to the parameters required to configure the USART after initialization.
80  * @return 0 succeed, others fail.
81  */
82 int USART_RTOS_Init(usart_rtos_handle_t *handle, usart_handle_t *t_handle, const struct rtos_usart_config *cfg);
83 
84 /*!
85  * @brief Deinitializes a USART instance for operation.
86  *
87  * This function deinitializes the USART module, sets all register values to reset value,
88  * and releases the resources.
89  *
90  * @param handle The RTOS USART handle.
91  */
92 int USART_RTOS_Deinit(usart_rtos_handle_t *handle);
93 
94 /*!
95  * @name USART transactional Operation
96  * @{
97  */
98 
99 /*!
100  * @brief Sends data in the background.
101  *
102  * This function sends data. It is a synchronous API.
103  * If the hardware buffer is full, the task is in the blocked state.
104  *
105  * @param handle The RTOS USART handle.
106  * @param buffer The pointer to buffer to send.
107  * @param length The number of bytes to send.
108  */
109 int USART_RTOS_Send(usart_rtos_handle_t *handle, uint8_t *buffer, uint32_t length);
110 
111 /*!
112  * @brief Receives data.
113  *
114  * This function receives data from USART. It is a synchronous API. If data is immediately available,
115  * it is returned immediately and the number of bytes received.
116  *
117  * @param handle The RTOS USART handle.
118  * @param buffer The pointer to buffer where to write received data.
119  * @param length The number of bytes to receive.
120  * @param received The pointer to a variable of size_t where the number of received data is filled.
121  */
122 int USART_RTOS_Receive(usart_rtos_handle_t *handle, uint8_t *buffer, uint32_t length, size_t *received);
123 
124 /*! @} */
125 
126 #if defined(__cplusplus)
127 }
128 #endif
129 
130 /*! @}*/
131 
132 #endif /* FSL_USART_FREERTOS_H__ */
133