USBD driver

group nrfx_usbd

Universal Serial Bus Device (USBD) peripheral driver.

Macros for creating endpoint identifiers.

Auxiliary macros for creating endpoint identifiers compatible with the USB specification.

NRFX_USBD_EPIN(n)

Create identifier for IN endpoint.

Simple macro to create IN endpoint identifier for given endpoint number.

Parameters
  • n[in] Endpoint number.

Returns

Endpoint identifier that connects endpoint number and endpoint direction.

NRFX_USBD_EPOUT(n)

Create identifier for OUT endpoint.

Simple macro to create OUT endpoint identifier for given endpoint number.

Parameters
  • n[in] Endpoint number.

Returns

Endpoint identifier that connects endpoint number and endpoint direction.

Defines

NRFX_USBD_EPSIZE

Number of bytes in the endpoint.

NRFX_USBD_ISOSIZE

Number of bytes for isochronous endpoints.

Number of bytes for isochronous endpoints in total. This number would be shared between IN and OUT endpoint. It may be also assigned totaly to one endpoint.

NRFX_USBD_FEEDER_BUFFER_SIZE

The size of internal feeder buffer.

NRFX_USBD_TRANSFER_IN(name, tx_buff, tx_size, tx_flags)

Auxiliary macro for declaring IN transfer description with optional flags.

The base macro for creating transfers with any configuration option.

Parameters
  • name – Instance name.

  • tx_buff – Buffer to transfer.

  • tx_size – Transfer size.

  • tx_flags – Flags for the transfer (see nrfx_usbd_transfer_flags_t).

Returns

Configured variable with total transfer description.

NRFX_USBD_TRANSFER_OUT(name, rx_buff, rx_size)

Helper macro for declaring OUT transfer item (nrfx_usbd_transfer_t).

Parameters
  • name – Instance name.

  • rx_buff – Buffer to transfer.

  • rx_size – Transfer size.

Typedefs

typedef void (*nrfx_usbd_event_handler_t)(nrfx_usbd_evt_t const *p_event)

USBD event callback function type.

Param p_event

[in] Event information structure.

typedef bool (*nrfx_usbd_feeder_t)(nrfx_usbd_ep_transfer_t *p_next, void *p_context, size_t ep_size)

USBD transfer feeder.

Pointer for a transfer feeder. Transfer feeder is a feedback function used to prepare a single TX (Device->Host) endpoint transfer.

The transfers provided by the feeder must be simple:

  • The size of the transfer provided by this function is limited to a single endpoint buffer. Bigger transfers are not handled automatically in this case.

  • Flash transfers are not automatically supported- you must copy them to the RAM buffer before.

Note

This function may use nrfx_usbd_feeder_buffer_get to gain a temporary buffer that can be used to prepare transfer.

Param p_next

[out] Structure with the data for the next transfer to be filled. Required only if the function returns true.

Param p_context

[inout] Context variable configured with the transfer.

Param ep_size

[in] The endpoint size.

Retval false

The current transfer is the last one - you do not need to call the function again.

Retval true

There is more data to be prepared and when the current transfer finishes, the feeder function is expected to be called again.

typedef bool (*nrfx_usbd_consumer_t)(nrfx_usbd_ep_transfer_t *p_next, void *p_context, size_t ep_size, size_t data_size)

USBD transfer consumer.

Pointer for a transfer consumer. Transfer consumer is a feedback function used to prepare a single RX (Host->Device) endpoint transfer.

The transfer must provide a buffer big enough to fit the whole data from the endpoint. Otherwise, the NRFX_USBD_EP_OVERLOAD event is generated.

Param p_next

[out] Structure with the data for the next transfer to be filled. Required only if the function returns true.

Param p_context

[inout] Context variable configured with the transfer.

Param ep_size

[in] The endpoint size.

Param data_size

[in] Number of received bytes in the endpoint buffer.

Retval false

Current transfer is the last one - you do not need to call the function again.

Retval true

There is more data to be prepared and when current transfer finishes, the feeder function is expected to be called again.

Enums

enum nrfx_usbd_ep_t

Endpoint identifier.

Endpoint identifier used in the driver. This endpoint number is consistent with USB 2.0 specification.

Values:

enumerator NRFX_USBD_EPOUT0

Endpoint OUT 0

enumerator NRFX_USBD_EPOUT1

Endpoint OUT 1

enumerator NRFX_USBD_EPOUT2

Endpoint OUT 2

enumerator NRFX_USBD_EPOUT3

Endpoint OUT 3

enumerator NRFX_USBD_EPOUT4

Endpoint OUT 4

enumerator NRFX_USBD_EPOUT5

Endpoint OUT 5

enumerator NRFX_USBD_EPOUT6

Endpoint OUT 6

enumerator NRFX_USBD_EPOUT7

Endpoint OUT 7

enumerator NRFX_USBD_EPOUT8

Endpoint OUT 8

enumerator NRFX_USBD_EPIN0

Endpoint IN 0

enumerator NRFX_USBD_EPIN1

Endpoint IN 1

enumerator NRFX_USBD_EPIN2

Endpoint IN 2

enumerator NRFX_USBD_EPIN3

Endpoint IN 3

enumerator NRFX_USBD_EPIN4

Endpoint IN 4

enumerator NRFX_USBD_EPIN5

Endpoint IN 5

enumerator NRFX_USBD_EPIN6

Endpoint IN 6

enumerator NRFX_USBD_EPIN7

Endpoint IN 7

enumerator NRFX_USBD_EPIN8

Endpoint IN 8

enum nrfx_usbd_event_type_t

Events generated by the driver.

Enumeration of possible events that may be generated by the driver.

Values:

enumerator NRFX_USBD_EVT_SOF

Start Of Frame event on USB bus detected.

enumerator NRFX_USBD_EVT_RESET

Reset condition on USB bus detected.

enumerator NRFX_USBD_EVT_SUSPEND

This device should go to suspend mode now.

enumerator NRFX_USBD_EVT_RESUME

This device should resume from suspend now.

enumerator NRFX_USBD_EVT_WUREQ

Wakeup request - the USBD peripheral is ready to generate WAKEUP signal after exiting low power mode.

enumerator NRFX_USBD_EVT_SETUP

Setup frame received and decoded.

enumerator NRFX_USBD_EVT_EPTRANSFER

For Rx (OUT: Host->Device):

  1. The packet has been received but there is no buffer prepared for transfer already.

  2. Whole transfer has been finished.

For Tx (IN: Device->Host): The last packet from requested transfer has been transfered over USB bus and acknowledged.

enumerator NRFX_USBD_EVT_CNT

Number of defined events.

enum nrfx_usbd_ep_status_t

Endpoint status codes.

Status codes that may be returned by nrfx_usbd_ep_status_get or, except for NRFX_USBD_EP_BUSY, reported together with NRFX_USBD_EVT_EPTRANSFER.

Values:

enumerator NRFX_USBD_EP_OK

No error occured.

enumerator NRFX_USBD_EP_WAITING

Data received, no buffer prepared already - waiting for configured transfer.

enumerator NRFX_USBD_EP_OVERLOAD

Received number of bytes cannot fit given buffer. This error would also be returned when next_transfer function has been defined but currently received data cannot fit completely in current buffer. No data split from single endpoint transmission is supported.

When this error is reported - data is left inside endpoint buffer. Clear endpoint or prepare new buffer and read it.

enumerator NRFX_USBD_EP_ABORTED

EP0 transfer can be aborted when new setup comes. Any other transfer can be aborted by USB reset or driver stopping.

enumerator NRFX_USBD_EP_BUSY

Transfer is in progress.

enum nrfx_usbd_transfer_flags_t

Flags for the current transfer.

Flags configured for the transfer that can be merged using the bitwise ‘or’ operator (|).

Values:

enumerator NRFX_USBD_TRANSFER_ZLP_FLAG

Add a zero-length packet.

Functions

nrfx_err_t nrfx_usbd_init(nrfx_usbd_event_handler_t event_handler)

Driver initialization.

Parameters
  • event_handler[in] Event handler provided by the user. Cannot be null.

Return values
  • NRFX_SUCCESS – Initialization successful.

  • NRFX_ERROR_INVALID_STATE – Driver was already initialized.

void nrfx_usbd_uninit(void)

Driver deinitialization.

void nrfx_usbd_enable(void)

Enable the USBD port.

After calling this function USBD peripheral would be enabled. The USB LDO would be enabled. Enabled USBD peripheral would request HFCLK. This function does not enable external oscillator, so if it is not enabled by other part of the program after enabling USBD driver HFINT would be used for the USBD peripheral. It is perfectly fine until USBD is started. See nrfx_usbd_start.

In normal situation this function should be called in reaction to USBDETECTED event from POWER peripheral.

Interrupts and USB pins pull-up would stay disabled until nrfx_usbd_start function is called.

void nrfx_usbd_disable(void)

Disable the USBD port.

After calling this function USBD peripheral would be disabled. No events would be detected or processed by the driver. Clock for the peripheral would be disconnected.

void nrfx_usbd_start(bool enable_sof)

Start USB functionality.

After calling this function USBD peripheral should be fully functional and all new incoming events / interrupts would be processed by the driver.

Also only after calling this function host sees new connected device.

Call this function when USBD power LDO regulator is ready - on USBPWRRDY event from POWER peripheral.

Before USBD interrupts are enabled, external HFXO is requested.

Note

If the isochronous endpoints are going to be used, it is required to enable the SOF. In other case any isochronous endpoint would stay busy after first transmission.

Parameters
  • enable_sof – The flag that is used to enable SOF processing. If it is false, SOF interrupt is left disabled and will not be generated. This improves power saving if SOF is not required.

void nrfx_usbd_stop(void)

Stop USB functionality.

This function disables USBD pull-up and interrupts.

The HFXO request is released in this function.

Note

This function can also be used to logically disconnect USB from the HOST that would force it to enumerate device after calling nrfx_usbd_start.

bool nrfx_usbd_is_initialized(void)

Check if driver is initialized.

Return values
  • false – Driver is not initialized.

  • true – Driver is initialized.

bool nrfx_usbd_is_enabled(void)

Check if driver is enabled.

Return values
  • false – Driver is disabled.

  • true – Driver is enabled.

bool nrfx_usbd_is_started(void)

Check if driver is started.

Note

The USBD peripheral interrupt state is checked.

Return values
  • false – Driver is not started.

  • true – Driver is started (fully functional).

bool nrfx_usbd_suspend(void)

Suspend USBD operation.

The USBD peripheral is forced to go into the low power mode. The function has to be called in the reaction to NRFX_USBD_EVT_SUSPEND event when the firmware is ready.

After successful call of this function most of the USBD registers would be unavailable.

Note

Check returned value for the feedback if suspending was successful.

Return values
  • true – USBD peripheral successfully suspended.

  • false – USBD peripheral was not suspended due to resume detection.

bool nrfx_usbd_wakeup_req(void)

Start wake up procedure.

The USBD peripheral is forced to quit the low power mode. After calling this function all the USBD registers would be available.

The hardware starts measuring time when wake up is possible. This may take 0-5ms depending on how long the SUSPEND state was kept on the USB line.

When NRFX_USBD_EVT_WUREQ event is generated it means that Wake Up signaling has just been started on the USB lines.

Note

Do not expect only NRFX_USBD_EVT_WUREQ event. There always may appear NRFX_USBD_EVT_RESUME event.

Note

NRFX_USBD_EVT_WUREQ event means that Remote WakeUp signal has just begun to be generated. This may take up to 20ms for the bus to become active.

Return values
  • true – WakeUp procedure started.

  • false – No WakeUp procedure started - bus is already active.

bool nrfx_usbd_suspend_check(void)

Check if USBD is in SUSPEND mode.

Note

This is the information about peripheral itself, not about the bus state.

Return values
  • true – USBD peripheral is suspended.

  • false – USBD peripheral is active.

void nrfx_usbd_suspend_irq_config(void)

Enable only interrupts that should be processed in SUSPEND mode.

Auxiliary function to help with SUSPEND mode integration. It enables only the interrupts that can be properly processed without stable HFCLK.

Normally all the interrupts are enabled. Use this function to suspend interrupt processing that may require stable HFCLK until the clock is enabled.

void nrfx_usbd_active_irq_config(void)

Default active interrupt configuration.

Default interrupt configuration. Use in a pair with nrfx_usbd_active_irq_config.

bool nrfx_usbd_bus_suspend_check(void)

Check the bus state.

This function checks if the bus state is suspended.

Note

The value returned by this function changes on SUSPEND and RESUME event processing.

Return values
  • true – USBD bus is suspended.

  • false – USBD bus is active.

void nrfx_usbd_force_bus_wakeup(void)

Force the bus state to active.

void nrfx_usbd_ep_max_packet_size_set(nrfx_usbd_ep_t ep, uint16_t size)

Configure packet size that should be supported by the endpoint.

The real endpoint buffer size is always the same. This value sets max packet size that would be transmitted over the endpoint. This is required by the driver.

Note

Endpoint size is always set to NRFX_USBD_EPSIZE or NRFX_USBD_ISOSIZE / 2 when nrfx_usbd_ep_enable function is called.

Parameters
  • ep[in] Endpoint number.

  • size[in] Required maximum packet size.

uint16_t nrfx_usbd_ep_max_packet_size_get(nrfx_usbd_ep_t ep)

Get configured endpoint packet size.

Function to get configured endpoint size on the buffer.

Parameters
  • ep[in] Endpoint number.

Returns

Maximum pocket size configured on selected endpoint.

bool nrfx_usbd_ep_enable_check(nrfx_usbd_ep_t ep)

Check if the selected endpoint is enabled.

Parameters
  • ep[in] Endpoint number to check.

Return values
  • true – Endpoint is enabled.

  • false – Endpoint is disabled.

void nrfx_usbd_ep_enable(nrfx_usbd_ep_t ep)

Enable selected endpoint.

This function enables endpoint itself and its interrupts.

Note

Max packet size is set to endpoint default maximum value.

Parameters
  • ep[in] Endpoint number to enable.

void nrfx_usbd_ep_disable(nrfx_usbd_ep_t ep)

Disable selected endpoint.

This function disables endpoint itself and its interrupts.

Parameters
  • ep[in] Endpoint number to disable.

void nrfx_usbd_ep_default_config(void)

Disable all endpoints except for EP0.

Disable all endpoints that can be disabled in USB device while it is still active.

nrfx_err_t nrfx_usbd_ep_transfer(nrfx_usbd_ep_t ep, nrfx_usbd_transfer_t const *p_transfer)

Start sending data over endpoint.

Function initializes endpoint transmission. This is asynchronous function - it finishes immediately after configuration for transmission is prepared.

Note

Data buffer pointed by p_data have to be kept active till NRFX_USBD_EVT_EPTRANSFER event is generated.

Parameters
  • ep[in] Endpoint number. For IN endpoint sending would be initiated. For OUT endpoint receiving would be initiated.

  • p_transfer[in] Transfer parameters.

Return values
  • NRFX_SUCCESS – Transfer queued or started.

  • NRFX_ERROR_BUSY – Selected endpoint is pending.

  • NRFX_ERROR_INVALID_ADDR – Unexpected transfer on EPIN0 or EPOUT0.

nrfx_err_t nrfx_usbd_ep_handled_transfer(nrfx_usbd_ep_t ep, nrfx_usbd_handler_desc_t const *p_handler)

Start sending data over the endpoint using the transfer handler function.

This function initializes an endpoint transmission. Just before data is transmitted, the transfer handler is called and it prepares a data chunk.

Parameters
  • ep[in] Endpoint number. For an IN endpoint, sending is initiated. For an OUT endpoint, receiving is initiated.

  • p_handler[in] Transfer handler - feeder for IN direction and consumer for OUT direction.

Return values
  • NRFX_SUCCESS – Transfer queued or started.

  • NRFX_ERROR_BUSY – Selected endpoint is pending.

  • NRFX_ERROR_INVALID_ADDR – Unexpected transfer on EPIN0 or EPOUT0.

void *nrfx_usbd_feeder_buffer_get(void)

Get the temporary buffer to be used by the feeder.

This buffer is used for TX transfers and it can be reused automatically when the transfer is finished. Use it for transfer preparation.

May be used inside the feeder configured in nrfx_usbd_ep_handled_transfer.

Returns

Pointer to the buffer that can be used temporarily.

nrfx_usbd_ep_status_t nrfx_usbd_ep_status_get(nrfx_usbd_ep_t ep, size_t *p_size)

Get the information about last finished or current transfer.

Function returns the status of the last buffer set for transfer on selected endpoint. The status considers last buffer set by nrfx_usbd_ep_transfer function or by transfer callback function.

Parameters
  • ep[in] Endpoint number.

  • p_size[out] Information about the current/last transfer size.

Returns

Endpoint status.

size_t nrfx_usbd_epout_size_get(nrfx_usbd_ep_t ep)

Get number of received bytes.

Get the number of received bytes. The function behavior is undefined when called on IN endpoint.

Parameters
  • ep[in] Endpoint number.

Returns

Number of received bytes.

bool nrfx_usbd_ep_is_busy(nrfx_usbd_ep_t ep)

Check if endpoint buffer is ready or is under USB IP control.

Function to test if endpoint is busy. Endpoint that is busy cannot be accessed by MCU. It means that:

  • OUT (TX) endpoint: Last uploaded data is still in endpoint and is waiting to be received by the host.

  • IN (RX) endpoint: Endpoint is ready to receive data from the host and the endpoint does not have any data. When endpoint is not busy:

  • OUT (TX) endpoint: New data can be uploaded.

  • IN (RX) endpoint: New data can be downloaded using nrfx_usbd_ep_transfer function.

Parameters
  • ep[in] Endpoint number.

Return values
  • false – Endpoint is not busy.

  • true – Endpoint is busy.

void nrfx_usbd_ep_stall(nrfx_usbd_ep_t ep)

Stall endpoint.

Stall endpoit to send error information during next transfer request from the host.

Note

To stall endpoint it is safer to use nrfx_usbd_setup_stall

Note

Stalled endpoint would not be cleared when DMA transfer finishes.

Parameters
  • ep[in] Endpoint number to stall.

void nrfx_usbd_ep_stall_clear(nrfx_usbd_ep_t ep)

Clear stall flag on endpoint.

This function clears endpoint that is stalled.

Note

If it is OUT endpoint (receiving) it would be also prepared for reception. It means that busy flag would be set.

Note

In endpoint (transmitting) would not be cleared - it gives possibility to write new data before transmitting.

Parameters
  • ep[in] Endpoint number.

bool nrfx_usbd_ep_stall_check(nrfx_usbd_ep_t ep)

Check if endpoint is stalled.

This function gets stall state of selected endpoint.

Parameters
  • ep[in] Endpoint number to check.

Return values
  • false – Endpoint is not stalled.

  • true – Endpoint is stalled.

void nrfx_usbd_ep_dtoggle_clear(nrfx_usbd_ep_t ep)

Clear current endpoint data toggle.

Parameters
  • ep[in] Endpoint number to clear.

void nrfx_usbd_setup_get(nrfx_usbd_setup_t *p_setup)

Get parsed setup data.

Function fills the parsed setup data structure.

Parameters
  • p_setup[out] Pointer to data structure that would be filled by parsed data.

void nrfx_usbd_setup_data_clear(void)

Clear the control endpoint for packet reception during DATA stage.

This function may be called if any more data in control write transfer is expected. Clears only OUT endpoint to be able to take another OUT data token. It does not allow STATUS stage.

void nrfx_usbd_setup_clear(void)

Clear setup endpoint.

This function acknowledges setup when SETUP command was received and processed. It has to be called if no data respond for the SETUP command is sent.

void nrfx_usbd_setup_stall(void)

Stall setup endpoint.

Mark an error on setup endpoint.

void nrfx_usbd_ep_abort(nrfx_usbd_ep_t ep)

Abort pending transfer on selected endpoint.

Parameters
  • ep[in] Endpoint number.

nrfx_usbd_ep_t nrfx_usbd_last_setup_dir_get(void)

Get the information about expected transfer SETUP data direction.

Function returns the information about last expected transfer direction.

Return values
  • NRFX_USBD_EPOUT0 – Expecting OUT (Host->Device) direction or no data.

  • NRFX_USBD_EPIN0 – Expecting IN (Device->Host) direction.

void nrfx_usbd_transfer_out_drop(nrfx_usbd_ep_t ep)

Drop transfer on OUT endpoint.

Parameters
  • ep[in] OUT endpoint ID.

struct nrfx_usbd_evt_t
#include <>

Event structure.

Structure passed to event handler.

Public Members

nrfx_usbd_event_type_t type

Event type.

uint16_t framecnt

Current value of frame counter.

struct nrfx_usbd_evt_t.[anonymous].[anonymous] sof

Data available for NRFX_USBD_EVT_SOF.

nrfx_usbd_ep_t ep

Endpoint number.

struct nrfx_usbd_evt_t.[anonymous].[anonymous] isocrc

Isochronouns channel endpoint number.

nrfx_usbd_ep_status_t status

Status for the endpoint.

struct nrfx_usbd_evt_t.[anonymous].[anonymous] eptransfer

Endpoint transfer status.

union nrfx_usbd_evt_t.[anonymous] data

Union to store event data.

union nrfx_usbd_data_ptr_t
#include <>

Universal data pointer.

Universal data pointer that can be used for any type of transfer.

Public Members

void const *tx

Constant TX buffer pointer.

void *rx

Writable RX buffer pointer.

uint32_t addr

Numeric value used internally by the driver.

struct nrfx_usbd_ep_transfer_t
#include <>

Structure to be filled with information about the next transfer.

This is used mainly for transfer feeders and consumers. It describes a single endpoint transfer and therefore the size of the buffer can never be higher than the endpoint size.

Public Members

nrfx_usbd_data_ptr_t p_data

Union with available data pointers used by the driver.

size_t size

Size of the requested transfer.

struct nrfx_usbd_transfer_t
#include <>

Total transfer configuration.

This structure is used to configure total transfer information. It is used by internal built-in feeders and consumers.

Public Members

nrfx_usbd_data_ptr_t p_data

Union with available data pointers used by the driver.

size_t size

Total size of the requested transfer.

uint32_t flags

Transfer flags.

Use the nrfx_usbd_transfer_flags_t values.

union nrfx_usbd_handler_t
#include <>

Universal transfer handler.

Union with feeder and consumer function pointer.

Public Members

nrfx_usbd_feeder_t feeder

Feeder function pointer.

nrfx_usbd_consumer_t consumer

Consumer function pointer.

struct nrfx_usbd_handler_desc_t
#include <>

USBD transfer descriptor.

Universal structure that may hold the setup for callback configuration for IN or OUT type of the transfer.

Public Members

nrfx_usbd_handler_t handler

Handler for the current transfer, function pointer.

void *p_context

Context for the transfer handler.

struct nrfx_usbd_setup_t
#include <>

Setup packet structure.

Structure that contains interpreted SETUP packet as described in USB specification.

Public Members

uint8_t bmRequestType

byte 0

uint8_t bRequest

byte 1

uint16_t wValue

byte 2, 3

uint16_t wIndex

byte 4, 5

uint16_t wLength

byte 6, 7