From dcd613ddf0709988bd003a1fc7563bc6f58b1bb5 Mon Sep 17 00:00:00 2001 From: dario Date: Thu, 14 Nov 2024 21:58:42 +0100 Subject: [PATCH] Fixed compiler errors, untested due to lack of hardware --- include/sta/bus/device.hpp | 2 +- include/sta/bus/interface.hpp | 4 ++-- include/sta/bus/spi/spi.hpp | 18 ------------------ src/bus/interface.cpp | 26 ++++++++++++++++++++++++-- src/bus/spi/spi.cpp | 22 ---------------------- src/devices/stm32/bus/i2c.cpp | 11 +++++++++-- 6 files changed, 36 insertions(+), 47 deletions(-) diff --git a/include/sta/bus/device.hpp b/include/sta/bus/device.hpp index 6c5b6fd..853f4aa 100644 --- a/include/sta/bus/device.hpp +++ b/include/sta/bus/device.hpp @@ -84,7 +84,7 @@ namespace sta * @param value 8-bit value to repeat * @param count Number of repetitions */ - void fill(uint8_t value, size_t count, uint32_t timeout = STA_MAX_TIMEOUT); + bool fill(uint8_t value, size_t count, uint32_t timeout = STA_MAX_TIMEOUT); protected: /** diff --git a/include/sta/bus/interface.hpp b/include/sta/bus/interface.hpp index 24b630f..9aa6b5e 100644 --- a/include/sta/bus/interface.hpp +++ b/include/sta/bus/interface.hpp @@ -51,7 +51,7 @@ namespace sta * @param buffer The buffer of data to write to the address * @param size The number of bytes to write to the peripheral. */ - virtual bool writeMemory(uint8_t regAddr, const uint8_t * buffer, size_t size, uint32_t timeout = STA_MAX_TIMEOUT) = 0; + virtual bool writeMemory(uint8_t regAddr, const uint8_t * buffer, size_t size, uint32_t timeout = STA_MAX_TIMEOUT); /** * @brief Read incoming data to buffer. @@ -68,7 +68,7 @@ namespace sta * @param buffer The buffer of data to write the received data to. * @param size The number of bytes to receive from the peripheral. */ - virtual bool readMemory(uint8_t regAddr, uint8_t * buffer, size_t size, uint32_t timeout = STA_MAX_TIMEOUT) = 0; + virtual bool readMemory(uint8_t regAddr, uint8_t * buffer, size_t size, uint32_t timeout = STA_MAX_TIMEOUT); /** * @brief Send byte value repeatedly. diff --git a/include/sta/bus/spi/spi.hpp b/include/sta/bus/spi/spi.hpp index 6d30450..4321d02 100644 --- a/include/sta/bus/spi/spi.hpp +++ b/include/sta/bus/spi/spi.hpp @@ -40,24 +40,6 @@ namespace sta */ virtual bool transfer(const uint8_t * txBuffer, uint8_t * rxBuffer, size_t size, uint32_t timeout = STA_MAX_TIMEOUT) = 0; - /** - * @brief Write data to a specific memory address of the peripheral. - * - * @param regAddr The memory address. - * @param buffer The buffer of data to write to the address - * @param size The number of bytes to write to the peripheral. - */ - bool writeMemory(uint8_t regAddr, const uint8_t * buffer, size_t size, uint32_t timeout = STA_MAX_TIMEOUT) override; - - /** - * @brief Read data from a specific memory address of the peripheral. - * - * @param regAddr The memory address. - * @param buffer The buffer of data to write the received data to. - * @param size The number of bytes to receive from the peripheral. - */ - bool readMemory(uint8_t regAddr, uint8_t * buffer, size_t size, uint32_t timeout = STA_MAX_TIMEOUT) override; - /** * @brief Get %SPI interface settings. * diff --git a/src/bus/interface.cpp b/src/bus/interface.cpp index 69c1900..8721fe6 100644 --- a/src/bus/interface.cpp +++ b/src/bus/interface.cpp @@ -10,13 +10,35 @@ namespace sta STA_ASSERT(mutex != nullptr); } - void Interface::acquire() + bool Interface::writeMemory(uint8_t regAddr, const uint8_t * buffer, size_t size, uint32_t timeout /* = STA_MAX_TIMEOUT */) + { + if (!transfer(regAddr, timeout)) + return false; + + if (!transfer(buffer, size, timeout)) + return false; + + return true; + } + + bool Interface::readMemory(uint8_t regAddr, uint8_t * buffer, size_t size, uint32_t timeout /* = STA_MAX_TIMEOUT */) + { + if (!transfer(regAddr, timeout)) + return false; + + if (!receive(buffer, size, timeout)) + return false; + + return true; + } + + void Interface::acquire(uint32_t timeout /* = STA_MAX_TIMEOUT */) { mutex_->acquire(); acquired_ = true; } - void Interface::release() + void Interface::release(uint32_t timeout /* = STA_MAX_TIMEOUT */) { acquired_ = false; mutex_->release(); diff --git a/src/bus/spi/spi.cpp b/src/bus/spi/spi.cpp index ba3c961..012bcf3 100644 --- a/src/bus/spi/spi.cpp +++ b/src/bus/spi/spi.cpp @@ -10,28 +10,6 @@ namespace sta } - bool SPI::writeMemory(uint8_t regAddr, const uint8_t * buffer, size_t size, uint32_t timeout /* = STA_MAX_TIMEOUT */) - { - if (!transfer(regAddr, timeout)) - return false; - - if (!transfer(buffer, size, timeout)) - return false; - - return true; - } - - bool SPI::readMemory(uint8_t regAddr, uint8_t * buffer, size_t size, uint32_t timeout /* = STA_MAX_TIMEOUT */) - { - if (!transfer(regAddr, timeout)) - return false; - - if (!receive(buffer, size, timeout)) - return false; - - return true; - } - const SPISettings & SPI::settings() { return settings_; diff --git a/src/devices/stm32/bus/i2c.cpp b/src/devices/stm32/bus/i2c.cpp index 6469bd6..0e99833 100644 --- a/src/devices/stm32/bus/i2c.cpp +++ b/src/devices/stm32/bus/i2c.cpp @@ -77,10 +77,17 @@ namespace sta bool STM32I2C::writeMemory(uint8_t regAddr, const uint8_t * buffer, size_t size, uint32_t timeout /* = STA_MAX_TIMEOUT */) { + /* + * 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_) { - return HAL_I2C_Mem_Write(handle_, address_, regAddr, 1, buffer, size, timeout) == HAL_OK; + return HAL_I2C_Mem_Write(handle_, address_, regAddr, 1, temp_buffer, size, timeout) == HAL_OK; } else { - return HAL_I2C_Mem_Write_IT(handle_, address_, regAddr, 1, buffer, size) == HAL_OK; + return HAL_I2C_Mem_Write_IT(handle_, address_, regAddr, 1, temp_buffer, size) == HAL_OK; } }