Raspberry Pio Pico Drivers project
Loading...
Searching...
No Matches
i2cmaster.hpp
Go to the documentation of this file.
1
11#ifndef PICO_DRIVER_SRC_I2C_I2CMASATER_HPP_
12#define PICO_DRIVER_SRC_I2C_I2CMASATER_HPP_
13#if __has_include(<hardware/i2c.h>) || __has_include(<gmock/gmock.h>) || DOXYGEN_COMPILE
14
15#include <stdint.h>
16
17#if __has_include(<hardware/i2c.h>)
18#include "hardware/i2c.h"
19#else
21typedef int i2c_inst_t;
23#define PICO_ERROR_GENERIC -1
26#define GPIO_FUNC_I2C 11
27#endif // __has_include(<hardware/i2c.h>)
28
29#include "sdk/sdkwrapper.hpp"
30
31namespace rpp_driver {
83class I2cMaster {
84 public:
97 I2cMaster(SdkWrapper &sdk, i2c_inst_t &i2c, uint clock_freq, uint scl_pin,
98 uint sda_pin);
99 I2cMaster() = delete;
103 virtual ~I2cMaster();
117 virtual int ReadBlocking(uint8_t addr, uint8_t *dst, size_t len, bool nostop);
129 virtual int WriteBlocking(uint8_t addr, const uint8_t *src, size_t len,
130 bool nostop);
131
138 virtual bool IsDeviceExisting(uint8_t addr);
139
140 private:
141 i2c_inst_t &i2c_;
142 SdkWrapper &sdk_;
143}; // I2cMaster
144
145#if __has_include(<gmock/gmock.h>)|| DOXYGEN_COMPILE
146// GCOVR_EXCL_START
150class MockI2cMaster : public I2cMaster {
151 private:
152 i2c_inst_t dummy_i2c = 3;
153
154 public:
156 : I2cMaster(sdk, dummy_i2c, 100'000, 10, 11) {
157 }; // Sdk wrapper and dummy parameters.
158 MOCK_METHOD4(ReadBlocking,
159 int(uint8_t addr, uint8_t *dst, size_t len, bool nostop));
160 MOCK_METHOD4(WriteBlocking,
161 int(uint8_t addr, const uint8_t *src, size_t len, bool nostop));
162 MOCK_METHOD1(IsDeviceExisting, bool(uint8_t addr));
163};
164// GCOVR_EXCL_STOP
165#endif // __has_include(<gmock/gmock.h>)
166
167}; // namespace rpp_driver
168#endif // __has_include(<hardware/i2c.h>) || __has_include(<gmock/gmock.h>)
169
170#endif /* PICO_DRIVER_SRC_I2C_I2CMASATER_HPP_ */
I2C Master controller class.
Definition: i2cmaster.hpp:83
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 int ReadBlocking(uint8_t addr, uint8_t *dst, size_t len, bool nostop)
Attempt to read specified number of bytes from address, blocking.
virtual ~I2cMaster()
deinit the I2C by SdkWrapper::i2c_deinit();
virtual bool IsDeviceExisting(uint8_t addr)
Check wether device at specified I2C address exists or not.
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.
Mock class for test.
Definition: i2cmaster.hpp:150
Wrapper class for the RasPi Pico SDK functions.
Definition: sdkwrapper.hpp:74
int i2c_inst_t
Alternate definition for unit test.
Definition: i2cmaster.hpp:21
Collection of the RP2040/RP2350 control.
Definition: adau1361.hpp:17
Wrapper for RasPi Pico SDK API.