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

#include <message.h>

Public Member Functions

bool IsRead () const
 
bool IsTenBit () const
 
bool IsWriteContinuation () const
 
Address GetAddress () const
 
ByteSpan GetMutableData () const
 
ConstByteSpan GetData () const
 

Static Public Member Functions

static constexpr Message WriteMessage (Address address, ConstByteSpan data)
 
static constexpr Message WriteMessageContinuation (ConstByteSpan data)
 
static constexpr Message ReadMessage (Address address, ByteSpan data)
 

Detailed Description

A struct that represents I2C read and write messages.

Individual messages can be accumulated into a span and transmitted in one atomic i2c transaction using an Initiator implementation.

#include "pw_i2c/message.h"
constexpr auto kAddress = pw::i2c::Address::SevenBit<0x42>()
constexpr chrono::SystemClock::duration kTimeout =
std::chrono::duration_cast<chrono::SystemClock::duration>(100ms);
const std::array<std::byte, 2> tx_buffer = {std::byte{0xCD},
std::byte{0xEF}};
std::array<std::byte, 2> rx_buffer;
Vector<Message, 2> messages;
if (!tx_buffer.empty()) {
messages.push_back(Message::WriteMessage(kAddress, tx_buffer));
}
if (!rx_buffer.empty()) {
messages.push_back(Message::ReadMessage(kAddress, rx_buffer));
}
initiator.TransferFor(messages, kTimeout);
static constexpr Message ReadMessage(Address address, ByteSpan data)
Definition: message.h:106
static constexpr Message WriteMessage(Address address, ConstByteSpan data)
Definition: message.h:63

Member Function Documentation

◆ GetAddress()

Address pw::i2c::Message::GetAddress ( ) const
inline

Getter for the address component.

Returns
the Address passed into one of the constructors or factories.

◆ GetData()

ConstByteSpan pw::i2c::Message::GetData ( ) const
inline

Getter for the data component.

Returns
the data passed into one of the constructors or factories.

◆ GetMutableData()

ByteSpan pw::i2c::Message::GetMutableData ( ) const
inline

Getter for the data component.

This method is only valid for Read messages and will runtime ASSERT on other messages.

Returns
the mutable variant of the data passed into one of the constructors or factories.

◆ IsRead()

bool pw::i2c::Message::IsRead ( ) const
inline

Getter for whether this object represents a read operation.

Returns
true if the message represents a read operation.

◆ IsTenBit()

bool pw::i2c::Message::IsTenBit ( ) const
inline

Getter for whether this object represents an operation addressed with a ten-bit address. When true, communicate on the wire using the i2c 10-bit addressing protocol.

Returns
true if the message represents a 10-bit addressed operation.

◆ IsWriteContinuation()

bool pw::i2c::Message::IsWriteContinuation ( ) const
inline

Getter for whether this object represents a continued write.

Returns
true if the message represents a continued write.

◆ ReadMessage()

static constexpr Message pw::i2c::Message::ReadMessage ( Address  address,
ByteSpan  data 
)
inlinestaticconstexpr

Creates a pw::i2c::Message instance for an i2c read message.

This Message can be passed to Initiator::TransferFor().

constexpr Message kMessage = Message::ReadMessage(
pw::i2c::Address::SevenBit<0x42>(),
data
);
Definition: message.h:49
Returns
A pw::i2c::Message instance.

◆ WriteMessage()

static constexpr Message pw::i2c::Message::WriteMessage ( Address  address,
ConstByteSpan  data 
)
inlinestaticconstexpr

Creates a pw::i2c::Message instance for an i2c write message.

This Message can be passed to Initiator::TransferFor().

constexpr Message kMessage = Message::WriteMessage(
pw::i2c::Address::SevenBit<0x42>(),
data
);
Returns
A pw::i2c::Message instance.

◆ WriteMessageContinuation()

static constexpr Message pw::i2c::Message::WriteMessageContinuation ( ConstByteSpan  data)
inlinestaticconstexpr

Creates a pw::i2c::Message instance for an i2c write message without a start condition sent on the bus. Chaining one or more of these messages after a regular Write message allows the client to send non-contiguous blocks of memory as one single write message to the i2c target.

Note: This message must follow another write message.

Note: No addresses is needed and no address will be transmitted. The data should immediately follow the data from the previous write message.

This Message can be passed to Initiator::TransferFor().

constexpr Message kMessage = Message::WriteMessageContinuation(data);
static constexpr Message WriteMessageContinuation(ConstByteSpan data)
Definition: message.h:87
Returns
A pw::i2c::Message instance.

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