From 96d94edc52b7984a07507a4941b19c94f2df3022 Mon Sep 17 00:00:00 2001 From: dvdb97 Date: Wed, 12 Jul 2023 13:33:34 +0200 Subject: [PATCH] First reworks for debugging --- include/sta/bus/uart/settings.hpp | 2 + include/sta/bus/uart/uart.hpp | 4 +- include/sta/debugging.hpp | 40 +++++++++++++ include/sta/devices/stm32/bus/i2c.hpp | 3 +- include/sta/devices/stm32/{ => bus}/spi.hpp | 0 include/sta/devices/stm32/{ => bus}/uart.hpp | 11 +++- include/sta/printable.hpp | 3 +- include/sta/uart.hpp | 61 -------------------- src/bus/uart/uart.cpp | 4 +- src/debug_serial.cpp | 4 +- src/devices/stm32/bus/i2c.cpp | 6 ++ src/devices/stm32/{ => bus}/spi.cpp | 2 +- src/devices/stm32/bus/uart.cpp | 58 +++++++++++++++++++ src/devices/stm32/uart.cpp | 25 -------- src/uart.cpp | 28 --------- 15 files changed, 125 insertions(+), 126 deletions(-) create mode 100644 include/sta/debugging.hpp rename include/sta/devices/stm32/{ => bus}/spi.hpp (100%) rename include/sta/devices/stm32/{ => bus}/uart.hpp (69%) delete mode 100644 include/sta/uart.hpp rename src/devices/stm32/{ => bus}/spi.cpp (99%) create mode 100644 src/devices/stm32/bus/uart.cpp delete mode 100644 src/devices/stm32/uart.cpp delete mode 100644 src/uart.cpp diff --git a/include/sta/bus/uart/settings.hpp b/include/sta/bus/uart/settings.hpp index c3bbe2b..390774c 100644 --- a/include/sta/bus/uart/settings.hpp +++ b/include/sta/bus/uart/settings.hpp @@ -5,6 +5,8 @@ namespace sta { + + enum class UARTMode { RX, diff --git a/include/sta/bus/uart/uart.hpp b/include/sta/bus/uart/uart.hpp index fafd153..aee3232 100644 --- a/include/sta/bus/uart/uart.hpp +++ b/include/sta/bus/uart/uart.hpp @@ -9,7 +9,7 @@ namespace sta class UART : public Interface { public: - UART(const UARTSettings & settings, Mutex * mutex=nullptr); + UART(Mutex * mutex=nullptr); /** * @brief Get %UART interface settings. @@ -22,4 +22,4 @@ namespace sta }; } // namespace sta -#endif // STA_CORE_UART_UART_HPP \ No newline at end of file +#endif // STA_CORE_UART_UART_HPP diff --git a/include/sta/debugging.hpp b/include/sta/debugging.hpp new file mode 100644 index 0000000..e5604ed --- /dev/null +++ b/include/sta/debugging.hpp @@ -0,0 +1,40 @@ +#ifndef STA_CORE_DEBUGGING_HPP +#define STA_CORE_DEBUGGING_HPP + +#include +#ifdef STA_DEBUGGING_ENABLED + +#include + +namespace sta +{ + extern Printable * Debug; +} // namespace sta + + +/** + * @brief Debug print message. + * + * @param ... See @ref sta::PrintableUART::print + * + * @ingroup sta_core_debug + */ +# define STA_DEBUG_PRINT(...) sta::Debug->print(__VA_ARGS__) + +/** + * @brief Debug print message followed by new-line to UART. + * + * @param ... See @ref sta::PrintableUART::println + * + * @ingroup sta_core_debug + */ +# define STA_DEBUG_PRINTLN(...) sta::Debug->println(__VA_ARGS__) + +#else // !STA_DEBUGGING_ENABLED + +# define STA_DEBUG_PRINT(...) ((void)0) +# define STA_DEBUG_PRINTLN(...) ((void)0) + +#endif // STA_DEBUGGING_ENABLED + +#endif // STA_CORE_DEBUGGING_HPP diff --git a/include/sta/devices/stm32/bus/i2c.hpp b/include/sta/devices/stm32/bus/i2c.hpp index be939d2..b2b34d1 100644 --- a/include/sta/devices/stm32/bus/i2c.hpp +++ b/include/sta/devices/stm32/bus/i2c.hpp @@ -35,7 +35,8 @@ namespace sta class STM32I2CDevice : public I2CDevice { - STM32I2CDevice(); + public: + STM32I2CDevice(STM32I2C * intf, int address, bool master=true, bool blocking=true); }; } diff --git a/include/sta/devices/stm32/spi.hpp b/include/sta/devices/stm32/bus/spi.hpp similarity index 100% rename from include/sta/devices/stm32/spi.hpp rename to include/sta/devices/stm32/bus/spi.hpp diff --git a/include/sta/devices/stm32/uart.hpp b/include/sta/devices/stm32/bus/uart.hpp similarity index 69% rename from include/sta/devices/stm32/uart.hpp rename to include/sta/devices/stm32/bus/uart.hpp index 1b79738..f50477d 100644 --- a/include/sta/devices/stm32/uart.hpp +++ b/include/sta/devices/stm32/bus/uart.hpp @@ -18,7 +18,7 @@ #if defined(STA_STM32_UART_ENABLED) || defined(DOXYGEN) -#include +#include /** @@ -41,10 +41,15 @@ namespace sta /** * @param handle STM32 HAL handle */ - STM32UART(UART_HandleTypeDef * handle); + STM32UART(UART_HandleTypeDef * handle, Mutex * mutex); - void write(const uint8_t * buffer, size_t size) override; + 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; private: UART_HandleTypeDef * handle_; /**< STM32 HAL handle */ }; diff --git a/include/sta/printable.hpp b/include/sta/printable.hpp index 7925c8c..77117a3 100644 --- a/include/sta/printable.hpp +++ b/include/sta/printable.hpp @@ -25,6 +25,7 @@ namespace sta class Printable { + public: /** * @brief Print single character. * @@ -188,4 +189,4 @@ namespace sta } // namespace sta -#endif // STA_CORE_PRINTABLE_HPP \ No newline at end of file +#endif // STA_CORE_PRINTABLE_HPP diff --git a/include/sta/uart.hpp b/include/sta/uart.hpp deleted file mode 100644 index 400f871..0000000 --- a/include/sta/uart.hpp +++ /dev/null @@ -1,61 +0,0 @@ -/** - * @file - * @brief UART interface definition. - */ -#ifndef STA_CORE_UART_HPP -#define STA_CORE_UART_HPP - -#include -#include - - -/** - * @defgroup sta_core_uart UART - * @ingroup sta_core - * @brief UART interface. - */ - - -namespace sta -{ - /** - * @brief Interface for %UART. - * - * @ingroup sta_core_uart - */ - class UART - { - public: - /** - * @brief Write buffer to %UART. - * - * @param buffer Source buffer - * @param size Number of bytes in buffer - */ - virtual void write(const uint8_t * buffer, size_t size) = 0; - - /** - * @brief Write unsigned integer to %UART. - * - * @param value Unsigned integer value - */ - void write(uint8_t value); - - /** - * @brief Write unsigned integer to %UART. - * - * @param value Unsigned integer value - */ - void write(uint16_t value); - - /** - * @brief Write unsigned integer to %UART. - * - * @param value Unsigned integer value - */ - void write(uint32_t value); - }; -} // namespace sta - - -#endif // STA_CORE_UART_HPP diff --git a/src/bus/uart/uart.cpp b/src/bus/uart/uart.cpp index 6225576..d2e2767 100644 --- a/src/bus/uart/uart.cpp +++ b/src/bus/uart/uart.cpp @@ -2,8 +2,8 @@ namespace sta { - UART::UART(const UARTSettings & settings, Mutex * mutex) - : Interface{mutex}, settings_{settings} + UART::UART(Mutex * mutex) + : Interface{mutex} { } diff --git a/src/debug_serial.cpp b/src/debug_serial.cpp index 80a0db5..b6ff7fe 100644 --- a/src/debug_serial.cpp +++ b/src/debug_serial.cpp @@ -4,9 +4,9 @@ #ifdef STA_PLATFORM_STM32 -#include +#include -#include +// #include // Set platform UART alias using PlatformUART = sta::STM32UART; diff --git a/src/devices/stm32/bus/i2c.cpp b/src/devices/stm32/bus/i2c.cpp index 6cbd5e4..83f4dda 100644 --- a/src/devices/stm32/bus/i2c.cpp +++ b/src/devices/stm32/bus/i2c.cpp @@ -119,4 +119,10 @@ namespace sta delete [] buffer; } + STM32I2CDevice::STM32I2CDevice(STM32I2C * intf, int address, bool master, bool blocking) + : I2CDevice(intf, address, master, blocking) + { + + } + } // namespace sta diff --git a/src/devices/stm32/spi.cpp b/src/devices/stm32/bus/spi.cpp similarity index 99% rename from src/devices/stm32/spi.cpp rename to src/devices/stm32/bus/spi.cpp index 2a2676a..7cfa8b5 100644 --- a/src/devices/stm32/spi.cpp +++ b/src/devices/stm32/bus/spi.cpp @@ -1,4 +1,4 @@ -#include +#include #ifdef STA_STM32_SPI_ENABLED #include diff --git a/src/devices/stm32/bus/uart.cpp b/src/devices/stm32/bus/uart.cpp new file mode 100644 index 0000000..2d36ef3 --- /dev/null +++ b/src/devices/stm32/bus/uart.cpp @@ -0,0 +1,58 @@ +#include +#ifdef STA_STM32_UART_ENABLED + +#include +#include + +namespace sta +{ + STM32UART::STM32UART(UART_HandleTypeDef * handle, Mutex * mutex) + : UART{mutex}, handle_{handle} + { + STA_ASSERT(handle != nullptr); + } + + void STM32UART::transfer(uint8_t value) + { + HAL_UART_Transmit(handle_, &value, 1, HAL_MAX_DELAY); + } + + void STM32UART::transfer16(uint16_t value) + { + HAL_UART_Transmit(handle_, reinterpret_cast(&value), 2, HAL_MAX_DELAY); + } + + void STM32UART::transfer(const uint8_t * buffer, size_t size) + { + STA_ASSERT(buffer != nullptr); + + HAL_UART_Transmit(handle_, const_cast(buffer), size, HAL_MAX_DELAY); + } + + void STM32UART::transfer(const uint8_t * txBuffer, uint8_t * rxBuffer, size_t size) + { + // IS THIS A THING HERE? + } + + void STM32UART::receive(uint8_t * buffer, size_t size) + { + STA_ASSERT(buffer != nullptr); + + HAL_UART_Receive(handle_, buffer, size, HAL_MAX_DELAY); + } + + void STM32UART::fill(uint8_t value, size_t count) + { + // 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); + + delete [] buffer; + } +} // namespace sta + + +#endif // STA_STM32_UART_ENABLED diff --git a/src/devices/stm32/uart.cpp b/src/devices/stm32/uart.cpp deleted file mode 100644 index d377cd5..0000000 --- a/src/devices/stm32/uart.cpp +++ /dev/null @@ -1,25 +0,0 @@ -#include -#ifdef STA_STM32_UART_ENABLED - -#include - - -namespace sta -{ - STM32UART::STM32UART(UART_HandleTypeDef * handle) - : handle_{handle} - { - STA_ASSERT(handle != nullptr); - } - - - void STM32UART::write(const uint8_t * buffer, size_t size) - { - STA_ASSERT(buffer != nullptr); - - HAL_UART_Transmit(handle_, const_cast(buffer), size, HAL_MAX_DELAY); - } -} // namespace sta - - -#endif // STA_STM32_UART_ENABLED diff --git a/src/uart.cpp b/src/uart.cpp deleted file mode 100644 index dd0f69f..0000000 --- a/src/uart.cpp +++ /dev/null @@ -1,28 +0,0 @@ -#include - -#include - -#include -#include - - -namespace sta -{ - void UART::write(uint8_t value) - { - // TODO Handle endian-ness - write(&value, 1); - } - - void UART::write(uint16_t value) - { - // TODO Handle endian-ness - write(reinterpret_cast(&value), sizeof(value)); - } - - void UART::write(uint32_t value) - { - // TODO Handle endian-ness - write(reinterpret_cast(&value), sizeof(value)); - } -} // namespace sta