STM32G4 USB full-speed device API.
More...
#include <stdint.h>
Go to the source code of this file.
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.
◆ 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_callback()
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
-
| event | USB event for the current interrupt. |
◆ 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.
◆ PHAL_USB_deinit()
| bool PHAL_USB_deinit |
( |
void | | ) |
|
Detach and disable the USB peripheral.
- Returns
- true when deinitialization completes; false when USB is inactive.
◆ 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.
◆ 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.
◆ 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
-
| endpoint | PHAL_USB_CONTROL_ENDPOINT or PHAL_USB_DATA_ENDPOINT. |
| destination | Packet destination, or null for zero capacity. |
| destination_capacity | Destination capacity in bytes. |
| received_length | Receives the packet length. |
- Returns
- true when the packet is consumed; false when a precondition fails, reception is pending, or the destination capacity is insufficient.
◆ 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
-
| address | Device address from 0 through 127. |
- Returns
- true when the address is applied; false for an inactive peripheral or an address above 127.
◆ PHAL_USB_stall()
Stall one endpoint direction.
- Parameters
-
| endpoint | PHAL_USB_CONTROL_ENDPOINT or PHAL_USB_DATA_ENDPOINT. |
| direction | Endpoint direction. |
- Returns
- true when the endpoint is stalled; false when a precondition fails.
◆ 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
-
| endpoint | PHAL_USB_CONTROL_ENDPOINT or PHAL_USB_DATA_ENDPOINT. |
| source | Packet bytes, or null for a zero-length packet. |
| length_bytes | Packet length from 0 through 64. |
- Returns
- true when the packet is queued; false when a precondition fails or the endpoint is busy.
◆ 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_PACKET_SIZE_BYTES
| uint16_t PHAL_USB_PACKET_SIZE_BYTES = 64U |
|
staticconstexpr |
Maximum packet size for both endpoints.