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

STM32G4 USB register and packet-memory implementation. More...

#include "common/phal_G4/usb/usb_priv.h"
#include "stm32g474xx.h"
Include dependency graph for usb_priv.c:

Functions

static volatile uint16_t * usb_endpoint_register (uint8_t endpoint)
 
static volatile uint16_t * usb_descriptor_word (uint8_t endpoint, uint8_t word)
 
static volatile uint16_t * usb_pma (uint16_t address)
 
static uint16_t usb_transmit_address (uint8_t endpoint)
 
static uint16_t usb_receive_address (uint8_t endpoint)
 
static void usb_delay (uint32_t iterations)
 
static bool usb_enable_clock (void)
 
static void usb_configure_pins (void)
 
static void usb_configure_type_c_sink (void)
 
static void usb_reset_peripheral (void)
 
static void usb_set_status (uint8_t endpoint, PHAL_USB_EndpointDirection_t direction, uint8_t status)
 
static void usb_clear_transfer (uint8_t endpoint, PHAL_USB_EndpointDirection_t direction)
 
static void usb_configure_endpoint (uint8_t endpoint, uint16_t type_bits)
 
static void usb_configure_endpoints (void)
 
static void usb_copy_to_pma (uint16_t address, const void *source, uint16_t length_bytes)
 
static void usb_copy_from_pma (uint16_t address, void *destination, uint16_t length_bytes)
 
bool USB_PRIV_init (void)
 Enable and reset the USB hardware, configure pins and packet memory.
 
void USB_PRIV_deinit (void)
 Detach, disable interrupts, and power down the USB peripheral.
 
void USB_PRIV_enableInterrupts (void)
 Enable USB event interrupts and endpoint operation.
 
void USB_PRIV_connect (void)
 Attach to the bus by enabling the internal D+ pull-up.
 
void USB_PRIV_setAddress (uint8_t address)
 Write the seven-bit device address while keeping the device enabled.
 
void USB_PRIV_stall (uint8_t endpoint, PHAL_USB_EndpointDirection_t direction)
 Set one endpoint direction to the USB STALL handshake state.
 
bool USB_PRIV_write (uint8_t endpoint, const void *source, uint16_t length_bytes)
 Copy one IN packet to packet memory and make it available to the host.
 
bool USB_PRIV_read (uint8_t endpoint, void *destination, uint16_t destination_capacity, uint16_t *received_length)
 Copy one completed OUT packet from packet memory and rearm reception.
 
static void usb_clear_interrupt_flags (uint16_t interrupt_status)
 
void USB_PRIV_handleInterrupt (void)
 Decode one pending USB interrupt and notify the application.
 
uint16_t USB_PRIV_getFrameNumber (void)
 Read the USB peripheral's current 11-bit frame number.
 

Variables

static constexpr uint16_t USB_PRIV_BTABLE_OFFSET = 0U
 
static constexpr uint16_t USB_PRIV_BUFFER_START = 64U
 
static constexpr uint32_t USB_PRIV_CLOCK_TIMEOUT = 1'000'000U
 
static constexpr uint32_t USB_PRIV_STARTUP_DELAY = 1'000U
 
static constexpr uint8_t USB_PRIV_STATUS_STALL = 1U
 
static constexpr uint8_t USB_PRIV_STATUS_NAK = 2U
 
static constexpr uint8_t USB_PRIV_STATUS_VALID = 3U
 
static constexpr uint8_t USB_PRIV_DM_PIN = 11U
 
static constexpr uint8_t USB_PRIV_DP_PIN = 12U
 
static constexpr uint8_t USB_PRIV_CC1_PIN = 8U
 
static constexpr uint8_t USB_PRIV_CC2_PIN = 9U
 
static constexpr uint8_t USB_PRIV_GPIO_MODE_BITS = 0b11U
 
static constexpr uint8_t USB_PRIV_GPIO_MODE_BIT_WIDTH = 2U
 
static constexpr uint8_t USB_PRIV_ENDPOINT_REGISTER_STRIDE = 2U
 
static constexpr uint8_t USB_PRIV_BTABLE_ENTRY_SIZE_BYTES = 8U
 
static constexpr uint8_t USB_PRIV_BTABLE_WORD_SIZE_BYTES = 2U
 
static constexpr uint8_t USB_PRIV_BUFFERS_PER_ENDPOINT = 2U
 
static constexpr uint8_t USB_PRIV_ENDPOINT_COUNT = 8U
 
static constexpr uint8_t USB_PRIV_STATUS_TX_SHIFT = 4U
 
static constexpr uint8_t USB_PRIV_STATUS_RX_SHIFT = 12U
 
static constexpr uint8_t USB_PRIV_RX_BLOCK_COUNT = 1U
 
static constexpr uint8_t USB_PRIV_RX_BLOCK_COUNT_SHIFT = 10U
 

Detailed Description

STM32G4 USB register and packet-memory implementation.

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

Function Documentation

◆ usb_clear_interrupt_flags()

static void usb_clear_interrupt_flags ( uint16_t interrupt_status)
static

Acknowledge non-endpoint flags present in an ISTR snapshot.

Here is the caller graph for this function:

◆ usb_clear_transfer()

static void usb_clear_transfer ( uint8_t endpoint,
PHAL_USB_EndpointDirection_t direction )
static

Acknowledge one endpoint transfer without clearing the opposite direction.

Here is the call graph for this function:
Here is the caller graph for this function:

◆ usb_configure_endpoint()

static void usb_configure_endpoint ( uint8_t endpoint,
uint16_t type_bits )
static

Populate one BTABLE entry and enable its IN and OUT directions.

Here is the call graph for this function:
Here is the caller graph for this function:

◆ usb_configure_endpoints()

static void usb_configure_endpoints ( void )
static

Restore the fixed control and bulk endpoint configuration.

Here is the call graph for this function:
Here is the caller graph for this function:

◆ usb_configure_pins()

static void usb_configure_pins ( void )
static

Release the USB-C CC and USB data pins from their GPIO digital paths.

Here is the caller graph for this function:

◆ usb_configure_type_c_sink()

static void usb_configure_type_c_sink ( void )
static

Configure UCPD1 to advertise this board as a USB-C power sink.

Here is the caller graph for this function:

◆ usb_copy_from_pma()

static void usb_copy_from_pma ( uint16_t address,
void * destination,
uint16_t length_bytes )
static

Copy a packet from STM32 packet memory into application memory.

Here is the call graph for this function:
Here is the caller graph for this function:

◆ usb_copy_to_pma()

static void usb_copy_to_pma ( uint16_t address,
const void * source,
uint16_t length_bytes )
static

Copy an application packet into the STM32's 16-bit packet memory.

Here is the call graph for this function:
Here is the caller graph for this function:

◆ usb_delay()

static void usb_delay ( uint32_t iterations)
static

Provide the minimum peripheral power-up delay without requiring a timer.

Here is the caller graph for this function:

◆ usb_descriptor_word()

static volatile uint16_t * usb_descriptor_word ( uint8_t endpoint,
uint8_t word )
static

Return one 16-bit field from an endpoint's four-word BTABLE entry.

Here is the caller graph for this function:

◆ usb_enable_clock()

static bool usb_enable_clock ( void )
static

Enable HSI48 and wait a bounded period for clock-ready acknowledgement.

Here is the caller graph for this function:

◆ usb_endpoint_register()

static volatile uint16_t * usb_endpoint_register ( uint8_t endpoint)
static

Return the register that controls both directions of one endpoint.

Here is the caller graph for this function:

◆ usb_pma()

static volatile uint16_t * usb_pma ( uint16_t address)
static

Convert a USB byte address to its memory-mapped packet-memory address.

Here is the caller graph for this function:

◆ USB_PRIV_connect()

void USB_PRIV_connect ( void )

Attach to the bus by enabling the internal D+ pull-up.

Here is the caller graph for this function:

◆ USB_PRIV_deinit()

void USB_PRIV_deinit ( void )

Detach, disable interrupts, and power down the USB peripheral.

Here is the caller graph for this function:

◆ USB_PRIV_enableInterrupts()

void USB_PRIV_enableInterrupts ( void )

Enable USB event interrupts and endpoint operation.

Here is the caller graph for this function:

◆ USB_PRIV_getFrameNumber()

uint16_t USB_PRIV_getFrameNumber ( void )

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

Returns
The frame number maintained from Start-of-Frame packets.
Here is the caller graph for this function:

◆ USB_PRIV_handleInterrupt()

void USB_PRIV_handleInterrupt ( void )

Decode one pending USB interrupt and notify the application.

Here is the call graph for this function:
Here is the caller graph for this function:

◆ USB_PRIV_init()

bool USB_PRIV_init ( void )

Enable and reset the USB hardware, configure pins and packet memory.

Returns
true once HSI48 is ready and initialization completes.
Here is the call graph for this function:
Here is the caller graph for this function:

◆ USB_PRIV_read()

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

Copy one completed OUT packet from packet memory and rearm reception.

The received length is reported even when the destination is too small; in that case the packet remains pending and the OUT endpoint is not rearmed.

Returns
true when a complete packet fit in the destination and was consumed.
Here is the call graph for this function:
Here is the caller graph for this function:

◆ USB_PRIV_setAddress()

void USB_PRIV_setAddress ( uint8_t address)

Write the seven-bit device address while keeping the device enabled.

Here is the caller graph for this function:

◆ USB_PRIV_stall()

void USB_PRIV_stall ( uint8_t endpoint,
PHAL_USB_EndpointDirection_t direction )

Set one endpoint direction to the USB STALL handshake state.

Here is the call graph for this function:
Here is the caller graph for this function:

◆ USB_PRIV_write()

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

Copy one IN packet to packet memory and make it available to the host.

Returns
true when the endpoint was idle and accepted the packet.
Here is the call graph for this function:
Here is the caller graph for this function:

◆ usb_receive_address()

static uint16_t usb_receive_address ( uint8_t endpoint)
static

Calculate the packet-memory byte address of an endpoint's OUT buffer.

Here is the call graph for this function:
Here is the caller graph for this function:

◆ usb_reset_peripheral()

static void usb_reset_peripheral ( void )
static

Reset and power up USB into a detached, interrupt-disabled state.

Here is the call graph for this function:
Here is the caller graph for this function:

◆ usb_set_status()

static void usb_set_status ( uint8_t endpoint,
PHAL_USB_EndpointDirection_t direction,
uint8_t status )
static

Change the hardware handshake state for one endpoint direction.

Here is the call graph for this function:
Here is the caller graph for this function:

◆ usb_transmit_address()

static uint16_t usb_transmit_address ( uint8_t endpoint)
static

Calculate the packet-memory byte address of an endpoint's IN buffer.

Here is the caller graph for this function:

Variable Documentation

◆ USB_PRIV_BTABLE_ENTRY_SIZE_BYTES

uint8_t USB_PRIV_BTABLE_ENTRY_SIZE_BYTES = 8U
staticconstexpr

◆ USB_PRIV_BTABLE_OFFSET

uint16_t USB_PRIV_BTABLE_OFFSET = 0U
staticconstexpr

◆ USB_PRIV_BTABLE_WORD_SIZE_BYTES

uint8_t USB_PRIV_BTABLE_WORD_SIZE_BYTES = 2U
staticconstexpr

◆ USB_PRIV_BUFFER_START

uint16_t USB_PRIV_BUFFER_START = 64U
staticconstexpr

◆ USB_PRIV_BUFFERS_PER_ENDPOINT

uint8_t USB_PRIV_BUFFERS_PER_ENDPOINT = 2U
staticconstexpr

◆ USB_PRIV_CC1_PIN

uint8_t USB_PRIV_CC1_PIN = 8U
staticconstexpr

◆ USB_PRIV_CC2_PIN

uint8_t USB_PRIV_CC2_PIN = 9U
staticconstexpr

◆ USB_PRIV_CLOCK_TIMEOUT

uint32_t USB_PRIV_CLOCK_TIMEOUT = 1'000'000U
staticconstexpr

◆ USB_PRIV_DM_PIN

uint8_t USB_PRIV_DM_PIN = 11U
staticconstexpr

◆ USB_PRIV_DP_PIN

uint8_t USB_PRIV_DP_PIN = 12U
staticconstexpr

◆ USB_PRIV_ENDPOINT_COUNT

uint8_t USB_PRIV_ENDPOINT_COUNT = 8U
staticconstexpr

◆ USB_PRIV_ENDPOINT_REGISTER_STRIDE

uint8_t USB_PRIV_ENDPOINT_REGISTER_STRIDE = 2U
staticconstexpr

◆ USB_PRIV_GPIO_MODE_BIT_WIDTH

uint8_t USB_PRIV_GPIO_MODE_BIT_WIDTH = 2U
staticconstexpr

◆ USB_PRIV_GPIO_MODE_BITS

uint8_t USB_PRIV_GPIO_MODE_BITS = 0b11U
staticconstexpr

◆ USB_PRIV_RX_BLOCK_COUNT

uint8_t USB_PRIV_RX_BLOCK_COUNT = 1U
staticconstexpr

◆ USB_PRIV_RX_BLOCK_COUNT_SHIFT

uint8_t USB_PRIV_RX_BLOCK_COUNT_SHIFT = 10U
staticconstexpr

◆ USB_PRIV_STARTUP_DELAY

uint32_t USB_PRIV_STARTUP_DELAY = 1'000U
staticconstexpr

◆ USB_PRIV_STATUS_NAK

uint8_t USB_PRIV_STATUS_NAK = 2U
staticconstexpr

◆ USB_PRIV_STATUS_RX_SHIFT

uint8_t USB_PRIV_STATUS_RX_SHIFT = 12U
staticconstexpr

◆ USB_PRIV_STATUS_STALL

uint8_t USB_PRIV_STATUS_STALL = 1U
staticconstexpr

◆ USB_PRIV_STATUS_TX_SHIFT

uint8_t USB_PRIV_STATUS_TX_SHIFT = 4U
staticconstexpr

◆ USB_PRIV_STATUS_VALID

uint8_t USB_PRIV_STATUS_VALID = 3U
staticconstexpr