kfsw-comms/src/can.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/can.h> | ||
| 7 | |||
| 8 | #include <csp/csp.h> | ||
| 9 | #include <csp/csp_iflist.h> | ||
| 10 | #include <csp/csp_rtable.h> | ||
| 11 | #include <csp/drivers/can_zephyr.h> | ||
| 12 | |||
| 13 | #include <kfsw/comms/can.h> | ||
| 14 | #include <kfsw/comms/csp.h> | ||
| 15 | |||
| 16 | /* No logging here: this layer is below the log service. */ | ||
| 17 | |||
| 18 | #define KFSW_CAN_NODE DT_CHOSEN(kfsw_csp_can) | ||
| 19 | |||
| 20 | BUILD_ASSERT(DT_NODE_EXISTS(KFSW_CAN_NODE), | ||
| 21 | "KFSW_CSP_CAN requires a kfsw,csp-can chosen node naming a CAN controller"); | ||
| 22 | |||
| 23 | BUILD_ASSERT(sizeof(CONFIG_KFSW_CSP_CAN_INTERFACE_NAME) <= KFSW_CAN_INTERFACE_NAME_SIZE, | ||
| 24 | "the CAN interface name must fit libcsp's nine-character parser limit"); | ||
| 25 | |||
| 26 | /* libcsp shifts by host bits minus the prefix; a longer prefix is undefined. */ | ||
| 27 | BUILD_ASSERT(CONFIG_KFSW_CSP_CAN_PREFIX_LENGTH <= KFSW_CSP_HOST_BITS, | ||
| 28 | "the CAN prefix length must not exceed the CSP address width"); | ||
| 29 | |||
| 30 | static const struct device *const can_device = DEVICE_DT_GET(KFSW_CAN_NODE); | ||
| 31 | |||
| 32 | /* Several CFP frames, so the test covers reassembly as well as one frame. */ | ||
| 33 | #define KFSW_CAN_TEST_PAYLOAD_SIZE 64U | ||
| 34 | |||
| 35 | static csp_iface_t *can_interface; | ||
| 36 | static uint32_t applied_bitrate = CONFIG_KFSW_CSP_CAN_BITRATE; | ||
| 37 | |||
| 38 | 6 | bool kfsw_can_bitrate_supported(uint32_t bitrate) | |
| 39 | { | ||
| 40 | 6 | switch (bitrate) { | |
| 41 | 4 | case KFSW_CAN_BITRATE_125K: | |
| 42 | case KFSW_CAN_BITRATE_250K: | ||
| 43 | case KFSW_CAN_BITRATE_500K: | ||
| 44 | case KFSW_CAN_BITRATE_800K: | ||
| 45 | case KFSW_CAN_BITRATE_1M: | ||
| 46 | 4 | return true; | |
| 47 | 2 | default: | |
| 48 | 2 | return false; | |
| 49 | } | ||
| 50 | } | ||
| 51 | |||
| 52 | 4 | int kfsw_can_open(void) | |
| 53 | { | ||
| 54 | int result; | ||
| 55 | |||
| 56 | 4 | if (can_interface != NULL) { | |
| 57 | ✗ | return 0; | |
| 58 | } | ||
| 59 | 4 | if (!device_is_ready(can_device)) { | |
| 60 | ✗ | return -ENODEV; | |
| 61 | } | ||
| 62 | 4 | if (!kfsw_can_bitrate_supported(applied_bitrate)) { | |
| 63 | ✗ | return -EINVAL; | |
| 64 | } | ||
| 65 | |||
| 66 | /* Accept every frame; libcsp filters by the CFP header. */ | ||
| 67 | 4 | result = csp_can_open_and_add_interface(can_device, CONFIG_KFSW_CSP_CAN_INTERFACE_NAME, | |
| 68 | CONFIG_KFSW_CSP_ADDRESS, applied_bitrate, 0U, 0U, | ||
| 69 | &can_interface); | ||
| 70 | 4 | if (result != CSP_ERR_NONE) { | |
| 71 | 2 | can_interface = NULL; | |
| 72 | 2 | return -EIO; | |
| 73 | } | ||
| 74 | |||
| 75 | 2 | can_interface->netmask = CONFIG_KFSW_CSP_CAN_PREFIX_LENGTH; | |
| 76 | 2 | return 0; | |
| 77 | } | ||
| 78 | |||
| 79 | 6 | void kfsw_can_get_info(struct kfsw_can_info *info) | |
| 80 | { | ||
| 81 | 6 | if (info == NULL) { | |
| 82 | ✗ | return; | |
| 83 | } | ||
| 84 | 6 | memset(info, 0, sizeof(*info)); | |
| 85 | 6 | info->bitrate = applied_bitrate; | |
| 86 | 6 | if (can_interface == NULL) { | |
| 87 | 6 | return; | |
| 88 | } | ||
| 89 | ✗ | (void)strncpy(info->interface_name, CONFIG_KFSW_CSP_CAN_INTERFACE_NAME, | |
| 90 | sizeof(info->interface_name) - 1U); | ||
| 91 | ✗ | info->address = (uint16_t)CONFIG_KFSW_CSP_ADDRESS; | |
| 92 | ✗ | info->prefix_length = (uint8_t)CONFIG_KFSW_CSP_CAN_PREFIX_LENGTH; | |
| 93 | ✗ | info->ready = true; | |
| 94 | } | ||
| 95 | |||
| 96 | 2 | int kfsw_can_set_bitrate(uint32_t bitrate) | |
| 97 | { | ||
| 98 | int result; | ||
| 99 | |||
| 100 | 2 | if (!kfsw_can_bitrate_supported(bitrate)) { | |
| 101 | 2 | return -EINVAL; | |
| 102 | } | ||
| 103 | ✗ | if (bitrate == applied_bitrate) { | |
| 104 | ✗ | return 0; | |
| 105 | } | ||
| 106 | ✗ | if (can_interface == NULL) { | |
| 107 | /* Not open yet: keep the value for the next open. */ | ||
| 108 | ✗ | applied_bitrate = bitrate; | |
| 109 | ✗ | return 0; | |
| 110 | } | ||
| 111 | |||
| 112 | ✗ | result = csp_can_stop(can_interface); | |
| 113 | ✗ | if (result != CSP_ERR_NONE) { | |
| 114 | ✗ | return -EIO; | |
| 115 | } | ||
| 116 | ✗ | can_interface = NULL; | |
| 117 | |||
| 118 | ✗ | applied_bitrate = bitrate; | |
| 119 | ✗ | return kfsw_can_open(); | |
| 120 | } | ||
| 121 | |||
| 122 | ✗ | int kfsw_can_test_peer(uint16_t peer, uint32_t timeout_ms, struct kfsw_can_test_result *result) | |
| 123 | { | ||
| 124 | csp_route_t *route; | ||
| 125 | uint32_t round_trip_us; | ||
| 126 | int outcome; | ||
| 127 | |||
| 128 | ✗ | if ((result == NULL) || (can_interface == NULL)) { | |
| 129 | ✗ | return CSP_ERR_INVAL; | |
| 130 | } | ||
| 131 | /* A packet to this node's own address is delivered locally, whatever the route. */ | ||
| 132 | ✗ | route = csp_rtable_find_route(peer); | |
| 133 | ✗ | if ((route == NULL) || (route->iface != can_interface) || | |
| 134 | ✗ | (csp_iflist_get_by_addr(peer) != NULL)) { | |
| 135 | ✗ | return CSP_ERR_NOTSUP; | |
| 136 | } | ||
| 137 | ✗ | outcome = kfsw_csp_ping(peer, timeout_ms, KFSW_CAN_TEST_PAYLOAD_SIZE, &round_trip_us); | |
| 138 | ✗ | if (outcome != CSP_ERR_NONE) { | |
| 139 | ✗ | return outcome; | |
| 140 | } | ||
| 141 | ✗ | result->peer = peer; | |
| 142 | ✗ | result->round_trip_us = round_trip_us; | |
| 143 | ✗ | return CSP_ERR_NONE; | |
| 144 | } | ||
| 145 |