caffeine-hal 0.6.6
A Header-Defined Interface c library, it provides the hal layer for the Caffeine framework
Loading...
Searching...
No Matches
Data Structures | Typedefs | Enumerations | Functions
cfn_hal_usb.h File Reference

USB Device HAL API (PCD Layer). This HAL focuses strictly on the Peripheral Controller Device (PCD) hardware. High-level USB stacks utilize this interface to manage endpoints and transfers. More...

#include "cfn_hal.h"
#include "cfn_hal_base.h"
#include "cfn_hal_gpio.h"
#include "cfn_hal_types.h"

Go to the source code of this file.

Data Structures

struct  cfn_hal_usb_config_t
 USB configuration structure. More...
 
struct  cfn_hal_usb_phy_t
 USB hardware physical mapping. More...
 
struct  cfn_hal_usb_api_s
 USB Virtual Method Table (VMT). Focuses exclusively on Peripheral Controller Device (PCD) functions. More...
 

Typedefs

typedef struct cfn_hal_usb_s cfn_hal_usb_t
 
typedef struct cfn_hal_usb_api_s cfn_hal_usb_api_t
 
typedef void(* cfn_hal_usb_callback_t) (cfn_hal_usb_t *driver, uint32_t event_mask, uint32_t error_mask, uint8_t ep_addr, void *user_arg)
 USB callback signature.
 

Enumerations

enum  cfn_hal_usb_event_t {
  CFN_HAL_USB_EVENT_NONE = 0 , CFN_HAL_USB_EVENT_SUSPEND = CFN_HAL_BIT(0) , CFN_HAL_USB_EVENT_RESUME = CFN_HAL_BIT(1) , CFN_HAL_USB_EVENT_RESET = CFN_HAL_BIT(2) ,
  CFN_HAL_USB_EVENT_SETUP_READY = CFN_HAL_BIT(3) , CFN_HAL_USB_EVENT_EP_DATA_OUT = CFN_HAL_BIT(4) , CFN_HAL_USB_EVENT_EP_DATA_IN = CFN_HAL_BIT(5)
}
 USB nominal event flags. These events drive the state machine of the upper USB stack. More...
 
enum  cfn_hal_usb_error_t { CFN_HAL_USB_ERROR_NONE = 0 , CFN_HAL_USB_ERROR_ISO_OUT = CFN_HAL_BIT(0) , CFN_HAL_USB_ERROR_ISO_IN = CFN_HAL_BIT(1) , CFN_HAL_USB_ERROR_GENERAL = CFN_HAL_BIT(2) }
 USB exception error flags. More...
 
enum  cfn_hal_usb_ep_type_t { CFN_HAL_USB_EP_TYPE_CTRL , CFN_HAL_USB_EP_TYPE_ISOC , CFN_HAL_USB_EP_TYPE_BULK , CFN_HAL_USB_EP_TYPE_INTR }
 USB endpoint types. More...
 

Functions

 CFN_HAL_VMT_CHECK (struct cfn_hal_usb_api_s)
 
 CFN_HAL_CREATE_DRIVER_TYPE (usb, cfn_hal_usb_config_t, cfn_hal_usb_api_t, cfn_hal_usb_phy_t, cfn_hal_usb_callback_t)
 
CFN_HAL_INLINE void cfn_hal_usb_populate (cfn_hal_usb_t *driver, uint32_t peripheral_id, struct cfn_hal_clock_s *clock, void *dependency, const cfn_hal_usb_api_t *api, const cfn_hal_usb_phy_t *phy, const cfn_hal_usb_config_t *config, cfn_hal_usb_callback_t callback, void *user_arg)
 
CFN_HAL_INLINE cfn_hal_error_code_t cfn_hal_usb_config_validate (const cfn_hal_usb_t *driver, const cfn_hal_usb_config_t *config)
 Validates the USB configuration.
 
CFN_HAL_INLINE cfn_hal_error_code_t cfn_hal_usb_init (cfn_hal_usb_t *driver)
 Initializes the USB driver.
 
CFN_HAL_INLINE cfn_hal_error_code_t cfn_hal_usb_deinit (cfn_hal_usb_t *driver)
 Deinitializes the USB driver.
 
CFN_HAL_INLINE cfn_hal_error_code_t cfn_hal_usb_config_set (cfn_hal_usb_t *driver, const cfn_hal_usb_config_t *config)
 Sets the USB configuration.
 
CFN_HAL_INLINE cfn_hal_error_code_t cfn_hal_usb_config_get (cfn_hal_usb_t *driver, cfn_hal_usb_config_t *config)
 Gets the current USB configuration.
 
CFN_HAL_INLINE cfn_hal_error_code_t cfn_hal_usb_callback_register (cfn_hal_usb_t *driver, const cfn_hal_usb_callback_t callback, void *user_arg)
 Registers a callback for USB events and errors.
 
CFN_HAL_INLINE cfn_hal_error_code_t cfn_hal_usb_power_state_set (cfn_hal_usb_t *driver, cfn_hal_power_state_t state)
 Sets the USB power state.
 
CFN_HAL_INLINE cfn_hal_error_code_t cfn_hal_usb_event_enable (cfn_hal_usb_t *driver, uint32_t event_mask)
 Enables one or more USB nominal events.
 
CFN_HAL_INLINE cfn_hal_error_code_t cfn_hal_usb_event_disable (cfn_hal_usb_t *driver, uint32_t event_mask)
 Disables one or more USB nominal events.
 
CFN_HAL_INLINE cfn_hal_error_code_t cfn_hal_usb_event_get (cfn_hal_usb_t *driver, uint32_t *event_mask)
 Retrieves the current USB nominal event status.
 
CFN_HAL_INLINE cfn_hal_error_code_t cfn_hal_usb_error_enable (cfn_hal_usb_t *driver, uint32_t error_mask)
 Enables one or more USB exception errors.
 
CFN_HAL_INLINE cfn_hal_error_code_t cfn_hal_usb_error_disable (cfn_hal_usb_t *driver, uint32_t error_mask)
 Disables one or more USB exception errors.
 
CFN_HAL_INLINE cfn_hal_error_code_t cfn_hal_usb_error_get (cfn_hal_usb_t *driver, uint32_t *error_mask)
 Retrieves the current USB exception error status.
 
CFN_HAL_INLINE cfn_hal_error_code_t cfn_hal_usb_start (cfn_hal_usb_t *driver)
 Starts the USB device controller (connects pull-up).
 
CFN_HAL_INLINE cfn_hal_error_code_t cfn_hal_usb_stop (cfn_hal_usb_t *driver)
 Stops the USB device controller (disconnects pull-up).
 
CFN_HAL_INLINE cfn_hal_error_code_t cfn_hal_usb_set_address (cfn_hal_usb_t *driver, uint8_t address)
 Sets the USB device address.
 
CFN_HAL_INLINE cfn_hal_error_code_t cfn_hal_usb_ep_open (cfn_hal_usb_t *driver, uint8_t ep_addr, cfn_hal_usb_ep_type_t ep_type, uint16_t ep_mps)
 Configures and enables a USB endpoint.
 
CFN_HAL_INLINE cfn_hal_error_code_t cfn_hal_usb_ep_close (cfn_hal_usb_t *driver, uint8_t ep_addr)
 Disables and closes a USB endpoint.
 
CFN_HAL_INLINE cfn_hal_error_code_t cfn_hal_usb_ep_transmit (cfn_hal_usb_t *driver, uint8_t ep_addr, const uint8_t *data, size_t length)
 Initiates data transmission on an IN endpoint.
 
CFN_HAL_INLINE cfn_hal_error_code_t cfn_hal_usb_ep_receive (cfn_hal_usb_t *driver, uint8_t ep_addr, uint8_t *buffer, size_t length)
 Prepares an OUT endpoint for data reception.
 
CFN_HAL_INLINE cfn_hal_error_code_t cfn_hal_usb_ep_stall (cfn_hal_usb_t *driver, uint8_t ep_addr, bool stall)
 Controls the stall condition of an endpoint.
 
CFN_HAL_INLINE cfn_hal_error_code_t cfn_hal_usb_read_setup_packet (cfn_hal_usb_t *driver, uint8_t *buffer)
 Reads the latest received 8-byte setup packet.
 
CFN_HAL_INLINE cfn_hal_error_code_t cfn_hal_usb_get_rx_data_size (cfn_hal_usb_t *driver, uint8_t ep_addr, size_t *size)
 Retrieves the size of the last received data on an endpoint.
 
cfn_hal_error_code_t cfn_hal_usb_construct (cfn_hal_usb_t *driver, const cfn_hal_usb_config_t *config, const cfn_hal_usb_phy_t *phy, struct cfn_hal_clock_s *clock, void *dependency, cfn_hal_usb_callback_t callback, void *user_arg)
 
cfn_hal_error_code_t cfn_hal_usb_destruct (cfn_hal_usb_t *driver)
 

Detailed Description

USB Device HAL API (PCD Layer). This HAL focuses strictly on the Peripheral Controller Device (PCD) hardware. High-level USB stacks utilize this interface to manage endpoints and transfers.

Copyright (c) 2026 Hisham Moussa Daou https://www.whileone.me

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

Typedef Documentation

◆ cfn_hal_usb_api_t

◆ cfn_hal_usb_callback_t

typedef void(* cfn_hal_usb_callback_t) (cfn_hal_usb_t *driver, uint32_t event_mask, uint32_t error_mask, uint8_t ep_addr, void *user_arg)

USB callback signature.

Parameters
driverPointer to the USB driver instance.
event_maskMask of triggered nominal events.
error_maskMask of triggered exception errors.
ep_addrEndpoint address associated with the event (if applicable).
user_argUser-defined argument passed during registration.

◆ cfn_hal_usb_t

typedef struct cfn_hal_usb_s cfn_hal_usb_t

Enumeration Type Documentation

◆ cfn_hal_usb_ep_type_t

USB endpoint types.

Enumerator
CFN_HAL_USB_EP_TYPE_CTRL 

Control endpoint

CFN_HAL_USB_EP_TYPE_ISOC 

Isochronous endpoint

CFN_HAL_USB_EP_TYPE_BULK 

Bulk endpoint

CFN_HAL_USB_EP_TYPE_INTR 

Interrupt endpoint

◆ cfn_hal_usb_error_t

USB exception error flags.

Enumerator
CFN_HAL_USB_ERROR_NONE 
CFN_HAL_USB_ERROR_ISO_OUT 

Isochronous OUT data lost

CFN_HAL_USB_ERROR_ISO_IN 

Isochronous IN data lost

CFN_HAL_USB_ERROR_GENERAL 

General hardware error

◆ cfn_hal_usb_event_t

USB nominal event flags. These events drive the state machine of the upper USB stack.

Enumerator
CFN_HAL_USB_EVENT_NONE 
CFN_HAL_USB_EVENT_SUSPEND 

Bus entered low power state

CFN_HAL_USB_EVENT_RESUME 

Bus activity resumed

CFN_HAL_USB_EVENT_RESET 

USB bus reset detected

CFN_HAL_USB_EVENT_SETUP_READY 

Setup packet received on EP0

CFN_HAL_USB_EVENT_EP_DATA_OUT 

Data received on endpoint (Host -> Device)

CFN_HAL_USB_EVENT_EP_DATA_IN 

Data transmitted on endpoint (Device -> Host)

Function Documentation

◆ CFN_HAL_CREATE_DRIVER_TYPE()

CFN_HAL_CREATE_DRIVER_TYPE ( usb  ,
cfn_hal_usb_config_t  ,
cfn_hal_usb_api_t  ,
cfn_hal_usb_phy_t  ,
cfn_hal_usb_callback_t   
)

◆ cfn_hal_usb_callback_register()

CFN_HAL_INLINE cfn_hal_error_code_t cfn_hal_usb_callback_register ( cfn_hal_usb_t driver,
const cfn_hal_usb_callback_t  callback,
void *  user_arg 
)

Registers a callback for USB events and errors.

Parameters
driverPointer to the USB driver instance.
callbackThe callback function to register.
user_argUser-defined argument passed to the callback.
Returns
CFN_HAL_ERROR_OK on success, or a specific error code on failure.

◆ cfn_hal_usb_config_get()

CFN_HAL_INLINE cfn_hal_error_code_t cfn_hal_usb_config_get ( cfn_hal_usb_t driver,
cfn_hal_usb_config_t config 
)

Gets the current USB configuration.

Parameters
driverPointer to the USB driver instance.
config[out] Pointer to store the configuration.
Returns
CFN_HAL_ERROR_OK on success, or a specific error code on failure.

◆ cfn_hal_usb_config_set()

CFN_HAL_INLINE cfn_hal_error_code_t cfn_hal_usb_config_set ( cfn_hal_usb_t driver,
const cfn_hal_usb_config_t config 
)

Sets the USB configuration.

Parameters
driverPointer to the USB driver instance.
configPointer to the configuration structure.
Returns
CFN_HAL_ERROR_OK on success, or a specific error code on failure.

◆ cfn_hal_usb_config_validate()

CFN_HAL_INLINE cfn_hal_error_code_t cfn_hal_usb_config_validate ( const cfn_hal_usb_t driver,
const cfn_hal_usb_config_t config 
)

Validates the USB configuration.

Parameters
driverPointer to the USB driver instance.
configPointer to the configuration structure.
Returns
CFN_HAL_ERROR_OK on success, or a specific error code on failure.

◆ cfn_hal_usb_construct()

cfn_hal_error_code_t cfn_hal_usb_construct ( cfn_hal_usb_t driver,
const cfn_hal_usb_config_t config,
const cfn_hal_usb_phy_t phy,
struct cfn_hal_clock_s *  clock,
void *  dependency,
cfn_hal_usb_callback_t  callback,
void *  user_arg 
)

◆ cfn_hal_usb_deinit()

CFN_HAL_INLINE cfn_hal_error_code_t cfn_hal_usb_deinit ( cfn_hal_usb_t driver)

Deinitializes the USB driver.

Parameters
driverPointer to the USB driver instance.
Returns
CFN_HAL_ERROR_OK on success, or a specific error code on failure.

◆ cfn_hal_usb_destruct()

cfn_hal_error_code_t cfn_hal_usb_destruct ( cfn_hal_usb_t driver)

◆ cfn_hal_usb_ep_close()

CFN_HAL_INLINE cfn_hal_error_code_t cfn_hal_usb_ep_close ( cfn_hal_usb_t driver,
uint8_t  ep_addr 
)

Disables and closes a USB endpoint.

Parameters
driverPointer to the USB driver instance.
ep_addrEndpoint address.
Returns
CFN_HAL_ERROR_OK on success, or a specific error code on failure.

◆ cfn_hal_usb_ep_open()

CFN_HAL_INLINE cfn_hal_error_code_t cfn_hal_usb_ep_open ( cfn_hal_usb_t driver,
uint8_t  ep_addr,
cfn_hal_usb_ep_type_t  ep_type,
uint16_t  ep_mps 
)

Configures and enables a USB endpoint.

Parameters
driverPointer to the USB driver instance.
ep_addrEndpoint address (including direction bit).
ep_typeEndpoint transfer type (Bulk, Interrupt, etc.).
ep_mpsMaximum Packet Size for the endpoint.
Returns
CFN_HAL_ERROR_OK on success, or a specific error code on failure.

◆ cfn_hal_usb_ep_receive()

CFN_HAL_INLINE cfn_hal_error_code_t cfn_hal_usb_ep_receive ( cfn_hal_usb_t driver,
uint8_t  ep_addr,
uint8_t *  buffer,
size_t  length 
)

Prepares an OUT endpoint for data reception.

Parameters
driverPointer to the USB driver instance.
ep_addrOUT endpoint address.
bufferPointer to the buffer where received data will be stored.
lengthMaximum number of bytes to receive.
Returns
CFN_HAL_ERROR_OK on success, or a specific error code on failure.

◆ cfn_hal_usb_ep_stall()

CFN_HAL_INLINE cfn_hal_error_code_t cfn_hal_usb_ep_stall ( cfn_hal_usb_t driver,
uint8_t  ep_addr,
bool  stall 
)

Controls the stall condition of an endpoint.

Parameters
driverPointer to the USB driver instance.
ep_addrEndpoint address.
stallTrue to set STALL, False to clear.
Returns
CFN_HAL_ERROR_OK on success, or a specific error code on failure.

◆ cfn_hal_usb_ep_transmit()

CFN_HAL_INLINE cfn_hal_error_code_t cfn_hal_usb_ep_transmit ( cfn_hal_usb_t driver,
uint8_t  ep_addr,
const uint8_t *  data,
size_t  length 
)

Initiates data transmission on an IN endpoint.

Parameters
driverPointer to the USB driver instance.
ep_addrIN endpoint address.
dataPointer to the buffer containing data to send.
lengthNumber of bytes to transmit.
Returns
CFN_HAL_ERROR_OK on success, or a specific error code on failure.

◆ cfn_hal_usb_error_disable()

CFN_HAL_INLINE cfn_hal_error_code_t cfn_hal_usb_error_disable ( cfn_hal_usb_t driver,
uint32_t  error_mask 
)

Disables one or more USB exception errors.

Parameters
driverPointer to the USB driver instance.
error_maskMask of errors to disable.
Returns
CFN_HAL_ERROR_OK on success, or a specific error code on failure.

◆ cfn_hal_usb_error_enable()

CFN_HAL_INLINE cfn_hal_error_code_t cfn_hal_usb_error_enable ( cfn_hal_usb_t driver,
uint32_t  error_mask 
)

Enables one or more USB exception errors.

Parameters
driverPointer to the USB driver instance.
error_maskMask of errors to enable.
Returns
CFN_HAL_ERROR_OK on success, or a specific error code on failure.

◆ cfn_hal_usb_error_get()

CFN_HAL_INLINE cfn_hal_error_code_t cfn_hal_usb_error_get ( cfn_hal_usb_t driver,
uint32_t *  error_mask 
)

Retrieves the current USB exception error status.

Parameters
driverPointer to the USB driver instance.
error_mask[out] Pointer to store the error mask.
Returns
CFN_HAL_ERROR_OK on success, or a specific error code on failure.

◆ cfn_hal_usb_event_disable()

CFN_HAL_INLINE cfn_hal_error_code_t cfn_hal_usb_event_disable ( cfn_hal_usb_t driver,
uint32_t  event_mask 
)

Disables one or more USB nominal events.

Parameters
driverPointer to the USB driver instance.
event_maskMask of events to disable.
Returns
CFN_HAL_ERROR_OK on success, or a specific error code on failure.

◆ cfn_hal_usb_event_enable()

CFN_HAL_INLINE cfn_hal_error_code_t cfn_hal_usb_event_enable ( cfn_hal_usb_t driver,
uint32_t  event_mask 
)

Enables one or more USB nominal events.

Parameters
driverPointer to the USB driver instance.
event_maskMask of events to enable.
Returns
CFN_HAL_ERROR_OK on success, or a specific error code on failure.

◆ cfn_hal_usb_event_get()

CFN_HAL_INLINE cfn_hal_error_code_t cfn_hal_usb_event_get ( cfn_hal_usb_t driver,
uint32_t *  event_mask 
)

Retrieves the current USB nominal event status.

Parameters
driverPointer to the USB driver instance.
event_mask[out] Pointer to store the event mask.
Returns
CFN_HAL_ERROR_OK on success, or a specific error code on failure.

◆ cfn_hal_usb_get_rx_data_size()

CFN_HAL_INLINE cfn_hal_error_code_t cfn_hal_usb_get_rx_data_size ( cfn_hal_usb_t driver,
uint8_t  ep_addr,
size_t *  size 
)

Retrieves the size of the last received data on an endpoint.

Parameters
driverPointer to the USB driver instance.
ep_addrEndpoint address.
size[out] Pointer to store the number of bytes received.
Returns
CFN_HAL_ERROR_OK on success, or a specific error code on failure.

◆ cfn_hal_usb_init()

CFN_HAL_INLINE cfn_hal_error_code_t cfn_hal_usb_init ( cfn_hal_usb_t driver)

Initializes the USB driver.

Parameters
driverPointer to the USB driver instance.
Returns
CFN_HAL_ERROR_OK on success, or a specific error code on failure.

◆ cfn_hal_usb_populate()

CFN_HAL_INLINE void cfn_hal_usb_populate ( cfn_hal_usb_t driver,
uint32_t  peripheral_id,
struct cfn_hal_clock_s *  clock,
void *  dependency,
const cfn_hal_usb_api_t api,
const cfn_hal_usb_phy_t phy,
const cfn_hal_usb_config_t config,
cfn_hal_usb_callback_t  callback,
void *  user_arg 
)

◆ cfn_hal_usb_power_state_set()

CFN_HAL_INLINE cfn_hal_error_code_t cfn_hal_usb_power_state_set ( cfn_hal_usb_t driver,
cfn_hal_power_state_t  state 
)

Sets the USB power state.

Parameters
driverPointer to the USB driver instance.
stateTarget power state.
Returns
CFN_HAL_ERROR_OK on success, or a specific error code on failure.

◆ cfn_hal_usb_read_setup_packet()

CFN_HAL_INLINE cfn_hal_error_code_t cfn_hal_usb_read_setup_packet ( cfn_hal_usb_t driver,
uint8_t *  buffer 
)

Reads the latest received 8-byte setup packet.

Parameters
driverPointer to the USB driver instance.
bufferPointer to the 8-byte buffer to store the setup packet.
Returns
CFN_HAL_ERROR_OK on success, or a specific error code on failure.

◆ cfn_hal_usb_set_address()

CFN_HAL_INLINE cfn_hal_error_code_t cfn_hal_usb_set_address ( cfn_hal_usb_t driver,
uint8_t  address 
)

Sets the USB device address.

Parameters
driverPointer to the USB driver instance.
addressAddress assigned by the host (0 to 127).
Returns
CFN_HAL_ERROR_OK on success, or a specific error code on failure.

◆ cfn_hal_usb_start()

CFN_HAL_INLINE cfn_hal_error_code_t cfn_hal_usb_start ( cfn_hal_usb_t driver)

Starts the USB device controller (connects pull-up).

Parameters
driverPointer to the USB driver instance.
Returns
CFN_HAL_ERROR_OK on success, or a specific error code on failure.

◆ cfn_hal_usb_stop()

CFN_HAL_INLINE cfn_hal_error_code_t cfn_hal_usb_stop ( cfn_hal_usb_t driver)

Stops the USB device controller (disconnects pull-up).

Parameters
driverPointer to the USB driver instance.
Returns
CFN_HAL_ERROR_OK on success, or a specific error code on failure.

◆ CFN_HAL_VMT_CHECK()

CFN_HAL_VMT_CHECK ( struct cfn_hal_usb_api_s  )