esp-sdr-zephyr
Raw I/Q receive and transmit on the ESP32-S3/C6 Wi-Fi radio, for Zephyr
Loading...
Searching...
No Matches
esdr_link.h
Go to the documentation of this file.
1/*
2 * SPDX-License-Identifier: GPL-3.0-or-later
3 */
4
25
26#ifndef ESP_SDR_ESDR_LINK_H_
27#define ESP_SDR_ESDR_LINK_H_
28
29#include <stddef.h>
30#include <stdint.h>
31#include <string.h>
32
33#ifdef __cplusplus
34extern "C" {
35#endif
36
37#define ESDR_LINK_VERSION 1U
38/* Type, flags, sequence number. */
39#define ESDR_LINK_HDR_BYTES 4U
40#define ESDR_LINK_CRC_BYTES 4U
41/* A device goes back to the older transport after this many empty frames in a row. */
42#define ESDR_LINK_FALLBACK_ZEROS 64U
43/* The bytes on the wire of a payload of this length at most: COBS' overhead and the zero. */
44#define ESDR_LINK_ENCODED_MAX(len) \
45 ((len) + ESDR_LINK_HDR_BYTES + ESDR_LINK_CRC_BYTES + \
46 ((len) + ESDR_LINK_HDR_BYTES + ESDR_LINK_CRC_BYTES) / 254U + 2U)
47
48enum esdr_link_type {
49 /*
50 * Host to device: struct esdr_link_hello, from here on it is frames. The device answers
51 * with one, whatever it was doing (a stream stops, as for any request).
52 */
53 ESDR_LINK_HELLO = 1,
54 /* Host to device: struct esdr_ser_hdr and its data, as the older transport's frames. */
55 ESDR_LINK_REQUEST = 2,
56 /* Device to host: its reply, the same. */
57 ESDR_LINK_REPLY = 3,
58 /* Device to host: struct esdr_block_hdr and its samples, no padding. */
59 ESDR_LINK_BLOCK = 4,
60 /* Device to host: text, from a console. */
61 ESDR_LINK_TEXT = 5,
62 /* Host to device: uint32_t, wire bytes of blocks the device may send on top of what it may yet. */
63 ESDR_LINK_CREDIT = 6,
64 /* Host to device: back to the older transport. */
65 ESDR_LINK_BYE = 7,
66};
67
68/*
69 * struct esdr_link_hello.flags. Host to device: the RP2040 reads the FPGA's fast line
70 * (COPROC_REQ_FPGA_FASTLINK), blocks may go that way. Device to host: they will.
71 */
72#define ESDR_LINK_HELLO_F_FAST 0x01U
73
74/*
75 * The MCH2022 badge's FPGA with the link bitstream (gateware/esdr_fpga/link.py): an SPI frame with
76 * this opcode holds link bytes, and bit 1 of its register CAPS says it is there.
77 */
78#define ESDR_LINK_FPGA_OP 0x20U
79#define ESDR_LINK_FPGA_CAPS 0x0002U
80/* The FPGA's FIFO, bytes: the host's credit stays below it. */
81#define ESDR_LINK_FPGA_FIFO 32768U
82
83/* ESDR_LINK_HELLO's payload. */
85 uint8_t version;
86 /* ESDR_LINK_HELLO_F_* */
87 uint8_t flags;
88 /* Device to host: the longest frame it takes, encoded. */
89 uint16_t max_frame;
90 /*
91 * Host to device: the wire bytes of receive blocks it may send until more credit comes; 0
92 * no limit. Device to host: what it took.
93 */
94 uint32_t credit;
95 /* Device to host: the highest baud rate of ESDR_REQ_SET_LINK_BAUD. */
96 uint32_t baud_max;
97} __attribute__((packed));
98
100 uint8_t type;
101 uint8_t flags;
102 uint16_t seq;
103 const uint8_t *payload;
104 size_t len;
105};
106
107/*
108 * CRC-32 (reflected 0xEDB88320, as zlib's) a nibble at a time: 16 words of table. A user with
109 * a faster one defines ESDR_LINK_CRC_ADD(crc, p, n) first, with this function's arguments.
110 */
111static inline uint32_t esdr_link_crc_add(uint32_t crc, const uint8_t *p, size_t n)
112{
113#if defined(ESDR_LINK_CRC_ADD)
114 return ESDR_LINK_CRC_ADD(crc, p, n);
115#else
116 static const uint32_t table[16] = {
117 0x00000000U, 0x1DB71064U, 0x3B6E20C8U, 0x26D930ACU,
118 0x76DC4190U, 0x6B6B51F4U, 0x4DB26158U, 0x5005713CU,
119 0xEDB88320U, 0xF00F9344U, 0xD6D6A3E8U, 0xCB61B38CU,
120 0x9B64C2B0U, 0x86D3D2D4U, 0xA00AE278U, 0xBDBDF21CU,
121 };
122
123 while (n-- > 0U) {
124 crc ^= *p++;
125 crc = (crc >> 4) ^ table[crc & 0xFU];
126 crc = (crc >> 4) ^ table[crc & 0xFU];
127 }
128 return crc;
129#endif
130}
131
132/* The encoder's state: where the current COBS block's code byte goes. */
134 uint8_t *out;
135 size_t at, code_at, max;
136 uint8_t code;
137 int overflow;
138};
139
140static inline void esdr_link_cobs_start(struct esdr_link_cobs *c, uint8_t *out, size_t max)
141{
142 c->out = out;
143 c->max = max;
144 c->at = 1U;
145 c->code_at = 0U;
146 c->code = 1U;
147 c->overflow = max < 2U;
148}
149
150static inline void esdr_link_cobs_put(struct esdr_link_cobs *c, const uint8_t *p, size_t n)
151{
152 /* In locals: this runs over every byte of a block, some 2 MB/s on the MCH2022's ESP32. */
153 uint8_t *const out = c->out;
154 const size_t max = c->max;
155 size_t at = c->at, code_at = c->code_at;
156 uint8_t code = c->code;
157
158 while (n-- > 0U) {
159 const uint8_t b = *p++;
160
161 if (at >= max) {
162 c->overflow = 1;
163 break;
164 }
165 if (b != 0U) {
166 out[at++] = b;
167 code++;
168 }
169 if (b == 0U || code == 0xFFU) {
170 out[code_at] = code;
171 code_at = at++;
172 code = 1U;
173 }
174 }
175 c->at = at;
176 c->code_at = code_at;
177 c->code = code;
178}
179
180/*
181 * One frame on the wire into @p out: the payload is @p a then @p b (either may be empty). The bytes
182 * written, the ending zero included; 0 if @p max is too small.
183 */
184static inline size_t esdr_link_encode(uint8_t *out, size_t max, uint8_t type, uint16_t seq,
185 const void *a, size_t a_len, const void *b, size_t b_len)
186{
187 const uint8_t hdr[ESDR_LINK_HDR_BYTES] = {type, 0U, (uint8_t)seq, (uint8_t)(seq >> 8)};
188 struct esdr_link_cobs c;
189 uint32_t crc = 0xFFFFFFFFU;
190 uint8_t tail[ESDR_LINK_CRC_BYTES];
191
192 esdr_link_cobs_start(&c, out, max);
193 crc = esdr_link_crc_add(crc, hdr, sizeof(hdr));
194 esdr_link_cobs_put(&c, hdr, sizeof(hdr));
195 if (a_len > 0U) {
196 crc = esdr_link_crc_add(crc, (const uint8_t *)a, a_len);
197 esdr_link_cobs_put(&c, (const uint8_t *)a, a_len);
198 }
199 if (b_len > 0U) {
200 crc = esdr_link_crc_add(crc, (const uint8_t *)b, b_len);
201 esdr_link_cobs_put(&c, (const uint8_t *)b, b_len);
202 }
203 crc = ~crc;
204 tail[0] = (uint8_t)crc;
205 tail[1] = (uint8_t)(crc >> 8);
206 tail[2] = (uint8_t)(crc >> 16);
207 tail[3] = (uint8_t)(crc >> 24);
208 esdr_link_cobs_put(&c, tail, sizeof(tail));
209 if (c.overflow || c.at >= c.max) {
210 return 0U;
211 }
212 c.out[c.code_at] = c.code;
213 c.out[c.at++] = 0U;
214 return c.at;
215}
216
217/*
218 * The frame in @p buf, @p n bytes without the ending zero, decoded in place. 0 and @p f filled (its
219 * payload inside @p buf), or -1: not COBS, too short, or the CRC does not match.
220 */
221static inline int esdr_link_decode(uint8_t *buf, size_t n, struct esdr_link_frame *f)
222{
223 size_t in = 0U, to = 0U;
224 uint32_t crc;
225
226 while (in < n) {
227 const uint8_t code = buf[in++];
228
229 if (code == 0U || in + code - 1U > n) {
230 return -1;
231 }
232 for (uint8_t k = 1U; k < code; k++) {
233 buf[to++] = buf[in++];
234 }
235 if (code != 0xFFU && in < n) {
236 buf[to++] = 0U;
237 }
238 }
239 if (to < ESDR_LINK_HDR_BYTES + ESDR_LINK_CRC_BYTES) {
240 return -1;
241 }
242 crc = ~esdr_link_crc_add(0xFFFFFFFFU, buf, to - ESDR_LINK_CRC_BYTES);
243 if ((uint8_t)crc != buf[to - 4U] || (uint8_t)(crc >> 8) != buf[to - 3U] ||
244 (uint8_t)(crc >> 16) != buf[to - 2U] || (uint8_t)(crc >> 24) != buf[to - 1U]) {
245 return -1;
246 }
247 f->type = buf[0];
248 f->flags = buf[1];
249 f->seq = (uint16_t)(buf[2] | ((uint16_t)buf[3] << 8));
250 f->payload = buf + ESDR_LINK_HDR_BYTES;
251 f->len = to - ESDR_LINK_HDR_BYTES - ESDR_LINK_CRC_BYTES;
252 return 0;
253}
254
255#ifdef __cplusplus
256}
257#endif
258
259#endif /* ESP_SDR_ESDR_LINK_H_ */