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

USB self-loop: the board is its own dfu-util – DFU-flash + commit a bootable Slot-A image. More...

#include <stdint.h>
#include <string.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_dfu_host.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_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  dfu_config_t : uint32_t {
  k_dfu_thread_stack = 4096U ,
  k_dfu_host_stack = 8192U ,
  k_dfu_usbx_pool_bytes = 32768U ,
  k_dfu_idle_ticks = 50U ,
  k_dfu_boot_wait_ticks = 500U ,
  k_dfu_retry_ticks = 3000U ,
  k_dfu_baud = 115200U ,
  k_dfu_print_cap = 160U ,
  k_dfu_dev_priority = 8U ,
  k_dfu_host_priority = 24U ,
  k_dfu_commit_tries = 200U
}
 Compile-time settings: threads, pool, console, cadence. More...
enum  dfu_hex_t : uint8_t {
  k_dfu_hex_chars_u32 = 8U ,
  k_dfu_nibble_bits = 4U ,
  k_dfu_hex_digit_split = 10U
}
 Hex/decimal text-formatter sizing constants. More...
enum  dfu_mask_t : uint32_t { k_dfu_nibble_mask = 0xFU }
 Bit-mask constant used by the hex formatter. More...
enum  dfu_phase_t : uint32_t {
  k_dfu_phase_boot = 0U ,
  k_dfu_phase_init = 1U ,
  k_dfu_phase_enum = 2U ,
  k_dfu_phase_download = 3U ,
  k_dfu_phase_upload = 4U ,
  k_dfu_phase_pass = 5U
}
 J-Link probe values marking host-ladder progress. More...
enum  usb_langid_byte_t : uint8_t {
  k_usb_langid_en_us_lo = 0x09U ,
  k_usb_langid_en_us_hi = 0x04U
}
enum  dfu_req_t : uint16_t {
  k_dfu_bm_std_dev_in = 0x80U ,
  k_dfu_bm_std_dev_out = 0x00U ,
  k_dfu_bm_class_if_out = 0x21U ,
  k_dfu_bm_class_if_in = 0xA1U ,
  k_dfu_breq_get_desc = 0x06U ,
  k_dfu_breq_set_addr = 0x05U ,
  k_dfu_breq_set_config = 0x09U ,
  k_dfu_breq_dnload = 0x01U ,
  k_dfu_breq_upload = 0x02U ,
  k_dfu_breq_getstatus = 0x03U ,
  k_dfu_breq_abort = 0x06U ,
  k_dfu_desc_device = 0x01U ,
  k_dfu_dev_desc_len = 18U ,
  k_dfu_off_dev_pid = 10U ,
  k_dfu_byte_bits = 8U ,
  k_dfu_getstatus_len = 6U ,
  k_dfu_off_status_state = 4U ,
  k_dfu_state_dnload_idle = 5U ,
  k_dfu_state_idle = 2U
}
 Chapter-9 + DFU class request / descriptor constants. More...
enum  dfu_enum_tune_t : uint32_t {
  k_dfu_vbus_settle_ms = 200U ,
  k_dfu_attach_to_ms = 2000U ,
  k_dfu_debounce_ms = 500U ,
  k_dfu_reset_hold_ms = 50U ,
  k_dfu_recovery_ms = 20U ,
  k_dfu_addr_settle_ms = 5U ,
  k_dfu_status_poll_ms = 2U ,
  k_dfu_status_tries = 50U ,
  k_dfu_enum_tries = 8U ,
  k_dfu_attach_spin = 50000000U
}
 Timing / retry tunables for the polled enumeration + status polling. 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 VOID dfu_device_worker (ULONG arg)
 Device-side worker: bring the DFU device up, then park.
static uint8_t dfu_nibble_to_hex (uint32_t nibble)
 Format one nibble (0..15) into an uppercase hex character.
static uint32_t dfu_str_len (const char *text)
 Bounded ASCII string length (cap k_dfu_print_cap).
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 ra8_err_t dfu_print (const char *text)
 Print a NUL-terminated ASCII string over the console.
static ra8_err_t dfu_print_hex (uint32_t value, uint8_t digits)
 Print a value as fixed-width uppercase hex.
static ra8_err_t dfu_print_fail (const char *what, ra8_err_t err)
 Print "FAIL <what> err=0xNNNNNNNN" on its own line.
static ra8_err_t dfu_host_pass (void)
 Run the full host pass: enumerate, download, upload-verify.
static VOID dfu_host_worker (ULONG arg)
 Host-side worker: retry the full pass until it succeeds, then park.
VOID tx_application_define (VOID *first_unused_memory)
 ThreadX application-define hook.
static void dfu_panic_halt (void)
 Halt forever in WFI – panic stop on init failure.
static void dfu_route_usb_or_halt (void)
 Route both ports' pins: FS as device, HS as host.
static void dfu_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 so a pre-kernel SysTick (the U15 expander I2C blocks for ms during setup) cannot feed ThreadX's zeroed timer state.
static const ra8_port_pin_t k_dfu_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_dfu_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_dfu_pin_fs_dp = (ra8_port_pin_t)k_ra8_board_usbfs_pin_dp
 USBFS D+ (P8_14).
static const ra8_port_pin_t k_dfu_pin_fs_dm = (ra8_port_pin_t)k_ra8_board_usbfs_pin_dm
 USBFS D- (P8_15).
static const ra8_port_pin_t k_dfu_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_dfu_pin_hs_pwr = (ra8_port_pin_t)k_ra8_board_usbhs_pin_pwr
 J7 host-power switch (PD07): HIGH = U18 supplies VBUS.
static TX_THREAD s_device_thread
 ThreadX TCB for the USBX device-side worker thread.
static UCHAR s_device_stack [k_dfu_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_dfu_host_stack]
 Stack backing storage for s_host_thread.
static UCHAR s_usbx_pool [k_dfu_usbx_pool_bytes]
 USBX memory pool (USBX uses tx_byte_pool internally).
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, reset = entry+1) followed by a reset stub that writes the sentinel 0x600D600D to SRAM 0x22040000 and spins.
static volatile uint32_t s_dbg_phase
 Host-ladder phase marker (dfu_phase_t).
static volatile uint32_t s_dbg_pid
 Device-reported product id captured at enumeration.
static volatile uint32_t s_dbg_pass_count
 Completed full passes (sticky success counter).
static volatile uint32_t s_dbg_dev_writes
 Device-side download block-write count.
static volatile uint32_t s_dbg_dev_step
 Device worker progress: 1 stack, 2 class, 3 dcd, 4 attach.
static volatile uint32_t s_dbg_host_err
 Host-ladder first failing return code (0 = none).
static UCHAR s_device_framework []
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.

Detailed Description

USB self-loop: the board is its own dfu-util – DFU-flash + commit a bootable Slot-A image.

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

Validates the dfu_bootloader's REAL flash flow on-chip, with no external USB host: the two USB ports are cabled to EACH OTHER and one image runs both stacks, so the board plays the dfu-util host AND the DFU device.

  • USBFS (J11) = DEVICE: the ThreadX + USBX DFU class from libs/ra8_dfu, wired to real MRAM and targeting Slot A. It programs each DFU_DNLOAD block into the slot and, on the end-of-download manifest, commits the image header.
  • USBHS (J7) = HOST: the first-party polled host (ra8_dfu_host_program). It enumerates the device, DFU_DNLOADs a real bootable Slot-A payload (a vector table + a reset stub that writes the sentinel 0x600D600D to SRAM), then issues a zero-length DFU_DNLOAD (manifest) so the device commits.

After the manifest the driver waits for ra8_dfu_device_committed and confirms Slot A is bootable (ra8_dfu_slot_valid – CRC over the just-programmed body), then prints the pass banner. This exercises the program + COMMIT path the bootloader uses for a real dfu-util flash. The complementary jump – the dfu_bootloader booting this committed slot – is verified by flashing the bootloader and reading the sentinel over J-Link (see dfu_bootloader/README).

Verdicts stream over SCI8 (J-Link OB CDC console, 115200).

Pinout

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 (PSEL sci_async).

Author
Brighton Sikarskie
Date
2026-06-16
Since
0.1.0

Definition in file main.c.

Enumeration Type Documentation

◆ dfu_config_t

enum dfu_config_t : uint32_t

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

Enumerator
k_dfu_thread_stack 

Device worker stack (bytes).

k_dfu_host_stack 

Host worker stack (bytes).

k_dfu_usbx_pool_bytes 

USBX memory pool (bytes).

k_dfu_idle_ticks 

Parked-loop back-off (ticks).

k_dfu_boot_wait_ticks 

Host start delay (1 ms ticks).

k_dfu_retry_ticks 

Pause between ladder retries.

k_dfu_baud 

J-Link OB CDC log baud.

k_dfu_print_cap 

Bound for console-string scans.

k_dfu_dev_priority 

Device bring-up worker priority.

k_dfu_host_priority 

Host worker priority (below USBX).

k_dfu_commit_tries 

Polls awaiting the device commit.

Definition at line 120 of file main.c.

◆ dfu_enum_tune_t

enum dfu_enum_tune_t : uint32_t

Timing / retry tunables for the polled enumeration + status polling.

Enumerator
k_dfu_vbus_settle_ms 

VBUS settle before probing.

k_dfu_attach_to_ms 

Wait for the D+ pull-up.

k_dfu_debounce_ms 

Post-attach debounce (>=100 ms).

k_dfu_reset_hold_ms 

USB bus-reset hold (>=10 ms).

k_dfu_recovery_ms 

Post-reset recovery (TRSTRCY).

k_dfu_addr_settle_ms 

Post-SET_ADDRESS recovery.

k_dfu_status_poll_ms 

Pause between GETSTATUS polls.

k_dfu_status_tries 

GETSTATUS polls before giving up.

k_dfu_enum_tries 

Reset+probe attempts.

k_dfu_attach_spin 

Attach spin cap (frozen-tick guard).

Definition at line 571 of file main.c.

◆ dfu_hex_t

enum dfu_hex_t : uint8_t

Hex/decimal text-formatter sizing constants.

Enumerator
k_dfu_hex_chars_u32 

32-bit value -> "ABCDEF01".

k_dfu_nibble_bits 

Bits per hex nibble.

k_dfu_hex_digit_split 

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

Definition at line 138 of file main.c.

◆ dfu_mask_t

enum dfu_mask_t : uint32_t

Bit-mask constant used by the hex formatter.

Enumerator
k_dfu_nibble_mask 

4-bit nibble mask.

Definition at line 148 of file main.c.

◆ dfu_phase_t

enum dfu_phase_t : uint32_t

J-Link probe values marking host-ladder progress.

Enumerator
k_dfu_phase_boot 

Host thread not started.

k_dfu_phase_init 

Host controller init.

k_dfu_phase_enum 

Enumerating.

k_dfu_phase_download 

Running DFU_DNLOAD.

k_dfu_phase_upload 

Running DFU_UPLOAD.

k_dfu_phase_pass 

Image verified byte-equal.

Definition at line 156 of file main.c.

◆ dfu_req_t

enum dfu_req_t : uint16_t

Chapter-9 + DFU class request / descriptor constants.

Enumerator
k_dfu_bm_std_dev_in 

bmRequestType: Std | Device | In.

k_dfu_bm_std_dev_out 

bmRequestType: Std | Device | Out.

k_dfu_bm_class_if_out 

bmRequestType: Class | Interface | Out.

k_dfu_bm_class_if_in 

bmRequestType: Class | Interface | In.

k_dfu_breq_get_desc 

GET_DESCRIPTOR.

k_dfu_breq_set_addr 

SET_ADDRESS.

k_dfu_breq_set_config 

SET_CONFIGURATION.

k_dfu_breq_dnload 

DFU_DNLOAD.

k_dfu_breq_upload 

DFU_UPLOAD.

k_dfu_breq_getstatus 

DFU_GETSTATUS.

k_dfu_breq_abort 

DFU_ABORT (-> dfuIDLE).

k_dfu_desc_device 

DEVICE descriptor type.

k_dfu_dev_desc_len 

DEVICE descriptor length.

k_dfu_off_dev_pid 

idProduct LSB byte offset.

k_dfu_byte_bits 

Bits per byte.

k_dfu_getstatus_len 

DFU_GETSTATUS payload len.

k_dfu_off_status_state 

bState offset in GETSTATUS.

k_dfu_state_dnload_idle 

dfuDNLOAD-IDLE.

k_dfu_state_idle 

dfuIDLE.

Definition at line 545 of file main.c.

◆ usb_langid_byte_t

enum usb_langid_byte_t : uint8_t
Enumerator
k_usb_langid_en_us_lo 

LANGID 0x0409 low byte.

k_usb_langid_en_us_hi 

LANGID 0x0409 high byte.

Definition at line 339 of file main.c.

Function Documentation

◆ _tx_timer_interrupt()

void _tx_timer_interrupt ( void )
extern

ThreadX 1 ms tick worker.

◆ dfu_device_worker()

VOID dfu_device_worker ( ULONG arg)
static

Device-side worker: bring the DFU device up, then park.

Parameters
[in]argThreadX entry argument (unused).
Returns
Never returns.
Precondition
tx_application_define created this thread.
USB-FS pins + 48 MHz clock are up (main did both).
Postcondition
The FS device is attached in DFU mode; the DFU class services EP0.
On any bring-up failure the thread exits (s_dbg_dev_step frozen).
Note
The DFU class runs its own thread; this worker only brings it up.
Since
0.1.0

Definition at line 362 of file main.c.

References k_dfu_idle_ticks, k_ra8_dfu_slot_a, k_ra8_ok, k_ra8_usb_speed_fs, ra8_dfu_device_set_target(), ra8_dfu_device_start(), ra8_dfu_device_worker_step(), s_dbg_dev_step, s_dbg_host_err, s_device_framework, s_language_id_framework, s_string_framework, s_usbx_pool, and tx_thread_sleep.

Referenced by tx_application_define().

◆ dfu_host_pass()

ra8_err_t dfu_host_pass ( void )
staticnodiscard

Run the full host pass: enumerate, download, upload-verify.

Returns
First failing step's error, or k_ra8_ok.
Return values
k_ra8_okThe pass printed DFU PASS.
Precondition
Device-side DFU class is registered (other thread).
The self-loop cable connects J7 to J11.
Postcondition
On success s_dbg_pass_count advanced and LED2 is on.
On failure the host controller is deinitialized for a clean retry.
Note
Blocking; runs on the low-priority host thread.
Since
0.1.0

Definition at line 595 of file main.c.

References dfu_print(), dfu_print_fail(), k_dfu_commit_tries, k_dfu_idle_ticks, k_dfu_phase_download, k_dfu_phase_init, k_dfu_phase_pass, k_ra8_board_led2, k_ra8_dfu_slot_a, k_ra8_err_crc_mismatch, k_ra8_err_hw_timeout, k_ra8_ok, k_ra8_usb_speed_hs, ra8_dfu_host_result_t::pid, ra8_board_led_on(), ra8_dfu_device_committed(), ra8_dfu_device_last_error(), ra8_dfu_host_program(), ra8_dfu_slot_valid(), s_boot_payload, s_dbg_pass_count, s_dbg_phase, s_dbg_pid, and tx_thread_sleep.

Referenced by dfu_host_worker().

◆ dfu_host_worker()

VOID dfu_host_worker ( ULONG arg)
static

Host-side worker: retry the full pass until it succeeds, then park.

Parameters
[in]argThreadX entry argument (unused).
Returns
Never returns.
Precondition
tx_application_define created this thread.
The HS host pins, expander switch, and PLL are up (main).
Postcondition
On success the pass counter and LED2 are latched.
Retries forever otherwise; each failure prints its step.
Note
Blocking calls; ms timeouts via ra8_time.
Since
0.1.0

Definition at line 666 of file main.c.

References dfu_host_pass(), k_dfu_boot_wait_ticks, k_dfu_idle_ticks, k_dfu_retry_ticks, k_ra8_ok, s_dbg_host_err, and tx_thread_sleep.

Referenced by tx_application_define().

◆ dfu_nibble_to_hex()

uint8_t dfu_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 409 of file main.c.

References k_dfu_hex_digit_split.

Referenced by dfu_print_hex().

◆ dfu_panic_halt()

void dfu_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 739 of file main.c.

Referenced by dfu_route_usb_or_halt(), dfu_setup_or_halt(), and main().

◆ dfu_print()

ra8_err_t dfu_print ( const char * text)
staticnodiscard

Print a NUL-terminated ASCII string over the console.

Parameters
[in]textString to print (CR/LF included by the caller).
Returns
ra8_err_t propagated from the SCI helper.
Return values
k_ra8_okAll bytes queued.
Precondition
SCI8 init already ran; text is non-NULL.
text is NUL-terminated within k_dfu_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 471 of file main.c.

References dfu_sci_write(), and dfu_str_len().

Referenced by dfu_host_pass(), and dfu_print_fail().

◆ dfu_print_fail()

ra8_err_t dfu_print_fail ( const char * what,
ra8_err_t err )
staticnodiscard

Print "FAIL <what> err=0xNNNNNNNN" on its own line.

Parameters
[in]whatShort description of the failed step.
[in]errError code returned by the step.
Returns
ra8_err_t propagated from the SCI helpers.
Return values
k_ra8_okThe diagnostic line is queued.
Precondition
SCI8 init already ran; what is NUL-terminated within the cap.
None beyond console readiness.
Postcondition
One diagnostic line is in the SCI8 TX FIFO.
No other state changes.
Note
Blocking polled TX.
Since
0.1.0

Definition at line 516 of file main.c.

References dfu_print(), dfu_print_hex(), k_dfu_hex_chars_u32, and k_ra8_ok.

Referenced by dfu_host_pass().

◆ dfu_print_hex()

ra8_err_t dfu_print_hex ( uint32_t value,
uint8_t digits )
staticnodiscard

Print a value as fixed-width uppercase hex.

Parameters
[in]valueValue to print.
[in]digitsHex digit count (4 for u16, 8 for u32).
Returns
ra8_err_t propagated from the SCI helper.
Return values
k_ra8_okAll bytes queued.
Precondition
SCI8 init already ran.
digits is at most k_dfu_hex_chars_u32.
Postcondition
One fixed-width hex token is in the SCI8 TX FIFO.
No other state changes.
Note
Blocking polled TX.
Since
0.1.0

Definition at line 489 of file main.c.

References dfu_nibble_to_hex(), dfu_sci_write(), k_dfu_hex_chars_u32, k_dfu_nibble_bits, and k_dfu_nibble_mask.

Referenced by dfu_host_pass(), and dfu_print_fail().

◆ dfu_route_usb_or_halt()

void dfu_route_usb_or_halt ( void )
static

Route both ports' pins: FS as device, HS as host.

Returns
void.
Precondition
IOPORT and the U15 expander are reachable.
Called once from dfu_setup_or_halt.
Postcondition
FS pins carry the device role, HS pins the host role, PD07 HIGH.
Panic-halts on any routing failure.
Note
Panic-halts on any routing failure.
Since
0.1.0

Definition at line 756 of file main.c.

References dfu_panic_halt(), k_dfu_pin_fs_dm, k_dfu_pin_fs_dp, k_dfu_pin_fs_vbus, k_dfu_pin_fs_vbusen, k_dfu_pin_hs_pwr, k_dfu_pin_hs_vbus, k_ra8_level_high, k_ra8_level_low, 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 dfu_setup_or_halt().

◆ dfu_sci_write()

ra8_err_t dfu_sci_write ( const uint8_t * data,
uint32_t len )
staticnodiscard

Push a literal block over the board UART console (SCI8) polled.

Parameters
[in]dataBuffer to send.
[in]lenByte count.
Returns
ra8_err_t passthrough from ra8_board_uart_console_write.
Return values
k_ra8_okAll bytes queued.
Precondition
data is non-NULL; the board console init already ran.
len excludes any NUL terminator.
Postcondition
Bytes are in the SCI8 TX FIFO.
No other state changes.
Note
Blocking polled TX.
Since
0.1.0

Definition at line 454 of file main.c.

References ra8_board_uart_console_write().

Referenced by dfu_print(), dfu_print_dec(), and dfu_print_hex().

◆ dfu_setup_or_halt()

void dfu_setup_or_halt ( void )
static

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

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

Definition at line 791 of file main.c.

References dfu_panic_halt(), dfu_route_usb_or_halt(), k_dfu_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().

◆ dfu_str_len()

uint32_t dfu_str_len ( const char * text)
static

Bounded ASCII string length (cap k_dfu_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_dfu_print_cap.
Note
Bounded scan.
Since
0.1.0

Definition at line 429 of file main.c.

References k_dfu_print_cap.

Referenced by dfu_print().

◆ main()

void main ( void )

Application entry: bring the board up, then hand off to ThreadX.

The application entry point Reset_Handler hands control to.

Precondition
Reset_Handler copied .data and zeroed .bss.
SystemInit set VTOR, FPU, priority grouping.
Postcondition
On clean entry the CPU stays in tx_kernel_enter forever.
On any HAL init failure the function halts in WFI.
Note
Single entry point; not re-entrant.
Since
0.1.0

Definition at line 830 of file main.c.

References dfu_panic_halt(), dfu_setup_or_halt(), and ra8_isr_globals_enable().

◆ 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 80 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 both workers.

Parameters
[in]first_unused_memorySentinel (unused; static stacks).
Returns
void.
Precondition
Called from tx_kernel_enter after scheduler init.
Static stacks are reserved at file scope.
Postcondition
Two auto-start workers are queued; s_tx_kernel_up is true.
The scheduler runs the device DFU bring-up + the host DFU ladder.
Note
Called once at boot; not thread-safe.
Since
0.1.0

Definition at line 695 of file main.c.

References dfu_device_worker(), dfu_host_worker(), k_dfu_dev_priority, k_dfu_host_priority, k_dfu_host_stack, k_dfu_thread_stack, 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.

Variable Documentation

◆ k_dfu_pin_fs_dm

const ra8_port_pin_t k_dfu_pin_fs_dm = (ra8_port_pin_t)k_ra8_board_usbfs_pin_dm
static

USBFS D- (P8_15).

Definition at line 104 of file main.c.

Referenced by dfu_route_usb_or_halt().

◆ k_dfu_pin_fs_dp

const ra8_port_pin_t k_dfu_pin_fs_dp = (ra8_port_pin_t)k_ra8_board_usbfs_pin_dp
static

USBFS D+ (P8_14).

Definition at line 101 of file main.c.

Referenced by dfu_route_usb_or_halt().

◆ k_dfu_pin_fs_vbus

const ra8_port_pin_t k_dfu_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 95 of file main.c.

Referenced by dfu_route_usb_or_halt().

◆ k_dfu_pin_fs_vbusen

const ra8_port_pin_t k_dfu_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 98 of file main.c.

Referenced by dfu_route_usb_or_halt().

◆ k_dfu_pin_hs_pwr

const ra8_port_pin_t k_dfu_pin_hs_pwr = (ra8_port_pin_t)k_ra8_board_usbhs_pin_pwr
static

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

Definition at line 110 of file main.c.

Referenced by dfu_route_usb_or_halt().

◆ k_dfu_pin_hs_vbus

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

Referenced by dfu_route_usb_or_halt().

◆ s_boot_payload

const UCHAR s_boot_payload[64]
static
Initial value:
= {
0x00U, 0x00U, 0x07U, 0x22U, 0x11U, 0x00U, 0x02U, 0x02U, 0xAFU, 0xF3U, 0x00U, 0x80U, 0xAFU,
0xF3U, 0x00U, 0x80U, 0x46U, 0xF2U, 0x0DU, 0x00U, 0xC6U, 0xF2U, 0x0DU, 0x00U, 0x40U, 0xF2U,
0x00U, 0x01U, 0xC2U, 0xF2U, 0x04U, 0x21U, 0x08U, 0x60U, 0xFEU, 0xE7U, 0xFFU, 0xFFU, 0xFFU,
0xFFU, 0xFFU, 0xFFU, 0xFFU, 0xFFU, 0xFFU, 0xFFU, 0xFFU, 0xFFU, 0xFFU, 0xFFU, 0xFFU, 0xFFU,
0xFFU, 0xFFU, 0xFFU, 0xFFU, 0xFFU, 0xFFU, 0xFFU, 0xFFU, 0xFFU, 0xFFU, 0xFFU, 0xFFU,
}

A real bootable Slot-A image the host DFU-flashes: a 64 KiB-aligned vector table (initial MSP, reset = entry+1) followed by a reset stub that writes the sentinel 0x600D600D to SRAM 0x22040000 and spins.

Hand-assembled (cortex-m thumb) linked at the Slot-A base 0x02020000, so vector[1] = 0x02020011. After the bootloader jumps to the committed slot, a J-Link read of 0x22040000 == 0x600D600D proves the e-ink-free hand-off executed the DFU-received image. Padded to one 64-byte block.

Since
0.1.0

Definition at line 193 of file main.c.

Referenced by dfu_host_pass().

◆ s_dbg_dev_step

volatile uint32_t s_dbg_dev_step
static

Device worker progress: 1 stack, 2 class, 3 dcd, 4 attach.

Definition at line 214 of file main.c.

◆ s_dbg_dev_writes

volatile uint32_t s_dbg_dev_writes
static

Device-side download block-write count.

Definition at line 212 of file main.c.

Referenced by dfu_write().

◆ s_dbg_host_err

volatile uint32_t s_dbg_host_err
static

Host-ladder first failing return code (0 = none).

Definition at line 216 of file main.c.

Referenced by dfu_device_worker(), and dfu_host_worker().

◆ s_dbg_pass_count

volatile uint32_t s_dbg_pass_count
static

Completed full passes (sticky success counter).

Definition at line 210 of file main.c.

◆ s_dbg_phase

volatile uint32_t s_dbg_phase
static

Host-ladder phase marker (dfu_phase_t).

Definition at line 206 of file main.c.

◆ s_dbg_pid

volatile uint32_t s_dbg_pid
static

Device-reported product id captured at enumeration.

Definition at line 208 of file main.c.

Referenced by cdc_host_pass(), dfu_host_enumerate(), dfu_host_pass(), hid_host_pass(), and hid_host_pass().

◆ s_device_framework

UCHAR s_device_framework[]
static

Definition at line 227 of file main.c.

◆ s_device_stack

UCHAR s_device_stack[k_dfu_thread_stack]
static

Stack backing storage for s_device_thread.

Definition at line 174 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 172 of file main.c.

◆ s_host_stack

UCHAR s_host_stack[k_dfu_host_stack]
static

Stack backing storage for s_host_thread.

Definition at line 178 of file main.c.

◆ s_host_thread

TX_THREAD s_host_thread
static

ThreadX TCB for the host-side worker thread.

Definition at line 176 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 345 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 285 of file main.c.

◆ s_tx_kernel_up

volatile bool s_tx_kernel_up = false
static

Set in tx_application_define; gates ThreadX tick delivery so a pre-kernel SysTick (the U15 expander I2C blocks for ms during setup) cannot feed ThreadX's zeroed timer state.

Since
0.1.0

Definition at line 77 of file main.c.

◆ s_usbx_pool

UCHAR s_usbx_pool[k_dfu_usbx_pool_bytes]
static

USBX memory pool (USBX uses tx_byte_pool internally).

Definition at line 180 of file main.c.