Raspberry Pio Pico Drivers project
Loading...
Searching...
No Matches
Public Member Functions
rpp_driver::I2cMaster Class Reference

I2C Master controller class. More...

#include <i2cmaster.hpp>

Inheritance diagram for rpp_driver::I2cMaster:
Inheritance graph
[legend]

Public Member Functions

 I2cMaster (SdkWrapper &sdk, i2c_inst_t &i2c, uint clock_freq, uint scl_pin, uint sda_pin)
 Initialize the given I2C port and setup the pins.
 
virtual ~I2cMaster ()
 deinit the I2C by SdkWrapper::i2c_deinit();
 
virtual int ReadBlocking (uint8_t addr, uint8_t *dst, size_t len, bool nostop)
 Attempt to read specified number of bytes from address, blocking.
 
virtual int WriteBlocking (uint8_t addr, const uint8_t *src, size_t len, bool nostop)
 Attempt to write specified number of bytes to address, blocking.
 
virtual bool IsDeviceExisting (uint8_t addr)
 Check wether device at specified I2C address exists or not.
 

Detailed Description

I2C Master controller class.

This class provides a set of easy to use member function to control the I2C master controller. Everything operation in this class is polling based and blocking.

The constructor and destructor initializes and finalize the given I2C controller, respectively.

The ReadBlocking() and WriteBlocking() functions has nostop parameter. To use the restart condition, set this parameter to true.

Usage of mock

In the case of the testing of the user program which uses this class, a programmer can use the pre-defined mock class rpp_driver::MockI2cMaster. inside i2cmaster.hpp.

#include <gmock/gmock.h>
#include <gtest/gtest.h>
using ::testing::_;
class UserCodeTest : public ::testing::Test {
protected:
::rpp_driver::MockSdkWrapper mock_sdk_;
virtual void SetUp() {
// Constructor will call these functiions.
EXPECT_CALL(mock_sdk_, i2c_init(_, _));
EXPECT_CALL(mock_sdk_, gpio_set_function(_, GPIO_FUNC_I2C)).Times(2);
EXPECT_CALL(mock_sdk_, gpio_pull_up(_)).Times(2);
mock_i2c_ = new ::rpp_driver::MockI2cMaster(mock_sdk_);
}
virtual void TearDown() {
// We can ignore these call inside destructor
EXPECT_CALL(mock_sdk_, i2c_deinit(_));
delete mock_i2c_;
}
};
TEST_F(UserCodeTest, foo) {
// Write Test code here.
}
Mock class for test.
Definition: i2cmaster.hpp:150
I2C Master Controller.
#define GPIO_FUNC_I2C
Definition: i2cmaster.hpp:26

Constructor & Destructor Documentation

◆ I2cMaster()

rpp_driver::I2cMaster::I2cMaster ( SdkWrapper sdk,
i2c_inst_t i2c,
uint  clock_freq,
uint  scl_pin,
uint  sda_pin 
)

Initialize the given I2C port and setup the pins.

Parameters
sdkObject of the SdkWrapper class.
i2ci2c_inst_t type &. *I2C0 or *I2C1
clock_freqUsually 100,000 or 400,000[Hz].
scl_pinGPIO pin #
sda_pinGPIO pin #

Receive the uninitialized I2C hardware by parameter i2c, and initialize it by SdkWrapper::i2c_init(). And then, set given pins to I2C function, and pull them up.

Member Function Documentation

◆ IsDeviceExisting()

virtual bool rpp_driver::I2cMaster::IsDeviceExisting ( uint8_t  addr)
virtual

Check wether device at specified I2C address exists or not.

Parameters
addr7-bit address of device to read from
Returns
true if exist, false if not exist.

◆ ReadBlocking()

virtual int rpp_driver::I2cMaster::ReadBlocking ( uint8_t  addr,
uint8_t *  dst,
size_t  len,
bool  nostop 
)
virtual

Attempt to read specified number of bytes from address, blocking.

Parameters
addr7-bit address of device to read from
dstPointer to buffer to receive data
lenLength of data in bytes to receive
nostopIf true, master retains control of the bus at the end of the transfer (no Stop is issued), and the next transfer will begin with a Restart rather than a Start.
Returns
Number of bytes read, or PICO_ERROR_GENERIC if address not acknowledged or no device present.

◆ WriteBlocking()

virtual int rpp_driver::I2cMaster::WriteBlocking ( uint8_t  addr,
const uint8_t *  src,
size_t  len,
bool  nostop 
)
virtual

Attempt to write specified number of bytes to address, blocking.

Parameters
addr7-bit address of device to write to
srcPointer to data to send
lenLength of data in bytes to send
nostopIf true, master retains control of the bus at the end of the transfer (no Stop is issued), and the next transfer will begin with a Restart rather than a Start.
Returns
Number of bytes written, or PICO_ERROR_GENERIC if address not acknowledged, no device present.

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