Zephyr API Documentation 3.7.99
A Scalable Open Source RTOS
Loading...
Searching...
No Matches
uart_async_to_irq.h
Go to the documentation of this file.
1/*
2 * Copyright (c) 2023 Nordic Semiconductor ASA
3 *
4 * SPDX-License-Identifier: Apache-2.0
5 */
6
7#ifndef ZEPHYR_DRIVERS_SERIAL_UART_ASYNC_TO_IRQ_H_
8#define ZEPHYR_DRIVERS_SERIAL_UART_ASYNC_TO_IRQ_H_
9
10#include <zephyr/drivers/uart.h>
11#include <zephyr/logging/log.h>
12#include <zephyr/spinlock.h>
13#include <zephyr/sys/util.h>
15
22#ifdef __cplusplus
23extern "C" {
24#endif
25
26/* Forward declarations. */
27
32struct uart_async_to_irq_data;
33
38struct uart_async_to_irq_config;
39
40/* @brief Function that triggers trampoline to the interrupt context.
41 *
42 * This context is used to call user UART interrupt handler. It is to used to
43 * fulfill the requirement that UART interrupt driven API shall be called from
44 * the UART interrupt. Trampoline context shall have the same priority as UART.
45 *
46 * One option may be to use k_timer configured to expire immediately.
47 */
48typedef void (*uart_async_to_irq_trampoline)(const struct device *dev);
49
55
61#define UART_ASYNC_TO_IRQ_API_INIT() \
62 .fifo_fill = z_uart_async_to_irq_fifo_fill, \
63 .fifo_read = z_uart_async_to_irq_fifo_read, \
64 .irq_tx_enable = z_uart_async_to_irq_irq_tx_enable, \
65 .irq_tx_disable = z_uart_async_to_irq_irq_tx_disable, \
66 .irq_tx_ready = z_uart_async_to_irq_irq_tx_ready, \
67 .irq_rx_enable = z_uart_async_to_irq_irq_rx_enable, \
68 .irq_rx_disable = z_uart_async_to_irq_irq_rx_disable, \
69 .irq_tx_complete = z_uart_async_to_irq_irq_tx_complete,\
70 .irq_rx_ready = z_uart_async_to_irq_irq_rx_ready, \
71 .irq_err_enable = z_uart_async_to_irq_irq_err_enable, \
72 .irq_err_disable = z_uart_async_to_irq_irq_err_disable,\
73 .irq_is_pending = z_uart_async_to_irq_irq_is_pending, \
74 .irq_update = z_uart_async_to_irq_irq_update, \
75 .irq_callback_set = z_uart_async_to_irq_irq_callback_set
76
89#define UART_ASYNC_TO_IRQ_API_CONFIG_INITIALIZER(_api, _trampoline, _baudrate, _tx_buf, \
90 _tx_len, _rx_buf, _rx_len, _rx_cnt, _log) \
91 { \
92 .tx_buf = _tx_buf, \
93 .tx_len = _tx_len, \
94 .async_rx = { \
95 .buffer = _rx_buf, \
96 .length = _rx_len, \
97 .buf_cnt = _rx_cnt \
98 }, \
99 .api = _api, \
100 .trampoline = _trampoline, \
101 .baudrate = _baudrate, \
102 LOG_OBJECT_PTR_INIT(log, \
103 COND_CODE_1(IS_EMPTY(_log), \
104 (LOG_OBJECT_PTR(UART_ASYNC_TO_IRQ_LOG_NAME)), \
105 (_log) \
106 ) \
107 ) \
108 }
109
116int uart_async_to_irq_init(const struct device *dev);
117
118/* @brief Enable RX for interrupt driven API.
119 *
120 * @param dev UART device. Device must support asynchronous API.
121 *
122 * @retval 0 on successful operation.
123 * @retval -EINVAL if adaption layer has wrong configuration.
124 * @retval negative value Error reported by the UART API.
125 */
126int uart_async_to_irq_rx_enable(const struct device *dev);
127
128/* @brief Disable RX for interrupt driven API.
129 *
130 * @param dev UART device. Device must support asynchronous API.
131 *
132 * @retval 0 on successful operation.
133 * @retval -EINVAL if adaption layer has wrong configuration.
134 * @retval negative value Error reported by the UART API.
135 */
137
138/* Starting from here API is internal only. */
139
143struct uart_async_to_irq_config {
145 uint8_t *tx_buf;
146
148 size_t tx_len;
149
151 struct uart_async_rx_config async_rx;
152
154 const struct uart_async_to_irq_async_api *api;
155
158
160 uint32_t baudrate;
161
164};
165
167struct uart_async_to_irq_async_api {
168 int (*callback_set)(const struct device *dev,
169 uart_callback_t callback,
170 void *user_data);
171
172 int (*tx)(const struct device *dev, const uint8_t *buf, size_t len,
173 int32_t timeout);
174 int (*tx_abort)(const struct device *dev);
175
176 int (*rx_enable)(const struct device *dev, uint8_t *buf, size_t len,
177 int32_t timeout);
178 int (*rx_buf_rsp)(const struct device *dev, uint8_t *buf, size_t len);
179 int (*rx_disable)(const struct device *dev);
180};
181
183struct uart_async_to_irq_rx_data {
185 struct uart_async_rx async_rx;
186
188 struct k_sem sem;
189
191 atomic_t pending_buf_req;
192};
193
195struct uart_async_to_irq_tx_data {
197 uint8_t *buf;
198
200 size_t len;
201};
202
204struct uart_async_to_irq_data {
207
209 void *user_data;
210
212 atomic_t irq_req;
213
215 struct uart_async_to_irq_rx_data rx;
216
218 struct uart_async_to_irq_tx_data tx;
219
221 struct k_spinlock lock;
222
225};
226
228int z_uart_async_to_irq_fifo_fill(const struct device *dev,
229 const uint8_t *buf,
230 int len);
231
233int z_uart_async_to_irq_fifo_read(const struct device *dev,
234 uint8_t *buf,
235 const int len);
236
238void z_uart_async_to_irq_irq_tx_enable(const struct device *dev);
239
241void z_uart_async_to_irq_irq_tx_disable(const struct device *dev);
242
244int z_uart_async_to_irq_irq_tx_ready(const struct device *dev);
245
247void z_uart_async_to_irq_irq_rx_enable(const struct device *dev);
248
250void z_uart_async_to_irq_irq_rx_disable(const struct device *dev);
251
253int z_uart_async_to_irq_irq_tx_complete(const struct device *dev);
254
256int z_uart_async_to_irq_irq_rx_ready(const struct device *dev);
257
259void z_uart_async_to_irq_irq_err_enable(const struct device *dev);
260
262void z_uart_async_to_irq_irq_err_disable(const struct device *dev);
263
265int z_uart_async_to_irq_irq_is_pending(const struct device *dev);
266
268int z_uart_async_to_irq_irq_update(const struct device *dev);
269
271void z_uart_async_to_irq_irq_callback_set(const struct device *dev,
273 void *user_data);
274
277#ifdef __cplusplus
278}
279#endif
280
283#endif /* ZEPHYR_DRIVERS_SERIAL_UART_ASYNC_TO_IRQ_H_ */
long atomic_t
Definition atomic_types.h:15
Public APIs for UART drivers.
void(* uart_callback_t)(const struct device *dev, struct uart_event *evt, void *user_data)
Define the application callback function signature for uart_callback_set() function.
Definition uart.h:324
void(* uart_irq_callback_user_data_t)(const struct device *dev, void *user_data)
Define the application callback function signature for uart_irq_callback_user_data_set() function.
Definition uart.h:141
#define LOG_INSTANCE_PTR_DECLARE(_name)
Declare a logger instance pointer in the module structure.
Definition log_instance.h:147
flags
Definition parser.h:96
Public interface for spinlocks.
__UINT32_TYPE__ uint32_t
Definition stdint.h:90
__INT32_TYPE__ int32_t
Definition stdint.h:74
__UINT8_TYPE__ uint8_t
Definition stdint.h:88
Runtime device structure (in ROM) per driver instance.
Definition device.h:403
Kernel Spin Lock.
Definition spinlock.h:45
UART asynchronous RX helper configuration structure.
Definition uart_async_rx.h:62
UART asynchronous RX helper structure.
Definition uart_async_rx.h:36
Misc utilities.
Helper module for receiving using UART Asynchronous API.
int uart_async_to_irq_rx_enable(const struct device *dev)
int uart_async_to_irq_rx_disable(const struct device *dev)
void(* uart_async_to_irq_trampoline)(const struct device *dev)
Definition uart_async_to_irq.h:48
void uart_async_to_irq_trampoline_cb(const struct device *dev)
Callback to be called from trampoline context.
int uart_async_to_irq_init(const struct device *dev)
Initialize the adaptation layer.