56 lines
1.5 KiB
C++

#include <sta/devices/stm32/bus/uart.hpp>
#ifdef STA_STM32_UART_ENABLED
#include <sta/debug/assert.hpp>
#include <cstring>
namespace sta
{
STM32UART::STM32UART(UART_HandleTypeDef * handle, UARTSettings & settings, Mutex * mutex)
: UART{settings, mutex}, handle_{handle}
{
STA_ASSERT(handle != nullptr);
}
bool STM32UART::transfer(uint8_t value, uint32_t timeout /* = STA_MAX_TIMEOUT */)
{
return HAL_UART_Transmit(handle_, &value, 1, timeout) != HAL_OK;
}
bool STM32UART::transfer16(uint16_t value, uint32_t timeout /* = STA_MAX_TIMEOUT */)
{
return HAL_UART_Transmit(handle_, reinterpret_cast<uint8_t *>(&value), 2, timeout) == HAL_OK;
}
bool STM32UART::transfer(const uint8_t * buffer, size_t size, uint32_t timeout /* = STA_MAX_TIMEOUT */)
{
STA_ASSERT(buffer != nullptr);
return HAL_UART_Transmit(handle_, const_cast<uint8_t *>(buffer), size, timeout) == HAL_OK;
}
bool STM32UART::receive(uint8_t * buffer, size_t size, uint32_t timeout /* = STA_MAX_TIMEOUT */)
{
STA_ASSERT(buffer != nullptr);
return HAL_UART_Receive(handle_, buffer, size, timeout) == HAL_OK;
}
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.
bool rslt = transfer(buffer, count, timeout);
delete [] buffer;
return rslt;
}
} // namespace sta
#endif // STA_STM32_UART_ENABLED