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
40
41#include <stdint.h>
42#include <string.h>
43
44#include "ra8_board_ek_ra8d2.h"
45#include "ra8_boot_entry.h"
46#include "ra8_cgc.h"
47#include "ra8_check.h"
48#include "ra8_err.h"
49#include "ra8_fs.h"
50#include "ra8_io.h"
51#include "ra8_isr.h"
52#include "ra8_log.h"
53#include "ra8_sdcard.h"
54#include "ra8_sdhi.h"
55#include "ra8_time.h"
56
57/* =============================================================================
58 * Tunables
59 * =============================================================================
60 */
61
76
77/* =============================================================================
78 * Static message strings (ASCII-only per project policy)
79 * =============================================================================
80 */
81
82static const uint8_t k_msg_boot[] = "ra8_io_sdhi_demo: boot\r\n";
83static const uint8_t k_msg_card_ok[] = "ra8_io_sdhi_demo: card ready\r\n";
84static const uint8_t k_msg_init_fail[] = "ra8_io_sdhi_demo: FAIL init\r\n";
85static const uint8_t k_msg_pass[] = "ra8_io_sdhi_demo: sd:/LOGS/A.TXT 512 bytes PASS\r\n";
86static const uint8_t k_msg_fail[] = "ra8_io_sdhi_demo: FAIL\r\n";
87
89static const char k_mount_name[] = "sd";
90static const char k_dir_path[] = "sd:/LOGS";
91static const char k_file_path[] = "sd:/LOGS/A.TXT";
92
94static const char* const s_tag = "ra8_io_sdhi_demo";
95
96/* =============================================================================
97 * UART output helpers
98 * =============================================================================
99 */
100
121static void sdhi_demo_print(const uint8_t* msg, uint32_t len)
122{
123 (void)ra8_board_uart_console_write(msg, (size_t)len);
124}
125
127#define SDHI_DEMO_PUTS(lit) sdhi_demo_print((lit), (uint32_t)sizeof(lit) - 1U)
128
146static void sdhi_demo_panic_halt(void)
147{
148 while (true) {
149 __asm__ volatile("wfi");
150 }
151}
152
153/* =============================================================================
154 * Hardware bring-up
155 * =============================================================================
156 */
157
177static void sdhi_demo_setup_or_halt(void)
178{
179 uint32_t cpuclk0_hz = 0U;
180 if (ra8_cgc_init() != k_ra8_ok) {
182 }
185 }
186 if (ra8_time_init(cpuclk0_hz) != k_ra8_ok) {
188 }
191 }
194 }
195}
196
215{
216 const ra8_sdcard_cfg_t cfg = {.instance = (uint8_t)k_sdhi_demo_instance};
217 if (ra8_sdcard_init(&cfg) != k_ra8_ok) {
220 }
222}
223
224/* =============================================================================
225 * Payload + ra8_io round-trip
226 * =============================================================================
227 */
228
232
236
254static void sdhi_demo_fill_payload(void)
255{
256 uint32_t state = (uint32_t)k_sdhi_demo_prng_seed;
257 for (uint32_t i = 0U; i < (uint32_t)k_sdhi_demo_payload_bytes; i++) {
258 state = (state * (uint32_t)k_sdhi_demo_prng_mul) + (uint32_t)k_sdhi_demo_prng_add;
260 }
261}
262
286[[nodiscard]] static ra8_err_t sdhi_demo_mount_via_io(void)
287{
291 ra8_fs_format_opts_t opts = {};
293 opts.label = "RAIOSD";
294 RA8_RETURN_ON_ERROR(ra8_fs_format(&s_be, &opts), s_tag, "format");
295 ra8_fs_mount_t* mnt = nullptr;
296 RA8_RETURN_ON_ERROR(ra8_fs_mount(&s_be, &mnt), s_tag, "mount");
298 return k_ra8_ok;
299}
300
322[[nodiscard]] static ra8_err_t sdhi_demo_write_payload(const char* path)
323{
324 ra8_fs_file_t* wf = nullptr;
327 if (err != k_ra8_ok) {
328 (void)ra8_fs_close(wf);
329 return err;
330 }
331 return ra8_fs_close(wf);
332}
333
357[[nodiscard]] static ra8_err_t sdhi_demo_read_and_verify(const char* path)
358{
359 ra8_fs_file_t* rf = nullptr;
361 memset(s_readback, 0, sizeof(s_readback));
362 uint32_t got = 0U;
363 ra8_err_t err = ra8_fs_read(rf, s_readback, (uint32_t)k_sdhi_demo_payload_bytes, &got);
364 (void)ra8_fs_close(rf);
365 if (err != k_ra8_ok) {
366 return err;
367 }
368 if (got != (uint32_t)k_sdhi_demo_payload_bytes) {
370 }
373 }
374 return k_ra8_ok;
375}
376
404
405/* =============================================================================
406 * Main
407 * =============================================================================
408 */
409
427void main(void)
428{
431 ra8_log_init();
433
436
437 const ra8_err_t r = sdhi_demo_roundtrip();
438 if (r != k_ra8_ok) {
441 }
444
445 while (true) {
446 __asm__ volatile("wfi");
447 }
448}
void main(void)
Secure fallback main entry point.
Definition main.c:37
static uint8_t s_payload[(size_t) k_demo_payload]
Original payload, compressed-blob staging, and the inflated copy.
Definition main.c:92
static const uint8_t k_msg_boot[]
Definition main.c:140
static const uint8_t k_msg_fail[]
Definition main.c:63
static ra8_fs_backend_t s_be
ra8_fs backend bridged onto the cached block device.
Definition main.c:71
static ra8_io_blockdev_t s_bd
Block-device handle + its RAM backend state.
Definition main.c:52
static const uint8_t k_msg_pass[]
Definition main.c:200
static const uint8_t k_msg_card_ok[]
Definition main.c:190
static ra8_err_t sdhi_demo_mount_via_io(void)
Bind the native-SDHI block device, bridge it to ra8_fs, format + mount FAT16, and register it in the ...
Definition main.c:286
static const char k_file_path[]
Definition main.c:91
sdhi_demo_config_t
Compile-time settings for the ra8_io native-SDHI backend demo.
Definition main.c:66
@ k_sdhi_demo_prng_add
Numerical Recipes LCG increment.
Definition main.c:72
@ k_sdhi_demo_instance
SDHI0 drives the micro-SD bus.
Definition main.c:68
@ k_sdhi_demo_prng_seed
Deterministic payload seed.
Definition main.c:70
@ k_sdhi_demo_payload_bytes
One-sector deterministic test payload.
Definition main.c:69
@ k_sdhi_demo_byte_mask
Low-byte mask.
Definition main.c:74
@ k_sdhi_demo_prng_mul
Numerical Recipes LCG multiplier.
Definition main.c:71
@ k_sdhi_demo_prng_byte_shift
Bit shift selecting the PRNG byte.
Definition main.c:73
@ k_sdhi_demo_uart_baud
J-Link OB CDC console baud.
Definition main.c:67
#define SDHI_DEMO_PUTS(lit)
Emit a NUL-terminated literal (length via sizeof at the call site).
Definition main.c:127
static const char k_mount_name[]
VFS mount name and target path for the round-trip file.
Definition main.c:89
static ra8_err_t sdhi_demo_write_payload(const char *path)
Write s_payload to the VFS path through ra8_io_vfs_open + ra8_fs_write.
Definition main.c:322
static void sdhi_demo_setup_or_halt(void)
Bring up CGC + SysTick + the board console + SDHI bus pins; panic on fail.
Definition main.c:177
static const uint8_t k_msg_init_fail[]
Definition main.c:84
static void sdhi_demo_panic_halt(void)
Halt forever in WFI – panic stop on irrecoverable failure.
Definition main.c:146
static ra8_err_t sdhi_demo_read_and_verify(const char *path)
Read the VFS file back and byte-compare against s_payload.
Definition main.c:357
static const char k_dir_path[]
Definition main.c:90
static void sdhi_demo_print(const uint8_t *msg, uint32_t len)
Write a byte run on the J-Link OB VCOM console.
Definition main.c:121
static ra8_err_t sdhi_demo_roundtrip(void)
Run the full ra8_io round-trip: mount, mkdir, write, read-back, verify.
Definition main.c:396
static void sdhi_demo_init_card_or_halt(void)
Init the SD card over native SDHI, panic-halt on failure.
Definition main.c:214
static void sdhi_demo_fill_payload(void)
Fill the payload buffer with a deterministic LCG byte sequence.
Definition main.c:254
static const char * s_tag
Logging / check tag.
Definition ra8_app.c:17
Board-support layer for the Renesas EK-RA8D2 v1 evaluation kit.
ra8_err_t ra8_board_sdhi_pins_init(void)
Route the eight SDHI0 bus pins to the SDHI peripheral function.
ra8_err_t ra8_board_uart_console_write(const uint8_t *data, size_t len)
Polled blocking write to the J-Link OB VCOM console.
ra8_err_t ra8_board_uart_console_init(uint32_t baud)
Configure SCI8 + PD02/PD03 as the debug-console UART.
ra8_err_t ra8_board_uart_console_flush(void)
Block until every byte queued on the J-Link OB VCOM console has finished clocking out on the wire.
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
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
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
void * memset(void *dst, int value, size_t n)
Fill memory with a constant byte value.
int memcmp(const void *a, const void *b, size_t n)
Compare bytes in two memory areas.
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_write(ra8_fs_file_t *file, const uint8_t *buf, uint32_t len)
Write len bytes; allocate new clusters from FAT free-space scan as needed.
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.
ra8_err_t ra8_fs_read(ra8_fs_file_t *file, uint8_t *buf, uint32_t max_len, uint32_t *got_len)
Read up to max_len bytes; advance the cluster chain on cluster crossings.
ra8_err_t ra8_fs_close(ra8_fs_file_t *file)
Close an open file, stamping its final modification time.
@ k_ra8_fs_type_fat16
4085 <= count_of_clusters < 65525.
@ k_ra8_fs_mode_read
Read-only, must exist.
@ k_ra8_fs_mode_write
Truncate (or create) for writing.
ra8_io – unified peripheral-agnostic I/O fabric (umbrella header).
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_sdhi_init(ra8_io_blockdev_t *bd)
Bind the native-SDHI SD-card backend into a caller-owned handle.
static uint8_t s_readback[k_ra8_io_roundtrip_max_payload]
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
ra8_err_t ra8_io_vfs_open(const char *path, ra8_fs_mode_t mode, ra8_fs_file_t **out_file)
Open a file by "name:/path".
Definition ra8_io_vfs.c:547
ra8_err_t ra8_io_vfs_mkdir(const char *path)
Create a directory named "name:/path".
ra8_err_t ra8_io_vfs_init(void)
Reset the VFS mount table to empty.
Definition ra8_io_vfs.c:403
NVIC + ICU IELSR allocator.
void ra8_isr_globals_enable(void)
Globally enable maskable interrupts (PRIMASK = 0).
Definition ra8_isr.c:439
Lightweight Logging Interface for ra8-firmware.
void ra8_log_init(void)
Initialise the logging backend.
Definition ra8_log.c:379
SD card driver layered on top of the RA8D2 SDHI peripheral.
ra8_err_t ra8_sdcard_init(const ra8_sdcard_cfg_t *cfg)
Bring up the SDHI block and perform full SD card initialization.
Definition ra8_sdcard.c:544
SD/MMC Host Interface (SDHI) driver scaffold.
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
Block-device interface that ra8_fs runs on top of.
Open-file state.
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-allocated block-device handle binding a backend to its context.
User-supplied configuration for ra8_sdcard_init.
Definition ra8_sdcard.h:88