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

Real USB-DFU MRAM bootloader: boot the active app slot, or accept a DFU update. More...

#include <stdint.h>
#include "ra8_board_ek_ra8d2.h"
#include "ra8_boot_entry.h"
#include "ra8_cgc.h"
#include "ra8_dfu.h"
#include "ra8_dfu_device.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.h"
#include "ra8_time.h"
#include "ra8_usb.h"
#include "tx_api.h"
#include "ux_api.h"
#include "ux_dcd_ra8_usb.h"
#include "ux_device_class_dfu.h"
#include "ux_device_stack.h"
Include dependency graph for main.c:

Go to the source code of this file.

Enumerations

enum  blc_config_t : uint32_t {
  k_blc_thread_stack = 4096U ,
  k_blc_usbx_pool_bytes = 32768U ,
  k_blc_idle_ticks = 50U ,
  k_blc_baud = 115200U ,
  k_blc_sci_channel = 8U ,
  k_blc_print_cap = 160U ,
  k_blc_dev_priority = 8U
}
 Compile-time settings: thread, pool, console, cadence. More...
enum  blc_hex_t : uint8_t {
  k_blc_hex_chars_u32 = 8U ,
  k_blc_nibble_bits = 4U ,
  k_blc_hex_digit_split = 10U
}
 Hex text-formatter sizing constants. More...
enum  blc_mask_t : uint32_t { k_blc_nibble_mask = 0xFU }
 4-bit nibble mask for the hex formatter. More...
enum  blc_scb_t : uintptr_t { k_blc_scb_aircr_addr = 0xE000ED0CU }
 Cortex-M85 System Control Block address used by the DFU-commit reset. More...
enum  blc_reset_t : uint32_t { k_blc_aircr_sysreset = 0x05FA0004U }
 AIRCR write to request a system reset (VECTKEY 0x05FA | SYSRESETREQ). More...
enum  usb_langid_byte_t : uint8_t {
  k_usb_langid_en_us_lo = 0x09U ,
  k_usb_langid_en_us_hi = 0x04U
}
 USBX LANGID descriptor 0x0409 (English-US), little-endian byte pair. More...
enum  blc_dbg_t : uint32_t { k_blc_dbg_unset = 0xFFFFFFFFU }
 J-Link debug-probe "not yet written" sentinel. More...

Functions

void _tx_timer_interrupt (void)
 ThreadX 1 ms tick worker.
void SysTick_Handler (void)
 Service the SysTick exception selected by the linked application.
static uint8_t blc_nibble_to_hex (uint32_t nibble)
 Format one nibble (0..15) into an uppercase hex character.
static uint32_t blc_str_len (const char *text)
 Bounded ASCII string length (cap k_blc_print_cap).
static ra8_err_t blc_print (const char *text)
 Print a NUL-terminated ASCII string over SCI8 (polled).
static ra8_err_t blc_print_hex (uint32_t value)
 Print a value as fixed-width (8-digit) uppercase hex.
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 void blc_boot_slot (ra8_dfu_slot_t slot)
 Copy a validated slot's image to the SRAM run base and launch it.
static void blc_system_reset (void)
 Request a system reset via AIRCR.SYSRESETREQ.
static VOID blc_device_worker (ULONG arg)
 DFU device worker: bring the DFU class up, drain commits, reset on done.
VOID tx_application_define (VOID *first_unused_memory)
 ThreadX application-define hook.
static void blc_panic_halt (void)
 Halt forever in WFI – panic stop on init failure.
static void blc_route_usb_or_halt (void)
 Route the USB-FS device pins (P4_07/P5_00/P8_14/P8_15).
static void blc_setup_or_halt (void)
 Bring CGC + USB-FS clock + SysTick + SCI8 + LEDs + pins up.
void main (void)
 Application entry: decide, then boot a slot or enter the DFU device.

Variables

static volatile bool s_tx_kernel_up = false
 Gates ThreadX tick delivery until tx_application_define has run.
static const ra8_port_pin_t k_blc_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_blc_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_blc_pin_fs_dp = (ra8_port_pin_t)k_ra8_board_usbfs_pin_dp
 USBFS D+ (P8_14).
static const ra8_port_pin_t k_blc_pin_fs_dm = (ra8_port_pin_t)k_ra8_board_usbfs_pin_dm
 USBFS D- (P8_15).
static const ra8_port_pin_t k_blc_pin_sci_tx = (ra8_port_pin_t)k_ra8_board_uart_console_pin_txd
 J-Link OB CDC TX pin (PD_02 – SCI8 TX).
static const ra8_port_pin_t k_blc_pin_sci_rx = (ra8_port_pin_t)k_ra8_board_uart_console_pin_rxd
 J-Link OB CDC RX pin (PD_03 – SCI8 RX).
static volatile uint32_t g_dfu_trigger
 No-init SRAM word an application writes (== k_ra8_dfu_trigger_magic) before resetting to request the bootloader's DFU mode.
static TX_THREAD s_device_thread
 ThreadX TCB for the USBX device-side worker thread.
static UCHAR s_device_stack [k_blc_thread_stack]
 Stack backing storage for s_device_thread.
static UCHAR s_usbx_pool [k_blc_usbx_pool_bytes]
 USBX memory pool (USBX uses tx_byte_pool internally).
static UCHAR s_device_framework []
 USBX device + DFU descriptors: a DFU-mode interface (bInterfaceProtocol 0x02, enumerates straight into dfuIDLE) + the DFU functional descriptor (CAN_DNLOAD | CAN_UPLOAD, wTransferSize 64).
static UCHAR s_string_framework []
 USBX string descriptor table (vendor / product / serial).
static UCHAR s_language_id_framework [] = {k_usb_langid_en_us_lo, k_usb_langid_en_us_hi}
 USBX language-id table – US English.
static volatile uint32_t s_dbg_action = k_blc_dbg_unset
 Boot decision outcome (ra8_dfu_action_t), J-Link-readable.
static volatile uint32_t s_dbg_target = k_blc_dbg_unset
 Slot the DFU path targets (ra8_dfu_slot_t), J-Link-readable.
static volatile uint32_t s_dbg_dev_step
 Device worker progress: 4 = DFU class attached.
static volatile uint32_t s_dbg_dev_err
 Device bring-up error (0 = none).
volatile uint32_t g_blc_dfu_tick = 0U
 HIL liveness counter – advanced once per DFU device service step.

Detailed Description

Real USB-DFU MRAM bootloader: boot the active app slot, or accept a DFU update.

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

The immutable first 128 KiB of code-MRAM. At every reset it:

  1. Reads the no-init SRAM DFU trigger word (one-shot, cleared after read).
  2. Validates both application slots (magic + length + software CRC32 over the image body) and reads their monotonic sequence numbers.
  3. Runs the pure ra8_dfu_boot_decide policy: trigger set OR neither slot valid -> stay in DFU; otherwise jump to the valid slot with the higher sequence number (Slot A on a tie).

On a JUMP it copies the slot's image body to a fixed SRAM run base (k_ra8_dfu_run_base), points VTOR there, loads the image's initial MSP, and branches to its reset vector – a same-world (Secure) hand-off. Copy-to-run means an app is linked ONCE at the run base and the same image boots from either slot (no per-slot build). On DFU it brings up the USBX DFU device (libs/ra8_dfu) on USB-FS (J11), programs the INACTIVE slot as the host DFU_DNLOADs it, and soft-resets once the image header is committed so the next boot decision selects the freshly written slot.

Brick-safety: the bootloader region is never erased, a freshly written bad slot fails its CRC (so the older valid slot still boots), and SWD re-flash of the bootloader is always available. The DFU path writes only the inactive slot – no BTFLG, no option-setting, nothing irreversible.

Note
The console is hand-rolled (raw SCI8, not the BSP ra8_board_uart_console API) on purpose: this immutable bootloader keeps its dependency surface minimal and self-contained for brick-safety, so it does not pull in the board-support library.
An app is copied to SRAM and run there, so it is linked at the single k_ra8_dfu_run_base (linker ORIGIN), not at a slot base; the identical image boots from either slot. See this app's README for the run base and the staging procedure.

Pinout (USB-FS device + console only)

FS device: P4_07 VBUS sense, P5_00 VBUSEN GPIO LOW (device role; the USB host supplies VBUS), P8_14 D+, P8_15 D- (PSEL usb_fs). Console: PD_02/PD_03 SCI8 (PSEL sci_async, 115200 to the J-Link OB CDC).

Author
Brighton Sikarskie
Date
2026-06-16
Since
0.1.0

Definition in file main.c.

Enumeration Type Documentation

◆ blc_config_t

enum blc_config_t : uint32_t

Compile-time settings: thread, pool, console, cadence.

Enumerator
k_blc_thread_stack 

Device worker stack (bytes).

k_blc_usbx_pool_bytes 

USBX memory pool (bytes).

k_blc_idle_ticks 

Worker back-off (1 ms ticks).

k_blc_baud 

J-Link OB CDC log baud.

k_blc_sci_channel 

SCI8 -> J-Link OB CDC bridge.

k_blc_print_cap 

Bound for console-string scans.

k_blc_dev_priority 

Device bring-up worker priority.

Definition at line 132 of file main.c.

◆ blc_dbg_t

enum blc_dbg_t : uint32_t

J-Link debug-probe "not yet written" sentinel.

Enumerator
k_blc_dbg_unset 

Probe word value before main writes it.

Definition at line 332 of file main.c.

◆ blc_hex_t

enum blc_hex_t : uint8_t

Hex text-formatter sizing constants.

Enumerator
k_blc_hex_chars_u32 

32-bit value -> "ABCDEF01".

k_blc_nibble_bits 

Bits per hex nibble.

k_blc_hex_digit_split 

Threshold between '0-9'/'A-F'.

Definition at line 146 of file main.c.

◆ blc_mask_t

enum blc_mask_t : uint32_t

4-bit nibble mask for the hex formatter.

Enumerator
k_blc_nibble_mask 

4-bit nibble mask.

Definition at line 153 of file main.c.

◆ blc_reset_t

enum blc_reset_t : uint32_t

AIRCR write to request a system reset (VECTKEY 0x05FA | SYSRESETREQ).

Enumerator
k_blc_aircr_sysreset 

0x05FA<<16 | (1<<2).

Definition at line 170 of file main.c.

◆ blc_scb_t

enum blc_scb_t : uintptr_t

Cortex-M85 System Control Block address used by the DFU-commit reset.

Accessed by absolute address (the project does not vendor a CMSIS SCB struct). AIRCR is the Secure alias since the bootloader runs Secure. The copy-to-run hand-off's VTOR write lives in ra8_dfu_launch.

Enumerator
k_blc_scb_aircr_addr 

SCB->AIRCR (reset control).

Definition at line 165 of file main.c.

◆ usb_langid_byte_t

enum usb_langid_byte_t : uint8_t

USBX LANGID descriptor 0x0409 (English-US), little-endian byte pair.

Enumerator
k_usb_langid_en_us_lo 

LANGID 0x0409 low byte.

k_usb_langid_en_us_hi 

LANGID 0x0409 high byte.

Definition at line 317 of file main.c.

Function Documentation

◆ _tx_timer_interrupt()

void _tx_timer_interrupt ( void )
extern

ThreadX 1 ms tick worker.

◆ blc_boot_slot()

void blc_boot_slot ( ra8_dfu_slot_t slot)
static

Copy a validated slot's image to the SRAM run base and launch it.

Reads the slot header for its img_len / entry, then delegates to the shared ra8_dfu_launch, which cross-checks the run target, copies the image body from the slot to k_ra8_dfu_run_base in SRAM, and branches to it. Because an app is linked ONCE at the run base, the same image boots from either slot. ra8_dfu_launch returns here only when the run target fails validation (a corrupted entry/length), in which case main drops to DFU.

Parameters
[in]slotSlot to boot (A or B); already passed ra8_dfu_slot_valid.
Returns
void – returns to the caller ONLY when the run target is invalid (so main drops to DFU); on a valid image it does not return.
Precondition
slot is A or B and passed CRC validation in blc_decide.
The image was linked at k_ra8_dfu_run_base (its header entry records it).
Postcondition
On a valid image, control is in the application at the run base.
On an invalid run target, no copy/jump happens and control returns.
Note
Not thread-safe; the final hand-off of a single-threaded boot.
Since
0.1.0

Definition at line 529 of file main.c.

References ra8_dfu_img_hdr_t::entry, ra8_dfu_img_hdr_t::img_len, k_ra8_ok, ra8_dfu_launch(), ra8_dfu_read_header(), and ra8_dfu_slot_base().

Referenced by main().

◆ blc_decide()

ra8_dfu_action_t blc_decide ( ra8_dfu_slot_t * out_target)
static

Read the trigger + both slots and resolve the reset-time action.

Reads and clears the one-shot g_dfu_trigger, validates both slots (CRC over the body) and their sequence numbers, then folds them through the pure ra8_dfu_boot_decide policy. The DFU target (the slot to program) is the INACTIVE slot, or Slot A when neither slot is valid.

Parameters
[out]out_targetSlot the DFU path should program. Non-NULL.
Returns
The resolved ra8_dfu_action_t.
Return values
k_ra8_dfu_action_jump_aBoot Slot A.
k_ra8_dfu_action_jump_bBoot Slot B.
k_ra8_dfu_action_dfuStay in the DFU device.
Precondition
out_target is non-NULL.
Both slots are readable code-MRAM (always true post-reset).
Postcondition
g_dfu_trigger has been cleared to 0.
*out_target holds the inactive slot (or Slot A).
Note
Pure reads only – no ra8_flash_open, no programming.
Since
0.1.0

Definition at line 482 of file main.c.

References g_dfu_trigger, k_ra8_dfu_action_dfu, k_ra8_dfu_slot_a, k_ra8_dfu_slot_b, k_ra8_dfu_slot_none, k_ra8_dfu_trigger_magic, ra8_dfu_boot_decide(), ra8_dfu_other_slot(), ra8_dfu_select_slot(), ra8_dfu_slot_seq(), and ra8_dfu_slot_valid().

Referenced by main().

◆ blc_device_worker()

VOID blc_device_worker ( ULONG arg)
static

DFU device worker: bring the DFU class up, drain commits, reset on done.

Parameters
[in]argThreadX entry argument (unused).
Returns
Never returns.
Precondition
ra8_dfu_device_set_target selected the inactive slot (main did it).
USB-FS pins + clock are up (main did both).
Postcondition
The FS device is attached in DFU mode; commits trigger a system reset.
On any bring-up failure the thread parks (s_dbg_dev_err set).
Note
The DFU class runs its own thread; this worker only services it.
Since
0.1.0

Definition at line 575 of file main.c.

References blc_print(), blc_system_reset(), g_blc_dfu_tick, k_blc_idle_ticks, k_ra8_ok, k_ra8_usb_speed_fs, ra8_dfu_device_committed(), ra8_dfu_device_last_error(), ra8_dfu_device_start(), ra8_dfu_device_worker_step(), s_dbg_dev_err, s_dbg_dev_step, s_device_framework, s_language_id_framework, s_string_framework, s_usbx_pool, and tx_thread_sleep.

Referenced by tx_application_define().

◆ blc_nibble_to_hex()

uint8_t blc_nibble_to_hex ( uint32_t nibble)
static

Format one nibble (0..15) into an uppercase hex character.

Parameters
[in]nibble4-bit value.
Returns
ASCII '0'..'9' or 'A'..'F'.
Return values
'0'For a zero nibble.
Precondition
Caller has masked the value to 4 bits.
None beyond the mask contract.
Postcondition
Returned byte is printable hex.
No state changes.
Note
Pure function.
Since
0.1.0

Definition at line 384 of file main.c.

References k_blc_hex_digit_split.

Referenced by blc_print_hex().

◆ blc_panic_halt()

void blc_panic_halt ( void )
static

Halt forever in WFI – panic stop on init failure.

Returns
void.
Precondition
Called only after a fatal boot error.
Interrupts may be in any state.
Postcondition
CPU is parked.
No further code runs.
Note
Not reachable post-boot.
Since
0.1.0

Definition at line 652 of file main.c.

Referenced by blc_route_usb_or_halt(), blc_setup_or_halt(), and main().

◆ blc_print()

ra8_err_t blc_print ( const char * text)
staticnodiscard

Print a NUL-terminated ASCII string over SCI8 (polled).

Parameters
[in]textString to print (CR/LF included by the caller).
Returns
ra8_err_t propagated from ra8_sci_write_polling.
Return values
k_ra8_okAll bytes queued.
Precondition
SCI8 init already ran; text is non-NULL.
text is NUL-terminated within k_blc_print_cap bytes.
Postcondition
The string bytes are in the SCI8 TX FIFO.
No other state changes.
Note
Blocking polled TX.
Since
0.1.0

Definition at line 428 of file main.c.

References blc_str_len(), k_blc_sci_channel, and ra8_sci_write_polling().

Referenced by blc_device_worker(), and main().

◆ blc_print_hex()

ra8_err_t blc_print_hex ( uint32_t value)
staticnodiscard

Print a value as fixed-width (8-digit) uppercase hex.

Parameters
[in]valueValue to print.
Returns
ra8_err_t propagated from the SCI helper.
Return values
k_ra8_okAll bytes queued.
Precondition
SCI8 init already ran.
None beyond console readiness.
Postcondition
One 8-character hex token is in the SCI8 TX FIFO.
No other state changes.
Note
Blocking polled TX.
Since
0.1.0

Definition at line 445 of file main.c.

References blc_nibble_to_hex(), k_blc_hex_chars_u32, k_blc_nibble_bits, k_blc_nibble_mask, k_blc_sci_channel, and ra8_sci_write_polling().

Referenced by main().

◆ blc_route_usb_or_halt()

void blc_route_usb_or_halt ( void )
static

Route the USB-FS device pins (P4_07/P5_00/P8_14/P8_15).

Returns
void.
Precondition
IOPORT is reachable.
Called once from blc_setup_or_halt.
Postcondition
FS carries the device role (VBUSEN LOW; the host supplies VBUS).
Panic-halts on any routing failure.
Note
Panic-halts on any routing failure.
Since
0.1.0

Definition at line 670 of file main.c.

References blc_panic_halt(), k_blc_pin_fs_dm, k_blc_pin_fs_dp, k_blc_pin_fs_vbus, k_blc_pin_fs_vbusen, k_ra8_level_low, k_ra8_ok, k_ra8_psel_usb_fs, ra8_gpio_output_init(), and ra8_pfs_route_peripheral().

Referenced by blc_setup_or_halt().

◆ blc_setup_or_halt()

void blc_setup_or_halt ( void )
static

Bring CGC + USB-FS clock + SysTick + SCI8 + LEDs + pins up.

Returns
void.
Precondition
Reset_Handler finished C runtime init.
SystemInit has run.
Postcondition
Console works; the USB-FS pins and clock are live.
Panic-halts on any failure.
Note
Called once from main.
Since
0.1.0

Definition at line 717 of file main.c.

References blc_panic_halt(), blc_route_usb_or_halt(), k_blc_baud, k_blc_pin_sci_rx, k_blc_pin_sci_tx, k_blc_sci_channel, k_ra8_board_led1, k_ra8_clock_id_cpuclk0, k_ra8_clock_id_pclka, k_ra8_ok, k_ra8_psel_sci_async, k_ra8_sci_data_8, k_ra8_sci_parity_none, k_ra8_sci_stop_1, ra8_board_led_init(), ra8_cgc_get_clock_hz(), ra8_cgc_init(), ra8_cgc_usbfs_clock_enable(), ra8_pfs_route_peripheral(), ra8_psa_crypto_init(), ra8_sci_init(), and ra8_time_init().

Referenced by main().

◆ blc_str_len()

uint32_t blc_str_len ( const char * text)
static

Bounded ASCII string length (cap k_blc_print_cap).

Parameters
[in]textNUL-terminated string.
Returns
Number of bytes before the NUL, capped.
Return values
0For an empty string.
Precondition
text is non-NULL with readable storage.
text fits the cap.
Postcondition
No state changes.
Return value never exceeds k_blc_print_cap.
Note
Bounded scan.
Since
0.1.0

Definition at line 404 of file main.c.

References k_blc_print_cap.

Referenced by blc_print().

◆ blc_system_reset()

void blc_system_reset ( void )
static

Request a system reset via AIRCR.SYSRESETREQ.

Never returns.

Returns
Does not return.
Precondition
Called after a committed DFU image, to re-run the boot decision.
Caller accepts that volatile state is lost.
Postcondition
The core resets and re-enters Reset_Handler.
Note
Spins in WFI until the reset lands.
Since
0.1.0

Definition at line 550 of file main.c.

References k_blc_aircr_sysreset, and k_blc_scb_aircr_addr.

Referenced by blc_device_worker().

◆ main()

void main ( void )

Application entry: decide, then boot a slot or enter the DFU device.

The application entry point Reset_Handler hands control to.

Precondition
Reset_Handler copied .data and zeroed .bss.
SystemInit set VTOR, FPU, priority grouping.
Postcondition
Control passes to the active app, or to the ThreadX-hosted DFU device.
On any HAL init failure the function halts in WFI.
Note
Single entry point; not re-entrant.
Since
0.1.0

Definition at line 777 of file main.c.

References blc_boot_slot(), blc_decide(), blc_panic_halt(), blc_print(), blc_print_hex(), blc_setup_or_halt(), k_ra8_dfu_action_jump_a, k_ra8_dfu_action_jump_b, k_ra8_dfu_run_base, k_ra8_dfu_slot_a, k_ra8_dfu_slot_b, ra8_dfu_device_set_target(), ra8_isr_globals_enable(), s_dbg_action, and s_dbg_target.

◆ SysTick_Handler()

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.

Precondition
Entered for a SysTick exception with a valid exception frame.
The selected handler's timebase or scheduler state is initialised.
Postcondition
The selected implementation has processed one SysTick event.
The vector table itself remains unchanged.
Note
Runs in handler mode; thread-safety follows the selected implementation.
Since
0.1.0

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.

Precondition
The handler is entered for one SysTick event or a host-test equivalent.
On target, ra8_time_init() configured the SysTick period.
Postcondition
s_tick_ms has advanced by exactly one.
Optional subsystem hooks run only when linked and ready.
Note
IRQ-safe; the weak symbol may be overridden by an application handler.
Since
0.1.0

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.

Precondition
The handler is entered for one SysTick event or a host-test equivalent.
On target, ra8_time_init() configured the SysTick period.
Postcondition
s_tick_ms has advanced by exactly one.
Optional subsystem hooks run only when linked and ready.
Note
IRQ-safe; the weak symbol may be overridden by an application handler.
Since
0.1.0

Service the SysTick exception selected by the linked application.

Precondition
Called from exception context (IPSR == 15).
_tx_initialize_low_level has programmed SysTick.
Postcondition
One ThreadX tick elapsed; PendSV may be pending.

Definition at line 92 of file main.c.

References _tx_timer_interrupt(), ra8_time_on_tick(), s_tx_kernel_up, and ux_dcd_ra8_usb_irq_reenable().

◆ tx_application_define()

VOID tx_application_define ( VOID * first_unused_memory)

ThreadX application-define hook.

Spawns the single DFU device worker.

Parameters
[in]first_unused_memorySentinel (unused; static stacks).
Returns
void.
Precondition
Called from tx_kernel_enter after scheduler init.
The DFU target slot was selected before tx_kernel_enter.
Postcondition
One auto-start worker is queued; s_tx_kernel_up is true.
Note
Called once at boot; not thread-safe.
Since
0.1.0

Definition at line 619 of file main.c.

References blc_device_worker(), k_blc_dev_priority, k_blc_thread_stack, s_device_stack, s_device_thread, s_device_thread_name, s_tx_kernel_up, TX_AUTO_START, TX_NO_TIME_SLICE, and tx_thread_create.

Variable Documentation

◆ g_blc_dfu_tick

volatile uint32_t g_blc_dfu_tick = 0U

HIL liveness counter – advanced once per DFU device service step.

Flashed standalone (the HIL scenario), neither application slot is valid, so blc_decide deterministically returns k_ra8_dfu_action_dfu and control ends up in blc_device_worker's service loop. This counter is incremented on every pass of that loop, so a J-Link double-halt mem32 read (scripts/ hil_jlink_memprobe.sh, HIL_MODE=jlink_memprobe) sees it advance and proves the bootloader booted, decided "no bootable slot", and brought the USBX DFU device up – not merely that the chip is alive with PC in MRAM. A copy-to-run jump to a valid slot would leave this counter frozen (PC moves to the SRAM run window); the dfu_copy_to_run app gates that path instead.

Global (non-static) and volatile so the symbol stays linker-visible for nm and the increment is not optimized away.

Note
Read externally by J-Link only; firmware never reads it back.
Since
0.1.0

Definition at line 366 of file main.c.

Referenced by blc_device_worker().

◆ g_dfu_trigger

volatile uint32_t g_dfu_trigger
static

No-init SRAM word an application writes (== k_ra8_dfu_trigger_magic) before resetting to request the bootloader's DFU mode.

Lives in .noinit (survives a warm reset, never bss-cleared). The bootloader reads it once and clears it, so the request is one-shot.

Warning
Garbage on a cold boot; an exact magic match is required, so a stray value almost never (1 in 2^32) trips a false DFU entry.
Since
0.1.0

Definition at line 184 of file main.c.

Referenced by blc_decide().

◆ k_blc_pin_fs_dm

const ra8_port_pin_t k_blc_pin_fs_dm = (ra8_port_pin_t)k_ra8_board_usbfs_pin_dm
static

USBFS D- (P8_15).

Definition at line 116 of file main.c.

Referenced by blc_route_usb_or_halt().

◆ k_blc_pin_fs_dp

const ra8_port_pin_t k_blc_pin_fs_dp = (ra8_port_pin_t)k_ra8_board_usbfs_pin_dp
static

USBFS D+ (P8_14).

Definition at line 113 of file main.c.

Referenced by blc_route_usb_or_halt().

◆ k_blc_pin_fs_vbus

const ra8_port_pin_t k_blc_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 107 of file main.c.

Referenced by blc_route_usb_or_halt().

◆ k_blc_pin_fs_vbusen

const ra8_port_pin_t k_blc_pin_fs_vbusen = (ra8_port_pin_t)k_ra8_board_usbfs_pin_vbusen
static

USBFS VBUSEN (P5_00) – GPIO LOW for the device role.

Definition at line 110 of file main.c.

Referenced by blc_route_usb_or_halt().

◆ k_blc_pin_sci_rx

J-Link OB CDC RX pin (PD_03 – SCI8 RX).

Definition at line 122 of file main.c.

Referenced by blc_setup_or_halt().

◆ k_blc_pin_sci_tx

J-Link OB CDC TX pin (PD_02 – SCI8 TX).

Definition at line 119 of file main.c.

Referenced by blc_setup_or_halt().

◆ s_dbg_action

volatile uint32_t s_dbg_action = k_blc_dbg_unset
static

Boot decision outcome (ra8_dfu_action_t), J-Link-readable.

Definition at line 337 of file main.c.

Referenced by main().

◆ s_dbg_dev_err

volatile uint32_t s_dbg_dev_err
static

Device bring-up error (0 = none).

Definition at line 343 of file main.c.

◆ s_dbg_dev_step

volatile uint32_t s_dbg_dev_step
static

Device worker progress: 4 = DFU class attached.

Definition at line 341 of file main.c.

◆ s_dbg_target

volatile uint32_t s_dbg_target = k_blc_dbg_unset
static

Slot the DFU path targets (ra8_dfu_slot_t), J-Link-readable.

Definition at line 339 of file main.c.

Referenced by main().

◆ s_device_framework

UCHAR s_device_framework[]
static

USBX device + DFU descriptors: a DFU-mode interface (bInterfaceProtocol 0x02, enumerates straight into dfuIDLE) + the DFU functional descriptor (CAN_DNLOAD | CAN_UPLOAD, wTransferSize 64).

DFU runs over EP0.

Since
0.1.0

Definition at line 206 of file main.c.

Referenced by blc_device_worker(), dfu_class_register(), dfu_device_worker(), and dfu_usbx_stack_up().

◆ s_device_stack

UCHAR s_device_stack[k_blc_thread_stack]
static

Stack backing storage for s_device_thread.

Definition at line 195 of file main.c.

◆ s_device_thread

TX_THREAD s_device_thread
static

ThreadX TCB for the USBX device-side worker thread.

Definition at line 193 of file main.c.

◆ s_language_id_framework

UCHAR s_language_id_framework[] = {k_usb_langid_en_us_lo, k_usb_langid_en_us_hi}
static

USBX language-id table – US English.

Definition at line 323 of file main.c.

◆ s_string_framework

UCHAR s_string_framework[]
static

USBX string descriptor table (vendor / product / serial).

Since
0.1.0

Definition at line 263 of file main.c.

◆ s_tx_kernel_up

volatile bool s_tx_kernel_up = false
static

Gates ThreadX tick delivery until tx_application_define has run.

Since
0.1.0

Definition at line 89 of file main.c.

◆ s_usbx_pool

UCHAR s_usbx_pool[k_blc_usbx_pool_bytes]
static

USBX memory pool (USBX uses tx_byte_pool internally).

Definition at line 197 of file main.c.