nrfx 4.5.0
Loading...
Searching...
No Matches
TWI driver

Two Wire Interface master (TWI) peripheral driver. More...

Topics

 TWI peripheral driver configuration
 

Data Structures

struct  nrfx_twi_xfer_desc_t
 Structure for a TWI transfer descriptor. More...
 
struct  nrfx_twi_event_t
 Structure for a TWI event. More...
 
struct  nrfx_twi_t
 Structure for the TWI master driver instance. More...
 
struct  nrfx_twi_config_t
 Structure for the configuration of the TWI master driver instance. More...
 

Macros

#define NRFX_TWI_INSTANCE(reg)
 Macro for creating a TWI master driver instance.
 
#define NRFX_TWI_DEFAULT_CONFIG(_pin_scl, _pin_sda)
 TWI master driver instance default configuration.
 
#define NRFX_TWI_FLAG_NO_XFER_EVT_HANDLER   (1UL << 2)
 Flag indicating that the interrupt after each transfer will be suppressed, and the event handler will not be called.
 
#define NRFX_TWI_FLAG_TX_NO_STOP   (1UL << 5)
 Flag indicating that the TX transfer will not end with a stop condition.
 
#define NRFX_TWI_FLAG_SUSPEND   (1UL << 6)
 Flag indicating that the transfer will be suspended.
 
#define NRFX_TWI_XFER_DESC_TX(addr, p_data, length)
 Macro for setting the TX transfer descriptor.
 
#define NRFX_TWI_XFER_DESC_RX(addr, p_data, length)
 Macro for setting the RX transfer descriptor.
 
#define NRFX_TWI_XFER_DESC_TXRX(addr, p_tx, tx_len, p_rx, rx_len)
 Macro for setting the TX-RX transfer descriptor.
 
#define NRFX_TWI_XFER_DESC_TXTX(addr, p_tx, tx_len, p_tx2, tx_len2)
 Macro for setting the TX-TX transfer descriptor.
 
#define NRFX_TWI_INST_HANDLER_GET(idx)
 Macro returning TWI interrupt handler.
 

Typedefs

typedef void(* nrfx_twi_event_handler_t) (nrfx_twi_event_t const *p_event, void *p_context)
 TWI event handler prototype.
 

Enumerations

enum  nrfx_twi_xfer_type_t {
  NRFX_TWI_XFER_TX ,
  NRFX_TWI_XFER_RX ,
  NRFX_TWI_XFER_TXRX ,
  NRFX_TWI_XFER_TXTX
}
 TWI master driver transfer types. More...
 
enum  nrfx_twi_event_type_t {
  NRFX_TWI_EVT_DONE ,
  NRFX_TWI_EVT_ADDRESS_NACK ,
  NRFX_TWI_EVT_DATA_NACK ,
  NRFX_TWI_EVT_OVERRUN ,
  NRFX_TWI_EVT_BUS_ERROR
}
 TWI master driver event types. More...
 

Functions

int nrfx_twi_init (nrfx_twi_t *p_instance, nrfx_twi_config_t const *p_config, nrfx_twi_event_handler_t event_handler, void *p_context)
 Function for initializing the TWI driver instance.
 
int nrfx_twi_reconfigure (nrfx_twi_t *p_instance, nrfx_twi_config_t const *p_config)
 Function for reconfiguring the TWI instance.
 
void nrfx_twi_uninit (nrfx_twi_t *p_instance)
 Function for uninitializing the TWI instance.
 
void nrfx_twi_enable (nrfx_twi_t *p_instance)
 Function for enabling the TWI instance.
 
void nrfx_twi_disable (nrfx_twi_t *p_instance)
 Function for disabling the TWI instance.
 
int nrfx_twi_xfer (nrfx_twi_t *p_instance, nrfx_twi_xfer_desc_t const *p_xfer_desc, uint32_t flags)
 Function for performing a TWI transfer.
 
void nrfx_twi_irq_handler (nrfx_twi_t *p_instance)
 Driver interrupt handler.
 
bool nrfx_twi_init_check (nrfx_twi_t const *p_instance)
 Function for checking if the TWI driver instance is initialized.
 
bool nrfx_twi_is_busy (nrfx_twi_t const *p_instance)
 Function for checking the TWI driver state.
 
size_t nrfx_twi_data_count_get (nrfx_twi_t const *p_instance)
 Function for getting the transferred data count.
 
uint32_t nrfx_twi_stopped_event_get (nrfx_twi_t const *p_instance)
 Function for returning the address of a STOPPED TWI event.
 
NRFX_STATIC_INLINE int nrfx_twi_bus_recover (uint32_t scl_pin, uint32_t sda_pin)
 Function for recovering the bus.
 

Detailed Description

Two Wire Interface master (TWI) peripheral driver.

Macro Definition Documentation

◆ NRFX_TWI_DEFAULT_CONFIG

#define NRFX_TWI_DEFAULT_CONFIG ( _pin_scl,
_pin_sda )
Value:
{ \
.scl = _pin_scl, \
.sda = _pin_sda, \
.frequency = NRF_TWI_FREQ_100K, \
.interrupt_priority = NRFX_TWI_DEFAULT_CONFIG_IRQ_PRIORITY, \
.hold_bus_uninit = false, \
}
@ NRF_TWI_FREQ_100K
100 kbps.
Definition nrf_twi.h:119
#define NRFX_TWI_DEFAULT_CONFIG_IRQ_PRIORITY
Interrupt priority.
Definition nrfx_twi_dox_config.h:30

TWI master driver instance default configuration.

This configuration sets up TWI with the following options:

  • clock frequency: 100 kHz
  • disable bus holding after uninit
Parameters
[in]_pin_sclSCL pin.
[in]_pin_sdaSDA pin.

◆ NRFX_TWI_INST_HANDLER_GET

#define NRFX_TWI_INST_HANDLER_GET ( idx)
Value:
NRFX_CONCAT_3(nrfx_twi_, idx, _irq_handler)
#define NRFX_CONCAT_3(p1, p2, p3)
Macro for concatenating three tokens in macro expansion.
Definition nrfx_utils.h:164

Macro returning TWI interrupt handler.

param[in] idx TWI index.

Returns
Interrupt handler.

◆ NRFX_TWI_INSTANCE

#define NRFX_TWI_INSTANCE ( reg)
Value:
{ \
.p_reg = (NRF_TWI_Type *)reg, \
.cb = {0}, \
}

Macro for creating a TWI master driver instance.

◆ NRFX_TWI_XFER_DESC_RX

#define NRFX_TWI_XFER_DESC_RX ( addr,
p_data,
length )
Value:
{ \
.type = NRFX_TWI_XFER_RX, \
.address = (addr), \
.primary_length = (length), \
.secondary_length = 0, \
.p_primary_buf = (p_data), \
.p_secondary_buf = NULL, \
}
@ NRFX_TWI_XFER_RX
RX transfer.
Definition nrfx_twi.h:56

Macro for setting the RX transfer descriptor.

◆ NRFX_TWI_XFER_DESC_TX

#define NRFX_TWI_XFER_DESC_TX ( addr,
p_data,
length )
Value:
{ \
.type = NRFX_TWI_XFER_TX, \
.address = (addr), \
.primary_length = (length), \
.secondary_length = 0, \
.p_primary_buf = (p_data), \
.p_secondary_buf = NULL, \
}
@ NRFX_TWI_XFER_TX
TX transfer.
Definition nrfx_twi.h:55

Macro for setting the TX transfer descriptor.

◆ NRFX_TWI_XFER_DESC_TXRX

#define NRFX_TWI_XFER_DESC_TXRX ( addr,
p_tx,
tx_len,
p_rx,
rx_len )
Value:
{ \
.type = NRFX_TWI_XFER_TXRX, \
.address = (addr), \
.primary_length = (tx_len), \
.secondary_length = (rx_len), \
.p_primary_buf = (p_tx), \
.p_secondary_buf = (p_rx), \
}
@ NRFX_TWI_XFER_TXRX
TX transfer followed by RX transfer with repeated start.
Definition nrfx_twi.h:57

Macro for setting the TX-RX transfer descriptor.

◆ NRFX_TWI_XFER_DESC_TXTX

#define NRFX_TWI_XFER_DESC_TXTX ( addr,
p_tx,
tx_len,
p_tx2,
tx_len2 )
Value:
{ \
.type = NRFX_TWI_XFER_TXTX, \
.address = (addr), \
.primary_length = (tx_len), \
.secondary_length = (tx_len2), \
.p_primary_buf = (p_tx), \
.p_secondary_buf = (p_tx2), \
}
@ NRFX_TWI_XFER_TXTX
TX transfer followed by TX transfer with repeated start.
Definition nrfx_twi.h:58

Macro for setting the TX-TX transfer descriptor.

Enumeration Type Documentation

◆ nrfx_twi_event_type_t

TWI master driver event types.

Enumerator
NRFX_TWI_EVT_DONE 

Transfer completed event.

NRFX_TWI_EVT_ADDRESS_NACK 

Error event: NACK received after sending the address.

NRFX_TWI_EVT_DATA_NACK 

Error event: NACK received after sending a data byte.

NRFX_TWI_EVT_OVERRUN 

Error event: The unread data is replaced by new data.

NRFX_TWI_EVT_BUS_ERROR 

Error event: An unexpected transition occurred on the bus.

◆ nrfx_twi_xfer_type_t

TWI master driver transfer types.

Enumerator
NRFX_TWI_XFER_TX 

TX transfer.

NRFX_TWI_XFER_RX 

RX transfer.

NRFX_TWI_XFER_TXRX 

TX transfer followed by RX transfer with repeated start.

NRFX_TWI_XFER_TXTX 

TX transfer followed by TX transfer with repeated start.

Function Documentation

◆ nrfx_twi_bus_recover()

NRFX_STATIC_INLINE int nrfx_twi_bus_recover ( uint32_t scl_pin,
uint32_t sda_pin )

Function for recovering the bus.

This function checks if the bus is not stuck because of a slave holding the SDA line in the low state, and if needed it performs required number of pulses on the SCL line to make the slave release the SDA line. Finally, the function generates a STOP condition on the bus to put it into a known state.

Note
This function can be used only if the TWI driver is uninitialized.
Parameters
[in]scl_pinSCL pin number.
[in]sda_pinSDA pin number.
Return values
0Bus recovery was successful.
-ECANCELEDBus recovery failed.

◆ nrfx_twi_data_count_get()

size_t nrfx_twi_data_count_get ( nrfx_twi_t const * p_instance)

Function for getting the transferred data count.

Parameters
[in]p_instancePointer to the driver instance structure.
Returns
Data count.

◆ nrfx_twi_disable()

void nrfx_twi_disable ( nrfx_twi_t * p_instance)

Function for disabling the TWI instance.

Parameters
[in]p_instancePointer to the driver instance structure.

◆ nrfx_twi_enable()

void nrfx_twi_enable ( nrfx_twi_t * p_instance)

Function for enabling the TWI instance.

Parameters
[in]p_instancePointer to the driver instance structure.

◆ nrfx_twi_init()

int nrfx_twi_init ( nrfx_twi_t * p_instance,
nrfx_twi_config_t const * p_config,
nrfx_twi_event_handler_t event_handler,
void * p_context )

Function for initializing the TWI driver instance.

Parameters
[in]p_instancePointer to the driver instance structure.
[in]p_configPointer to the structure with the initial configuration.
[in]event_handlerEvent handler provided by the user. If NULL, blocking mode is enabled.
[in]p_contextContext passed to event handler.
Return values
0Initialization is successful.
-EALREADYThe driver is already initialized.
-EBUSYSome other peripheral with the same instance ID is already in use. This is possible only if Peripheral Resource Sharing (PRS) module is enabled.

◆ nrfx_twi_init_check()

bool nrfx_twi_init_check ( nrfx_twi_t const * p_instance)

Function for checking if the TWI driver instance is initialized.

Parameters
[in]p_instancePointer to the driver instance structure.
Return values
trueInstance is already initialized.
falseInstance is not initialized.

◆ nrfx_twi_irq_handler()

void nrfx_twi_irq_handler ( nrfx_twi_t * p_instance)

Driver interrupt handler.

Parameters
[in]p_instancePointer to the driver instance structure.

◆ nrfx_twi_is_busy()

bool nrfx_twi_is_busy ( nrfx_twi_t const * p_instance)

Function for checking the TWI driver state.

Parameters
[in]p_instanceTWI instance.
Return values
trueThe TWI driver is currently busy performing a transfer.
falseThe TWI driver is ready for a new transfer.

◆ nrfx_twi_reconfigure()

int nrfx_twi_reconfigure ( nrfx_twi_t * p_instance,
nrfx_twi_config_t const * p_config )

Function for reconfiguring the TWI instance.

Parameters
[in]p_instancePointer to the driver instance structure.
[in]p_configPointer to the structure with the configuration.
Return values
0Reconfiguration was successful.
-EBUSYThe driver is during transaction.
-EINPROGRESSThe driver is uninitialized.

◆ nrfx_twi_stopped_event_get()

uint32_t nrfx_twi_stopped_event_get ( nrfx_twi_t const * p_instance)

Function for returning the address of a STOPPED TWI event.

A STOPPED event can be used to detect the end of a transfer if the NRFX_TWI_FLAG_NO_XFER_EVT_HANDLER option is used.

Parameters
[in]p_instancePointer to the driver instance structure.
Returns
STOPPED event address.

◆ nrfx_twi_uninit()

void nrfx_twi_uninit ( nrfx_twi_t * p_instance)

Function for uninitializing the TWI instance.

Parameters
[in]p_instancePointer to the driver instance structure.

◆ nrfx_twi_xfer()

int nrfx_twi_xfer ( nrfx_twi_t * p_instance,
nrfx_twi_xfer_desc_t const * p_xfer_desc,
uint32_t flags )

Function for performing a TWI transfer.

The following transfer types can be configured (nrfx_twi_xfer_desc_t::type):

Note
TX-RX and TX-TX transfers are supported only in non-blocking mode.

Additional options are provided using the flags parameter:

  • NRFX_TWI_FLAG_NO_XFER_EVT_HANDLER - No user event handler after transfer completion. In most cases, this also means no interrupt at the end of the transfer.
  • NRFX_TWI_FLAG_TX_NO_STOP - No stop condition after TX transfer.
  • NRFX_TWI_FLAG_SUSPEND - Transfer will be suspended. This allows for combining multiple transfers into one transaction. Only transactions with the same direction can be combined. To finish the transaction, call the function without this flag.
Note
Some flag combinations are invalid:
Parameters
[in]p_instancePointer to the driver instance structure.
[in]p_xfer_descPointer to the transfer descriptor.
[in]flagsTransfer options (0 for default settings).
Return values
0The procedure is successful.
-EBUSYThe driver is not ready for a new transfer.
-ECANCELEDAn unexpected transition occurred on the bus.
-EINPROGRESSOther direction of transaction is suspended on the bus.
-EOVERFLOWThe unread data is replaced by new data (TXRX and RX)
-EIONegative acknowledgement (NACK) is received after sending the address in polling mode.
-EAGAINNegative acknowledgement (NACK) is received after sending a data byte in polling mode.