ra8-firmware 0.1.0
Bare-metal firmware for the Renesas RA8 family (RA8D2 / RA8P1)
Loading...
Searching...
No Matches
c6_frame.c
Go to the documentation of this file.
1
19
20#include <stdint.h>
21
22#include "c6_probe.h"
23#include "c6_proto.h"
24
46static uint16_t internal_le16(const uint8_t* buf, uint8_t off_lo)
47{
48 if (buf == nullptr) {
49 return 0U;
50 }
51 return (uint16_t)((uint16_t)buf[off_lo] |
52 (uint16_t)((uint16_t)buf[off_lo + 1U] << (uint16_t)k_c6_shift_byte));
53}
54
55uint16_t c6_probe_checksum(const uint8_t* buf, uint16_t count)
56{
57 if (buf == nullptr) {
58 return 0U;
59 }
60 const uint16_t capped =
61 (count > (uint16_t)k_c6_proto_buf_size) ? (uint16_t)k_c6_proto_buf_size : count;
62 uint16_t sum = 0U;
63 for (uint16_t i = 0U; i < capped; i++) {
64 if (i == (uint16_t)k_c6_hdr_off_csum_lo || i == (uint16_t)k_c6_hdr_off_csum_hi) {
65 continue;
66 }
67 sum = (uint16_t)(sum + (uint16_t)buf[i]);
68 }
69 return sum;
70}
71
72void c6_probe_decode_header(const uint8_t* buf, c6_hdr_t* out)
73{
74 if (buf == nullptr || out == nullptr) {
75 return;
76 }
77 const uint8_t iface = buf[k_c6_hdr_off_iface];
78 out->if_type = (uint8_t)(iface & (uint8_t)k_c6_mask_nibble);
79 out->if_num = (uint8_t)((iface >> (uint8_t)k_c6_shift_nibble) & (uint8_t)k_c6_mask_nibble);
80 out->flags = buf[k_c6_hdr_off_flags];
81 out->len = internal_le16(buf, (uint8_t)k_c6_hdr_off_len_lo);
82 out->offset = internal_le16(buf, (uint8_t)k_c6_hdr_off_off_lo);
83 out->checksum = internal_le16(buf, (uint8_t)k_c6_hdr_off_csum_lo);
84 out->seq_num = internal_le16(buf, (uint8_t)k_c6_hdr_off_seq_lo);
85 out->throttle = (uint8_t)(buf[k_c6_hdr_off_throttle] & (uint8_t)k_c6_mask_throttle);
87 out->computed = 0U;
88 if (out->offset == (uint16_t)k_c6_proto_hdr_size &&
89 out->len <= (uint16_t)k_c6_proto_payload_max) {
90 out->computed = c6_probe_checksum(buf, (uint16_t)(out->offset + out->len));
91 }
92}
93
96{
97 if (h == nullptr) {
98 return k_c6_frame_garbage;
99 }
100 if (h->if_type == (uint8_t)k_c6_if_max && h->if_num == (uint8_t)k_c6_dummy_if_num &&
101 h->len == 0U) {
102 return k_c6_frame_idle;
103 }
104 if (h->offset != (uint16_t)k_c6_proto_hdr_size) {
105 return k_c6_frame_garbage;
106 }
107 if (h->len == 0U || h->len > (uint16_t)k_c6_proto_payload_max) {
108 return k_c6_frame_garbage;
109 }
110 if (h->if_type >= (uint8_t)k_c6_if_max) {
111 return k_c6_frame_garbage;
112 }
114}
115
117{
118 if (h == nullptr) {
119 return;
120 }
121 c6_probe_puts(" hdr if_type=");
122 c6_probe_put_u32((uint32_t)h->if_type);
123 c6_probe_puts(" if_num=");
124 c6_probe_put_u32((uint32_t)h->if_num);
125 c6_probe_puts(" flags=0x");
126 c6_probe_put_hex((uint32_t)h->flags, (uint8_t)k_c6_fmt_hex_byte);
127 c6_probe_puts(" len=");
128 c6_probe_put_u32((uint32_t)h->len);
129 c6_probe_puts(" offset=");
130 c6_probe_put_u32((uint32_t)h->offset);
131 c6_probe_puts(" csum=0x");
132 c6_probe_put_hex((uint32_t)h->checksum, (uint8_t)k_c6_fmt_hex_word);
133 c6_probe_puts(" calc=0x");
134 c6_probe_put_hex((uint32_t)h->computed, (uint8_t)k_c6_fmt_hex_word);
135 c6_probe_puts(" seq=");
136 c6_probe_put_u32((uint32_t)h->seq_num);
137 c6_probe_puts(" pkt=0x");
138 c6_probe_put_hex((uint32_t)h->pkt_type, (uint8_t)k_c6_fmt_hex_byte);
139 c6_probe_puts("\r\n");
140}
141
142void c6_probe_dump_payload(const uint8_t* buf, uint16_t len)
143{
144 if (buf == nullptr || len == 0U) {
145 return;
146 }
147 const uint16_t shown =
148 (len > (uint16_t)k_c6_probe_dump_bytes) ? (uint16_t)k_c6_probe_dump_bytes : len;
149 c6_probe_puts(" payload");
150 for (uint16_t i = 0U; i < shown; i++) {
151 c6_probe_puts(" ");
152 c6_probe_put_hex((uint32_t)buf[(uint16_t)k_c6_proto_hdr_size + i], (uint8_t)k_c6_fmt_hex_byte);
153 }
154 c6_probe_puts("\r\n");
155}
void c6_probe_decode_header(const uint8_t *buf, c6_hdr_t *out)
Unpack the twelve-byte esp-hosted payload header.
Definition c6_frame.c:72
void c6_probe_print_header(const c6_hdr_t *h)
Print the decoded header fields on one console line.
Definition c6_frame.c:116
void c6_probe_dump_payload(const uint8_t *buf, uint16_t len)
Hex-dump the leading payload bytes of a received frame.
Definition c6_frame.c:142
static uint16_t internal_le16(const uint8_t *buf, uint8_t off_lo)
Read a little-endian 16-bit field out of a byte buffer.
Definition c6_frame.c:46
uint16_t c6_probe_checksum(const uint8_t *buf, uint16_t count)
Recompute the esp-hosted checksum over a received frame.
Definition c6_frame.c:55
c6_frame_kind_t c6_probe_classify(const c6_hdr_t *h)
Implementation of c6_probe_classify() – filler check first.
Definition c6_frame.c:95
Shared contract for the EK-RA8D2 <-> ESP32-C6 esp-hosted SPI probe.
@ k_c6_fmt_hex_byte
Nibbles printed for one byte.
Definition c6_probe.h:142
@ k_c6_fmt_hex_word
Nibbles printed for a 16-bit word.
Definition c6_probe.h:143
void c6_probe_puts(const char *text)
Emit a bounded, NUL-terminated ASCII literal on the console.
Definition c6_console.c:25
void c6_probe_put_u32(uint32_t value)
Emit an unsigned integer in decimal.
Definition c6_console.c:38
@ k_c6_probe_dump_bytes
Payload bytes hex-dumped per frame.
Definition c6_probe.h:110
void c6_probe_put_hex(uint32_t value, uint8_t digits)
Emit a value as fixed-width lowercase hexadecimal.
Definition c6_console.c:56
The esp-hosted SPI full-duplex wire format, hand-decoded.
@ k_c6_shift_nibble
if_num sits in the upper nibble.
Definition c6_proto.h:194
@ k_c6_mask_throttle
throttle_cmd occupies bits 1:0.
Definition c6_proto.h:197
@ k_c6_mask_nibble
Four-bit field mask.
Definition c6_proto.h:195
@ k_c6_shift_byte
Little-endian high-byte shift.
Definition c6_proto.h:196
@ k_c6_hdr_off_off_lo
Payload offset, low byte.
Definition c6_proto.h:100
@ k_c6_hdr_off_pkttype
reserved3 / hci_ or priv_pkt_type.
Definition c6_proto.h:107
@ k_c6_hdr_off_throttle
throttle_cmd[1:0] | reserved[7:2].
Definition c6_proto.h:106
@ k_c6_hdr_off_seq_lo
Sequence number, low byte.
Definition c6_proto.h:104
@ k_c6_hdr_off_iface
if_type[3:0] | if_num[7:4].
Definition c6_proto.h:96
@ k_c6_hdr_off_flags
Fragment / wake / power-save bits.
Definition c6_proto.h:97
@ k_c6_hdr_off_csum_lo
Checksum, low byte.
Definition c6_proto.h:102
@ k_c6_hdr_off_len_lo
Payload length, low byte.
Definition c6_proto.h:98
@ k_c6_hdr_off_csum_hi
Checksum, high byte.
Definition c6_proto.h:103
@ k_c6_dummy_if_num
if_num the C6 stamps into filler frames.
Definition c6_proto.h:174
@ k_c6_proto_payload_max
Largest payload the header may claim.
Definition c6_proto.h:70
@ k_c6_proto_buf_size
Bytes clocked per transaction.
Definition c6_proto.h:68
@ k_c6_proto_hdr_size
sizeof(struct esp_payload_header).
Definition c6_proto.h:69
c6_frame_kind_t
Classification of one received 1600-byte transaction.
Definition c6_proto.h:224
@ k_c6_frame_data
Real frame, checksum verified.
Definition c6_proto.h:227
@ k_c6_frame_bad_csum
Header shape sane, checksum mismatched.
Definition c6_proto.h:228
@ k_c6_frame_garbage
No recognisable esp-hosted structure.
Definition c6_proto.h:225
@ k_c6_frame_idle
The C6's idle filler frame.
Definition c6_proto.h:226
@ k_c6_if_max
Idle filler marker.
Definition c6_proto.h:139
Decoded esp-hosted payload header plus the locally recomputed sum.
Definition c6_proto.h:246
uint16_t computed
Checksum recomputed over the receive buffer.
Definition c6_proto.h:256
uint16_t seq_num
Sender sequence number.
Definition c6_proto.h:253
uint16_t checksum
Checksum carried on the wire.
Definition c6_proto.h:252
uint16_t offset
Payload offset; always the header size.
Definition c6_proto.h:251
uint16_t len
Payload length claimed by the sender.
Definition c6_proto.h:250
uint8_t pkt_type
HCI / private packet sub-type.
Definition c6_proto.h:255
uint8_t if_num
Interface instance number.
Definition c6_proto.h:248
uint8_t flags
Fragment / wake / power-save bits.
Definition c6_proto.h:249
uint8_t throttle
Wi-Fi transmit throttle command.
Definition c6_proto.h:254
uint8_t if_type
Interface identifier (c6_if_type_t).
Definition c6_proto.h:247