32 lines
960 B
C++
32 lines
960 B
C++
#pragma once
|
|
#include <vector>
|
|
|
|
#include "stm32f401xe.h"
|
|
#include "stm32f4xx_hal.h"
|
|
#include "stm32f4xx_hal_i2c.h"
|
|
#include "stm32f4xx_hal_uart.h"
|
|
|
|
namespace driver::i2c {
|
|
|
|
constexpr const uint32_t I2C_TIMEOUT_MS{200};
|
|
|
|
class I2c {
|
|
public:
|
|
explicit I2c(I2C_TypeDef* i2c, const uint32_t clockSpeed, const uint32_t dutyCycle,
|
|
const uint32_t ownAddress1, const uint32_t addressingMode,
|
|
const uint32_t dualAddressMode, const uint32_t ownAddress2,
|
|
const uint32_t generalCallMode, const uint32_t noStretchMode);
|
|
~I2c() = default;
|
|
|
|
bool init();
|
|
void deinit();
|
|
HAL_StatusTypeDef write(const uint16_t slaveAddr, const uint16_t memAddr,
|
|
std::vector<uint8_t>& data);
|
|
std::pair<HAL_StatusTypeDef, uint8_t> read(const uint16_t slaveAddr, const uint16_t memAddr);
|
|
uint32_t getLastError();
|
|
|
|
private:
|
|
I2C_HandleTypeDef mHandle;
|
|
};
|
|
|
|
} // namespace driver::i2c
|