kfsw-comms/src/uart.c
| Line | Branch | Exec | Source |
|---|---|---|---|
| 1 | #include <errno.h> | ||
| 2 | #include <string.h> | ||
| 3 | |||
| 4 | #include <zephyr/device.h> | ||
| 5 | #include <zephyr/devicetree.h> | ||
| 6 | #include <zephyr/drivers/uart.h> | ||
| 7 | #include <zephyr/sys/util.h> | ||
| 8 | |||
| 9 | #include <csp/csp.h> | ||
| 10 | #include <csp/csp_id.h> | ||
| 11 | #include <csp/csp_iflist.h> | ||
| 12 | #include <csp/csp_rtable.h> | ||
| 13 | #include <csp/drivers/usart.h> | ||
| 14 | #include <csp/interfaces/csp_if_kiss.h> | ||
| 15 | |||
| 16 | #include <kfsw/comms/csp.h> | ||
| 17 | #include <kfsw/comms/uart.h> | ||
| 18 | |||
| 19 | #include "uart_internal.h" | ||
| 20 | |||
| 21 | #define KFSW_UART_TEST_PAYLOAD_SIZE 128U | ||
| 22 | #define KFSW_UART_IRQ_RX_CHUNK_SIZE 32U | ||
| 23 | #define KFSW_CSP_KISS_UARTS_COMPAT kfsw_csp_kiss_uarts | ||
| 24 | |||
| 25 | struct kfsw_uart_config { | ||
| 26 | const struct device *device; | ||
| 27 | const char *interface_name; | ||
| 28 | struct uart_config serial; | ||
| 29 | uint16_t address; | ||
| 30 | uint16_t prefix_length; | ||
| 31 | }; | ||
| 32 | |||
| 33 | struct kfsw_uart_context { | ||
| 34 | const struct kfsw_uart_config *config; | ||
| 35 | csp_iface_t *interface; | ||
| 36 | #if CONFIG_KFSW_CSP_UART_INTERRUPT_DRIVEN | ||
| 37 | csp_iface_t interrupt_interface; | ||
| 38 | csp_kiss_interface_data_t kiss_data; | ||
| 39 | #endif | ||
| 40 | }; | ||
| 41 | |||
| 42 | #define KFSW_UART_NODE(child_id) DT_PHANDLE(child_id, uart) | ||
| 43 | #define KFSW_UART_SERIAL_CONFIG(node_id) \ | ||
| 44 | { \ | ||
| 45 | .baudrate = DT_PROP(node_id, current_speed), \ | ||
| 46 | .parity = DT_ENUM_IDX(node_id, parity), \ | ||
| 47 | .stop_bits = DT_ENUM_IDX(node_id, stop_bits), \ | ||
| 48 | .data_bits = DT_PROP(node_id, data_bits) - 5, \ | ||
| 49 | .flow_ctrl = DT_PROP(node_id, hw_flow_control) ? UART_CFG_FLOW_CTRL_RTS_CTS \ | ||
| 50 | : UART_CFG_FLOW_CTRL_NONE, \ | ||
| 51 | } | ||
| 52 | |||
| 53 | #if DT_HAS_COMPAT_STATUS_OKAY(KFSW_CSP_KISS_UARTS_COMPAT) | ||
| 54 | |||
| 55 | #define KFSW_CSP_UARTS_NODE DT_COMPAT_GET_ANY_STATUS_OKAY(KFSW_CSP_KISS_UARTS_COMPAT) | ||
| 56 | #define KFSW_UART_ASSERT_CHILD(child_id) \ | ||
| 57 | BUILD_ASSERT(DT_NODE_HAS_STATUS(KFSW_UART_NODE(child_id), okay), \ | ||
| 58 | "each K-FSW CSP KISS UART must be enabled"); \ | ||
| 59 | BUILD_ASSERT(DT_NODE_HAS_PROP(KFSW_UART_NODE(child_id), current_speed), \ | ||
| 60 | "each K-FSW CSP KISS UART must define current-speed"); \ | ||
| 61 | BUILD_ASSERT(sizeof(DT_PROP(child_id, interface_name)) <= CSP_IFLIST_NAME_MAX, \ | ||
| 62 | "CSP interface names must contain at most nine route-safe characters"); | ||
| 63 | |||
| 64 | DT_FOREACH_CHILD_STATUS_OKAY(KFSW_CSP_UARTS_NODE, KFSW_UART_ASSERT_CHILD) | ||
| 65 | |||
| 66 | #define KFSW_UART_CONFIG_ENTRY(child_id) \ | ||
| 67 | { \ | ||
| 68 | .device = DEVICE_DT_GET(KFSW_UART_NODE(child_id)), \ | ||
| 69 | .interface_name = DT_PROP(child_id, interface_name), \ | ||
| 70 | .serial = KFSW_UART_SERIAL_CONFIG(KFSW_UART_NODE(child_id)), \ | ||
| 71 | .address = DT_PROP_OR(child_id, address, CONFIG_KFSW_CSP_ADDRESS), \ | ||
| 72 | .prefix_length = DT_PROP(child_id, prefix_length), \ | ||
| 73 | } | ||
| 74 | |||
| 75 | static const struct kfsw_uart_config uart_configs[] = { | ||
| 76 | DT_FOREACH_CHILD_STATUS_OKAY_SEP(KFSW_CSP_UARTS_NODE, KFSW_UART_CONFIG_ENTRY, (, ))}; | ||
| 77 | |||
| 78 | BUILD_ASSERT(ARRAY_SIZE(uart_configs) > 0, | ||
| 79 | "a kfsw,csp-kiss-uarts node must have at least one enabled child"); | ||
| 80 | |||
| 81 | #else | ||
| 82 | |||
| 83 | #define KFSW_CSP_UART_NODE DT_CHOSEN(kfsw_csp_uart) | ||
| 84 | |||
| 85 | BUILD_ASSERT(DT_HAS_CHOSEN(kfsw_csp_uart), | ||
| 86 | "KFSW_CSP_KISS_UART requires a kfsw,csp-uart chosen node or " | ||
| 87 | "kfsw,csp-kiss-uarts container"); | ||
| 88 | BUILD_ASSERT(DT_NODE_HAS_STATUS(KFSW_CSP_UART_NODE, okay), | ||
| 89 | "the chosen K-FSW CSP UART must be enabled"); | ||
| 90 | BUILD_ASSERT(DT_NODE_HAS_PROP(KFSW_CSP_UART_NODE, current_speed), | ||
| 91 | "the chosen K-FSW CSP UART must define current-speed"); | ||
| 92 | |||
| 93 | static const struct kfsw_uart_config uart_configs[] = { | ||
| 94 | { | ||
| 95 | .device = DEVICE_DT_GET(KFSW_CSP_UART_NODE), | ||
| 96 | .interface_name = "KISS", | ||
| 97 | .serial = KFSW_UART_SERIAL_CONFIG(KFSW_CSP_UART_NODE), | ||
| 98 | .address = CONFIG_KFSW_CSP_ADDRESS, | ||
| 99 | .prefix_length = 0, | ||
| 100 | }, | ||
| 101 | }; | ||
| 102 | |||
| 103 | #endif | ||
| 104 | |||
| 105 | #if !CONFIG_KFSW_CSP_UART_INTERRUPT_DRIVEN | ||
| 106 | BUILD_ASSERT(CONFIG_CSP_UART_RX_THREAD_NUM >= ARRAY_SIZE(uart_configs), | ||
| 107 | "CONFIG_CSP_UART_RX_THREAD_NUM must cover every polling KISS/UART interface"); | ||
| 108 | #endif | ||
| 109 | |||
| 110 | static struct kfsw_uart_context uart_contexts[ARRAY_SIZE(uart_configs)]; | ||
| 111 | static bool opened; | ||
| 112 | static int open_result; | ||
| 113 | |||
| 114 | 28 | static bool route_safe_name(const char *name) | |
| 115 | { | ||
| 116 | 28 | size_t length = strnlen(name, CSP_IFLIST_NAME_MAX + 1U); | |
| 117 | |||
| 118 | 28 | if (length == 0U || length >= CSP_IFLIST_NAME_MAX) { | |
| 119 | ✗ | return false; | |
| 120 | } | ||
| 121 | |||
| 122 | 196 | for (size_t i = 0U; i < length; ++i) { | |
| 123 | 168 | const char character = name[i]; | |
| 124 | |||
| 125 | 168 | if (!((character >= 'A' && character <= 'Z') || | |
| 126 | 56 | (character >= 'a' && character <= 'z') || | |
| 127 | 56 | (character >= '0' && character <= '9') || character == '_' || | |
| 128 | character == '-')) { | ||
| 129 | ✗ | return false; | |
| 130 | } | ||
| 131 | } | ||
| 132 | |||
| 133 | 28 | return true; | |
| 134 | } | ||
| 135 | |||
| 136 | 14 | static int preflight(void) | |
| 137 | { | ||
| 138 | 38 | for (size_t i = 0U; i < ARRAY_SIZE(uart_configs); ++i) { | |
| 139 | 28 | const struct kfsw_uart_config *config = &uart_configs[i]; | |
| 140 | |||
| 141 | 56 | if (!route_safe_name(config->interface_name) || | |
| 142 | 56 | config->address > csp_id_get_max_nodeid() || | |
| 143 | 28 | config->prefix_length > csp_id_get_host_bits() || | |
| 144 | 56 | !device_is_ready(config->device) || | |
| 145 | 28 | csp_iflist_get_by_name(config->interface_name) != NULL) { | |
| 146 | ✗ | return CSP_ERR_INVAL; | |
| 147 | } | ||
| 148 | |||
| 149 | 38 | for (size_t earlier = 0U; earlier < i; ++earlier) { | |
| 150 | 14 | if (strcmp(config->interface_name, uart_configs[earlier].interface_name) == | |
| 151 | 10 | 0 || | |
| 152 | 10 | config->device == uart_configs[earlier].device) { | |
| 153 | 4 | return CSP_ERR_INVAL; | |
| 154 | } | ||
| 155 | } | ||
| 156 | } | ||
| 157 | |||
| 158 | 10 | return CSP_ERR_NONE; | |
| 159 | } | ||
| 160 | |||
| 161 | 20 | static int configure_device(const struct kfsw_uart_config *config) | |
| 162 | { | ||
| 163 | /* | ||
| 164 | * Native PTYs return ENOSYS; baud and framing are set by the host bridge. | ||
| 165 | */ | ||
| 166 | 20 | int result = uart_configure(config->device, &config->serial); | |
| 167 | |||
| 168 | 20 | if (result != 0 && result != -ENOSYS) { | |
| 169 | ✗ | return CSP_ERR_DRIVER; | |
| 170 | } | ||
| 171 | |||
| 172 | 20 | return CSP_ERR_NONE; | |
| 173 | } | ||
| 174 | |||
| 175 | #if CONFIG_KFSW_CSP_UART_INTERRUPT_DRIVEN | ||
| 176 | static int interrupt_tx(void *driver_data, const uint8_t *data, size_t data_length) | ||
| 177 | { | ||
| 178 | struct kfsw_uart_context *context = driver_data; | ||
| 179 | |||
| 180 | for (size_t i = 0U; i < data_length; ++i) { | ||
| 181 | uart_poll_out(context->config->device, data[i]); | ||
| 182 | } | ||
| 183 | |||
| 184 | return CSP_ERR_NONE; | ||
| 185 | } | ||
| 186 | |||
| 187 | static void interrupt_rx(const struct device *device, void *user_data) | ||
| 188 | { | ||
| 189 | struct kfsw_uart_context *context = user_data; | ||
| 190 | uint8_t data[KFSW_UART_IRQ_RX_CHUNK_SIZE]; | ||
| 191 | int task_woken = 0; | ||
| 192 | int received; | ||
| 193 | |||
| 194 | if (!uart_irq_update(device)) { | ||
| 195 | return; | ||
| 196 | } | ||
| 197 | |||
| 198 | if (uart_err_check(device) > 0) { | ||
| 199 | context->interrupt_interface.rx_error++; | ||
| 200 | } | ||
| 201 | |||
| 202 | if (!uart_irq_rx_ready(device)) { | ||
| 203 | return; | ||
| 204 | } | ||
| 205 | |||
| 206 | do { | ||
| 207 | received = uart_fifo_read(device, data, sizeof(data)); | ||
| 208 | if (received > 0) { | ||
| 209 | csp_kiss_rx(&context->interrupt_interface, data, (size_t)received, | ||
| 210 | &task_woken); | ||
| 211 | } | ||
| 212 | } while (received > 0); | ||
| 213 | } | ||
| 214 | |||
| 215 | static int open_interrupt(struct kfsw_uart_context *context) | ||
| 216 | { | ||
| 217 | int result; | ||
| 218 | |||
| 219 | context->interrupt_interface.name = context->config->interface_name; | ||
| 220 | context->interrupt_interface.addr = context->config->address; | ||
| 221 | context->interrupt_interface.netmask = context->config->prefix_length; | ||
| 222 | context->interrupt_interface.driver_data = context; | ||
| 223 | context->interrupt_interface.interface_data = &context->kiss_data; | ||
| 224 | context->kiss_data.tx_func = interrupt_tx; | ||
| 225 | |||
| 226 | result = uart_irq_callback_user_data_set(context->config->device, interrupt_rx, context); | ||
| 227 | if (result != 0) { | ||
| 228 | return CSP_ERR_DRIVER; | ||
| 229 | } | ||
| 230 | |||
| 231 | result = csp_kiss_add_interface(&context->interrupt_interface); | ||
| 232 | if (result != CSP_ERR_NONE) { | ||
| 233 | return result; | ||
| 234 | } | ||
| 235 | |||
| 236 | context->interface = &context->interrupt_interface; | ||
| 237 | uart_irq_err_enable(context->config->device); | ||
| 238 | uart_irq_rx_enable(context->config->device); | ||
| 239 | return CSP_ERR_NONE; | ||
| 240 | } | ||
| 241 | #endif | ||
| 242 | |||
| 243 | 18 | static int open_context(struct kfsw_uart_context *context) | |
| 244 | { | ||
| 245 | #if CONFIG_KFSW_CSP_UART_INTERRUPT_DRIVEN | ||
| 246 | return open_interrupt(context); | ||
| 247 | #else | ||
| 248 | 18 | const csp_usart_conf_t usart_config = { | |
| 249 | 18 | .device = context->config->device->name, | |
| 250 | 18 | .baudrate = context->config->serial.baudrate, | |
| 251 | 18 | .databits = context->config->serial.data_bits + 5, | |
| 252 | 18 | .stopbits = context->config->serial.stop_bits == UART_CFG_STOP_BITS_2 ? 2 : 1, | |
| 253 | 18 | .paritysetting = context->config->serial.parity, | |
| 254 | }; | ||
| 255 | 18 | int result = csp_usart_open_and_add_kiss_interface( | |
| 256 | 18 | &usart_config, context->config->interface_name, context->config->address, | |
| 257 | &context->interface); | ||
| 258 | |||
| 259 | 18 | if (result == CSP_ERR_NONE) { | |
| 260 | 14 | context->interface->netmask = context->config->prefix_length; | |
| 261 | } | ||
| 262 | 18 | return result; | |
| 263 | #endif | ||
| 264 | } | ||
| 265 | |||
| 266 | 30 | int kfsw_uart_open_all(void) | |
| 267 | { | ||
| 268 | int result; | ||
| 269 | |||
| 270 | /* The USART driver cannot close contexts after a partial open. */ | ||
| 271 | 30 | if (opened || open_result != CSP_ERR_NONE) { | |
| 272 | 16 | return open_result; | |
| 273 | } | ||
| 274 | |||
| 275 | 14 | result = preflight(); | |
| 276 | 14 | if (result != CSP_ERR_NONE) { | |
| 277 | 4 | return result; | |
| 278 | } | ||
| 279 | |||
| 280 | 30 | for (size_t i = 0U; i < ARRAY_SIZE(uart_configs); ++i) { | |
| 281 | 20 | result = configure_device(&uart_configs[i]); | |
| 282 | 20 | if (result != CSP_ERR_NONE) { | |
| 283 | ✗ | return result; | |
| 284 | } | ||
| 285 | } | ||
| 286 | |||
| 287 | 22 | for (size_t i = 0U; i < ARRAY_SIZE(uart_configs); ++i) { | |
| 288 | 18 | uart_contexts[i].config = &uart_configs[i]; | |
| 289 | 18 | open_result = open_context(&uart_contexts[i]); | |
| 290 | 18 | if (open_result != CSP_ERR_NONE) { | |
| 291 | 4 | return open_result; | |
| 292 | } | ||
| 293 | #if CONFIG_KFSW_CSP_UART_CODEC | ||
| 294 | 8 | open_result = kfsw_uart_codec_attach(uart_contexts[i].interface); | |
| 295 | 8 | if (open_result != 0) { | |
| 296 | 2 | return open_result; | |
| 297 | } | ||
| 298 | #endif | ||
| 299 | } | ||
| 300 | |||
| 301 | #if CONFIG_KFSW_CSP_UART_CODEC | ||
| 302 | 2 | open_result = kfsw_uart_codec_check(); | |
| 303 | 2 | if (open_result != 0) { | |
| 304 | 2 | return open_result; | |
| 305 | } | ||
| 306 | #endif | ||
| 307 | 2 | opened = true; | |
| 308 | 2 | return CSP_ERR_NONE; | |
| 309 | } | ||
| 310 | |||
| 311 | 20 | static void get_info_at(size_t index, struct kfsw_uart_info *info) | |
| 312 | { | ||
| 313 | 20 | const struct kfsw_uart_config *config = &uart_configs[index]; | |
| 314 | 20 | csp_iface_t *interface = uart_contexts[index].interface; | |
| 315 | |||
| 316 | 20 | memset(info, 0, sizeof(*info)); | |
| 317 | 20 | info->device_name = config->device->name; | |
| 318 | 20 | info->baudrate = config->serial.baudrate; | |
| 319 | 20 | info->ready = device_is_ready(config->device) && interface != NULL; | |
| 320 | 20 | info->interface_started = interface != NULL; | |
| 321 | 20 | info->interface_name = interface != NULL ? interface->name : config->interface_name; | |
| 322 | 20 | info->node = config->address; | |
| 323 | 20 | info->prefix_length = config->prefix_length; | |
| 324 | 20 | info->peer = CONFIG_KFSW_CSP_UART_PEER_ADDRESS; | |
| 325 | |||
| 326 | 20 | if (interface != NULL) { | |
| 327 | 18 | info->tx_packets = interface->tx; | |
| 328 | 18 | info->rx_packets = interface->rx; | |
| 329 | 18 | info->tx_errors = interface->tx_error; | |
| 330 | 18 | info->rx_errors = interface->rx_error; | |
| 331 | 18 | info->dropped_packets = interface->drop; | |
| 332 | 18 | info->frame_errors = interface->frame; | |
| 333 | } | ||
| 334 | 20 | } | |
| 335 | |||
| 336 | ✗ | void kfsw_uart_get_info(struct kfsw_uart_info *info) | |
| 337 | { | ||
| 338 | ✗ | if (info != NULL) { | |
| 339 | ✗ | get_info_at(0U, info); | |
| 340 | } | ||
| 341 | ✗ | } | |
| 342 | |||
| 343 | 10 | void kfsw_uart_visit(kfsw_uart_visitor_t visitor, void *context) | |
| 344 | { | ||
| 345 | 10 | if (visitor == NULL) { | |
| 346 | ✗ | return; | |
| 347 | } | ||
| 348 | |||
| 349 | 30 | for (size_t i = 0U; i < ARRAY_SIZE(uart_configs); ++i) { | |
| 350 | struct kfsw_uart_info info; | ||
| 351 | |||
| 352 | 20 | get_info_at(i, &info); | |
| 353 | 20 | if (!visitor(&info, context)) { | |
| 354 | ✗ | break; | |
| 355 | } | ||
| 356 | } | ||
| 357 | } | ||
| 358 | |||
| 359 | ✗ | static bool managed_interface(const csp_iface_t *interface) | |
| 360 | { | ||
| 361 | ✗ | for (size_t i = 0U; i < ARRAY_SIZE(uart_contexts); ++i) { | |
| 362 | ✗ | if (uart_contexts[i].interface == interface) { | |
| 363 | ✗ | return true; | |
| 364 | } | ||
| 365 | } | ||
| 366 | |||
| 367 | ✗ | return false; | |
| 368 | } | ||
| 369 | |||
| 370 | ✗ | int kfsw_uart_test_peer(uint16_t peer, uint32_t timeout_ms, | |
| 371 | struct kfsw_uart_test_result *test_result) | ||
| 372 | { | ||
| 373 | csp_route_t *route; | ||
| 374 | uint32_t round_trip_us; | ||
| 375 | int result; | ||
| 376 | |||
| 377 | ✗ | if (test_result == NULL || !opened) { | |
| 378 | ✗ | return CSP_ERR_INVAL; | |
| 379 | } | ||
| 380 | |||
| 381 | /* A packet to this node's own address is delivered locally, whatever the route. */ | ||
| 382 | ✗ | route = csp_rtable_find_route(peer); | |
| 383 | ✗ | if (route == NULL || !managed_interface(route->iface) || | |
| 384 | ✗ | csp_iflist_get_by_addr(peer) != NULL) { | |
| 385 | ✗ | return CSP_ERR_NOTSUP; | |
| 386 | } | ||
| 387 | |||
| 388 | ✗ | result = kfsw_csp_ping(peer, timeout_ms, KFSW_UART_TEST_PAYLOAD_SIZE, &round_trip_us); | |
| 389 | ✗ | if (result != CSP_ERR_NONE) { | |
| 390 | ✗ | return result; | |
| 391 | } | ||
| 392 | |||
| 393 | ✗ | test_result->peer = peer; | |
| 394 | ✗ | test_result->interface_name = route->iface->name; | |
| 395 | ✗ | test_result->round_trip_us = round_trip_us; | |
| 396 | ✗ | return CSP_ERR_NONE; | |
| 397 | } | ||
| 398 |