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
52
53#include <stdint.h>
54
55#include "ra8_board_ek_ra8d2.h"
56#include "ra8_boot_entry.h"
57#include "ra8_cgc.h"
58#include "ra8_err.h"
59#include "ra8_gpio_constants.h"
60#include "ra8_isr.h"
61#include "ra8_port_constants.h"
62#include "ra8_port_utils.h"
63#include "ra8_sci_spi.h"
64#include "ra8_sdmmc_spi.h"
65#include "ra8_spi.h"
66#include "ra8_time.h"
68
69#ifndef RA8_OFF_TARGET
70#include "tx_api.h"
71#include "ux_dcd_ra8_usb.h"
72
73/* Strong SysTick override: route the tick into BOTH the ra8_time millisecond
74 * counter (for ra8_delay_ms and the polled host stack's timeouts) AND
75 * ThreadX's timer; the 1 ms pulse also recovers the DCD's storm-guard mask. */
76
77extern void _tx_timer_interrupt(void);
78
88static volatile bool s_tx_kernel_up = false;
89
90void SysTick_Handler(void);
99#endif
100
101/* -------------------------------------------------------------------------- */
102/* Pinout (FSP-aligned, EK-RA8D2 v1 User's Manual) */
103/* -------------------------------------------------------------------------- */
104
107
110
113
116
119
122
131
132/* -------------------------------------------------------------------------- */
133/* Tunables */
134/* -------------------------------------------------------------------------- */
135
136/*
137 * The shared compile-time tunables, hex/mask formatter sizing, LUN geometry,
138 * host/device progress phases, SCSI sense triples, and USB LANGID bytes all
139 * live as typed enums in "usb_selftest_microsd_steps.h" so main.c and both
140 * helper siblings observe one definition.
141 */
142
143#ifndef RA8_OFF_TARGET
144
145/* -------------------------------------------------------------------------- */
146/* ThreadX workers + USBX pool storage */
147/* -------------------------------------------------------------------------- */
148
156
163
171
178
179/* The USBX pool, SCSI INQUIRY strings, USB descriptor frameworks, and the
180 * device/host-owned J-Link progress probes live in
181 * "usb_selftest_microsd_steps.c"; only the SD bring-up probes and the two
182 * cross-TU snapshot symbols are owned here. */
183
184/* -------------------------------------------------------------------------- */
185/* J-Link probes (SD bring-up owned) */
186/* -------------------------------------------------------------------------- */
187
189static volatile uint32_t s_dbg_sd_err = (uint32_t)k_microsd_no_mismatch;
191static volatile uint32_t s_dbg_sd_blocks;
192
202
204static uint32_t s_pclka_hz = 0U;
205
216
217/* -------------------------------------------------------------------------- */
218/* microSD over SCI0 Simple-SPI -- READ-ONLY backing snapshot */
219/* -------------------------------------------------------------------------- */
220
221/* cppcheck-suppress-begin [constParameterCallback] -- these three hooks
222 * are bound to the ra8_sdmmc_spi_transport_t vtable, whose signatures take
223 * a non-const void* ctx; const-qualifying would break the type. */
224
242static ra8_err_t microsd_spi_set_clock(void* ctx, uint32_t hz)
243{
244 const uint32_t pclka = *(const uint32_t*)ctx;
245 return ra8_sci_spi_set_clock((uint8_t)k_microsd_sd_spi_channel, hz, pclka);
246}
247
265static ra8_err_t microsd_spi_cs(void* ctx, bool asserted)
266{
267 (void)ctx;
269}
270
290static ra8_err_t microsd_spi_xfer(void* ctx, const uint8_t* tx, uint8_t* rx, uint32_t len)
291{
292 (void)ctx;
293 return ra8_sci_spi_xfer((uint8_t)k_microsd_sd_spi_channel, tx, rx, len);
294}
295/* cppcheck-suppress-end [constParameterCallback] */
296
317[[nodiscard]] static ra8_err_t microsd_sd_spi_open(void)
318{
319 ra8_err_t err =
321 if (err != k_ra8_ok) {
322 return err;
323 }
325 if (err != k_ra8_ok) {
326 return err;
327 }
329 if (err != k_ra8_ok) {
330 return err;
331 }
333 if (err != k_ra8_ok) {
334 return err;
335 }
336 const ra8_sci_spi_cfg_t spi_cfg = {
337 .baud_hz = (uint32_t)k_ra8_sdmmc_spi_clock_init_hz,
338 .pclk_hz = s_pclka_hz,
339 .mode = k_ra8_spi_mode_0,
340 .lsb_first = false,
341 };
342 return ra8_sci_spi_init((uint8_t)k_microsd_sd_spi_channel, &spi_cfg);
343}
344
367[[nodiscard]] static ra8_err_t microsd_sd_snapshot(void)
368{
370 if (err != k_ra8_ok) {
371 s_dbg_sd_err = (uint32_t)err;
372 return err;
373 }
374 const ra8_sdmmc_spi_transport_t transport = {
375 .set_clock = microsd_spi_set_clock,
376 .cs = microsd_spi_cs,
377 .xfer = microsd_spi_xfer,
378 .ctx = &s_pclka_hz,
379 };
380 err = ra8_sdmmc_spi_init(&transport);
381 if (err != k_ra8_ok) {
382 s_dbg_sd_err = (uint32_t)err;
383 return err;
384 }
385 uint32_t blocks = 0U;
386 (void)ra8_sdmmc_spi_get_capacity(&blocks);
387 s_dbg_sd_blocks = blocks;
388 for (uint32_t lba = 0U; lba < (uint32_t)k_microsd_sectors; lba++) {
390 lba,
392 if (err != k_ra8_ok) {
393 s_dbg_sd_err = (uint32_t)err;
394 return err;
395 }
396 }
397 const bool sig =
401 s_dbg_sd_err = 0U;
402 return k_ra8_ok;
403}
404
422VOID tx_application_define(VOID* first_unused_memory)
423{
424 static CHAR s_device_thread_name[] = "microsd_device";
425 static CHAR s_host_thread_name[] = "microsd_host";
426
427 (void)first_unused_memory;
428 s_tx_kernel_up = true;
432 0UL,
442 0UL,
449}
450#endif /* !RA8_OFF_TARGET */
451
452/* -------------------------------------------------------------------------- */
453/* Startup */
454/* -------------------------------------------------------------------------- */
455
469static void microsd_panic_halt(void)
470{
471 while (1) {
472 __asm__ volatile("wfi");
473 }
474}
475
520
535static void microsd_setup_or_halt(void)
536{
537 uint32_t cpuclk0_hz = 0U;
538 uint32_t pclka_hz = 0U;
539 if (ra8_cgc_init() != k_ra8_ok) {
541 }
544 }
547 }
550 }
553 }
554 s_pclka_hz = pclka_hz;
555 if (ra8_time_init(cpuclk0_hz) != k_ra8_ok) {
557 }
560 }
563 }
566 }
567 /* Snapshot SD LBA 0..63 into RAM (read-only) before the kernel starts.
568 * On failure s_dbg_sd_err records it and the host verifier reports the
569 * missing boot signature -- the board does not hang. */
570 (void)microsd_sd_snapshot();
572}
573
588void main(void)
589{
591
593
594#ifndef RA8_OFF_TARGET
595 tx_kernel_enter();
596#endif
597
599}
void main(void)
Secure fallback main entry point.
Definition main.c:37
static uint32_t s_pclka_hz
Definition c6_cam_app.c:38
void tx_application_define(void *first_unused_memory)
ThreadX system-define hook: build worker thread + byte pool.
Definition main.c:942
void _tx_timer_interrupt(void)
void SysTick_Handler(void)
SysTick exception handler – tail-calls the ThreadX timer.
Definition main.c:146
static volatile bool s_tx_kernel_up
Set in tx_application_define; gates ThreadX tick delivery.
Definition main.c:86
static UCHAR s_host_stack[k_wlun_host_stack]
Stack backing storage for s_host_thread.
Definition main.c:186
static TX_THREAD s_device_thread
ThreadX TCB for the USBX device-side worker thread.
Definition main.c:164
static CHAR s_host_thread_name[]
Definition main.c:202
static TX_THREAD s_host_thread
ThreadX TCB for the host-side worker thread.
Definition main.c:179
static UCHAR s_device_stack[k_wlun_thread_stack]
Stack backing storage for s_device_thread.
Definition main.c:171
static CHAR s_device_thread_name[]
Definition main.c:201
static volatile uint32_t s_dbg_sd_err
SD bring-up result: 0 = card enumerated OK, else the error code.
Definition main.c:123
static void microsd_route_usb_or_halt(void)
Route both ports' pins: FS as device, HS as host.
Definition main.c:492
static void microsd_setup_or_halt(void)
Bring CGC + both USB clocks + SysTick + SCI8 + LEDs + pins up.
Definition main.c:535
static ra8_err_t microsd_sd_snapshot(void)
Bring the card up and snapshot LBA 0..63 into RAM (read-only).
Definition main.c:367
static const ra8_port_pin_t k_microsd_pin_sd_cipo
Pmod2 microSD SPI CIPO (card -> MCU).
Definition main.c:126
static const ra8_port_pin_t k_microsd_pin_sd_copi
Pmod2 microSD SPI COPI (MCU -> card).
Definition main.c:128
static ra8_err_t microsd_spi_cs(void *ctx, bool asserted)
SD transport hook: drive the chip-select line.
Definition main.c:265
static const ra8_port_pin_t k_microsd_pin_sd_sck
Pmod2 microSD SPI clock (SCI0, Simple-SPI).
Definition main.c:124
static ra8_err_t microsd_spi_xfer(void *ctx, const uint8_t *tx, uint8_t *rx, uint32_t len)
SD transport hook: full-duplex SPI byte exchange.
Definition main.c:290
static const ra8_port_pin_t k_microsd_pin_fs_dm
USBFS D- (P8_15).
Definition main.c:115
static const ra8_port_pin_t k_microsd_pin_fs_vbusen
USBFS VBUSEN (P5_00) – GPIO LOW for the device role.
Definition main.c:109
static volatile uint32_t s_dbg_sd_blocks
SD card capacity in 512-B blocks (sanity probe).
Definition main.c:191
static void microsd_panic_halt(void)
Halt forever in WFI – panic stop on init failure.
Definition main.c:469
static ra8_err_t microsd_spi_set_clock(void *ctx, uint32_t hz)
SD transport hook: program the Simple-SPI bit clock.
Definition main.c:242
static const ra8_port_pin_t k_microsd_pin_sd_cs
Pmod2 microSD chip-select (GPIO, idle high).
Definition main.c:130
static const ra8_port_pin_t k_microsd_pin_hs_pwr
J7 host-power switch (PD07): HIGH = U18 supplies VBUS (UM 6.2).
Definition main.c:121
static const ra8_port_pin_t k_microsd_pin_fs_dp
USBFS D+ (P8_14).
Definition main.c:112
static ra8_err_t microsd_sd_spi_open(void)
Route Pmod2 SCI0 pins and open the Simple-SPI controller for the card.
Definition main.c:317
static const ra8_port_pin_t k_microsd_pin_hs_vbus
USBHS_VBUS sense pin (P4_08, PSEL = 0x14).
Definition main.c:118
static const ra8_port_pin_t k_microsd_pin_fs_vbus
USBFS VBUS sense pin (P4_07, PSEL = 0x13).
Definition main.c:106
Board-support layer for the Renesas EK-RA8D2 v1 evaluation kit.
@ k_ra8_board_pmod2_spi_cipo
Pmod2.3 CIPO (CIPO0_B), P602.
@ k_ra8_board_pmod2_spi_cs
Pmod2.1 CS (SS0_B), P604.
@ k_ra8_board_pmod2_spi_copi
Pmod2.2 COPI (COPI0_B), P603.
@ k_ra8_board_pmod2_spi_sck
Pmod2.4 SCK (SCK0_B), P601.
ra8_err_t ra8_board_led_init(ra8_board_led_id_t led)
Configure led as a digital output, initial level low (off).
@ k_ra8_board_led2
LED2, GREEN, P303 (jumper E26).
@ k_ra8_board_led1
LED1, BLUE, P600 (jumper E27).
ra8_err_t ra8_board_io_expander_set_usbhs_host_mode(void)
Drive the U15 I/O expander to select USB-HS HOST mode.
ra8_err_t ra8_board_uart_console_init(uint32_t baud)
Configure SCI8 + PD02/PD03 as the debug-console UART.
@ k_ra8_board_usbhs_pin_pwr
PD07 J7 host-power switch (HIGH = U18 drives 5 V VBUS).
@ k_ra8_board_usbhs_pin_vbus
P4_08 USBHS_VBUS sense.
@ k_ra8_board_usbfs_pin_dm
P8_15 D-.
@ k_ra8_board_usbfs_pin_vbusen
P5_00 VBUSEN GPIO.
@ k_ra8_board_usbfs_pin_vbus
P4_07 VBUS sense.
@ k_ra8_board_usbfs_pin_dp
P8_14 D+.
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
ra8_err_t ra8_cgc_usbfs_clock_enable(void)
Bring up the USB-FS module clock (USBCKCR / USBCKDIVCR).
ra8_err_t ra8_cgc_usbhs_pll_enable(void)
Bring up the USBHS PHY reference clock + module clock.
Error Code Definitions for ra8-firmware.
@ k_ra8_ok
Success – operation completed with all postconditions satisfied.
Definition ra8_err.h:119
ra8_err_codes_t ra8_err_t
Canonical error-return type used by every ra8-firmware API.
Definition ra8_err.h:546
PSEL codes for peripheral pin routing on the RA8D2.
@ k_ra8_psel_usb_fs
10011b: USB Full-Speed.
@ k_ra8_psel_sci_async
00100b: SCI async serial (UART).
@ k_ra8_psel_usb_hs
10100b: USB High-Speed (e.g.
NVIC + ICU IELSR allocator.
void ra8_isr_globals_enable(void)
Globally enable maskable interrupts (PRIMASK = 0).
Definition ra8_isr.c:439
Typed Port / Pin Constants for the RA8D2 IOPORT Module.
ra8_port_pin_t
Packed (port << 8) | pin pin identifier.
@ k_ra8_level_high
Drive or read 1.
@ k_ra8_level_low
Drive or read 0.
High-level GPIO helpers on top of the PORT + PFS register layer.
ra8_err_t ra8_gpio_output_init(ra8_port_pin_t pin, ra8_level_t init_level)
Configure a pin as a digital output and drive it to an initial level.
Definition gpio.c:88
ra8_err_t ra8_gpio_write(ra8_port_pin_t pin, ra8_level_t level)
Drive a previously-configured output to the given level.
Definition gpio.c:154
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
SCI Simple-SPI controller-mode driver (polling, full-duplex).
ra8_err_t ra8_sci_spi_set_clock(uint8_t channel, uint32_t baud_hz, uint32_t pclk_hz)
Re-program the bit-rate without otherwise reconfiguring.
ra8_err_t ra8_sci_spi_xfer(uint8_t channel, const uint8_t *tx, uint8_t *rx, uint32_t len)
Full-duplex multi-byte transfer.
ra8_err_t ra8_sci_spi_init(uint8_t channel, const ra8_sci_spi_cfg_t *cfg)
Bring an SCI channel up as a Simple-SPI controller.
SD card driver in SPI-mode (PMOD-attached cards).
@ k_ra8_sdmmc_spi_clock_init_hz
Mandatory init clock floor.
ra8_err_t ra8_sdmmc_spi_read_block(uint32_t lba, uint8_t *buf)
Read one 512-byte block at logical block address lba.
ra8_err_t ra8_sdmmc_spi_get_capacity(uint32_t *out_blocks)
Return the card capacity in 512-byte blocks.
ra8_err_t ra8_sdmmc_spi_init(const ra8_sdmmc_spi_transport_t *transport)
Run the SD SPI-mode identification sequence on a card.
SPI_B controller driver (RA8D2 Type-B SPI peripheral).
@ k_ra8_spi_mode_0
CPOL=0, CPHA=0.
Definition ra8_spi.h:62
void ux_dcd_ra8_usb_irq_reenable(void)
Storm-guard recovery: zero the run counter and re-enable the USB IRQ.
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
void ra8_time_on_tick(void)
SysTick IRQ handler – called from the vector table.
Definition ra8_time.c:170
unsigned int UINT
ThreadX-compatible unsigned int (host stub).
#define tx_thread_create
#define TX_NO_TIME_SLICE
char CHAR
ThreadX-compatible CHAR (host stub).
#define TX_AUTO_START
Opaque thread stand-in for the host build.
Configuration descriptor for ra8_sci_spi_init.
Definition ra8_sci_spi.h:49
Driver-to-bus binding (Dependency Inversion seam).
examples/ek_ra8d2/hw_validated/hil/usb_selftest_microsd/inc/usb_selftest_microsd_steps....
unsigned char s_usb_selftest_microsd_sd_snapshot[(uint32_t) k_microsd_snap_bytes]
Read-only RAM snapshot of SD LBA 0..63, taken once at boot.
Definition main.c:215
@ k_microsd_sd_spi_channel
SCI0 Simple-SPI -> Pmod2 microSD.
@ k_microsd_thread_stack
Device worker stack (bytes).
@ k_microsd_host_priority
Host worker priority (below USBX).
@ k_microsd_host_stack
Host worker stack (bytes).
@ k_microsd_dev_priority
Device bring-up worker priority.
@ k_microsd_baud
J-Link OB CDC log baud.
@ k_microsd_block_size
SCSI logical block size.
@ k_microsd_no_mismatch
Probe: no mismatch.
@ k_microsd_bootsig_off
FAT/exFAT 0x55AA at byte 510.
@ k_microsd_snap_bytes
RAM snapshot of LBA 0..63.
@ k_microsd_sectors
Exposed sectors (LBA 0..63).
@ k_microsd_bootsig_lo
Boot-signature byte 510.
@ k_microsd_bootsig_hi
Boot-signature byte 511.
VOID microsd_host_worker(ULONG arg)
Host-side worker: retry the full pass until it succeeds.
VOID microsd_device_worker(ULONG arg)
Device-side worker: bring the read-only SD FS device up, then park.
volatile uint32_t s_usb_selftest_microsd_dbg_bootsig
1 = LBA0 carries the 0x55AA FAT/exFAT boot signature.
Definition main.c:201
USBX device-controller-driver (DCD) bridge to ra8_usb.