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

69
.vscode/settings.json vendored Normal file
View File

@ -0,0 +1,69 @@
{
"files.associations": {
"__bit_reference": "cpp",
"__bits": "cpp",
"__config": "cpp",
"__debug": "cpp",
"__errc": "cpp",
"__hash_table": "cpp",
"__locale": "cpp",
"__mutex_base": "cpp",
"__node_handle": "cpp",
"__nullptr": "cpp",
"__split_buffer": "cpp",
"__string": "cpp",
"__threading_support": "cpp",
"__tuple": "cpp",
"array": "cpp",
"atomic": "cpp",
"bit": "cpp",
"bitset": "cpp",
"cctype": "cpp",
"chrono": "cpp",
"cinttypes": "cpp",
"clocale": "cpp",
"cmath": "cpp",
"compare": "cpp",
"complex": "cpp",
"concepts": "cpp",
"cstdarg": "cpp",
"cstddef": "cpp",
"cstdint": "cpp",
"cstdio": "cpp",
"cstdlib": "cpp",
"cstring": "cpp",
"ctime": "cpp",
"cwchar": "cpp",
"cwctype": "cpp",
"exception": "cpp",
"initializer_list": "cpp",
"ios": "cpp",
"iosfwd": "cpp",
"istream": "cpp",
"limits": "cpp",
"locale": "cpp",
"memory": "cpp",
"mutex": "cpp",
"new": "cpp",
"optional": "cpp",
"ostream": "cpp",
"ratio": "cpp",
"sstream": "cpp",
"stdexcept": "cpp",
"streambuf": "cpp",
"string": "cpp",
"string_view": "cpp",
"system_error": "cpp",
"tuple": "cpp",
"type_traits": "cpp",
"typeinfo": "cpp",
"unordered_map": "cpp",
"variant": "cpp",
"vector": "cpp",
"__functional_base": "cpp",
"algorithm": "cpp",
"functional": "cpp",
"iterator": "cpp",
"utility": "cpp"
}
}

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

21
src/i2c.cpp Normal file
View File

@ -0,0 +1,21 @@
#include <sta/i2c.hpp>
namespace sta {
I2cDevice::I2cDevice(uint16_t address_7bit, Mutex* mutex, bool master) {
this->address = address_7bit << 1;
this->mutex = mutex;
this->master = master;
}
void I2cDevice::acquire() {
if (this->mutex != nullptr) {
mutex->acquire();
}
}
void I2cDevice::release() {
if (this->mutex != nullptr) {
mutex->release();
}
}
}

53
src/stm32/i2c.cpp Normal file
View File

@ -0,0 +1,53 @@
#include <sta/stm32/i2c.hpp>
namespace sta {
STM32I2cDevice::STM32I2cDevice(I2C_HandleTypeDef* i2cHandle, uint16_t address, Mutex* mutex, bool master, bool blocking)
: I2cDevice(address, mutex, master, blocking) {
this->master = master;
}
bool STM32I2cDevice::transmit(uint8_t* data, uint16_t size) {
HAL_StatusTypeDef res;
if (this->blocking) {
if (!this->master) {
res = HAL_I2C_Master_Transmit(i2cHandle, address, data, size, this->timeout);
} else {
res = HAL_I2C_Slave_Transmit(i2cHandle , data, size, this->timeout);
}
} else {
if (!this->master) {
res = HAL_I2C_Master_Transmit_IT(i2cHandle, address, data, size);
} else {
res = HAL_I2C_Slave_Transmit_IT(i2cHandle , data, size);
}
}
return res == HAL_OK;
}
bool STM32I2cDevice::receive(uint8_t* data, uint16_t size) {
HAL_StatusTypeDef res;
if (this->blocking) {
if (!this->master) {
res = HAL_I2C_Master_Receive(i2cHandle, address, data, size, this->timeout);
} else {
res = HAL_I2C_Slave_Receive(i2cHandle , data, size, this->timeout);
}
} else {
if (!this->master) {
res = HAL_I2C_Master_Receive_IT(i2cHandle, address, data, size);
} else {
res = HAL_I2C_Slave_Receive_IT(i2cHandle , data, size);
}
}
return res == HAL_OK;
}
bool STM32I2cDevice::deviceReady() {
HAL_StatusTypeDef res = HAL_I2C_IsDeviceReady(this->i2cHandle, this->address, 8, this->timeout);
return res == HAL_OK;
}
}