rusEFI
The most advanced open source ECU
Loading...
Searching...
No Matches
hal_uart_lld.h
Go to the documentation of this file.
1/*
2 ChibiOS - Copyright (C) 2006..2018 Giovanni Di Sirio
3
4 Licensed under the Apache License, Version 2.0 (the "License");
5 you may not use this file except in compliance with the License.
6 You may obtain a copy of the License at
7
8 http://www.apache.org/licenses/LICENSE-2.0
9
10 Unless required by applicable law or agreed to in writing, software
11 distributed under the License is distributed on an "AS IS" BASIS,
12 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 See the License for the specific language governing permissions and
14 limitations under the License.
15*/
16
17/**
18 * @file hal_uart_lld.h
19 * @brief KINETIS UART subsystem low level driver header.
20 * @author andreika <prometheus.pcb@gmail.com>
21 *
22 * @addtogroup UART
23 * @{
24 */
25
26#ifndef HAL_UART_LLD_H
27#define HAL_UART_LLD_H
28
29#if (HAL_USE_UART == TRUE) || defined(__DOXYGEN__)
30
31#include "fsl_common.h"
32#include "fsl_lpuart_edma.h"
33
34/*===========================================================================*/
35/* Driver constants. */
36/*===========================================================================*/
37
38/*===========================================================================*/
39/* Driver pre-compile time settings. */
40/*===========================================================================*/
41
42/**
43 * @name Kinetis configuration options
44 * @{
45 */
46/**
47 * @brief UART driver enable switch.
48 * @details If set to @p TRUE the support for UART1 is included.
49 * @note The default is @p FALSE.
50 */
51#if !defined(KINETIS_UART_USE_UART1) || defined(__DOXYGEN__)
52#define KINETIS_UART_USE_UART1 FALSE
53#endif
54
55#if !defined(KINETIS_UART_USE_UART2) || defined(__DOXYGEN__)
56#define KINETIS_UART_USE_UART2 FALSE
57#endif
58
59#if !defined(KINETIS_UART_USE_UART3) || defined(__DOXYGEN__)
60#define KINETIS_UART_USE_UART3 FALSE
61#endif
62/** @} */
63
64/*===========================================================================*/
65/* Derived constants and error checks. */
66/*===========================================================================*/
67
68#define UART_USE_BLOCKING_SEND TRUE
69//#define UART_USE_RING_BUFFER TRUE
70//#define KINETIS_UART_RX_RING_BUFFER_SIZE 64
71
72// STM32-compatible flags
73#define USART_CR1_PS (1U << 9) /*!< 0x00000200 */
74#define USART_CR1_PCE (1U << 10) /*!< 0x00000400 */
75#define USART_CR2_STOP1_BITS (0 << 12) /**< @brief CR2 1 stop bit value.*/
76#define USART_CR2_STOP2_BITS (2 << 12) /**< @brief CR2 2 stop bit value.*/
77
78
79/*===========================================================================*/
80/* Driver data structures and types. */
81/*===========================================================================*/
82
83/**
84 * @brief UART driver condition flags type.
85 */
86typedef uint32_t uartflags_t;
87
88/**
89 * @brief Structure representing an UART driver.
90 */
91typedef struct UARTDriver UARTDriver;
92
93/**
94 * @brief Generic UART notification callback type.
95 *
96 * @param[in] uartp pointer to the @p UARTDriver object
97 */
98typedef void (*uartcb_t)(UARTDriver *uartp);
99
100/**
101 * @brief Character received UART notification callback type.
102 *
103 * @param[in] uartp pointer to the @p UARTDriver object
104 * @param[in] c received character
105 */
106typedef void (*uartccb_t)(UARTDriver *uartp, uint16_t c);
107
108/**
109 * @brief Receive error UART notification callback type.
110 *
111 * @param[in] uartp pointer to the @p UARTDriver object
112 * @param[in] e receive error mask
113 */
114typedef void (*uartecb_t)(UARTDriver *uartp, uartflags_t e);
115
116/**
117 * @brief Receive Half-transfer UART notification callback type.
118 *
119 * @param[in] uartp pointer to the @p UARTDriver object
120 * @param[in] full flag set to 1 for the second half, and 0 for the first half
121 */
122typedef void (*uarthcb_t)(UARTDriver *uartp, uartflags_t full);
123
124/**
125 * @brief Driver configuration structure.
126 * @note Implementations may extend this structure to contain more,
127 * architecture dependent, fields.
128 */
129typedef struct {
130 /**
131 * @brief End of transmission buffer callback.
132 */
134 /**
135 * @brief Physical end of transmission callback.
136 */
138 /**
139 * @brief Receive buffer filled callback.
140 */
142 /**
143 * @brief Character received while out if the @p UART_RECEIVE state.
144 */
146 /**
147 * @brief Receive error callback.
148 */
150 /**
151 * @brief Receiver timeout (idle) callback.
152 */
154 /* End of the mandatory fields.*/
155 /**
156 * @brief Bit rate.
157 */
158 uint32_t speed;
159 /**
160 * @brief Initialization value for the CR1 register.
161 */
162 uint16_t cr1;
163 /**
164 * @brief Initialization value for the CR2 register.
165 */
166 uint16_t cr2;
167 /**
168 * @brief Initialization value for the CR3 register.
169 */
170 uint16_t cr3;
171 /**
172 * @brief Half-transfer receive buffer callback.
173 */
175} UARTConfig;
176
177/**
178 * @brief Structure representing an UART driver.
179 * @note Implementations may extend this structure to contain more,
180 * architecture dependent, fields.
181 */
183 /**
184 * @brief Driver state.
185 */
186 uartstate_t state;
187 /**
188 * @brief Transmitter state.
189 */
190 uarttxstate_t txstate;
191 /**
192 * @brief Receiver state.
193 */
194 uartrxstate_t rxstate;
195 /**
196 * @brief Current configuration data.
197 */
199#if (UART_USE_WAIT == TRUE) || defined(__DOXYGEN__)
200 /**
201 * @brief Synchronization flag for transmit operations.
202 */
203 bool early;
204 /**
205 * @brief Waiting thread on RX.
206 */
207 thread_reference_t threadrx;
208 /**
209 * @brief Waiting thread on TX.
210 */
211 thread_reference_t threadtx;
212#endif /* UART_USE_WAIT */
213#if (UART_USE_MUTUAL_EXCLUSION == TRUE) || defined(__DOXYGEN__)
214 /**
215 * @brief Mutex protecting the peripheral.
216 */
217 mutex_t mutex;
218#endif /* UART_USE_MUTUAL_EXCLUSION */
219#if defined(UART_DRIVER_EXT_FIELDS)
220 UART_DRIVER_EXT_FIELDS
221#endif
222 /* End of the mandatory fields.*/
223 /**
224 * @brief Pointer to the USART registers block.
225 */
226 LPUART_Type *lpuart;
227
228 /**
229 * @brief LPUART common eDMA channel handle.
230 */
232 /**
233 * @brief eDMA transfer handles.
234 */
236
237 /**
238 * @brief Used to trigger the IRQ from our software handler.
239 * See uart_lld_callback() and UART_USE_RING_BUFFER
240 */
242
243 /**
244 * @brief Used for the RingBuffer mode or to handle UART errors in EDMA mode.
245 */
247
248#ifdef UART_USE_RING_BUFFER
249 uint8_t rxRingBuffer[KINETIS_UART_RX_RING_BUFFER_SIZE];
250#endif /* UART_USE_RING_BUFFER */
251};
252
253/*===========================================================================*/
254/* Driver macros. */
255/*===========================================================================*/
256
257/*===========================================================================*/
258/* External declarations. */
259/*===========================================================================*/
260
261#if (KINETIS_UART_USE_UART1 == TRUE) && !defined(__DOXYGEN__)
262extern UARTDriver UARTD1;
263#endif
264#if (KINETIS_UART_USE_UART2 == TRUE) && !defined(__DOXYGEN__)
265extern UARTDriver UARTD2;
266#endif
267#if (KINETIS_UART_USE_UART3 == TRUE) && !defined(__DOXYGEN__)
268extern UARTDriver UARTD3;
269#endif
270
271#ifdef __cplusplus
272extern "C" {
273#endif
274 void uart_lld_init(void);
275 void uart_lld_start(UARTDriver *uartp);
276 void uart_lld_stop(UARTDriver *uartp);
277 void uart_lld_start_send(UARTDriver *uartp, size_t n, const void *txbuf);
278 size_t uart_lld_stop_send(UARTDriver *uartp);
279 void uart_lld_start_receive(UARTDriver *uartp, size_t n, void *rxbuf);
280 size_t uart_lld_stop_receive(UARTDriver *uartp);
281 void uart_lld_blocking_send(UARTDriver *uartp, size_t n, const void *txbuf);
282#ifdef __cplusplus
283}
284#endif
285
286#endif /* HAL_USE_UART == TRUE */
287
288#endif /* HAL_UART_LLD_H */
289
290/** @} */
void(* uartecb_t)(UARTDriver *uartp, uartflags_t e)
Receive error UART notification callback type.
void uart_lld_start_send(UARTDriver *uartp, size_t n, const void *txbuf)
Starts a transmission on the UART peripheral.
void uart_lld_blocking_send(UARTDriver *uartp, size_t n, const void *txbuf)
void uart_lld_start(UARTDriver *uartp)
Configures and activates the UART peripheral.
void(* uartcb_t)(UARTDriver *uartp)
Generic UART notification callback type.
void(* uartccb_t)(UARTDriver *uartp, uint16_t c)
Character received UART notification callback type.
UARTDriver UARTD2
UARTDriver UARTD1
UART1 driver identifier.
void uart_lld_init(void)
Low level UART driver initialization.
UARTDriver UARTD3
uint32_t uartflags_t
UART driver condition flags type.
size_t uart_lld_stop_send(UARTDriver *uartp)
Stops any ongoing transmission.
void uart_lld_start_receive(UARTDriver *uartp, size_t n, void *rxbuf)
Starts a receive operation on the UART peripheral.
size_t uart_lld_stop_receive(UARTDriver *uartp)
Stops any ongoing receive operation.
void uart_lld_stop(UARTDriver *uartp)
Deactivates the UART peripheral.
void(* uarthcb_t)(UARTDriver *uartp, uartflags_t full)
Receive Half-transfer UART notification callback type.
eDMA transfer handle structure
Definition fsl_edma.h:246
LPUART eDMA handle.
LPUART handle structure.
Definition fsl_lpuart.h:229
Driver configuration structure.
uartcb_t timeout_cb
Receiver timeout (idle) callback.
uartecb_t rxerr_cb
Receive error callback.
uartcb_t txend2_cb
Physical end of transmission callback.
uarthcb_t rxhalf_cb
Half-transfer receive buffer callback.
uint16_t cr3
Initialization value for the CR3 register.
uint16_t cr1
Initialization value for the CR1 register.
uartccb_t rxchar_cb
Character received while out if the UART_RECEIVE state.
uartcb_t txend1_cb
End of transmission buffer callback.
uartcb_t rxend_cb
Receive buffer filled callback.
uint16_t cr2
Initialization value for the CR2 register.
uint32_t speed
Bit rate.
Structure representing an UART driver.
lpuart_edma_handle_t dmaHandle
LPUART common eDMA channel handle.
edma_handle_t lpuartRxEdmaHandle
eDMA transfer handles.
uartrxstate_t rxstate
Receiver state.
int pendingRxIrq
Used to trigger the IRQ from our software handler. See uart_lld_callback() and UART_USE_RING_BUFFER.
mutex_t mutex
Mutex protecting the peripheral.
lpuart_handle_t rxHandle
Used for the RingBuffer mode or to handle UART errors in EDMA mode.
thread_reference_t threadrx
Waiting thread on RX.
bool early
Synchronization flag for transmit operations.
const UARTConfig * config
Current configuration data.
uartstate_t state
Driver state.
UART_DRIVER_EXT_FIELDS LPUART_Type * lpuart
Pointer to the USART registers block.
uint8_t rxRingBuffer[KINETIS_UART_RX_RING_BUFFER_SIZE]
uarttxstate_t txstate
Transmitter state.
edma_handle_t lpuartTxEdmaHandle
thread_reference_t threadtx
Waiting thread on TX.