|
ra8-firmware 0.1.0
Bare-metal firmware for the Renesas RA8 family (RA8D2 / RA8P1)
|
Native (bare-metal) USB printer + vendor composite device for EK-RA8D2. More...
#include <stddef.h>#include <stdint.h>#include <string.h>#include "ra8_board_ek_ra8d2.h"#include "ra8_boot_entry.h"#include "ra8_cgc.h"#include "ra8_err.h"#include "ra8_isr.h"#include "ra8_port_constants.h"#include "ra8_port_utils.h"#include "ra8_time.h"#include "ra8_usb.h"#include "ra8_usb_pprn.h"#include "ra8_usb_pvnd.h"#include "ra8_usb_regs.h"#include "usb_printer_vendor_ch9.h"Go to the source code of this file.
Enumerations | |
| enum | demo_cfg_t : uint32_t { k_demo_console_baud = 115200U , k_demo_poll_budget = 200000U } |
| Compile-time settings for the polled device loop. More... | |
| enum | demo_bulk_t : uint16_t { k_demo_bulk_cap = 64U } |
| Bulk staging sizes for the printer / vendor data path. More... | |
| enum | demo_intsts0_t : uint16_t { k_demo_int_brdy = (uint16_t)(1U << (uint16_t)k_ra8_int0_bit_brdy) , k_demo_int_ctrt = (uint16_t)(1U << (uint16_t)k_ra8_int0_bit_ctrt) , k_demo_int_dvst = (uint16_t)(1U << (uint16_t)k_ra8_int0_bit_dvst) } |
| The INTSTS0 event bits the polled loop reacts to (HUM Ch 36.2.9). More... | |
Functions | |
| static void | demo_puts (const char *s) |
| Write a NUL-terminated string to the UART console. | |
| static ra8_err_t | demo_printer_setup (void *ctx, const ra8_usb_setup_t *setup) |
| Printer class-setup handler: answer GET_PORT_STATUS / GET_DEVICE_ID. | |
| static ra8_err_t | demo_vendor_setup (void *ctx, const ra8_usb_setup_t *setup) |
| Vendor class-setup handler: ACK the vendor control request. | |
| static ra8_err_t | demo_stage_descriptor (pv_action_t action, const ra8_usb_setup_t *setup) |
| Stage a standard GET_DESCRIPTOR response on EP0. | |
| static void | demo_dispatch_setup (const ra8_usb_setup_t *setup) |
| Dispatch one decoded SETUP packet from the DCP. | |
| static void | demo_handle_dvst (void) |
| React to a device-state transition (INTSTS0.DVST). | |
| static void | demo_drain_print_job (void) |
| Drain a bulk-OUT print job and echo it to the UART console. | |
| static void | demo_drain_vendor (void) |
| Drain a bulk-OUT vendor packet and loop it back on the vendor IN pipe. | |
| static void | demo_poll_once (void) |
| Run one poll-loop iteration: service EP0 + bulk events. | |
| static void | demo_panic_halt (void) |
| Halt forever in WFI after a fatal bring-up error. | |
| static ra8_err_t | demo_pins_init (void) |
| Route the four USB-FS pins to the USBFS controller. | |
| static ra8_err_t | demo_usb_bringup (void) |
| Bring up both native class layers and advertise the device. | |
| void | main (void) |
| Application entry: bring up the clocks + USB + poll chapter-9. | |
Variables | |
| static const uint8_t | s_device_id [] |
| IEEE 1284 device-ID payload returned by Printer GET_DEVICE_ID. | |
| static volatile bool | s_configured = false |
| Latched true once the host drives the device to CONFIGURED. | |
| volatile uint32_t | g_usb_print_jobs = 0U |
| HIL liveness counter – print-job packets drained to the UART. | |
| volatile uint32_t | g_usb_vendor_loops = 0U |
| HIL liveness counter – vendor bulk packets looped back. | |
| static const ra8_port_pin_t | k_demo_pin_vbus = (ra8_port_pin_t)k_ra8_board_usbfs_pin_vbus |
| USB_FS VBUS pin, packed ra8_port_pin_t. | |
| static const ra8_port_pin_t | k_demo_pin_vbusen = (ra8_port_pin_t)k_ra8_board_usbfs_pin_vbusen |
| USB_FS VBUSEN pin, packed ra8_port_pin_t. | |
| static const ra8_port_pin_t | k_demo_pin_dp = (ra8_port_pin_t)k_ra8_board_usbfs_pin_dp |
| USB_FS D+ pin, packed ra8_port_pin_t. | |
| static const ra8_port_pin_t | k_demo_pin_dm = (ra8_port_pin_t)k_ra8_board_usbfs_pin_dm |
| USB_FS D- pin, packed ra8_port_pin_t. | |
Native (bare-metal) USB printer + vendor composite device for EK-RA8D2.
Enumerates the EK-RA8D2 USB-FS port as a composite device that exposes two interfaces in a single configuration so one enumeration exercises both native class layers (issue #265):
Unlike the ThreadX + USBX device examples, this app is fully bare-metal: the native ra8_usb driver has no chapter-9 responder, so main.c runs a small polled chapter-9 loop (see usb_printer_vendor_ch9.c for the pure SETUP router + descriptor tables) that answers the standard GET_DESCRIPTOR / SET_ADDRESS / SET_CONFIGURATION requests on EP0 and hands the printer / vendor SETUPs to the class layers.
| Net | Pin | Source |
|---|---|---|
| USB_FS_VBUS | P4_07 | k_ra8_board_usbfs_pin_vbus |
| USB_FS_VBUSEN | P5_00 | k_ra8_board_usbfs_pin_vbusen |
| USB_FS_DP | P8_14 | k_ra8_board_usbfs_pin_dp |
| USB_FS_DM | P8_15 | k_ra8_board_usbfs_pin_dm |
Definition in file main.c.
| enum demo_bulk_t : uint16_t |
| enum demo_cfg_t : uint32_t |
| enum demo_intsts0_t : uint16_t |
|
static |
Dispatch one decoded SETUP packet from the DCP.
Routes via pv_route_setup: descriptor actions stage bytes, SET_ADDRESS latches the address, SET_CONFIGURATION ACKs and the class actions hand off to ra8_usb_pprn_handle_setup / _pvnd_handle_setup. Unsupported requests STALL EP0.
| [in] | setup | Decoded SETUP packet (non-NULL). |
setup is non-NULL. Definition at line 308 of file main.c.
References demo_stage_descriptor(), k_pv_action_get_config_desc, k_pv_action_get_device_desc, k_pv_action_get_string_desc, k_pv_action_printer_class, k_pv_action_set_address, k_pv_action_set_configuration, k_pv_action_stall, k_pv_action_vendor_class, k_ra8_err_not_supported, k_ra8_ok, k_ra8_usb_speed_fs, pv_route_setup(), ra8_usb_control_response(), ra8_usb_pprn_handle_setup(), ra8_usb_pvnd_handle_setup(), ra8_usb_set_address(), and ra8_usb_setup_t::w_value.
Referenced by demo_poll_once().
|
static |
Drain a bulk-OUT print job and echo it to the UART console.
TODO(host-side print job): a physical host print subsystem (CUPS / usblp) must send the job; ra8_emulator reaches CONFIGURED but does not drive bulk traffic, so this path runs only on hardware.
Definition at line 386 of file main.c.
References g_usb_print_jobs, k_demo_bulk_cap, k_ra8_ok, ra8_board_uart_console_write(), and ra8_usb_pprn_recv().
Referenced by demo_poll_once().
|
static |
Drain a bulk-OUT vendor packet and loop it back on the vendor IN pipe.
TODO(host-side vendor loopback): a host libusb / WinUSB tool must drive the bulk transfer; this path runs only on hardware.
Definition at line 409 of file main.c.
References g_usb_vendor_loops, k_demo_bulk_cap, k_ra8_ok, ra8_usb_pvnd_recv(), and ra8_usb_pvnd_send().
Referenced by demo_poll_once().
|
static |
React to a device-state transition (INTSTS0.DVST).
On the host's bus reset (Default state) the DCP is re-armed; on CONFIGURED the banner is emitted once. USB 2.0 sec 9.1 "Device States".
Definition at line 356 of file main.c.
References demo_puts(), k_ra8_ok, k_ra8_usb_dev_state_configured, k_ra8_usb_dev_state_default, k_ra8_usb_dev_state_powered, k_ra8_usb_speed_fs, ra8_usb_device_busreset_rearm(), ra8_usb_get_device_state(), and s_configured.
Referenced by demo_poll_once().
|
static |
Halt forever in WFI after a fatal bring-up error.
Definition at line 470 of file main.c.
References demo_puts().
|
staticnodiscard |
Route the four USB-FS pins to the USBFS controller.
| k_ra8_ok | All four pins routed. |
Definition at line 491 of file main.c.
References k_demo_pin_dm, k_demo_pin_dp, k_demo_pin_vbus, k_demo_pin_vbusen, k_ra8_level_low, k_ra8_ok, k_ra8_psel_usb_fs, ra8_gpio_output_init(), and ra8_pfs_route_peripheral().
Referenced by main().
|
static |
Run one poll-loop iteration: service EP0 + bulk events.
Definition at line 429 of file main.c.
References demo_dispatch_setup(), demo_drain_print_job(), demo_drain_vendor(), demo_handle_dvst(), k_demo_int_brdy, k_demo_int_ctrt, k_demo_int_dvst, k_ra8_ok, k_ra8_usb_speed_fs, ra8_usb_clear_status(), ra8_usb_control_response(), ra8_usb_get_status(), and ra8_usb_read_setup_if_valid().
Referenced by main().
|
static |
Printer class-setup handler: answer GET_PORT_STATUS / GET_DEVICE_ID.
Invoked by ra8_usb_pprn_handle_setup after it validates the envelope. GET_PORT_STATUS stages the 1-byte port-status shadow; GET_DEVICE_ID stages the IEEE 1284 identity string; SOFT_RESET carries no data.
| [in] | ctx | Unused context. |
| [in] | setup | Decoded printer class SETUP (non-NULL, class-validated). |
| k_ra8_ok | Data staged (or none required). |
setup is non-NULL (class-layer guarantee). Definition at line 209 of file main.c.
References ra8_usb_setup_t::b_request, k_pv_pprn_get_device_id, k_pv_pprn_get_port_status, k_ra8_err_null_ptr, k_ra8_ok, k_ra8_usb_speed_fs, pv_clamp_len(), ra8_usb_dcp_in_data(), ra8_usb_pprn_get_port_status(), s_device_id, and ra8_usb_setup_t::w_length.
Referenced by demo_usb_bringup().
|
static |
Write a NUL-terminated string to the UART console.
| [in] | s | String to emit (non-NULL, NUL-terminated). |
s is non-NULL. Definition at line 177 of file main.c.
References ra8_board_uart_console_write(), and strlen().
Referenced by demo_handle_dvst(), demo_panic_halt(), and main().
|
static |
Stage a standard GET_DESCRIPTOR response on EP0.
| [in] | action | ::k_pv_action_get_* descriptor action. |
| [in] | setup | The originating SETUP (for its wLength ceiling). |
| k_ra8_ok | Descriptor staged (PID = BUF). |
| k_ra8_err_not_supported | Action names no descriptor. |
setup is non-NULL. Definition at line 280 of file main.c.
References k_ra8_err_not_supported, k_ra8_usb_speed_fs, pv_clamp_len(), pv_descriptor(), ra8_usb_dcp_in_data(), and ra8_usb_setup_t::w_length.
Referenced by demo_dispatch_setup().
|
staticnodiscard |
Bring up both native class layers and advertise the device.
Initialises ra8_usb_pprn (printer pipes PIPE3/PIPE4) then ra8_usb_pvnd (vendor pipes PIPE5/PIPE1), caches each layer's descriptor blob + the printer device-ID, installs the class-setup callbacks, and raises the D+ pull-up so the host begins enumeration. Both *_init calls run the shared ra8_usb_device_init; the second is idempotent for this polled device because the loop reads INTSTS0 directly and does not rely on the per-pipe BRDY enables the re-init clears.
| k_ra8_ok | Both class layers up, pull-up raised. |
Definition at line 528 of file main.c.
References demo_printer_setup(), demo_vendor_setup(), k_pv_action_get_config_desc, k_ra8_err_not_supported, k_ra8_ok, k_ra8_usb_speed_fs, pv_descriptor(), ra8_usb_device_attach(), ra8_usb_pprn_attach_setup_handler(), ra8_usb_pprn_init(), ra8_usb_pprn_set_descriptors(), ra8_usb_pvnd_attach_setup_handler(), ra8_usb_pvnd_init(), ra8_usb_pvnd_set_descriptors(), and s_device_id.
Referenced by main().
|
static |
Vendor class-setup handler: ACK the vendor control request.
The demo defines no vendor control protocol; every vendor-recipient request is ACKed so a host libusb tool can probe the interface. Real firmware would decode setup->b_request here.
| [in] | ctx | Unused context. |
| [in] | setup | Decoded vendor SETUP (non-NULL, envelope-validated). |
| k_ra8_ok | Always. |
setup is non-NULL (class-layer guarantee). Definition at line 250 of file main.c.
References k_ra8_err_null_ptr, and k_ra8_ok.
Referenced by demo_usb_bringup().
| void main | ( | void | ) |
Application entry: bring up the clocks + USB + poll chapter-9.
The application entry point Reset_Handler hands control to.
Definition at line 577 of file main.c.
References demo_panic_halt(), demo_pins_init(), demo_poll_once(), demo_puts(), demo_usb_bringup(), k_demo_console_baud, k_demo_poll_budget, k_ra8_clock_id_cpuclk0, k_ra8_ok, ra8_board_uart_console_init(), ra8_cgc_get_clock_hz(), ra8_cgc_init(), ra8_cgc_usbfs_clock_enable(), ra8_isr_globals_enable(), and ra8_time_init().
| volatile uint32_t g_usb_print_jobs = 0U |
HIL liveness counter – print-job packets drained to the UART.
Advances on every bulk-OUT packet the host sends to the printer interface. Read externally over SWD by the HIL mem-probe.
Definition at line 138 of file main.c.
Referenced by demo_drain_print_job().
| volatile uint32_t g_usb_vendor_loops = 0U |
HIL liveness counter – vendor bulk packets looped back.
Definition at line 146 of file main.c.
Referenced by demo_drain_vendor().
|
static |
USB_FS D- pin, packed ra8_port_pin_t.
Definition at line 159 of file main.c.
Referenced by demo_pins_init().
|
static |
USB_FS D+ pin, packed ra8_port_pin_t.
Definition at line 157 of file main.c.
Referenced by demo_pins_init().
|
static |
USB_FS VBUS pin, packed ra8_port_pin_t.
Definition at line 153 of file main.c.
Referenced by demo_pins_init().
|
static |
USB_FS VBUSEN pin, packed ra8_port_pin_t.
Definition at line 155 of file main.c.
Referenced by demo_pins_init().
|
static |
Latched true once the host drives the device to CONFIGURED.
Definition at line 128 of file main.c.
Referenced by board_usb_configured(), board_usb_init(), board_usb_report(), board_usb_state_string(), board_usb_tick(), demo_handle_dvst(), and priv_host_mark_configured().
|
static |
IEEE 1284 device-ID payload returned by Printer GET_DEVICE_ID.
Big-endian 2-byte length prefix then the ID string, per USB Printer Class 1.1 sec 4.2.1. Length prefix = 0x0031 = 49 bytes (the 2 length bytes plus the 47-char body).
Definition at line 115 of file main.c.
Referenced by demo_printer_setup(), and demo_usb_bringup().