i2c/i2c.hpp

52 lines
767 B
C++

#pragma once
#include <stddef.h>
#include <stdint.h>
#include "hardware.hpp"
namespace i2c {
template <class Driver>
struct I2c {
static void init()
{
Driver::init();
}
template <uint8_t Addr>
static bool start(bool read)
{
return Driver::template start<Addr>(read);
}
static bool write(uint8_t data)
{
return Driver::write(data);
}
template <bool LastByte = false>
static uint8_t read()
{
return Driver::template read<LastByte>();
}
template <size_t Bytes>
static void readBytes(uint8_t *buffer)
{
static_assert(Bytes > 0, "Must read at least 1 byte");
for (size_t i = 0; i < Bytes - 1; ++i) {
buffer[i] = read();
}
buffer[Bytes - 1] = read<true>();
}
static void stop()
{
Driver::stop();
}
};
} // namespace i2c