From ce4f8eb8f5c02e2832ad3f396a0d84822e2b04ad Mon Sep 17 00:00:00 2001 From: Erki Date: Fri, 2 Apr 2021 00:50:59 +0300 Subject: [PATCH] New logging interfaces --- Peripherals/Inc/peripherals_hal_st.hpp | 65 +++++++++++++++- Peripherals/Inc/peripherals_imu_icm.hpp | 2 - Peripherals/Inc/peripherals_utility.hpp | 4 + Utility/CMakeLists.txt | 11 +-- Utility/Inc/utility_asyncaurtlogger.hpp | 49 ------------ Utility/Inc/utility_asynclogger.hpp | 97 ++++++++++++++++++++++++ Utility/Inc/utility_atomicscopeguard.hpp | 26 ++++++- Utility/Inc/utility_itmlogger.hpp | 30 -------- Utility/Inc/utility_seriallogger.hpp | 52 +++++++++++++ Utility/Inc/utility_uartlogger.hpp | 39 ---------- Utility/Src/utility_asynchuartlogger.cpp | 73 ------------------ Utility/Src/utility_atomicscopeguard.cpp | 32 -------- Utility/Src/utility_itmlogger.cpp | 33 -------- Utility/Src/utility_uartlogger.cpp | 32 -------- 14 files changed, 243 insertions(+), 302 deletions(-) delete mode 100644 Utility/Inc/utility_asyncaurtlogger.hpp create mode 100644 Utility/Inc/utility_asynclogger.hpp delete mode 100644 Utility/Inc/utility_itmlogger.hpp create mode 100644 Utility/Inc/utility_seriallogger.hpp delete mode 100644 Utility/Inc/utility_uartlogger.hpp delete mode 100644 Utility/Src/utility_asynchuartlogger.cpp delete mode 100644 Utility/Src/utility_atomicscopeguard.cpp delete mode 100644 Utility/Src/utility_itmlogger.cpp delete mode 100644 Utility/Src/utility_uartlogger.cpp diff --git a/Peripherals/Inc/peripherals_hal_st.hpp b/Peripherals/Inc/peripherals_hal_st.hpp index 40080b4..ff58dfe 100644 --- a/Peripherals/Inc/peripherals_hal_st.hpp +++ b/Peripherals/Inc/peripherals_hal_st.hpp @@ -21,6 +21,22 @@ namespace Hal namespace St { +template +using IsrCallbackFn = void(*)(Origin*); + +template +IsrCallbackFn createCallback(Handler& h_in) +{ + static Handler* h = &h_in; + printf("Set handler: %p\n", h); + return +[](Origin*) + { + printf("Into callback we go.\n"); + (h->*func)(); + printf("And we're out.\n"); + }; +} + struct StaticHal { static void Initialize() @@ -48,6 +64,16 @@ struct StaticHal (void)micros; #endif } + + static void enableInterrupts() + { + __enable_irq(); + } + + static void disableInterrupts() + { + __disable_irq(); + } }; #ifdef HAL_GPIO_MODULE_ENABLED @@ -101,7 +127,31 @@ struct SerialInterface bool Receive(std::uint8_t* data, const std::uint32_t data_len) { - return transmit(handle, data, data_len, 100) == HAL_StatusTypeDef::HAL_OK; + return receive(handle, data, data_len, 100) == HAL_StatusTypeDef::HAL_OK; + } +}; + +template +struct SerialInterfaceAsync +{ + using underlying_handle_type = T; + underlying_handle_type* handle; + + SerialInterfaceAsync() = delete; + explicit SerialInterfaceAsync(underlying_handle_type* handle) + : handle(handle) + { } + + bool Transmit(std::uint8_t* data, const std::uint32_t data_len) + { + return transmit(handle, data, data_len) == HAL_StatusTypeDef::HAL_OK; + } + + bool Receive(std::uint8_t* data, const std::uint32_t data_len) + { + return receive(handle, data, data_len) == HAL_StatusTypeDef::HAL_OK; } }; @@ -178,6 +228,7 @@ struct SpiRegisters #ifdef HAL_UART_MODULE_ENABLED using UartInterface = SerialInterface; +using UartInterfaceDMA = SerialInterfaceAsync; #endif // HAL_UART_MODULE_ENABLED @@ -217,6 +268,18 @@ struct PwmChannel #endif // HAL_TIM_MODULE_ENABLED +struct ItmSerialInterface +{ + bool Transmit(std::uint8_t* data, const std::uint32_t data_len) + { + for (std::uint32_t i = 0; i < data_len; i++) + { + ITM_SendChar(char(data[i])); + } + return true; + } +}; + } } } diff --git a/Peripherals/Inc/peripherals_imu_icm.hpp b/Peripherals/Inc/peripherals_imu_icm.hpp index a3affb8..72bf4d2 100644 --- a/Peripherals/Inc/peripherals_imu_icm.hpp +++ b/Peripherals/Inc/peripherals_imu_icm.hpp @@ -11,8 +11,6 @@ #include #include -#include "spi.h" - #include "peripherals_imu.hpp" #include "peripherals_utility.hpp" diff --git a/Peripherals/Inc/peripherals_utility.hpp b/Peripherals/Inc/peripherals_utility.hpp index e8aa3b8..d3e1c12 100644 --- a/Peripherals/Inc/peripherals_utility.hpp +++ b/Peripherals/Inc/peripherals_utility.hpp @@ -13,6 +13,10 @@ namespace Peripherals { +#define SKULLC_CONCAT_IMPL(x, y) x ## y +#define SKULLC_CONCAT(x, y) SKULLC_CONCAT_IMPL(x, y) +#define SKULLC_TAG struct SKULLC_CONCAT(SkullCTag_, __COUNTER__) + template constexpr const T& Clamp(const T& v, const T& lo, const T& hi) { diff --git a/Utility/CMakeLists.txt b/Utility/CMakeLists.txt index 1d0fff6..0ff13a7 100644 --- a/Utility/CMakeLists.txt +++ b/Utility/CMakeLists.txt @@ -1,12 +1,9 @@ cmake_minimum_required(VERSION 3.8 FATAL_ERROR) -if(WITH_HAL) - set(additional_sources - Src/utility_asynchuartlogger.cpp - Src/utility_atomicscopeguard.cpp - Src/utility_itmlogger.cpp - Src/utility_uartlogger.cpp) -endif() +#if(WITH_HAL) +# set(additional_sources +# ) +#endif() add_library(utility STATIC Src/utility_logging.cpp diff --git a/Utility/Inc/utility_asyncaurtlogger.hpp b/Utility/Inc/utility_asyncaurtlogger.hpp deleted file mode 100644 index 58d2353..0000000 --- a/Utility/Inc/utility_asyncaurtlogger.hpp +++ /dev/null @@ -1,49 +0,0 @@ -/* - * utility_asyncaurtlogger.hpp - * - * Created on: Mar 20, 2021 - * Author: erki - */ - -#ifndef SKULLC_UTILITY_ASYNCAURTLOGGER_HPP_ -#define SKULLC_UTILITY_ASYNCAURTLOGGER_HPP_ - -#include "utility_ilogger.hpp" -#include "utility_ringbuffer.hpp" - -#include "usart.h" - -namespace Utility -{ - -class AsyncUARTLogger : public ILogger -{ -public: - explicit AsyncUARTLogger(UART_HandleTypeDef* huart); - AsyncUARTLogger() = delete; - AsyncUARTLogger(const AsyncUARTLogger&) = delete; - AsyncUARTLogger(AsyncUARTLogger&&) = delete; - - void log(const char* format, ...); - -private: - struct _Data - { - std::array buffer; - std::int32_t length; - }; - - Ringbuffer<_Data, 10> _buffer_queue; - UART_HandleTypeDef* _huart; - bool _in_flight = false; - - static AsyncUARTLogger* _this; - static void _txCompleteCallback(UART_HandleTypeDef* huart); - - void _sendNextLog(); -}; - -} - - -#endif /* SKULLC_UTILITY_ASYNCAURTLOGGER_HPP_ */ diff --git a/Utility/Inc/utility_asynclogger.hpp b/Utility/Inc/utility_asynclogger.hpp new file mode 100644 index 0000000..b23a65f --- /dev/null +++ b/Utility/Inc/utility_asynclogger.hpp @@ -0,0 +1,97 @@ +/* + * utility_asynclogger.hpp + * + * Created on: Apr 1, 2021 + * Author: erki + */ + +#ifndef UTILITY_INC_UTILITY_ASYNCLOGGER_HPP_ +#define UTILITY_INC_UTILITY_ASYNCLOGGER_HPP_ + +#include "utility_atomicscopeguard.hpp" +#include "utility_ringbuffer.hpp" +#include "utility_ilogger.hpp" + +#include +#include +#include + +namespace Utility +{ + +template +class AsyncLogger : public ILogger +{ +public: + using serial_interface = T; + using hal = H; + + AsyncLogger() = delete; + explicit AsyncLogger(const serial_interface& serial) + : _serial(serial) + { } + + AsyncLogger(const AsyncLogger&) = delete; + AsyncLogger(AsyncLogger&&) = delete; + AsyncLogger& operator=(const AsyncLogger&) = delete; + AsyncLogger& operator=(AsyncLogger&&) = delete; + + void log(const char* format, ...) + { + { + AtomicScopeGuard s; + + if (_buffer_queue.size() == _buffer_queue.max_size()) + return; + } + + std::va_list args; + va_start(args, format); + + auto tail = _buffer_queue.end(); + std::array& buffer = tail->buffer; + + tail->length = vsnprintf(buffer.data(), buffer.size(), format, args); + + { + AtomicScopeGuard s; + _buffer_queue.increment_tail(); + + if (!_in_flight) + _sendNextLog(); + } + } + + void txCompleteCallback() + { + if (!_buffer_queue.empty()) + _sendNextLog(); + else + _in_flight = false; + } + +private: + struct _Data + { + std::array buffer; + std::int32_t length = 0; + }; + + Ringbuffer<_Data, BufferCount> _buffer_queue; + serial_interface _serial; + bool _in_flight = false; + + void _sendNextLog() + { + _in_flight = true; + + _Data& head = _buffer_queue.front(); + _serial.Transmit(reinterpret_cast(head.buffer.data()), head.length); + _buffer_queue.pop_front(); + } +}; + +} + + +#endif /* UTILITY_INC_UTILITY_ASYNCLOGGER_HPP_ */ diff --git a/Utility/Inc/utility_atomicscopeguard.hpp b/Utility/Inc/utility_atomicscopeguard.hpp index beef132..60866b7 100644 --- a/Utility/Inc/utility_atomicscopeguard.hpp +++ b/Utility/Inc/utility_atomicscopeguard.hpp @@ -13,19 +13,37 @@ namespace Utility { -class AtomicScopeGuard +template +struct AtomicScopeGuard { -public: - AtomicScopeGuard(); + using hal = H; + AtomicScopeGuard() + { + hal::disableInterrupts(); + _reentrancy_counter++; + } + AtomicScopeGuard(const AtomicScopeGuard&) = delete; AtomicScopeGuard(AtomicScopeGuard&&) = delete; + AtomicScopeGuard& operator=(const AtomicScopeGuard&) = delete; + AtomicScopeGuard& operator=(AtomicScopeGuard&&) = delete; + + ~AtomicScopeGuard() + { + _reentrancy_counter--; + + if (!_reentrancy_counter) + hal::enableInterrupts(); + } - ~AtomicScopeGuard(); private: static std::int32_t _reentrancy_counter; }; +template +std::int32_t AtomicScopeGuard::_reentrancy_counter = 0; + } #endif /* UTILITY_INC_UTILITY_ATOMICSCOPEGUARD_HPP_ */ diff --git a/Utility/Inc/utility_itmlogger.hpp b/Utility/Inc/utility_itmlogger.hpp deleted file mode 100644 index 563e5ce..0000000 --- a/Utility/Inc/utility_itmlogger.hpp +++ /dev/null @@ -1,30 +0,0 @@ -// -// Created by erki on 14.03.21. -// - -#ifndef SKULLC_UTILITY_ITMLOGGER_HPP_ -#define SKULLC_UTILITY_ITMLOGGER_HPP_ - -#include "utility_logging.hpp" - -#include - -namespace Utility -{ - -class ITMLogger : public ILogger -{ -public: - ITMLogger() = default; - ITMLogger(const ITMLogger&) = delete; - ITMLogger(ITMLogger&&) = delete; - - void log(const char* format, ...) override; - -private: - std::array _buffer; -}; - -} - -#endif //SKULLC_UTILITY_ITMLOGGER_HPP_ diff --git a/Utility/Inc/utility_seriallogger.hpp b/Utility/Inc/utility_seriallogger.hpp new file mode 100644 index 0000000..cd71080 --- /dev/null +++ b/Utility/Inc/utility_seriallogger.hpp @@ -0,0 +1,52 @@ +/* + * utility_seriallogger.hpp + * + * Created on: Apr 1, 2021 + * Author: erki + */ + +#ifndef UTILITY_INC_UTILITY_SERIALLOGGER_HPP_ +#define UTILITY_INC_UTILITY_SERIALLOGGER_HPP_ + +#include +#include +#include + +#include "utility_ilogger.hpp" + +namespace Utility +{ + +template +class SerialLogger : public ILogger +{ +public: + using serial_interface = T; + + SerialLogger() = delete; + explicit SerialLogger(const serial_interface& serial) + : _serial(serial) + { } + + void log(const char* format, ...) override + { + std::va_list args; + va_start(args, format); + + const std::int32_t len = vsnprintf(_buffer.data(), _buffer.size(), format, args); + + if (len > 0) + _serial.Transmit(reinterpret_cast(_buffer.data()), len); + + va_end(args); + } + +private: + serial_interface _serial; + std::array _buffer; +}; + +} + + +#endif /* UTILITY_INC_UTILITY_SERIALLOGGER_HPP_ */ diff --git a/Utility/Inc/utility_uartlogger.hpp b/Utility/Inc/utility_uartlogger.hpp deleted file mode 100644 index fcad76f..0000000 --- a/Utility/Inc/utility_uartlogger.hpp +++ /dev/null @@ -1,39 +0,0 @@ -/* - * utility_uartlogger.hpp - * - * Created on: Mar 20, 2021 - * Author: erki - */ - -#ifndef SKULLC_UTILITY_UARTLOGGER_HPP_ -#define SKULLC_UTILITY_UARTLOGGER_HPP_ - -#include "utility_logging.hpp" - -#include "usart.h" - -#include - -namespace Utility -{ - -class UARTLogger : public ILogger -{ -public: - explicit UARTLogger(UART_HandleTypeDef* huart); - - UARTLogger() = delete; - UARTLogger(const UARTLogger&) = delete; - UARTLogger(UARTLogger&&) = delete; - - void log(const char* format, ...) override; - -private: - UART_HandleTypeDef* _huart; - std::array _buffer; -}; - -} - - -#endif /* SKULLC_UTILITY_UARTLOGGER_HPP_ */ diff --git a/Utility/Src/utility_asynchuartlogger.cpp b/Utility/Src/utility_asynchuartlogger.cpp deleted file mode 100644 index a529789..0000000 --- a/Utility/Src/utility_asynchuartlogger.cpp +++ /dev/null @@ -1,73 +0,0 @@ -/* - * utility_asynchuartlogger.cpp - * - * Created on: Mar 20, 2021 - * Author: erki - */ - -#include "utility_asyncaurtlogger.hpp" - -#include -#include -#include - -#include "utility_atomicscopeguard.hpp" - -namespace Utility -{ - -AsyncUARTLogger* AsyncUARTLogger::_this = nullptr; - -AsyncUARTLogger::AsyncUARTLogger(UART_HandleTypeDef* huart) - : _huart(huart) -{ - assert(!_this); - - _this = this; - _huart->TxCpltCallback = &AsyncUARTLogger::_txCompleteCallback; -} - -void AsyncUARTLogger::log(const char* format, ...) -{ - { - AtomicScopeGuard s; - - if (_buffer_queue.size() == _buffer_queue.max_size()) - return; - } - - std::va_list args; - va_start(args, format); - - auto tail = _buffer_queue.end(); - std::array& buffer = tail->buffer; - - tail->length = vsnprintf(buffer.data(), buffer.size(), format, args); - - { - AtomicScopeGuard s; - _buffer_queue.increment_tail(); - - if (!_in_flight) - _sendNextLog(); - } -} - -void AsyncUARTLogger::_txCompleteCallback(UART_HandleTypeDef*) -{ - if (!AsyncUARTLogger::_this->_buffer_queue.empty()) - AsyncUARTLogger::_this->_sendNextLog(); - else - AsyncUARTLogger::_this->_in_flight = false; -} - -void AsyncUARTLogger::_sendNextLog() -{ - _in_flight = true; - - _Data& head = _buffer_queue.front(); - HAL_UART_Transmit_DMA(_huart, reinterpret_cast(head.buffer.data()), head.length); - _buffer_queue.pop_front(); -} - -} diff --git a/Utility/Src/utility_atomicscopeguard.cpp b/Utility/Src/utility_atomicscopeguard.cpp deleted file mode 100644 index 17ca0b6..0000000 --- a/Utility/Src/utility_atomicscopeguard.cpp +++ /dev/null @@ -1,32 +0,0 @@ -/* - * utility_atomicscopeguard.cpp - * - * Created on: Mar 21, 2021 - * Author: erki - */ - -#include "utility_atomicscopeguard.hpp" - -#include "cmsis_gcc.h" - -namespace Utility -{ - -std::int32_t AtomicScopeGuard::_reentrancy_counter; - -AtomicScopeGuard::AtomicScopeGuard() -{ - __disable_irq(); - _reentrancy_counter++; -} - -AtomicScopeGuard::~AtomicScopeGuard() -{ - _reentrancy_counter--; - - if (!_reentrancy_counter) - __enable_irq(); -} - -} - diff --git a/Utility/Src/utility_itmlogger.cpp b/Utility/Src/utility_itmlogger.cpp deleted file mode 100644 index 7d6a5e3..0000000 --- a/Utility/Src/utility_itmlogger.cpp +++ /dev/null @@ -1,33 +0,0 @@ -/* - * utility_logger.cpp - * - * Created on: Mar 13, 2021 - * Author: erki - */ - -#include "utility_itmlogger.hpp" - -#include -#include - -#include "main.h" - -namespace Utility -{ - -void ITMLogger::log(const char* format, ...) -{ - std::va_list args; - va_start(args, format); - - const std::int32_t len = vsnprintf(_buffer.data(), _buffer.size(), format, args); - - for (std::int32_t i = 0; i < len; i++) - { - ITM_SendChar(_buffer[i]); - } - - va_end(args); -} - -} diff --git a/Utility/Src/utility_uartlogger.cpp b/Utility/Src/utility_uartlogger.cpp deleted file mode 100644 index 3418a20..0000000 --- a/Utility/Src/utility_uartlogger.cpp +++ /dev/null @@ -1,32 +0,0 @@ -/* - * utility_uartlogger.cpp - * - * Created on: Mar 20, 2021 - * Author: erki - */ - -#include "utility_uartlogger.hpp" - -#include -#include - -namespace Utility -{ - -UARTLogger::UARTLogger(UART_HandleTypeDef* huart) - : _huart(huart) -{ } - -void UARTLogger::log(const char* format, ...) -{ - std::va_list args; - va_start(args, format); - - const std::int32_t len = vsnprintf(_buffer.data(), _buffer.size(), format, args); - - HAL_UART_Transmit(_huart, reinterpret_cast(_buffer.data()), len, 10); - - va_end(args); -} - -}