diff options
author | xengineering <me@xengineering.eu> | 2024-05-31 10:33:56 +0200 |
---|---|---|
committer | xengineering <me@xengineering.eu> | 2024-06-01 10:25:12 +0200 |
commit | ff3fa235236f2d6d92f7869fb4f55e3655b0502a (patch) | |
tree | 8ef09c07efcdfe5194bc17a4debaba18d3286bdd | |
parent | bb029aacf08983ad6c1a6360d988e89394fc4d85 (diff) | |
download | iot-core-ff3fa235236f2d6d92f7869fb4f55e3655b0502a.tar iot-core-ff3fa235236f2d6d92f7869fb4f55e3655b0502a.tar.zst iot-core-ff3fa235236f2d6d92f7869fb4f55e3655b0502a.zip |
firmware: Separate data link related code
-rw-r--r-- | firmware/CMakeLists.txt | 1 | ||||
-rw-r--r-- | firmware/src/data_link.c | 24 | ||||
-rw-r--r-- | firmware/src/data_link.h | 10 | ||||
-rw-r--r-- | firmware/src/main.c | 32 |
4 files changed, 39 insertions, 28 deletions
diff --git a/firmware/CMakeLists.txt b/firmware/CMakeLists.txt index 8af1e4a..0a18295 100644 --- a/firmware/CMakeLists.txt +++ b/firmware/CMakeLists.txt @@ -4,4 +4,5 @@ project(iot-core) target_sources(app PRIVATE src/main.c + src/data_link.c src/eui64.c) diff --git a/firmware/src/data_link.c b/firmware/src/data_link.c new file mode 100644 index 0000000..a6d091a --- /dev/null +++ b/firmware/src/data_link.c @@ -0,0 +1,24 @@ +#include <zephyr/drivers/uart.h> + +#include "data_link.h" + +void dl_send_frame(const struct device *const uart_dev, uint8_t *buffer, + size_t len) { + for (size_t i = 0; i < len; i++) { + uint8_t octet = *(buffer + i); + switch (octet) { + case DL_SLIP_END: + uart_poll_out(uart_dev, DL_SLIP_ESC); + uart_poll_out(uart_dev, DL_SLIP_ESC_END); + break; + case DL_SLIP_ESC: + uart_poll_out(uart_dev, DL_SLIP_ESC); + uart_poll_out(uart_dev, DL_SLIP_ESC_ESC); + break; + default: + uart_poll_out(uart_dev, octet); + break; + } + } + uart_poll_out(uart_dev, DL_SLIP_END); +} diff --git a/firmware/src/data_link.h b/firmware/src/data_link.h new file mode 100644 index 0000000..359cb2b --- /dev/null +++ b/firmware/src/data_link.h @@ -0,0 +1,10 @@ +#include <sys/types.h> +#include <zephyr/device.h> + +#define DL_SLIP_END 0xC0 +#define DL_SLIP_ESC 0xDB +#define DL_SLIP_ESC_END 0xDC +#define DL_SLIP_ESC_ESC 0xDD + +void dl_send_frame(const struct device *const uart_dev, uint8_t *buffer, + size_t len); diff --git a/firmware/src/main.c b/firmware/src/main.c index dedd603..18e4564 100644 --- a/firmware/src/main.c +++ b/firmware/src/main.c @@ -1,39 +1,15 @@ #include <stdbool.h> -#include <stdint.h> #include <zephyr/device.h> #include <zephyr/drivers/uart.h> #include <zephyr/kernel.h> #include <zephyr/sys/util.h> +#include "data_link.h" + #define UART_DEVICE_NODE DT_CHOSEN(zephyr_shell_uart) static const struct device *const uart_dev = DEVICE_DT_GET(UART_DEVICE_NODE); -#define SLIP_END 0xC0 -#define SLIP_ESC 0xDB -#define SLIP_ESC_END 0xDC -#define SLIP_ESC_ESC 0xDD - -void send_frame(uint8_t *buffer, size_t len) { - for (size_t i = 0; i < len; i++) { - uint8_t octet = *(buffer + i); - switch (octet) { - case SLIP_END: - uart_poll_out(uart_dev, SLIP_ESC); - uart_poll_out(uart_dev, SLIP_ESC_END); - break; - case SLIP_ESC: - uart_poll_out(uart_dev, SLIP_ESC); - uart_poll_out(uart_dev, SLIP_ESC_ESC); - break; - default: - uart_poll_out(uart_dev, octet); - break; - } - } - uart_poll_out(uart_dev, SLIP_END); -} - int main(void) { if (!device_is_ready(uart_dev)) { @@ -41,11 +17,11 @@ int main(void) return 0; } - uint8_t frame[] = {0xDE, SLIP_END, SLIP_ESC, 0xAD}; + uint8_t frame[] = {0xDE, DL_SLIP_END, DL_SLIP_ESC, 0xAD}; while (true) { k_sleep(K_MSEC(1000)); - send_frame(frame, ARRAY_SIZE(frame)); + dl_send_frame(uart_dev, frame, ARRAY_SIZE(frame)); } return 0; |