60#include "ux_device_class_hid.h"
61#include "ux_device_stack.h"
124typedef enum : uint32_t {
132#ifndef RA8_OFF_TARGET
197 return (
char)(
'A' + (int)(kc - (uint8_t)
k_hid_kc_a));
200 return (
char)(
'1' + (int)(kc - (uint8_t)
k_hid_kc_1));
216typedef enum : uint16_t {
233typedef enum : uint32_t {
318 for (uint8_t attempt = 0U; attempt < (uint8_t)
k_hid_enum_tries; attempt++) {
523 err =
hid_print(
" reports -- USB HOST KEYBOARD PASS\r\n");
581 for (uint32_t i = 0U; i < (uint32_t)
k_hid_nkeys; i++) {
633 for (uint32_t r = 0U; r < (uint32_t)
k_hid_rounds; r++) {
706 static CHAR s_semaphore_name[] =
"hid_active";
709 (void)first_unused_memory;
746 __asm__
volatile(
"wfi");
807 uint32_t cpuclk0_hz = 0U;
855#ifndef RA8_OFF_TARGET
void main(void)
Secure fallback main entry point.
void tx_application_define(void *first_unused_memory)
ThreadX system-define hook: build worker thread + byte pool.
void _tx_timer_interrupt(void)
void SysTick_Handler(void)
SysTick exception handler – tail-calls the ThreadX timer.
static volatile bool s_tx_kernel_up
Set in tx_application_define; gates ThreadX tick delivery.
static UCHAR s_host_stack[k_wlun_host_stack]
Stack backing storage for s_host_thread.
static CHAR s_host_thread_name[]
static TX_THREAD s_host_thread
ThreadX TCB for the host-side worker thread.
static volatile uint32_t s_dbg_pid
Device-reported product id captured at enumeration.
static void hid_route_usb_or_halt(void)
Route both ports' pins: FS as device, HS as host.
static void hid_setup_or_halt(void)
Bring CGC + both USB clocks + SysTick + SCI8 + LEDs + pins up.
hid_phase_t
J-Link probe values marking host-ladder progress.
@ k_hid_phase_enum
Enumerating.
@ k_hid_phase_boot
Host thread not started.
@ k_hid_phase_pass
All reports verified.
@ k_hid_phase_verify
Reading + checking reports.
@ k_hid_phase_init
Host controller init.
static volatile uint32_t s_dbg_last_seq
Seq byte of the most recent report the host read back.
static ra8_err_t hid_read_round(uint32_t round)
One report round: read an interrupt-IN report, check its body.
static ra8_err_t hid_print_enum(uint32_t pid)
Print "enumerated pid=0xNNNN" for the looped device.
static const ra8_port_pin_t k_hid_pin_hs_vbus
USBHS_VBUS sense pin (P4_08, PSEL = 0x14).
static void hid_panic_halt(void)
Halt forever in WFI – panic stop on init failure.
static ra8_err_t hid_enum_hunt(uint8_t *desc)
Wait for the device to attach, then bus-reset + read its descriptor.
static const ra8_port_pin_t k_hid_pin_fs_dm
USBFS D- (P8_15).
static ra8_err_t hid_enumerate(uint32_t *out_pid)
Run the full enumeration ladder and open the bulk pipes.
static ra8_err_t hid_print_pass(void)
Print the decoded keys + "USB HOST KEYBOARD PASS".
static ra8_err_t hid_open_pipes(void)
Open the host receive pipe for the device's HID interrupt-IN endpoint.
hid_usb_req_t
Standard chapter-9 request / descriptor constants for the host.
@ k_hid_desc_device
DEVICE descriptor type.
@ k_hid_dev_desc_len
DEVICE descriptor length.
@ k_hid_bm_std_dev_out
bmRequestType: Std | Device | Out.
@ k_hid_config_value
bConfigurationValue to select.
@ k_hid_byte_bits
Bits per byte.
@ k_hid_off_dev_pid
idProduct LSB byte offset.
@ k_hid_breq_set_config
SET_CONFIGURATION.
@ k_hid_bm_std_dev_in
bmRequestType: Std | Device | In.
@ k_hid_breq_set_addr
SET_ADDRESS.
@ k_hid_breq_get_desc
GET_DESCRIPTOR.
static ra8_err_t hid_enum_set_address(void)
SET_ADDRESS to k_hid_dev_addr, then retarget the DCP.
static const ra8_port_pin_t k_hid_pin_fs_vbusen
USBFS VBUSEN (P5_00) – GPIO LOW for the device role.
hid_enum_tune_t
Timing / retry tunables for the polled enumeration ladder.
@ k_hid_enum_tries
Reset+probe attempts.
@ k_hid_vbus_settle_ms
VBUS settle before probing.
@ k_hid_recovery_ms
Post-reset recovery (TRSTRCY).
@ k_hid_addr_settle_ms
Post-SET_ADDRESS recovery.
@ k_hid_reset_hold_ms
USB bus-reset hold (>=10 ms).
@ k_hid_attach_spin
Attach spin cap (frozen-tick guard).
@ k_hid_attach_to_ms
Wait for the D+ pull-up.
@ k_hid_debounce_ms
Post-attach debounce (>=100 ms).
static const ra8_port_pin_t k_hid_pin_fs_dp
USBFS D+ (P8_14).
static VOID hid_host_worker(ULONG arg)
Host-side worker: retry the full pass until it succeeds.
static volatile uint32_t s_dbg_rounds_ok
Reports the host confirmed pattern-equal (expect k_hid_rounds).
static ra8_err_t hid_enum_set_config(void)
SET_CONFIGURATION(::k_hid_config_value) on the addressed device.
static char s_typed[k_hid_nkeys+1U]
ASCII the host decoded from the read keycodes (filled by the verify).
static ra8_err_t hid_host_pass(void)
One full host-side pass: enumerate, then read + check reports.
static const ra8_port_pin_t k_hid_pin_hs_pwr
J7 host-power switch (PD07): HIGH = U18 supplies VBUS (UM 6.2).
static char hid_keycode_to_ascii(uint8_t kc)
Decode one HID Usage-Table keycode to its ASCII character.
static const ra8_port_pin_t k_hid_pin_fs_vbus
USBFS VBUS sense pin (P4_07, PSEL = 0x13).
static ra8_err_t hid_ctrl_get_dev_desc(uint8_t *desc)
GET_DESCRIPTOR(DEVICE) over the polled control engine.
Board-support layer for the Renesas EK-RA8D2 v1 evaluation kit.
ra8_err_t ra8_board_led_init(ra8_board_led_id_t led)
Configure led as a digital output, initial level low (off).
ra8_err_t ra8_board_led_on(ra8_board_led_id_t led)
Drive led HIGH (light it).
@ k_ra8_board_led2
LED2, GREEN, P303 (jumper E26).
@ k_ra8_board_led1
LED1, BLUE, P600 (jumper E27).
ra8_err_t ra8_board_io_expander_set_usbhs_host_mode(void)
Drive the U15 I/O expander to select USB-HS HOST mode.
ra8_err_t ra8_board_uart_console_init(uint32_t baud)
Configure SCI8 + PD02/PD03 as the debug-console UART.
@ k_ra8_board_usbhs_pin_pwr
PD07 J7 host-power switch (HIGH = U18 drives 5 V VBUS).
@ k_ra8_board_usbhs_pin_vbus
P4_08 USBHS_VBUS sense.
@ k_ra8_board_usbfs_pin_dm
P8_15 D-.
@ k_ra8_board_usbfs_pin_vbusen
P5_00 VBUSEN GPIO.
@ k_ra8_board_usbfs_pin_vbus
P4_07 VBUS sense.
@ k_ra8_board_usbfs_pin_dp
P8_14 D+.
Boot entry points shared between a vector table and its startup code.
High-level Clock Generation Circuit driver.
ra8_err_t ra8_cgc_get_clock_hz(ra8_clock_id_t id, uint32_t *out_hz)
Query the current frequency of a clock-tree domain.
@ k_ra8_clock_id_cpuclk0
Cortex-M85 CPUCLK0.
ra8_err_t ra8_cgc_init(void)
Configure the clock tree to a safe default.
ra8_err_t ra8_cgc_usbfs_clock_enable(void)
Bring up the USB-FS module clock (USBCKCR / USBCKDIVCR).
ra8_err_t ra8_cgc_usbhs_pll_enable(void)
Bring up the USBHS PHY reference clock + module clock.
Error Code Definitions for ra8-firmware.
@ k_ra8_err_invalid_state
Module in wrong state for requested operation.
@ k_ra8_err_hw_timeout
Hardware timed out waiting for a flag or handshake.
@ k_ra8_ok
Success – operation completed with all postconditions satisfied.
@ k_ra8_err_hw_error
Generic hardware fault detected (error flag set, fault interrupt).
@ k_ra8_err_invalid_size
Invalid size parameter (too large, too small, or misaligned).
ra8_err_codes_t ra8_err_t
Canonical error-return type used by every ra8-firmware API.
int memcmp(const void *a, const void *b, size_t n)
Compare bytes in two memory areas.
PSEL codes for peripheral pin routing on the RA8D2.
@ k_ra8_psel_usb_fs
10011b: USB Full-Speed.
@ k_ra8_psel_usb_hs
10100b: USB High-Speed (e.g.
NVIC + ICU IELSR allocator.
void ra8_isr_globals_enable(void)
Globally enable maskable interrupts (PRIMASK = 0).
Typed Port / Pin Constants for the RA8D2 IOPORT Module.
ra8_port_pin_t
Packed (port << 8) | pin pin identifier.
@ k_ra8_level_high
Drive or read 1.
@ k_ra8_level_low
Drive or read 0.
High-level GPIO helpers on top of the PORT + PFS register layer.
ra8_err_t ra8_gpio_output_init(ra8_port_pin_t pin, ra8_level_t init_level)
Configure a pin as a digital output and drive it to an initial level.
ra8_err_t ra8_pfs_route_peripheral(ra8_port_pin_t pin, ra8_psel_t psel, const char *owner)
Route a pin to a non-IRQ peripheral function via PFS.PSEL.
void ux_dcd_ra8_usb_irq_reenable(void)
Storm-guard recovery: zero the run counter and re-enable the USB IRQ.
SysTick-based tick counter, delay and timestamp helpers.
ra8_err_t ra8_time_init(uint32_t cpu_hz)
Initialise SysTick for a 1 kHz tick interrupt.
void ra8_time_on_tick(void)
SysTick IRQ handler – called from the vector table.
void ra8_delay_ms(uint32_t ms)
Busy-wait for at least ms milliseconds.
uint32_t ra8_time_ms(void)
Get the current 1 kHz tick count.
Native USB controller driver public API (device + host modes).
@ k_ra8_usb_speed_hs
High-Speed controller (USBHS @ 0x40351000).
ra8_err_t ra8_usb_host_bus_reset(ra8_usb_speed_t speed, bool assert_reset)
Drive (or release) the bus reset line.
uint16_t ra8_usb_host_line_state(ra8_usb_speed_t speed)
Read the host port's D+/D- line state (SYSSTS0.LNST).
ra8_err_t ra8_usb_host_pipe_setup(ra8_usb_speed_t speed, uint8_t pipe_num, uint8_t dev_addr, uint8_t ep_num, bool device_to_host, uint16_t max_packet)
Configure a controller pipe against an attached device's bulk endpoint.
ra8_err_t ra8_usb_host_bulk_in(ra8_usb_speed_t speed, uint8_t pipe_num, uint8_t *buf, uint16_t max_len, uint16_t *out_received)
Receive a bulk transfer from the device into buf.
ra8_err_t ra8_usb_host_init(ra8_usb_speed_t speed)
Bring up a USB controller in HOST mode.
ra8_err_t ra8_usb_host_deinit(ra8_usb_speed_t speed)
Tear down a host-mode controller and drop its MSTP reference.
ra8_err_t ra8_usb_host_control_xfer(ra8_usb_speed_t speed, const ra8_usb_setup_t *setup, uint8_t *data, uint16_t data_len, uint16_t *out_received)
Run a complete host control transfer (SETUP, optional DATA, STATUS).
ra8_err_t ra8_usb_host_set_uact(ra8_usb_speed_t speed, bool enable)
Enable / disable SOF (Start-of-Frame) generation on the bus.
ra8_err_t ra8_usb_host_set_target(ra8_usb_speed_t speed, uint8_t dev_addr)
Retarget the host's default control pipe at a device address.
unsigned int UINT
ThreadX-compatible unsigned int (host stub).
char CHAR
ThreadX-compatible CHAR (host stub).
unsigned long ULONG
ThreadX-compatible unsigned long (host stub).
Opaque thread stand-in for the host build.
Decoded 8-byte USB SETUP packet.
Shared seam between usb_host_keyboard main.c and its sibling TUs.
void usb_host_keyboard_device_thread_create(void)
Create + auto-start the USBX HID device worker thread.
ra8_err_t hid_print(const char *text)
Print a NUL-terminated ASCII string over the console.
ra8_err_t hid_print_fail(const char *what, ra8_err_t err)
Print "FAIL <what> err=0xNNNNNNNN" on its own line.
ra8_err_t hid_print_hex(uint32_t value, uint8_t digits)
Print a value as fixed-width uppercase hex.
@ k_hid_hex_chars_u16
16-bit value -> "ABCD".
void hid_fill_report_body(uint8_t *out, uint32_t len)
Fill the fixed body of a HID report (bytes 1..len-1).
ra8_err_t hid_print_dec(uint32_t value)
Print a uint32_t as ASCII decimal.
@ k_hid_report_len
Vendor input report width (bytes).
@ k_hid_ep_in_num
Device interrupt-IN endpoint num.
@ k_hid_rounds
Reports the host reads + checks.
@ k_hid_pipe_in
Host pipe for the device IN.
@ k_hid_nkeys
Keycodes typed ("RA8D2").
@ k_hid_mps
Interrupt endpoint wMaxPacketSize.
@ k_hid_no_mismatch
Probe: no mismatch.
@ k_hid_read_buf
One-MPS receive buffer (bytes).
@ k_hid_dev_addr
Address the host assigns.
@ k_hid_key0_idx
Boot-keyboard first-keycode byte.
@ k_hid_body_idx
Report body starts at byte 1.
@ k_hid_seq_idx
Report byte 0: modifier / rolling seq.
@ k_hid_kc_a
Keycode for 'a' / 'A'.
@ k_hid_kc_z
Keycode for 'z' / 'Z'.
@ k_hid_kc_1
Keycode for '1'.
@ k_hid_kc_0
Keycode for '0' (top of digits).
TX_SEMAPHORE s_usb_host_keyboard_hid_active_sem
Activation semaphore shared by the device worker and main.c.
@ k_hid_idle_ticks
Parked-loop back-off (ticks).
@ k_hid_host_stack
Host worker stack (bytes).
@ k_hid_host_priority
Host worker priority (below USBX).
@ k_hid_retry_ticks
Pause between ladder retries.
@ k_hid_baud
J-Link OB CDC log baud.
@ k_hid_boot_wait_ticks
Host start delay (1 ms ticks).
static volatile uint32_t s_dbg_phase
Host-ladder phase marker (wlun_phase_t).
static volatile uint32_t s_dbg_pass_count
Completed full passes (sticky success counter).
static volatile uint32_t s_dbg_mismatch
First mismatching sector, or k_wlun_no_mismatch.
USBX device-controller-driver (DCD) bridge to ra8_usb.