Pigweed
 
Loading...
Searching...
No Matches
pw::i2c::RegisterDevice Class Reference

#include <register_device.h>

Inheritance diagram for pw::i2c::RegisterDevice:
pw::i2c::Device

Public Member Functions

constexpr RegisterDevice (Initiator &initiator, Address address, endian register_address_order, endian data_order, RegisterAddressSize register_address_size)
 
constexpr RegisterDevice (Initiator &initiator, Address address, endian order, RegisterAddressSize register_address_size)
 
Status WriteRegisters (uint32_t register_address, ConstByteSpan register_data, ByteSpan buffer, chrono::SystemClock::duration timeout)
 
Status WriteRegisters8 (uint32_t register_address, span< const uint8_t > register_data, ByteSpan buffer, chrono::SystemClock::duration timeout)
 
Status WriteRegisters16 (uint32_t register_address, span< const uint16_t > register_data, ByteSpan buffer, chrono::SystemClock::duration timeout)
 
Status WriteRegisters32 (uint32_t register_address, span< const uint32_t > register_data, ByteSpan buffer, chrono::SystemClock::duration timeout)
 
Status ReadRegisters (uint32_t register_address, ByteSpan return_data, chrono::SystemClock::duration timeout)
 
Status ReadRegisters8 (uint32_t register_address, span< uint8_t > return_data, chrono::SystemClock::duration timeout)
 
Status ReadRegisters16 (uint32_t register_address, span< uint16_t > return_data, chrono::SystemClock::duration timeout)
 
Status ReadRegisters32 (uint32_t register_address, span< uint32_t > return_data, chrono::SystemClock::duration timeout)
 
Status WriteRegister (uint32_t register_address, std::byte register_data, chrono::SystemClock::duration timeout)
 
Status WriteRegister8 (uint32_t register_address, uint8_t register_data, chrono::SystemClock::duration timeout)
 
Status WriteRegister16 (uint32_t register_address, uint16_t register_data, chrono::SystemClock::duration timeout)
 
Status WriteRegister32 (uint32_t register_address, uint32_t register_data, chrono::SystemClock::duration timeout)
 
Result< std::byte > ReadRegister (uint32_t register_address, chrono::SystemClock::duration timeout)
 
Result< uint8_t > ReadRegister8 (uint32_t register_address, chrono::SystemClock::duration timeout)
 
Result< uint16_t > ReadRegister16 (uint32_t register_address, chrono::SystemClock::duration timeout)
 
Result< uint32_t > ReadRegister32 (uint32_t register_address, chrono::SystemClock::duration timeout)
 
- Public Member Functions inherited from pw::i2c::Device
constexpr Device (Initiator &initiator, Address device_address)
 
 Device (const Device &)=delete
 
 Device (Device &&)=default
 
Status TransferFor (span< const Message > messages, chrono::SystemClock::duration timeout)
 Wraps pw::i2c::Initiator::TransferFor.
 
Status WriteReadFor (ConstByteSpan tx_buffer, ByteSpan rx_buffer, chrono::SystemClock::duration timeout)
 Wraps pw::i2c::Initiator::WriteReadFor.
 
Status WriteReadFor (const void *tx_buffer, size_t tx_size_bytes, void *rx_buffer, size_t rx_size_bytes, chrono::SystemClock::duration timeout)
 
Status WriteFor (ConstByteSpan tx_buffer, chrono::SystemClock::duration timeout)
 Wraps pw::i2c::Initiator::WriteFor.
 
Status WriteFor (const void *tx_buffer, size_t tx_size_bytes, chrono::SystemClock::duration timeout)
 
Status ReadFor (ByteSpan rx_buffer, chrono::SystemClock::duration timeout)
 Wraps pw::i2c::Initiator::ReadFor.
 
Status ReadFor (void *rx_buffer, size_t rx_size_bytes, chrono::SystemClock::duration timeout)
 
Status ProbeFor (chrono::SystemClock::duration timeout)
 Wraps pw::i2c::Initiator::ProbeDeviceFor.
 

Detailed Description

The common interface for I2C register devices. Contains methods to help read and write the device's registers.

Warning
This interface assumes that you know how to consult your device's datasheet to determine correct address sizes, data sizes, endianness, etc.

Constructor & Destructor Documentation

◆ RegisterDevice() [1/2]

constexpr pw::i2c::RegisterDevice::RegisterDevice ( Initiator initiator,
Address  address,
endian  register_address_order,
endian  data_order,
RegisterAddressSize  register_address_size 
)
inlineconstexpr

This constructor specifies the endianness of the register address and data separately. If your register address and data have the same endianness and you'd like to specify them both with a single argument, see the other pw::i2c::RegisterDevice constructor.

Parameters
[in]initiatorA pw::i2c::Initiator instance for the bus that the device is on.
[in]addressThe address of the I2C device.
[in]register_address_orderThe endianness of the register address.
[in]data_orderThe endianness of the data.
[in]register_address_sizeThe size of the register address.

◆ RegisterDevice() [2/2]

constexpr pw::i2c::RegisterDevice::RegisterDevice ( Initiator initiator,
Address  address,
endian  order,
RegisterAddressSize  register_address_size 
)
inlineconstexpr

This constructor specifies the endianness of the register address and data with a single argument. If your register address and data have different endianness, use the other pw::i2c::RegisterDevice constructor.

Parameters
[in]initiatorA pw::i2c::Initiator instance for the bus that the device is on.
[in]addressThe address of the I2C device.
[in]orderThe endianness of both the register address and register data.
[in]register_address_sizeThe size of the register address.

Member Function Documentation

◆ ReadRegister()

Result< std::byte > pw::i2c::RegisterDevice::ReadRegister ( uint32_t  register_address,
chrono::SystemClock::duration  timeout 
)
inline

Sends a register address to read from and then reads from that address.

register_address and the return data use the endianness that was provided when this pw::i2c::RegisterDevice instance was constructed.

Precondition
This method assumes that you've verified that the return data size is a correct size for your device.
Parameters
[in]register_addressThe register address to read.
[in]timeoutThe maximum duration to block waiting for both exclusive bus access and the completion of the I2C transaction.
Returns
embed:rst:leading-asterisk
 
* 
*  .. pw-status-codes::
* 
*     OK: Returns the register data.
* 
*     DEADLINE_EXCEEDED: Unable to acquire exclusive bus access and
*     complete the transaction in time.
* 
*     FAILED_PRECONDITION: The interface is not initialized or enabled.
* 
*     INTERNAL: An issue occurred while building the return data.
* 
*     INVALID_ARGUMENT: ``register_address`` is larger than the 10-bit
*     address space.
* 
*     UNAVAILABLE: The device took too long to respond to the NACK.
* 
*  

◆ ReadRegister16()

Result< uint16_t > pw::i2c::RegisterDevice::ReadRegister16 ( uint32_t  register_address,
chrono::SystemClock::duration  timeout 
)
inline

Variant of pw::i2c::RegisterDevice::ReadRegister() that returns exactly 16 bits.

◆ ReadRegister32()

Result< uint32_t > pw::i2c::RegisterDevice::ReadRegister32 ( uint32_t  register_address,
chrono::SystemClock::duration  timeout 
)
inline

Variant of pw::i2c::RegisterDevice::ReadRegister() that returns exactly 32 bits.

◆ ReadRegister8()

Result< uint8_t > pw::i2c::RegisterDevice::ReadRegister8 ( uint32_t  register_address,
chrono::SystemClock::duration  timeout 
)
inline

Variant of pw::i2c::RegisterDevice::ReadRegister() that returns exactly 8 bits.

◆ ReadRegisters()

Status pw::i2c::RegisterDevice::ReadRegisters ( uint32_t  register_address,
ByteSpan  return_data,
chrono::SystemClock::duration  timeout 
)

Reads data from multiple contiguous registers starting from a specific offset or register. This method is byte-addressable.

register_address and return_data use the endianness that was provided when this pw::i2c::RegisterDevice instance was constructed.

Precondition
This method assumes that you've verified that your device supports bulk reads and that return_data is a correct size for your device.
Parameters
[in]register_addressThe register address to begin reading at.
[out]return_dataThe area to read the data into. The amount of data that will be read is equal to the size of this span. Endianness is taken into account if this span is 2 or 4 bytes.
[in]timeoutThe maximum duration to block waiting for both exclusive bus access and the completion of the I2C transaction.
Returns
embed:rst:leading-asterisk
 
* 
*  .. pw-status-codes::
* 
*     OK: The bulk read was successful.
* 
*     DEADLINE_EXCEEDED: Unable to acquire exclusive bus access and
*     complete the transaction in time.
* 
*     FAILED_PRECONDITION: The interface is not initialized or enabled.
* 
*     INTERNAL: An issue occurred while building ``return_data``.
* 
*     INVALID_ARGUMENT: ``register_address`` is larger than the 10-bit
*     address space.
* 
*     UNAVAILABLE: The device took too long to respond to the NACK.
* 
*  

◆ ReadRegisters16()

Status pw::i2c::RegisterDevice::ReadRegisters16 ( uint32_t  register_address,
span< uint16_t >  return_data,
chrono::SystemClock::duration  timeout 
)
inline

Variant of pw::i2c::RegisterDevice::ReadRegisters() that requires return_data to be exactly 16 bits.

◆ ReadRegisters32()

Status pw::i2c::RegisterDevice::ReadRegisters32 ( uint32_t  register_address,
span< uint32_t >  return_data,
chrono::SystemClock::duration  timeout 
)
inline

Variant of pw::i2c::RegisterDevice::ReadRegisters() that requires return_data to be exactly 32 bits.

◆ ReadRegisters8()

Status pw::i2c::RegisterDevice::ReadRegisters8 ( uint32_t  register_address,
span< uint8_t >  return_data,
chrono::SystemClock::duration  timeout 
)
inline

Variant of pw::i2c::RegisterDevice::ReadRegisters() that requires return_data to be exactly 8 bits.

◆ WriteRegister()

Status pw::i2c::RegisterDevice::WriteRegister ( uint32_t  register_address,
std::byte  register_data,
chrono::SystemClock::duration  timeout 
)
inline

Sends a register address to write to and then writes to that address.

register_address and register_data use the endianness that was provided when this pw::i2c::RegisterDevice instance was constructed.

Precondition
This method assumes that you've verified that register_data is a correct size for your device.
Parameters
[in]register_addressThe register address to write to.
[in]register_dataThe data that should be written at the address. The maximum allowed size is 4 bytes.
[in]timeoutThe maximum duration to block waiting for both exclusive bus access and the completion of the I2C transaction.
Returns
embed:rst:leading-asterisk
 
* 
*  .. pw-status-codes::
* 
*     OK: The write was successful.
* 
*     DEADLINE_EXCEEDED: Unable to acquire exclusive bus access
*     and complete the transaction in time.
* 
*     FAILED_PRECONDITION: The interface is not initialized or enabled.
* 
*     INTERNAL: An issue occurred while writing the data.
* 
*     INVALID_ARGUMENT: ``register_address`` is larger than the 10-bit
*     address space.
* 
*     UNAVAILABLE: The device took too long to respond to the NACK.
* 
*  

◆ WriteRegister16()

Status pw::i2c::RegisterDevice::WriteRegister16 ( uint32_t  register_address,
uint16_t  register_data,
chrono::SystemClock::duration  timeout 
)
inline

Variant of pw::i2c::RegisterDevice::WriteRegister() that writes exactly 16 bits.

◆ WriteRegister32()

Status pw::i2c::RegisterDevice::WriteRegister32 ( uint32_t  register_address,
uint32_t  register_data,
chrono::SystemClock::duration  timeout 
)
inline

Variant of pw::i2c::RegisterDevice::WriteRegister() that writes exactly 32 bits.

◆ WriteRegister8()

Status pw::i2c::RegisterDevice::WriteRegister8 ( uint32_t  register_address,
uint8_t  register_data,
chrono::SystemClock::duration  timeout 
)
inline

Variant of pw::i2c::RegisterDevice::WriteRegister() that writes exactly 8 bits.

◆ WriteRegisters()

Status pw::i2c::RegisterDevice::WriteRegisters ( uint32_t  register_address,
ConstByteSpan  register_data,
ByteSpan  buffer,
chrono::SystemClock::duration  timeout 
)
inline

Writes data to multiple contiguous registers starting at a specific register. This method is byte-addressable.

register_address and register_data use the endianness that was provided when this pw::i2c::RegisterDevice instance was constructed.

Precondition
This method assumes that you've verified that your device supports bulk writes and that register_data is a correct size for your device.
Parameters
[in]register_addressThe register address to begin writing at.
[in]register_dataThe data to write. Endianness is taken into account if the data is 2 or 4 bytes.
[in]bufferA buffer for constructing the write data. The size of this buffer must be at least as large as the size of register_address plus the size of register_data.
[in]timeoutThe maximum duration to block waiting for both exclusive bus access and the completion of the I2C transaction.
Returns
embed:rst:leading-asterisk
 
* 
*  .. pw-status-codes::
* 
*     OK: The bulk write was successful.
* 
*     DEADLINE_EXCEEDED: Unable to acquire exclusive bus access and
*     complete the transaction in time.
* 
*     FAILED_PRECONDITION: The interface is not initialized or enabled.
* 
*     INTERNAL: An issue occurred while building ``register_data``.
* 
*     INVALID_ARGUMENT: ``register_address`` is larger than the 10-bit
*     address space.
* 
*     OUT_OF_RANGE: The size of ``buffer`` is less than the size
*     of ``register_address`` plus the size of ``register_data``.
* 
*     UNAVAILABLE: The device took too long to respond to the NACK.
* 
*  

◆ WriteRegisters16()

Status pw::i2c::RegisterDevice::WriteRegisters16 ( uint32_t  register_address,
span< const uint16_t >  register_data,
ByteSpan  buffer,
chrono::SystemClock::duration  timeout 
)
inline

Variant of pw::i2c::RegisterDevice::WriteRegisters() that requires register_data to be exactly 16 bits.

◆ WriteRegisters32()

Status pw::i2c::RegisterDevice::WriteRegisters32 ( uint32_t  register_address,
span< const uint32_t >  register_data,
ByteSpan  buffer,
chrono::SystemClock::duration  timeout 
)
inline

Variant of pw::i2c::RegisterDevice::WriteRegisters() that requires register_data to be exactly 32 bits.

◆ WriteRegisters8()

Status pw::i2c::RegisterDevice::WriteRegisters8 ( uint32_t  register_address,
span< const uint8_t >  register_data,
ByteSpan  buffer,
chrono::SystemClock::duration  timeout 
)
inline

Variant of pw::i2c::RegisterDevice::WriteRegisters() that requires register_data to be exactly 8 bits.


The documentation for this class was generated from the following file: