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 53567724f1
commit bbb8f3505e
14 changed files with 212 additions and 109 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

@ -21,15 +21,13 @@ namespace sta
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 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:
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 */
};

View File

@ -23,45 +23,78 @@ namespace sta
intf_->release();
}
void Device::transfer(uint8_t value)
bool Device::transfer(uint8_t value, uint32_t timeout /* = STA_MAX_TIMEOUT */)
{
// If these asserts fail, the user either forgot to call _beginTransmission()_ or the mutex acquision failed.
STA_ASSERT(intf_->isAcquired());
STA_ASSERT(selected_);
intf_->transfer(value);
return intf_->transfer(value, timeout);
}
void Device::transfer16(uint16_t value)
bool Device::transfer16(uint16_t value, uint32_t timeout /* = STA_MAX_TIMEOUT */)
{
// If these asserts fail, the user either forgot to call _beginTransmission()_ or the mutex acquision failed.
STA_ASSERT(intf_->isAcquired());
STA_ASSERT(selected_);
intf_->transfer16(value);
return intf_->transfer16(value, timeout);
}
void Device::transfer(const uint8_t * buffer, size_t size)
bool Device::transfer(const uint8_t * buffer, size_t size, uint32_t timeout /* = STA_MAX_TIMEOUT */)
{
// If these asserts fail, the user either forgot to call _beginTransmission()_ or the mutex acquision failed.
STA_ASSERT(intf_->isAcquired());
STA_ASSERT(selected_);
// Check if the user passed a null pointer as a buffer.
STA_ASSERT(buffer != nullptr);
intf_->transfer(buffer, size);
return intf_->transfer(buffer, size, timeout);
}
void Device::receive(uint8_t * buffer, size_t size)
bool Device::writeMemory(uint8_t regAddr, const uint8_t * buffer, size_t size, uint32_t timeout /* = STA_MAX_TIMEOUT */)
{
// If these asserts fail, the user either forgot to call _beginTransmission()_ or the mutex acquision failed.
STA_ASSERT(intf_->isAcquired());
STA_ASSERT(selected_);
// Check if the user passed a null pointer as a buffer.
STA_ASSERT(buffer != nullptr);
intf_->receive(buffer, size);
return intf_->writeMemory(regAddr, buffer, size, timeout);
}
void Device::fill(uint8_t value, size_t count)
bool Device::receive(uint8_t * buffer, size_t size, uint32_t timeout /* = STA_MAX_TIMEOUT */)
{
// If these asserts fail, the user either forgot to call _beginTransmission()_ or the mutex acquision failed.
STA_ASSERT(intf_->isAcquired());
STA_ASSERT(selected_);
intf_->fill(value, count);
// Check if the user passed a null pointer as a buffer.
STA_ASSERT(buffer != nullptr);
return intf_->receive(buffer, size, timeout);
}
bool Device::readMemory(uint8_t regAddr, uint8_t * buffer, size_t size, uint32_t timeout /* = STA_MAX_TIMEOUT */)
{
// If these asserts fail, the user either forgot to call _beginTransmission()_ or the mutex acquision failed.
STA_ASSERT(intf_->isAcquired());
STA_ASSERT(selected_);
// Check if the user passed a null pointer as a buffer.
STA_ASSERT(buffer != nullptr);
return intf_->readMemory(regAddr, buffer, size, timeout);
}
bool Device::fill(uint8_t value, size_t count, uint32_t timeout /* = STA_MAX_TIMEOUT */)
{
// If these asserts fail, the user either forgot to call _beginTransmission()_ or the mutex acquision failed.
STA_ASSERT(intf_->isAcquired());
STA_ASSERT(selected_);
intf_->fill(value, count, timeout);
}
} // namespace sta

View File

@ -12,13 +12,16 @@ namespace sta
STA_ASSERT(csPin != nullptr);
}
void SPIDevice::transfer(const uint8_t * txBuffer, uint8_t * rxBuffer, size_t size)
bool SPIDevice::transfer(const uint8_t * txBuffer, uint8_t * rxBuffer, size_t size, uint32_t timeout /* = STA_MAX_TIMEOUT */)
{
STA_ASSERT(intf_->isAcquired());
// If these asserts fail, the user either forgot to call _beginTransmission()_ or the mutex acquision failed.
STA_ASSERT(intf_->isAcquired());
// Check if the user passed a null pointer as a buffer.
STA_ASSERT(txBuffer != nullptr);
STA_ASSERT(rxBuffer != nullptr);
intf_->transfer(txBuffer, rxBuffer, size);
intf_->transfer(txBuffer, rxBuffer, size, timeout);
}
const SPISettings & SPIDevice::settings() const
@ -28,11 +31,11 @@ namespace sta
void SPIDevice::select()
{
csPin_->setState(GpioPinState::GPIO_LOW);
csPin_->setLow();
}
void SPIDevice::deselect()
{
csPin_->setState(GpioPinState::GPIO_HIGH);
csPin_->setHigh();
}
} // namespace sta

View File

@ -10,6 +10,28 @@ 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_;

View File

@ -13,45 +13,37 @@ namespace sta
STA_ASSERT(handle != nullptr);
}
void STM32I2C::transfer(uint8_t value)
bool STM32I2C::transfer(uint8_t value, uint32_t timeout /* = STA_MAX_TIMEOUT */)
{
HAL_StatusTypeDef res;
if (master_)
{
res = HAL_I2C_Master_Transmit(handle_, address_, &value, 1, timeout_);
return HAL_I2C_Master_Transmit(handle_, address_, &value, 1, timeout) == HAL_OK;
} else {
res = HAL_I2C_Slave_Transmit(handle_, &value, 1, timeout_);
return HAL_I2C_Slave_Transmit(handle_, &value, 1, timeout) == HAL_OK;
}
STA_ASSERT(res == HAL_OK);
}
void STM32I2C::transfer16(uint16_t value)
bool STM32I2C::transfer16(uint16_t value, uint32_t timeout /* = STA_MAX_TIMEOUT */)
{
HAL_StatusTypeDef res;
if (blocking_)
{
if (master_)
{
res = HAL_I2C_Master_Transmit(handle_, address_, reinterpret_cast<uint8_t *>(&value), 2, timeout_);
return HAL_I2C_Master_Transmit(handle_, address_, reinterpret_cast<uint8_t *>(&value), 2, timeout) == HAL_OK;
} else {
res = HAL_I2C_Slave_Transmit(handle_, reinterpret_cast<uint8_t *>(&value), 2, timeout_);
return HAL_I2C_Slave_Transmit(handle_, reinterpret_cast<uint8_t *>(&value), 2, timeout) == HAL_OK;
}
} else {
if (master_)
{
res = HAL_I2C_Slave_Transmit_IT(handle_, reinterpret_cast<uint8_t *>(&value), 2);
return HAL_I2C_Slave_Transmit_IT(handle_, reinterpret_cast<uint8_t *>(&value), 2) == HAL_OK;
} else {
res = HAL_I2C_Slave_Transmit_IT(handle_, reinterpret_cast<uint8_t *>(&value), 2);
return HAL_I2C_Slave_Transmit_IT(handle_, reinterpret_cast<uint8_t *>(&value), 2) == HAL_OK;
}
}
STA_ASSERT(res == HAL_OK);
}
void STM32I2C::transfer(const uint8_t * buffer, size_t size)
bool STM32I2C::transfer(const uint8_t * buffer, size_t size, uint32_t timeout /* = STA_MAX_TIMEOUT */)
{
HAL_StatusTypeDef res;
@ -66,9 +58,9 @@ namespace sta
{
if (master_)
{
res = HAL_I2C_Master_Transmit(handle_, address_, temp_buffer, size, timeout_);
res = HAL_I2C_Master_Transmit(handle_, address_, temp_buffer, size, timeout);
} else {
res = HAL_I2C_Slave_Transmit(handle_, temp_buffer, size, timeout_);
res = HAL_I2C_Slave_Transmit(handle_, temp_buffer, size, timeout);
}
} else {
if (master_) {
@ -80,40 +72,39 @@ namespace sta
delete [] temp_buffer;
STA_ASSERT(res == HAL_OK);
return res == HAL_OK;
}
void STM32I2C::receive(uint8_t * buffer, size_t size)
bool STM32I2C::receive(uint8_t * buffer, size_t size, uint32_t timeout /* = STA_MAX_TIMEOUT */)
{
HAL_StatusTypeDef res;
if (blocking_) {
if (master_) {
res = HAL_I2C_Master_Receive(handle_, address_, buffer, size, timeout_);
return HAL_I2C_Master_Receive(handle_, address_, buffer, size, timeout) == HAL_OK;
} else {
res = HAL_I2C_Slave_Receive(handle_, buffer, size, timeout_);
return HAL_I2C_Slave_Receive(handle_, buffer, size, timeout) == HAL_OK;
}
} else {
if (master_) {
res = HAL_I2C_Master_Receive_IT(handle_, address_, buffer, size);
return HAL_I2C_Master_Receive_IT(handle_, address_, buffer, size) == HAL_OK;
} else {
res = HAL_I2C_Slave_Receive_IT(handle_, buffer, size);
return HAL_I2C_Slave_Receive_IT(handle_, buffer, size) == HAL_OK;
}
}
STA_ASSERT(res == HAL_OK);
}
void STM32I2C::fill(uint8_t value, size_t count)
bool STM32I2C::fill(uint8_t value, size_t count, uint32_t timeout /* = STA_MAX_TIMEOUT */)
{
// 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);
bool rslt = transfer(buffer, count, timeout);
delete [] buffer;
return rslt;
}
STM32I2CDevice::STM32I2CDevice(STM32I2C * intf, int address, bool master, bool blocking)

View File

@ -77,21 +77,21 @@ namespace sta
{}
void STM32SPI::transfer(uint8_t value)
bool STM32SPI::transfer(uint8_t value, uint32_t timeout /* = STA_MAX_TIMEOUT */)
{
if (settings().dataSize == SPIDataSize::SIZE_8)
{
HAL_SPI_Transmit(handle_, &value, 1, HAL_MAX_DELAY);
return HAL_SPI_Transmit(handle_, &value, 1, timeout) == HAL_OK;
}
else
{
// Required since tx buffer is cast to uint16_t * internally
uint16_t dummy = value;
HAL_SPI_Transmit(handle_, reinterpret_cast<uint8_t *>(&dummy), 1, HAL_MAX_DELAY);
return HAL_SPI_Transmit(handle_, reinterpret_cast<uint8_t *>(&dummy), 1, timeout) == HAL_OK;
}
}
void STM32SPI::transfer16(uint16_t value)
bool STM32SPI::transfer16(uint16_t value, uint32_t timeout /* = STA_MAX_TIMEOUT */)
{
uint16_t size = 1;
@ -107,35 +107,34 @@ namespace sta
}
}
HAL_SPI_Transmit(handle_, reinterpret_cast<uint8_t *>(&value), size, HAL_MAX_DELAY);
return HAL_SPI_Transmit(handle_, reinterpret_cast<uint8_t *>(&value), size, timeout) == HAL_OK;
}
void STM32SPI::transfer(const uint8_t * buffer, size_t size)
bool STM32SPI::transfer(const uint8_t * buffer, size_t size, uint32_t timeout /* = STA_MAX_TIMEOUT */)
{
STA_ASSERT(buffer != nullptr);
STA_ASSERT(size != 0);
HAL_SPI_Transmit(handle_, const_cast<uint8_t *>(buffer), size, HAL_MAX_DELAY);
return HAL_SPI_Transmit(handle_, const_cast<uint8_t *>(buffer), size, timeout) == HAL_OK;
}
void STM32SPI::transfer(const uint8_t * txBuffer, uint8_t * rxBuffer, size_t size)
bool STM32SPI::transfer(const uint8_t * txBuffer, uint8_t * rxBuffer, size_t size, uint32_t timeout /* = STA_MAX_TIMEOUT */)
{
STA_ASSERT(txBuffer != nullptr);
STA_ASSERT(rxBuffer != nullptr);
STA_ASSERT(size != 0);
HAL_SPI_TransmitReceive(handle_, const_cast<uint8_t *>(txBuffer), rxBuffer, size, HAL_MAX_DELAY);
return HAL_SPI_TransmitReceive(handle_, const_cast<uint8_t *>(txBuffer), rxBuffer, size, timeout) == HAL_OK;
}
void STM32SPI::receive(uint8_t * buffer, size_t size)
bool STM32SPI::receive(uint8_t * buffer, size_t size, uint32_t timeout /* = STA_MAX_TIMEOUT */)
{
STA_ASSERT(buffer != nullptr);
HAL_SPI_Receive(handle_, buffer, size, HAL_MAX_DELAY);
return HAL_SPI_Receive(handle_, buffer, size, timeout) == HAL_OK;
}
void STM32SPI::fill(uint8_t value, size_t count)
bool STM32SPI::fill(uint8_t value, size_t count, uint32_t timeout /* = STA_MAX_TIMEOUT */)
{
STA_ASSERT(count != 0);
@ -143,7 +142,7 @@ namespace sta
{
for (size_t i = 0; i < count; ++i)
{
HAL_SPI_Transmit(handle_, &value, 1, HAL_MAX_DELAY);
return HAL_SPI_Transmit(handle_, &value, 1, timeout) == HAL_OK;
}
}
else
@ -152,7 +151,7 @@ namespace sta
uint16_t dummy = value;
for (size_t i = 0; i < count; ++i)
{
HAL_SPI_Transmit(handle_, reinterpret_cast<uint8_t *>(&dummy), 1, HAL_MAX_DELAY);
return HAL_SPI_Transmit(handle_, reinterpret_cast<uint8_t *>(&dummy), 1, timeout) == HAL_OK;
}
}
}

View File

@ -12,40 +12,42 @@ namespace sta
STA_ASSERT(handle != nullptr);
}
void STM32UART::transfer(uint8_t value)
bool STM32UART::transfer(uint8_t value, uint32_t timeout /* = STA_MAX_TIMEOUT */)
{
HAL_UART_Transmit(handle_, &value, 1, HAL_MAX_DELAY);
return HAL_UART_Transmit(handle_, &value, 1, timeout) != HAL_OK;
}
void STM32UART::transfer16(uint16_t value)
bool STM32UART::transfer16(uint16_t value, uint32_t timeout /* = STA_MAX_TIMEOUT */)
{
HAL_UART_Transmit(handle_, reinterpret_cast<uint8_t *>(&value), 2, HAL_MAX_DELAY);
return HAL_UART_Transmit(handle_, reinterpret_cast<uint8_t *>(&value), 2, timeout) == HAL_OK;
}
void STM32UART::transfer(const uint8_t * buffer, size_t size)
bool STM32UART::transfer(const uint8_t * buffer, size_t size, uint32_t timeout /* = STA_MAX_TIMEOUT */)
{
STA_ASSERT(buffer != nullptr);
HAL_UART_Transmit(handle_, const_cast<uint8_t *>(buffer), size, HAL_MAX_DELAY);
return HAL_UART_Transmit(handle_, const_cast<uint8_t *>(buffer), size, timeout) == HAL_OK;
}
void STM32UART::receive(uint8_t * buffer, size_t size)
bool STM32UART::receive(uint8_t * buffer, size_t size, uint32_t timeout /* = STA_MAX_TIMEOUT */)
{
STA_ASSERT(buffer != nullptr);
HAL_UART_Receive(handle_, buffer, size, HAL_MAX_DELAY);
return HAL_UART_Receive(handle_, buffer, size, timeout) == HAL_OK;
}
void STM32UART::fill(uint8_t value, size_t count)
bool STM32UART::fill(uint8_t value, size_t count, uint32_t timeout /* = STA_MAX_TIMEOUT */)
{
// 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);
bool rslt = transfer(buffer, count, timeout);
delete [] buffer;
return rslt;
}
} // namespace sta