forked from nrfconnect/sdk-nrf
-
Notifications
You must be signed in to change notification settings - Fork 0
/
uart_async_adapter.h
136 lines (120 loc) · 3.85 KB
/
uart_async_adapter.h
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
/*
* Copyright (c) 2021 Nordic Semiconductor ASA
*
* SPDX-License-Identifier: LicenseRef-Nordic-5-Clause
*/
#ifndef __UART_ASYNC_ADAPTER_H
#define __UART_ASYNC_ADAPTER_H
/** @file
* @brief UART asynchronous API adapter
*/
#include <zephyr/device.h>
#include <zephyr/drivers/uart.h>
#include <zephyr/kernel.h>
#ifdef __cplusplus
extern "C" {
#endif
/**
* @defgroup uart_async_adapter UART async adapter
* @{
* @brief UART asynchronous API universal adapter
*
* This module acts as an adapter between UART interrupt and async interface.
*
* @note The UART async API adapter implementation is experimental.
* It means it is not guaranteed to work in any corner situation.
*/
/**
* @brief UART asynch adapter data structure
*
* The data used by asynch adapter.
*/
struct uart_async_adapter_data {
/** Target device pointer */
const struct device *target;
/** User callback function for synchronous interface */
uart_callback_t user_callback;
/** Pointer to the user data */
void *user_data;
/* The interface realization */
/** Lock for the data that requires it */
struct k_spinlock lock;
/** Data used for output transmission */
struct uart_async_adapter_data_rx {
/** The original buffer pointer set when data transfer was requested */
const uint8_t *buf;
/** Current buffer position */
const uint8_t *curr_buf;
/** Number of data left in the current buffer */
volatile size_t size_left;
/** Timer used for timeout */
struct k_timer timeout_timer;
/** Tx state */
bool enabled;
} tx;
/** Data used for input transmission */
struct uart_async_adapter_data_tx {
/** Base buffer pointer used now for data reception */
uint8_t *buf;
/** Current position to write data into */
uint8_t *curr_buf;
/** Last position of the notified buffer */
uint8_t *last_notify_buf;
/** Number of bytes left in the current buffer */
size_t size_left;
/** Buffer prepared for the next transfer */
uint8_t *next_buf;
/** The size of the buffer for the next transfer */
size_t next_buf_len;
/** Timeout set by the user */
int32_t timeout;
/** Timer used for timeout */
struct k_timer timeout_timer;
/** RX state */
bool enabled;
} rx;
};
/**
* @brief Driver API for async adapter
*
* The API of the UART async adapter uses standard UART API structure.
*/
extern const struct uart_driver_api uart_async_adapter_driver_api;
/**
* @brief The name of the data instance connected with created device instance
*
* @param _dev_name The name of the created device instance
*/
#define UART_ASYNC_ADAPTER_INST_DATA_NAME(_dev_name) _CONCAT(uart_async_adapter_data_, _dev_name)
#define UART_ASYNC_ADAPTER_INST_STATE_NAME(_dev_name) _CONCAT(uart_async_adapter_state_, _dev_name)
#define UART_ASYNC_ADAPTER_INST_NAME(_dev_name) _CONCAT(_dev_name, _inst)
/**
* @brief The macro that creates and instance of the UART async adapter
*
* @param _dev The name of the created device instance
*/
#define UART_ASYNC_ADAPTER_INST_DEFINE(_dev) \
static struct uart_async_adapter_data UART_ASYNC_ADAPTER_INST_DATA_NAME(_dev); \
static struct device_state UART_ASYNC_ADAPTER_INST_STATE_NAME(_dev); \
static const struct device UART_ASYNC_ADAPTER_INST_NAME(_dev) = { \
.name = STRINGIFY(_dev), \
.api = &uart_async_adapter_driver_api, \
.state = &UART_ASYNC_ADAPTER_INST_STATE_NAME(_dev), \
.data = &UART_ASYNC_ADAPTER_INST_DATA_NAME(_dev), \
}; \
static const struct device *const _dev = &UART_ASYNC_ADAPTER_INST_NAME(_dev)
/**
* @brief Initialize adapter
*
* Call this function before uart adapter is used.
* The function configures connected UART device to work with the adapter.
*
* @param dev The adapter interface
* @param target Target UART device with only interrupt interface
*/
void uart_async_adapter_init(const struct device *dev, const struct device *target);
/** @} */
#ifdef __cplusplus
}
#endif
#endif /* __UART_ASYNC_ADAPTER_H */