RISC-V/i2c/i2cmaster.h
2024-04-08 20:55:20 +02:00

28 lines
714 B
C++

#ifndef I2CMASTER_H
#define I2CMASTER_H
class Print;
class I2CMaster {
public:
explicit I2CMaster () noexcept;
bool Send (const unsigned char addr, const unsigned char * data, const unsigned len);
bool Receive (const unsigned char addr, unsigned char * data, const unsigned len);
void FindActive (Print & print);
protected:
bool stop_condition ();
bool send_address (const unsigned char addr);
template<typename F> bool wait_for (F f, const unsigned timeout = 10000) {
volatile unsigned counter = 0u;
while (f()) {
counter += 1u;
if (counter > timeout) return false;
}
return true;
}
};
#endif // I2CMASTER_H