ra8-firmware 0.1.0
Bare-metal firmware for the Renesas RA8 family (RA8D2 / RA8P1)
Loading...
Searching...
No Matches
board_usb_internal.h
Go to the documentation of this file.
1
18
19#pragma once
20
21#include <stdint.h>
22#include <unicorn/unicorn.h>
23
24#include "board_usb.h"
25#include "ra8_attributes.h"
26
27#ifdef __cplusplus
28extern "C" {
29#endif
30
31/* =============================================================================
32 * Window geometry + model sizing.
33 * =============================================================================
34 */
35
37typedef enum : uint64_t {
38 k_usb_base = 0x40250000UL,
39 k_usb_span = 0x100UL,
40 k_usb_reg_words = 0x100UL / 2U,
45 k_usb_in_cap = 512UL,
47
48/* =============================================================================
49 * Standard USB chapter-9 request constants (USB 2.0 sec 9.3 / 9.4).
50 * =============================================================================
51 */
52
54typedef enum : uint8_t {
59
61typedef enum : uint8_t {
65
67typedef enum : uint8_t {
71} usb_dt_t;
72
74typedef enum : uint8_t {
77} usb_dir_t;
78
80typedef enum : uint16_t {
81 k_usb_rhst_fs = 0x0002U,
83
109
110/* =============================================================================
111 * Controller register state + FIFO staging.
112 * =============================================================================
113 */
114
123typedef struct {
125 uint16_t len;
126 uint16_t rd;
127 bool ready;
129
137typedef struct {
139 uint16_t len;
140 bool valid;
142
156
165
167typedef enum : uint8_t {
174
194
205typedef enum : uint8_t {
211
220typedef enum : uint8_t {
224
226typedef enum : uint8_t {
232
242
253
255typedef struct {
257 uint8_t b_request;
258 uint16_t w_value;
259 uint16_t w_index;
260 uint16_t w_length;
261 const char* name;
263
275RA8_INTERNAL static inline uint32_t internal_usb_word(uint64_t off)
276{
277 return (uint32_t)((off & ~(uint64_t)1U) / 2U);
278}
279
280/* Shared model state -- one logical module across the board_usb_* TUs; the
281 * defining TU is noted per declaration. */
282extern usb_state_t s_usb;
283extern bool s_trace;
285extern bool s_external_host;
286extern bool s_roles_swapped;
287extern uint16_t g_dev_irq_event;
288extern uint8_t s_dcp_hold[k_usb_in_cap];
289extern uint16_t s_dcp_hold_len;
290extern bool s_dcp_hold_pending;
291extern uint8_t s_host_phase;
292extern uint8_t s_host_step;
293extern uint8_t s_host_substate;
294extern uint32_t s_host_wait;
295extern bool s_configured;
296extern uint32_t s_usb_irqs;
297extern uint8_t s_echo_out[k_usb_echo_cap];
298extern uint32_t s_echo_out_len;
299extern uint32_t s_echo_out_sent;
300extern uint32_t s_echo_in_got;
301extern uint8_t s_dev_class;
302extern uint32_t s_hid_reports;
303extern int32_t s_hid_cx;
304extern int32_t s_hid_cy;
305extern uint8_t s_hid_buttons;
306extern uint32_t s_msc_blocks;
307extern uint32_t s_msc_block_len;
308extern uint32_t s_msc_read_ok;
309extern bool s_msc_inquiry_ok;
310extern bool s_loop_latched;
311extern uint32_t s_loop_setups;
312extern uint32_t s_loop_bulk_out_pkts;
313extern uint32_t s_loop_bulk_in_pkts;
314
324RA8_PRIV void priv_usb_log_line(const char* msg);
325
336RA8_PRIV void priv_usb_log_count(const char* label, unsigned n);
337
348RA8_PRIV uint16_t priv_usb_intsts0(void);
349
359RA8_PRIV void priv_usb_intsts0_set(uint8_t bit);
360
370RA8_PRIV void priv_usb_raise_irq(uc_engine* uc);
371
384RA8_PRIV uint32_t priv_usb_reg_read(uint64_t off, unsigned size);
385
397RA8_PRIV void priv_usb_reg_write(uint64_t off, uint32_t value, unsigned size);
398
409RA8_PRIV void priv_usb_detect_class(const uint8_t* d, uint16_t len);
410
412RA8_PRIV const char* priv_usb_class_active_str(void);
413
423RA8_PRIV void priv_host_run_idle_phase(uc_engine* uc);
424
434RA8_PRIV void priv_host_run_reset_phase(uc_engine* uc);
435
445RA8_PRIV void priv_host_run_setup_phase(uc_engine* uc);
446
456RA8_PRIV void priv_host_run_configured_phase(uc_engine* uc);
457
467RA8_PRIV void priv_host_echo_read_in(uc_engine* uc);
468
478RA8_PRIV void priv_bridge_pump_device(uc_engine* uc);
479
491
502RA8_PRIV void priv_host_deliver_setup(uc_engine* uc, const usb_setup_step_t* s);
503
514RA8_PRIV void priv_host_apply_no_data(uc_engine* uc, const usb_setup_step_t* s);
515
527
539
549RA8_PRIV void priv_host_mark_configured(uc_engine* uc);
550
551#ifdef __cplusplus
552}
553#endif
USBFS controller model + a virtual USB host for the board emulator.
void(* board_usb_irq_raiser_t)(uc_engine *uc, uint16_t event)
Signature of the ICU event-raise hook board_periph installs.
Definition board_usb.h:68
void priv_usb_reg_write(uint64_t off, uint32_t value, unsigned size)
USBFS window register write dispatch (dev).
uint32_t s_msc_blocks
MSC capacity in blocks (vhost).
usb_dir_t
bmRequestType direction bit (USB 2.0 sec 9.3 Table 9-2).
@ k_usb_dir_host_to_device
OUT: host -> device (control write).
@ k_usb_dir_device_to_host
IN: device -> host (control read).
void priv_usb_raise_irq(uc_engine *uc)
Pend the device's ICU interrupt via the registered raiser (dev).
uint32_t s_loop_bulk_in_pkts
Bulk-IN packets (loop).
usb_le_lane_t
Byte-lane offsets within a little-endian 32-bit protocol field.
@ k_le_lane_b2
Bits 23:16.
@ k_le_lane_b3
Most-significant byte (bits 31:24).
@ k_le_lane_b1
Bits 15:8.
@ k_le_lane_b0
Least-significant byte (bits 7:0).
uint32_t s_loop_setups
SETUPs from the fw host (loop).
uint8_t s_dcp_hold[k_usb_in_cap]
Held control-OUT bytes (core).
Definition board_usb.c:63
usb_host_sub_t
Per-SETUP-step sub-states.
@ k_sub_status
Deliver the status stage + raise CTRT.
@ k_sub_wait_in
Wait for the device's IN data (control read).
@ k_sub_deliver
Latch the SETUP + raise CTRT.
@ k_sub_next
Advance to the next script step.
@ k_sub_wait_ack
Wait for the device's CCPL (transfer end).
bool s_external_host
Bridge host owns the bus (core).
Definition board_usb.c:41
uint16_t priv_usb_intsts0(void)
INTSTS0 value the device reads: event bits OR computed fields (dev).
usb_host_phase_t
Host state-machine phases.
@ k_phase_reset
Holding bus reset; device re-arms its DCP.
@ k_phase_done
Terminal idle.
@ k_phase_idle
Waiting for the device pull-up (DPRPU).
@ k_phase_configured
Device configured; optional bulk echo.
@ k_phase_setup
Walking the enumeration script.
cdc_breq_t
CDC class bRequest values issued after configuration (CDC 1.20).
@ k_cdc_req_set_control_line_state
SET_CONTROL_LINE_STATE.
@ k_cdc_req_set_line_coding
SET_LINE_CODING (OUT data).
usb_state_t s_usb
Controller + FIFO state (core).
Definition board_usb.c:33
uint32_t s_loop_bulk_out_pkts
Bulk-OUT packets (loop).
uint8_t s_host_substate
Per-step sub-state (core).
Definition board_usb.c:70
void priv_host_mark_configured(uc_engine *uc)
Advance the device state to CONFIGURED (vhost).
int32_t s_hid_cy
Accumulated HID mouse Y (vhost).
uint32_t s_echo_in_got
Echoed bytes read back (core).
Definition board_usb.c:79
usb_iclass_t
bInterfaceClass codes + the INTERFACE descriptor type (USB 2.0).
@ k_usb_dt_interface
INTERFACE descriptor type.
@ k_usb_iclass_vendor
Vendor specific (issue #265).
@ k_usb_iclass_cdc_comm
Communications (CDC control).
@ k_usb_iclass_hid
Human Interface Device.
@ k_usb_iclass_cdc_data
CDC data.
@ k_usb_iclass_msc
Mass Storage.
@ k_usb_iclass_printer
Printer (issue #265).
uint8_t s_host_step
Enumeration-script index (core).
Definition board_usb.c:69
uint32_t s_host_wait
Sub-state wait ticks (core).
Definition board_usb.c:71
uint32_t s_msc_block_len
MSC block size (vhost).
usb_rhst_t
DVSTCTR0.RHST reset-handshake field values (HUM Ch 36.2.5 "DVSTCTR0", p 1971).
@ k_usb_rhst_fs
Link settled at full speed after the bus reset.
uint8_t s_dev_class
Detected device class (vhost).
uint8_t s_echo_out[k_usb_echo_cap]
Host bulk-OUT payload (core).
Definition board_usb.c:76
uint8_t s_host_phase
Virtual-host phase (core).
Definition board_usb.c:68
usb_breq_t
bRequest values the virtual host issues (USB 2.0 sec 9.4).
@ k_usb_req_set_config
SET_CONFIGURATION.
@ k_usb_req_get_descriptor
GET_DESCRIPTOR.
@ k_usb_req_set_address
SET_ADDRESS.
void priv_usb_intsts0_set(uint8_t bit)
Set one INTSTS0 event bit in the register shadow (dev).
void priv_host_run_configured_phase(uc_engine *uc)
Virtual-host phase driver: post-CONFIGURED class traffic (vhost).
void priv_bridge_pump_device(uc_engine *uc)
Pump the device's level-triggered DCP-OUT receive (bridge).
void priv_host_run_setup_phase(uc_engine *uc)
Virtual-host phase driver: walking the enumeration script (vhost).
bool priv_host_take_ccpl(void)
Consume the device's CCPL (control transfer complete) (vhost).
usb_geom_t
USBFS register-window geometry and the staging-buffer caps.
@ k_usb_echo_cap
Bulk echo staging capacity.
@ k_usb_base
USBFS base (HUM Ch 36, p 1965).
@ k_usb_in_cap
DCP/bulk IN staging cap (multi-MPS).
@ k_usb_span
Modelled window (PIPECTR end < 0x90).
@ k_usb_pipe_count
DCP (0) + PIPE1..PIPE9.
@ k_usb_reg_words
16-bit register shadow word count.
@ k_usb_dcp_mps
EP0 default control-pipe max packet.
@ k_usb_pipe_mps
Bulk-FS data pipe max packet.
void priv_host_apply_no_data(uc_engine *uc, const usb_setup_step_t *s)
Apply a no-data control write's device-state side effects (vhost).
usb_dt_t
bDescriptorType values used in GET_DESCRIPTOR wValue high byte.
@ k_usb_dt_string
STRING descriptor.
@ k_usb_dt_device
DEVICE descriptor.
@ k_usb_dt_config
CONFIGURATION descriptor.
usb_be_lane_t
Byte-lane offsets within a big-endian 32-bit SCSI field.
@ k_be_lane_b3
Most-significant byte (bits 31:24).
@ k_be_lane_b1
Bits 15:8.
@ k_be_lane_b2
Bits 23:16.
@ k_be_lane_b0
Least-significant byte (bits 7:0).
bool priv_host_device_attached(void)
True when SYSCFG.DPRPU is set: the device attached its pull-up (vhost).
uint32_t s_msc_read_ok
MSC read data bytes (vhost).
void priv_usb_log_line(const char *msg)
Append one already-formatted line to the enumeration-step log (core).
Definition board_usb.c:91
const char * priv_usb_class_active_str(void)
Human label of the detected class + live traffic totals (vhost).
int32_t s_hid_cx
Accumulated HID mouse X (vhost).
usb_dev_class_t
USB device class the host detected from the interface descriptor.
@ k_usb_class_printer
Printer (7/1/x), issue #265.
@ k_usb_class_cdc
CDC-ACM (virtual serial).
@ k_usb_class_hid
HID (boot mouse / keyboard).
@ k_usb_class_msc
Mass storage (BOT/SCSI).
@ k_usb_class_unknown
Not yet detected.
@ k_usb_class_vendor
Vendor specific (0xFF).
void priv_host_echo_read_in(uc_engine *uc)
Drain any device bulk-IN data the virtual host is owed (vhost).
usb_lit_t
USB class-request, SCSI command, and CBW-layout constants.
@ k_cdc_line_coding_len
SET_LINE_CODING wLength.
@ k_cbw_cdblen_off
CBW bCBWCBLength offset.
@ k_usb_shift24
Byte-3 position in a 32-bit word.
@ k_sector_bytes
Logical block size.
@ k_usb_req_class_iface
bmRequestType: class, interface, host->device.
@ k_cbw_tag_off
CBW dCBWTag offset (LE u32).
@ k_scsi_inquiry
SCSI INQUIRY opcode.
@ k_iface_class_off
bInterfaceClass offset in the iface descriptor.
@ k_scsi_read_capacity
SCSI READ CAPACITY(10) opcode.
@ k_cbw_flags_off
CBW bmCBWFlags offset.
@ k_cbw_cdb_off
CBW CBWCB (command block) offset.
@ k_cbw_dtl_off
CBW dCBWDataTransferLength offset (LE u32).
@ k_inquiry_len
INQUIRY allocation length.
@ k_cdb10_len
10-byte CDB length.
@ k_cbw_lun_off
CBW bCBWLUN offset.
@ k_scsi_read10
SCSI READ(10) opcode.
uint32_t s_usb_irqs
USB interrupts raised (core).
Definition board_usb.c:73
bool s_dcp_hold_pending
Held bytes await the arm (core).
Definition board_usb.c:65
bool s_msc_inquiry_ok
MSC INQUIRY completed (vhost).
scsi_cap10_off_t
Field offsets in a SCSI READ CAPACITY(10) parameter block.
@ k_cap10_last_lba_off
Returned logical block address (BE u32).
@ k_cap10_blocklen_off
Block length in bytes (BE u32).
usb_const_t
Misc small constants used across the model (avoid bare literals).
@ k_usb_desc_dev_len
Full device-descriptor length.
@ k_usb_cfifo_hh
CFIFOHH alias offset from CFIFO (+0x3).
@ k_usb_str_len
String-descriptor request length.
@ k_usb_step_timeout
Host per-step wait budget (ticks).
@ k_usb_reset_settle
Ticks held in bus reset before SETUP.
@ k_usb_cfg_full_cap
Config-descriptor full request cap.
@ k_usb_cfifo_h
CFIFOH alias offset from CFIFO (+0x2).
@ k_usb_bulk_in_pipe
CDC bulk IN pipe (EP1 IN -> pipe 1).
@ k_usb_bulk_out_pipe
CDC bulk OUT pipe (EP2 OUT -> pipe 2).
@ k_usb_desc8_len
First GET_DESCRIPTOR(device) length.
@ k_usb_byte_mask
One-byte mask.
@ k_usb_log_width
Bytes per enumeration-step log line.
@ k_usb_post_cfg_idle
Settle ticks after CONFIGURED.
@ k_usb_cfg_probe_len
Config-descriptor header probe len.
@ k_usb_addr_assigned
Address the host assigns the device.
@ k_usb_trace_dump_max
Control-IN payload bytes shown in –trace.
@ k_usb_dcp_pipe_bit
DCP (pipe 0) BRDYSTS/BRDYENB bit mask.
@ k_usb_config_value
bConfigurationValue the host sets.
@ k_usb_byte_bits
Bits per byte.
@ k_usb_log_cap
Enumeration-step log capacity.
static uint32_t internal_usb_word(uint64_t off)
Word index into the 16-bit register shadow for a window offset.
void priv_host_run_idle_phase(uc_engine *uc)
Virtual-host phase driver: waiting for the device pull-up (vhost).
uint16_t s_dcp_hold_len
Held byte count (core).
Definition board_usb.c:64
void priv_usb_log_count(const char *label, unsigned n)
Log a labelled count once (label + n) into the step log (core).
Definition board_usb.c:109
uint32_t s_hid_reports
HID reports read (vhost).
void priv_usb_detect_class(const uint8_t *d, uint16_t len)
Detect the device class from the enumerated config descriptor (vhost).
uint32_t s_echo_out_len
Bytes queued by –usb-in (core).
Definition board_usb.c:77
bool priv_host_dcp_pid_buf(void)
True when the device armed its DCP to BUF (ready for OUT) (vhost).
bool s_loop_latched
Firmware host owns the bus (loop).
bool s_roles_swapped
Self-loop role polarity (core).
Definition board_usb.c:49
uint32_t priv_usb_reg_read(uint64_t off, unsigned size)
USBFS window register read dispatch (dev).
uint16_t g_dev_irq_event
Device ICU event number (core).
Definition board_usb.c:54
uint8_t s_hid_buttons
Last HID button bitmap (vhost).
board_usb_irq_raiser_t s_raise
ICU pend callback (core).
Definition board_usb.c:35
void priv_host_run_reset_phase(uc_engine *uc)
Virtual-host phase driver: holding bus reset (vhost).
uint32_t s_echo_out_sent
Bytes delivered to device (core).
Definition board_usb.c:78
void priv_host_deliver_setup(uc_engine *uc, const usb_setup_step_t *s)
Latch a SETUP packet into USBREQ..USBLENG + raise CTRT (vhost).
static volatile bool s_configured
Latched true once the host drives the device to CONFIGURED.
Definition main.c:128
Annotation-attribute framework macros for ra8-firmware.
#define RA8_PRIV
Module-private helper: shared across TUs but only inside one library.
#define RA8_INTERNAL
Marker that a function is intended to be static (file-local).
One pipe's device-visible IN staging buffer (device -> host payload).
uint16_t len
Filled length (may span several MPS).
bool valid
BVAL pulsed: ready for the host.
uint8_t data[k_usb_in_cap]
Bytes the device queued for the host.
One pipe's host-visible OUT staging buffer (host -> device payload).
uint16_t len
Total staged length.
uint8_t data[k_usb_pipe_mps]
Staged bytes the device will read.
uint16_t rd
Read cursor as the device drains.
bool ready
Data present (BRDY-equivalent).
One SETUP step in the host enumeration script (USB 2.0 sec 9.4).
uint16_t w_length
wLength (host's expected data size).
uint16_t w_value
wValue.
uint16_t w_index
wIndex.
uint8_t b_request
bRequest.
uint8_t bm_request_type
Direction + type + recipient.
const char * name
Human label for the step log.
Aggregate USBFS controller + virtual-host model state.
usb_in_buf_t pipe_in[k_usb_pipe_count]
Bulk IN per pipe.
usb_out_buf_t pipe_out[k_usb_pipe_count]
Bulk OUT per pipe.
uint16_t reg[k_usb_reg_words]
16-bit reflect-on-read shadow.
uint16_t dvsq
INTSTS0.DVSQ device-state value.
uint16_t ctsq
INTSTS0.CTSQ control-stage value.
usb_out_buf_t dcp_out
DCP OUT buffer (control-write data).
bool setup_valid
INTSTS0.VALID: a SETUP is latched.
usb_in_buf_t dcp_in
DCP IN buffer (descriptor response).
static volatile uint32_t s_trace[k_dcd_trace_entries]
JLink-readable ring of packed transfer/SETUP events.