Fixed broken imports after dir structure rework & added stm32 i2c

This commit is contained in:
dvdb97
2023-06-24 15:44:56 +02:00
parent 6b4acfd27b
commit e7ddbbf365
26 changed files with 224 additions and 150 deletions

View File

@@ -5,19 +5,20 @@
namespace sta
{
I2cDevice::I2cDevice(I2c * intf, int addr)
: Device{intf}, addr_{addr}
I2CDevice::I2CDevice(I2C * intf, int address, bool master, bool blocking)
: Device{intf}, intf_{intf}, address_{address}, master_{master}, blocking_{blocking}
{
STA_ASSERT(intf != nullptr);
}
void I2cDevice::select()
void I2CDevice::select()
{
// TODO: Implement address selection here?
// Initialize the interface to match the device's settings.
intf_->setSettings(address_, master_, blocking_);
}
void I2cDevice::deselect()
void I2CDevice::deselect()
{
// TODO: Implement address deselection here?
// Nothing to do here.
}
} // namespace sta

View File

@@ -3,9 +3,16 @@
namespace sta
{
I2c::I2c(Mutex * mutex=nullptr)
I2C::I2C(Mutex * mutex)
: Interface{mutex}
{
}
} // namespace sta
void I2C::setSettings(uint16_t address, bool master, bool blocking)
{
address_ = address;
master_ = master;
blocking_ = blocking;
}
} // namespace sta

View File

@@ -1,4 +1,4 @@
#include <sta/can/id.hpp>
#include <sta/bus/can/id.hpp>
namespace sta

View File

@@ -1,4 +1,4 @@
#include <sta/can/iter.hpp>
#include <sta/bus/can/iter.hpp>
#include <sta/assert.hpp>

View File

@@ -1,4 +1,4 @@
#include <sta/raspi/gpio_pin.hpp>
#include <sta/devices/raspi/gpio_pin.hpp>
#ifdef STA_RASPI_GPIO_ENABLED
#include <sta/assert.hpp>
@@ -40,4 +40,4 @@ namespace sta
} // namespace sta
#endif // STA_ARDUINO_GPIO_ENABLED
#endif // STA_ARDUINO_GPIO_ENABLED

View File

@@ -0,0 +1,122 @@
#include <sta/devices/stm32/bus/i2c.hpp>
#include <sta/assert.hpp>
#include <cstring>
namespace sta
{
STM32I2C::STM32I2C(I2C_HandleTypeDef * handle, Mutex * mutex)
: I2C{mutex}, handle_{handle}
{
STA_ASSERT(handle != nullptr);
}
void STM32I2C::transfer(uint8_t value)
{
HAL_StatusTypeDef res;
if (master_)
{
res = HAL_I2C_Master_Transmit(handle_, address_, &value, 1, timeout_);
} else {
res = HAL_I2C_Slave_Transmit(handle_, &value, 1, timeout_);
}
STA_ASSERT(res == HAL_OK);
}
void STM32I2C::transfer16(uint16_t value)
{
HAL_StatusTypeDef res;
if (blocking_)
{
if (master_)
{
res = HAL_I2C_Master_Transmit(handle_, address_, reinterpret_cast<uint8_t *>(&value), 2, timeout_);
} else {
res = HAL_I2C_Slave_Transmit(handle_, reinterpret_cast<uint8_t *>(&value), 2, timeout_);
}
} else {
if (master_)
{
res = HAL_I2C_Slave_Transmit(handle_, reinterpret_cast<uint8_t *>(&value), 2, timeout_);
} else {
res = HAL_I2C_Slave_Transmit_IT(handle_, reinterpret_cast<uint8_t *>(&value), 2);
}
}
STA_ASSERT(res == HAL_OK);
}
void STM32I2C::transfer(const uint8_t * buffer, size_t size)
{
HAL_StatusTypeDef res;
/*
* It's undecided if we want to change the parameter for this function. Since the transmission
* doesn't take a const buffer as an argument, we are using this fix by creating a temporary buffer.
*/
uint8_t * temp_buffer = new uint8_t[size];
memcpy(temp_buffer, buffer, size);
if (blocking_)
{
if (master_)
{
res = HAL_I2C_Master_Transmit(handle_, address_, temp_buffer, size, timeout_);
} else {
res = HAL_I2C_Slave_Transmit(handle_, temp_buffer, size, timeout_);
}
} else {
if (master_) {
res = HAL_I2C_Master_Transmit_IT(handle_, address_, temp_buffer, size);
} else {
res = HAL_I2C_Slave_Transmit_IT(handle_, temp_buffer, size);
}
}
delete [] temp_buffer;
STA_ASSERT(res == HAL_OK);
}
void STM32I2C::transfer(const uint8_t * txBuffer, uint8_t * rxBuffer, size_t size)
{
// TODO: Is this even something necessary for I2C?
}
void STM32I2C::receive(uint8_t * buffer, size_t size)
{
HAL_StatusTypeDef res;
if (blocking_) {
if (!master_) {
res = HAL_I2C_Master_Receive(handle_, address_, buffer, size, timeout_);
} else {
res = HAL_I2C_Slave_Receive(handle_, buffer, size, timeout_);
}
} else {
if (master_) {
res = HAL_I2C_Master_Receive_IT(handle_, address_, buffer, size);
} else {
res = HAL_I2C_Slave_Receive_IT(handle_, buffer, size);
}
}
STA_ASSERT(res == HAL_OK);
}
void STM32I2C::fill(uint8_t value, size_t count)
{
// Initialize a buffer of size count and fill it with the value.
uint8_t *buffer = new uint8_t[count];
memset(buffer, value, count);
// Transfer the buffer via the bus.
transfer(buffer, count);
delete [] buffer;
}
} // namespace sta

View File

@@ -1,4 +1,4 @@
#include <sta/stm32/can.hpp>
#include <sta/devices/stm32/can.hpp>
#ifdef STA_STM32_CAN_ENABLED
#include <sta/assert.hpp>

View File

@@ -1,8 +1,8 @@
#include <sta/stm32/delay.hpp>
#include <sta/devices/stm32/delay.hpp>
#ifdef STA_PLATFORM_STM32
#include <sta/stm32/hal.hpp>
#include <sta/stm32/clocks.hpp>
#include <sta/devices/stm32/hal.hpp>
#include <sta/devices/stm32/clocks.hpp>
#include <sta/assert.hpp>
#include <sta/lang.hpp>

View File

@@ -1,4 +1,4 @@
#include <sta/stm32/gpio_pin.hpp>
#include <sta/devices/stm32/gpio_pin.hpp>
#ifdef STA_STM32_GPIO_ENABLED
#include <sta/assert.hpp>
@@ -15,7 +15,7 @@ namespace sta
void STM32GpioPin::setState(GpioPinState state)
{
HAL_GPIO_WritePin(port_, pin_, (state == GpioPinState::LOW) ? GPIO_PIN_RESET : GPIO_PIN_SET);
HAL_GPIO_WritePin(port_, pin_, (state == GpioPinState::GPIO_LOW) ? GPIO_PIN_RESET : GPIO_PIN_SET);
}
GPIO_TypeDef * STM32GpioPin::getPort() const

View File

@@ -1,57 +0,0 @@
#include <sta/stm32/i2c.hpp>
#if false
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;
}
}
#endif // false

View File

@@ -1,4 +1,4 @@
#include <sta/stm32/init.hpp>
#include <sta/devices/stm32/init.hpp>
#include <sta/assert.hpp>

View File

@@ -1,4 +1,4 @@
#include <sta/stm32/spi.hpp>
#include <sta/devices/stm32/spi.hpp>
#ifdef STA_STM32_SPI_ENABLED
#include <sta/assert.hpp>

View File

@@ -13,8 +13,6 @@
* - Remove the <sta/lang.hpp> import if no longer needed.
*/
#define STA_PLATFORM_YOUR_DEVICE
#include <sta/devices/template/delay.hpp>
#ifdef STA_PLATFORM_YOUR_DEVICE
@@ -33,4 +31,4 @@ namespace sta
}
} // namespace sta
#endif // STA_PLATFORM_YOUR_DEVICE
#endif // STA_PLATFORM_YOUR_DEVICE