Line data Source code
1 0 : /*
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 : #include <zephyr/modem/stats.h>
16 :
17 : #ifndef ZEPHYR_MODEM_BACKEND_UART_
18 0 : #define ZEPHYR_MODEM_BACKEND_UART_
19 :
20 : #ifdef __cplusplus
21 : extern "C" {
22 : #endif
23 :
24 0 : struct modem_backend_uart_isr {
25 0 : struct ring_buf receive_rdb[2];
26 0 : struct ring_buf transmit_rb;
27 0 : atomic_t transmit_buf_len;
28 0 : atomic_t receive_buf_len;
29 0 : uint8_t receive_rdb_used;
30 0 : uint32_t transmit_buf_put_limit;
31 : };
32 :
33 0 : struct modem_backend_uart_async_common {
34 0 : uint8_t *transmit_buf;
35 0 : uint32_t transmit_buf_size;
36 0 : struct k_work rx_disabled_work;
37 0 : atomic_t state;
38 : };
39 :
40 : #ifdef CONFIG_MODEM_BACKEND_UART_ASYNC_HWFC
41 :
42 : struct rx_queue_event {
43 : uint8_t *buf;
44 : size_t len;
45 : };
46 :
47 : struct modem_backend_uart_async {
48 : struct modem_backend_uart_async_common common;
49 : struct k_mem_slab rx_slab;
50 : struct k_msgq rx_queue;
51 : struct rx_queue_event rx_event;
52 : struct rx_queue_event rx_queue_buf[CONFIG_MODEM_BACKEND_UART_ASYNC_HWFC_BUFFER_COUNT];
53 : uint32_t rx_buf_size;
54 : uint8_t rx_buf_count;
55 : };
56 :
57 : #else
58 :
59 0 : struct modem_backend_uart_async {
60 0 : struct modem_backend_uart_async_common common;
61 0 : uint8_t *receive_bufs[2];
62 0 : uint32_t receive_buf_size;
63 0 : struct ring_buf receive_rb;
64 0 : struct k_spinlock receive_rb_lock;
65 : };
66 :
67 : #endif /* CONFIG_MODEM_BACKEND_UART_ASYNC_HWFC */
68 :
69 0 : struct modem_backend_uart {
70 0 : const struct device *uart;
71 0 : struct modem_pipe pipe;
72 0 : struct k_work_delayable receive_ready_work;
73 0 : struct k_work transmit_idle_work;
74 :
75 : #if CONFIG_MODEM_STATS
76 : struct modem_stats_buffer receive_buf_stats;
77 : struct modem_stats_buffer transmit_buf_stats;
78 : #endif
79 :
80 : union {
81 0 : struct modem_backend_uart_isr isr;
82 0 : struct modem_backend_uart_async async;
83 0 : };
84 : };
85 :
86 0 : struct modem_backend_uart_config {
87 0 : const struct device *uart;
88 : /* Address must be word-aligned when CONFIG_MODEM_BACKEND_UART_ASYNC_HWFC is enabled. */
89 0 : uint8_t *receive_buf;
90 0 : uint32_t receive_buf_size;
91 0 : uint8_t *transmit_buf;
92 0 : uint32_t transmit_buf_size;
93 : };
94 :
95 0 : struct modem_pipe *modem_backend_uart_init(struct modem_backend_uart *backend,
96 : const struct modem_backend_uart_config *config);
97 :
98 : #ifdef __cplusplus
99 : }
100 : #endif
101 :
102 : #endif /* ZEPHYR_MODEM_BACKEND_UART_ */
|