PER Firmware
Loading...
Searching...
No Matches
usb.c File Reference

Validation and state for the public STM32G4 USB API. More...

Include dependency graph for usb.c:

Functions

static bool usb_endpoint_is_valid (uint8_t endpoint)
 
static bool usb_direction_is_valid (PHAL_USB_EndpointDirection_t direction)
 
bool PHAL_USB_init (void)
 Initialize the USB device hardware and endpoints.
 
bool PHAL_USB_deinit (void)
 Detach and disable the USB peripheral.
 
bool PHAL_USB_connect (void)
 Attach the initialized USB device to the host.
 
bool PHAL_USB_setAddress (uint8_t address)
 Apply the address assigned by a SET_ADDRESS request.
 
bool PHAL_USB_stall (uint8_t endpoint, PHAL_USB_EndpointDirection_t direction)
 Stall one endpoint direction.
 
bool PHAL_USB_write (uint8_t endpoint, const void *source, uint16_t length_bytes)
 Queue one device-to-host packet.
 
bool PHAL_USB_read (uint8_t endpoint, void *destination, uint16_t destination_capacity, uint16_t *received_length)
 Consume one host-to-device packet.
 
uint16_t PHAL_USB_getFrameNumber (void)
 Read the USB peripheral's current 11-bit frame number.
 
void PHAL_USB_callback (const PHAL_USB_Event_t *event)
 Handle USB events.
 
void USB_LP_IRQHandler (void)
 
void USB_HP_IRQHandler (void)
 

Variables

static bool g_initialized = false
 

Detailed Description

Validation and state for the public STM32G4 USB API.

Author
Ronak Jain (jain7.nosp@m.17@p.nosp@m.urdue.nosp@m..edu)

Function Documentation

◆ PHAL_USB_callback()

void PHAL_USB_callback ( const PHAL_USB_Event_t * event)

Handle USB events.

Applications define this callback to process bus resets, SETUP packets, OUT packets, and IN completions. Check event->transfer before accessing transfer_data. The HAL provides a weak empty implementation. OUT data remains available through PHAL_USB_read(). An IN completion marks the endpoint ready for PHAL_USB_write().

Parameters
eventUSB event for the current interrupt.
Here is the caller graph for this function:

◆ PHAL_USB_connect()

bool PHAL_USB_connect ( void )

Attach the initialized USB device to the host.

Returns
true when the D+ pull-up is enabled; false when USB is inactive.
Here is the call graph for this function:

◆ PHAL_USB_deinit()

bool PHAL_USB_deinit ( void )

Detach and disable the USB peripheral.

Returns
true when deinitialization completes; false when USB is inactive.
Here is the call graph for this function:

◆ PHAL_USB_getFrameNumber()

uint16_t PHAL_USB_getFrameNumber ( void )

Read the USB peripheral's current 11-bit frame number.

The value is maintained by the peripheral from Start-of-Frame packets and wraps after 2047.

Returns
The current frame number.
Here is the call graph for this function:

◆ PHAL_USB_init()

bool PHAL_USB_init ( void )

Initialize the USB device hardware and endpoints.

Initialization enables HSI48, USB, UCPD1, PA8/PA9 USB-C sink signaling, PA11/PA12 USB data signaling, USB packet memory, endpoint zero as control, endpoint one as bulk, and USB interrupts. The device remains detached until PHAL_USB_connect() enables the D+ pull-up.

Returns
true when initialization completes; false when USB is active or the 48 MHz clock startup times out.
Here is the call graph for this function:

◆ PHAL_USB_read()

bool PHAL_USB_read ( uint8_t endpoint,
void * destination,
uint16_t destination_capacity,
uint16_t * received_length )

Consume one host-to-device packet.

A successful read copies the packet and rearms reception. An undersized destination leaves the packet pending and reports its length through received_length. A zero-length packet accepts a null destination.

Parameters
endpointPHAL_USB_CONTROL_ENDPOINT or PHAL_USB_DATA_ENDPOINT.
destinationPacket destination, or null for zero capacity.
destination_capacityDestination capacity in bytes.
received_lengthReceives the packet length.
Returns
true when the packet is consumed; false when a precondition fails, reception is pending, or the destination capacity is insufficient.
Here is the call graph for this function:

◆ PHAL_USB_setAddress()

bool PHAL_USB_setAddress ( uint8_t address)

Apply the address assigned by a SET_ADDRESS request.

Apply the address after the control endpoint reports completion of the IN status packet.

Parameters
addressDevice address from 0 through 127.
Returns
true when the address is applied; false for an inactive peripheral or an address above 127.
Here is the call graph for this function:

◆ PHAL_USB_stall()

bool PHAL_USB_stall ( uint8_t endpoint,
PHAL_USB_EndpointDirection_t direction )

Stall one endpoint direction.

Parameters
endpointPHAL_USB_CONTROL_ENDPOINT or PHAL_USB_DATA_ENDPOINT.
directionEndpoint direction.
Returns
true when the endpoint is stalled; false when a precondition fails.
Here is the call graph for this function:

◆ PHAL_USB_write()

bool PHAL_USB_write ( uint8_t endpoint,
const void * source,
uint16_t length_bytes )

Queue one device-to-host packet.

The HAL copies exactly length_bytes into USB packet memory, where length_bytes may be zero through PHAL_USB_PACKET_SIZE_BYTES. A zero-length packet accepts a null source. The endpoint is ready for its next packet when the IN completion callback runs.

Parameters
endpointPHAL_USB_CONTROL_ENDPOINT or PHAL_USB_DATA_ENDPOINT.
sourcePacket bytes, or null for a zero-length packet.
length_bytesPacket length from 0 through 64.
Returns
true when the packet is queued; false when a precondition fails or the endpoint is busy.
Here is the call graph for this function:

◆ usb_direction_is_valid()

static bool usb_direction_is_valid ( PHAL_USB_EndpointDirection_t direction)
static
Here is the caller graph for this function:

◆ usb_endpoint_is_valid()

static bool usb_endpoint_is_valid ( uint8_t endpoint)
static
Here is the caller graph for this function:

◆ USB_HP_IRQHandler()

void USB_HP_IRQHandler ( void )
Here is the call graph for this function:

◆ USB_LP_IRQHandler()

void USB_LP_IRQHandler ( void )
Here is the call graph for this function:

Variable Documentation

◆ g_initialized

bool g_initialized = false
static