mirror of
https://git.intern.spaceteamaachen.de/ALPAKA/sta-core.git
synced 2025-06-10 16:55:58 +00:00
Added timeout to bus communication and added error handling via return values
This commit is contained in:
parent
53567724f1
commit
bbb8f3505e
@ -35,14 +35,14 @@ namespace sta
|
|||||||
*
|
*
|
||||||
* @param value 8-bit value
|
* @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.
|
* @brief Send two bytes of data.
|
||||||
*
|
*
|
||||||
* @param value 16-bit value
|
* @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.
|
* @brief Send data from buffer.
|
||||||
@ -50,7 +50,16 @@ namespace sta
|
|||||||
* @param buffer Source buffer
|
* @param buffer Source buffer
|
||||||
* @param size Number of bytes to transfer
|
* @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.
|
* @brief Read incoming data to buffer.
|
||||||
@ -58,7 +67,16 @@ namespace sta
|
|||||||
* @param buffer Destination buffer
|
* @param buffer Destination buffer
|
||||||
* @param size Number of bytes to read
|
* @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.
|
* @brief Send byte value repeatedly.
|
||||||
@ -66,7 +84,7 @@ namespace sta
|
|||||||
* @param value 8-bit value to repeat
|
* @param value 8-bit value to repeat
|
||||||
* @param count Number of repetitions
|
* @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:
|
protected:
|
||||||
/**
|
/**
|
||||||
@ -78,7 +96,6 @@ namespace sta
|
|||||||
* @brief Deactivate device..
|
* @brief Deactivate device..
|
||||||
*/
|
*/
|
||||||
virtual void deselect() = 0;
|
virtual void deselect() = 0;
|
||||||
|
|
||||||
private:
|
private:
|
||||||
Interface * intf_;
|
Interface * intf_;
|
||||||
bool selected_ = false;
|
bool selected_ = false;
|
||||||
|
@ -35,7 +35,4 @@ namespace sta
|
|||||||
|
|
||||||
} // namespace sta
|
} // namespace sta
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
#endif // STA_CORE_I2C_DEVICE_HPP
|
#endif // STA_CORE_I2C_DEVICE_HPP
|
||||||
|
@ -6,6 +6,11 @@
|
|||||||
#include <cstdint>
|
#include <cstdint>
|
||||||
#include <cstddef>
|
#include <cstddef>
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @brief The maximum timeout for bus communication in sta-core.
|
||||||
|
*/
|
||||||
|
#define STA_MAX_TIMEOUT 0xFFFFFFFFU
|
||||||
|
|
||||||
namespace sta
|
namespace sta
|
||||||
{
|
{
|
||||||
/**
|
/**
|
||||||
@ -24,27 +29,46 @@ namespace sta
|
|||||||
*
|
*
|
||||||
* @param value 8-bit value
|
* @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.
|
* @brief Send two bytes of data.
|
||||||
*
|
*
|
||||||
* @param value 16-bit value
|
* @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.
|
* @brief Send data from buffer.
|
||||||
*
|
*
|
||||||
* @param buffer Source buffer
|
* @param buffer Source buffer
|
||||||
* @param size Number of bytes to transfer
|
* @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.
|
* @brief Read incoming data to buffer.
|
||||||
*
|
*
|
||||||
* @param buffer Destination buffer
|
* @param buffer Destination buffer
|
||||||
* @param size Number of bytes to read
|
* @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.
|
* @brief Send byte value repeatedly.
|
||||||
@ -52,25 +76,26 @@ namespace sta
|
|||||||
* @param value 8-bit value to repeat
|
* @param value 8-bit value to repeat
|
||||||
* @param count Number of repetitions
|
* @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.
|
* @brief Acquire usage rights to use the interface.
|
||||||
*
|
*
|
||||||
* Must be called before any I/O operations are executed.
|
* 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.
|
* @brief Release usage rights for interface.
|
||||||
*
|
*
|
||||||
* Must be called after last I/O operation.
|
* 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.
|
* @returns true if the interface has been aquired.
|
||||||
*/
|
*/
|
||||||
bool isAcquired();
|
bool isAcquired();
|
||||||
|
|
||||||
private:
|
private:
|
||||||
Mutex * mutex_;
|
Mutex * mutex_;
|
||||||
bool acquired_ = false;
|
bool acquired_ = false;
|
||||||
|
@ -30,6 +30,7 @@ namespace sta
|
|||||||
SPIDevice(SPI * intf, GpioPin * csPin);
|
SPIDevice(SPI * intf, GpioPin * csPin);
|
||||||
|
|
||||||
using Device::transfer;
|
using Device::transfer;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @brief Send and receive data simultaneously.
|
* @brief Send and receive data simultaneously.
|
||||||
*
|
*
|
||||||
@ -37,7 +38,7 @@ namespace sta
|
|||||||
* @param rxBuffer Receive buffer
|
* @param rxBuffer Receive buffer
|
||||||
* @param size Number of bytes to transfer
|
* @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.
|
* @brief Get %SPI interface settings.
|
||||||
|
@ -38,7 +38,25 @@ namespace sta
|
|||||||
* @param rxBuffer Receive buffer
|
* @param rxBuffer Receive buffer
|
||||||
* @param size Number of bytes to transfer
|
* @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.
|
* @brief Get %SPI interface settings.
|
||||||
@ -46,7 +64,6 @@ namespace sta
|
|||||||
* @return %SPI settings
|
* @return %SPI settings
|
||||||
*/
|
*/
|
||||||
const SPISettings & settings();
|
const SPISettings & settings();
|
||||||
|
|
||||||
private:
|
private:
|
||||||
SPISettings settings_; /**< %SPI settings */
|
SPISettings settings_; /**< %SPI settings */
|
||||||
};
|
};
|
||||||
|
@ -21,15 +21,13 @@ namespace sta
|
|||||||
public:
|
public:
|
||||||
STM32I2C(I2C_HandleTypeDef * handle, Mutex * mutex=nullptr);
|
STM32I2C(I2C_HandleTypeDef * handle, Mutex * mutex=nullptr);
|
||||||
|
|
||||||
void transfer(uint8_t value) override;
|
bool transfer(uint8_t value, uint32_t timeout = STA_MAX_TIMEOUT) override;
|
||||||
void transfer16(uint16_t value) override;
|
bool transfer16(uint16_t value, uint32_t timeout = STA_MAX_TIMEOUT) override;
|
||||||
void transfer(const uint8_t * buffer, size_t size) override;
|
bool transfer(const uint8_t * buffer, size_t size, uint32_t timeout = STA_MAX_TIMEOUT) override;
|
||||||
void receive(uint8_t * buffer, size_t size) 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;
|
||||||
void fill(uint8_t value, size_t count) override;
|
|
||||||
private:
|
private:
|
||||||
I2C_HandleTypeDef * handle_;
|
I2C_HandleTypeDef * handle_;
|
||||||
const uint32_t timeout_ = 1000;
|
|
||||||
};
|
};
|
||||||
|
|
||||||
class STM32I2CDevice : public I2CDevice
|
class STM32I2CDevice : public I2CDevice
|
||||||
|
@ -72,13 +72,12 @@ namespace sta
|
|||||||
*/
|
*/
|
||||||
STM32SPI(const Info & info, Mutex * mutex = nullptr);
|
STM32SPI(const Info & info, Mutex * mutex = nullptr);
|
||||||
|
|
||||||
void transfer(uint8_t value) override;
|
bool transfer(uint8_t value, uint32_t timeout = STA_MAX_TIMEOUT) override;
|
||||||
void transfer16(uint16_t value) override;
|
bool transfer16(uint16_t value, uint32_t timeout = STA_MAX_TIMEOUT) override;
|
||||||
void transfer(const uint8_t * buffer, size_t size) override;
|
bool transfer(const uint8_t * buffer, size_t size, uint32_t timeout = STA_MAX_TIMEOUT) override;
|
||||||
void transfer(const uint8_t * txBuffer, uint8_t * rxBuffer, size_t size) override;
|
bool transfer(const uint8_t * txBuffer, uint8_t * rxBuffer, size_t size, uint32_t timeout = STA_MAX_TIMEOUT) override;
|
||||||
void receive(uint8_t * buffer, size_t size) 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;
|
||||||
void fill(uint8_t value, size_t count) override;
|
|
||||||
|
|
||||||
private:
|
private:
|
||||||
SPI_HandleTypeDef * handle_; /**< STM32 HAL handle */
|
SPI_HandleTypeDef * handle_; /**< STM32 HAL handle */
|
||||||
|
@ -45,12 +45,11 @@ namespace sta
|
|||||||
*/
|
*/
|
||||||
STM32UART(UART_HandleTypeDef * handle, UARTSettings & settings, Mutex * mutex);
|
STM32UART(UART_HandleTypeDef * handle, UARTSettings & settings, Mutex * mutex);
|
||||||
|
|
||||||
void transfer(uint8_t value) override;
|
bool transfer(uint8_t value, uint32_t timeout = STA_MAX_TIMEOUT) override;
|
||||||
void transfer16(uint16_t value) override;
|
bool transfer16(uint16_t value, uint32_t timeout = STA_MAX_TIMEOUT) override;
|
||||||
void transfer(const uint8_t * buffer, size_t size) override;
|
bool transfer(const uint8_t * buffer, size_t size, uint32_t timeout = STA_MAX_TIMEOUT) override;
|
||||||
void receive(uint8_t * buffer, size_t size) 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;
|
||||||
void fill(uint8_t value, size_t count) override;
|
|
||||||
private:
|
private:
|
||||||
UART_HandleTypeDef * handle_; /**< STM32 HAL handle */
|
UART_HandleTypeDef * handle_; /**< STM32 HAL handle */
|
||||||
};
|
};
|
||||||
|
@ -23,45 +23,78 @@ namespace sta
|
|||||||
intf_->release();
|
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(intf_->isAcquired());
|
||||||
STA_ASSERT(selected_);
|
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(intf_->isAcquired());
|
||||||
STA_ASSERT(selected_);
|
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(intf_->isAcquired());
|
||||||
STA_ASSERT(selected_);
|
STA_ASSERT(selected_);
|
||||||
|
|
||||||
|
// Check if the user passed a null pointer as a buffer.
|
||||||
STA_ASSERT(buffer != nullptr);
|
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(intf_->isAcquired());
|
||||||
STA_ASSERT(selected_);
|
STA_ASSERT(selected_);
|
||||||
|
|
||||||
|
// Check if the user passed a null pointer as a buffer.
|
||||||
STA_ASSERT(buffer != nullptr);
|
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(intf_->isAcquired());
|
||||||
STA_ASSERT(selected_);
|
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
|
} // namespace sta
|
||||||
|
@ -12,13 +12,16 @@ namespace sta
|
|||||||
STA_ASSERT(csPin != nullptr);
|
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(txBuffer != nullptr);
|
||||||
STA_ASSERT(rxBuffer != nullptr);
|
STA_ASSERT(rxBuffer != nullptr);
|
||||||
|
|
||||||
intf_->transfer(txBuffer, rxBuffer, size);
|
intf_->transfer(txBuffer, rxBuffer, size, timeout);
|
||||||
}
|
}
|
||||||
|
|
||||||
const SPISettings & SPIDevice::settings() const
|
const SPISettings & SPIDevice::settings() const
|
||||||
@ -28,11 +31,11 @@ namespace sta
|
|||||||
|
|
||||||
void SPIDevice::select()
|
void SPIDevice::select()
|
||||||
{
|
{
|
||||||
csPin_->setState(GpioPinState::GPIO_LOW);
|
csPin_->setLow();
|
||||||
}
|
}
|
||||||
|
|
||||||
void SPIDevice::deselect()
|
void SPIDevice::deselect()
|
||||||
{
|
{
|
||||||
csPin_->setState(GpioPinState::GPIO_HIGH);
|
csPin_->setHigh();
|
||||||
}
|
}
|
||||||
} // namespace sta
|
} // namespace sta
|
||||||
|
@ -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()
|
const SPISettings & SPI::settings()
|
||||||
{
|
{
|
||||||
return settings_;
|
return settings_;
|
||||||
|
@ -13,45 +13,37 @@ namespace sta
|
|||||||
STA_ASSERT(handle != nullptr);
|
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_)
|
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 {
|
} 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 (blocking_)
|
||||||
{
|
{
|
||||||
if (master_)
|
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 {
|
} 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 {
|
} else {
|
||||||
if (master_)
|
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 {
|
} 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;
|
HAL_StatusTypeDef res;
|
||||||
|
|
||||||
@ -66,9 +58,9 @@ namespace sta
|
|||||||
{
|
{
|
||||||
if (master_)
|
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 {
|
} else {
|
||||||
res = HAL_I2C_Slave_Transmit(handle_, temp_buffer, size, timeout_);
|
res = HAL_I2C_Slave_Transmit(handle_, temp_buffer, size, timeout);
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
if (master_) {
|
if (master_) {
|
||||||
@ -80,40 +72,39 @@ namespace sta
|
|||||||
|
|
||||||
delete [] temp_buffer;
|
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 (blocking_) {
|
||||||
if (master_) {
|
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 {
|
} else {
|
||||||
res = HAL_I2C_Slave_Receive(handle_, buffer, size, timeout_);
|
return HAL_I2C_Slave_Receive(handle_, buffer, size, timeout) == HAL_OK;
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
if (master_) {
|
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 {
|
} 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.
|
// Initialize a buffer of size count and fill it with the value.
|
||||||
uint8_t *buffer = new uint8_t[count];
|
uint8_t *buffer = new uint8_t[count];
|
||||||
memset(buffer, value, count);
|
memset(buffer, value, count);
|
||||||
|
|
||||||
// Transfer the buffer via the bus.
|
// Transfer the buffer via the bus.
|
||||||
transfer(buffer, count);
|
bool rslt = transfer(buffer, count, timeout);
|
||||||
|
|
||||||
delete [] buffer;
|
delete [] buffer;
|
||||||
|
|
||||||
|
return rslt;
|
||||||
}
|
}
|
||||||
|
|
||||||
STM32I2CDevice::STM32I2CDevice(STM32I2C * intf, int address, bool master, bool blocking)
|
STM32I2CDevice::STM32I2CDevice(STM32I2C * intf, int address, bool master, bool blocking)
|
||||||
|
@ -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)
|
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
|
else
|
||||||
{
|
{
|
||||||
// Required since tx buffer is cast to uint16_t * internally
|
// Required since tx buffer is cast to uint16_t * internally
|
||||||
uint16_t dummy = value;
|
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;
|
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(buffer != nullptr);
|
||||||
STA_ASSERT(size != 0);
|
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(txBuffer != nullptr);
|
||||||
STA_ASSERT(rxBuffer != nullptr);
|
STA_ASSERT(rxBuffer != nullptr);
|
||||||
STA_ASSERT(size != 0);
|
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);
|
STA_ASSERT(buffer != nullptr);
|
||||||
|
|
||||||
HAL_SPI_Receive(handle_, buffer, size, HAL_MAX_DELAY);
|
return HAL_SPI_Receive(handle_, buffer, size, timeout) == HAL_OK;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
bool STM32SPI::fill(uint8_t value, size_t count, uint32_t timeout /* = STA_MAX_TIMEOUT */)
|
||||||
void STM32SPI::fill(uint8_t value, size_t count)
|
|
||||||
{
|
{
|
||||||
STA_ASSERT(count != 0);
|
STA_ASSERT(count != 0);
|
||||||
|
|
||||||
@ -143,7 +142,7 @@ namespace sta
|
|||||||
{
|
{
|
||||||
for (size_t i = 0; i < count; ++i)
|
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
|
else
|
||||||
@ -152,7 +151,7 @@ namespace sta
|
|||||||
uint16_t dummy = value;
|
uint16_t dummy = value;
|
||||||
for (size_t i = 0; i < count; ++i)
|
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;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -12,40 +12,42 @@ namespace sta
|
|||||||
STA_ASSERT(handle != nullptr);
|
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);
|
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);
|
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.
|
// Initialize a buffer of size count and fill it with the value.
|
||||||
uint8_t *buffer = new uint8_t[count];
|
uint8_t *buffer = new uint8_t[count];
|
||||||
memset(buffer, value, count);
|
memset(buffer, value, count);
|
||||||
|
|
||||||
// Transfer the buffer via the bus.
|
// Transfer the buffer via the bus.
|
||||||
transfer(buffer, count);
|
bool rslt = transfer(buffer, count, timeout);
|
||||||
|
|
||||||
delete [] buffer;
|
delete [] buffer;
|
||||||
|
|
||||||
|
return rslt;
|
||||||
}
|
}
|
||||||
} // namespace sta
|
} // namespace sta
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user