Repository navigation
Expand file tree
/
Copy pathbnb_uart.h
More file actions
103 lines (89 loc) · 3.79 KB
/
Copy pathbnb_uart.h
File metadata and controls
103 lines (89 loc) · 3.79 KB
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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
#ifndef BNB_UART_H
#define BNB_UART_H
/*
* bnb_uart: send and receive framed, CRC-checked messages over a UART.
* Plain C, no dependencies beyond libc. Works the same on ESP32 and Linux.
*
* Frame on the wire:
*
* [0x02] [LEN_HI] [LEN_LO] [payload ... LEN bytes] [CRC_HI] [CRC_LO] [0x03]
* start \_____ length __/ \__ CRC __/ end
*
* CRC is CRC-16/CCITT-FALSE (poly 0x1021, init 0xFFFF) over the two length
* bytes plus the payload.
*
* ---------------------------------------------------------------------------
* EXTENDING
*
* New platform (e.g. a TCP socket, a USB serial port):
* Implement the two bnb_port_* functions below, plus an open function that
* returns an int "port" (a UART number, a file descriptor, ...). Nothing
* in bnb_uart.c needs to change.
*
* Message types / extra header fields:
* Put them at the start of the payload, e.g. payload[0] = message type.
* The CRC already covers them.
*
* Different limits or markers:
* Change the #defines below. Both sides must agree.
* ---------------------------------------------------------------------------
*/
#include <stdint.h>
#include <stddef.h>
#define BNB_START 0x02
#define BNB_END 0x03
#define BNB_MAX_PAYLOAD 512
#define BNB_CRC_INIT 0xFFFF
/* Return codes (0 = success, negative = failure) */
enum {
BNB_OK = 0,
BNB_NO_FRAME = -1, /* no complete frame yet (not an error: try again later) */
BNB_ERR_IO = -2, /* port read/write failed */
BNB_ERR_FRAME = -3, /* bad length or missing end byte */
BNB_ERR_CRC = -4, /* checksum mismatch */
BNB_ERR_SIZE = -5, /* payload too big to send */
};
/* Send one message. */
int bnb_send(int port, const void *payload, size_t len);
/*
* Receiver state. Declare one per port and zero it before first use:
*
* bnb_rx_t rx = {0};
*
* It remembers a half-received frame between calls, which is what lets
* bnb_receive return without blocking.
*/
typedef struct {
uint8_t buf[BNB_MAX_PAYLOAD + 6]; /* frame being assembled */
size_t count; /* bytes of it received so far */
const uint8_t *payload; /* set on BNB_OK: NUL-terminated payload ... */
size_t len; /* ... and its length. Valid until the next bnb_receive call. */
} bnb_rx_t;
/*
* Read whatever bytes are available and try to finish a frame.
*
* BNB_OK a valid frame is ready in rx->payload / rx->len
* BNB_NO_FRAME not enough data yet; call again later (progress is kept)
* BNB_ERR_CRC / BNB_ERR_FRAME a bad frame was dropped; just call again
* BNB_ERR_IO the port failed
*
* timeout_ms is how long to wait for each incoming byte:
* 0 never wait: return immediately if nothing is waiting (non-blocking)
* >0 wait up to this long for more bytes (blocking)
*
* After BNB_OK, call again to get the next frame; any bytes already received
* for it are still waiting in the port.
*/
int bnb_receive(int port, bnb_rx_t *rx, int timeout_ms);
/* CRC helper. Start with BNB_CRC_INIT; pass the result back in to continue. */
uint16_t bnb_crc16(uint16_t crc, const void *data, size_t len);
/* ---- Port interface: implement these for each platform ---- */
/* Write all len bytes. Return bytes written, or -1 on error. */
int bnb_port_write(int port, const uint8_t *data, size_t len);
/* Read one byte, waiting up to timeout_ms (0 = don't wait).
Return 1 = got a byte, 0 = nothing available, -1 = error. */
int bnb_port_read_byte(int port, uint8_t *byte, int timeout_ms);
/* ---- Opening a port (platform specific; each returns a port or -1) ---- */
int bnb_esp32_uart_open(int uart_num, int tx_pin, int rx_pin, int baud); /* port_esp32.c */
int bnb_linux_uart_open(const char *device, int baud); /* port_linux.c */
#endif