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

#include <address.h>

Public Member Functions

 Address (uint16_t address)
 
uint8_t GetSevenBit () const
 
uint16_t GetTenBit () const
 
uint16_t GetAddress () const
 
constexpr bool IsTenBit () const
 

Static Public Member Functions

template<uint16_t kAddress>
static constexpr Address TenBit ()
 
static Address TenBit (uint16_t address)
 
template<uint8_t kAddress>
static constexpr Address SevenBit ()
 
static Address SevenBit (uint16_t address)
 

Static Public Attributes

static constexpr uint8_t kMaxSevenBitAddress = (1 << 7) - 1
 
static constexpr uint16_t kMaxTenBitAddress = (1 << 10) - 1
 

Friends

bool operator== (const Address &a1, const Address &a2)
 

Detailed Description

A helper class that represents I2C addresses.

An address instance remembers whether it was constructed as a seven-bit or ten-bit address. This attribute can be used by Initiators to determine the i2c addressing style to transmit.

Note: Per the above, a ten-bit constructed instance may still have an an address of seven or fewer bits.

#include "pw_i2c/address.h"
constexpr pw::i2c::Address kAddress1 = pw::i2c::Address::SevenBit<0x42>();
uint8_t raw_address_1 = kAddress1.GetSevenBit();
const pw::i2c::Address kAddress2<0x200>(); // 10-bit
uint16_t raw_address_2 = kAddress2.GetAddress();
// Note: kAddress2.GetSevenBit() would fail an assertion here.
Definition: address.h:39
uint8_t GetSevenBit() const

Constructor & Destructor Documentation

◆ Address()

pw::i2c::Address::Address ( uint16_t  address)
explicit

Creates a pw::i2c::Address instance from a 7 or 10 bit address.

Note
This function is deprecated. You should almost certainly use either Address::SevenBit<0x1>() for addresses known at compile time, or Address::SevenBit(0x1) for addresses known at run-time.

If the address argument is 7-bits or less, a 7-bit address is constructed equivalent to Address::SevenBit(address);

If the address argument is 8, 9 or 10 bits, a ten-bit address is constructed equivalent to Address::TenBit(address);

The type of address construced will affect how the i2c address is transmitted on the bus. You should always use 7-bit addresses unless you are certain you have a host and device that support 10-bit addresses.

Parameters
[in]addressAn address no larger than 10-bits as an unsigned integer. This method does a runtime assertion to ensure that address is 10 bits or less.
constexpr pw::i2c::Address kAddress(0x200);
Returns
A pw::i2c::Address instance.

Member Function Documentation

◆ GetAddress()

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

Gets the raw address that was provided when this Address was created.

Use IsTenBit() to know whether the address should be interpreted as a 7-bit or 10-bit address.

Returns
A an address as an unsigned integer.

◆ GetSevenBit()

uint8_t pw::i2c::Address::GetSevenBit ( ) const

Gets the 7-bit address that was provided when this instance was created.

This method does a runtime assertion to ensure that the address was constructed in 7-bit mode.

Returns
A 7-bit address as an unsigned integer.

◆ GetTenBit()

uint16_t pw::i2c::Address::GetTenBit ( ) const
inline

Gets the 10-bit address that was provided when this instance was created.

Returns
A 10-bit address as an unsigned integer.

◆ IsTenBit()

constexpr bool pw::i2c::Address::IsTenBit ( ) const
inlineconstexpr

Getter for whether this object represents a Ten bit address. Note: The address itself may still be fewer than 10 bits.

Returns
true if the address represents a 10-bit address.

◆ SevenBit() [1/2]

template<uint8_t kAddress>
static constexpr Address pw::i2c::Address::SevenBit ( )
inlinestaticconstexpr

Creates a pw::i2c::Address instance for an address that's 7 bits or less.

This constant expression does a compile-time assertion to ensure that the provided address is 7 bits or less.

constexpr pw::i2c::Address kAddress =
pw::i2c::Address::SevenBit<0x42>();
Returns
A pw::i2c::Address instance.

◆ SevenBit() [2/2]

static Address pw::i2c::Address::SevenBit ( uint16_t  address)
inlinestatic

Creates a pw::i2c::Address instance for an address that's 7 bits or less.

This constructor does a run-time check to ensure that the provided address is 7 bits or less.

static constexpr Address SevenBit()
Definition: address.h:89
Returns
A pw::i2c::Address instance.

◆ TenBit() [1/2]

template<uint16_t kAddress>
static constexpr Address pw::i2c::Address::TenBit ( )
inlinestaticconstexpr

Creates a pw::i2c::Address instance for an address that's 10 bits or less.

This constant expression does a compile-time assertion to ensure that the provided address is 10 bits or less.

constexpr pw::i2c::Address kAddress = pw::i2c::Address::TenBit<0x200>();
Returns
A pw::i2c::Address instance.

◆ TenBit() [2/2]

static Address pw::i2c::Address::TenBit ( uint16_t  address)
inlinestatic

Creates a pw::i2c::Address instance for an address that's 10 bits or less.

This constructor does a run-time check to ensure that the provided address is 10 bits or less.

static constexpr Address TenBit()
Definition: address.h:56
Returns
A pw::i2c::Address instance.

Friends And Related Function Documentation

◆ operator==

bool operator== ( const Address a1,
const Address a2 
)
friend

Operator for testing equality of two Address objects.

Returns
true if the two Address objects are the same.

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