|
ra8-firmware 0.1.0
Bare-metal firmware for the Renesas RA8 family (RA8D2 / RA8P1)
|
USB self-loop: HS host DFU-downloads firmware to an FS 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_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 "usb_selftest_dfu_steps.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"Go to the source code of this file.
Typedefs | |
| typedef UINT(* | dfu_write_cb_t) (VOID *dfu, ULONG block_number, UCHAR *data, ULONG length, ULONG *media_status) |
| Signature of the USBX DFU download-write callback field. | |
Enumerations | |
| enum | usb_langid_byte_t : uint8_t { k_usb_langid_en_us_lo = 0x09U , k_usb_langid_en_us_hi = 0x04U } |
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_activate (VOID *dfu) |
| DFU activate callback. | |
| static VOID | dfu_deactivate (VOID *dfu) |
| DFU deactivate callback. | |
| static UINT | dfu_write (VOID *dfu, ULONG block_number, const UCHAR *data, ULONG length, ULONG *media_status) |
| DFU write callback – store one downloaded block into the RAM image. | |
| static UINT | dfu_read (VOID *dfu, ULONG block_number, UCHAR *data, ULONG length, ULONG *actual_length) |
| DFU read callback – serve one block of the RAM image on UPLOAD. | |
| static UINT | dfu_get_status (VOID *dfu, ULONG *media_status) |
| DFU get-status callback – report the media always ready. | |
| static UINT | dfu_notify (VOID *dfu, ULONG notification) |
| DFU notify callback – accept begin/end/manifest notifications. | |
| static UINT | dfu_usbx_stack_up (void) |
| Bring USBX system + device stack up with the DFU framework. | |
| static UINT | dfu_class_register (void) |
| Register the DFU class against configuration 1, interface 0. | |
| static VOID | dfu_device_worker (ULONG arg) |
| Device-side worker: bring the DFU device up, then park. | |
| 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 + console + 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 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_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. | |
USB self-loop: HS host DFU-downloads firmware to an FS DFU device, uploads + verifies.
Rehearses a firmware-update-over-USB path entirely on-chip. The two USB ports are cabled to EACH OTHER and one image runs both stacks:
The download exercises the host control-OUT data stage added to ra8_usb_host_control_xfer for this app (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_*).
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).
Definition in file main.c.
| typedef UINT( * dfu_write_cb_t) (VOID *dfu, ULONG block_number, UCHAR *data, ULONG length, ULONG *media_status) |
Signature of the USBX DFU download-write callback field.
Mirrors ::UX_SLAVE_CLASS_DFU_PARAMETER::ux_slave_class_dfu_parameter_write, whose vendored prototype takes a non-const UCHAR* payload. Our dfu_write keeps the payload const (it never mutates the block), so the assignment is funnelled through this typedef with a single explicit cast that narrows only the const qualifier of the data pointer – a layout-identical function-pointer conversion.
| enum usb_langid_byte_t : uint8_t |
|
extern |
ThreadX 1 ms tick worker.
|
static |
DFU activate callback.
The captured image lives at file scope.
| [in] | dfu | Unused (single instance). |
Definition at line 310 of file main.c.
Referenced by dfu_class_register().
|
static |
Register the DFU class against configuration 1, interface 0.
| UX_SUCCESS | Class registered. |
Definition at line 492 of file main.c.
References dfu_activate(), dfu_deactivate(), dfu_get_status(), dfu_notify(), dfu_read(), dfu_write(), and s_device_framework.
Referenced by dfu_device_worker().
|
static |
DFU deactivate callback.
| [in] | dfu | Unused. |
Definition at line 326 of file main.c.
Referenced by dfu_class_register().
|
static |
Device-side worker: bring the DFU device up, then park.
| [in] | arg | ThreadX entry argument (unused). |
Definition at line 528 of file main.c.
References dfu_class_register(), dfu_usbx_stack_up(), k_dfu_idle_ticks, k_ra8_ok, k_ra8_usb_speed_fs, ra8_usb_device_attach(), s_dbg_dev_step, s_dbg_host_err, tx_thread_sleep, and ux_dcd_ra8_usb_initialize().
DFU get-status callback – report the media always ready.
| [in] | dfu | Unused. |
| [out] | media_status | Receives ::UX_SLAVE_CLASS_DFU_MEDIA_STATUS_OK. |
| UX_SUCCESS | Media OK. |
media_status is OK. Definition at line 411 of file main.c.
Referenced by dfu_class_register().
|
static |
Host-side worker: retry the full pass until it succeeds, then park.
| [in] | arg | ThreadX entry argument (unused). |
Definition at line 572 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 notify callback – accept begin/end/manifest notifications.
| [in] | dfu | Unused. |
| [in] | notification | The DFU notification code. |
| UX_SUCCESS | Notification accepted. |
Definition at line 431 of file main.c.
Referenced by dfu_class_register().
|
static |
|
static |
DFU read callback – serve one block of the RAM image on UPLOAD.
| [in] | dfu | Unused. |
| [in] | block_number | DFU block sequence number (byte offset = n * 64). |
| [out] | data | Destination for the block payload. |
| [in] | length | Bytes the host can accept (wTransferSize). |
| [out] | actual_length | Receives the bytes returned (0 ends the upload). |
| UX_SUCCESS | Block (or short/zero end-of-image) returned. |
data holds length bytes; called from the USBX class thread. actual_length holds min(remaining, length); a short block ends the upload. Definition at line 381 of file main.c.
References k_dfu_xfer_size, memcpy(), s_dfu_image, and s_dfu_image_len.
Referenced by dfu_class_register().
|
static |
Route both ports' pins: FS as device, HS as host.
Definition at line 662 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().
|
static |
Bring CGC + both USB clocks + SysTick + console + LEDs + pins up.
Definition at line 697 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().
|
static |
Bring USBX system + device stack up with the DFU framework.
| UX_SUCCESS | Stack ready. |
Definition at line 449 of file main.c.
References k_dfu_usbx_pool_bytes, s_device_framework, s_language_id_framework, s_string_framework, and s_usbx_pool.
Referenced by dfu_device_worker().
|
static |
DFU write callback – store one downloaded block into the RAM image.
| [in] | dfu | Unused. |
| [in] | block_number | DFU block sequence number (byte offset = n * 64). |
| [in] | data | Block payload from the control-OUT data stage. |
| [in] | length | Block length in bytes (0 ends the download). |
| [out] | media_status | Receives ::UX_SLAVE_CLASS_DFU_MEDIA_STATUS_OK. |
| UX_SUCCESS | Block stored (or zero-length end-of-download accepted). |
data holds length bytes; called from the USBX class thread. block_number * 64 + length fits s_dfu_image. media_status is OK so the DFU state machine advances to DNLOAD_IDLE. Definition at line 349 of file main.c.
References k_dfu_xfer_size, memcpy(), s_dbg_dev_writes, s_dfu_image, and s_dfu_image_len.
Referenced by dfu_class_register().
| void main | ( | void | ) |
Application entry: bring the board up, then hand off to ThreadX.
The application entry point Reset_Handler hands control to.
Definition at line 736 of file main.c.
References dfu_panic_halt(), dfu_setup_or_halt(), and ra8_isr_globals_enable().
| 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.
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.
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.
Service the SysTick exception selected by the linked application.
Definition at line 79 of file main.c.
References _tx_timer_interrupt(), ra8_time_on_tick(), s_tx_kernel_up, and ux_dcd_ra8_usb_irq_reenable().
| VOID tx_application_define | ( | VOID * | first_unused_memory | ) |
ThreadX application-define hook.
Spawns both workers.
| [in] | first_unused_memory | Sentinel (unused; static stacks). |
Definition at line 601 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.
|
static |
|
static |
|
static |
|
static |
|
static |
|
static |
|
static |
|
static |
|
static |
|
static |
Stack backing storage for s_device_thread.
|
static |
|
static |
|
static |
|
static |
Stack backing storage for s_host_thread.
|
static |
|
static |
|
static |
|
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.
|
static |