From 221b455e43ca30d1376af22b2712fdb15a9ebebf Mon Sep 17 00:00:00 2001 From: Henrik Stickann <4376396-Mithradir@users.noreply.gitlab.com> Date: Sat, 9 Apr 2022 21:49:20 +0200 Subject: [PATCH] Add HAL UART implementation --- include/sta/hal/uart.hpp | 53 ++++++++++++++++++++++++++++++++++++++++ src/hal/uart.cpp | 32 ++++++++++++++++++++++++ 2 files changed, 85 insertions(+) create mode 100644 include/sta/hal/uart.hpp create mode 100644 src/hal/uart.cpp diff --git a/include/sta/hal/uart.hpp b/include/sta/hal/uart.hpp new file mode 100644 index 0000000..b9dd65e --- /dev/null +++ b/include/sta/hal/uart.hpp @@ -0,0 +1,53 @@ +/** + * @brief Implementation for UART using HAL. + * + * Define STA_HAL_UART_ENABLE in to enable. + * + * To enable the global DebugSerial instance (example using UART1) define: + * #define STA_HAL_UART_DEBUG huart1 + */ +#ifndef STA_HAL_UART_HPP +#define STA_HAL_UART_HPP + +#include +#ifdef STA_HAL_UART_ENABLE + +#include + +#include + + +namespace sta +{ + /** + * @brief Implementation of UART for HAL. + */ + class HalUART : public UART + { + public: + /** + * @param handle UART handle + */ + HalUART(UART_HandleTypeDef * handle); + + using UART::print; + + void write(const uint8_t * buffer, size_t size) override; + + private: + UART_HandleTypeDef * handle_; /**< UART handle */ + }; + + +#ifdef STA_HAL_UART_DEBUG + /** + * @brief Global UART instance for debug output. + */ + extern HalUART DebugSerial; +#endif // STA_HAL_UART_DEBUG +} // namespace sta + + +#endif // STA_HAL_UART_ENABLE + +#endif // STA_HAL_UART_HPP diff --git a/src/hal/uart.cpp b/src/hal/uart.cpp new file mode 100644 index 0000000..9413a28 --- /dev/null +++ b/src/hal/uart.cpp @@ -0,0 +1,32 @@ +#include + +#ifdef STA_HAL_UART_ENABLE + + +namespace sta +{ + HalUART::HalUART(UART_HandleTypeDef * handle) + : handle_{handle} + {} + + + void HalUART::write(const uint8_t * buffer, size_t size) + { + HAL_UART_Transmit(handle_, const_cast(buffer), size, HAL_MAX_DELAY); + } +} // namespace sta + + +#ifdef STA_HAL_UART_DEBUG + +#include + +namespace sta +{ + HalUART DebugSerial(&STA_HAL_UART_DEBUG); +} // namespace sta + +#endif // STA_HAL_UART_DEBUG + + +#endif // STA_HAL_UART_ENABLE