ra8-firmware 0.1.0
Bare-metal firmware for the Renesas RA8 family (RA8D2 / RA8P1)
Loading...
Searching...
No Matches
board_usb_vhost.c File Reference

Built-in virtual chapter-9 USB host (see board_usb_internal.h). More...

#include <stdio.h>
#include <string.h>
#include "board_usb_internal.h"
#include "emu_host_io_internal.h"
#include "ra8_elc_regs.h"
#include "ra8_usb_regs.h"
Include dependency graph for board_usb_vhost.c:

Go to the source code of this file.

Enumerations

enum  msc_phase_t : uint8_t {
  k_msc_send = 0U ,
  k_msc_data = 1U ,
  k_msc_csw = 2U ,
  k_msc_done = 3U
}
 MSC BOT per-command phase. More...
enum  msc_script_cmd_t : uint8_t {
  k_msc_cmd_inquiry = 0U ,
  k_msc_cmd_read_capacity = 1U ,
  k_msc_cmd_read10 = 2U
}
 Index of each command in the scripted SCSI sequence. More...
enum  msc_const_t : uint32_t {
  k_msc_cbw_len = 31U ,
  k_msc_csw_len = 13U ,
  k_msc_flag_in = 0x80U ,
  k_msc_settle = 4U ,
  k_msc_cmd_count = 3U
}
 BOT / SCSI sizing the host uses (USB Mass Storage BBB 1.0). More...

Functions

const char * priv_usb_class_active_str (void)
 Human-readable active-class suffix once the device is configured.
void priv_usb_detect_class (const uint8_t *d, uint16_t len)
 Detect the device class from a returned descriptor, if it is config.
static void internal_usb_hid_decode_report (const uint8_t *d, uint16_t len)
 Decode one HID boot-protocol mouse input report from the device.
bool priv_host_device_attached (void)
 True when SYSCFG.DPRPU is set: the device has attached its pull-up.
static void internal_host_latch_setup (const usb_setup_step_t *s)
 Latch a SETUP packet into USBREQ..USBLENG (host -> device).
static uint16_t internal_host_setup_ctsq (const usb_setup_step_t *s)
 CTSQ control-stage value the host advertises for a SETUP.
void priv_host_deliver_setup (uc_engine *uc, const usb_setup_step_t *s)
 Deliver the current script step's SETUP and raise the CTRT IRQ.
static void internal_host_drain_in (void)
 Drain the device's queued control-IN data as the host's read.
bool priv_host_dcp_pid_buf (void)
 DCPCTR.PID == BUF: the device has armed an IN response.
bool priv_host_take_ccpl (void)
 Observe (and clear) DCPCTR.CCPL: the device ended a control transfer.
void priv_host_apply_no_data (uc_engine *uc, const usb_setup_step_t *s)
 Apply the SIE-owned side effects of a no-data control request.
static void internal_host_step_deliver (uc_engine *uc, const usb_setup_step_t *s)
 Sub-state k_sub_deliver: latch + raise CTRT, then pick the next sub.
static void internal_host_step_wait_in (void)
 Sub-state k_sub_wait_in: await the device's control-IN response.
static void internal_host_step_status (uc_engine *uc)
 Sub-state k_sub_status: deliver the control-read status stage.
static void internal_host_step_wait_ack (void)
 Sub-state k_sub_wait_ack: await the device's CCPL (transfer end).
static uint32_t internal_host_script_len (void)
 Number of steps in the enumeration script.
void priv_host_mark_configured (uc_engine *uc)
 Mark the device CONFIGURED: advance DVSQ and raise DVST.
static void internal_host_step_next (uc_engine *uc, const usb_setup_step_t *s)
 Sub-state k_sub_next: record any post-step effect, advance the step.
void priv_host_run_setup_phase (uc_engine *uc)
 Run one micro-step of the active enumeration script entry.
void priv_host_run_idle_phase (uc_engine *uc)
 Phase k_phase_idle: wait for the device pull-up, then bus-reset.
void priv_host_run_reset_phase (uc_engine *uc)
 Phase k_phase_reset: hold reset a few ticks so the DCP re-arms.
static void internal_host_echo_send_out (uc_engine *uc)
 Deliver one bulk-OUT packet to the CDC data OUT pipe and signal BRDY.
void priv_host_echo_read_in (uc_engine *uc)
 Drain bytes the device queued on the IN pipe; ack the IN transfer.
static void internal_host_msc_build_cdb (uint8_t cmd, uint8_t *cdb, uint8_t *cdb_len, uint32_t *data_len)
 Build the SCSI CDB for the scripted command cmd.
static void internal_host_msc_send_cbw (uc_engine *uc)
 Push the current command's CBW onto the bulk-OUT pipe (raise BRDY).
static uint16_t internal_host_msc_take_in (uc_engine *uc, uint8_t *out, uint16_t cap)
 Consume one IN buffer (data or CSW) and acknowledge it with BEMP.
static void internal_host_msc_parse_capacity (const uint8_t *d, uint16_t n)
 Parse a READ CAPACITY (10) response into block count + size.
static void internal_host_msc_phase_send (uc_engine *uc)
 Phase k_msc_send: push the next CBW, or finish the script.
static void internal_host_msc_record_data (const uint8_t *d, uint16_t n)
 Record one data-phase burst against the command that is in flight.
static void internal_host_msc_phase_data (uc_engine *uc)
 Phase k_msc_data: drain the data phase until it is done or stalls.
static void internal_host_msc_phase_csw (uc_engine *uc)
 Phase k_msc_csw: take the CSW, then advance to the next command.
static void internal_host_msc_drive (uc_engine *uc)
 Drive the MSC BOT state machine one tick while CONFIGURED.
void priv_host_run_configured_phase (uc_engine *uc)
 Phase k_phase_configured: optionally drive the CDC bulk echo.

Variables

uint8_t s_dev_class
 Detected usb_dev_class_t.
uint32_t s_hid_reports
 HID input reports the host has read.
int32_t s_hid_cx
 Accumulated HID boot-mouse X.
int32_t s_hid_cy
 Accumulated HID boot-mouse Y.
uint8_t s_hid_buttons
 Last HID button bitmap.
static const usb_setup_step_t s_k_enum_script []
 The chapter-9 + CDC SETUP sequence the virtual host drives.
static uint8_t s_msc_phase
 msc_phase_t for the active command.
static uint8_t s_msc_cmd
 Index into the SCSI command script.
static uint32_t s_msc_tag
 Running dCBWTag.
static uint32_t s_msc_data_len
 Expected data-phase length.
static uint32_t s_msc_data_got
 Data-phase bytes read so far.
static uint32_t s_msc_wait
 Phase pacing.
uint32_t s_msc_blocks
 Capacity in blocks (READ CAPACITY).
uint32_t s_msc_block_len
 Block size in bytes.
uint32_t s_msc_read_ok
 Sector-read data-phase bytes captured.
bool s_msc_inquiry_ok
 INQUIRY data phase completed.

Detailed Description

Built-in virtual chapter-9 USB host (see board_usb_internal.h).

The device-class detection, the chapter-9 + CDC enumeration script, the per-step SETUP state machine, the CDC bulk echo and the MSC BOT/SCSI driver – moved verbatim out of board_usb.c.

Since
0.1.0

Definition in file board_usb_vhost.c.

Enumeration Type Documentation

◆ msc_const_t

enum msc_const_t : uint32_t

BOT / SCSI sizing the host uses (USB Mass Storage BBB 1.0).

Enumerator
k_msc_cbw_len 

Command Block Wrapper length.

k_msc_csw_len 

Command Status Wrapper length.

k_msc_flag_in 

bmCBWFlags: device-to-host.

k_msc_settle 

Ticks to wait for a phase.

k_msc_cmd_count 

Scripted commands (below).

Definition at line 633 of file board_usb_vhost.c.

◆ msc_phase_t

enum msc_phase_t : uint8_t

MSC BOT per-command phase.

Enumerator
k_msc_send 

Push the next command's CBW.

k_msc_data 

Read the data-phase bytes.

k_msc_csw 

Read the 13-byte CSW.

k_msc_done 

All scripted commands finished.

Definition at line 618 of file board_usb_vhost.c.

◆ msc_script_cmd_t

enum msc_script_cmd_t : uint8_t

Index of each command in the scripted SCSI sequence.

Enumerator
k_msc_cmd_inquiry 

INQUIRY: standard data, 36-byte allocation.

k_msc_cmd_read_capacity 

READ CAPACITY (10): last LBA + block size.

k_msc_cmd_read10 

READ (10): LBA 0, one block.

Definition at line 626 of file board_usb_vhost.c.

Function Documentation

◆ internal_host_drain_in()

void internal_host_drain_in ( void )
static

Drain the device's queued control-IN data as the host's read.

Drain the device's queued control-in data as the host's read; this step is contained within the board USB vhost model and uses bounded caller or module-owned storage.

Precondition
Arguments satisfy the ranges documented for host drain in.
The call executes on the emulator's single owning thread.
Postcondition
State changes remain confined to the board USB vhost model and documented output objects.
Ownership of caller-supplied storage is unchanged.
Note
The operation is synchronous and does not transfer heap ownership.
Since
0.1.0

Definition at line 306 of file board_usb_vhost.c.

References priv_usb_detect_class(), priv_usb_log_count(), RA8_INTERNAL, and s_usb.

Referenced by internal_host_step_wait_in().

◆ internal_host_echo_send_out()

void internal_host_echo_send_out ( uc_engine * uc)
static

Deliver one bulk-OUT packet to the CDC data OUT pipe and signal BRDY.

Deliver one bulk-out packet to the cdc data out pipe and signal brdy; this step is contained within the board USB vhost model and uses bounded caller or module-owned storage.

Parameters
[in,out]ucUnicorn engine whose emulated state is read or updated.
Precondition
Arguments satisfy the ranges documented for host echo send out.
The call executes on the emulator's single owning thread.
Postcondition
State changes remain confined to the board USB vhost model and documented output objects.
Ownership of caller-supplied storage is unchanged.
Note
The operation is synchronous and does not transfer heap ownership.
Since
0.1.0

Definition at line 560 of file board_usb_vhost.c.

References internal_usb_word(), k_ra8_int0_bit_brdy, k_ra8_usb_off_brdysts, k_usb_bulk_out_pipe, k_usb_pipe_mps, memcpy(), priv_usb_intsts0_set(), priv_usb_log_count(), priv_usb_raise_irq(), RA8_INTERNAL, s_echo_out, s_echo_out_len, s_echo_out_sent, and s_usb.

Referenced by priv_host_run_configured_phase().

◆ internal_host_latch_setup()

void internal_host_latch_setup ( const usb_setup_step_t * s)
static

Latch a SETUP packet into USBREQ..USBLENG (host -> device).

Latch a setup packet into usbreq..usbleng (host -> device); this step is contained within the board USB vhost model and uses bounded caller or module-owned storage.

Parameters
[in]sModule state instance processed by the operation.
Precondition
Arguments satisfy the ranges documented for host latch setup.
The call executes on the emulator's single owning thread.
Postcondition
State changes remain confined to the board USB vhost model and documented output objects.
Ownership of caller-supplied storage is unchanged.
Note
The operation is synchronous and does not transfer heap ownership.
Since
0.1.0

Definition at line 234 of file board_usb_vhost.c.

References usb_setup_step_t::b_request, usb_setup_step_t::bm_request_type, internal_usb_word(), k_ra8_usb_off_usbindx, k_ra8_usb_off_usbleng, k_ra8_usb_off_usbreq, k_ra8_usb_off_usbval, k_usb_byte_bits, RA8_INTERNAL, s_usb, usb_setup_step_t::w_index, usb_setup_step_t::w_length, and usb_setup_step_t::w_value.

Referenced by priv_host_deliver_setup().

◆ internal_host_msc_build_cdb()

void internal_host_msc_build_cdb ( uint8_t cmd,
uint8_t * cdb,
uint8_t * cdb_len,
uint32_t * data_len )
static

Build the SCSI CDB for the scripted command cmd.

Parameters
[in]cmdScript index (0=INQUIRY, 1=READ CAPACITY(10), 2=READ(10)).
[out]cdb16-byte command block to fill (pre-zeroed).
[out]cdb_lenLength of the CDB.
[out]data_lenExpected data-phase byte count.

Build the scsi cdb for the scripted command cmd; this step is contained within the board USB vhost model and uses bounded caller or module-owned storage.

Precondition
Arguments satisfy the ranges documented for host mass-storage build cdb.
The call executes on the emulator's single owning thread.
Postcondition
State changes remain confined to the board USB vhost model and documented output objects.
Ownership of caller-supplied storage is unchanged.
Note
The operation is synchronous and does not transfer heap ownership.
Since
0.1.0

Definition at line 666 of file board_usb_vhost.c.

References k_cdb10_len, k_inquiry_len, k_msc_cmd_inquiry, k_msc_cmd_read10, k_msc_cmd_read_capacity, k_scsi_inquiry, k_scsi_read10, k_scsi_read_capacity, and k_sector_bytes.

Referenced by internal_host_msc_send_cbw().

◆ internal_host_msc_drive()

void internal_host_msc_drive ( uc_engine * uc)
static

Drive the MSC BOT state machine one tick while CONFIGURED.

Drive the msc bot state machine one tick while configured; this step is contained within the board USB vhost model and uses bounded caller or module-owned storage.

Parameters
[in,out]ucUnicorn engine whose emulated state is read or updated.
Precondition
Arguments satisfy the ranges documented for host mass-storage drive.
The call executes on the emulator's single owning thread.
Postcondition
State changes remain confined to the board USB vhost model and documented output objects.
Ownership of caller-supplied storage is unchanged.
Note
The operation is synchronous and does not transfer heap ownership.
Since
0.1.0

Definition at line 916 of file board_usb_vhost.c.

References internal_host_msc_phase_csw(), internal_host_msc_phase_data(), internal_host_msc_phase_send(), k_msc_csw, k_msc_data, k_msc_done, k_msc_send, RA8_INTERNAL, and s_msc_phase.

Referenced by priv_host_run_configured_phase().

◆ internal_host_msc_parse_capacity()

void internal_host_msc_parse_capacity ( const uint8_t * d,
uint16_t n )
static

Parse a READ CAPACITY (10) response into block count + size.

Parse a read capacity (10) response into block count + size; this step is contained within the board USB vhost model and uses bounded caller or module-owned storage.

Parameters
[in]dD input used by the operation.
[in]nNumber of elements or bytes participating in the operation.
Precondition
Arguments satisfy the ranges documented for host mass-storage parse capacity.
The call executes on the emulator's single owning thread.
Postcondition
State changes remain confined to the board USB vhost model and documented output objects.
Ownership of caller-supplied storage is unchanged.
Note
The operation is synchronous and does not transfer heap ownership.
Since
0.1.0

Definition at line 786 of file board_usb_vhost.c.

References k_be_lane_b0, k_be_lane_b1, k_be_lane_b2, k_be_lane_b3, k_cap10_blocklen_off, k_cap10_last_lba_off, k_usb_shift24, RA8_INTERNAL, s_msc_block_len, and s_msc_blocks.

Referenced by internal_host_msc_record_data().

◆ internal_host_msc_phase_csw()

void internal_host_msc_phase_csw ( uc_engine * uc)
static

Phase k_msc_csw: take the CSW, then advance to the next command.

Phase k_msc_csw: take the csw, then advance to the next command; this step is contained within the board USB vhost model and uses bounded caller or module-owned storage.

Parameters
[in,out]ucUnicorn engine whose emulated state is read or updated.
Precondition
Arguments satisfy the ranges documented for host mass-storage phase csw.
The call executes on the emulator's single owning thread.
Postcondition
State changes remain confined to the board USB vhost model and documented output objects.
Ownership of caller-supplied storage is unchanged.
Note
The operation is synchronous and does not transfer heap ownership.
Since
0.1.0

Definition at line 894 of file board_usb_vhost.c.

References internal_host_msc_take_in(), k_msc_csw_len, k_msc_send, k_usb_in_cap, k_usb_step_timeout, RA8_INTERNAL, s_msc_cmd, s_msc_phase, and s_msc_wait.

Referenced by internal_host_msc_drive().

◆ internal_host_msc_phase_data()

void internal_host_msc_phase_data ( uc_engine * uc)
static

Phase k_msc_data: drain the data phase until it is done or stalls.

Phase k_msc_data: drain the data phase until it is done or stalls; this step is contained within the board USB vhost model and uses bounded caller or module-owned storage.

Parameters
[in,out]ucUnicorn engine whose emulated state is read or updated.
Precondition
Arguments satisfy the ranges documented for host mass-storage phase data.
The call executes on the emulator's single owning thread.
Postcondition
State changes remain confined to the board USB vhost model and documented output objects.
Ownership of caller-supplied storage is unchanged.
Note
The operation is synchronous and does not transfer heap ownership.
Since
0.1.0

Definition at line 868 of file board_usb_vhost.c.

References internal_host_msc_record_data(), internal_host_msc_take_in(), k_msc_csw, k_usb_in_cap, k_usb_step_timeout, RA8_INTERNAL, s_msc_data_got, s_msc_data_len, s_msc_phase, and s_msc_wait.

Referenced by internal_host_msc_drive().

◆ internal_host_msc_phase_send()

void internal_host_msc_phase_send ( uc_engine * uc)
static

Phase k_msc_send: push the next CBW, or finish the script.

Phase k_msc_send: push the next cbw, or finish the script; this step is contained within the board USB vhost model and uses bounded caller or module-owned storage.

Parameters
[in,out]ucUnicorn engine whose emulated state is read or updated.
Precondition
Arguments satisfy the ranges documented for host mass-storage phase send.
The call executes on the emulator's single owning thread.
Postcondition
State changes remain confined to the board USB vhost model and documented output objects.
Ownership of caller-supplied storage is unchanged.
Note
The operation is synchronous and does not transfer heap ownership.
Since
0.1.0

Definition at line 813 of file board_usb_vhost.c.

References internal_host_msc_send_cbw(), k_msc_cmd_count, k_msc_data, k_msc_done, RA8_INTERNAL, s_msc_cmd, s_msc_phase, and s_msc_wait.

Referenced by internal_host_msc_drive().

◆ internal_host_msc_record_data()

void internal_host_msc_record_data ( const uint8_t * d,
uint16_t n )
static

Record one data-phase burst against the command that is in flight.

Each scripted command captures its data phase differently: READ CAPACITY is parsed into the geometry globals, INQUIRY only needs to be seen at all, and READ (10) accumulates its byte count. Anything else is counted but not interpreted.

Parameters
[in]dData-phase bytes just taken off the bulk-IN pipe.
[in]nNumber of valid bytes in d, always non-zero.
Precondition
d is non-null.
n is greater than zero (the caller checked).
Postcondition
The globals for the in-flight command reflect this burst.
d is unmodified.
Note
Not thread-safe; mutates file-scope MSC state.
Since
0.1.0

Definition at line 842 of file board_usb_vhost.c.

References internal_host_msc_parse_capacity(), k_msc_cmd_inquiry, k_msc_cmd_read10, k_msc_cmd_read_capacity, RA8_INTERNAL, s_msc_cmd, s_msc_inquiry_ok, and s_msc_read_ok.

Referenced by internal_host_msc_phase_data().

◆ internal_host_msc_send_cbw()

void internal_host_msc_send_cbw ( uc_engine * uc)
static

Push the current command's CBW onto the bulk-OUT pipe (raise BRDY).

Push the current command's cbw onto the bulk-out pipe (raise brdy); this step is contained within the board USB vhost model and uses bounded caller or module-owned storage.

Parameters
[in,out]ucUnicorn engine whose emulated state is read or updated.
Precondition
Arguments satisfy the ranges documented for host mass-storage send cbw.
The call executes on the emulator's single owning thread.
Postcondition
State changes remain confined to the board USB vhost model and documented output objects.
Ownership of caller-supplied storage is unchanged.
Note
The operation is synchronous and does not transfer heap ownership.
Since
0.1.0

Definition at line 702 of file board_usb_vhost.c.

References internal_host_msc_build_cdb(), internal_usb_word(), k_cbw_cdb_off, k_cbw_cdblen_off, k_cbw_dtl_off, k_cbw_flags_off, k_cbw_lun_off, k_cbw_tag_off, k_le_lane_b0, k_le_lane_b1, k_le_lane_b2, k_le_lane_b3, k_msc_cbw_len, k_msc_flag_in, k_ra8_int0_bit_brdy, k_ra8_usb_off_brdysts, k_usb_bulk_out_pipe, k_usb_byte_mask, k_usb_shift24, priv_usb_intsts0_set(), priv_usb_raise_irq(), RA8_INTERNAL, s_msc_cmd, s_msc_data_got, s_msc_data_len, s_msc_tag, and s_usb.

Referenced by internal_host_msc_phase_send().

◆ internal_host_msc_take_in()

uint16_t internal_host_msc_take_in ( uc_engine * uc,
uint8_t * out,
uint16_t cap )
static

Consume one IN buffer (data or CSW) and acknowledge it with BEMP.

Consume one in buffer (data or csw) and acknowledge it with bemp; this step is contained within the board USB vhost model and uses bounded caller or module-owned storage.

Parameters
[in,out]ucUnicorn engine whose emulated state is read or updated.
[out]outDestination storage receiving the computed result.
[in]capCap input used by the operation.
Returns
The host mass-storage take in result produced by the board USB vhost model.
Return values
valueThe operation-specific host mass-storage take in value.
Precondition
Arguments satisfy the ranges documented for host mass-storage take in.
The call executes on the emulator's single owning thread.
Postcondition
State changes remain confined to the board USB vhost model and documented output objects.
Ownership of caller-supplied storage is unchanged.
Note
The operation is synchronous and does not transfer heap ownership.
Since
0.1.0

Definition at line 757 of file board_usb_vhost.c.

References internal_usb_word(), k_ra8_int0_bit_bemp, k_ra8_usb_off_bempsts, k_usb_bulk_in_pipe, priv_usb_intsts0_set(), priv_usb_raise_irq(), RA8_INTERNAL, and s_usb.

Referenced by internal_host_msc_phase_csw(), and internal_host_msc_phase_data().

◆ internal_host_script_len()

uint32_t internal_host_script_len ( void )
static

Number of steps in the enumeration script.

Number of steps in the enumeration script; this step is contained within the board USB vhost model and uses bounded caller or module-owned storage.

Returns
The host script len result produced by the board USB vhost model.
Return values
valueThe operation-specific host script len value.
Precondition
Arguments satisfy the ranges documented for host script len.
The call executes on the emulator's single owning thread.
Postcondition
State changes remain confined to the board USB vhost model and documented output objects.
Ownership of caller-supplied storage is unchanged.
Note
The operation is synchronous and does not transfer heap ownership.
Since
0.1.0

Definition at line 450 of file board_usb_vhost.c.

References RA8_INTERNAL, and s_k_enum_script.

Referenced by internal_host_step_next(), and priv_host_run_setup_phase().

◆ internal_host_setup_ctsq()

uint16_t internal_host_setup_ctsq ( const usb_setup_step_t * s)
static

CTSQ control-stage value the host advertises for a SETUP.

Ctsq control-stage value the host advertises for a setup; this step is contained within the board USB vhost model and uses bounded caller or module-owned storage.

Parameters
[in]sModule state instance processed by the operation.
Returns
The host setup ctsq result produced by the board USB vhost model.
Return values
valueThe operation-specific host setup ctsq value.
Precondition
Arguments satisfy the ranges documented for host setup ctsq.
The call executes on the emulator's single owning thread.
Postcondition
State changes remain confined to the board USB vhost model and documented output objects.
Ownership of caller-supplied storage is unchanged.
Note
The operation is synchronous and does not transfer heap ownership.
Since
0.1.0

Definition at line 254 of file board_usb_vhost.c.

References usb_setup_step_t::bm_request_type, k_ra8_ctsq_rdds, k_ra8_ctsq_wrds, k_ra8_ctsq_wrnd, k_usb_dir_device_to_host, RA8_INTERNAL, and usb_setup_step_t::w_length.

Referenced by priv_host_deliver_setup().

◆ internal_host_step_deliver()

void internal_host_step_deliver ( uc_engine * uc,
const usb_setup_step_t * s )
static

Sub-state k_sub_deliver: latch + raise CTRT, then pick the next sub.

Sub-state k_sub_deliver: latch + raise ctrt, then pick the next sub; this step is contained within the board USB vhost model and uses bounded caller or module-owned storage.

Parameters
[in,out]ucUnicorn engine whose emulated state is read or updated.
[in]sModule state instance processed by the operation.
Precondition
Arguments satisfy the ranges documented for host step deliver.
The call executes on the emulator's single owning thread.
Postcondition
State changes remain confined to the board USB vhost model and documented output objects.
Ownership of caller-supplied storage is unchanged.
Note
The operation is synchronous and does not transfer heap ownership.
Since
0.1.0

Definition at line 361 of file board_usb_vhost.c.

References usb_setup_step_t::b_request, usb_setup_step_t::bm_request_type, k_sub_next, k_sub_wait_ack, k_sub_wait_in, k_usb_dir_device_to_host, k_usb_req_set_address, priv_host_apply_no_data(), priv_host_deliver_setup(), RA8_INTERNAL, s_host_substate, and s_host_wait.

Referenced by priv_host_run_setup_phase().

◆ internal_host_step_next()

void internal_host_step_next ( uc_engine * uc,
const usb_setup_step_t * s )
static

Sub-state k_sub_next: record any post-step effect, advance the step.

Sub-state k_sub_next: record any post-step effect, advance the step; this step is contained within the board USB vhost model and uses bounded caller or module-owned storage.

Parameters
[in,out]ucUnicorn engine whose emulated state is read or updated.
[in]sModule state instance processed by the operation.
Precondition
Arguments satisfy the ranges documented for host step next.
The call executes on the emulator's single owning thread.
Postcondition
State changes remain confined to the board USB vhost model and documented output objects.
Ownership of caller-supplied storage is unchanged.
Note
The operation is synchronous and does not transfer heap ownership.
Since
0.1.0

Definition at line 476 of file board_usb_vhost.c.

References usb_setup_step_t::b_request, internal_host_script_len(), k_phase_configured, k_sub_deliver, k_usb_req_set_config, priv_host_mark_configured(), RA8_INTERNAL, s_host_phase, s_host_step, s_host_substate, and s_host_wait.

Referenced by priv_host_run_setup_phase().

◆ internal_host_step_status()

void internal_host_step_status ( uc_engine * uc)
static

Sub-state k_sub_status: deliver the control-read status stage.

Sub-state k_sub_status: deliver the control-read status stage; this step is contained within the board USB vhost model and uses bounded caller or module-owned storage.

Parameters
[in,out]ucUnicorn engine whose emulated state is read or updated.
Precondition
Arguments satisfy the ranges documented for host step status.
The call executes on the emulator's single owning thread.
Postcondition
State changes remain confined to the board USB vhost model and documented output objects.
Ownership of caller-supplied storage is unchanged.
Note
The operation is synchronous and does not transfer heap ownership.
Since
0.1.0

Definition at line 408 of file board_usb_vhost.c.

References k_ra8_ctsq_rdss, k_ra8_int0_bit_ctrt, k_sub_wait_ack, priv_usb_intsts0_set(), priv_usb_raise_irq(), RA8_INTERNAL, s_host_substate, s_host_wait, and s_usb.

Referenced by priv_host_run_setup_phase().

◆ internal_host_step_wait_ack()

void internal_host_step_wait_ack ( void )
static

Sub-state k_sub_wait_ack: await the device's CCPL (transfer end).

Sub-state k_sub_wait_ack: await the device's ccpl (transfer end); this step is contained within the board USB vhost model and uses bounded caller or module-owned storage.

Precondition
Arguments satisfy the ranges documented for host step wait ack.
The call executes on the emulator's single owning thread.
Postcondition
State changes remain confined to the board USB vhost model and documented output objects.
Ownership of caller-supplied storage is unchanged.
Note
The operation is synchronous and does not transfer heap ownership.
Since
0.1.0

Definition at line 426 of file board_usb_vhost.c.

References k_sub_next, k_usb_step_timeout, priv_host_take_ccpl(), RA8_INTERNAL, s_host_substate, and s_host_wait.

Referenced by priv_host_run_setup_phase().

◆ internal_host_step_wait_in()

void internal_host_step_wait_in ( void )
static

Sub-state k_sub_wait_in: await the device's control-IN response.

Sub-state k_sub_wait_in: await the device's control-in response; this step is contained within the board USB vhost model and uses bounded caller or module-owned storage.

Precondition
Arguments satisfy the ranges documented for host step wait in.
The call executes on the emulator's single owning thread.
Postcondition
State changes remain confined to the board USB vhost model and documented output objects.
Ownership of caller-supplied storage is unchanged.
Note
The operation is synchronous and does not transfer heap ownership.
Since
0.1.0

Definition at line 383 of file board_usb_vhost.c.

References internal_host_drain_in(), k_sub_status, k_usb_step_timeout, priv_host_dcp_pid_buf(), priv_usb_log_count(), RA8_INTERNAL, s_host_step, s_host_substate, s_host_wait, and s_usb.

Referenced by priv_host_run_setup_phase().

◆ internal_usb_hid_decode_report()

void internal_usb_hid_decode_report ( const uint8_t * d,
uint16_t len )
static

Decode one HID boot-protocol mouse input report from the device.

The report is { buttons, dx, dy } (USB HID 1.11 sec E.10): byte 0 is the button bitmap, bytes 1-2 are signed X/Y deltas. The host accumulates the deltas into a virtual cursor so the report shows real motion (the demo walks the cursor in a square) – exactly what a real host driver would render.

Parameters
[in]dReport bytes.
[in]lenReport length (>= 3 for a boot mouse).
Precondition
Arguments satisfy the ranges documented for USB HID decode report.
The call executes on the emulator's single owning thread.
Postcondition
State changes remain confined to the board USB vhost model and documented output objects.
Ownership of caller-supplied storage is unchanged.
Note
The operation is synchronous and does not transfer heap ownership.
Since
0.1.0

Definition at line 123 of file board_usb_vhost.c.

References priv_emu_io_errf(), RA8_INTERNAL, s_hid_buttons, s_hid_cx, s_hid_cy, s_hid_reports, and s_trace.

Referenced by priv_host_echo_read_in().

◆ priv_host_apply_no_data()

void priv_host_apply_no_data ( uc_engine * uc,
const usb_setup_step_t * s )

Apply the SIE-owned side effects of a no-data control request.

Apply a no-data control write's device-state side effects (vhost).

Definition at line 334 of file board_usb_vhost.c.

References usb_setup_step_t::b_request, internal_usb_word(), k_ra8_dvsq_address, k_ra8_int0_bit_dvst, k_ra8_usb_off_usbaddr, k_ra8_usbaddr_addr_mask, k_usb_log_width, k_usb_req_set_address, priv_usb_intsts0_set(), priv_usb_log_line(), priv_usb_raise_irq(), s_usb, and usb_setup_step_t::w_value.

Referenced by board_usb_bridge_deliver_setup(), and internal_host_step_deliver().

◆ priv_host_dcp_pid_buf()

bool priv_host_dcp_pid_buf ( void )

DCPCTR.PID == BUF: the device has armed an IN response.

True when the device armed its DCP to BUF (ready for OUT) (vhost).

Definition at line 315 of file board_usb_vhost.c.

References internal_usb_word(), k_ra8_pid_buf, k_ra8_pid_mask, k_ra8_usb_off_dcpctr, and s_usb.

Referenced by board_usb_bridge_dcp_in_ready(), and internal_host_step_wait_in().

◆ priv_host_deliver_setup()

void priv_host_deliver_setup ( uc_engine * uc,
const usb_setup_step_t * s )

◆ priv_host_device_attached()

bool priv_host_device_attached ( void )

True when SYSCFG.DPRPU is set: the device has attached its pull-up.

True when SYSCFG.DPRPU is set: the device attached its pull-up (vhost).

Definition at line 219 of file board_usb_vhost.c.

References internal_usb_word(), k_ra8_syscfg_bit_dprpu, k_ra8_usb_off_syscfg, and s_usb.

Referenced by board_usb_dev_attached(), board_usb_loop_attached(), and priv_host_run_idle_phase().

◆ priv_host_echo_read_in()

void priv_host_echo_read_in ( uc_engine * uc)

Drain bytes the device queued on the IN pipe; ack the IN transfer.

Drain any device bulk-IN data the virtual host is owed (vhost).

After consuming the buffer the host raises BEMP for the pipe – the "transmit buffer empty" interrupt the RA dcd uses to complete an IN transfer. Without it the device-side USBX blocks after one packet, so a HID device that streams reports (the boot mouse) freezes after the first; with it the reports keep flowing and the cursor walks its square.

Parameters
[in,out]ucUnicorn engine (to pend the USB interrupt).

Definition at line 589 of file board_usb_vhost.c.

References internal_usb_hid_decode_report(), internal_usb_word(), k_ra8_int0_bit_bemp, k_ra8_usb_off_bempsts, k_usb_bulk_in_pipe, k_usb_class_hid, priv_usb_intsts0_set(), priv_usb_log_count(), priv_usb_raise_irq(), s_dev_class, s_echo_in_got, and s_usb.

Referenced by board_usb_tick(), and priv_host_run_configured_phase().

◆ priv_host_mark_configured()

void priv_host_mark_configured ( uc_engine * uc)

Mark the device CONFIGURED: advance DVSQ and raise DVST.

Advance the device state to CONFIGURED (vhost).

Definition at line 456 of file board_usb_vhost.c.

References k_ra8_dvsq_configured, k_ra8_int0_bit_dvst, priv_usb_intsts0_set(), priv_usb_log_line(), priv_usb_raise_irq(), s_configured, and s_usb.

Referenced by board_usb_bridge_mark_configured(), board_usb_loop_take_ccpl(), and internal_host_step_next().

◆ priv_host_run_configured_phase()

void priv_host_run_configured_phase ( uc_engine * uc)

Phase k_phase_configured: optionally drive the CDC bulk echo.

Virtual-host phase driver: post-CONFIGURED class traffic (vhost).

Definition at line 935 of file board_usb_vhost.c.

References internal_host_echo_send_out(), internal_host_msc_drive(), k_phase_done, k_usb_class_hid, k_usb_class_msc, k_usb_post_cfg_idle, k_usb_reset_settle, priv_host_echo_read_in(), s_dev_class, s_echo_out_len, s_echo_out_sent, s_host_phase, and s_host_wait.

Referenced by board_usb_tick().

◆ priv_host_run_idle_phase()

void priv_host_run_idle_phase ( uc_engine * uc)

Phase k_phase_idle: wait for the device pull-up, then bus-reset.

Virtual-host phase driver: waiting for the device pull-up (vhost).

Definition at line 519 of file board_usb_vhost.c.

References k_phase_reset, k_ra8_dvsq_default, k_ra8_int0_bit_dvst, priv_host_device_attached(), priv_usb_intsts0_set(), priv_usb_log_line(), priv_usb_raise_irq(), s_host_phase, s_host_wait, and s_usb.

Referenced by board_usb_tick().

◆ priv_host_run_reset_phase()

void priv_host_run_reset_phase ( uc_engine * uc)

Phase k_phase_reset: hold reset a few ticks so the DCP re-arms.

Virtual-host phase driver: holding bus reset (vhost).

Definition at line 533 of file board_usb_vhost.c.

References k_phase_setup, k_ra8_dvsq_default, k_ra8_int0_bit_dvst, k_sub_deliver, k_usb_reset_settle, priv_usb_intsts0_set(), priv_usb_raise_irq(), s_host_phase, s_host_step, s_host_substate, s_host_wait, and s_usb.

Referenced by board_usb_tick().

◆ priv_host_run_setup_phase()

void priv_host_run_setup_phase ( uc_engine * uc)

◆ priv_host_take_ccpl()

bool priv_host_take_ccpl ( void )

Observe (and clear) DCPCTR.CCPL: the device ended a control transfer.

Consume the device's CCPL (control transfer complete) (vhost).

Definition at line 322 of file board_usb_vhost.c.

References internal_usb_word(), k_ra8_dcpctr_bit_ccpl, k_ra8_usb_off_dcpctr, and s_usb.

Referenced by board_usb_bridge_dev_took_ccpl(), board_usb_loop_setup(), board_usb_loop_take_ccpl(), and internal_host_step_wait_ack().

◆ priv_usb_class_active_str()

const char * priv_usb_class_active_str ( void )

Human-readable active-class suffix once the device is configured.

Human label of the detected class + live traffic totals (vhost).

Definition at line 38 of file board_usb_vhost.c.

References k_usb_class_cdc, k_usb_class_hid, k_usb_class_msc, k_usb_class_printer, k_usb_class_unknown, k_usb_class_vendor, and s_dev_class.

Referenced by board_usb_report().

◆ priv_usb_detect_class()

void priv_usb_detect_class ( const uint8_t * d,
uint16_t len )

Detect the device class from a returned descriptor, if it is config.

Detect the device class from the enumerated config descriptor (vhost).

Walks the standard descriptor chain (each entry: bLength, bDescriptorType, ...). The configuration descriptor carries the interface descriptor(s); the first interface's bInterfaceClass (offset 5) names the device class. Called for every control-IN response – only the configuration descriptor contains an interface descriptor, so the first hit wins.

Parameters
[in]dDescriptor bytes the device returned.
[in]lenNumber of valid bytes in d.

Definition at line 69 of file board_usb_vhost.c.

References k_iface_class_off, k_usb_class_cdc, k_usb_class_hid, k_usb_class_msc, k_usb_class_printer, k_usb_class_unknown, k_usb_class_vendor, k_usb_dt_interface, k_usb_iclass_cdc_comm, k_usb_iclass_cdc_data, k_usb_iclass_hid, k_usb_iclass_msc, k_usb_iclass_printer, k_usb_iclass_vendor, and s_dev_class.

Referenced by board_usb_bridge_dcp_in_take(), board_usb_loop_ctrl_in_read(), and internal_host_drain_in().

Variable Documentation

◆ s_dev_class

uint8_t s_dev_class

◆ s_hid_buttons

uint8_t s_hid_buttons

Last HID button bitmap.

Last HID button bitmap (vhost).

Definition at line 35 of file board_usb_vhost.c.

Referenced by board_usb_report(), and internal_usb_hid_decode_report().

◆ s_hid_cx

int32_t s_hid_cx

Accumulated HID boot-mouse X.

Accumulated HID mouse X (vhost).

Definition at line 33 of file board_usb_vhost.c.

Referenced by board_usb_report(), and internal_usb_hid_decode_report().

◆ s_hid_cy

int32_t s_hid_cy

Accumulated HID boot-mouse Y.

Accumulated HID mouse Y (vhost).

Definition at line 34 of file board_usb_vhost.c.

Referenced by board_usb_report(), and internal_usb_hid_decode_report().

◆ s_hid_reports

uint32_t s_hid_reports

HID input reports the host has read.

HID reports read (vhost).

Definition at line 32 of file board_usb_vhost.c.

Referenced by board_usb_report(), and internal_usb_hid_decode_report().

◆ s_k_enum_script

const usb_setup_step_t s_k_enum_script[]
static

The chapter-9 + CDC SETUP sequence the virtual host drives.

Mirrors the request order a real host (macOS / Linux) issues for a CDC-ACM device: probe the device descriptor, assign an address, re-read the full device + configuration + string descriptors, select the configuration (which fires USBX's CDC-ACM activate), then the two CDC line requests.

Definition at line 155 of file board_usb_vhost.c.

Referenced by internal_host_script_len(), and priv_host_run_setup_phase().

◆ s_msc_block_len

uint32_t s_msc_block_len

Block size in bytes.

MSC block size (vhost).

Definition at line 648 of file board_usb_vhost.c.

Referenced by board_usb_report(), and internal_host_msc_parse_capacity().

◆ s_msc_blocks

uint32_t s_msc_blocks

Capacity in blocks (READ CAPACITY).

MSC capacity in blocks (vhost).

Definition at line 647 of file board_usb_vhost.c.

Referenced by board_usb_report(), and internal_host_msc_parse_capacity().

◆ s_msc_cmd

uint8_t s_msc_cmd
static

Index into the SCSI command script.

Definition at line 642 of file board_usb_vhost.c.

Referenced by internal_host_msc_phase_csw(), internal_host_msc_phase_send(), internal_host_msc_record_data(), and internal_host_msc_send_cbw().

◆ s_msc_data_got

uint32_t s_msc_data_got
static

Data-phase bytes read so far.

Definition at line 645 of file board_usb_vhost.c.

Referenced by internal_host_msc_phase_data(), and internal_host_msc_send_cbw().

◆ s_msc_data_len

uint32_t s_msc_data_len
static

Expected data-phase length.

Definition at line 644 of file board_usb_vhost.c.

Referenced by internal_host_msc_phase_data(), and internal_host_msc_send_cbw().

◆ s_msc_inquiry_ok

bool s_msc_inquiry_ok

INQUIRY data phase completed.

MSC INQUIRY completed (vhost).

Definition at line 650 of file board_usb_vhost.c.

Referenced by board_usb_report(), and internal_host_msc_record_data().

◆ s_msc_phase

uint8_t s_msc_phase
static

◆ s_msc_read_ok

uint32_t s_msc_read_ok

Sector-read data-phase bytes captured.

MSC read data bytes (vhost).

Definition at line 649 of file board_usb_vhost.c.

Referenced by board_usb_report(), and internal_host_msc_record_data().

◆ s_msc_tag

uint32_t s_msc_tag
static

Running dCBWTag.

Definition at line 643 of file board_usb_vhost.c.

Referenced by internal_host_msc_send_cbw().

◆ s_msc_wait

uint32_t s_msc_wait
static