|
ra8-firmware 0.1.0
Bare-metal firmware for the Renesas RA8 family (RA8D2 / RA8P1)
|
USB self-loop: HS host reads the microSD card as a read-only MSC. 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_gpio_constants.h"#include "ra8_isr.h"#include "ra8_port_constants.h"#include "ra8_port_utils.h"#include "ra8_sci_spi.h"#include "ra8_sdmmc_spi.h"#include "ra8_spi.h"#include "ra8_time.h"#include "usb_selftest_microsd_steps.h"#include "tx_api.h"#include "ux_dcd_ra8_usb.h"Go to the source code of this file.
Functions | |
| void | _tx_timer_interrupt (void) |
| void | SysTick_Handler (void) |
| Service the SysTick exception selected by the linked application. | |
| static ra8_err_t | microsd_spi_set_clock (void *ctx, uint32_t hz) |
| SD transport hook: program the Simple-SPI bit clock. | |
| static ra8_err_t | microsd_spi_cs (void *ctx, bool asserted) |
| SD transport hook: drive the chip-select line. | |
| 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. | |
| static ra8_err_t | microsd_sd_spi_open (void) |
| Route Pmod2 SCI0 pins and open the Simple-SPI controller for the card. | |
| static ra8_err_t | microsd_sd_snapshot (void) |
| Bring the card up and snapshot LBA 0..63 into RAM (read-only). | |
| VOID | tx_application_define (VOID *first_unused_memory) |
| ThreadX application-define hook. | |
| static void | microsd_panic_halt (void) |
| Halt forever in WFI – panic stop on init failure. | |
| static void | microsd_route_usb_or_halt (void) |
| Route both ports' pins: FS as device, HS as host. | |
| static void | microsd_setup_or_halt (void) |
| Bring CGC + both USB clocks + SysTick + SCI8 + LEDs + pins up. | |
| void | main (void) |
| Application entry: bring the board up, then hand off to ThreadX. | |
Variables | |
| static volatile bool | s_tx_kernel_up = false |
| Set in tx_application_define; gates ThreadX tick delivery. | |
| static const ra8_port_pin_t | k_microsd_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_microsd_pin_fs_vbusen = (ra8_port_pin_t)k_ra8_board_usbfs_pin_vbusen |
| USBFS VBUSEN (P5_00) – GPIO LOW for the device role. | |
| static const ra8_port_pin_t | k_microsd_pin_fs_dp = (ra8_port_pin_t)k_ra8_board_usbfs_pin_dp |
| USBFS D+ (P8_14). | |
| static const ra8_port_pin_t | k_microsd_pin_fs_dm = (ra8_port_pin_t)k_ra8_board_usbfs_pin_dm |
| USBFS D- (P8_15). | |
| static const ra8_port_pin_t | k_microsd_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_microsd_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_microsd_pin_sd_sck = (ra8_port_pin_t)k_ra8_board_pmod2_spi_sck |
| Pmod2 microSD SPI clock (SCI0, Simple-SPI). | |
| static const ra8_port_pin_t | k_microsd_pin_sd_cipo = (ra8_port_pin_t)k_ra8_board_pmod2_spi_cipo |
| Pmod2 microSD SPI CIPO (card -> MCU). | |
| static const ra8_port_pin_t | k_microsd_pin_sd_copi = (ra8_port_pin_t)k_ra8_board_pmod2_spi_copi |
| Pmod2 microSD SPI COPI (MCU -> card). | |
| static const ra8_port_pin_t | k_microsd_pin_sd_cs = (ra8_port_pin_t)k_ra8_board_pmod2_spi_cs |
| Pmod2 microSD chip-select (GPIO, idle high). | |
| static TX_THREAD | s_device_thread |
| ThreadX TCB for the USBX device-side worker thread. | |
| static UCHAR | s_device_stack [k_microsd_thread_stack] |
| Stack backing storage for s_device_thread. | |
| static TX_THREAD | s_host_thread |
| ThreadX TCB for the host-side worker thread. | |
| static UCHAR | s_host_stack [k_microsd_host_stack] |
| Stack backing storage for s_host_thread. | |
| static volatile uint32_t | s_dbg_sd_err = (uint32_t)k_microsd_no_mismatch |
| SD bring-up result: 0 = card read into snapshot OK, else error. | |
| static volatile uint32_t | s_dbg_sd_blocks |
| SD card capacity in 512-B blocks (sanity probe). | |
| volatile uint32_t | s_usb_selftest_microsd_dbg_bootsig |
| 1 = LBA0 carries the 0x55AA FAT/exFAT boot signature. | |
| static uint32_t | s_pclka_hz = 0U |
| PCLKA in Hz, captured at setup for the SD SPI clock adapter. | |
| UCHAR | s_usb_selftest_microsd_sd_snapshot [k_microsd_snap_bytes] |
| Read-only RAM snapshot of SD LBA 0..63, taken once at boot. | |
USB self-loop: HS host reads the microSD card as a read-only MSC.
Exposes the onboard Pmod2 microSD card as a USB drive and verifies it against itself on-chip – no PC in the loop. The two USB ports are cabled to EACH OTHER and one firmware image runs both USB stacks plus the SCI0 Simple-SPI SD driver:
No filesystem parsing is involved (raw SCSI READ(10)); this is a pure read-path test that needs no host WRITE support and no SD writes.
The link runs at 12 Mbps (FS device ceiling; HS host serves an FS downstream device).
Verdicts stream over SCI8 (J-Link OB CDC console, 115200) and are mirrored in J-Link-readable probes (s_dbg_*).
FS device: P4_07 VBUS sense, P5_00 VBUSEN GPIO LOW (device role), P8_14 D+, P8_15 D- (PSEL usb_fs). HS host: SW4-8 to Host via the U15 expander, PD07 HIGH (U18 supplies J7 VBUS), P4_08 USBHS_VBUS (PSEL usb_hs). Console: PD_02/PD_03 SCI8. microSD: Pmod2 SCI0 Simple-SPI (SCK/CIPO/COPI PSEL sci_async, CS GPIO idle high).
Definition in file main.c.
|
extern |
| void main | ( | void | ) |
Application entry: bring the board up, then hand off to ThreadX.
The application entry point Reset_Handler hands control to.
Both USB controllers' clocks and pins come up before the kernel so the workers only deal with stack bring-up.
Definition at line 588 of file main.c.
References microsd_panic_halt(), microsd_setup_or_halt(), and ra8_isr_globals_enable().
|
static |
Halt forever in WFI – panic stop on init failure.
Last-resort stop; only a debugger or reset recovers.
Definition at line 469 of file main.c.
Referenced by main(), microsd_route_usb_or_halt(), and microsd_setup_or_halt().
|
static |
Route both ports' pins: FS as device, HS as host.
FS device: P4_07 VBUS sense, P5_00 VBUSEN GPIO LOW (else peripheral routing forces host VBUSEN and blocks device enum), P8_14/P8_15 data. HS host: SW4-8 to Host via the U15 expander, PD07 HIGH (U18 supplies J7), P4_08 VBUS sense.
Definition at line 492 of file main.c.
References k_microsd_pin_fs_dm, k_microsd_pin_fs_dp, k_microsd_pin_fs_vbus, k_microsd_pin_fs_vbusen, k_microsd_pin_hs_pwr, k_microsd_pin_hs_vbus, k_ra8_level_high, k_ra8_level_low, k_ra8_ok, k_ra8_psel_usb_fs, k_ra8_psel_usb_hs, microsd_panic_halt(), ra8_board_io_expander_set_usbhs_host_mode(), ra8_gpio_output_init(), and ra8_pfs_route_peripheral().
Referenced by microsd_setup_or_halt().
|
staticnodiscard |
Bring the card up and snapshot LBA 0..63 into RAM (read-only).
Opens the SPI controller, inits ::ra8_sdmmc_spi (card enumeration), records capacity, then reads the exposed window into s_usb_selftest_microsd_sd_snapshot with ra8_sdmmc_spi_read_block. NEVER writes the card. Stamps the SD probes and flags the FAT/exFAT 0x55AA boot signature on LBA 0.
| k_ra8_ok | Snapshot captured; the snapshot buffer is valid. |
| (other) | First failing bring-up or block-read step. |
Definition at line 367 of file main.c.
References k_microsd_block_size, k_microsd_bootsig_hi, k_microsd_bootsig_lo, k_microsd_bootsig_off, k_microsd_sectors, k_ra8_ok, microsd_sd_spi_open(), microsd_spi_cs(), microsd_spi_set_clock(), microsd_spi_xfer(), ra8_sdmmc_spi_get_capacity(), ra8_sdmmc_spi_init(), ra8_sdmmc_spi_read_block(), s_dbg_sd_blocks, s_dbg_sd_err, s_pclka_hz, s_usb_selftest_microsd_dbg_bootsig, and s_usb_selftest_microsd_sd_snapshot.
Referenced by microsd_setup_or_halt().
|
staticnodiscard |
Route Pmod2 SCI0 pins and open the Simple-SPI controller for the card.
SCK/CIPO/COPI go to the SCI async PSEL; CS is a plain GPIO held across each multi-byte SD command (the SCI hardware CS pulses per word and cannot frame an SD transaction), idle high. Boots SPI at the slow init clock; ra8_sdmmc_spi_init negotiates up afterwards.
| k_ra8_ok | SCI0 Simple-SPI controller is up. |
| (other) | A pin route, GPIO, or SPI-init step failed. |
Definition at line 317 of file main.c.
References k_microsd_pin_sd_cipo, k_microsd_pin_sd_copi, k_microsd_pin_sd_cs, k_microsd_pin_sd_sck, k_microsd_sd_spi_channel, k_ra8_level_high, k_ra8_ok, k_ra8_psel_sci_async, k_ra8_sdmmc_spi_clock_init_hz, k_ra8_spi_mode_0, ra8_gpio_output_init(), ra8_pfs_route_peripheral(), ra8_sci_spi_init(), and s_pclka_hz.
Referenced by microsd_sd_snapshot().
|
static |
Bring CGC + both USB clocks + SysTick + SCI8 + LEDs + pins up.
USBFS needs the 48 MHz PLL2 reference; USBHS needs its UTMI PLL. SCI8 is the J-Link OB CDC console at 115200.
Definition at line 535 of file main.c.
References k_microsd_baud, k_ra8_board_led1, k_ra8_board_led2, k_ra8_clock_id_cpuclk0, k_ra8_clock_id_pclka, k_ra8_ok, microsd_panic_halt(), microsd_route_usb_or_halt(), microsd_sd_snapshot(), 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(), ra8_time_init(), and s_pclka_hz.
Referenced by main().
|
static |
SD transport hook: drive the chip-select line.
| [in] | ctx | Unused. |
| [in] | asserted | true = select (CS low), false = deselect (CS high). |
| k_ra8_ok | CS level updated. |
asserted (active-low). Definition at line 265 of file main.c.
References k_microsd_pin_sd_cs, k_ra8_level_high, k_ra8_level_low, and ra8_gpio_write().
Referenced by microsd_sd_snapshot().
|
static |
SD transport hook: program the Simple-SPI bit clock.
| [in] | ctx | PCLKA-Hz pointer (s_pclka_hz). |
| [in] | hz | Requested SCLK frequency in Hz. |
| k_ra8_ok | Clock applied. |
ctx is non-null. hz. Definition at line 242 of file main.c.
References k_microsd_sd_spi_channel, and ra8_sci_spi_set_clock().
Referenced by microsd_sd_snapshot().
|
static |
SD transport hook: full-duplex SPI byte exchange.
| [in] | ctx | Unused. |
| [in] | tx | Bytes to clock out (may be null for read-only). |
| [out] | rx | Bytes clocked in (may be null for write-only). |
| [in] | len | Transfer length in bytes. |
| k_ra8_ok | Transfer complete. |
rx holds the clocked-in bytes when non-null. Definition at line 290 of file main.c.
References k_microsd_sd_spi_channel, and ra8_sci_spi_xfer().
Referenced by microsd_sd_snapshot().
| void SysTick_Handler | ( | void | ) |
Service the SysTick exception selected by the linked application.
Service one SysTick event for the core timebase.
Service one SysTick event for the core timebase.
Resolves to the weak core timebase handler unless a strong RTOS or application handler overrides it. The vector table references the selected implementation without owning its subsystem policy.
The weak firmware implementation advances the millisecond counter, then services linked ThreadX and USB hooks when their weak symbols and readiness state permit. The host-test implementation advances only the counter. An application may replace either weak definition with a strong handler.
Service the SysTick exception selected by the linked application.
The weak firmware implementation advances the millisecond counter, then services linked ThreadX and USB hooks when their weak symbols and readiness state permit. The host-test implementation advances only the counter. An application may replace either weak definition with a strong handler.
Service the SysTick exception selected by the linked application.
Definition at line 91 of file main.c.
References _tx_timer_interrupt(), ra8_time_on_tick(), s_tx_kernel_up, and ux_dcd_ra8_usb_irq_reenable().
| VOID tx_application_define | ( | VOID * | first_unused_memory | ) |
ThreadX application-define hook.
Spawns both workers.
Device worker at priority 8, host worker at 24 (below the USBX class threads). Sets s_tx_kernel_up so SysTick may feed ThreadX from here on.
| [in] | first_unused_memory | Sentinel (unused; static stacks). |
Definition at line 422 of file main.c.
References k_microsd_dev_priority, k_microsd_host_priority, k_microsd_host_stack, k_microsd_thread_stack, microsd_device_worker(), microsd_host_worker(), s_device_stack, s_device_thread, s_device_thread_name, s_host_stack, s_host_thread, s_host_thread_name, s_tx_kernel_up, TX_AUTO_START, TX_NO_TIME_SLICE, and tx_thread_create.
|
static |
|
static |
|
static |
USBFS VBUS sense pin (P4_07, PSEL = 0x13).
Definition at line 106 of file main.c.
Referenced by microsd_route_usb_or_halt().
|
static |
USBFS VBUSEN (P5_00) – GPIO LOW for the device role.
Definition at line 109 of file main.c.
Referenced by microsd_route_usb_or_halt().
|
static |
J7 host-power switch (PD07): HIGH = U18 supplies VBUS (UM 6.2).
Definition at line 121 of file main.c.
Referenced by microsd_route_usb_or_halt().
|
static |
USBHS_VBUS sense pin (P4_08, PSEL = 0x14).
Definition at line 118 of file main.c.
Referenced by microsd_route_usb_or_halt().
|
static |
Pmod2 microSD SPI CIPO (card -> MCU).
Definition at line 126 of file main.c.
Referenced by microsd_sd_spi_open().
|
static |
Pmod2 microSD SPI COPI (MCU -> card).
Definition at line 128 of file main.c.
Referenced by microsd_sd_spi_open().
|
static |
Pmod2 microSD chip-select (GPIO, idle high).
Definition at line 130 of file main.c.
Referenced by microsd_sd_spi_open(), and microsd_spi_cs().
|
static |
Pmod2 microSD SPI clock (SCI0, Simple-SPI).
Definition at line 124 of file main.c.
Referenced by microsd_sd_spi_open().
|
static |
SD card capacity in 512-B blocks (sanity probe).
Definition at line 191 of file main.c.
Referenced by microsd_sd_snapshot().
|
static |
|
static |
|
static |
|
static |
|
static |
|
static |
|
static |
Set in tx_application_define; gates ThreadX tick delivery.
main() starts SysTick before tx_kernel_enter and the setup window is long (U15 expander I2C blocks for ms), so the tick fires pre-kernel; feeding _tx_timer_interrupt into ThreadX's zeroed timer state bus-faults. Gate it until the kernel runs.
| volatile uint32_t s_usb_selftest_microsd_dbg_bootsig |
1 = LBA0 carries the 0x55AA FAT/exFAT boot signature.
Defined and stamped in main.c during SD bring-up; read by the host-side verify ladder to confirm the snapshot is a valid boot region before the READ(10) sweep.
Stamped here at SD bring-up and read by the host-side verify ladder in "usb_selftest_microsd_steps.c".
Definition at line 201 of file main.c.
Referenced by microsd_sd_snapshot(), and microsd_verify_one().
| UCHAR s_usb_selftest_microsd_sd_snapshot[k_microsd_snap_bytes] |
Read-only RAM snapshot of SD LBA 0..63, taken once at boot.
Defined in main.c (populated before the kernel starts by the SD bring-up step) and read by both the device-side media-read callback and the host-side verify ladder in usb_selftest_microsd_steps.c.
Populated here before the kernel starts; consumed by both the device-side media-read callback and the host-side verify ladder in "usb_selftest_microsd_steps.c".
Definition at line 215 of file main.c.
Referenced by microsd_msc_read(), microsd_sd_snapshot(), and microsd_verify_one().