ra8-firmware 0.1.0
Bare-metal firmware for the Renesas RA8 family (RA8D2 / RA8P1)
Loading...
Searching...
No Matches
main.c File Reference

USB host MSC + ra8_fs file-operations exerciser for EK-RA8D2 (USB-HS). More...

#include <stdint.h>
#include "ra8_board_ek_ra8d2.h"
#include "ra8_boot_entry.h"
#include "ra8_cgc.h"
#include "ra8_err.h"
#include "ra8_fs.h"
#include "ra8_gpio_constants.h"
#include "ra8_isr.h"
#include "ra8_port_constants.h"
#include "ra8_port_utils.h"
#include "ra8_time.h"
#include "ra8_usb.h"
#include "ra8_usb_hmsc.h"
#include "usb_host_file_ops_steps.h"
Include dependency graph for main.c:

Go to the source code of this file.

Functions

static void fileops_panic_halt (void)
 Halt forever in WFI – panic stop.
static void fileops_route_usb_or_halt (void)
 Route both USB ports' board pins for the host role.
static void fileops_setup_or_halt (void)
 Bring CGC + both USB clocks + SysTick + SCI8 + LEDs + pins up.
static const char * fileops_port_name (ra8_usb_speed_t speed)
 Map a USB speed to the printable port name.
static ra8_err_t fileops_print_attach (const ra8_usb_hmsc_device_t *device, ra8_usb_speed_t speed)
 Print "device attached vid=0x.. pid=0x.. port=USB-xS".
static ra8_err_t fileops_run_ladder (ra8_usb_speed_t speed)
 One full pass on one port: host up, enumerate, mount, run the 9-step file-op suite.
void main (void)
 Application entry.

Variables

static const ra8_port_pin_t k_fileops_pin_hs_vbus = (ra8_port_pin_t)k_ra8_board_usbhs_pin_vbus
 USBHS_VBUS sense pin (P4_08, PSEL = 0x14).
static const ra8_port_pin_t k_fileops_pin_hs_pwr = (ra8_port_pin_t)k_ra8_board_usbhs_pin_pwr
 J7 host-power switch (PD07): HIGH = U18 supplies VBUS (UM 6.2).
static const ra8_port_pin_t k_fileops_pin_fs_vbus = (ra8_port_pin_t)k_ra8_board_usbfs_pin_vbus
 USBFS VBUS sense pin (P4_07, PSEL = 0x13).
static const ra8_port_pin_t k_fileops_pin_fs_vbusen = (ra8_port_pin_t)k_ra8_board_usbfs_pin_vbusen
 USBFS VBUSEN (P5_00): the USB function drives host VBUS.
static const ra8_port_pin_t k_fileops_pin_fs_dp = (ra8_port_pin_t)k_ra8_board_usbfs_pin_dp
 USBFS D+ (P8_14) – PFS-muxed, unlike the dedicated HS balls.
static const ra8_port_pin_t k_fileops_pin_fs_dm = (ra8_port_pin_t)k_ra8_board_usbfs_pin_dm
 USBFS D- (P8_15).

Detailed Description

USB host MSC + ra8_fs file-operations exerciser for EK-RA8D2 (USB-HS).

Tag
[Ring 6 / APP] {World: S}

Manual HIL test that proves real file operations work end to end on a USB thumb drive plugged into the J7 USB-HS jack: the board enumerates the stick via ra8_usb_hmsc_enumerate, mounts its FAT/exFAT volume through ra8_fs (block I/O bridged onto ra8_usb_hmsc_read10 / ra8_usb_hmsc_write10), then runs a 9-step suite over the J-Link OB CDC console:

  1. cleanup: best-effort unlink of leftover test files
  2. listdir of the root directory (every entry printed)
  3. write a new file USBTEST.TXT with a known payload
  4. open + read back + byte-compare the payload
  5. listdir must show USBTEST.TXT
  6. rename USBTEST.TXT -> USBDONE.TXT
  7. old name must be gone, new name must read back intact
  8. listdir must show USBDONE.TXT and not USBTEST.TXT
  9. unlink USBDONE.TXT; listdir must no longer show it

Every step prints a pass/fail verdict; the run ends with ALL FILE OPS PASSED and a solid LED1. The ladder retries every 5 s so a freshly inserted drive is picked up automatically.

Board specifics (EK-RA8D2 v1 UM):

  • PD07 HIGH = U18 supplies VBUS to J7 (Sec 6.2 p 34, 2 A budget).
  • SW4-8 must sit in the Host position; set in software through the U15 expander (ra8_board_io_expander_set_usbhs_host_mode).
  • P4_08 is the only PFS-muxed USBHS pin (VBUS sense, PSEL 0x14); D+/D- are dedicated PHY package balls.
Author
Brighton Sikarskie
Date
2026-06-12
Since
0.1.0

Definition in file main.c.

Function Documentation

◆ fileops_panic_halt()

void fileops_panic_halt ( void )
static

Halt forever in WFI – panic stop.

Precondition
Called only after a fatal boot error.
Interrupts may be in any state.
Postcondition
CPU is parked; only a debugger or external reset wakes it.
No further code runs.
Note
Never returns.
Since
0.1.0

Definition at line 97 of file main.c.

Referenced by fileops_route_usb_or_halt(), fileops_setup_or_halt(), and main().

◆ fileops_port_name()

const char * fileops_port_name ( ra8_usb_speed_t speed)
static

Map a USB speed to the printable port name.

Parameters
[in]speedController selector.
Returns
Static NUL-terminated name string.
Return values
USB-HSFor k_ra8_usb_speed_hs.
Precondition
None – total over the two host speeds.
speed is one of the two host-capable controllers.
Postcondition
No state changes.
Returned pointer references static storage.
Note
Pure function.
Since
0.1.0

Definition at line 214 of file main.c.

References k_ra8_usb_speed_hs.

Referenced by fileops_print_attach(), and fileops_run_ladder().

◆ fileops_print_attach()

ra8_err_t fileops_print_attach ( const ra8_usb_hmsc_device_t * device,
ra8_usb_speed_t speed )
staticnodiscard

Print "device attached vid=0x.. pid=0x.. port=USB-xS".

Parameters
[in]deviceEnumerated device snapshot.
[in]speedController the device answered on.
Returns
ra8_err_t propagated from the SCI helpers.
Return values
k_ra8_okAll chunks queued.
Precondition
SCI8 init already ran; device is non-NULL.
The snapshot was filled by ra8_usb_hmsc_enumerate.
Postcondition
One ASCII line is in the SCI8 TX FIFO.
No other state changes.
Note
Blocking polled TX.
Since
0.1.0

Definition at line 236 of file main.c.

References fileops_port_name(), fileops_print(), fileops_print_hex(), k_fileops_hex_chars_u16, k_ra8_ok, ra8_usb_hmsc_device_t::product_id, and ra8_usb_hmsc_device_t::vendor_id.

Referenced by fileops_run_ladder().

◆ fileops_route_usb_or_halt()

void fileops_route_usb_or_halt ( void )
static

Route both USB ports' board pins for the host role.

Panic-halts.

HS (J7): SW4-8 to Host via the U15 expander, PD07 HIGH (U18 supplies the jack), P4_08 to USBHS_VBUS. FS: P4_07 (VBUS sense), P5_00 (VBUSEN – the controller drives host VBUS through it), and the PFS-muxed data lines P8_14/P8_15.

Precondition
IOPORT and the U15 expander are reachable.
Called once from fileops_setup_or_halt.
Postcondition
Both ports' pins carry their USB peripheral functions.
PD07 is HIGH (J7 powered).
Note
Panic-halts on any routing failure.
Since
0.1.0

Definition at line 124 of file main.c.

References fileops_panic_halt(), k_fileops_pin_fs_dm, k_fileops_pin_fs_dp, k_fileops_pin_fs_vbus, k_fileops_pin_fs_vbusen, k_fileops_pin_hs_pwr, k_fileops_pin_hs_vbus, k_ra8_level_high, k_ra8_ok, k_ra8_psel_usb_fs, k_ra8_psel_usb_hs, ra8_board_io_expander_set_usbhs_host_mode(), ra8_gpio_output_init(), and ra8_pfs_route_peripheral().

Referenced by fileops_setup_or_halt().

◆ fileops_run_ladder()

ra8_err_t fileops_run_ladder ( ra8_usb_speed_t speed)
staticnodiscard

One full pass on one port: host up, enumerate, mount, run the 9-step file-op suite.

Brings the selected controller up (ra8_usb_hmsc_init), so the caller can alternate between the HS and FS ports per retry cycle. On any failure the controller is closed again before returning; the mount handle is always released so the static mount-slot pool cannot leak.

Parameters
[in]speedController to drive this cycle (HS or FS port).
Returns
First failing step's error, or k_ra8_ok.
Return values
k_ra8_okThe suite printed ALL FILE OPS PASSED.
Precondition
fileops_setup_or_halt completed.
The USB drive is inserted in either host jack.
Postcondition
On success LED1 is on and all verdict lines are printed.
The volume's test files are removed again (suite is self-cleaning).
Note
Blocking; bounded by the class-layer timeouts.
Since
0.1.0

Definition at line 285 of file main.c.

References fileops_mount_volume(), fileops_port_name(), fileops_print(), fileops_print_attach(), fileops_print_fail(), fileops_probe_layout(), fileops_suite_create(), fileops_suite_mutate(), k_ra8_board_led1, k_ra8_ok, ra8_board_led_on(), ra8_fs_unmount(), ra8_usb_hmsc_close(), ra8_usb_hmsc_enumerate(), and ra8_usb_hmsc_init().

Referenced by main().

◆ fileops_setup_or_halt()

void fileops_setup_or_halt ( void )
static

Bring CGC + both USB clocks + SysTick + SCI8 + LEDs + pins up.

Panic-halts on any failure.

Enables the USBHS 60 MHz PLL and the USBFS 48 MHz PLL2 clock (the latter must run before MSTPB11 is released or UACT never sticks), then routes both ports' pins. The controller itself is brought up per retry cycle by the ladder (ra8_usb_hmsc_init), since the app alternates between the HS and FS ports.

Precondition
Reset_Handler has finished C runtime init.
SystemInit has run.
Postcondition
Console prints work; both USB ports' pins and clocks are live.
LED1/LED2 are initialized.
Note
Called exactly once from main.
Since
0.1.0

Definition at line 171 of file main.c.

References fileops_panic_halt(), fileops_route_usb_or_halt(), k_fileops_baud, k_ra8_board_led1, k_ra8_board_led2, k_ra8_clock_id_cpuclk0, k_ra8_ok, ra8_board_led_init(), ra8_board_uart_console_init(), ra8_cgc_get_clock_hz(), ra8_cgc_init(), ra8_cgc_usbfs_clock_enable(), ra8_cgc_usbhs_pll_enable(), and ra8_time_init().

Referenced by main().

◆ main()

void main ( void )

Application entry.

The application entry point Reset_Handler hands control to.

Alternates between the HS and FS host ports and runs the file-op suite until it fully passes.

Precondition
Reset_Handler has copied .data and zeroed .bss.
SystemInit has set VTOR, FPU, and priority grouping.
Postcondition
On a clean run every suite verdict line is printed, LED1 is lit, and the CPU parks in WFI.
On any HAL init failure the function halts in WFI.
Since
0.1.0

Definition at line 359 of file main.c.

References fileops_panic_halt(), fileops_print(), fileops_print_fail(), fileops_run_ladder(), fileops_setup_or_halt(), k_fileops_idle_ms, k_fileops_retry_ms, k_ra8_ok, k_ra8_usb_speed_fs, k_ra8_usb_speed_hs, ra8_delay_ms(), and ra8_isr_globals_enable().

Variable Documentation

◆ k_fileops_pin_fs_dm

const ra8_port_pin_t k_fileops_pin_fs_dm = (ra8_port_pin_t)k_ra8_board_usbfs_pin_dm
static

USBFS D- (P8_15).

Definition at line 80 of file main.c.

Referenced by fileops_route_usb_or_halt().

◆ k_fileops_pin_fs_dp

const ra8_port_pin_t k_fileops_pin_fs_dp = (ra8_port_pin_t)k_ra8_board_usbfs_pin_dp
static

USBFS D+ (P8_14) – PFS-muxed, unlike the dedicated HS balls.

Definition at line 77 of file main.c.

Referenced by fileops_route_usb_or_halt().

◆ k_fileops_pin_fs_vbus

const ra8_port_pin_t k_fileops_pin_fs_vbus = (ra8_port_pin_t)k_ra8_board_usbfs_pin_vbus
static

USBFS VBUS sense pin (P4_07, PSEL = 0x13).

Definition at line 71 of file main.c.

Referenced by fileops_route_usb_or_halt().

◆ k_fileops_pin_fs_vbusen

const ra8_port_pin_t k_fileops_pin_fs_vbusen = (ra8_port_pin_t)k_ra8_board_usbfs_pin_vbusen
static

USBFS VBUSEN (P5_00): the USB function drives host VBUS.

Definition at line 74 of file main.c.

Referenced by fileops_route_usb_or_halt().

◆ k_fileops_pin_hs_pwr

const ra8_port_pin_t k_fileops_pin_hs_pwr = (ra8_port_pin_t)k_ra8_board_usbhs_pin_pwr
static

J7 host-power switch (PD07): HIGH = U18 supplies VBUS (UM 6.2).

Definition at line 68 of file main.c.

Referenced by fileops_route_usb_or_halt().

◆ k_fileops_pin_hs_vbus

const ra8_port_pin_t k_fileops_pin_hs_vbus = (ra8_port_pin_t)k_ra8_board_usbhs_pin_vbus
static

USBHS_VBUS sense pin (P4_08, PSEL = 0x14).

Definition at line 65 of file main.c.

Referenced by fileops_route_usb_or_halt().