ra8-firmware 0.1.0
Bare-metal firmware for the Renesas RA8 family (RA8D2 / RA8P1)
Loading...
Searching...
No Matches
main.c
Go to the documentation of this file.
1
37
38#include <stdint.h>
39#include <string.h>
40
41#include "ra8_attributes.h"
42#include "ra8_boot_entry.h"
43#include "ra8_cgc.h"
44#include "ra8_check.h"
45#include "ra8_err.h"
46#include "ra8_fs.h"
47#include "ra8_io.h"
48#include "ra8_log.h"
49#include "ra8_port_constants.h"
50#include "ra8_port_utils.h"
51#include "ra8_sci.h"
52#include "ra8_time.h"
53#include "ra8_vfs_compress.h"
54
66
69 (ra8_port_pin_t)(((uint16_t)k_ra8_port_13 << (uint16_t)k_demo_pin_shift) | (uint16_t)k_ra8_pin_2);
72 (ra8_port_pin_t)(((uint16_t)k_ra8_port_13 << (uint16_t)k_demo_pin_shift) | (uint16_t)k_ra8_pin_3);
73
75static uint8_t s_disk[(size_t)k_demo_disk_blocks * (size_t)k_ra8_io_block_size_bytes];
76
86
90
92static uint8_t s_payload[(size_t)k_demo_payload];
93static uint8_t s_blob[(size_t)k_demo_blob_cap];
94static uint8_t s_restored[(size_t)k_demo_payload];
97
99static const char* const s_tag = "compress_demo";
100
119RA8_INTERNAL static void internal_demo_print(const char* msg)
120{
121 (void)ra8_io_stream_puts(&s_uart, msg);
122}
123
142{
143 uint32_t cpuclk0_hz = 0U;
144 uint32_t pclka_hz = 0U;
145 if ((ra8_cgc_init() != k_ra8_ok) ||
148 (ra8_time_init(cpuclk0_hz) != k_ra8_ok) ||
151 while (true) {
152 }
153 }
154 const ra8_sci_cfg_t sci_cfg = {.baud = (uint32_t)k_demo_uart_baud,
155 .data_bits = k_ra8_sci_data_8,
156 .parity = k_ra8_sci_parity_none,
157 .stop_bits = k_ra8_sci_stop_1,
158 .pclk_hz = pclka_hz};
159 if (ra8_sci_init((uint8_t)k_demo_uart_chan, &sci_cfg) != k_ra8_ok) {
160 while (true) {
161 }
162 }
163}
164
191{
192 RA8_CHECK_NULL_PTR(bd, s_tag, "bd");
193 RA8_CHECK_NULL_PTR(be, s_tag, "be");
194 RA8_CHECK_NULL_PTR(name, s_tag, "name");
196 ra8_fs_format_opts_t opts = {};
198 opts.label = "RAIO";
199 RA8_RETURN_ON_ERROR(ra8_fs_format(be, &opts), s_tag, "format");
200 ra8_fs_mount_t* mnt = nullptr;
201 RA8_RETURN_ON_ERROR(ra8_fs_mount(be, &mnt), s_tag, "mount");
202 RA8_RETURN_ON_ERROR(ra8_io_vfs_mount(name, mnt), s_tag, "vfs mount");
203 return k_ra8_ok;
204}
205
235RA8_INTERNAL static ra8_err_t internal_demo_roundtrip(const char* prefix, uint32_t* out_blob_len)
236{
237 RA8_CHECK_NULL_PTR(prefix, s_tag, "prefix");
238 RA8_CHECK_NULL_PTR(out_blob_len, s_tag, "out_blob_len");
239
240 char path[(size_t)k_ra8_io_vfs_name_max + sizeof("STORY.RBK") + 1U] = {};
241 const size_t prefix_limit = sizeof(path) - sizeof("STORY.RBK");
242 const size_t prefix_len = strnlen(prefix, prefix_limit);
243 if (prefix_len >= prefix_limit) {
245 }
246 (void)memcpy(path, prefix, prefix_len);
247 (void)memcpy(&path[prefix_len], "STORY.RBK", sizeof("STORY.RBK"));
248
249 uint32_t blob_len = 0;
251 s_payload,
252 (uint32_t)k_demo_payload,
253 s_scratch,
255 s_blob,
256 (uint32_t)k_demo_blob_cap,
257 &blob_len),
258 s_tag,
259 "write compressed");
260
261 uint32_t out_len = 0;
263 s_blob,
264 (uint32_t)k_demo_blob_cap,
266 (uint32_t)k_demo_payload,
267 &out_len),
268 s_tag,
269 "read compressed");
270 if (out_len != (uint32_t)k_demo_payload) {
272 }
273 if (memcmp(s_restored, s_payload, sizeof(s_payload)) != 0) {
275 }
276 *out_blob_len = blob_len;
277 return k_ra8_ok;
278}
279
303RA8_INTERNAL static ra8_err_t internal_demo_run(uint32_t* out_blob_len)
304{
305 RA8_CHECK_NULL_PTR(out_blob_len, s_tag, "out_blob_len");
306
307 for (uint32_t i = 0; i < (uint32_t)k_demo_payload; ++i) {
308 s_payload[i] = (uint8_t)((i * (uint32_t)k_demo_seed_mul) % (uint32_t)k_demo_pattern_mod);
309 }
310
311 /* Backend 1: RAM disk over an in-SRAM buffer, mounted as "ram". */
314 s_tag,
315 "ram blockdev init");
317 uint32_t ram_blob_len = 0;
318 RA8_RETURN_ON_ERROR(internal_demo_roundtrip("ram:/", &ram_blob_len), s_tag, "ram round-trip");
319
320 /* Backend 2: external SDRAM as a volatile ramdisk, mounted as "dr". */
323 s_tag,
324 "sdram blockdev init");
326 uint32_t dr_blob_len = 0;
327 RA8_RETURN_ON_ERROR(internal_demo_roundtrip("dr:/", &dr_blob_len), s_tag, "dr round-trip");
328
329 *out_blob_len = ram_blob_len;
330 return k_ra8_ok;
331}
332
338void main(void)
339{
340 ra8_log_init();
344 internal_demo_print("compress_demo: boot\r\n");
345
346 uint32_t blob_len = 0;
347 const ra8_err_t e = internal_demo_run(&blob_len);
348 if (e == k_ra8_ok) {
349 internal_demo_print("compress_demo: ram+dr 4096 -> ");
350 (void)ra8_io_stream_put_u32(&s_uart, blob_len);
351 internal_demo_print(" bytes -> 4096 round-trip PASS\r\n");
352 } else {
353 internal_demo_print("compress_demo: FAIL\r\n");
354 }
355 (void)ra8_sci_flush((uint8_t)k_demo_uart_chan);
356 while (true) {
357 }
358}
void main(void)
Secure fallback main entry point.
Definition main.c:37
static void internal_demo_print(const char *s)
Convenience wrapper to write a NUL-terminated string to SCI8.
Definition main.c:326
static void internal_demo_setup_or_halt(void)
Bring CGC + the J-Link OB VCOM console + on-board Ethernet + RSIP up.
Definition main.c:285
static ra8_err_t internal_demo_run(uint32_t *out_blob_len)
Bind both backends and run the transparent round-trip on each.
Definition main.c:303
static uint8_t s_disk[(size_t) k_demo_disk_blocks *(size_t) k_ra8_io_block_size_bytes]
256 KiB RAM-disk backing buffer (in SRAM .bss) for the "ram" mount.
Definition main.c:75
demo_const_t
Console + volume + payload knobs (no magic numbers).
Definition main.c:56
@ k_demo_payload
Source bytes (compressible).
Definition main.c:60
@ k_demo_disk_blocks
256 KiB per volume (FAT12).
Definition main.c:59
@ k_demo_uart_baud
Console baud.
Definition main.c:58
@ k_demo_uart_chan
SCI8 J-Link OB console.
Definition main.c:57
@ k_demo_pin_shift
Port byte position in ra8_port_pin_t.
Definition main.c:62
@ k_demo_pattern_mod
Small alphabet -> highly compressible.
Definition main.c:64
@ k_demo_seed_mul
Test-pattern multiplier.
Definition main.c:63
@ k_demo_blob_cap
Compressed-blob staging capacity.
Definition main.c:61
static uint8_t s_payload[(size_t) k_demo_payload]
Original payload, compressed-blob staging, and the inflated copy.
Definition main.c:92
static ra8_io_stream_uart_state_t s_ust
Definition main.c:89
static ra8_io_blockdev_ram_state_t s_bstate_dr
Definition main.c:82
static ra8_io_blockdev_t s_bd_ram
RAM block-device handle + its backend state.
Definition main.c:78
static const ra8_port_pin_t s_demo_rxd
SCI8 console RXD = PD03.
Definition main.c:71
static const ra8_port_pin_t s_demo_txd
SCI8 console TXD = PD02.
Definition main.c:68
static ra8_io_stream_t s_uart
UART output stream + its sink state.
Definition main.c:88
static ra8_fs_backend_t s_be_dr
Definition main.c:85
static ra8_io_blockdev_ram_state_t s_bstate_ram
Definition main.c:79
static ra8_err_t internal_demo_mount(ra8_io_blockdev_t *bd, ra8_fs_backend_t *be, const char *name)
Format + mount a fresh FAT12 volume and register it in the VFS.
Definition main.c:190
static ra8_io_blockdev_t s_bd_dr
SDRAM block-device handle + its (reused RAM-backend) state.
Definition main.c:81
static uint8_t s_blob[(size_t) k_demo_blob_cap]
Definition main.c:93
static ra8_fs_backend_t s_be_ram
ra8_fs backends bridged onto each block device.
Definition main.c:84
static uint8_t s_scratch[(size_t) k_ra8_compress_scratch_bytes]
Compressor scratch (one miniz tdefl_compressor) in SRAM.
Definition main.c:96
static uint8_t s_restored[(size_t) k_demo_payload]
Definition main.c:94
static ra8_err_t internal_demo_roundtrip(void)
Erase a block, program a pattern into it, read it back, and compare.
Definition main.c:187
static const char * s_tag
Logging / check tag.
Definition ra8_app.c:17
Annotation-attribute framework macros for ra8-firmware.
#define RA8_INTERNAL
Marker that a function is intended to be static (file-local).
Boot entry points shared between a vector table and its startup code.
High-level Clock Generation Circuit driver.
ra8_err_t ra8_cgc_get_clock_hz(ra8_clock_id_t id, uint32_t *out_hz)
Query the current frequency of a clock-tree domain.
Definition ra8_cgc.c:132
@ k_ra8_clock_id_cpuclk0
Cortex-M85 CPUCLK0.
Definition ra8_cgc.h:70
@ k_ra8_clock_id_pclka
PCLKA.
Definition ra8_cgc.h:73
ra8_err_t ra8_cgc_init(void)
Configure the clock tree to a safe default.
Definition ra8_cgc.c:727
Validation and Error-Checking Macros for ra8-firmware.
#define RA8_RETURN_ON_ERROR(err, tag, message)
Early return on error, propagating the code upward.
Definition ra8_check.h:184
#define RA8_CHECK_NULL_PTR(ptr, tag, message)
Reject nullptr pointer, returning k_ra8_err_null_ptr.
Definition ra8_check.h:243
@ k_ra8_compress_scratch_bytes
Minimum compress scratch size (one miniz tdefl_compressor).
Error Code Definitions for ra8-firmware.
@ k_ra8_err_checksum_mismatch
Stored / transmitted checksum does not match computed value.
Definition ra8_err.h:465
@ k_ra8_ok
Success – operation completed with all postconditions satisfied.
Definition ra8_err.h:119
@ k_ra8_err_invalid_size
Invalid size parameter (too large, too small, or misaligned).
Definition ra8_err.h:167
ra8_err_codes_t ra8_err_t
Canonical error-return type used by every ra8-firmware API.
Definition ra8_err.h:546
int memcmp(const void *a, const void *b, size_t n)
Compare bytes in two memory areas.
void * memcpy(void *dst, const void *src, size_t n)
Copy memory area between non-overlapping regions.
size_t strnlen(const char *s, size_t maxlen)
Calculate bounded string length.
Minimal FAT12/FAT16/FAT32 filesystem adapter (read + write).
ra8_err_t ra8_fs_format(const ra8_fs_backend_t *backend, const ra8_fs_format_opts_t *opts)
Format a block device as a fresh, empty FAT12/FAT16/FAT32/exFAT volume.
ra8_err_t ra8_fs_mount(const ra8_fs_backend_t *backend, ra8_fs_mount_t **out_handle)
Mount a FAT volume from a block-device backend, auto-selecting the first partition.
@ k_ra8_fs_type_fat12
count_of_clusters < 4085.
@ k_ra8_psel_sci_async
00100b: SCI async serial (UART).
ra8_io – unified peripheral-agnostic I/O fabric (umbrella header).
@ k_ra8_io_block_size_bytes
Bytes per logical block (the only size).
ra8_err_t ra8_io_blockdev_as_fs_backend(const ra8_io_blockdev_t *bd, ra8_fs_backend_t *out)
Expose a bound block device as an ra8_fs_backend_t for ra8_fs.
ra8_err_t ra8_io_blockdev_ram_init(ra8_io_blockdev_t *bd, ra8_io_blockdev_ram_state_t *state, uint8_t *storage, uint32_t block_count, bool read_only)
Bind a RAM block-device backend into a caller-owned handle.
ra8_err_t ra8_io_blockdev_sdram_init(ra8_io_blockdev_t *bd, ra8_io_blockdev_ram_state_t *state, uint32_t block_count)
Bring up the SDRAM controller and bind it as a volatile ramdisk.
ra8_err_t ra8_io_log_attach(ra8_io_stream_t *s)
Route ra8_log output to a bound ra8_io stream.
Definition ra8_io_log.c:61
ra8_err_t ra8_io_stream_puts(ra8_io_stream_t *s, const char *str)
Write a NUL-terminated string (without the NUL) to the bound sink.
ra8_err_t ra8_io_stream_put_u32(ra8_io_stream_t *s, uint32_t value)
Write value as unsigned decimal ASCII (no leading zeros).
ra8_err_t ra8_io_stream_uart_init(ra8_io_stream_t *s, ra8_io_stream_uart_state_t *state, uint8_t channel)
Bind a UART stream sink into a caller-owned stream handle.
ra8_err_t ra8_io_vfs_mount(const char *name, ra8_fs_mount_t *mount)
Register a mounted volume under a name.
Definition ra8_io_vfs.c:418
@ k_ra8_io_vfs_name_max
Mount name length incl NUL.
Definition ra8_io_vfs.h:58
Lightweight Logging Interface for ra8-firmware.
void ra8_log_init(void)
Initialise the logging backend.
Definition ra8_log.c:379
Typed Port / Pin Constants for the RA8D2 IOPORT Module.
ra8_port_pin_t
Packed (port << 8) | pin pin identifier.
@ k_ra8_port_13
RA8 port 13.
@ k_ra8_pin_2
RA8 pin 2.
@ k_ra8_pin_3
RA8 pin 3.
High-level GPIO helpers on top of the PORT + PFS register layer.
ra8_err_t ra8_pfs_route_peripheral(ra8_port_pin_t pin, ra8_psel_t psel, const char *owner)
Route a pin to a non-IRQ peripheral function via PFS.PSEL.
Definition gpio.c:238
Full-featured Serial Communications Interface driver.
@ k_ra8_sci_stop_1
RA8 SCI stop 1.
Definition ra8_sci.h:81
@ k_ra8_sci_parity_none
RA8 SCI parity none.
Definition ra8_sci.h:71
ra8_err_t ra8_sci_init(uint8_t channel, const ra8_sci_cfg_t *cfg)
Initialise an SCI channel using the descriptor.
Definition ra8_sci.c:410
@ k_ra8_sci_data_8
RA8 SCI data 8.
Definition ra8_sci.h:91
ra8_err_t ra8_sci_flush(uint8_t channel)
Block until the channel's transmit shift register is empty.
Definition ra8_sci.c:543
SysTick-based tick counter, delay and timestamp helpers.
ra8_err_t ra8_time_init(uint32_t cpu_hz)
Initialise SysTick for a 1 kHz tick interrupt.
Definition ra8_time.c:59
Transparent VFS-level whole-file compression (compress-on-write,decompress-on-read) over the ra8_io f...
ra8_err_t ra8_vfs_compress_read(const char *path, uint8_t *blob_buf, uint32_t blob_cap, uint8_t *out, uint32_t out_cap, uint32_t *out_len)
Read a whole compressed file through the VFS and decompress it.
ra8_err_t ra8_vfs_compress_write(const char *path, const uint8_t *src, uint32_t src_len, void *scratch, uint32_t scratch_len, uint8_t *blob_buf, uint32_t blob_cap, uint32_t *out_blob_len)
Compress a payload and store it as a whole file through the VFS.
Block-device interface that ra8_fs runs on top of.
Tunables for ra8_fs_format() (the on-disk geometry of a fresh volume).
ra8_fs_type_t type
FAT variant to lay down (12/16/32).
const char * label
0..11 char volume label, or NULL.
Cached parse of one mounted FAT volume.
Caller-owned private state for a RAM block device.
Caller-allocated block-device handle binding a backend to its context.
Caller-allocated byte-stream handle binding a sink to its context.
Caller-owned private state for a UART stream sink.
Configuration descriptor for ra8_sci_init.
Definition ra8_sci.h:103