From e7ddbbf365f061abbc940588ecb5b42cc43d7ec3 Mon Sep 17 00:00:00 2001 From: dvdb97 Date: Sat, 24 Jun 2023 15:44:56 +0200 Subject: [PATCH] Fixed broken imports after dir structure rework & added stm32 i2c --- include/sta/bus/i2c/device.hpp | 13 +- include/sta/bus/i2c/i2c.hpp | 20 ++- include/sta/bus/interface.hpp | 2 +- include/sta/config.hpp | 11 -- include/sta/devices/stm32/bus/i2c.hpp | 44 +++++++ include/sta/devices/stm32/can.hpp | 2 +- include/sta/devices/stm32/clocks.hpp | 2 +- include/sta/devices/stm32/gpio_pin.hpp | 2 +- include/sta/devices/stm32/i2c.hpp | 39 ------ include/sta/devices/stm32/mcu/STM32F411xE.hpp | 2 +- include/sta/devices/stm32/spi.hpp | 2 +- include/sta/devices/stm32/uart.hpp | 2 +- include/sta/lang.hpp | 2 +- src/bus/i2c/device.cpp | 13 +- src/bus/i2c/i2c.cpp | 11 +- src/can/id.cpp | 2 +- src/can/iter.cpp | 2 +- src/devices/raspi/gpio_pin.cpp | 4 +- src/devices/stm32/bus/i2c.cpp | 122 ++++++++++++++++++ src/devices/stm32/can.cpp | 2 +- src/devices/stm32/delay.cpp | 6 +- src/devices/stm32/gpio_pin.cpp | 4 +- src/devices/stm32/i2c.cpp | 57 -------- src/devices/stm32/init.cpp | 2 +- src/devices/stm32/spi.cpp | 2 +- src/devices/template/delay.cpp | 4 +- 26 files changed, 224 insertions(+), 150 deletions(-) delete mode 100644 include/sta/config.hpp create mode 100644 include/sta/devices/stm32/bus/i2c.hpp delete mode 100644 include/sta/devices/stm32/i2c.hpp create mode 100644 src/devices/stm32/bus/i2c.cpp delete mode 100644 src/devices/stm32/i2c.cpp diff --git a/include/sta/bus/i2c/device.hpp b/include/sta/bus/i2c/device.hpp index 2a96b4d..88485b3 100644 --- a/include/sta/bus/i2c/device.hpp +++ b/include/sta/bus/i2c/device.hpp @@ -11,22 +11,25 @@ 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; void deselect() override; - + private: - int addr_; /**< device address */ + I2C * intf_; + int address_; /**< device address */ + int master_; + int blocking_; }; } // namespace sta @@ -34,4 +37,4 @@ namespace sta -#endif // STA_CORE_I2C_DEVICE_HPP \ No newline at end of file +#endif // STA_CORE_I2C_DEVICE_HPP diff --git a/include/sta/bus/i2c/i2c.hpp b/include/sta/bus/i2c/i2c.hpp index 8d3123a..e2e37c4 100644 --- a/include/sta/bus/i2c/i2c.hpp +++ b/include/sta/bus/i2c/i2c.hpp @@ -16,18 +16,24 @@ 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. - * - * @param address The address to select. + * @brief Specify the mode of communication via the bus. + * + * @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 -#endif // STA_CORE_I2C_I2C_HPP \ No newline at end of file +#endif // STA_CORE_I2C_I2C_HPP diff --git a/include/sta/bus/interface.hpp b/include/sta/bus/interface.hpp index a8581d8..a26151a 100644 --- a/include/sta/bus/interface.hpp +++ b/include/sta/bus/interface.hpp @@ -83,4 +83,4 @@ namespace sta } // namespace sta -#endif // STA_CORE_BUS_SERIAL_INTERFACE_HPP \ No newline at end of file +#endif // STA_CORE_BUS_SERIAL_INTERFACE_HPP diff --git a/include/sta/config.hpp b/include/sta/config.hpp deleted file mode 100644 index f2f4489..0000000 --- a/include/sta/config.hpp +++ /dev/null @@ -1,11 +0,0 @@ -#ifndef STA_CONFIG -#define STA_CONFIG - -// Use the raspberry pi for this project. -#include - -// #define DEBUG -#define STA_PRINTF_USE_STDLIB -#define STA_DEBUG_PRINTF - -#endif \ No newline at end of file diff --git a/include/sta/devices/stm32/bus/i2c.hpp b/include/sta/devices/stm32/bus/i2c.hpp new file mode 100644 index 0000000..be939d2 --- /dev/null +++ b/include/sta/devices/stm32/bus/i2c.hpp @@ -0,0 +1,44 @@ +#ifndef STA_CORE_STM32_I2C_HPP +#define STA_CORE_STM32_I2C_HPP + +#include +#ifdef STA_PLATFORM_STM32 +# include +# 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 +#include + +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 diff --git a/include/sta/devices/stm32/can.hpp b/include/sta/devices/stm32/can.hpp index 2bf910c..8eb5d70 100644 --- a/include/sta/devices/stm32/can.hpp +++ b/include/sta/devices/stm32/can.hpp @@ -19,7 +19,7 @@ // Only enable module on STM32 platform w/ HAL CAN module enabled #include #ifdef STA_PLATFORM_STM32 -# include +# include # ifdef HAL_CAN_MODULE_ENABLED # define STA_STM32_CAN_ENABLED # endif // HAL_CAN_MODULE_ENABLED diff --git a/include/sta/devices/stm32/clocks.hpp b/include/sta/devices/stm32/clocks.hpp index a556fb5..df4604e 100644 --- a/include/sta/devices/stm32/clocks.hpp +++ b/include/sta/devices/stm32/clocks.hpp @@ -12,7 +12,7 @@ #if defined(STA_PLATFORM_STM32) || defined(DOXYGEN) -#include +#include /** diff --git a/include/sta/devices/stm32/gpio_pin.hpp b/include/sta/devices/stm32/gpio_pin.hpp index f6f355e..ee87f2d 100644 --- a/include/sta/devices/stm32/gpio_pin.hpp +++ b/include/sta/devices/stm32/gpio_pin.hpp @@ -9,7 +9,7 @@ // Only enable module on STM32 platform w/ HAL GPIO module enabled #include #ifdef STA_PLATFORM_STM32 -# include +# include # ifdef HAL_GPIO_MODULE_ENABLED # define STA_STM32_GPIO_ENABLED # endif // HAL_GPIO_MODULE_ENABLED diff --git a/include/sta/devices/stm32/i2c.hpp b/include/sta/devices/stm32/i2c.hpp deleted file mode 100644 index 4804e1a..0000000 --- a/include/sta/devices/stm32/i2c.hpp +++ /dev/null @@ -1,39 +0,0 @@ -#ifndef STA_STM32_I2C_HPP -#define STA_STM32_I2C_HPP - -#include -#ifdef STA_PLATFORM_STM32 -# include -# 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 - -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 diff --git a/include/sta/devices/stm32/mcu/STM32F411xE.hpp b/include/sta/devices/stm32/mcu/STM32F411xE.hpp index 838408d..cdca7c2 100644 --- a/include/sta/devices/stm32/mcu/STM32F411xE.hpp +++ b/include/sta/devices/stm32/mcu/STM32F411xE.hpp @@ -11,7 +11,7 @@ #endif // !STM32F411xE -#include +#include // Peripheral clock mappings diff --git a/include/sta/devices/stm32/spi.hpp b/include/sta/devices/stm32/spi.hpp index 0e477bf..385e1e9 100644 --- a/include/sta/devices/stm32/spi.hpp +++ b/include/sta/devices/stm32/spi.hpp @@ -9,7 +9,7 @@ // Only enable module on STM32 platform w/ HAL SPI module enabled #include #ifdef STA_PLATFORM_STM32 -# include +# include # ifndef HAL_GPIO_MODULE_ENABLED # error "STM32 GPIO module required!" # endif // !HAL_GPIO_MODULE_ENABLED diff --git a/include/sta/devices/stm32/uart.hpp b/include/sta/devices/stm32/uart.hpp index fb1a10e..1b79738 100644 --- a/include/sta/devices/stm32/uart.hpp +++ b/include/sta/devices/stm32/uart.hpp @@ -9,7 +9,7 @@ // Only enable module on STM32 platform w/ HAL UART module enabled #include #ifdef STA_PLATFORM_STM32 -# include +# include # ifdef HAL_UART_MODULE_ENABLED # define STA_STM32_UART_ENABLED # endif // HAL_UART_MODULE_ENABLED diff --git a/include/sta/lang.hpp b/include/sta/lang.hpp index 0c68348..037823f 100644 --- a/include/sta/lang.hpp +++ b/include/sta/lang.hpp @@ -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 /** diff --git a/src/bus/i2c/device.cpp b/src/bus/i2c/device.cpp index dc54795..0235b7c 100644 --- a/src/bus/i2c/device.cpp +++ b/src/bus/i2c/device.cpp @@ -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 diff --git a/src/bus/i2c/i2c.cpp b/src/bus/i2c/i2c.cpp index db7d91f..289bbda 100644 --- a/src/bus/i2c/i2c.cpp +++ b/src/bus/i2c/i2c.cpp @@ -3,9 +3,16 @@ namespace sta { - I2c::I2c(Mutex * mutex=nullptr) + I2C::I2C(Mutex * mutex) : Interface{mutex} { } -} // namespace sta \ No newline at end of file + + void I2C::setSettings(uint16_t address, bool master, bool blocking) + { + address_ = address; + master_ = master; + blocking_ = blocking; + } +} // namespace sta diff --git a/src/can/id.cpp b/src/can/id.cpp index 8ba0fd9..26c8070 100644 --- a/src/can/id.cpp +++ b/src/can/id.cpp @@ -1,4 +1,4 @@ -#include +#include namespace sta diff --git a/src/can/iter.cpp b/src/can/iter.cpp index b7a7b06..b5dff60 100644 --- a/src/can/iter.cpp +++ b/src/can/iter.cpp @@ -1,4 +1,4 @@ -#include +#include #include diff --git a/src/devices/raspi/gpio_pin.cpp b/src/devices/raspi/gpio_pin.cpp index af3e210..fbfb512 100644 --- a/src/devices/raspi/gpio_pin.cpp +++ b/src/devices/raspi/gpio_pin.cpp @@ -1,4 +1,4 @@ -#include +#include #ifdef STA_RASPI_GPIO_ENABLED #include @@ -40,4 +40,4 @@ namespace sta } // namespace sta -#endif // STA_ARDUINO_GPIO_ENABLED \ No newline at end of file +#endif // STA_ARDUINO_GPIO_ENABLED diff --git a/src/devices/stm32/bus/i2c.cpp b/src/devices/stm32/bus/i2c.cpp new file mode 100644 index 0000000..6cbd5e4 --- /dev/null +++ b/src/devices/stm32/bus/i2c.cpp @@ -0,0 +1,122 @@ +#include + +#include +#include + +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(&value), 2, timeout_); + } else { + res = HAL_I2C_Slave_Transmit(handle_, reinterpret_cast(&value), 2, timeout_); + } + } else { + if (master_) + { + res = HAL_I2C_Slave_Transmit(handle_, reinterpret_cast(&value), 2, timeout_); + } else { + res = HAL_I2C_Slave_Transmit_IT(handle_, reinterpret_cast(&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 diff --git a/src/devices/stm32/can.cpp b/src/devices/stm32/can.cpp index 5c39fc7..1355b0e 100644 --- a/src/devices/stm32/can.cpp +++ b/src/devices/stm32/can.cpp @@ -1,4 +1,4 @@ -#include +#include #ifdef STA_STM32_CAN_ENABLED #include diff --git a/src/devices/stm32/delay.cpp b/src/devices/stm32/delay.cpp index a3b9a98..a9d4c51 100644 --- a/src/devices/stm32/delay.cpp +++ b/src/devices/stm32/delay.cpp @@ -1,8 +1,8 @@ -#include +#include #ifdef STA_PLATFORM_STM32 -#include -#include +#include +#include #include #include diff --git a/src/devices/stm32/gpio_pin.cpp b/src/devices/stm32/gpio_pin.cpp index c0d6a67..9318605 100644 --- a/src/devices/stm32/gpio_pin.cpp +++ b/src/devices/stm32/gpio_pin.cpp @@ -1,4 +1,4 @@ -#include +#include #ifdef STA_STM32_GPIO_ENABLED #include @@ -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 diff --git a/src/devices/stm32/i2c.cpp b/src/devices/stm32/i2c.cpp deleted file mode 100644 index ce4bdbd..0000000 --- a/src/devices/stm32/i2c.cpp +++ /dev/null @@ -1,57 +0,0 @@ -#include - -#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 \ No newline at end of file diff --git a/src/devices/stm32/init.cpp b/src/devices/stm32/init.cpp index fed0f3c..843c6d0 100644 --- a/src/devices/stm32/init.cpp +++ b/src/devices/stm32/init.cpp @@ -1,4 +1,4 @@ -#include +#include #include diff --git a/src/devices/stm32/spi.cpp b/src/devices/stm32/spi.cpp index ec5af2a..2a2676a 100644 --- a/src/devices/stm32/spi.cpp +++ b/src/devices/stm32/spi.cpp @@ -1,4 +1,4 @@ -#include +#include #ifdef STA_STM32_SPI_ENABLED #include diff --git a/src/devices/template/delay.cpp b/src/devices/template/delay.cpp index 18edb34..3b19826 100644 --- a/src/devices/template/delay.cpp +++ b/src/devices/template/delay.cpp @@ -13,8 +13,6 @@ * - Remove the import if no longer needed. */ -#define STA_PLATFORM_YOUR_DEVICE - #include #ifdef STA_PLATFORM_YOUR_DEVICE @@ -33,4 +31,4 @@ namespace sta } } // namespace sta -#endif // STA_PLATFORM_YOUR_DEVICE \ No newline at end of file +#endif // STA_PLATFORM_YOUR_DEVICE