Loading files
This file is pinned to the selected release. Download the original to open it in your design tool.
#include "Driver/Hardware/Inc/UartDriver.hpp"
#include "Driver/Interface/UartStatus.hpp"
#include "Driver/Interface/DriverState.hpp"
#include "stm32f1xx_hal_uart.h"
#include "stm32f1xx_hal_def.h"
#include <cstdint>
#include <array>
#include <span>
#include <cassert>
namespace Driver
{
namespace
{
struct HalStatusToDriverStatus
{
HAL_StatusTypeDef halStatus;
UartStatus driverStatus;
};
constexpr std::array<HalStatusToDriverStatus, 4> translation = {{{HAL_OK, UartStatus::Ok},
{HAL_ERROR, UartStatus::ErrorFromHal},
{HAL_BUSY, UartStatus::Busy},
{HAL_TIMEOUT, UartStatus::Timeout}}};
}
UartDriver::UartDriver(UART_HandleTypeDef &_uartHandler) : uartHandler(_uartHandler)
{
}
[[nodiscard]] bool UartDriver::onInitialize()
{
return true;
}
[[nodiscard]] bool UartDriver::onStart()
{
return true;
}
[[nodiscard]] bool UartDriver::onStop()
{
return true;
}
[[nodiscard]] bool UartDriver::onReset()
{
return true;
}
[[nodiscard]] UartStatus UartDriver::transmit(std::span<const std::uint8_t> data, std::uint32_t timeout)
{
UartStatus status = UartStatus::DriverInIncorrectMode;
if (getState() == DriverState::State::Running)
{
if (data.empty())
{
return UartStatus::ErrorFromHal;
}
// Ensure size fits in uint16_t for HAL function
assert(data.size() <= UINT16_MAX && "Data size exceeds UART HAL limit");
const auto size = static_cast<std::uint16_t>(data.size());
const HAL_StatusTypeDef halStatus = HAL_UART_Transmit(
&uartHandler,
const_cast<std::uint8_t *>(data.data()), // HAL API doesn't take const
size,
timeout);
status = getExchangeStatus(halStatus);
}
return status;
}
[[nodiscard]] UartStatus UartDriver::receive(std::span<std::uint8_t> data, std::uint32_t timeout)
{
UartStatus status = UartStatus::DriverInIncorrectMode;
if (getState() == DriverState::State::Running)
{
if (data.empty())
{
return UartStatus::ErrorFromHal;
}
// Ensure size fits in uint16_t for HAL function
assert(data.size() <= UINT16_MAX && "Data size exceeds UART HAL limit");
const auto size = static_cast<std::uint16_t>(data.size());
const HAL_StatusTypeDef halStatus = HAL_UART_Receive(
&uartHandler,
data.data(),
size,
timeout);
status = getExchangeStatus(halStatus);
}
return status;
}
}693a2df9fThis file is pinned to the selected release. Download the original to open it in your design tool.