1 /*
2 * Copyright (c) 2023 Trackunit Corporation
3 *
4 * SPDX-License-Identifier: Apache-2.0
5 */
6
7 #include "modem_backend_uart_isr.h"
8 #include "modem_backend_uart_async.h"
9
10 #include <zephyr/modem/backend/uart.h>
11
12 #include <string.h>
13
modem_backend_uart_receive_ready_handler(struct k_work * item)14 static void modem_backend_uart_receive_ready_handler(struct k_work *item)
15 {
16 struct modem_backend_uart *backend = CONTAINER_OF(
17 k_work_delayable_from_work(item), struct modem_backend_uart, receive_ready_work);
18
19 modem_pipe_notify_receive_ready(&backend->pipe);
20 }
21
modem_backend_uart_transmit_idle_handler(struct k_work * item)22 static void modem_backend_uart_transmit_idle_handler(struct k_work *item)
23 {
24 struct modem_backend_uart *backend =
25 CONTAINER_OF(item, struct modem_backend_uart, transmit_idle_work);
26
27 modem_pipe_notify_transmit_idle(&backend->pipe);
28 }
29
modem_backend_uart_init(struct modem_backend_uart * backend,const struct modem_backend_uart_config * config)30 struct modem_pipe *modem_backend_uart_init(struct modem_backend_uart *backend,
31 const struct modem_backend_uart_config *config)
32 {
33 __ASSERT_NO_MSG(config->uart != NULL);
34 __ASSERT_NO_MSG(config->receive_buf != NULL);
35 __ASSERT_NO_MSG(config->receive_buf_size > 1);
36 __ASSERT_NO_MSG((config->receive_buf_size % 2) == 0);
37 __ASSERT_NO_MSG(config->transmit_buf != NULL);
38 __ASSERT_NO_MSG(config->transmit_buf_size > 0);
39
40 memset(backend, 0x00, sizeof(*backend));
41 backend->uart = config->uart;
42 k_work_init_delayable(&backend->receive_ready_work,
43 modem_backend_uart_receive_ready_handler);
44 k_work_init(&backend->transmit_idle_work, modem_backend_uart_transmit_idle_handler);
45
46 #ifdef CONFIG_MODEM_BACKEND_UART_ASYNC
47 if (modem_backend_uart_async_is_supported(backend)) {
48 modem_backend_uart_async_init(backend, config);
49 return &backend->pipe;
50 }
51 #endif /* CONFIG_MODEM_BACKEND_UART_ASYNC */
52
53 #ifdef CONFIG_MODEM_BACKEND_UART_ISR
54 modem_backend_uart_isr_init(backend, config);
55
56 return &backend->pipe;
57 #endif /* CONFIG_MODEM_BACKEND_UART_ISR */
58
59 __ASSERT(0, "No supported UART API");
60
61 return NULL;
62 }
63