Zephyr Project API 3.7.0
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
117int uart_async_to_irq_init(struct uart_async_to_irq_data *data,
118 const struct uart_async_to_irq_config *config);
119
120/* @brief Enable RX for interrupt driven API.
121 *
122 * @param dev UART device. Device must support asynchronous API.
123 *
124 * @retval 0 on successful operation.
125 * @retval -EINVAL if adaption layer has wrong configuration.
126 * @retval negative value Error reported by the UART API.
127 */
128int uart_async_to_irq_rx_enable(const struct device *dev);
129
130/* @brief Disable RX for interrupt driven API.
131 *
132 * @param dev UART device. Device must support asynchronous API.
133 *
134 * @retval 0 on successful operation.
135 * @retval -EINVAL if adaption layer has wrong configuration.
136 * @retval negative value Error reported by the UART API.
137 */
139
140/* Starting from here API is internal only. */
141
145struct uart_async_to_irq_config {
147 uint8_t *tx_buf;
148
150 size_t tx_len;
151
153 struct uart_async_rx_config async_rx;
154
156 const struct uart_async_to_irq_async_api *api;
157
160
162 uint32_t baudrate;
163
166};
167
169struct uart_async_to_irq_async_api {
170 int (*callback_set)(const struct device *dev,
171 uart_callback_t callback,
172 void *user_data);
173
174 int (*tx)(const struct device *dev, const uint8_t *buf, size_t len,
176 int (*tx_abort)(const struct device *dev);
177
178 int (*rx_enable)(const struct device *dev, uint8_t *buf, size_t len,
180 int (*rx_buf_rsp)(const struct device *dev, uint8_t *buf, size_t len);
181 int (*rx_disable)(const struct device *dev);
182};
183
185struct uart_async_to_irq_rx_data {
187 struct uart_async_rx async_rx;
188
190 struct k_sem sem;
191
193 atomic_t pending_buf_req;
194};
195
197struct uart_async_to_irq_tx_data {
199 uint8_t *buf;
200
202 size_t len;
203};
204
206struct uart_async_to_irq_data {
209
211 void *user_data;
212
214 atomic_t irq_req;
215
217 struct uart_async_to_irq_rx_data rx;
218
220 struct uart_async_to_irq_tx_data tx;
221
223 struct k_spinlock lock;
224
227};
228
230int z_uart_async_to_irq_fifo_fill(const struct device *dev,
231 const uint8_t *buf,
232 int len);
233
235int z_uart_async_to_irq_fifo_read(const struct device *dev,
236 uint8_t *buf,
237 const int len);
238
240void z_uart_async_to_irq_irq_tx_enable(const struct device *dev);
241
243void z_uart_async_to_irq_irq_tx_disable(const struct device *dev);
244
246int z_uart_async_to_irq_irq_tx_ready(const struct device *dev);
247
249void z_uart_async_to_irq_irq_rx_enable(const struct device *dev);
250
252void z_uart_async_to_irq_irq_rx_disable(const struct device *dev);
253
255int z_uart_async_to_irq_irq_tx_complete(const struct device *dev);
256
258int z_uart_async_to_irq_irq_rx_ready(const struct device *dev);
259
261void z_uart_async_to_irq_irq_err_enable(const struct device *dev);
262
264void z_uart_async_to_irq_irq_err_disable(const struct device *dev);
265
267int z_uart_async_to_irq_irq_is_pending(const struct device *dev);
268
270int z_uart_async_to_irq_irq_update(const struct device *dev);
271
273void z_uart_async_to_irq_irq_callback_set(const struct device *dev,
275 void *user_data);
276
279#ifdef __cplusplus
280}
281#endif
282
285#endif /* ZEPHYR_DRIVERS_SERIAL_UART_ASYNC_TO_IRQ_H_ */
long atomic_t
Definition atomic_types.h:15
ZTEST_BMEM int timeout
Definition main.c:31
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.
static struct k_spinlock lock
Definition spinlock_error_case.c:13
__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
const void * config
Address of device instance config information.
Definition device.h:407
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
static fdata_t data[2]
Definition test_fifo_contexts.c:15
static struct k_sem sem[3]
Definition timeout_order.c:14
static const intptr_t user_data[5]
Definition main.c:588
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
int uart_async_to_irq_init(struct uart_async_to_irq_data *data, const struct uart_async_to_irq_config *config)
Initialize the adaptation layer.
void uart_async_to_irq_trampoline_cb(const struct device *dev)
Callback to be called from trampoline context.
Misc utilities.