Merge branch 'merge-stm32' of gitlab.com:sta-git/avionics/stm32/libs/sta-core into merge-stm32

This commit is contained in:
Henrik Stickann
2023-04-22 13:06:31 +02:00
7 changed files with 222 additions and 0 deletions

22
src/i2c.cpp Normal file
View File

@@ -0,0 +1,22 @@
#include <sta/i2c.hpp>
namespace sta {
I2cDevice::I2cDevice(uint16_t address_7bit, Mutex* mutex, bool master, bool blocking) {
this->address = address_7bit << 1;
this->mutex = mutex;
this->master = master;
this->blocking = blocking;
}
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;
}
}