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 config B: FS host DFU-downloads firmware to an HS DFU device, uploads + verifies. 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
}
 Compile-time settings: threads, pool, console, cadence. More...
enum  dfu_hex_t : uint8_t {
  k_dfu_hex_chars_u16 = 4U ,
  k_dfu_hex_chars_u32 = 8U ,
  k_dfu_dec_chars_u32 = 10U ,
  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 ,
  k_dfu_dec_radix = 10U
}
 Bit-mask constants used by the text formatters. More...
enum  dfu_geom_t : uint32_t {
  k_dfu_xfer_size = 64U ,
  k_dfu_blocks = 8U ,
  k_dfu_image_bytes = 512U ,
  k_dfu_no_mismatch = 0xFFFFFFFFU ,
  k_dfu_pat_blk_mul = 131U ,
  k_dfu_pat_idx_mul = 7U ,
  k_dfu_pat_bias = 0xA5U ,
  k_dfu_byte_mask = 0xFFU ,
  k_dfu_dev_addr = 1U ,
  k_dfu_intf = 0U ,
  k_dfu_config_val = 1U
}
 Firmware-image geometry + pattern constants. 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_pattern_fill (uint32_t block, uint8_t *out, uint32_t len)
 Fill a firmware block with this block's deterministic bytes.
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 BSP 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_dec (uint32_t value)
 Print a uint32_t as ASCII decimal.
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: HS as device, FS as host (config B).
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) – peripheral-routed; the FS host sources J11 VBUS.
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_role = (ra8_port_pin_t)k_ra8_board_usbhs_pin_pwr
 J7 role strap (PD07): LOW = Device, so U18 does not back-feed 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 UCHAR s_dfu_image [k_dfu_image_bytes]
 Device-side RAM "firmware" image: the DFU write callback stores each downloaded block here; the read callback serves it back on upload.
static volatile uint32_t s_dfu_image_len
 Highest byte offset the device has captured (download high-water mark).
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_blocks_ok
 Blocks the host confirmed byte-equal on upload (expect k_dfu_blocks).
static volatile uint32_t s_dbg_mismatch = (uint32_t)k_dfu_no_mismatch
 First mismatching block, or k_dfu_no_mismatch.
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 config B: FS host DFU-downloads firmware to an HS DFU device, uploads + verifies.

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

Rehearses a firmware-update-over-USB path entirely on-chip, with the host and device roles SWAPPED versus config A (dfu_selftest_hs_host). The two USB ports are cabled to EACH OTHER and one image runs both stacks:

  • USBHS (J7) = DEVICE: a ThreadX + USBX DFU class in DFU mode (bInterfaceProtocol 0x02, so it enumerates straight into dfuIDLE – no runtime/detach round-trip). Its write callback programs each downloaded block straight into the inactive MRAM slot (libs/ra8_dfu); its read callback serves the slot body back on UPLOAD. DFU runs entirely over EP0 control transfers (no data endpoints).
  • USBFS (J11) = HOST: a self-contained polled host on the first-party ra8_usb_host_* primitives. It enumerates the device, DFU_DNLOADs a deterministic multi-block image (with DFU_GETSTATUS polling), DFU_ABORTs back to dfuIDLE, then DFU_UPLOADs it back and byte-checks it – proving the MRAM program/read-back path round-trips intact.

The download exercises the host control-OUT data stage of ra8_usb_host_control_xfer (DFU_DNLOAD carries the firmware block in the SETUP data stage host -> device).

Verdicts stream over SCI8 (J-Link OB CDC console, 115200) and are mirrored in J-Link-readable probes (s_dbg_*).

Pinout

HS device: P4_08 USBHS_VBUS sense (PSEL usb_hs), PD07 driven LOW so J7's role is Device and U18 does not back-feed VBUS; D+/D- are dedicated PHY balls. FS host: P4_07 VBUS sense, P5_00 VBUSEN peripheral-routed so the USBFS controller sources J11 VBUS, P8_14 D+, P8_15 D- (PSEL usb_fs). Console: PD_02/PD_03 SCI8 (PSEL sci_async).

Author
Brighton Sikarskie
Date
2026-06-15
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).

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

◆ dfu_geom_t

enum dfu_geom_t : uint32_t

Firmware-image geometry + pattern constants.

Enumerator
k_dfu_xfer_size 

wTransferSize: bytes per DFU block.

k_dfu_blocks 

Blocks in the rehearsal image.

k_dfu_image_bytes 

k_dfu_blocks * k_dfu_xfer_size.

k_dfu_no_mismatch 

Probe: no mismatch.

k_dfu_pat_blk_mul 

Per-block pattern multiplier.

k_dfu_pat_idx_mul 

Per-index pattern multiplier.

k_dfu_pat_bias 

Pattern constant bias.

k_dfu_byte_mask 

Byte mask.

k_dfu_dev_addr 

Address the host assigns.

k_dfu_intf 

DFU interface number.

k_dfu_config_val 

bConfigurationValue.

Definition at line 161 of file main.c.

◆ dfu_hex_t

enum dfu_hex_t : uint8_t

Hex/decimal text-formatter sizing constants.

Enumerator
k_dfu_hex_chars_u16 

16-bit value -> "ABCD".

k_dfu_hex_chars_u32 

32-bit value -> "ABCDEF01".

k_dfu_dec_chars_u32 

Max digits for a 32-bit count.

k_dfu_nibble_bits 

Bits per hex nibble.

k_dfu_hex_digit_split 

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

Definition at line 140 of file main.c.

◆ dfu_mask_t

enum dfu_mask_t : uint32_t

Bit-mask constants used by the text formatters.

Enumerator
k_dfu_nibble_mask 

4-bit nibble mask.

k_dfu_dec_radix 

Base for decimal conversion.

Definition at line 152 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 179 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 633 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 391 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-HS pins + PHY PLL are up (main did both).
Postcondition
The HS 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 414 of file main.c.

References k_dfu_idle_ticks, k_ra8_dfu_slot_b, k_ra8_ok, k_ra8_usb_speed_hs, 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.

◆ 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 683 of file main.c.

References ra8_dfu_host_result_t::blocks_ok, dfu_pattern_fill(), dfu_print(), dfu_print_dec(), dfu_print_fail(), dfu_print_hex(), k_dfu_blocks, k_dfu_hex_chars_u16, k_dfu_image_bytes, k_dfu_phase_download, k_dfu_phase_init, k_dfu_phase_pass, k_dfu_xfer_size, k_ra8_board_led2, k_ra8_ok, k_ra8_usb_speed_fs, ra8_dfu_host_result_t::mismatch, ra8_dfu_host_result_t::pid, ra8_board_led_on(), ra8_dfu_host_run(), s_dbg_blocks_ok, s_dbg_mismatch, s_dbg_pass_count, s_dbg_phase, s_dbg_pid, and s_dfu_image.

◆ 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 FS host pins and clock 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 747 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.

◆ 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 461 of file main.c.

References k_dfu_hex_digit_split.

◆ 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 820 of file main.c.

◆ dfu_pattern_fill()

void dfu_pattern_fill ( uint32_t block,
uint8_t * out,
uint32_t len )
static

Fill a firmware block with this block's deterministic bytes.

Byte i = (block*131 + i*7 + 0xA5) & 0xFF – distinct per block so the upload check proves the bytes read back are the ones downloaded.

Parameters
[in]blockThe block index (0..k_dfu_blocks-1).
[out]outDestination buffer.
[in]lenBytes to fill.
Returns
void.
Precondition
out has len writable bytes; len <= k_dfu_xfer_size.
block < k_dfu_blocks.
Postcondition
out[0..len-1] hold the block's pattern bytes.
No global state changes.
Note
Pure function.
Since
0.1.0

Definition at line 261 of file main.c.

References k_dfu_byte_mask, k_dfu_pat_bias, k_dfu_pat_blk_mul, and k_dfu_pat_idx_mul.

Referenced by dfu_host_pass().

◆ 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 523 of file main.c.

References dfu_sci_write(), and dfu_str_len().

◆ dfu_print_dec()

ra8_err_t dfu_print_dec ( uint32_t value)
staticnodiscard

Print a uint32_t as ASCII decimal.

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 ASCII decimal token is in the SCI8 TX FIFO.
No other state changes.
Note
Blocking polled TX.
Since
0.1.0

Definition at line 567 of file main.c.

References dfu_sci_write(), k_dfu_dec_chars_u32, and k_dfu_dec_radix.

Referenced by dfu_host_pass().

◆ 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 604 of file main.c.

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

◆ 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 541 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.

◆ dfu_route_usb_or_halt()

void dfu_route_usb_or_halt ( void )
static

Route both ports' pins: HS as device, FS as host (config B).

Returns
void.

HS device: P4_08 VBUS sense (PSEL usb_hs), PD07 driven LOW so J7's role is Device and U18 does not back-feed VBUS into the FS host's cable; D+/D- are dedicated PHY balls (no PFS routing). FS host: P4_07 VBUS sense, P5_00 VBUSEN peripheral-routed so the USBFS controller sources J11 VBUS, P8_14/P8_15 data – all PSEL usb_fs.

Precondition
IOPORT is reachable.
Called once from dfu_setup_or_halt.
Postcondition
HS pins carry the device role, FS pins the host role.
PD07 is LOW (J7 device, not self-powered).
Note
Panic-halts on any routing failure.
Since
0.1.0

Definition at line 842 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_role, k_dfu_pin_hs_vbus, k_ra8_level_low, k_ra8_ok, k_ra8_psel_usb_fs, k_ra8_psel_usb_hs, ra8_gpio_output_init(), and ra8_pfs_route_peripheral().

◆ dfu_sci_write()

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

Push a literal block over the BSP 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 BSP 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 506 of file main.c.

References ra8_board_uart_console_write().

◆ 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 877 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().

◆ 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 481 of file main.c.

References k_dfu_print_cap.

◆ 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 916 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 83 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 776 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 107 of file main.c.

◆ 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 104 of file main.c.

◆ 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 98 of file main.c.

◆ 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) – peripheral-routed; the FS host sources J11 VBUS.

Definition at line 101 of file main.c.

◆ k_dfu_pin_hs_role

const ra8_port_pin_t k_dfu_pin_hs_role = (ra8_port_pin_t)k_ra8_board_usbhs_pin_pwr
static

J7 role strap (PD07): LOW = Device, so U18 does not back-feed VBUS.

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

◆ s_dbg_blocks_ok

volatile uint32_t s_dbg_blocks_ok
static

Blocks the host confirmed byte-equal on upload (expect k_dfu_blocks).

Definition at line 230 of file main.c.

Referenced by dfu_host_pass(), and dfu_upload_verify().

◆ 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 238 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 236 of file main.c.

◆ s_dbg_host_err

volatile uint32_t s_dbg_host_err
static

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

Definition at line 240 of file main.c.

◆ s_dbg_mismatch

volatile uint32_t s_dbg_mismatch = (uint32_t)k_dfu_no_mismatch
static

First mismatching block, or k_dfu_no_mismatch.

Definition at line 232 of file main.c.

◆ s_dbg_pass_count

volatile uint32_t s_dbg_pass_count
static

Completed full passes (sticky success counter).

Definition at line 234 of file main.c.

◆ s_dbg_phase

volatile uint32_t s_dbg_phase
static

Host-ladder phase marker (dfu_phase_t).

Definition at line 226 of file main.c.

◆ s_dbg_pid

volatile uint32_t s_dbg_pid
static

Device-reported product id captured at enumeration.

Definition at line 228 of file main.c.

◆ s_device_framework

UCHAR s_device_framework[]
static

Definition at line 279 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 197 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 195 of file main.c.

◆ s_dfu_image

UCHAR s_dfu_image[k_dfu_image_bytes]
static

Device-side RAM "firmware" image: the DFU write callback stores each downloaded block here; the read callback serves it back on upload.

Note
Written by the USBX DFU class thread; read by the same on upload.
Since
0.1.0

Definition at line 212 of file main.c.

Referenced by dfu_host_pass(), dfu_read(), and dfu_write().

◆ s_dfu_image_len

volatile uint32_t s_dfu_image_len
static

Highest byte offset the device has captured (download high-water mark).

Since
0.1.0

Definition at line 219 of file main.c.

Referenced by dfu_read(), and dfu_write().

◆ s_host_stack

UCHAR s_host_stack[k_dfu_host_stack]
static

Stack backing storage for s_host_thread.

Definition at line 201 of file main.c.

◆ s_host_thread

TX_THREAD s_host_thread
static

ThreadX TCB for the host-side worker thread.

Definition at line 199 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 397 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 337 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 80 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 203 of file main.c.