Basic I2C functionality

This commit is contained in:
2024-12-29 16:24:54 +01:00
parent 17099b0047
commit 1e648be211
9 changed files with 416 additions and 208 deletions

32
Core/Inc/i2c.hpp Normal file
View File

@@ -0,0 +1,32 @@
#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