1 /** @file
2  * @brief Modem interface for UART header file.
3  *
4  * Modem interface UART handling for modem context driver.
5  */
6 
7 /*
8  * Copyright (c) 2019 Foundries.io
9  *
10  * SPDX-License-Identifier: Apache-2.0
11  */
12 
13 #ifndef ZEPHYR_INCLUDE_DRIVERS_MODEM_MODEM_IFACE_UART_H_
14 #define ZEPHYR_INCLUDE_DRIVERS_MODEM_MODEM_IFACE_UART_H_
15 
16 #include <zephyr/kernel.h>
17 
18 #ifdef __cplusplus
19 extern "C" {
20 #endif
21 
22 struct modem_iface_uart_data {
23 	/* HW flow control */
24 	bool hw_flow_control;
25 
26 	/* ring buffer */
27 	struct ring_buf rx_rb;
28 
29 	/* rx semaphore */
30 	struct k_sem rx_sem;
31 
32 #ifdef CONFIG_MODEM_IFACE_UART_ASYNC
33 
34 	/* tx semaphore */
35 	struct k_sem tx_sem;
36 
37 #endif /* CONFIG_MODEM_IFACE_UART_ASYNC */
38 };
39 
40 /**
41  * @brief  Init modem interface device for UART
42  *
43  * @details This can be called after the init if the UART is changed.
44  *
45  * @param  iface: modem interface to initialize.
46  * @param  dev_name: name of the UART device to use
47  *
48  * @retval 0 if ok, < 0 if error.
49  */
50 int modem_iface_uart_init_dev(struct modem_iface *iface,
51 			      const struct device *dev);
52 
53 /**
54  * @brief Modem uart interface configuration
55  *
56  * @param rx_rb_buf Buffer used for internal ring buffer
57  * @param rx_rb_buf_len Size of buffer used for internal ring buffer
58  * @param dev UART device used for interface
59  * @param hw_flow_control Set if hardware flow control is used
60  */
61 struct modem_iface_uart_config {
62 	char *rx_rb_buf;
63 	size_t rx_rb_buf_len;
64 	const struct device *dev;
65 	bool hw_flow_control;
66 };
67 
68 /**
69  * @brief Initialize modem interface for UART
70  *
71  * @param iface Interface structure to initialize
72  * @param data UART data structure used by the modem interface
73  * @param config UART configuration structure used to configure UART data structure
74  *
75  * @return -EINVAL if any argument is invalid
76  * @return 0 if successful
77  */
78 int modem_iface_uart_init(struct modem_iface *iface, struct modem_iface_uart_data *data,
79 			  const struct modem_iface_uart_config *config);
80 
81 /**
82  * @brief Wait for rx data ready from uart interface
83  *
84  * @param iface Interface to wait on
85  *
86  * @return 0 if data is ready
87  * @return -EBUSY if returned without waiting
88  * @return -EAGAIN if timeout occurred
89  */
modem_iface_uart_rx_wait(struct modem_iface * iface,k_timeout_t timeout)90 static inline int modem_iface_uart_rx_wait(struct modem_iface *iface, k_timeout_t timeout)
91 {
92 	struct modem_iface_uart_data *data = (struct modem_iface_uart_data *)iface->iface_data;
93 
94 	return k_sem_take(&data->rx_sem, timeout);
95 }
96 
97 #ifdef __cplusplus
98 }
99 #endif
100 
101 #endif /* ZEPHYR_INCLUDE_DRIVERS_MODEM_MODEM_IFACE_UART_H_ */
102