65#include "ux_device_class_dfu.h"
66#include "ux_device_stack.h"
120typedef enum : uint32_t {
138typedef enum : uint8_t {
148typedef enum : uint32_t {
156typedef enum : uint32_t {
165#ifndef RA8_OFF_TARGET
194 0x00U, 0x00U, 0x07U, 0x22U, 0x11U, 0x00U, 0x02U, 0x02U, 0xAFU, 0xF3U, 0x00U, 0x80U, 0xAFU,
195 0xF3U, 0x00U, 0x80U, 0x46U, 0xF2U, 0x0DU, 0x00U, 0xC6U, 0xF2U, 0x0DU, 0x00U, 0x40U, 0xF2U,
196 0x00U, 0x01U, 0xC2U, 0xF2U, 0x04U, 0x21U, 0x08U, 0x60U, 0xFEU, 0xE7U, 0xFFU, 0xFFU, 0xFFU,
197 0xFFU, 0xFFU, 0xFFU, 0xFFU, 0xFFU, 0xFFU, 0xFFU, 0xFFU, 0xFFU, 0xFFU, 0xFFU, 0xFFU, 0xFFU,
198 0xFFU, 0xFFU, 0xFFU, 0xFFU, 0xFFU, 0xFFU, 0xFFU, 0xFFU, 0xFFU, 0xFFU, 0xFFU, 0xFFU,
339typedef enum : uint8_t {
412 return (uint8_t)((uint8_t)
'0' + (uint8_t)nibble);
433 if (text[len] ==
'\0') {
492 uint8_t width = digits;
496 for (uint8_t i = 0U; i < width; i++) {
545typedef enum : uint16_t {
571typedef enum : uint32_t {
599 dfu_print(
"ra8d2 dfu-boot: host up, DFU-flashing a bootable Slot-A payload...\r\n");
622 bool committed =
false;
647 "ra8d2 dfu-boot: Slot A DFU-committed + bootable -- USB SELFTEST DFU-BOOT COMMIT PASS\r\n");
700 (void)first_unused_memory;
742 __asm__
volatile(
"wfi");
793 uint32_t cpuclk0_hz = 0U;
836#ifndef RA8_OFF_TARGET
void main(void)
Secure fallback main entry point.
void tx_application_define(void *first_unused_memory)
ThreadX system-define hook: build worker thread + byte pool.
void _tx_timer_interrupt(void)
void SysTick_Handler(void)
SysTick exception handler – tail-calls the ThreadX timer.
static volatile bool s_tx_kernel_up
Set in tx_application_define; gates ThreadX tick delivery.
static UCHAR s_host_stack[k_wlun_host_stack]
Stack backing storage for s_host_thread.
static TX_THREAD s_device_thread
ThreadX TCB for the USBX device-side worker thread.
static UCHAR s_string_framework[]
USBX string descriptor table (vendor / product / serial).
static UCHAR s_usbx_pool[k_wlun_usbx_pool_bytes]
USBX memory pool (USBX uses tx_byte_pool internally).
@ k_usb_langid_en_us_lo
LANGID 0x0409 low byte.
@ k_usb_langid_en_us_hi
LANGID 0x0409 high byte.
static CHAR s_host_thread_name[]
static TX_THREAD s_host_thread
ThreadX TCB for the host-side worker thread.
static UCHAR s_language_id_framework[]
USBX language-id table – US English.
static UCHAR s_device_stack[k_wlun_thread_stack]
Stack backing storage for s_device_thread.
static volatile uint32_t s_dbg_dev_step
Device worker progress: 1 stack, 2 class, 3 dcd, 4 attach, 5 parked.
static CHAR s_device_thread_name[]
static UCHAR s_device_framework[]
USBX device + DFU descriptors: a DFU-mode interface (bInterfaceProtocol 0x02, enumerates straight int...
static volatile uint32_t s_dbg_dev_writes
Device-side download block-write count.
static ra8_err_t dfu_print_fail(const char *what, ra8_err_t err)
Print "FAIL <what> err=0xNNNNNNNN" on its own line.
static const ra8_port_pin_t k_dfu_pin_hs_vbus
USBHS_VBUS sense pin (P4_08, PSEL = 0x14).
static ra8_err_t dfu_sci_write(const uint8_t *data, uint32_t len)
Push a literal block over the board UART console (SCI8) polled.
static void dfu_panic_halt(void)
Halt forever in WFI – panic stop on init failure.
static ra8_err_t dfu_host_pass(void)
Run the full host pass: enumerate, download, upload-verify.
static const ra8_port_pin_t k_dfu_pin_hs_pwr
J7 host-power switch (PD07): HIGH = U18 supplies VBUS.
static const ra8_port_pin_t k_dfu_pin_fs_vbusen
USBFS VBUSEN (P5_00) – GPIO LOW for the device role.
static VOID dfu_host_worker(ULONG arg)
Host-side worker: retry the full pass until it succeeds, then park.
dfu_config_t
Compile-time settings: threads, pool, console, cadence.
@ k_dfu_print_cap
Bound for console-string scans.
@ k_dfu_usbx_pool_bytes
USBX memory pool (bytes).
@ k_dfu_host_priority
Host worker priority (below USBX).
@ k_dfu_retry_ticks
Pause between ladder retries.
@ k_dfu_boot_wait_ticks
Host start delay (1 ms ticks).
@ k_dfu_dev_priority
Device bring-up worker priority.
@ k_dfu_thread_stack
Device worker stack (bytes).
@ k_dfu_baud
J-Link OB CDC log baud.
@ k_dfu_idle_ticks
Parked-loop back-off (ticks).
@ k_dfu_commit_tries
Polls awaiting the device commit.
@ k_dfu_host_stack
Host worker stack (bytes).
dfu_req_t
Chapter-9 + DFU class request / descriptor constants.
@ k_dfu_breq_dnload
DFU_DNLOAD.
@ k_dfu_breq_getstatus
DFU_GETSTATUS.
@ k_dfu_breq_get_desc
GET_DESCRIPTOR.
@ k_dfu_byte_bits
Bits per byte.
@ k_dfu_bm_std_dev_in
bmRequestType: Std | Device | In.
@ k_dfu_getstatus_len
DFU_GETSTATUS payload len.
@ k_dfu_breq_set_config
SET_CONFIGURATION.
@ k_dfu_breq_set_addr
SET_ADDRESS.
@ k_dfu_desc_device
DEVICE descriptor type.
@ k_dfu_off_dev_pid
idProduct LSB byte offset.
@ k_dfu_state_dnload_idle
dfuDNLOAD-IDLE.
@ k_dfu_bm_class_if_in
bmRequestType: Class | Interface | In.
@ k_dfu_breq_abort
DFU_ABORT (-> dfuIDLE).
@ k_dfu_dev_desc_len
DEVICE descriptor length.
@ k_dfu_breq_upload
DFU_UPLOAD.
@ k_dfu_bm_class_if_out
bmRequestType: Class | Interface | Out.
@ k_dfu_state_idle
dfuIDLE.
@ k_dfu_bm_std_dev_out
bmRequestType: Std | Device | Out.
@ k_dfu_off_status_state
bState offset in GETSTATUS.
dfu_enum_tune_t
Timing / retry tunables for the polled enumeration + status polling.
@ k_dfu_reset_hold_ms
USB bus-reset hold (>=10 ms).
@ k_dfu_attach_to_ms
Wait for the D+ pull-up.
@ k_dfu_enum_tries
Reset+probe attempts.
@ k_dfu_attach_spin
Attach spin cap (frozen-tick guard).
@ k_dfu_recovery_ms
Post-reset recovery (TRSTRCY).
@ k_dfu_status_poll_ms
Pause between GETSTATUS polls.
@ k_dfu_addr_settle_ms
Post-SET_ADDRESS recovery.
@ k_dfu_vbus_settle_ms
VBUS settle before probing.
@ k_dfu_debounce_ms
Post-attach debounce (>=100 ms).
@ k_dfu_status_tries
GETSTATUS polls before giving up.
static VOID dfu_device_worker(ULONG arg)
Device-side worker: bring the DFU device up, then park.
static const UCHAR s_boot_payload[64]
A real bootable Slot-A image the host DFU-flashes: a 64 KiB-aligned vector table (initial MSP,...
static volatile uint32_t s_dbg_pid
Device-reported product id captured at enumeration.
static const ra8_port_pin_t k_dfu_pin_fs_dm
USBFS D- (P8_15).
static uint8_t dfu_nibble_to_hex(uint32_t nibble)
Format one nibble (0..15) into an uppercase hex character.
static void dfu_setup_or_halt(void)
Bring CGC + both USB clocks + SysTick + SCI8 + LEDs + pins up.
dfu_phase_t
J-Link probe values marking host-ladder progress.
@ k_dfu_phase_init
Host controller init.
@ k_dfu_phase_pass
Image verified byte-equal.
@ k_dfu_phase_upload
Running DFU_UPLOAD.
@ k_dfu_phase_download
Running DFU_DNLOAD.
@ k_dfu_phase_enum
Enumerating.
@ k_dfu_phase_boot
Host thread not started.
static ra8_err_t dfu_print_hex(uint32_t value, uint8_t digits)
Print a value as fixed-width uppercase hex.
static const ra8_port_pin_t k_dfu_pin_fs_dp
USBFS D+ (P8_14).
static const ra8_port_pin_t k_dfu_pin_fs_vbus
USBFS VBUS sense pin (P4_07, PSEL = 0x13).
static uint32_t dfu_str_len(const char *text)
Bounded ASCII string length (cap k_dfu_print_cap).
static void dfu_route_usb_or_halt(void)
Route both ports' pins: FS as device, HS as host.
static ra8_err_t dfu_print(const char *text)
Print a NUL-terminated ASCII string over the console.
static volatile uint32_t s_dbg_host_err
Host-ladder first failing return code (0 = none).
dfu_hex_t
Hex/decimal text-formatter sizing constants.
@ k_dfu_hex_chars_u32
32-bit value -> "ABCDEF01".
@ k_dfu_hex_digit_split
Threshold between '0-9'/'A-F'.
@ k_dfu_nibble_bits
Bits per hex nibble.
dfu_mask_t
Bit-mask constant used by the hex formatter.
@ k_dfu_nibble_mask
4-bit nibble mask.
Board-support layer for the Renesas EK-RA8D2 v1 evaluation kit.
ra8_err_t ra8_board_led_init(ra8_board_led_id_t led)
Configure led as a digital output, initial level low (off).
ra8_err_t ra8_board_led_on(ra8_board_led_id_t led)
Drive led HIGH (light it).
@ 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_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.
@ 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.
@ k_ra8_clock_id_cpuclk0
Cortex-M85 CPUCLK0.
ra8_err_t ra8_cgc_init(void)
Configure the clock tree to a safe default.
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.
Controller-agnostic USB-DFU MRAM bootloader core for the RA8D2.
@ k_ra8_dfu_slot_a
Slot A (0x02020000).
bool ra8_dfu_slot_valid(ra8_dfu_slot_t slot)
Validate a slot live: header magic/length/CRC over its real image.
USBX DFU device class wired to real MRAM, bound to either controller.
ra8_err_t ra8_dfu_device_last_error(void)
Latched last program error (diagnostic).
ra8_err_t ra8_dfu_device_start(ra8_usb_speed_t speed, void *usbx_pool, uint32_t pool_bytes, unsigned char *framework, uint32_t framework_len, unsigned char *strings, uint32_t strings_len, unsigned char *langids, uint32_t langids_len)
Bring up USBX + the DFU class on one controller and raise D+.
void ra8_dfu_device_set_target(ra8_dfu_slot_t target_slot)
Select the slot DFU_DNLOAD programs into / DFU_UPLOAD reads from.
ra8_err_t ra8_dfu_device_worker_step(void)
Program any pending DNLOAD block into MRAM; commit on end-of-download.
bool ra8_dfu_device_committed(void)
Whether the image header has been committed (slot now bootable).
Polled on-board USB-DFU host driver, bound to either controller.
ra8_err_t ra8_dfu_host_program(ra8_usb_speed_t host_speed, const uint8_t *img, uint32_t img_len, ra8_dfu_host_result_t *out)
Enumerate, DFU_DNLOAD img, then a zero-length DFU_DNLOAD (manifest).
Error Code Definitions for ra8-firmware.
@ k_ra8_err_crc_mismatch
CRC mismatch detected on received data.
@ k_ra8_err_hw_timeout
Hardware timed out waiting for a flag or handshake.
@ k_ra8_ok
Success – operation completed with all postconditions satisfied.
ra8_err_codes_t ra8_err_t
Canonical error-return type used by every ra8-firmware API.
PSEL codes for peripheral pin routing on the RA8D2.
@ k_ra8_psel_usb_fs
10011b: USB Full-Speed.
@ 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).
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.
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.
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.
void ra8_time_on_tick(void)
SysTick IRQ handler – called from the vector table.
Native USB controller driver public API (device + host modes).
@ k_ra8_usb_speed_hs
High-Speed controller (USBHS @ 0x40351000).
@ k_ra8_usb_speed_fs
Full-Speed controller (USBFS @ 0x40250000).
unsigned int UINT
ThreadX-compatible unsigned int (host stub).
char CHAR
ThreadX-compatible CHAR (host stub).
unsigned long ULONG
ThreadX-compatible unsigned long (host stub).
Opaque thread stand-in for the host build.
Outcome of a DFU round-trip for diagnostics / banners.
uint32_t pid
Enumerated idProduct.
static volatile uint32_t s_dbg_phase
Host-ladder phase marker (wlun_phase_t).
static volatile uint32_t s_dbg_pass_count
Completed full passes (sticky success counter).
USBX device-controller-driver (DCD) bridge to ra8_usb.