Michael Scott | 9182d2e | 2018-08-01 12:01:00 -0800 | [diff] [blame] | 1 | /** @file |
| 2 | * @brief Modem receiver driver |
| 3 | * |
| 4 | * A modem receiver driver allowing application to handle all |
| 5 | * aspects of received protocol data. |
| 6 | */ |
| 7 | |
| 8 | /* |
| 9 | * Copyright (c) 2018 Foundries.io |
| 10 | * |
| 11 | * SPDX-License-Identifier: Apache-2.0 |
| 12 | */ |
| 13 | |
Gerard Marull-Paretas | fb60aab | 2022-05-06 10:25:46 +0200 | [diff] [blame] | 14 | #include <zephyr/kernel.h> |
| 15 | #include <zephyr/init.h> |
| 16 | #include <zephyr/drivers/uart.h> |
| 17 | #include <zephyr/pm/device.h> |
Michael Scott | 9182d2e | 2018-08-01 12:01:00 -0800 | [diff] [blame] | 18 | |
Gerard Marull-Paretas | fb60aab | 2022-05-06 10:25:46 +0200 | [diff] [blame] | 19 | #include <zephyr/logging/log.h> |
Kumar Gala | b0e7d31 | 2019-02-12 00:00:29 -0600 | [diff] [blame] | 20 | |
| 21 | LOG_MODULE_REGISTER(mdm_receiver, CONFIG_MODEM_LOG_LEVEL); |
Anas Nashif | 5c76143 | 2018-10-11 10:20:56 -0400 | [diff] [blame] | 22 | |
Anas Nashif | 158d921 | 2019-06-21 13:30:27 -0400 | [diff] [blame] | 23 | #include "modem_receiver.h" |
Michael Scott | 9182d2e | 2018-08-01 12:01:00 -0800 | [diff] [blame] | 24 | |
| 25 | #define MAX_MDM_CTX CONFIG_MODEM_RECEIVER_MAX_CONTEXTS |
| 26 | #define MAX_READ_SIZE 128 |
| 27 | |
| 28 | static struct mdm_receiver_context *contexts[MAX_MDM_CTX]; |
| 29 | |
Georgij Cernysiov | de8b39f | 2019-02-13 22:59:17 +0100 | [diff] [blame] | 30 | /** |
| 31 | * @brief Finds receiver context which manages provided device. |
| 32 | * |
Benjamin Cabé | db53c3f | 2023-07-13 14:18:49 +0200 | [diff] [blame] | 33 | * @param dev: device used by the receiver context. |
Georgij Cernysiov | de8b39f | 2019-02-13 22:59:17 +0100 | [diff] [blame] | 34 | * |
| 35 | * @retval Receiver context or NULL. |
| 36 | */ |
Tomasz Bursztyka | e18fcbb | 2020-04-30 20:33:38 +0200 | [diff] [blame] | 37 | static struct mdm_receiver_context *context_from_dev(const struct device *dev) |
Michael Scott | 9182d2e | 2018-08-01 12:01:00 -0800 | [diff] [blame] | 38 | { |
| 39 | int i; |
| 40 | |
| 41 | for (i = 0; i < MAX_MDM_CTX; i++) { |
| 42 | if (contexts[i] && contexts[i]->uart_dev == dev) { |
| 43 | return contexts[i]; |
| 44 | } |
| 45 | } |
| 46 | |
| 47 | return NULL; |
| 48 | } |
| 49 | |
Georgij Cernysiov | de8b39f | 2019-02-13 22:59:17 +0100 | [diff] [blame] | 50 | /** |
| 51 | * @brief Persists receiver context if there is a free place. |
| 52 | * |
| 53 | * @note Amount of stored receiver contexts is determined by |
| 54 | * MAX_MDM_CTX. |
| 55 | * |
Benjamin Cabé | db53c3f | 2023-07-13 14:18:49 +0200 | [diff] [blame] | 56 | * @param ctx: receiver context to persist. |
Georgij Cernysiov | de8b39f | 2019-02-13 22:59:17 +0100 | [diff] [blame] | 57 | * |
| 58 | * @retval 0 if ok, < 0 if error. |
| 59 | */ |
Michael Scott | 9182d2e | 2018-08-01 12:01:00 -0800 | [diff] [blame] | 60 | static int mdm_receiver_get(struct mdm_receiver_context *ctx) |
| 61 | { |
| 62 | int i; |
| 63 | |
Michael Scott | 9182d2e | 2018-08-01 12:01:00 -0800 | [diff] [blame] | 64 | for (i = 0; i < MAX_MDM_CTX; i++) { |
| 65 | if (!contexts[i]) { |
| 66 | contexts[i] = ctx; |
| 67 | return 0; |
| 68 | } |
| 69 | } |
| 70 | |
| 71 | return -ENOMEM; |
| 72 | } |
| 73 | |
Georgij Cernysiov | de8b39f | 2019-02-13 22:59:17 +0100 | [diff] [blame] | 74 | /** |
| 75 | * @brief Drains UART. |
| 76 | * |
| 77 | * @note Discards remaining data. |
| 78 | * |
Benjamin Cabé | db53c3f | 2023-07-13 14:18:49 +0200 | [diff] [blame] | 79 | * @param ctx: receiver context. |
Georgij Cernysiov | de8b39f | 2019-02-13 22:59:17 +0100 | [diff] [blame] | 80 | * |
| 81 | * @retval None. |
| 82 | */ |
Michael Scott | 9182d2e | 2018-08-01 12:01:00 -0800 | [diff] [blame] | 83 | static void mdm_receiver_flush(struct mdm_receiver_context *ctx) |
| 84 | { |
Kumar Gala | a1b77fd | 2020-05-27 11:26:57 -0500 | [diff] [blame] | 85 | uint8_t c; |
Michael Scott | 9182d2e | 2018-08-01 12:01:00 -0800 | [diff] [blame] | 86 | |
Georgij Cernysiov | 886ab98 | 2019-02-13 23:06:40 +0100 | [diff] [blame] | 87 | __ASSERT(ctx, "invalid ctx"); |
| 88 | __ASSERT(ctx->uart_dev, "invalid ctx device"); |
Michael Scott | 9182d2e | 2018-08-01 12:01:00 -0800 | [diff] [blame] | 89 | |
Michael Scott | 9182d2e | 2018-08-01 12:01:00 -0800 | [diff] [blame] | 90 | while (uart_fifo_read(ctx->uart_dev, &c, 1) > 0) { |
| 91 | continue; |
| 92 | } |
Michael Scott | 9182d2e | 2018-08-01 12:01:00 -0800 | [diff] [blame] | 93 | } |
| 94 | |
Georgij Cernysiov | de8b39f | 2019-02-13 22:59:17 +0100 | [diff] [blame] | 95 | /** |
| 96 | * @brief Receiver UART interrupt handler. |
| 97 | * |
| 98 | * @note Fills contexts ring buffer with received data. |
| 99 | * When ring buffer is full the data is discarded. |
| 100 | * |
Benjamin Cabé | db53c3f | 2023-07-13 14:18:49 +0200 | [diff] [blame] | 101 | * @param uart_dev: uart device. |
Georgij Cernysiov | de8b39f | 2019-02-13 22:59:17 +0100 | [diff] [blame] | 102 | * |
| 103 | * @retval None. |
| 104 | */ |
Tomasz Bursztyka | e18fcbb | 2020-04-30 20:33:38 +0200 | [diff] [blame] | 105 | static void mdm_receiver_isr(const struct device *uart_dev, void *user_data) |
Michael Scott | 9182d2e | 2018-08-01 12:01:00 -0800 | [diff] [blame] | 106 | { |
| 107 | struct mdm_receiver_context *ctx; |
| 108 | int rx, ret; |
Kumar Gala | a1b77fd | 2020-05-27 11:26:57 -0500 | [diff] [blame] | 109 | static uint8_t read_buf[MAX_READ_SIZE]; |
Michael Scott | 9182d2e | 2018-08-01 12:01:00 -0800 | [diff] [blame] | 110 | |
Tomasz Bursztyka | 7d1af02 | 2020-06-24 19:06:44 +0200 | [diff] [blame] | 111 | ARG_UNUSED(user_data); |
| 112 | |
Michael Scott | 9182d2e | 2018-08-01 12:01:00 -0800 | [diff] [blame] | 113 | /* lookup the device */ |
| 114 | ctx = context_from_dev(uart_dev); |
| 115 | if (!ctx) { |
| 116 | return; |
| 117 | } |
| 118 | |
| 119 | /* get all of the data off UART as fast as we can */ |
| 120 | while (uart_irq_update(ctx->uart_dev) && |
| 121 | uart_irq_rx_ready(ctx->uart_dev)) { |
| 122 | rx = uart_fifo_read(ctx->uart_dev, read_buf, sizeof(read_buf)); |
| 123 | if (rx > 0) { |
Georgij Cernysiov | 94ee0dd | 2019-02-13 22:48:46 +0100 | [diff] [blame] | 124 | ret = ring_buf_put(&ctx->rx_rb, read_buf, rx); |
| 125 | if (ret != rx) { |
| 126 | LOG_ERR("Rx buffer doesn't have enough space. " |
| 127 | "Bytes pending: %d, written: %d", |
| 128 | rx, ret); |
Michael Scott | 9182d2e | 2018-08-01 12:01:00 -0800 | [diff] [blame] | 129 | mdm_receiver_flush(ctx); |
Georgij Cernysiov | 94ee0dd | 2019-02-13 22:48:46 +0100 | [diff] [blame] | 130 | k_sem_give(&ctx->rx_sem); |
| 131 | break; |
Michael Scott | 9182d2e | 2018-08-01 12:01:00 -0800 | [diff] [blame] | 132 | } |
Michael Scott | 9182d2e | 2018-08-01 12:01:00 -0800 | [diff] [blame] | 133 | k_sem_give(&ctx->rx_sem); |
| 134 | } |
| 135 | } |
| 136 | } |
| 137 | |
Georgij Cernysiov | de8b39f | 2019-02-13 22:59:17 +0100 | [diff] [blame] | 138 | /** |
| 139 | * @brief Configures receiver context and assigned device. |
| 140 | * |
Benjamin Cabé | db53c3f | 2023-07-13 14:18:49 +0200 | [diff] [blame] | 141 | * @param ctx: receiver context. |
Georgij Cernysiov | de8b39f | 2019-02-13 22:59:17 +0100 | [diff] [blame] | 142 | * |
| 143 | * @retval None. |
| 144 | */ |
| 145 | static void mdm_receiver_setup(struct mdm_receiver_context *ctx) |
| 146 | { |
Georgij Cernysiov | 886ab98 | 2019-02-13 23:06:40 +0100 | [diff] [blame] | 147 | __ASSERT(ctx, "invalid ctx"); |
Georgij Cernysiov | de8b39f | 2019-02-13 22:59:17 +0100 | [diff] [blame] | 148 | |
| 149 | uart_irq_rx_disable(ctx->uart_dev); |
| 150 | uart_irq_tx_disable(ctx->uart_dev); |
| 151 | mdm_receiver_flush(ctx); |
| 152 | uart_irq_callback_set(ctx->uart_dev, mdm_receiver_isr); |
| 153 | uart_irq_rx_enable(ctx->uart_dev); |
| 154 | } |
| 155 | |
| 156 | struct mdm_receiver_context *mdm_receiver_context_from_id(int id) |
| 157 | { |
| 158 | if (id >= 0 && id < MAX_MDM_CTX) { |
| 159 | return contexts[id]; |
| 160 | } else { |
| 161 | return NULL; |
| 162 | } |
| 163 | } |
| 164 | |
Michael Scott | 9182d2e | 2018-08-01 12:01:00 -0800 | [diff] [blame] | 165 | int mdm_receiver_recv(struct mdm_receiver_context *ctx, |
Kumar Gala | a1b77fd | 2020-05-27 11:26:57 -0500 | [diff] [blame] | 166 | uint8_t *buf, size_t size, size_t *bytes_read) |
Michael Scott | 9182d2e | 2018-08-01 12:01:00 -0800 | [diff] [blame] | 167 | { |
| 168 | if (!ctx) { |
| 169 | return -EINVAL; |
| 170 | } |
| 171 | |
Georgij Cernysiov | 3fe917e | 2019-02-13 23:36:39 +0100 | [diff] [blame] | 172 | if (size == 0) { |
| 173 | *bytes_read = 0; |
| 174 | return 0; |
| 175 | } |
Georgij Cernysiov | 94ee0dd | 2019-02-13 22:48:46 +0100 | [diff] [blame] | 176 | |
Georgij Cernysiov | 3fe917e | 2019-02-13 23:36:39 +0100 | [diff] [blame] | 177 | *bytes_read = ring_buf_get(&ctx->rx_rb, buf, size); |
Georgij Cernysiov | 94ee0dd | 2019-02-13 22:48:46 +0100 | [diff] [blame] | 178 | return 0; |
Michael Scott | 9182d2e | 2018-08-01 12:01:00 -0800 | [diff] [blame] | 179 | } |
| 180 | |
| 181 | int mdm_receiver_send(struct mdm_receiver_context *ctx, |
Kumar Gala | a1b77fd | 2020-05-27 11:26:57 -0500 | [diff] [blame] | 182 | const uint8_t *buf, size_t size) |
Michael Scott | 9182d2e | 2018-08-01 12:01:00 -0800 | [diff] [blame] | 183 | { |
| 184 | if (!ctx) { |
| 185 | return -EINVAL; |
| 186 | } |
| 187 | |
Georgij Cernysiov | 3fe917e | 2019-02-13 23:36:39 +0100 | [diff] [blame] | 188 | if (size == 0) { |
| 189 | return 0; |
| 190 | } |
| 191 | |
Georgij Cernysiov | 2761840 | 2019-02-13 23:20:21 +0100 | [diff] [blame] | 192 | do { |
| 193 | uart_poll_out(ctx->uart_dev, *buf++); |
Michael Scott | bc5a3f2 | 2019-03-02 09:51:05 -0800 | [diff] [blame] | 194 | } while (--size); |
Michael Scott | 9182d2e | 2018-08-01 12:01:00 -0800 | [diff] [blame] | 195 | |
| 196 | return 0; |
| 197 | } |
| 198 | |
Christoph Schramm | ce4cc46 | 2019-06-07 17:51:30 +0200 | [diff] [blame] | 199 | int mdm_receiver_sleep(struct mdm_receiver_context *ctx) |
| 200 | { |
| 201 | uart_irq_rx_disable(ctx->uart_dev); |
Gerard Marull-Paretas | 56a35e5 | 2021-07-30 11:34:09 +0200 | [diff] [blame] | 202 | #ifdef CONFIG_PM_DEVICE |
Flavio Ceolin | 6451626 | 2021-11-23 09:21:49 -0800 | [diff] [blame] | 203 | pm_device_action_run(ctx->uart_dev, PM_DEVICE_ACTION_SUSPEND); |
Christoph Schramm | ce4cc46 | 2019-06-07 17:51:30 +0200 | [diff] [blame] | 204 | #endif |
| 205 | return 0; |
| 206 | } |
| 207 | |
| 208 | int mdm_receiver_wake(struct mdm_receiver_context *ctx) |
| 209 | { |
Gerard Marull-Paretas | 56a35e5 | 2021-07-30 11:34:09 +0200 | [diff] [blame] | 210 | #ifdef CONFIG_PM_DEVICE |
Nick Ward | 941e4f0 | 2023-06-01 20:49:10 +1000 | [diff] [blame] | 211 | pm_device_action_run(ctx->uart_dev, PM_DEVICE_ACTION_RESUME); |
Christoph Schramm | ce4cc46 | 2019-06-07 17:51:30 +0200 | [diff] [blame] | 212 | #endif |
| 213 | uart_irq_rx_enable(ctx->uart_dev); |
| 214 | |
| 215 | return 0; |
| 216 | } |
| 217 | |
Michael Scott | 9182d2e | 2018-08-01 12:01:00 -0800 | [diff] [blame] | 218 | int mdm_receiver_register(struct mdm_receiver_context *ctx, |
Marcin Niestroj | 26bd4fb | 2021-06-29 15:11:22 +0200 | [diff] [blame] | 219 | const struct device *uart_dev, |
Kumar Gala | a1b77fd | 2020-05-27 11:26:57 -0500 | [diff] [blame] | 220 | uint8_t *buf, size_t size) |
Michael Scott | 9182d2e | 2018-08-01 12:01:00 -0800 | [diff] [blame] | 221 | { |
| 222 | int ret; |
| 223 | |
Georgij Cernysiov | 3fe917e | 2019-02-13 23:36:39 +0100 | [diff] [blame] | 224 | if ((!ctx) || (size == 0)) { |
Michael Scott | 9182d2e | 2018-08-01 12:01:00 -0800 | [diff] [blame] | 225 | return -EINVAL; |
| 226 | } |
| 227 | |
Marcin Niestroj | 26bd4fb | 2021-06-29 15:11:22 +0200 | [diff] [blame] | 228 | if (!device_is_ready(uart_dev)) { |
| 229 | LOG_ERR("Device is not ready: %s", |
| 230 | uart_dev ? uart_dev->name : "<null>"); |
Georgij Cernysiov | 23b64b0 | 2019-02-13 22:33:24 +0100 | [diff] [blame] | 231 | return -ENODEV; |
Michael Scott | 9182d2e | 2018-08-01 12:01:00 -0800 | [diff] [blame] | 232 | } |
| 233 | |
Marcin Niestroj | 26bd4fb | 2021-06-29 15:11:22 +0200 | [diff] [blame] | 234 | ctx->uart_dev = uart_dev; |
Georgij Cernysiov | 94ee0dd | 2019-02-13 22:48:46 +0100 | [diff] [blame] | 235 | ring_buf_init(&ctx->rx_rb, size, buf); |
Michael Scott | 9182d2e | 2018-08-01 12:01:00 -0800 | [diff] [blame] | 236 | k_sem_init(&ctx->rx_sem, 0, 1); |
| 237 | |
| 238 | ret = mdm_receiver_get(ctx); |
| 239 | if (ret < 0) { |
| 240 | return ret; |
| 241 | } |
| 242 | |
| 243 | mdm_receiver_setup(ctx); |
| 244 | return 0; |
| 245 | } |