|
ra8-firmware 0.1.0
Bare-metal firmware for the Renesas RA8 family (RA8D2 / RA8P1)
|
Host-side self-test ladder, console formatters, and echo pattern. More...
#include "usb_selftest_cdc_steps.h"#include <stdint.h>#include <string.h>#include "ra8_board_ek_ra8d2.h"#include "ra8_err.h"#include "ra8_time.h"#include "ra8_usb.h"Go to the source code of this file.
Enumerations | |
| enum | cdc_hex_t : uint8_t { k_cdc_hex_chars_u16 = 4U , k_cdc_hex_chars_u32 = 8U , k_cdc_dec_chars_u32 = 10U , k_cdc_nibble_bits = 4U , k_cdc_hex_digit_split = 10U } |
| Hex/decimal text-formatter sizing constants. More... | |
| enum | cdc_mask_t : uint32_t { k_cdc_nibble_mask = 0xFU , k_cdc_dec_radix = 10U } |
| Bit-mask constants used by the text formatters. More... | |
| enum | cdc_phase_t : uint32_t { k_cdc_phase_boot = 0U , k_cdc_phase_init = 1U , k_cdc_phase_enum = 2U , k_cdc_phase_verify = 3U , k_cdc_phase_pass = 4U } |
| J-Link probe values marking host-ladder progress. More... | |
| enum | cdc_usb_req_t : uint16_t { k_cdc_bm_std_dev_in = 0x80U , k_cdc_bm_std_dev_out = 0x00U , k_cdc_breq_get_desc = 0x06U , k_cdc_breq_set_addr = 0x05U , k_cdc_breq_set_config = 0x09U , k_cdc_desc_device = 0x01U , k_cdc_dev_desc_len = 18U , k_cdc_off_dev_pid = 10U , k_cdc_byte_bits = 8U , k_cdc_config_value = 1U } |
| Standard chapter-9 request / descriptor constants for the host. More... | |
| enum | cdc_enum_tune_t : uint32_t { k_cdc_vbus_settle_ms = 200U , k_cdc_attach_to_ms = 2000U , k_cdc_debounce_ms = 500U , k_cdc_reset_hold_ms = 50U , k_cdc_recovery_ms = 20U , k_cdc_addr_settle_ms = 5U , k_cdc_enum_tries = 8U , k_cdc_attach_spin = 50000000U } |
| Timing / retry tunables for the polled enumeration ladder. More... | |
Functions | |
| static void | cdc_pattern_fill (uint32_t round, uint8_t *out, uint32_t len) |
| Fill an echo payload with this round's deterministic bytes. | |
| static uint8_t | cdc_nibble_to_hex (uint32_t nibble) |
| Format one nibble (0..15) into an uppercase hex character. | |
| static uint32_t | cdc_str_len (const char *text) |
| Bounded ASCII string length (cap k_cdc_print_cap). | |
| static ra8_err_t | cdc_sci_write (const uint8_t *data, uint32_t len) |
| Push a literal block over SCI8 polled. | |
| static ra8_err_t | cdc_print (const char *text) |
| Print a NUL-terminated ASCII string over the console. | |
| static ra8_err_t | cdc_print_dec (uint32_t value) |
| Print a uint32_t as ASCII decimal. | |
| static ra8_err_t | cdc_print_hex (uint32_t value, uint8_t digits) |
| Print a value as fixed-width uppercase hex. | |
| static ra8_err_t | cdc_print_fail (const char *what, ra8_err_t err) |
| Print "FAIL <what> err=0xNNNNNNNN" on its own line. | |
| static ra8_err_t | cdc_ctrl_get_dev_desc (uint8_t *desc) |
| GET_DESCRIPTOR(DEVICE) over the polled control engine. | |
| static ra8_err_t | cdc_enum_hunt (uint8_t *desc) |
| Wait for the device to attach, then bus-reset + read its descriptor. | |
| static ra8_err_t | cdc_enum_set_address (void) |
| SET_ADDRESS to k_cdc_dev_addr, then retarget the DCP. | |
| static ra8_err_t | cdc_enum_set_config (void) |
| SET_CONFIGURATION(::k_cdc_config_value) on the addressed device. | |
| static ra8_err_t | cdc_open_pipes (void) |
| Open the host bulk pipes for the device's CDC data endpoints. | |
| static ra8_err_t | cdc_enumerate (uint32_t *out_pid) |
| Run the full enumeration ladder and open the bulk pipes. | |
| static ra8_err_t | cdc_print_enum (uint32_t pid) |
| Print "enumerated pid=0xNNNN" for the looped device. | |
| static ra8_err_t | cdc_print_pass (void) |
| Print "N rounds echoed -- USB SELFTEST CDC-ECHO PASS". | |
| static ra8_err_t | cdc_echo_round (uint32_t round) |
| One echo round: bulk-OUT a pattern, bulk-IN the echo, compare. | |
| static ra8_err_t | cdc_host_pass (void) |
| One full host-side pass: enumerate, then run the echo rounds. | |
| VOID | cdc_host_worker (ULONG arg) |
| Host-side worker: retry the full pass until it succeeds. | |
Variables | |
| static volatile uint32_t | s_dbg_phase |
| Host-ladder phase marker (cdc_phase_t). | |
| static volatile uint32_t | s_dbg_rounds_ok |
| Echo rounds the host confirmed byte-equal (expect k_cdc_rounds). | |
| static volatile uint32_t | s_dbg_pid |
| Device-reported product id captured at enumeration. | |
| static volatile uint32_t | s_dbg_mismatch = (uint32_t)k_cdc_no_mismatch |
| First mismatching round, or k_cdc_no_mismatch. | |
| static volatile uint32_t | s_dbg_pass_count |
| Completed full passes (sticky success counter). | |
Host-side self-test ladder, console formatters, and echo pattern.
Companion translation unit for the usb_selftest_cdc HIL example, carved out of main.c so every unit stays under the file-size cap. It holds the SCI8 console formatters, the shared per-round echo pattern, and the self-contained polled CDC host ladder (enumerate -> open bulk pipes -> run the echo rounds) that the host worker drives. main.c retains the device-side USBX worker, the board bring-up, and the entry point, and only reaches in through cdc_host_worker.
The host-side J-Link probes (s_dbg_*) live here because only the host routines write them; the device-side probes stay in main.c.
Definition in file usb_selftest_cdc_steps.c.
| enum cdc_enum_tune_t : uint32_t |
Timing / retry tunables for the polled enumeration ladder.
Definition at line 367 of file usb_selftest_cdc_steps.c.
| enum cdc_hex_t : uint8_t |
Hex/decimal text-formatter sizing constants.
Definition at line 43 of file usb_selftest_cdc_steps.c.
| enum cdc_mask_t : uint32_t |
Bit-mask constants used by the text formatters.
| Enumerator | |
|---|---|
| k_cdc_nibble_mask | 4-bit nibble mask. |
| k_cdc_dec_radix | Base for decimal conversion. |
Definition at line 55 of file usb_selftest_cdc_steps.c.
| enum cdc_phase_t : uint32_t |
J-Link probe values marking host-ladder progress.
Definition at line 64 of file usb_selftest_cdc_steps.c.
| enum cdc_usb_req_t : uint16_t |
Standard chapter-9 request / descriptor constants for the host.
Definition at line 350 of file usb_selftest_cdc_steps.c.
|
staticnodiscard |
GET_DESCRIPTOR(DEVICE) over the polled control engine.
| [out] | desc | Receives the 18-byte device descriptor. |
| k_ra8_ok | All 18 bytes arrived. |
| k_ra8_err_hw_error | A short descriptor came back. |
desc holds at least k_cdc_dev_desc_len bytes. desc carries the device descriptor on success. Definition at line 395 of file usb_selftest_cdc_steps.c.
References k_cdc_bm_std_dev_in, k_cdc_breq_get_desc, k_cdc_byte_bits, k_cdc_desc_device, k_cdc_dev_desc_len, k_ra8_err_hw_error, k_ra8_ok, k_ra8_usb_speed_hs, and ra8_usb_host_control_xfer().
Referenced by cdc_enum_hunt().
|
staticnodiscard |
One echo round: bulk-OUT a pattern, bulk-IN the echo, compare.
Fills k_cdc_payload bytes from cdc_pattern_fill keyed on round, ships them on the bulk-OUT pipe (one sub-MPS packet), then reads the bulk-IN echo and byte-checks it. A sub-MPS payload returns as a single short packet, so no MPS-exact ZLP is involved.
| [in] | round | The echo round index (0..k_cdc_rounds-1). |
| k_ra8_ok | The echo matched the sent payload. |
| k_ra8_err_invalid_size | The echo length differed. |
| k_ra8_err_invalid_state | The echo bytes differed. |
round on any mismatch. Definition at line 687 of file usb_selftest_cdc_steps.c.
References cdc_pattern_fill(), cdc_print_fail(), k_cdc_echo_buf, k_cdc_payload, k_cdc_pipe_in, k_cdc_pipe_out, k_ra8_err_invalid_size, k_ra8_err_invalid_state, k_ra8_ok, k_ra8_usb_speed_hs, memcmp(), ra8_usb_host_bulk_in(), ra8_usb_host_bulk_out(), and s_dbg_mismatch.
Referenced by cdc_host_pass().
|
staticnodiscard |
Wait for the device to attach, then bus-reset + read its descriptor.
Waits for the D+ pull-up (LNST leaves SE0) plus the spec debounce, then each attempt drives a full bus reset (returns the device to address 0), re-enables SOF, targets address 0, and reads the device descriptor. The first attempt that returns all 18 bytes wins.
| [out] | desc | Receives the winning 18-byte device descriptor. |
| k_ra8_ok | The device answered at address 0. |
| k_ra8_err_hw_timeout | Nothing attached / nothing answered. |
Definition at line 438 of file usb_selftest_cdc_steps.c.
References cdc_ctrl_get_dev_desc(), k_cdc_attach_spin, k_cdc_attach_to_ms, k_cdc_debounce_ms, k_cdc_enum_tries, k_cdc_recovery_ms, k_cdc_reset_hold_ms, k_cdc_vbus_settle_ms, k_ra8_err_hw_timeout, k_ra8_ok, k_ra8_usb_speed_hs, ra8_delay_ms(), ra8_time_ms(), ra8_usb_host_bus_reset(), ra8_usb_host_line_state(), ra8_usb_host_set_target(), and ra8_usb_host_set_uact().
Referenced by cdc_enumerate().
|
staticnodiscard |
SET_ADDRESS to k_cdc_dev_addr, then retarget the DCP.
| k_ra8_ok | The DCP now targets the operating address. |
Definition at line 481 of file usb_selftest_cdc_steps.c.
References k_cdc_addr_settle_ms, k_cdc_bm_std_dev_out, k_cdc_breq_set_addr, k_cdc_dev_addr, k_ra8_ok, k_ra8_usb_speed_hs, ra8_delay_ms(), ra8_usb_host_control_xfer(), and ra8_usb_host_set_target().
Referenced by cdc_enumerate().
|
staticnodiscard |
SET_CONFIGURATION(::k_cdc_config_value) on the addressed device.
| k_ra8_ok | The device entered the Configured state. |
Definition at line 512 of file usb_selftest_cdc_steps.c.
References k_cdc_bm_std_dev_out, k_cdc_breq_set_config, k_cdc_config_value, k_ra8_usb_speed_hs, and ra8_usb_host_control_xfer().
Referenced by cdc_enumerate().
|
staticnodiscard |
Run the full enumeration ladder and open the bulk pipes.
hunt -> SET_ADDRESS -> SET_CONFIGURATION -> open pipes, and extract idProduct from the device descriptor. Each step prints its own failure tag. The caller deinitializes the host controller on error.
| [out] | out_pid | Receives the device's idProduct on success. |
| k_ra8_ok | Device enumerated; bulk pipes open. |
out_pid is non-NULL. out_pid holds the device idProduct on success. Definition at line 581 of file usb_selftest_cdc_steps.c.
References cdc_enum_hunt(), cdc_enum_set_address(), cdc_enum_set_config(), cdc_open_pipes(), cdc_print_fail(), k_cdc_byte_bits, k_cdc_dev_desc_len, k_cdc_off_dev_pid, and k_ra8_ok.
Referenced by cdc_host_pass().
|
staticnodiscard |
One full host-side pass: enumerate, then run the echo rounds.
Phases mirror cdc_phase_t. On any failure the host controller is deinitialized so the next retry starts from a clean attach.
| k_ra8_ok | The pass printed CDC-ECHO PASS. |
Definition at line 740 of file usb_selftest_cdc_steps.c.
References cdc_echo_round(), cdc_enumerate(), cdc_print(), cdc_print_enum(), cdc_print_fail(), cdc_print_pass(), k_cdc_phase_enum, k_cdc_phase_init, k_cdc_phase_pass, k_cdc_phase_verify, k_cdc_rounds, k_ra8_board_led2, k_ra8_ok, k_ra8_usb_speed_hs, ra8_board_led_on(), ra8_usb_host_deinit(), ra8_usb_host_init(), s_dbg_pass_count, s_dbg_phase, s_dbg_pid, and s_dbg_rounds_ok.
Referenced by cdc_host_worker().
| VOID cdc_host_worker | ( | ULONG | arg | ) |
Host-side worker: retry the full pass until it succeeds.
Waits for the device side to attach, then loops the host pass with a retry pause until every echo round verifies; afterwards parks so the verdict stays on the wire. Spawned by tx_application_define in main.c as the host ThreadX entry.
| [in] | arg | ThreadX entry argument (unused). |
Definition at line 787 of file usb_selftest_cdc_steps.c.
References cdc_host_pass(), k_cdc_boot_wait_ticks, k_cdc_idle_ticks, k_cdc_retry_ticks, k_ra8_ok, and tx_thread_sleep.
Referenced by tx_application_define().
|
static |
Format one nibble (0..15) into an uppercase hex character.
Standard '0'-'9' then 'A'-'F' mapping.
| [in] | nibble | 4-bit value. |
| '0' | For a zero nibble. |
Definition at line 142 of file usb_selftest_cdc_steps.c.
References k_cdc_hex_digit_split.
Referenced by cdc_print_hex().
|
staticnodiscard |
Open the host bulk pipes for the device's CDC data endpoints.
Pipe k_cdc_pipe_out -> device EP2 OUT (host transmits), pipe k_cdc_pipe_in -> device EP1 IN (host receives), both 64-byte MPS. Endpoint numbers come from this app's own device framework.
| k_ra8_ok | Both bulk pipes configured and parked NAK. |
Definition at line 542 of file usb_selftest_cdc_steps.c.
References k_cdc_dev_addr, k_cdc_ep_in_num, k_cdc_ep_out_num, k_cdc_mps, k_cdc_pipe_in, k_cdc_pipe_out, k_ra8_ok, k_ra8_usb_speed_hs, and ra8_usb_host_pipe_setup().
Referenced by cdc_enumerate().
|
static |
Fill an echo payload with this round's deterministic bytes.
Byte i = (round*97 + i*7 + 0x5A) & 0xFF. Distinct per round so the host proves the bytes it read back are the ones it sent for that round (not a stale buffer). The device merely echoes, so only the host computes this; both the OUT payload and the IN check use it.
| [in] | round | The echo round index (0..k_cdc_rounds-1). |
| [out] | out | Destination buffer. |
| [in] | len | Bytes to fill. |
out has len writable bytes. len is at most k_cdc_payload. out[0..len-1] hold the round's pattern bytes. Definition at line 111 of file usb_selftest_cdc_steps.c.
References k_cdc_byte_mask, k_cdc_pat_bias, k_cdc_pat_idx_mul, and k_cdc_pat_round_mul.
Referenced by cdc_echo_round().
|
staticnodiscard |
Print a NUL-terminated ASCII string over the console.
Length-bounded by cdc_str_len.
| [in] | text | String to print (CR/LF included by the caller). |
| k_ra8_ok | All bytes queued. |
text is non-NULL. text is NUL-terminated within k_cdc_print_cap bytes. Definition at line 222 of file usb_selftest_cdc_steps.c.
References cdc_sci_write(), and cdc_str_len().
Referenced by cdc_host_pass(), cdc_print_enum(), cdc_print_fail(), and cdc_print_pass().
|
staticnodiscard |
Print a uint32_t as ASCII decimal.
Digit-reversal into a bounded scratch buffer.
| [in] | value | Value to print. |
| k_ra8_ok | All bytes queued. |
Definition at line 245 of file usb_selftest_cdc_steps.c.
References cdc_sci_write(), k_cdc_dec_chars_u32, and k_cdc_dec_radix.
Referenced by cdc_print_pass().
|
staticnodiscard |
Print "enumerated pid=0xNNNN" for the looped device.
| [in] | pid | The device idProduct to report. |
| k_ra8_ok | The line is queued. |
Definition at line 624 of file usb_selftest_cdc_steps.c.
References cdc_print(), cdc_print_hex(), k_cdc_hex_chars_u16, and k_ra8_ok.
Referenced by cdc_host_pass().
Print "FAIL <what> err=0xNNNNNNNN" on its own line.
One-line diagnostic; first failing chunk's code returned.
| [in] | what | Short description of the failed step. |
| [in] | err | Error code returned by the step. |
| k_ra8_ok | The diagnostic line is queued. |
what is NUL-terminated within the print cap. Definition at line 321 of file usb_selftest_cdc_steps.c.
References cdc_print(), cdc_print_hex(), k_cdc_hex_chars_u32, and k_ra8_ok.
Referenced by cdc_echo_round(), cdc_enumerate(), and cdc_host_pass().
|
staticnodiscard |
Print a value as fixed-width uppercase hex.
Width is clamped to 8 hex digits.
| [in] | value | Value to print. |
| [in] | digits | Hex digit count (4 for u16, 8 for u32). |
| k_ra8_ok | All bytes queued. |
digits is at most k_cdc_hex_chars_u32. Definition at line 288 of file usb_selftest_cdc_steps.c.
References cdc_nibble_to_hex(), cdc_sci_write(), k_cdc_hex_chars_u32, k_cdc_nibble_bits, and k_cdc_nibble_mask.
Referenced by cdc_print_enum(), and cdc_print_fail().
|
staticnodiscard |
Print "N rounds echoed -- USB SELFTEST CDC-ECHO PASS".
| k_ra8_ok | The verdict line is queued. |
Definition at line 651 of file usb_selftest_cdc_steps.c.
References cdc_print(), cdc_print_dec(), k_cdc_rounds, and k_ra8_ok.
Referenced by cdc_host_pass().
|
staticnodiscard |
Push a literal block over SCI8 polled.
Thin wrapper fixing the console channel.
| [in] | data | Buffer to send. |
| [in] | len | Byte count. |
| k_ra8_ok | All bytes queued. |
data is non-NULL; SCI8 init already ran. len excludes any NUL terminator. Definition at line 199 of file usb_selftest_cdc_steps.c.
References ra8_board_uart_console_write().
Referenced by cdc_print(), cdc_print_dec(), and cdc_print_hex().
|
static |
Bounded ASCII string length (cap k_cdc_print_cap).
Linear scan with a hard upper bound.
| [in] | text | NUL-terminated string. |
| 0 | For an empty string. |
text is non-NULL. text points to readable storage of at least the length. Definition at line 168 of file usb_selftest_cdc_steps.c.
References k_cdc_print_cap.
Referenced by cdc_print().
|
static |
First mismatching round, or k_cdc_no_mismatch.
Definition at line 83 of file usb_selftest_cdc_steps.c.
|
static |
Completed full passes (sticky success counter).
Definition at line 85 of file usb_selftest_cdc_steps.c.
|
static |
Host-ladder phase marker (cdc_phase_t).
Definition at line 77 of file usb_selftest_cdc_steps.c.
|
static |
Device-reported product id captured at enumeration.
Definition at line 81 of file usb_selftest_cdc_steps.c.
|
static |
Echo rounds the host confirmed byte-equal (expect k_cdc_rounds).
Definition at line 79 of file usb_selftest_cdc_steps.c.