70#ifdef RA8_ENABLE_ROOT_OF_TRUST
71#include "mbedtls/memory_buffer_alloc.h"
79#include "ux_device_class_dfu.h"
80#include "ux_device_stack.h"
132typedef enum : uint32_t {
146typedef enum : uint8_t {
153typedef enum : uint32_t {
165typedef enum : uintptr_t {
170typedef enum : uint32_t {
186#ifndef RA8_OFF_TARGET
317typedef enum : uint8_t {
332typedef enum : uint32_t {
387 return (uint8_t)((uint8_t)
'0' + (uint8_t)nibble);
408 if (text[len] ==
'\0') {
484 if (out_target ==
nullptr) {
539#ifndef RA8_OFF_TARGET
552 __asm__
volatile(
"dsb 0xF" :::
"memory");
556 __asm__
volatile(
"dsb 0xF" :::
"memory");
560 __asm__
volatile(
"wfi");
590 (void)
blc_print(
"dfu-bootloader: FAIL device bring-up\r\n");
594 (void)
blc_print(
"dfu-bootloader: DFU device up on USB-FS, awaiting DFU_DNLOAD...\r\n");
602 (void)
blc_print(
"dfu-bootloader: image committed -- resetting into new slot\r\n");
623 (void)first_unused_memory;
656 __asm__
volatile(
"wfi");
686#ifdef RA8_ENABLE_ROOT_OF_TRUST
691typedef enum : uint32_t {
692 k_blc_psa_heap_bytes = 0x10000U,
704static uint8_t s_blc_psa_heap[k_blc_psa_heap_bytes];
719 uint32_t cpuclk0_hz = 0U;
720 uint32_t pclka_hz = 0U;
755#ifdef RA8_ENABLE_ROOT_OF_TRUST
760 mbedtls_memory_buffer_alloc_init(s_blc_psa_heap,
sizeof(s_blc_psa_heap));
782 (void)
blc_print(
"\r\nra8d2 dfu-bootloader (immutable, 128K @ 0x02000000)\r\n");
790 (void)
blc_print(
"dfu-bootloader: Slot A -> copy-to-run @0x");
796 (void)
blc_print(
"dfu-bootloader: Slot B -> copy-to-run @0x");
804 (void)
blc_print(
"dfu-bootloader: no bootable slot -- entering DFU\r\n");
805#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 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 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 volatile uint32_t s_dbg_dev_err
Device worker first failing return code (0 = none).
static CHAR s_device_thread_name[]
blc_reset_t
AIRCR write to request a system reset (VECTKEY 0x05FA | SYSRESETREQ).
@ k_blc_aircr_sysreset
0x05FA<<16 | (1<<2).
static volatile uint32_t s_dbg_action
Boot decision outcome (ra8_dfu_action_t), J-Link-readable.
blc_mask_t
4-bit nibble mask for the hex formatter.
@ k_blc_nibble_mask
4-bit nibble mask.
blc_dbg_t
J-Link debug-probe "not yet written" sentinel.
@ k_blc_dbg_unset
Probe word value before main writes it.
static ra8_err_t blc_print(const char *text)
Print a NUL-terminated ASCII string over SCI8 (polled).
static const ra8_port_pin_t k_blc_pin_sci_rx
J-Link OB CDC RX pin (PD_03 – SCI8 RX).
blc_scb_t
Cortex-M85 System Control Block address used by the DFU-commit reset.
@ k_blc_scb_aircr_addr
SCB->AIRCR (reset control).
static VOID blc_device_worker(ULONG arg)
DFU device worker: bring the DFU class up, drain commits, reset on done.
static const ra8_port_pin_t k_blc_pin_fs_dm
USBFS D- (P8_15).
static void blc_panic_halt(void)
Halt forever in WFI – panic stop on init failure.
static UCHAR s_device_framework[]
USBX device + DFU descriptors: a DFU-mode interface (bInterfaceProtocol 0x02, enumerates straight int...
static void blc_system_reset(void)
Request a system reset via AIRCR.SYSRESETREQ.
static void blc_route_usb_or_halt(void)
Route the USB-FS device pins (P4_07/P5_00/P8_14/P8_15).
static const ra8_port_pin_t k_blc_pin_sci_tx
J-Link OB CDC TX pin (PD_02 – SCI8 TX).
static uint8_t blc_nibble_to_hex(uint32_t nibble)
Format one nibble (0..15) into an uppercase hex character.
static const ra8_port_pin_t k_blc_pin_fs_vbusen
USBFS VBUSEN (P5_00) – GPIO LOW for the device role.
static const ra8_port_pin_t k_blc_pin_fs_dp
USBFS D+ (P8_14).
static volatile uint32_t s_dbg_target
Slot the DFU path targets (ra8_dfu_slot_t), J-Link-readable.
static ra8_err_t blc_print_hex(uint32_t value)
Print a value as fixed-width (8-digit) uppercase hex.
static volatile uint32_t g_dfu_trigger
No-init SRAM word an application writes (== k_ra8_dfu_trigger_magic) before resetting to request the ...
blc_hex_t
Hex text-formatter sizing constants.
@ k_blc_nibble_bits
Bits per hex nibble.
@ k_blc_hex_chars_u32
32-bit value -> "ABCDEF01".
@ k_blc_hex_digit_split
Threshold between '0-9'/'A-F'.
static void blc_setup_or_halt(void)
Bring CGC + USB-FS clock + SysTick + SCI8 + LEDs + pins up.
static ra8_dfu_action_t blc_decide(ra8_dfu_slot_t *out_target)
Read the trigger + both slots and resolve the reset-time action.
static const ra8_port_pin_t k_blc_pin_fs_vbus
USBFS VBUS sense pin (P4_07, PSEL = 0x13).
blc_config_t
Compile-time settings: thread, pool, console, cadence.
@ k_blc_baud
J-Link OB CDC log baud.
@ k_blc_sci_channel
SCI8 -> J-Link OB CDC bridge.
@ k_blc_usbx_pool_bytes
USBX memory pool (bytes).
@ k_blc_thread_stack
Device worker stack (bytes).
@ k_blc_idle_ticks
Worker back-off (1 ms ticks).
@ k_blc_print_cap
Bound for console-string scans.
@ k_blc_dev_priority
Device bring-up worker priority.
volatile uint32_t g_blc_dfu_tick
HIL liveness counter – advanced once per DFU device service step.
static void blc_boot_slot(ra8_dfu_slot_t slot)
Copy a validated slot's image to the SRAM run base and launch it.
static uint32_t blc_str_len(const char *text)
Bounded ASCII string length (cap k_blc_print_cap).
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).
@ k_ra8_board_led1
LED1, BLUE, P600 (jumper E27).
@ k_ra8_board_uart_console_pin_txd
PD02 TXD.
@ k_ra8_board_uart_console_pin_rxd
PD03 RXD.
@ 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.
@ k_ra8_clock_id_pclka
PCLKA.
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).
Controller-agnostic USB-DFU MRAM bootloader core for the RA8D2.
ra8_err_t ra8_dfu_slot_seq(ra8_dfu_slot_t slot, uint32_t *out_seq)
Read a slot header's sequence number (0 if the magic is wrong).
ra8_dfu_slot_t
Application-slot identifier.
@ k_ra8_dfu_slot_b
Slot B (0x02090000).
@ k_ra8_dfu_slot_a
Slot A (0x02020000).
@ k_ra8_dfu_slot_none
No valid slot present.
ra8_dfu_slot_t ra8_dfu_select_slot(bool a_valid, uint32_t a_seq, bool b_valid, uint32_t b_seq)
Pick the active slot from the two slots' validity + sequence.
ra8_dfu_action_t
Outcome of the reset-time boot decision.
@ k_ra8_dfu_action_jump_b
Jump to the Slot B application.
@ k_ra8_dfu_action_dfu
Enter the DFU device; do not jump.
@ k_ra8_dfu_action_jump_a
Jump to the Slot A application.
@ k_ra8_dfu_trigger_magic
No-init SRAM DFU-request magic.
ra8_err_t ra8_dfu_read_header(ra8_dfu_slot_t slot, ra8_dfu_img_hdr_t *out_hdr)
Copy a slot's 32-byte header out of MRAM.
uintptr_t ra8_dfu_slot_base(ra8_dfu_slot_t slot)
Return the MRAM base address of a slot.
ra8_dfu_action_t ra8_dfu_boot_decide(bool dfu_trigger, bool a_valid, uint32_t a_seq, bool b_valid, uint32_t b_seq)
Reset-time boot decision: jump to a slot, or enter DFU.
ra8_dfu_slot_t ra8_dfu_other_slot(ra8_dfu_slot_t slot)
Return the opposite slot (A<->B).
@ k_ra8_dfu_run_base
SRAM copy-to-run / payload link base.
void ra8_dfu_launch(uintptr_t src, uint32_t img_len, uint32_t entry)
Copy an image to the SRAM run base and branch to it (copy-to-run).
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).
Error Code Definitions for ra8-firmware.
@ 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_sci_async
00100b: SCI async serial (UART).
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_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.
Application-level PSA Crypto facade over tf-psa-crypto.
ra8_err_t ra8_psa_crypto_init(void)
One-shot facade initialisation.
Full-featured Serial Communications Interface driver.
ra8_err_t ra8_sci_write_polling(uint8_t channel, const uint8_t *data, uint32_t len)
Send len bytes by polling (convenience wrapper).
@ k_ra8_sci_stop_1
RA8 SCI stop 1.
@ k_ra8_sci_parity_none
RA8 SCI parity none.
ra8_err_t ra8_sci_init(uint8_t channel, const ra8_sci_cfg_t *cfg)
Initialise an SCI channel using the descriptor.
@ k_ra8_sci_data_8
RA8 SCI data 8.
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_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.
32-byte application-image header at the base of each slot.
uint32_t entry
SRAM run base (== k_ra8_dfu_run_base).
uint32_t img_len
Image body length in bytes (32-byte multiple).
Configuration descriptor for ra8_sci_init.
USBX device-controller-driver (DCD) bridge to ra8_usb.