USB Device

USB Device Overview

A USB (Universal Serial Bus) device is a hardware component that connects to a host (e.g., a computer or microcontroller) via the USB interface. It enables data transfer and power delivery between the host and the device. USB is widely used for peripherals like keyboards, mice, storage drives, cameras, and more.

Usage

  1. All API functions have the first argument as the handle followed by data to the pointer.

  2. To initialize the usb device using rs call

  3. Transmit and receive data from the usb device using rs calls.

  4. Return codes of API functions are defined in header file.

Application Interface

USB device calls

Function

Description

rs_usbd_init

To open the usb device

rs_usbd_packet_received

To receive packet for the usb device

rs_usbd_transmit

To transmit the data to the usb device

Using these ‘rs’ calls to initialize usb device.

Error Code

  • Every API’s for the usb device and returns some success or failure values. Please refer below section,

USB device Header Details

Documentation from the relevant header as follows:

USB device Specific APIs.

Defines macros and functions specific to usb device functionality

Author

Embien RAPIDSEA Team

Copyright

Embien Technologies India Pvt. Ltd.

Typedefs

typedef struct tag_rs_usbd_endpoint rs_usbd_endpoint_t

Struct for USb bdx endpoint.

typedef struct tag_rs_usbd_config rs_usbd_config_t

Struct for USb bdx config.

Functions

rs_ret_val_t rs_usbd_init()

Initializes the USB device.

This function sets up the USB device stack and prepares it for operation

Returns:

0 on success or error code on failure

rs_ret_val_t rs_usbd_packet_received(rs_handle_t handle, uint32_t u32_ep, uint8_t *ptr_data, uint32_t u32_len)

Handles the reception of a USB packet.

This function processes a received packet from a USB endpoint.

Parameters:
  • handle[in] - The handle associated with the USB device.

  • u32_ep[in] - The endpoint number that received the packet.

  • ptr_data[out] - Pointer to the buffer where received data is stored.

  • u32_len[in] - The length of the received data.

Returns:

0 on success or error code on failure

rs_ret_val_t rs_usbd_transmit(rs_handle_t handle, uint32_t u32_ep, uint8_t *ptr_data, uint32_t u32_len)

Transmits data over a USB endpoint.

This function sends data through the specified USB endpoint

Parameters:
  • handle[in] - The handle associated with the USB device.

  • u32_ep[in] - The endpoint number to transmit data to.

  • ptr_data[in] - Pointer to the buffer containing data to be sent.

  • u32_len[in] - The length of the data to be transmitted.

Returns:

0 on success or error code on failure

rs_ret_val_t rcb_usbd_packet_received(rs_handle_t handle, uint32_t u32_ep, uint8_t *pre_data, uint32_t u32_len)

Handles the reception of a USB packet with custom handling.

This function processes the received USB packet and performs custom operations on the data received.

Parameters:
  • handle[in] - The handle associated with the USB device.

  • u32_ep[in] - The endpoint number where the packet was received.

  • ptr_data[in] - Pointer to the buffer where the received data is stored.

  • u32_len[in] - The length of the received data.

Returns:

0 on success or error code on failure

rs_ret_val_t rs_usbd_transfer_abort(rs_handle_t handle, uint32_t u32_ep)
rs_ret_val_t rs_usbd_set_pid(uint16_t u16_product_id)

Sets the USB product ID.

This function sets the product ID for the USB device descriptor.

Parameters:

u16_product_id[in] - The product ID to be set.

Returns:

0 on success or error code on failure

rs_ret_val_t rs_usbd_set_vid(uint16_t u16_vendor_id)

Sets the USB vendor ID.

This function sets the vendor ID for the USB device descriptor.

Parameters:

u16_vendor_id[in] - The vendor ID to be set.

Returns:

0 on success or error code on failure

rs_ret_val_t rs_usbd_set_device_name(uint8_t *ptr_device_name, uint8_t u8_len)

Sets the USB device name.

This function sets the device name for the USB device descriptor.

Parameters:

u16_vendor_id[in] - The vendor ID to be set.

Returns:

0 on success or error code on failure

struct tag_rs_usbd_endpoint
#include <rs_usbd.h>

Struct for USb bdx endpoint.

Public Members

uint8_t u8_address

Endpoint address.

uint8_t u8_type

Endpoint type.

uint16_t u16_max_packet_size

Maximum packet size.

struct tag_rs_usbd_config
#include <rs_usbd.h>

Struct for USb bdx config.

Public Members

uint16_t u16_vid

Vendor ID.

uint16_t u16_pid

Product ID.

uint8_t u8_usb_class

USB Device Class.

void *ptr_device_handle

Handle to the USB device.

rs_usbd_endpoint_t *ptr_usbd_ep

Pointer of usb device endpoints.

uint8_t u8_num_endpoints

Number of endpoints configured.