Implement I2C generic device and STM32 specific; Not yet tested, will be with ADC

This commit is contained in:
Theodor Teslia
2023-03-11 21:46:41 +01:00
parent 4204c028a2
commit be0abb1e80
5 changed files with 208 additions and 0 deletions

26
include/sta/i2c.hpp Normal file
View File

@@ -0,0 +1,26 @@
#ifndef STA_I2C_HPP
#define STA_I2C_HPP
#include <cstdint>
#include <sta/mutex.hpp>
namespace sta {
class I2cDevice {
protected:
uint16_t address;
Mutex* mutex;
bool master;
public:
I2cDevice(uint16_t address_10bit, Mutex* mutex=nullptr, bool master=false);
virtual bool transmit(uint8_t* data, uint16_t size, bool blocking=true) = 0;
virtual bool receive(uint8_t* data, uint16_t size, bool blocking=true) = 0;
virtual void acquire();
virtual void release();
};
}
#endif // #ifdef STA_I2C_HPP

39
include/sta/stm32/i2c.hpp Normal file
View File

@@ -0,0 +1,39 @@
#ifndef STA_STM32_I2C_HPP
#define STA_STM32_I2C_HPP
#include <sta/config.hpp>
#ifdef STA_PLATFORM_STM32
# include <sta/stm32/hal.hpp>
# ifdef HAL_I2C_MODULE_ENABLED
# define STA_STM32_I2C_ENABLED
# endif // HAL_SPI_MODULE_ENABLED
#endif // STA_PLATFORM_STM32
#ifdef STA_STM32_I2C_ENABLED
#include <sta/i2c.hpp>
namespace sta {
class STM32I2cDevice : public I2cDevice {
private:
I2C_HandleTypeDef* i2cHandle;
const uint32_t timeout = HAL_MAX_DELAY;
public:
STM32I2cDevice(
I2C_HandleTypeDef* i2cHandle,
uint16_t address,
Mutex* mutex=nullptr,
bool master=false,
bool blocking=true
);
bool transmit(uint8_t* data, uint16_t size) override;
bool receive(uint8_t* data, uint16_t size) override;
bool deviceReady();
};
}
#endif // STA_STM32_I2C_ENABLED
#endif // STA_STM32_I2C_HPP