1 /*
2  * Copyright (c) 2022 Trackunit Corporation
3  *
4  * SPDX-License-Identifier: Apache-2.0
5  */
6 
7 #include <zephyr/kernel.h>
8 #include <zephyr/types.h>
9 #include <zephyr/device.h>
10 #include <zephyr/drivers/uart.h>
11 #include <zephyr/sys/ring_buffer.h>
12 #include <zephyr/sys/atomic.h>
13 
14 #include <zephyr/modem/pipe.h>
15 
16 #ifndef ZEPHYR_MODEM_BACKEND_UART_
17 #define ZEPHYR_MODEM_BACKEND_UART_
18 
19 #ifdef __cplusplus
20 extern "C" {
21 #endif
22 
23 struct modem_backend_uart_isr {
24 	struct ring_buf receive_rdb[2];
25 	struct ring_buf transmit_rb;
26 	atomic_t transmit_buf_len;
27 	uint8_t receive_rdb_used;
28 	uint32_t transmit_buf_put_limit;
29 };
30 
31 struct modem_backend_uart_async {
32 	uint8_t *receive_bufs[2];
33 	uint32_t receive_buf_size;
34 	struct ring_buf receive_rb;
35 	struct k_spinlock receive_rb_lock;
36 	uint8_t *transmit_buf;
37 	uint32_t transmit_buf_size;
38 	struct k_work rx_disabled_work;
39 	atomic_t state;
40 };
41 
42 struct modem_backend_uart {
43 	const struct device *uart;
44 	struct modem_pipe pipe;
45 	struct k_work receive_ready_work;
46 	struct k_work transmit_idle_work;
47 
48 	union {
49 		struct modem_backend_uart_isr isr;
50 		struct modem_backend_uart_async async;
51 	};
52 };
53 
54 struct modem_backend_uart_config {
55 	const struct device *uart;
56 	uint8_t *receive_buf;
57 	uint32_t receive_buf_size;
58 	uint8_t *transmit_buf;
59 	uint32_t transmit_buf_size;
60 };
61 
62 struct modem_pipe *modem_backend_uart_init(struct modem_backend_uart *backend,
63 					   const struct modem_backend_uart_config *config);
64 
65 #ifdef __cplusplus
66 }
67 #endif
68 
69 #endif /* ZEPHYR_MODEM_BACKEND_UART_ */
70