TWI Sensor module.

nRF5 SDK v15.0.0

Data Structures

struct nrf_twi_sensor_t
Structure holding sensor instance. More...
struct nrf_twi_sensor_read_cmd_t
Struct describing sensor read command. More...
struct nrf_twi_sensor_write_cmd_t
Struct describing sensor write command. More...
union nrf_twi_sensor_cmd_t
Union for sensor commands. Needed in buffer definition. More...

Macros

#define NRF_TWI_SENSOR_SEND_BUF_SIZE 16
Internal write operation buffer length. More...
#define NRF_TWI_SENSOR_DEF (twi_sensor_name, p_nrf_twi_mngr, msg_buff_size)
Macro creating common twi sensor instance. More...
#define NRF_TWI_SENSOR_READ (p_reg_addr, p_buffer, byte_cnt)
Macro for defining TWI manager read transfer. More...
#define NRF_TWI_SENSOR_WRITE (p_buffer, byte_cnt) NRF_TWI_MNGR_WRITE (0x00, p_buffer, byte_cnt, 0)
Macro for defining TWI manager write transfer. More...
#define NRF_TWI_SENSOR_ADDRESS_SET (_transfers, _sensor_addr)
Macro for assigning sensor address to transfers. More...
#define NRF_TWI_SENSOR_REG_SET (_register, _msk, _pos, _val)
Macro for setting parameters in sensor register. More...
#define NRF_TWI_SENSOR_REG_VAL_GET (_register, _msk, _pos)   (((_register) & (_msk)) >> (_pos))
Macro for getting parameters from sensor register. More...

Typedefs

typedef void(* nrf_twi_sensor_reg_cb_t )( ret_code_t result, void *p_register_data)
Register read callback prototype. More...

Functions

ret_code_t nrf_twi_sensor_init ( nrf_twi_sensor_t *p_twi_sensor)
Function for initialization of sensor common instance. More...
ret_code_t nrf_twi_sensor_reg_read ( nrf_twi_sensor_t const *p_instance, uint8_t sensor_addr, uint8_t reg_address, nrf_twi_sensor_reg_cb_t user_cb, uint8_t *p_data, uint8_t length)
Function for reading sensor register. More...
ret_code_t nrf_twi_sensor_write ( nrf_twi_sensor_t const *p_instance, uint8_t sensor_addr, uint8_t const *p_data, uint8_t length, bool copy_flag)
Function for writing to sensor. More...
ret_code_t nrf_twi_sensor_reg_write ( nrf_twi_sensor_t const *p_instance, uint8_t sensor_addr, uint8_t reg_address, uint8_t *p_data, uint8_t length)
Function for writing to sensor register. More...
__STATIC_INLINE uint8_t nrf_twi_sensor_max_util_get ( nrf_twi_sensor_t const *p_twi_sensor)
Function for getting maximum utilization of sensor buffer. More...

Detailed Description

Macro Definition Documentation

#define NRF_TWI_SENSOR_ADDRESS_SET ( _transfers,
_sensor_addr
)
Value:
for (uint8_t i = 0; i < ARRAY_SIZE(_transfers); i++) \
{ \
if (i % 2 == 0) \
{ \
transfers[i].operation = NRF_TWI_MNGR_WRITE_OP (_sensor_addr); \
} \
else \
{ \
transfers[i].operation = NRF_TWI_MNGR_READ_OP (_sensor_addr); \
} \
}

Macro for assigning sensor address to transfers.

Parameters
[in] _transfers Transfers array.
[in] _sensor_addr Desired sensor address.
Note
For internal use only.
#define NRF_TWI_SENSOR_DEF ( twi_sensor_name,
p_nrf_twi_mngr,
msg_buff_size
)
Value:
NRF_BALLOC_DEF ( CONCAT_2 (twi_sensor_name,_pool), sizeof ( nrf_twi_sensor_cmd_t ), msg_buff_size);\
static nrf_twi_sensor_t twi_sensor_name = \
{ \
.p_twi_mngr = p_nrf_twi_mngr, \
.p_pool = & CONCAT_2 (twi_sensor_name,_pool) \
}

Macro creating common twi sensor instance.

Data in structure is used for basic communication with sensors. THere should be one instance per TWI bus.

Parameters
[in] twi_sensor_name TWI common sensor instance name.
[in] p_nrf_twi_mngr Pointer to TWI Manager instance. NRF_TWI_MNGR_DEF
[in] msg_buff_size Size of buffer used in communication
Note
Buffer size should be less or equal to TWI manager queue size. Minimum buffer size can be found after checking utilization of sensor buffer.
#define NRF_TWI_SENSOR_READ ( p_reg_addr,
p_buffer,
byte_cnt
)
Value:
NRF_TWI_MNGR_READ (0x00, p_buffer, byte_cnt, 0)

Macro for defining TWI manager read transfer.

Note
For internal use only.
#define NRF_TWI_SENSOR_REG_SET ( _register,
_msk,
_pos,
_val
)
Value:
_register &= ~(_msk); \
_register |= ((_msk) & ((_val) << (_pos)))

Macro for setting parameters in sensor register.

Parameters
[in,out] _register Register to be altered.
[in] _msk Parameter mask.
[in] _pos Parameter position.
[in] _val Parameter value to be set.
#define NRF_TWI_SENSOR_REG_VAL_GET ( _register,
_msk,
_pos
) (((_register) & (_msk)) >> (_pos))

Macro for getting parameters from sensor register.

Parameters
[in] _register Register to be processed.
[in] _msk Parameter mask.
[in] _pos Parameter position.
Note
For usage with registers read using nrf_twi_sensor_register_read function.
Returns
Parameter value
#define NRF_TWI_SENSOR_SEND_BUF_SIZE   16

Internal write operation buffer length.

Defines how many bytes can be stored internally. 16 bytes were selected so that nrf_twi_sensor_write_cmd_t size matches nrf_twi_sensor_read_cmd_t size.

#define NRF_TWI_SENSOR_WRITE ( p_buffer,
byte_cnt
) NRF_TWI_MNGR_WRITE (0x00, p_buffer, byte_cnt, 0)

Macro for defining TWI manager write transfer.

Note
For internal use only.

Typedef Documentation

typedef void(* nrf_twi_sensor_reg_cb_t)( ret_code_t result, void *p_register_data)

Register read callback prototype.

Parameters
[in] result Return error code from TWI manager and underlying drivers.
[in] p_register_data Pointer to register value.

Function Documentation

ret_code_t nrf_twi_sensor_init ( nrf_twi_sensor_t * p_twi_sensor )

Function for initialization of sensor common instance.

Note
TWI Manager should be initialized before nrf_twi_mngr_init
Parameters
[in] p_twi_sensor Pointer to sensor common instance.
Returns
Error code from nrf_balloc nrf_balloc_init
__STATIC_INLINE uint8_t nrf_twi_sensor_max_util_get ( nrf_twi_sensor_t const * p_twi_sensor )

Function for getting maximum utilization of sensor buffer.

Parameters
[in] p_twi_sensor Pointer to sensor buffer.
Returns
Maximum utilization.
ret_code_t nrf_twi_sensor_reg_read ( nrf_twi_sensor_t const * p_instance ,
uint8_t sensor_addr ,
uint8_t reg_address ,
nrf_twi_sensor_reg_cb_t user_cb ,
uint8_t * p_data ,
uint8_t length
)

Function for reading sensor register.

Parameters
[in] p_instance Pointer to sensor instance.
[in] sensor_addr Sensor address.
[in] reg_address Register address.
[in] user_cb User callback.
[out] p_data Pointer to data save location.
[in] length Number of bytes to read.
Return values
NRF_ERROR_NO_MEM If there is no memory in sensor buffer
NRF_SUCCESS If the operation was successful.
other Error code from TWI manager nrf_twi_mngr_schedule .
ret_code_t nrf_twi_sensor_reg_write ( nrf_twi_sensor_t const * p_instance ,
uint8_t sensor_addr ,
uint8_t reg_address ,
uint8_t * p_data ,
uint8_t length
)

Function for writing to sensor register.

Parameters
[in] p_instance Pointer to sensor instance.
[in] sensor_addr Sensor address.
[in] reg_address Register address.
[in] p_data Pointer to data to be written.
[in] length Number of bytes to write.
Note
Data is copied into internal buffer. Length has to be less than NRF_TWI_SENSOR_SEND_BUF_SIZE.
Return values
NRF_ERROR_NO_MEM If there is no memory in sensor buffer
NRF_ERROR_INVALID_LENGTH If trying to copy more bytes than NRF_TWI_SENSOR_SEND_BUF_SIZE - 1. One byte reserved for register address.
NRF_SUCCESS If the operation was successful.
other Error code from TWI manager nrf_twi_mngr_schedule .
ret_code_t nrf_twi_sensor_write ( nrf_twi_sensor_t const * p_instance ,
uint8_t sensor_addr ,
uint8_t const * p_data ,
uint8_t length ,
bool copy_flag
)

Function for writing to sensor.

Parameters
[in] p_instance Pointer to sensor instance.
[in] sensor_addr Sensor address.
[in] p_data Pointer to data to be written.
[in] length Number of bytes to write.
[in] copy_flag If true, p_data is copied into internal static buffer.
Note
Most of the time, to write to sensors register, first byte in p_data has to be register address.
Return values
NRF_ERROR_NO_MEM If there is no memory in sensor buffer
NRF_ERROR_INVALID_LENGTH If trying to copy more bytes than NRF_TWI_SENSOR_SEND_BUF_SIZE.
NRF_SUCCESS If the operation was successful.
other Error code from TWI manager nrf_twi_mngr_schedule .