summaryrefslogtreecommitdiff
path: root/firmware/src/main.c
blob: dedd60339eeb1f2d1a26a414b573c6248ade1ead (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
#include <stdbool.h>
#include <stdint.h>

#include <zephyr/device.h>
#include <zephyr/drivers/uart.h>
#include <zephyr/kernel.h>
#include <zephyr/sys/util.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)) {
		printk("UART device not found!");
		return 0;
	}

	uint8_t frame[] = {0xDE, SLIP_END, SLIP_ESC, 0xAD};

	while (true) {
		k_sleep(K_MSEC(1000));
		send_frame(frame, ARRAY_SIZE(frame));
	}

	return 0;
}