Added timeout to bus communication and added error handling via return values

This commit is contained in:
dario
2024-11-03 18:22:34 +01:00
parent f3dfb687f2
commit 8116a74b31
14 changed files with 207 additions and 103 deletions

View File

@@ -35,14 +35,14 @@ namespace sta
*
* @param value 8-bit value
*/
void transfer(uint8_t value);
bool transfer(uint8_t value, uint32_t timeout = STA_MAX_TIMEOUT);
/**
* @brief Send two bytes of data.
*
* @param value 16-bit value
*/
void transfer16(uint16_t value);
bool transfer16(uint16_t value, uint32_t timeout = STA_MAX_TIMEOUT);
/**
* @brief Send data from buffer.
@@ -50,7 +50,16 @@ namespace sta
* @param buffer Source buffer
* @param size Number of bytes to transfer
*/
void transfer(const uint8_t * buffer, size_t size);
bool transfer(const uint8_t * buffer, size_t size, uint32_t timeout = STA_MAX_TIMEOUT);
/**
* @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);
/**
* @brief Read incoming data to buffer.
@@ -58,7 +67,16 @@ namespace sta
* @param buffer Destination buffer
* @param size Number of bytes to read
*/
void receive(uint8_t * buffer, size_t size);
bool receive(uint8_t * buffer, size_t size, uint32_t timeout = STA_MAX_TIMEOUT);
/**
* @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);
/**
* @brief Send byte value repeatedly.
@@ -66,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);
void fill(uint8_t value, size_t count, uint32_t timeout = STA_MAX_TIMEOUT);
protected:
/**
@@ -78,7 +96,6 @@ namespace sta
* @brief Deactivate device..
*/
virtual void deselect() = 0;
private:
Interface * intf_;
bool selected_ = false;

View File

@@ -35,7 +35,4 @@ namespace sta
} // namespace sta
#endif // STA_CORE_I2C_DEVICE_HPP

View File

@@ -6,6 +6,11 @@
#include <cstdint>
#include <cstddef>
/**
* @brief The maximum timeout for bus communication in sta-core.
*/
#define STA_MAX_TIMEOUT 0xFFFFFFFFU
namespace sta
{
/**
@@ -24,27 +29,46 @@ namespace sta
*
* @param value 8-bit value
*/
virtual void transfer(uint8_t value) = 0;
virtual bool transfer(uint8_t value, uint32_t timeout = STA_MAX_TIMEOUT) = 0;
/**
* @brief Send two bytes of data.
*
* @param value 16-bit value
*/
virtual void transfer16(uint16_t value) = 0;
virtual bool transfer16(uint16_t value, uint32_t timeout = STA_MAX_TIMEOUT) = 0;
/**
* @brief Send data from buffer.
*
* @param buffer Source buffer
* @param size Number of bytes to transfer
*/
virtual void transfer(const uint8_t * buffer, size_t size) = 0;
virtual bool transfer(const uint8_t * buffer, 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.
*/
virtual bool writeMemory(uint8_t regAddr, const uint8_t * buffer, size_t size, uint32_t timeout = STA_MAX_TIMEOUT) = 0;
/**
* @brief Read incoming data to buffer.
*
* @param buffer Destination buffer
* @param size Number of bytes to read
*/
virtual void receive(uint8_t * buffer, size_t size) = 0;
virtual bool receive(uint8_t * buffer, size_t size, uint32_t timeout = STA_MAX_TIMEOUT) = 0;
/**
* @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.
*/
virtual bool readMemory(uint8_t regAddr, uint8_t * buffer, size_t size, uint32_t timeout = STA_MAX_TIMEOUT) = 0;
/**
* @brief Send byte value repeatedly.
@@ -52,25 +76,26 @@ namespace sta
* @param value 8-bit value to repeat
* @param count Number of repetitions
*/
virtual void fill(uint8_t value, size_t count) = 0;
virtual bool fill(uint8_t value, size_t count, uint32_t timeout = STA_MAX_TIMEOUT) = 0;
/**
* @brief Acquire usage rights to use the interface.
*
* Must be called before any I/O operations are executed.
*/
virtual void acquire();
virtual void acquire(uint32_t timeout = STA_MAX_TIMEOUT);
/**
* @brief Release usage rights for interface.
*
* Must be called after last I/O operation.
*/
virtual void release();
virtual void release(uint32_t timeout = STA_MAX_TIMEOUT);
/**
* @returns true if the interface has been aquired.
*/
bool isAcquired();
private:
Mutex * mutex_;
bool acquired_ = false;

View File

@@ -30,6 +30,7 @@ namespace sta
SPIDevice(SPI * intf, GpioPin * csPin);
using Device::transfer;
/**
* @brief Send and receive data simultaneously.
*
@@ -37,7 +38,7 @@ namespace sta
* @param rxBuffer Receive buffer
* @param size Number of bytes to transfer
*/
void transfer(const uint8_t * txBuffer, uint8_t * rxBuffer, size_t size);
bool transfer(const uint8_t * txBuffer, uint8_t * rxBuffer, size_t size, uint32_t timeout = STA_MAX_TIMEOUT);
/**
* @brief Get %SPI interface settings.

View File

@@ -38,7 +38,25 @@ namespace sta
* @param rxBuffer Receive buffer
* @param size Number of bytes to transfer
*/
virtual void transfer(const uint8_t * txBuffer, uint8_t * rxBuffer, size_t size) = 0;
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.
@@ -46,7 +64,6 @@ namespace sta
* @return %SPI settings
*/
const SPISettings & settings();
private:
SPISettings settings_; /**< %SPI settings */
};

View File

@@ -31,7 +31,6 @@ namespace sta
void fill(uint8_t value, size_t count) override;
private:
I2C_HandleTypeDef * handle_;
const uint32_t timeout_ = 1000;
};
class STM32I2CDevice : public I2CDevice

View File

@@ -72,13 +72,12 @@ namespace sta
*/
STM32SPI(const Info & info, 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;
bool transfer(uint8_t value, uint32_t timeout = STA_MAX_TIMEOUT) override;
bool transfer16(uint16_t value, uint32_t timeout = STA_MAX_TIMEOUT) override;
bool transfer(const uint8_t * buffer, size_t size, uint32_t timeout = STA_MAX_TIMEOUT) override;
bool transfer(const uint8_t * txBuffer, uint8_t * rxBuffer, size_t size, uint32_t timeout = STA_MAX_TIMEOUT) override;
bool receive(uint8_t * buffer, size_t size, uint32_t timeout = STA_MAX_TIMEOUT) override;
bool fill(uint8_t value, size_t count, uint32_t timeout = STA_MAX_TIMEOUT) override;
private:
SPI_HandleTypeDef * handle_; /**< STM32 HAL handle */

View File

@@ -45,12 +45,11 @@ namespace sta
*/
STM32UART(UART_HandleTypeDef * handle, UARTSettings & settings, Mutex * mutex);
void transfer(uint8_t value) override;
void transfer16(uint16_t value) override;
void transfer(const uint8_t * buffer, size_t size) override;
void receive(uint8_t * buffer, size_t size) override;
void fill(uint8_t value, size_t count) override;
bool transfer(uint8_t value, uint32_t timeout = STA_MAX_TIMEOUT) override;
bool transfer16(uint16_t value, uint32_t timeout = STA_MAX_TIMEOUT) override;
bool transfer(const uint8_t * buffer, size_t size, uint32_t timeout = STA_MAX_TIMEOUT) override;
bool receive(uint8_t * buffer, size_t size, uint32_t timeout = STA_MAX_TIMEOUT) override;
bool fill(uint8_t value, size_t count, uint32_t timeout = STA_MAX_TIMEOUT) override;
private:
UART_HandleTypeDef * handle_; /**< STM32 HAL handle */
};