ra8-firmware 0.1.0
Bare-metal firmware for the Renesas RA8 family (RA8D2 / RA8P1)
Loading...
Searching...
No Matches
ra8_c6link_frame.c
Go to the documentation of this file.
1
40
41#include <stdint.h>
42#include <string.h>
43
44#include "ra8_attributes.h"
45#include "ra8_c6link.h"
46#include "ra8_c6link_internal.h"
47
76
77/* The public header restates these so consumers need no esp-hosted include
78 path. If upstream ever changes either, the build stops here rather than
79 mis-framing on the wire. */
80static_assert((uint16_t)k_ra8_c6link_header_bytes == (uint16_t)sizeof(struct esp_payload_header),
81 "k_ra8_c6link_header_bytes must equal sizeof(struct esp_payload_header)");
82static_assert((uint16_t)k_ra8_c6link_frame_bytes == (uint16_t)ESP_TRANSPORT_SPI_MAX_BUF_SIZE,
83 "k_ra8_c6link_frame_bytes must equal ESP_TRANSPORT_SPI_MAX_BUF_SIZE");
84static_assert((uint16_t)k_ra8_c6link_max_payload ==
86 "k_ra8_c6link_max_payload must be the frame size less the header");
87
102RA8_INTERNAL static void internal_c6link_frame_clear(uint8_t* frame, uint16_t from)
103{
104 for (uint16_t i = from; i < (uint16_t)k_ra8_c6link_frame_bytes; i++) {
105 frame[i] = 0U;
106 }
107}
108
127RA8_INTERNAL static uint16_t internal_c6link_frame_sum(uint8_t* frame, uint16_t span)
128{
129 uint16_t sum = compute_checksum(frame, span);
130 for (uint8_t i = 0U; i < (uint8_t)k_ra8_c6link_hdr_csum_sz; i++) {
131 sum = (uint16_t)(sum - frame[(uint8_t)k_ra8_c6link_hdr_csum_at + i]);
132 }
133 return sum;
134}
135
137{
138 if (tx == nullptr) {
139 return;
140 }
141 struct esp_payload_header hdr = {};
142 hdr.if_type = (uint8_t)((uint8_t)ESP_MAX_IF & (uint8_t)k_ra8_c6link_hdr_nibble);
143
145 (void)memcpy(tx, &hdr, sizeof hdr);
146}
147
148RA8_PRIV void priv_c6link_frame_seal(uint8_t* tx, uint8_t if_type, uint8_t if_num, uint16_t len)
149{
150 if ((tx == nullptr) || (len > (uint16_t)k_ra8_c6link_max_payload)) {
151 return;
152 }
153 const uint16_t span = (uint16_t)((uint16_t)k_ra8_c6link_header_bytes + len);
154
155 struct esp_payload_header hdr = {};
156 hdr.if_type = (uint8_t)(if_type & (uint8_t)k_ra8_c6link_hdr_nibble);
157 hdr.if_num = (uint8_t)(if_num & (uint8_t)k_ra8_c6link_hdr_nibble);
158 hdr.len = len;
159 hdr.offset = (uint16_t)k_ra8_c6link_header_bytes;
160 hdr.seq_num = (uint16_t)k_ra8_c6link_hdr_seq;
161
163 (void)memcpy(tx, &hdr, sizeof hdr);
164 hdr.checksum = compute_checksum(tx, span);
165 (void)memcpy(tx, &hdr, sizeof hdr);
166}
167
185RA8_INTERNAL static uint8_t
186internal_c6link_caps_tlv(uint8_t* out, uint8_t at, uint8_t tag, uint8_t value)
187{
188 out[at] = tag;
189 out[at + 1U] = (uint8_t)k_ra8_c6link_caps_value_len;
190 out[at + 2U] = value;
191 return (uint8_t)(at + (uint8_t)k_ra8_c6link_caps_stride);
192}
193
194RA8_PRIV uint8_t priv_c6link_caps(uint8_t* out, uint8_t cap)
195{
196 if ((out == nullptr) || (cap < (uint8_t)k_ra8_c6link_caps_bytes)) {
197 return 0U;
198 }
199 const uint8_t tlv_bytes =
200 (uint8_t)((uint8_t)k_ra8_c6link_caps_tags * (uint8_t)k_ra8_c6link_caps_stride);
201
202 out[k_ra8_c6link_caps_type] = (uint8_t)ESP_PRIV_EVENT_INIT;
203 out[k_ra8_c6link_caps_len] = tlv_bytes;
204
205 uint8_t at = (uint8_t)k_ra8_c6link_caps_hdr;
206 at =
207 internal_c6link_caps_tlv(out, at, (uint8_t)HOST_CAPABILITIES, (uint8_t)k_ra8_c6link_caps_host);
209 at,
210 (uint8_t)RCVD_ESP_FIRMWARE_CHIP_ID,
211 (uint8_t)k_ra8_c6link_caps_chip);
213 at,
214 (uint8_t)SLV_CONFIG_TEST_RAW_TP,
215 (uint8_t)k_ra8_c6link_caps_raw_tp);
217 at,
218 (uint8_t)SLV_CONFIG_THROTTLE_HIGH_THRESHOLD,
221 at,
222 (uint8_t)SLV_CONFIG_THROTTLE_LOW_THRESHOLD,
224 return at;
225}
226
229{
230 if ((rx == nullptr) || (view == nullptr)) {
232 }
233 struct esp_payload_header hdr = {};
234 (void)memcpy(&hdr, rx, sizeof hdr);
235
236 if (hdr.len == 0U) {
238 }
239 if ((hdr.offset != (uint16_t)k_ra8_c6link_header_bytes) ||
240 (hdr.len > (uint16_t)k_ra8_c6link_max_payload)) {
242 }
243 if (internal_c6link_frame_sum(rx, (uint16_t)(hdr.offset + hdr.len)) != hdr.checksum) {
245 }
246
247 view->offset = hdr.offset;
248 view->len = hdr.len;
249 view->if_type = (uint8_t)hdr.if_type;
250 view->if_num = (uint8_t)hdr.if_num;
252}
Annotation-attribute framework macros for ra8-firmware.
#define RA8_PRIV
Module-private helper: shared across TUs but only inside one library.
#define RA8_INTERNAL
Marker that a function is intended to be static (file-local).
void * memcpy(void *dst, const void *src, size_t n)
Copy memory area between non-overlapping regions.