nRF52840 only: USB Device APIs. More...
Modules | |
USB driver configuration | |
Functions to check if selected PAN is present in current chip | |
Data Structures | |
struct | nrf_drv_usbd_evt_t |
Event structure. More... | |
union | nrf_drv_usbd_data_ptr_t |
Universal data pointer. More... | |
struct | nrf_drv_usbd_ep_transfer_t |
Structure to be filled with information about the next transfer. More... | |
struct | nrf_drv_usbd_transfer_t |
Total transfer configuration. More... | |
union | nrf_drv_usbd_handler_t |
Universal transfer handler. More... | |
struct | nrf_drv_usbd_handler_desc_t |
USBD transfer descriptor. More... | |
struct | nrf_drv_usbd_setup_t |
Setup packet structure. More... | |
Macros | |
#define | NRF_DRV_USBD_EPSIZE 64 |
Number of bytes in the endpoint. More... | |
#define | NRF_DRV_USBD_ISOSIZE 1024 |
Number of bytes for isochronous endpoints. More... | |
#define | NRF_DRV_USBD_FEEDER_BUFFER_SIZE NRF_DRV_USBD_EPSIZE |
The size of internal feeder buffer. More... | |
#define | NRF_DRV_USBD_TRANSFER_IN_FLAGS(name, tx_buff, tx_size, tx_flags) |
Auxiliary macro for declaring IN transfer description with flags. More... | |
#define | NRF_DRV_USBD_TRANSFER_IN(name, tx_buff, tx_size) NRF_DRV_USBD_TRANSFER_IN_FLAGS(name, tx_buff, tx_size, 0) |
Helper macro for declaring IN transfer description. More... | |
#define | NRF_DRV_USBD_TRANSFER_IN_ZLP(name, tx_buff, tx_size) |
Helper macro for declaring IN transfer description. More... | |
#define | NRF_DRV_USBD_TRANSFER_OUT(name, rx_buff, rx_size) |
Helper macro for declaring OUT transfer item (nrf_drv_usbd_transfer_t) More... | |
Typedefs | |
typedef void(* | nrf_drv_usbd_event_handler_t )(nrf_drv_usbd_evt_t const *const p_event) |
USBD event callback function type. More... | |
typedef bool(* | nrf_drv_usbd_feeder_t )(nrf_drv_usbd_ep_transfer_t *p_next, void *p_context, size_t ep_size) |
USBD transfer feeder. More... | |
typedef bool(* | nrf_drv_usbd_consumer_t )(nrf_drv_usbd_ep_transfer_t *p_next, void *p_context, size_t ep_size, size_t data_size) |
USBD transfer consumer. More... | |
Functions | |
ret_code_t | nrf_drv_usbd_init (nrf_drv_usbd_event_handler_t const event_handler) |
Library initialization. More... | |
ret_code_t | nrf_drv_usbd_uninit (void) |
Library deinitialization. | |
void | nrf_drv_usbd_enable (void) |
Enable the USBD port. More... | |
void | nrf_drv_usbd_disable (void) |
Disable the USBD port. More... | |
void | nrf_drv_usbd_start (bool enable_sof) |
Start USB functionality. More... | |
void | nrf_drv_usbd_stop (void) |
Stop USB functionality. More... | |
bool | nrf_drv_usbd_is_initialized (void) |
Check if driver is initialized. More... | |
bool | nrf_drv_usbd_is_enabled (void) |
Check if driver is enabled. More... | |
bool | nrf_drv_usbd_is_started (void) |
Check if driver is started. More... | |
bool | nrf_drv_usbd_suspend (void) |
Suspend USBD operation. More... | |
bool | nrf_drv_usbd_wakeup_req (void) |
Start wake up procedure. More... | |
bool | nrf_drv_usbd_suspend_check (void) |
Check if USBD is in SUSPEND mode. More... | |
void | nrf_drv_usbd_suspend_irq_config (void) |
Enable only interrupts that should be processed in SUSPEND mode. More... | |
void | nrf_drv_usbd_active_irq_config (void) |
Default active interrupt configuration. More... | |
bool | nrf_drv_usbd_bus_suspend_check (void) |
Check the bus state. More... | |
void | nrf_drv_usbd_ep_max_packet_size_set (nrf_drv_usbd_ep_t ep, uint16_t size) |
Configure packet size that should be supported by the endpoint. More... | |
uint16_t | nrf_drv_usbd_ep_max_packet_size_get (nrf_drv_usbd_ep_t ep) |
Get configured endpoint packet size. More... | |
bool | nrf_drv_usbd_ep_enable_check (nrf_drv_usbd_ep_t ep) |
Check if the selected endpoint is enabled. More... | |
void | nrf_drv_usbd_ep_enable (nrf_drv_usbd_ep_t ep) |
Enable selected endpoint. More... | |
void | nrf_drv_usbd_ep_disable (nrf_drv_usbd_ep_t ep) |
Disable selected endpoint. More... | |
void | nrf_drv_usbd_ep_default_config (void) |
Disable all endpoints except for EP0. More... | |
ret_code_t | nrf_drv_usbd_ep_transfer (nrf_drv_usbd_ep_t ep, nrf_drv_usbd_transfer_t const *const p_transfer) |
Start sending data over endpoint. More... | |
ret_code_t | nrf_drv_usbd_ep_handled_transfer (nrf_drv_usbd_ep_t ep, nrf_drv_usbd_handler_desc_t const *const p_handler) |
Start sending data over the endpoint using the transfer handler function. More... | |
void * | nrf_drv_usbd_feeder_buffer_get (void) |
Get the temporary buffer to be used by the feeder. More... | |
ret_code_t | nrf_drv_usbd_ep_status_get (nrf_drv_usbd_ep_t ep, size_t *p_size) |
Get the information about last finished or current transfer. More... | |
size_t | nrf_drv_usbd_epout_size_get (nrf_drv_usbd_ep_t ep) |
Get number of received bytes. More... | |
bool | nrf_drv_usbd_ep_is_busy (nrf_drv_usbd_ep_t ep) |
Check if endpoint buffer is ready or is under USB IP control. More... | |
void | nrf_drv_usbd_ep_stall (nrf_drv_usbd_ep_t ep) |
Stall endpoint. More... | |
void | nrf_drv_usbd_ep_stall_clear (nrf_drv_usbd_ep_t ep) |
Clear stall flag on endpoint. More... | |
bool | nrf_drv_usbd_ep_stall_check (nrf_drv_usbd_ep_t ep) |
Check if endpoint is stalled. More... | |
void | nrf_drv_usbd_ep_dtoggle_clear (nrf_drv_usbd_ep_t ep) |
Clear current endpoint data toggle. More... | |
void | nrf_drv_usbd_setup_get (nrf_drv_usbd_setup_t *const p_setup) |
Get parsed setup data. More... | |
void | nrf_drv_usbd_setup_data_clear (void) |
Clear only for data transmission on setup endpoint. More... | |
void | nrf_drv_usbd_setup_clear (void) |
Clear setup endpoint. More... | |
void | nrf_drv_usbd_setup_stall (void) |
Stall setup endpoint. More... | |
void | nrf_drv_usbd_ep_abort (nrf_drv_usbd_ep_t ep) |
nrf_drv_usbd_ep_t | nrf_drv_usbd_last_setup_dir_get (void) |
Get the information about expected transfer SETUP data direction. More... | |
void | nrf_drv_usbd_transfer_out_drop (nrf_drv_usbd_ep_t ep) |
Drop transfer on OUT endpoint. More... | |
Possible schemes of DMA scheduling | |
Definition of available configuration constants used by DMA scheduler | |
#define | NRF_DRV_USBD_DMASCHEDULER_PRIORITIZED 0 |
Highly prioritized access. More... | |
#define | NRF_DRV_USBD_DMASCHEDULER_ROUNDROBIN 1 |
Round robin scheme. More... | |
Macros for creating endpoint identifiers | |
Auxiliary macros to be used to create Endpoint identifier that is compatible with USB specification. | |
#define | NRF_DRV_USBD_EPIN(n) ((nrf_drv_usbd_ep_t)NRF_USBD_EPIN(n)) |
Create identifier for IN endpoint. More... | |
#define | NRF_DRV_USBD_EPOUT(n) ((nrf_drv_usbd_ep_t)NRF_USBD_EPOUT(n)) |
Create identifier for OUT endpoint. More... | |
nRF52840 only: USB Device APIs.
The USB Device HAL provides basic APIs for accessing the registers of the USBD. The USB Device driver provides APIs on a higher level.
#define NRF_DRV_USBD_DMASCHEDULER_PRIORITIZED 0 |
Highly prioritized access.
Endpoint with lower number has always higher priority and its data would be transfered first. OUT endpoints ale processed before IN endpoints
#define NRF_DRV_USBD_DMASCHEDULER_ROUNDROBIN 1 |
Round robin scheme.
All endpoints are processed in round-robin scheme. It means that when one endpoint is processed next in order would be the nearest with lower number. When no endpoints with lower number requires processing - then all endpoints from 0 are tested.
#define NRF_DRV_USBD_EPIN | ( | n | ) | ((nrf_drv_usbd_ep_t)NRF_USBD_EPIN(n)) |
Create identifier for IN endpoint.
Simple macro to create IN endpoint identifier for given endpoint number.
[in] | n | Endpoint number. |
#define NRF_DRV_USBD_EPOUT | ( | n | ) | ((nrf_drv_usbd_ep_t)NRF_USBD_EPOUT(n)) |
Create identifier for OUT endpoint.
Simple macro to create OUT endpoint identifier for given endpoint number.
[in] | n | Endpoint number. |
#define NRF_DRV_USBD_EPSIZE 64 |
Number of bytes in the endpoint.
Constant that informs about endpoint size
#define NRF_DRV_USBD_FEEDER_BUFFER_SIZE NRF_DRV_USBD_EPSIZE |
The size of internal feeder buffer.
#define NRF_DRV_USBD_ISOSIZE 1024 |
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.
#define NRF_DRV_USBD_TRANSFER_IN | ( | name, | |
tx_buff, | |||
tx_size | |||
) | NRF_DRV_USBD_TRANSFER_IN_FLAGS(name, tx_buff, tx_size, 0) |
Helper macro for declaring IN transfer description.
Normal transfer mode, no ZLP would be automatically generated.
name | Instance name |
tx_buff | Buffer to transfer |
tx_size | Transfer size |
#define NRF_DRV_USBD_TRANSFER_IN_FLAGS | ( | name, | |
tx_buff, | |||
tx_size, | |||
tx_flags | |||
) |
Auxiliary macro for declaring IN transfer description with flags.
The base macro for creating transfers with any configuration option.
name | Instance name. |
tx_buff | Buffer to transfer. |
tx_size | Transfer size. |
tx_flags | Flags for the transfer (see nrf_drv_usbd_transfer_flags_t). |
#define NRF_DRV_USBD_TRANSFER_IN_ZLP | ( | name, | |
tx_buff, | |||
tx_size | |||
) |
Helper macro for declaring IN transfer description.
ZLP mode - Zero Length Packet would be generated on the end of the transfer (always!).
name | Instance name |
tx_buff | Buffer to transfer |
tx_size | Transfer size |
#define NRF_DRV_USBD_TRANSFER_OUT | ( | name, | |
rx_buff, | |||
rx_size | |||
) |
Helper macro for declaring OUT transfer item (nrf_drv_usbd_transfer_t)
name | Instance name |
rx_buff | Buffer to transfer |
rx_size | Transfer size |
typedef bool(* nrf_drv_usbd_consumer_t)(nrf_drv_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 NRF_USBD_EP_OVERLOAD event is generated.
[out] | p_next | Structure with the data for the next transfer to be filled. Required only if the function returns true. |
[in,out] | p_context | Context variable configured with the transfer. |
[in] | ep_size | The endpoint size. |
[in] | data_size | Number of received bytes in the endpoint buffer. |
false | Current transfer is the last one - you do not need to call the function again. |
true | There is more data to be prepared and when current transfer finishes, the feeder function is expected to be called again. |
typedef void(* nrf_drv_usbd_event_handler_t)(nrf_drv_usbd_evt_t const *const p_event) |
USBD event callback function type.
[in] | p_event | Event information structure. |
typedef bool(* nrf_drv_usbd_feeder_t)(nrf_drv_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:
[out] | p_next | Structure with the data for the next transfer to be filled. Required only if the function returns true. |
[in,out] | p_context | Context variable configured with the transfer. |
[in] | ep_size | The endpoint size. |
false | The current transfer is the last one - you do not need to call the function again. |
true | There is more data to be prepared and when the current transfer finishes, the feeder function is expected to be called again. |
Possible endpoint error codes.
Error codes that may be returned with NRF_DRV_USBD_EVT_EPTRANSFER
enum nrf_drv_usbd_ep_t |
Endpoint identifier.
Endpoint identifier used in the driver. This endpoint number is consistent with USB 2.0 specification.
Events generated by the library.
Enumeration of possible events that may be generated by the library.
void nrf_drv_usbd_active_irq_config | ( | void | ) |
Default active interrupt configuration.
Default interrupt configuration. Use in a pair with nrf_drv_usbd_active_irq_config.
bool nrf_drv_usbd_bus_suspend_check | ( | void | ) |
Check the bus state.
This function checks if the bus state is suspended
true | USBD bus is suspended |
false | USBD bus is active |
void nrf_drv_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 library. Clock for the peripheral would be disconnected.
void nrf_drv_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 nrf_drv_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 nrf_drv_usbd_start function is called.
void nrf_drv_usbd_ep_abort | ( | nrf_drv_usbd_ep_t | ep | ) |
void nrf_drv_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.
void nrf_drv_usbd_ep_disable | ( | nrf_drv_usbd_ep_t | ep | ) |
Disable selected endpoint.
This function disables endpoint itself and its interrupts.
ep | Endpoint number to disable |
void nrf_drv_usbd_ep_dtoggle_clear | ( | nrf_drv_usbd_ep_t | ep | ) |
Clear current endpoint data toggle.
ep | Endpoint number to clear |
void nrf_drv_usbd_ep_enable | ( | nrf_drv_usbd_ep_t | ep | ) |
Enable selected endpoint.
This function enables endpoint itself and its interrupts.
ep | Endpoint number to enable |
bool nrf_drv_usbd_ep_enable_check | ( | nrf_drv_usbd_ep_t | ep | ) |
Check if the selected endpoint is enabled.
ep | Endpoint number to check. |
true | Endpoint is enabled. |
false | Endpoint is disabled. |
ret_code_t nrf_drv_usbd_ep_handled_transfer | ( | nrf_drv_usbd_ep_t | ep, |
nrf_drv_usbd_handler_desc_t const *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.
[in] | ep | Endpoint number. For an IN endpoint, sending is initiated. For an OUT endpoint, receiving is initiated. |
p_handler | Transfer handler - feeder for IN direction and consumer for OUT direction. |
NRF_ERROR_BUSY | Selected endpoint is pending. |
NRF_ERROR_INVALID_ADDR | Unexpected transfer on EPIN0 or EPOUT0. |
NRF_ERROR_FORBIDDEN | Endpoint stalled. |
NRF_SUCCESS | Transfer queued or started. |
bool nrf_drv_usbd_ep_is_busy | ( | nrf_drv_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:
uint16_t nrf_drv_usbd_ep_max_packet_size_get | ( | nrf_drv_usbd_ep_t | ep | ) |
Get configured endpoint packet size.
Function to get configured endpoint size on the buffer.
[in] | ep | Endpoint number |
void nrf_drv_usbd_ep_max_packet_size_set | ( | nrf_drv_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 library
[in] | ep | Endpoint number |
[in] | size | Required maximum packet size |
void nrf_drv_usbd_ep_stall | ( | nrf_drv_usbd_ep_t | ep | ) |
Stall endpoint.
Stall endpoit to send error information during next transfer request from the host.
ep | Endpoint number to stall |
bool nrf_drv_usbd_ep_stall_check | ( | nrf_drv_usbd_ep_t | ep | ) |
Check if endpoint is stalled.
This function gets stall state of selected endpoint
ep | Endpoint number to check |
void nrf_drv_usbd_ep_stall_clear | ( | nrf_drv_usbd_ep_t | ep | ) |
Clear stall flag on endpoint.
This function clears endpoint that is stalled.
ret_code_t nrf_drv_usbd_ep_status_get | ( | nrf_drv_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 nrf_drv_usbd_ep_transfer function or by transfer callback function.
[in] | ep | Endpoint number. |
[out] | p_size | Information about the current/last transfer size. |
NRF_SUCCESS | Transfer already finished |
NRF_ERROR_BUSY | Ongoing transfer |
NRF_ERROR_DATA_SIZE | Too much of data received that cannot fit into buffer and cannot be splited into chunks. This may happen if buffer size is not a multiplication of endpoint buffer size. |
ret_code_t nrf_drv_usbd_ep_transfer | ( | nrf_drv_usbd_ep_t | ep, |
nrf_drv_usbd_transfer_t const *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.
[in] | ep | Endpoint number. For IN endpoint sending would be initiated. For OUT endpoint receiving would be initiated. |
[in] | p_transfer |
NRF_ERROR_BUSY | Selected endpoint is pending. |
NRF_ERROR_INVALID_ADDR | Unexpected transfer on EPIN0 or EPOUT0. |
NRF_ERROR_FORBIDDEN | Endpoint stalled. |
NRF_SUCCESS | Transfer queued or started. |
size_t nrf_drv_usbd_epout_size_get | ( | nrf_drv_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.
ep | Endpoint number. |
void* nrf_drv_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 nrf_drv_usbd_ep_handled_transfer.
ret_code_t nrf_drv_usbd_init | ( | nrf_drv_usbd_event_handler_t const | event_handler | ) |
Library initialization.
[in] | event_handler | Event handler provided by the user. |
bool nrf_drv_usbd_is_enabled | ( | void | ) |
Check if driver is enabled.
false | Driver is disabled |
true | Driver is enabled |
bool nrf_drv_usbd_is_initialized | ( | void | ) |
Check if driver is initialized.
false | Driver is not initialized |
true | Driver is initialized |
bool nrf_drv_usbd_is_started | ( | void | ) |
Check if driver is started.
false | Driver is not started |
true | Driver is started (fully functional) |
nrf_drv_usbd_ep_t nrf_drv_usbd_last_setup_dir_get | ( | void | ) |
Get the information about expected transfer SETUP data direction.
Function returns the information about last expected transfer direction.
NRF_DRV_USBD_EPOUT0 | Expecting OUT (Host->Device) direction or no data |
NRF_DRV_USBD_EPIN0 | Expecting IN (Device->Host) direction |
void nrf_drv_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.
When there is any data transmission after SETUP command the data transmission itself would clear the endpoint.
void nrf_drv_usbd_setup_data_clear | ( | void | ) |
Clear only for data transmission on setup endpoint.
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 nrf_drv_usbd_setup_get | ( | nrf_drv_usbd_setup_t *const | p_setup | ) |
Get parsed setup data.
Function fills the parsed setup data structure.
[out] | p_setup | Pointer to data structure that would be filled by parsed data. |
void nrf_drv_usbd_setup_stall | ( | void | ) |
Stall setup endpoint.
Mark and error on setup endpoint.
void nrf_drv_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 library.
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.
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 nrf_drv_usbd_stop | ( | void | ) |
Stop USB functionality.
This function disables USBD pull-up and interrupts.
The HFXO request is released in this function.
bool nrf_drv_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 NRF_DRV_USBD_EVT_SUSPEND event when the firmware is ready.
After successful call of this function most of the USBD registers would be unavailable.
true | USBD peripheral successfully suspended |
false | USBD peripheral was not suspended due to resume detection. |
bool nrf_drv_usbd_suspend_check | ( | void | ) |
Check if USBD is in SUSPEND mode.
true | USBD peripheral is suspended |
false | USBD peripheral is active |
void nrf_drv_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 nrf_drv_usbd_transfer_out_drop | ( | nrf_drv_usbd_ep_t | ep | ) |
Drop transfer on OUT endpoint.
[in] | ep | OUT endpoint ID |
bool nrf_drv_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-5 ms depending on how long the SUSPEND state was kept on the USB line.
When NRF_DRV_USBD_EVT_WUREQ event is generated it means that Wake Up signaling has just been started on the USB lines.
true | WakeUp procedure started. |
false | No WakeUp procedure started - bus is already active. |