summaryrefslogtreecommitdiffstats
path: root/bsps/arm/imxrt/mcux-sdk/drivers/lpuart/fsl_lpuart_freertos.h
blob: 05d842efd4db49b65adf1b41941a51117fd5b287 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
/*
 * Copyright (c) 2015, Freescale Semiconductor, Inc.
 * Copyright 2016-2020 NXP
 * All rights reserved.
 *
 * SPDX-License-Identifier: BSD-3-Clause
 */
#ifndef __FSL_LPUART_RTOS_H__
#define __FSL_LPUART_RTOS_H__

#include "fsl_lpuart.h"
#include <FreeRTOS.h>
#include <event_groups.h>
#include <semphr.h>

/*!
 * @addtogroup lpuart_freertos_driver
 * @{
 */

/*******************************************************************************
 * Definitions
 ******************************************************************************/

/*! @name Driver version */
/*@{*/
/*! @brief LPUART FreeRTOS driver version. */
#define FSL_LPUART_FREERTOS_DRIVER_VERSION (MAKE_VERSION(2, 6, 0))
/*@}*/

/*! @brief LPUART RTOS configuration structure. */
typedef struct _lpuart_rtos_config
{
    LPUART_Type *base;                /*!< UART base address */
    uint32_t srcclk;                  /*!< UART source clock in Hz*/
    uint32_t baudrate;                /*!< Desired communication speed */
    lpuart_parity_mode_t parity;      /*!< Parity setting */
    lpuart_stop_bit_count_t stopbits; /*!< Number of stop bits to use */
    uint8_t *buffer;                  /*!< Buffer for background reception */
    uint32_t buffer_size;             /*!< Size of buffer for background reception */
    /* Zero in constant and multiplier is interpreted as infinit timeout. */
    uint32_t rx_timeout_constant_ms;   /*!< RX timeout applied per receive */
    uint32_t rx_timeout_multiplier_ms; /*!< RX timeout added for each byte of the receive. */
    uint32_t tx_timeout_constant_ms;   /*!< TX timeout applied per transmition */
    uint32_t tx_timeout_multiplier_ms; /*!< TX timeout added for each byte of the transmition. */
#if defined(FSL_FEATURE_LPUART_HAS_MODEM_SUPPORT) && FSL_FEATURE_LPUART_HAS_MODEM_SUPPORT
    bool enableRxRTS;                         /*!< RX RTS enable */
    bool enableTxCTS;                         /*!< TX CTS enable */
    lpuart_transmit_cts_source_t txCtsSource; /*!< TX CTS source */
    lpuart_transmit_cts_config_t txCtsConfig; /*!< TX CTS configure */
#endif
} lpuart_rtos_config_t;

/*!
 * @cond RTOS_PRIVATE
 * @name LPUART event flags
 *
 * This are only valid states for txEvent and rxEvent (lpuart_rtos_handle_t).
 */
/*@{*/
/*! @brief Event flag - uart transmit complete. */
#define RTOS_LPUART_TX_COMPLETE 0x1U
/*! @brief Event flag - uart receive complete. */
#define RTOS_LPUART_RX_COMPLETE 0x2U
/*! @brief Event flag - ring buffer overrun. */
#define RTOS_LPUART_RING_BUFFER_OVERRUN 0x4U
/*! @brief Event flag - hardware buffer overrun. */
#define RTOS_LPUART_HARDWARE_BUFFER_OVERRUN 0x8U
/*@}*/

/*! @brief LPUART FreeRTOS transfer structure. */
typedef struct _lpuart_rtos_handle
{
    LPUART_Type *base;                 /*!< UART base address */
    lpuart_transfer_t txTransfer;      /*!< TX transfer structure */
    lpuart_transfer_t rxTransfer;      /*!< RX transfer structure */
    SemaphoreHandle_t rxSemaphore;     /*!< RX semaphore for resource sharing */
    SemaphoreHandle_t txSemaphore;     /*!< TX semaphore for resource sharing */
    EventGroupHandle_t rxEvent;        /*!< RX completion event */
    EventGroupHandle_t txEvent;        /*!< TX completion event */
    uint32_t rx_timeout_constant_ms;   /*!< RX Timeout applied per transfer */
    uint32_t rx_timeout_multiplier_ms; /*!< RX Timeout added for each byte of the transfer. */
    uint32_t tx_timeout_constant_ms;   /*!< TX Timeout applied per transfer */
    uint32_t tx_timeout_multiplier_ms; /*!< TX Timeout added for each byte of the transfer. */
    void *t_state;                     /*!< Transactional state of the underlying driver */
#if (configSUPPORT_STATIC_ALLOCATION == 1)
    StaticSemaphore_t txSemaphoreBuffer; /*!< Statically allocated memory for txSemaphore */
    StaticSemaphore_t rxSemaphoreBuffer; /*!< Statically allocated memory for rxSemaphore */
    StaticEventGroup_t txEventBuffer;    /*!< Statically allocated memory for txEvent */
    StaticEventGroup_t rxEventBuffer;    /*!< Statically allocated memory for rxEvent */
#endif
} lpuart_rtos_handle_t;
/*! \endcond */

/*******************************************************************************
 * API
 ******************************************************************************/

#if defined(__cplusplus)
extern "C" {
#endif

/*!
 * @name LPUART RTOS Operation
 * @{
 */

/*!
 * @brief Initializes an LPUART instance for operation in RTOS.
 *
 * @param handle The RTOS LPUART handle, the pointer to an allocated space for RTOS context.
 * @param t_handle The pointer to an allocated space to store the transactional layer internal state.
 * @param cfg The pointer to the parameters required to configure the LPUART after initialization.
 * @return 0 succeed, others failed
 */
int LPUART_RTOS_Init(lpuart_rtos_handle_t *handle, lpuart_handle_t *t_handle, const lpuart_rtos_config_t *cfg);

/*!
 * @brief Deinitializes an LPUART instance for operation.
 *
 * This function deinitializes the LPUART module, sets all register value to the reset value,
 * and releases the resources.
 *
 * @param handle The RTOS LPUART handle.
 */
int LPUART_RTOS_Deinit(lpuart_rtos_handle_t *handle);

/*!
 * @name LPUART transactional Operation
 * @{
 */

/*!
 * @brief Sends data in the background.
 *
 * This function sends data. It is an synchronous API.
 * If the hardware buffer is full, the task is in the blocked state.
 *
 * @param handle The RTOS LPUART handle.
 * @param buffer The pointer to buffer to send.
 * @param length The number of bytes to send.
 */
int LPUART_RTOS_Send(lpuart_rtos_handle_t *handle, uint8_t *buffer, uint32_t length);

/*!
 * @brief Receives data.
 *
 * This function receives data from LPUART. It is an synchronous API. If any data is immediately available
 * it is returned immediately and the number of bytes received.
 *
 * @param handle The RTOS LPUART handle.
 * @param buffer The pointer to buffer where to write received data.
 * @param length The number of bytes to receive.
 * @param received The pointer to a variable of size_t where the number of received data is filled.
 */
int LPUART_RTOS_Receive(lpuart_rtos_handle_t *handle, uint8_t *buffer, uint32_t length, size_t *received);

/*!
 * @brief Set RX timeout in runtime
 *
 * This function can modify RX timeout between initialization and receive.
 *
 * param handle The RTOS LPUART handle.
 * param rx_timeout_constant_ms RX timeout applied per receive.
 * param rx_timeout_multiplier_ms RX timeout added for each byte of the receive.
 */
int LPUART_RTOS_SetRxTimeout(lpuart_rtos_handle_t *handle,
                             uint32_t rx_timeout_constant_ms,
                             uint32_t rx_timeout_multiplier_ms);

/*!
 * @brief Set TX timeout in runtime
 *
 * This function can modify TX timeout between initialization and send.
 *
 * param handle The RTOS LPUART handle.
 * param tx_timeout_constant_ms TX timeout applied per transmition.
 * param tx_timeout_multiplier_ms TX timeout added for each byte of the transmition.
 */
int LPUART_RTOS_SetTxTimeout(lpuart_rtos_handle_t *handle,
                             uint32_t tx_timeout_constant_ms,
                             uint32_t tx_timeout_multiplier_ms);

/* @} */

#if defined(__cplusplus)
}
#endif

/*! @}*/

#endif /* __FSL_LPUART_RTOS_H__ */