|
ra8-firmware 0.1.0
Bare-metal firmware for the Renesas RA8 family (RA8D2 / RA8P1)
|
examples/ek_ra8d2/hw_validated/manual/tz_secure_only_usb_hs/src/tz_secure_only_usb_hs_steps.c More...
#include "tz_secure_only_usb_hs_steps.h"#include <stdint.h>#include <string.h>#include "ra8_board_ek_ra8d2.h"#include "ra8_err.h"#include "ra8_isr.h"#include "ra8_usb.h"#include "ra8_usb_regs.h"#include "tx_api.h"#include "ux_api.h"#include "ux_dcd_ra8_usb.h"#include "ux_device_class_cdc_acm.h"#include "ux_device_stack.h"#include "ux_system.h"Go to the source code of this file.
Data Structures | |
| struct | demo_diag_t |
| Demo-loop counters; read via JLink to localise stalls. More... | |
Enumerations | |
| enum | usbhs_sec_reg_addr_t : uintptr_t { k_usbhs_psarb_addr = 0x40204004UL , k_usbhs_pparb_addr = 0x4020401CUL } |
| USBHS security/privilege attribution register addresses. More... | |
| enum | demo_config_t : uint32_t { k_demo_thread_stack = 8192U , k_demo_usbx_pool_bytes = 16384U , k_demo_echo_buf_bytes = 512U , k_demo_idle_ticks = 1U } |
| enum | boot_probe_step_t : uint32_t { k_boot_probe_thread_entry = 1U , k_boot_probe_pre_sys_init = 2U , k_boot_probe_pre_dev_stack_init = 3U , k_boot_probe_pre_class_register = 4U , k_boot_probe_pre_board_usbhs_init = 5U , k_boot_probe_post_board_usbhs_init = 7U , k_boot_probe_pre_ux_dcd_init = 8U , k_boot_probe_post_ux_dcd_init = 9U , k_boot_probe_pre_dev_attach = 10U , k_boot_probe_post_dev_attach = 11U , k_boot_probe_enter_echo_loop = 12U } |
| Bisect-probe step values for s_boot_probe. More... | |
Functions | |
| static VOID | intenb0_watchdog_entry (ULONG arg) |
| Watchdog entry: re-asserts USBHS INTENB0 = 0xFF00 if cleared. | |
| static VOID | demo_cdc_activate (VOID *cdc_instance) |
| CDC-ACM activate callback. | |
| static VOID | demo_cdc_deactivate (VOID *cdc_instance) |
| CDC-ACM deactivate callback. | |
| static bool | demo_worker_usbx_init (void) |
| Bring up USBX system + device stack with HS+FS frameworks. | |
| static bool | demo_worker_register_cdc (void) |
| Register the CDC-ACM class against configuration 1, interface 0. | |
| static bool | demo_worker_start_dcd (void) |
| Plug the DCD bridge into the device stack and attach the bus. | |
| static void | demo_worker_spawn_intenb0_watchdog (void) |
| Spawn the INTENB0 re-arm watchdog thread. | |
| static void | demo_worker_capture_echo_probes (void) |
| Capture bisect probes ONCE on entry to the echo loop. | |
| static void | demo_worker_echo_loop (void) |
| Run the CDC-ACM echo loop forever. | |
| static VOID | demo_worker (ULONG arg) |
| Worker thread entry. | |
| VOID | tx_application_define (VOID *first_unused_memory) |
Variables | |
| static TX_THREAD | s_demo_thread |
| ThreadX TCB for the USBX worker thread. | |
| static UCHAR | s_demo_stack [k_demo_thread_stack] |
| Stack backing storage for s_demo_thread. | |
| static TX_THREAD | s_intenb0_watchdog_thread |
| ThreadX TCB for the INTENB0 re-arm watchdog. | |
| static UCHAR | s_intenb0_watchdog_stack [1024U] |
| Stack backing storage for the INTENB0 watchdog thread. | |
| volatile uint32_t | s_intenb0_rearm_count = 0U |
| Number of times the watchdog has re-asserted INTENB0 = 0xFF00. | |
| volatile uint32_t | s_intenb0_watchdog_started = 0U |
| Set to 1 once the watchdog thread has begun polling. | |
| volatile uint32_t | s_host_kick_done = 0U |
| Set to 1 once the host-mode PHY analog kick has been applied. | |
| static UCHAR | s_usbx_pool [k_demo_usbx_pool_bytes] |
| USBX memory pool (USBX uses tx_byte_pool internally). | |
| static UX_SLAVE_CLASS_CDC_ACM *volatile | s_cdc_acm = UX_NULL |
| Active CDC-ACM class instance, captured by activate callback. | |
| volatile uint32_t | s_cdc_activate_count = 0U |
| volatile uint32_t | s_cdc_activate_post_put = 0U |
| volatile uint32_t | s_cdc_deactivate_count = 0U |
| volatile uint32_t | s_pendsv_observed_run_count = 0U |
| volatile demo_diag_t | s_demo_diag = {} |
| static volatile uint32_t | s_boot_probe = 0U |
| Temporary bisect probe for USBHS bring-up HardFault. | |
| volatile uint16_t | s_syscfg_in_echo_loop = 0U |
| SYSCFG snapshot taken on the FIRST iteration of the echo loop. | |
| volatile uint16_t | s_lpsts_in_echo_loop = 0U |
| LPSTS snapshot on first echo-loop iteration. | |
| volatile uint32_t | s_psar_state = 0U |
| Snapshot of R_PSCU->PSARB on entry to the echo loop. | |
| volatile uint32_t | s_ppar_state = 0U |
| Snapshot of R_PSCU->PPARB on entry to the echo loop. | |
| static TX_SEMAPHORE | s_cdc_active_sem |
| Posted by demo_cdc_activate; demo thread blocks on it instead of polling s_cdc_acm with tx_thread_sleep. | |
examples/ek_ra8d2/hw_validated/manual/tz_secure_only_usb_hs/src/tz_secure_only_usb_hs_steps.c
ThreadX + USBX worker machinery for the secure-only USB-HS echo app.
Sibling translation unit for examples/ek_ra8d2/hw_validated/manual/tz_secure_only_usb_hs/src/main.c. Holds the CDC-ACM activate/deactivate callbacks, the USBX bring-up step routines, the INTENB0 re-arm watchdog and the ThreadX tx_application_define kernel hook. These were moved here verbatim from main.c (a pure, behaviour-preserving code move) so that every translation unit stays under the 1000-line check_file_size.py cap. The four USBX descriptor byte arrays consumed by demo_worker_usbx_init live in the companion tz_secure_only_usb_hs_descriptors.c.
Definition in file tz_secure_only_usb_hs_steps.c.
| enum boot_probe_step_t : uint32_t |
Bisect-probe step values for s_boot_probe.
Definition at line 288 of file tz_secure_only_usb_hs_steps.c.
| enum demo_config_t : uint32_t |
| Enumerator | |
|---|---|
| k_demo_thread_stack | Worker thread stack (bytes). |
| k_demo_usbx_pool_bytes | USBX memory pool (bytes). |
| k_demo_echo_buf_bytes | HS bulk MPS. |
| k_demo_idle_ticks | Idle back-off when no class active. |
Definition at line 44 of file tz_secure_only_usb_hs_steps.c.
| enum usbhs_sec_reg_addr_t : uintptr_t |
USBHS security/privilege attribution register addresses.
| Enumerator | |
|---|---|
| k_usbhs_psarb_addr | Peripheral Security Attribution B. |
| k_usbhs_pparb_addr | Peripheral Privilege Attribution B. |
Definition at line 39 of file tz_secure_only_usb_hs_steps.c.
|
static |
CDC-ACM activate callback.
Captures the live class instance.
| [in] | cdc_instance | Pointer to UX_SLAVE_CLASS_CDC_ACM. |
Definition at line 330 of file tz_secure_only_usb_hs_steps.c.
References s_cdc_acm, s_cdc_activate_count, s_cdc_activate_post_put, s_cdc_active_sem, and ux_dcd_ra8_usb_auto_echo_enable().
|
static |
CDC-ACM deactivate callback.
Drops the live class pointer.
| [in] | cdc_instance | Unused. |
Definition at line 363 of file tz_secure_only_usb_hs_steps.c.
References s_cdc_acm, and s_cdc_deactivate_count.
|
static |
Worker thread entry.
Brings USBX + CDC up, then echoes forever.
| [in] | arg | Unused (ThreadX entry signature). |
Definition at line 628 of file tz_secure_only_usb_hs_steps.c.
References demo_worker_capture_echo_probes(), demo_worker_echo_loop(), demo_worker_register_cdc(), demo_worker_spawn_intenb0_watchdog(), demo_worker_start_dcd(), demo_worker_usbx_init(), k_boot_probe_enter_echo_loop, k_boot_probe_thread_entry, and s_boot_probe.
|
static |
Capture bisect probes ONCE on entry to the echo loop.
Reads SYSCFG / LPSTS / PSARB / PPARB into JLink-visible globals so post-mortem analysis can confirm the controller state at the moment the echo loop starts spinning.
HUM Ch 37.2.1 SYSCFG p 2060, HUM Ch 37.2.43 LPSTS p 2111, HUM Ch 51.8.1 PSARB p 3284, HUM Ch 51.8.6 PPARB p 3292.
Definition at line 547 of file tz_secure_only_usb_hs_steps.c.
References k_usbhs_pparb_addr, k_usbhs_psarb_addr, ra8_usb_hs(), ra8_usbhs_lpsts(), s_lpsts_in_echo_loop, s_ppar_state, s_psar_state, s_syscfg_in_echo_loop, and r_usb_regs_t::SYSCFG.
Referenced by demo_worker().
|
static |
Run the CDC-ACM echo loop forever.
Read up to k_demo_echo_buf_bytes bytes from the host, echo them straight back, and toggle LED1 once per byte echoed. s_demo_diag counters track each branch for JLink-side instrumentation.
Definition at line 571 of file tz_secure_only_usb_hs_steps.c.
References k_demo_echo_buf_bytes, k_demo_idle_ticks, k_ra8_board_led1, ra8_board_led_toggle(), s_cdc_acm, s_demo_diag, and tx_thread_sleep.
Referenced by demo_worker().
|
static |
Register the CDC-ACM class against configuration 1, interface 0.
Wires the activate/deactivate callbacks so s_cdc_acm tracks the live class instance. Must be called after the device stack has been initialized and before ux_dcd_ra8_usb_initialize.
| true | CDC-ACM class registered. |
| false | _ux_device_stack_class_register failed. |
Definition at line 431 of file tz_secure_only_usb_hs_steps.c.
References demo_cdc_activate(), demo_cdc_deactivate(), k_boot_probe_pre_class_register, and s_boot_probe.
Referenced by demo_worker().
|
static |
Spawn the INTENB0 re-arm watchdog thread.
USB Bus Reset on the RA8D2 HS controller auto-clears INTENB0 to 0 (verified live via JLink: a manual write of INTENB0 = 0xFF00 immediately resumed ISR firing after a host bus reset). The driver's busreset_rearm is supposed to do this from the DVST(=Default) ISR path, but the ISR cannot run while INTENB0 is zero. This polled watchdog breaks the chicken-and-egg by re-writing INTENB0 outside the ISR.
Definition at line 514 of file tz_secure_only_usb_hs_steps.c.
References intenb0_watchdog_entry(), s_intenb0_watchdog_stack, s_intenb0_watchdog_thread, s_thread_name, TX_AUTO_START, TX_NO_TIME_SLICE, and tx_thread_create.
Referenced by demo_worker().
|
static |
Plug the DCD bridge into the device stack and attach the bus.
USBHS controller bring-up is performed exactly once, inside ux_dcd_ra8_usb_initialize (which itself calls ra8_usb_device_init). The PHY clock was armed in main() via ra8_cgc_usbhs_pll_enable; PD07 role-select is owned by demo_pins_init. MSTPB12 ungate happens inside ra8_usb_device_init via ra8_mstp_enable.
Previously this site called ra8_board_usbhs_device_init() which also invoked ra8_usb_device_init(hs) and ux_dcd_ra8_usb_initialize then invoked it AGAIN. The duplicate PHY bring-up cleared INTENB0 and left the controller in a state where chirp completed but the host never issued SETUP – exactly the failure signature observed on J7.
The "host-mode kick" / SYSCFG reset block previously sitting between the two probes was compile-time disabled (#if 0) and has been removed; s_host_kick_done is left at 0 so JLink scripts that read it still link.
| true | DCD initialized and bus attached at HS. |
| false | ux_dcd_ra8_usb_initialize or ra8_usb_device_attach failed. |
Definition at line 479 of file tz_secure_only_usb_hs_steps.c.
References k_boot_probe_post_board_usbhs_init, k_boot_probe_post_dev_attach, k_boot_probe_post_ux_dcd_init, k_boot_probe_pre_dev_attach, k_boot_probe_pre_ux_dcd_init, k_ra8_ok, k_ra8_usb_speed_hs, ra8_usb_device_attach(), s_boot_probe, s_host_kick_done, and ux_dcd_ra8_usb_initialize().
Referenced by demo_worker().
|
static |
Bring up USBX system + device stack with HS+FS frameworks.
Initializes the USBX memory pool, then registers both HS and FS device frameworks. With the PHY CLKSEL=24 fix landed (see ra8_usb.c::internal_usbhs_phy_bringup), the HS chirp completes correctly and the host expects HS-conformant descriptors with 512-byte bulk MPS. USBX picks the framework matching the negotiated bus speed (RHST=011 -> HS framework, RHST=010 -> FS framework).
| true | Both system and device stacks initialized. |
| false | _ux_system_initialize or _ux_device_stack_initialize failed. |
Definition at line 395 of file tz_secure_only_usb_hs_steps.c.
References k_boot_probe_pre_dev_stack_init, k_boot_probe_pre_sys_init, k_demo_usbx_pool_bytes, s_boot_probe, s_tz_secure_only_usb_hs_device_framework_fs, s_tz_secure_only_usb_hs_device_framework_hs, s_tz_secure_only_usb_hs_language_id_framework, s_tz_secure_only_usb_hs_string_framework, and s_usbx_pool.
Referenced by demo_worker().
|
static |
Watchdog entry: re-asserts USBHS INTENB0 = 0xFF00 if cleared.
| [in] | arg | Unused (ThreadX entry signature). |
Definition at line 665 of file tz_secure_only_usb_hs_steps.c.
References r_usb_regs_t::INTENB0, k_ra8_int0_full_mask, ra8_usb_hs(), s_intenb0_rearm_count, s_intenb0_watchdog_started, and tx_thread_sleep.
Referenced by demo_worker_spawn_intenb0_watchdog().
| VOID tx_application_define | ( | VOID * | first_unused_memory | ) |
Definition at line 683 of file tz_secure_only_usb_hs_steps.c.
References demo_worker(), k_demo_thread_stack, s_cdc_active_sem, s_demo_stack, s_demo_thread, s_thread_name, TX_AUTO_START, TX_NO_TIME_SLICE, and tx_thread_create.
|
static |
Temporary bisect probe for USBHS bring-up HardFault.
Stepped through every major call in demo_worker so a JLink session can read the last value reached and localise which call faulted. Values: 1=thread entry, 2=pre _ux_system_initialize, 3=pre _ux_device_stack_initialize, 4=pre _ux_device_stack_class_register, 5=pre ra8_board_usbhs_device_init, 7=post that, 8=pre ux_dcd_ra8_usb_initialize, 9=post, 10=pre ra8_usb_device_attach, 11=post, 12=entering echo while(1). Remove once the offending call is identified.
Definition at line 219 of file tz_secure_only_usb_hs_steps.c.
Referenced by demo_worker(), demo_worker_register_cdc(), demo_worker_start_dcd(), and demo_worker_usbx_init().
|
static |
Active CDC-ACM class instance, captured by activate callback.
Definition at line 171 of file tz_secure_only_usb_hs_steps.c.
| volatile uint32_t s_cdc_activate_count = 0U |
Definition at line 174 of file tz_secure_only_usb_hs_steps.c.
Referenced by demo_cdc_activate().
| volatile uint32_t s_cdc_activate_post_put = 0U |
Definition at line 175 of file tz_secure_only_usb_hs_steps.c.
Referenced by demo_cdc_activate().
|
static |
Posted by demo_cdc_activate; demo thread blocks on it instead of polling s_cdc_acm with tx_thread_sleep.
Definition at line 309 of file tz_secure_only_usb_hs_steps.c.
| volatile uint32_t s_cdc_deactivate_count = 0U |
Definition at line 176 of file tz_secure_only_usb_hs_steps.c.
Referenced by demo_cdc_deactivate().
| volatile demo_diag_t s_demo_diag = {} |
Definition at line 200 of file tz_secure_only_usb_hs_steps.c.
|
static |
Stack backing storage for s_demo_thread.
Definition at line 82 of file tz_secure_only_usb_hs_steps.c.
|
static |
ThreadX TCB for the USBX worker thread.
Definition at line 75 of file tz_secure_only_usb_hs_steps.c.
| volatile uint32_t s_host_kick_done = 0U |
Set to 1 once the host-mode PHY analog kick has been applied.
Some Renesas USB-OTG PHYs latch into a stuck analog state after power-on if the controller never enters host mode. The mitigation is to briefly assert SYSCFG.DCFM + SYSCFG.DRPD (host + bus pull-down), wait, then clear them again before completing the device-mode bring-up. s_host_kick_done is incremented after the kick sequence completes so a JLink session can confirm the path was taken: mem32 &s_host_kick_done should read 1 after boot.
Definition at line 148 of file tz_secure_only_usb_hs_steps.c.
Referenced by demo_worker_start_dcd().
| volatile uint32_t s_intenb0_rearm_count = 0U |
Number of times the watchdog has re-asserted INTENB0 = 0xFF00.
Increments once per polling tick where INTENB0 != 0xFF00 was observed. A non-zero value here proves the watchdog is firing and that the IP is clearing INTENB0 (typically across a USB Bus Reset). Read via JLink: mem32 &s_intenb0_rearm_count.
Definition at line 122 of file tz_secure_only_usb_hs_steps.c.
Referenced by intenb0_watchdog_entry().
|
static |
Stack backing storage for the INTENB0 watchdog thread.
Definition at line 107 of file tz_secure_only_usb_hs_steps.c.
Referenced by demo_worker_spawn_intenb0_watchdog().
| volatile uint32_t s_intenb0_watchdog_started = 0U |
Set to 1 once the watchdog thread has begun polling.
Definition at line 130 of file tz_secure_only_usb_hs_steps.c.
Referenced by intenb0_watchdog_entry().
|
static |
ThreadX TCB for the INTENB0 re-arm watchdog.
USB Bus Reset on the HS controller auto-clears INTENB0 to 0. The driver's busreset_rearm is supposed to re-set INTENB0 = 0xFF00 from the ISR's DVST(=Default) handler, but the ISR cannot fire while INTENB0 is zero (chicken-and-egg). This dedicated low-priority thread polls INTENB0 every ThreadX tick and re-asserts the device- mode interrupt mask whenever the IP has cleared it. With the mask back in place, the next bus event raises USBI0 and the ISR fires.
Definition at line 100 of file tz_secure_only_usb_hs_steps.c.
Referenced by demo_worker_spawn_intenb0_watchdog().
| volatile uint16_t s_lpsts_in_echo_loop = 0U |
LPSTS snapshot on first echo-loop iteration.
Companion to s_syscfg_in_echo_loop; HUM Ch 37.2.43 LPSTS p 2111. Expected SUSPENDM=1 (0x4000).
Definition at line 244 of file tz_secure_only_usb_hs_steps.c.
Referenced by demo_worker_capture_echo_probes().
| volatile uint32_t s_pendsv_observed_run_count = 0U |
Definition at line 177 of file tz_secure_only_usb_hs_steps.c.
| volatile uint32_t s_ppar_state = 0U |
Snapshot of R_PSCU->PPARB on entry to the echo loop.
PSCU base = 0x40204000, PPARB offset 0x1C (HUM Ch 51.8.6 "PPARB : Peripheral Privilege Attribution Register B" p 3292). Bit 12 (PPARB12) is the USBHS Type1 privileged/unprivileged bit: 0 = Privileged-only access, 1 = Unprivileged access permitted. Reset value = 0xFFFFFFFF – USBHS defaults to "unprivileged access permitted", which means both privileged-secure and unprivileged- secure register accesses are allowed by the TrustZone Filter.
Definition at line 282 of file tz_secure_only_usb_hs_steps.c.
Referenced by demo_worker_capture_echo_probes().
| volatile uint32_t s_psar_state = 0U |
Snapshot of R_PSCU->PSARB on entry to the echo loop.
PSCU base = 0x40204000, PSARB offset 0x04 (HUM Ch 51.8.1 "PSARB : Peripheral Security Attribution Register B" p 3284). Bit 12 (PSARB12) is the USBHS Type1 secure/non-secure bit; bit 11 (PSARB11) is the USBFS0 equivalent. 0 = Secure, 1 = Non-secure. Reset value = 0x00000000 – both modules default to Secure, which is what this no-op-TrustZone build wants. We capture it once at the echo-loop entry so a JLink session can verify nothing else (e.g. ThreadX init or the USBX class layer) flipped PSARB12 to NS out from under us.
Definition at line 264 of file tz_secure_only_usb_hs_steps.c.
Referenced by demo_worker_capture_echo_probes().
| volatile uint16_t s_syscfg_in_echo_loop = 0U |
SYSCFG snapshot taken on the FIRST iteration of the echo loop.
Bisect probe (HUM Ch 37.2.1 SYSCFG p 2060) for the "USBE clears between phy bring-up and echo loop" regression. Captured exactly once – before the loop body has had a chance to do any USBX work – so a JLink session can compare it against ::s_syscfg_after_attach to localise whether anything between ra8_usb_device_attach and the echo-loop entry clears USBE.
Definition at line 235 of file tz_secure_only_usb_hs_steps.c.
Referenced by demo_worker_capture_echo_probes().
|
static |
USBX memory pool (USBX uses tx_byte_pool internally).
Definition at line 155 of file tz_secure_only_usb_hs_steps.c.