mirror of
https://git.intern.spaceteamaachen.de/ALPAKA/sta-core.git
synced 2025-06-10 16:55:58 +00:00
Fixed broken imports after dir structure rework & added stm32 i2c
This commit is contained in:
parent
6b4acfd27b
commit
e7ddbbf365
@ -11,14 +11,14 @@ namespace sta
|
||||
*
|
||||
* @ingroup sta_core_i2c
|
||||
*/
|
||||
class I2cDevice : public Device
|
||||
class I2CDevice : public Device
|
||||
{
|
||||
public:
|
||||
/**
|
||||
* @param intf %I2C hardware interface
|
||||
* @param csPin The peripheral's address.
|
||||
*/
|
||||
I2cDevice(I2c * intf, int addr);
|
||||
I2CDevice(I2C * intf, int address, bool master=false, bool blocking=true);
|
||||
|
||||
protected:
|
||||
void select() override;
|
||||
@ -26,7 +26,10 @@ namespace sta
|
||||
void deselect() override;
|
||||
|
||||
private:
|
||||
int addr_; /**< device address */
|
||||
I2C * intf_;
|
||||
int address_; /**< device address */
|
||||
int master_;
|
||||
int blocking_;
|
||||
};
|
||||
|
||||
} // namespace sta
|
||||
|
@ -16,17 +16,23 @@ namespace sta
|
||||
*
|
||||
* @ingroup sta_core_i2c
|
||||
*/
|
||||
class I2c : public Interface
|
||||
class I2C : public Interface
|
||||
{
|
||||
public:
|
||||
I2c(Mutex * mutex=nullptr);
|
||||
I2C(Mutex * mutex=nullptr);
|
||||
|
||||
/**
|
||||
* @brief Address selection for the I2C bus.
|
||||
* @brief Specify the mode of communication via the bus.
|
||||
*
|
||||
* @param address The address to select.
|
||||
* @param master
|
||||
* @param blocking
|
||||
*/
|
||||
virtual void selectAddress(uint16_t address) = 0;
|
||||
void setSettings(uint16_t address, bool master, bool blocking);
|
||||
|
||||
protected:
|
||||
uint16_t address_;
|
||||
bool master_;
|
||||
bool blocking_;
|
||||
};
|
||||
} // namespace sta
|
||||
|
||||
|
@ -1,11 +0,0 @@
|
||||
#ifndef STA_CONFIG
|
||||
#define STA_CONFIG
|
||||
|
||||
// Use the raspberry pi for this project.
|
||||
#include <sta/devices/raspi/mcu/common.hpp>
|
||||
|
||||
// #define DEBUG
|
||||
#define STA_PRINTF_USE_STDLIB
|
||||
#define STA_DEBUG_PRINTF
|
||||
|
||||
#endif
|
44
include/sta/devices/stm32/bus/i2c.hpp
Normal file
44
include/sta/devices/stm32/bus/i2c.hpp
Normal file
@ -0,0 +1,44 @@
|
||||
#ifndef STA_CORE_STM32_I2C_HPP
|
||||
#define STA_CORE_STM32_I2C_HPP
|
||||
|
||||
#include <sta/config.hpp>
|
||||
#ifdef STA_PLATFORM_STM32
|
||||
# include <sta/devices/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/bus/i2c/i2c.hpp>
|
||||
#include <sta/bus/i2c/device.hpp>
|
||||
|
||||
namespace sta
|
||||
{
|
||||
class STM32I2C : public I2C
|
||||
{
|
||||
public:
|
||||
STM32I2C(I2C_HandleTypeDef * handle, Mutex * mutex=nullptr);
|
||||
|
||||
void transfer(uint8_t value) override;
|
||||
void transfer16(uint16_t value) override;
|
||||
void transfer(const uint8_t * buffer, size_t size) override;
|
||||
void transfer(const uint8_t * txBuffer, uint8_t * rxBuffer, size_t size) override;
|
||||
void receive(uint8_t * buffer, size_t size) override;
|
||||
|
||||
void fill(uint8_t value, size_t count) override;
|
||||
private:
|
||||
I2C_HandleTypeDef * handle_;
|
||||
const uint32_t timeout_ = HAL_MAX_DELAY;
|
||||
};
|
||||
|
||||
class STM32I2CDevice : public I2CDevice
|
||||
{
|
||||
STM32I2CDevice();
|
||||
};
|
||||
}
|
||||
|
||||
#endif // STA_STM32_I2C_ENABLED
|
||||
|
||||
#endif // STA_CORE_STM32_I2C_HPP
|
@ -19,7 +19,7 @@
|
||||
// Only enable module on STM32 platform w/ HAL CAN module enabled
|
||||
#include <sta/config.hpp>
|
||||
#ifdef STA_PLATFORM_STM32
|
||||
# include <sta/stm32/hal.hpp>
|
||||
# include <sta/devices/stm32/hal.hpp>
|
||||
# ifdef HAL_CAN_MODULE_ENABLED
|
||||
# define STA_STM32_CAN_ENABLED
|
||||
# endif // HAL_CAN_MODULE_ENABLED
|
||||
|
@ -12,7 +12,7 @@
|
||||
|
||||
#if defined(STA_PLATFORM_STM32) || defined(DOXYGEN)
|
||||
|
||||
#include <sta/stm32/hal.hpp>
|
||||
#include <sta/devices/stm32/hal.hpp>
|
||||
|
||||
|
||||
/**
|
||||
|
@ -9,7 +9,7 @@
|
||||
// Only enable module on STM32 platform w/ HAL GPIO module enabled
|
||||
#include <sta/config.hpp>
|
||||
#ifdef STA_PLATFORM_STM32
|
||||
# include <sta/stm32/hal.hpp>
|
||||
# include <sta/devices/stm32/hal.hpp>
|
||||
# ifdef HAL_GPIO_MODULE_ENABLED
|
||||
# define STA_STM32_GPIO_ENABLED
|
||||
# endif // HAL_GPIO_MODULE_ENABLED
|
||||
|
@ -1,39 +0,0 @@
|
||||
#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
|
@ -11,7 +11,7 @@
|
||||
#endif // !STM32F411xE
|
||||
|
||||
|
||||
#include <sta/stm32/mcu/common.hpp>
|
||||
#include <sta/devices/stm32/mcu/common.hpp>
|
||||
|
||||
|
||||
// Peripheral clock mappings
|
||||
|
@ -9,7 +9,7 @@
|
||||
// Only enable module on STM32 platform w/ HAL SPI module enabled
|
||||
#include <sta/config.hpp>
|
||||
#ifdef STA_PLATFORM_STM32
|
||||
# include <sta/stm32/hal.hpp>
|
||||
# include <sta/devices/stm32/hal.hpp>
|
||||
# ifndef HAL_GPIO_MODULE_ENABLED
|
||||
# error "STM32 GPIO module required!"
|
||||
# endif // !HAL_GPIO_MODULE_ENABLED
|
||||
|
@ -9,7 +9,7 @@
|
||||
// Only enable module on STM32 platform w/ HAL UART module enabled
|
||||
#include <sta/config.hpp>
|
||||
#ifdef STA_PLATFORM_STM32
|
||||
# include <sta/stm32/hal.hpp>
|
||||
# include <sta/devices/stm32/hal.hpp>
|
||||
# ifdef HAL_UART_MODULE_ENABLED
|
||||
# define STA_STM32_UART_ENABLED
|
||||
# endif // HAL_UART_MODULE_ENABLED
|
||||
|
@ -100,7 +100,7 @@
|
||||
* crash with the appropriate error message if the code is executed.
|
||||
*/
|
||||
#ifndef STA_NOT_IMPLEMENTED
|
||||
# define STA_NOT_IMPLEMENTED() throw "myFunction is not implemented yet.";
|
||||
# define STA_NOT_IMPLEMENTED() // throw "myFunction is not implemented yet.";
|
||||
#endif // !STA_NOT_IMPLEMENTED
|
||||
|
||||
/**
|
||||
|
@ -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
|
||||
|
@ -3,9 +3,16 @@
|
||||
|
||||
namespace sta
|
||||
{
|
||||
I2c::I2c(Mutex * mutex=nullptr)
|
||||
I2C::I2C(Mutex * mutex)
|
||||
: Interface{mutex}
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
void I2C::setSettings(uint16_t address, bool master, bool blocking)
|
||||
{
|
||||
address_ = address;
|
||||
master_ = master;
|
||||
blocking_ = blocking;
|
||||
}
|
||||
} // namespace sta
|
@ -1,4 +1,4 @@
|
||||
#include <sta/can/id.hpp>
|
||||
#include <sta/bus/can/id.hpp>
|
||||
|
||||
|
||||
namespace sta
|
||||
|
@ -1,4 +1,4 @@
|
||||
#include <sta/can/iter.hpp>
|
||||
#include <sta/bus/can/iter.hpp>
|
||||
|
||||
#include <sta/assert.hpp>
|
||||
|
||||
|
@ -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>
|
||||
|
122
src/devices/stm32/bus/i2c.cpp
Normal file
122
src/devices/stm32/bus/i2c.cpp
Normal 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
|
@ -1,4 +1,4 @@
|
||||
#include <sta/stm32/can.hpp>
|
||||
#include <sta/devices/stm32/can.hpp>
|
||||
#ifdef STA_STM32_CAN_ENABLED
|
||||
|
||||
#include <sta/assert.hpp>
|
||||
|
@ -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>
|
||||
|
@ -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
|
||||
|
@ -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
|
@ -1,4 +1,4 @@
|
||||
#include <sta/stm32/init.hpp>
|
||||
#include <sta/devices/stm32/init.hpp>
|
||||
|
||||
#include <sta/assert.hpp>
|
||||
|
||||
|
@ -1,4 +1,4 @@
|
||||
#include <sta/stm32/spi.hpp>
|
||||
#include <sta/devices/stm32/spi.hpp>
|
||||
#ifdef STA_STM32_SPI_ENABLED
|
||||
|
||||
#include <sta/assert.hpp>
|
||||
|
@ -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
|
||||
|
||||
|
Loading…
x
Reference in New Issue
Block a user