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

STM32G4 USB full-speed device API. More...

#include <stdint.h>
Include dependency graph for usb.h:
This graph shows which files directly or indirectly include this file:

Go to the source code of this file.

Classes

struct  PHAL_USB_TransferEvent_t
 
struct  PHAL_USB_Event_t
 

Enumerations

enum  PHAL_USB_Request_t : uint8_t {
  PHAL_USB_REQUEST_GET_STATUS = 0U , PHAL_USB_REQUEST_CLEAR_FEATURE = 1U , PHAL_USB_REQUEST_SET_FEATURE = 3U , PHAL_USB_REQUEST_SET_ADDRESS = 5U ,
  PHAL_USB_REQUEST_GET_DESCRIPTOR = 6U , PHAL_USB_REQUEST_GET_CONFIGURATION = 8U , PHAL_USB_REQUEST_SET_CONFIGURATION = 9U , PHAL_USB_REQUEST_GET_INTERFACE = 10U ,
  PHAL_USB_REQUEST_SET_INTERFACE = 11U
}
 
enum  PHAL_USB_DescriptorType_t : uint8_t {
  PHAL_USB_DESCRIPTOR_DEVICE = 1U , PHAL_USB_DESCRIPTOR_CONFIGURATION = 2U , PHAL_USB_DESCRIPTOR_STRING = 3U , PHAL_USB_DESCRIPTOR_INTERFACE = 4U ,
  PHAL_USB_DESCRIPTOR_ENDPOINT = 5U
}
 
enum  PHAL_USB_RequestType_t : uint8_t {
  PHAL_USB_REQUEST_TYPE_DEVICE_OUT = 0x00U , PHAL_USB_REQUEST_TYPE_INTERFACE_OUT = 0x01U , PHAL_USB_REQUEST_TYPE_ENDPOINT_OUT = 0x02U , PHAL_USB_REQUEST_TYPE_DEVICE_IN = 0x80U ,
  PHAL_USB_REQUEST_TYPE_INTERFACE_IN = 0x81U , PHAL_USB_REQUEST_TYPE_ENDPOINT_IN = 0x82U
}
 
enum  PHAL_USB_EndpointDirection_t : uint8_t { PHAL_USB_ENDPOINT_DIRECTION_OUT = 0U , PHAL_USB_ENDPOINT_DIRECTION_IN = 1U }
 

Functions

struct __attribute__ ((packed))
 
void PHAL_USB_callback (const PHAL_USB_Event_t *event)
 Handle USB events.
 
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_clearHalt (uint8_t endpoint, PHAL_USB_EndpointDirection_t direction)
 Clear endpoint halt, discard any pending transfer, and reset to DATA0.
 
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.
 

Variables

static constexpr uint8_t PHAL_USB_CONTROL_ENDPOINT = 0U
 
static constexpr uint8_t PHAL_USB_DATA_ENDPOINT = 1U
 
static constexpr uint16_t PHAL_USB_PACKET_SIZE_BYTES = 64U
 
static constexpr uint8_t PHAL_USB_REQUEST_TYPE_MASK = 0x60U
 
static constexpr uint8_t PHAL_USB_REQUEST_TYPE_STANDARD = 0U
 
static constexpr uint16_t PHAL_USB_FEATURE_ENDPOINT_HALT = 0U
 
static constexpr uint8_t PHAL_USB_ENDPOINT_ADDRESS_IN = 0x80U
 
static constexpr uint8_t PHAL_USB_DEVICE_DESCRIPTOR_LENGTH = 18U
 
static constexpr uint8_t PHAL_USB_CONFIGURATION_DESCRIPTOR_LENGTH = 9U
 
static constexpr uint8_t PHAL_USB_INTERFACE_DESCRIPTOR_LENGTH = 9U
 
static constexpr uint8_t PHAL_USB_ENDPOINT_DESCRIPTOR_LENGTH = 7U
 
static constexpr uint8_t PHAL_USB_DEVICE_CLASS_VENDOR_SPECIFIC = 0xFFU
 
static constexpr uint8_t PHAL_USB_CONFIGURATION_ATTRIBUTES_BUS_POWERED = 0x80U
 
static constexpr uint8_t PHAL_USB_ENDPOINT_TRANSFER_TYPE_BULK = 2U
 
 PHAL_USB_SetupPacket_t
 

Detailed Description

STM32G4 USB full-speed device API.

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

The HAL provides one control endpoint and one bulk data endpoint. Applications supply USB descriptors, control-request handling, and data queues through the callback and packet-transfer functions below.

Enumeration Type Documentation

◆ PHAL_USB_DescriptorType_t

enum PHAL_USB_DescriptorType_t : uint8_t

Descriptor types used in bDescriptorType and the high byte of wValue.

Enumerator
PHAL_USB_DESCRIPTOR_DEVICE 
PHAL_USB_DESCRIPTOR_CONFIGURATION 
PHAL_USB_DESCRIPTOR_STRING 
PHAL_USB_DESCRIPTOR_INTERFACE 
PHAL_USB_DESCRIPTOR_ENDPOINT 

◆ PHAL_USB_EndpointDirection_t

USB-standard transfer direction, named from the host perspective.

Enumerator
PHAL_USB_ENDPOINT_DIRECTION_OUT 

Host to device.

PHAL_USB_ENDPOINT_DIRECTION_IN 

Device to host.

◆ PHAL_USB_Request_t

enum PHAL_USB_Request_t : uint8_t

Standard control requests (bRequest)

Enumerator
PHAL_USB_REQUEST_GET_STATUS 
PHAL_USB_REQUEST_CLEAR_FEATURE 
PHAL_USB_REQUEST_SET_FEATURE 
PHAL_USB_REQUEST_SET_ADDRESS 
PHAL_USB_REQUEST_GET_DESCRIPTOR 
PHAL_USB_REQUEST_GET_CONFIGURATION 
PHAL_USB_REQUEST_SET_CONFIGURATION 
PHAL_USB_REQUEST_GET_INTERFACE 
PHAL_USB_REQUEST_SET_INTERFACE 

◆ PHAL_USB_RequestType_t

enum PHAL_USB_RequestType_t : uint8_t

Standard bmRequestType combinations, including direction and recipient.

Enumerator
PHAL_USB_REQUEST_TYPE_DEVICE_OUT 
PHAL_USB_REQUEST_TYPE_INTERFACE_OUT 
PHAL_USB_REQUEST_TYPE_ENDPOINT_OUT 
PHAL_USB_REQUEST_TYPE_DEVICE_IN 
PHAL_USB_REQUEST_TYPE_INTERFACE_IN 
PHAL_USB_REQUEST_TYPE_ENDPOINT_IN 

Function Documentation

◆ __attribute__()

struct __attribute__ ( (packed) )

Eight-byte USB SETUP wire header; multibyte fields are little endian.

< bmRequestType: direction, category, and recipient.

< bRequest; standard values are PHAL_USB_Request_t.

< wValue: request-specific value or descriptor type/index.

< wIndex: interface, endpoint, or language.

< wLength: maximum data-stage byte count.

◆ 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(). If SETUP and a prior IN completion are both pending, the HAL delivers the IN completion first so SETUP cannot erase pending state.

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

◆ PHAL_USB_clearHalt()

bool PHAL_USB_clearHalt ( uint8_t endpoint,
PHAL_USB_EndpointDirection_t direction )

Clear endpoint halt, discard any pending transfer, and reset to DATA0.

Use for CLEAR_FEATURE(ENDPOINT_HALT) and bulk endpoint configuration changes. IN returns to NAK; OUT is rearmed. Call with USB interrupts excluded.

Returns
false for an inactive peripheral or invalid endpoint/direction.
Here is the call graph for this function:
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:
Here is the caller 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:
Here is the caller 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:
Here is the caller 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:
Here is the caller 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:
Here is the caller 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. Success does not establish host presence or delivery: without host polling the packet stays pending and later writes fail.

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:
Here is the caller graph for this function:

Variable Documentation

◆ PHAL_USB_CONFIGURATION_ATTRIBUTES_BUS_POWERED

uint8_t PHAL_USB_CONFIGURATION_ATTRIBUTES_BUS_POWERED = 0x80U
staticconstexpr

◆ PHAL_USB_CONFIGURATION_DESCRIPTOR_LENGTH

uint8_t PHAL_USB_CONFIGURATION_DESCRIPTOR_LENGTH = 9U
staticconstexpr

◆ PHAL_USB_CONTROL_ENDPOINT

uint8_t PHAL_USB_CONTROL_ENDPOINT = 0U
staticconstexpr

Endpoint zero for enumeration and control transfers.

◆ PHAL_USB_DATA_ENDPOINT

uint8_t PHAL_USB_DATA_ENDPOINT = 1U
staticconstexpr

Endpoint one for aggregate bulk application data.

◆ PHAL_USB_DEVICE_CLASS_VENDOR_SPECIFIC

uint8_t PHAL_USB_DEVICE_CLASS_VENDOR_SPECIFIC = 0xFFU
staticconstexpr

◆ PHAL_USB_DEVICE_DESCRIPTOR_LENGTH

uint8_t PHAL_USB_DEVICE_DESCRIPTOR_LENGTH = 18U
staticconstexpr

◆ PHAL_USB_ENDPOINT_ADDRESS_IN

uint8_t PHAL_USB_ENDPOINT_ADDRESS_IN = 0x80U
staticconstexpr

◆ PHAL_USB_ENDPOINT_DESCRIPTOR_LENGTH

uint8_t PHAL_USB_ENDPOINT_DESCRIPTOR_LENGTH = 7U
staticconstexpr

◆ PHAL_USB_ENDPOINT_TRANSFER_TYPE_BULK

uint8_t PHAL_USB_ENDPOINT_TRANSFER_TYPE_BULK = 2U
staticconstexpr

◆ PHAL_USB_FEATURE_ENDPOINT_HALT

uint16_t PHAL_USB_FEATURE_ENDPOINT_HALT = 0U
staticconstexpr

◆ PHAL_USB_INTERFACE_DESCRIPTOR_LENGTH

uint8_t PHAL_USB_INTERFACE_DESCRIPTOR_LENGTH = 9U
staticconstexpr

◆ PHAL_USB_PACKET_SIZE_BYTES

uint16_t PHAL_USB_PACKET_SIZE_BYTES = 64U
staticconstexpr

Maximum packet size for both endpoints.

◆ PHAL_USB_REQUEST_TYPE_MASK

uint8_t PHAL_USB_REQUEST_TYPE_MASK = 0x60U
staticconstexpr

◆ PHAL_USB_REQUEST_TYPE_STANDARD

uint8_t PHAL_USB_REQUEST_TYPE_STANDARD = 0U
staticconstexpr

◆ PHAL_USB_SetupPacket_t

PHAL_USB_SetupPacket_t