diff --git a/CMakeLists.txt b/CMakeLists.txt index d1aceb1..3d52b7e 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -27,6 +27,7 @@ find_package(HAL COMPONENTS "${HAL_COMP_LIST}" REQUIRED) # STM32VL-Discovery add_executable(stm32-blinky-f1 ${MAIN_SOURCE_FILE} + logging.cpp uart_handler.cpp spi.cpp rfm95.cpp diff --git a/logging.cpp b/logging.cpp new file mode 100644 index 0000000..9506a1d --- /dev/null +++ b/logging.cpp @@ -0,0 +1,4 @@ +#include "logging.hpp" + +logging_adapter* log::logger = nullptr; +LogLevel log::log_level = LogLevel::INFO; diff --git a/logging.hpp b/logging.hpp new file mode 100644 index 0000000..1f6e5b0 --- /dev/null +++ b/logging.hpp @@ -0,0 +1,94 @@ +#pragma once + +#include + +#include "uart_handler.hpp" + +enum class LogLevel { + ERROR = 0, + INFO, + DEBUG, + TRACE, +}; + +inline std::string_view get_loglevel_string(LogLevel level) { + switch (level) { + case (LogLevel::ERROR): { + return "[Error]"; + } + case (LogLevel::INFO): { + return "[Info] "; + } + case (LogLevel::DEBUG): { + return "[Debug]"; + } + case (LogLevel::TRACE): { + return "[Trace]"; + } + } + + return "[Unknown]"; +} + +class logging_adapter { + public: + virtual bool init(){} + virtual void log(std::string_view message) const = 0; +}; + +class uart_logger : public logging_adapter { + public: + bool init() { return uart_interface::init(); } + + virtual void log(std::string_view message) const override { + uart_interface::write(message); + } +}; + +class log { + public: + template + static bool init() { + if (logger) { + return true; + } + + static LoggerType logger_impl; + logger = &logger_impl; + + return logger_impl.init(); + } + + static void error(std::string_view message) { + log_impl(LogLevel::ERROR, message); + } + + static void debug(std::string_view message) { + log_impl(LogLevel::DEBUG, message); + } + + static void info(std::string_view message) { + log_impl(LogLevel::INFO, message); + } + + static void set_loglevel(LogLevel level) { log_level = level; } + + private: + static void log_impl(LogLevel level, std::string_view message) { + if (log_level < level) { + return; + } + + //std::stringstream msg; + //msg << get_loglevel_string(level) << ": " << message; + logger->log(get_loglevel_string(level)); + logger->log(": "); + logger->log(message); + + constexpr std::string_view carriage_return{"\r\n"}; + logger->log(carriage_return); + } + + static logging_adapter* logger; + static LogLevel log_level; +}; diff --git a/main.cpp b/main.cpp index c634793..c9b088b 100644 --- a/main.cpp +++ b/main.cpp @@ -1,7 +1,7 @@ #include +#include "logging.hpp" #include "spi.hpp" -#include "uart_handler.hpp" // STM32VL-Discovery green led - PC9 #define BTN_PORT GPIOA @@ -11,12 +11,6 @@ #define BTN_PIN GPIO_PIN_0 #define LED_PORT_CLK_ENABLE __HAL_RCC_GPIOC_CLK_ENABLE -constexpr auto UART_PORT = GPIOA_BASE; - -#define UARTPORT GPIOA -#define UARTTX_PIN GPIO_PIN_9 -#define UARTRX_PIN GPIO_PIN_10 - #define LoRa_RESET_Pin GPIO_PIN_3 #define LoRa_RESET_GPIO_Port GPIOA #define LoRa_CS_Pin GPIO_PIN_4 @@ -73,19 +67,17 @@ extern "C" { #include } -using uart_interface = - uart_handler; - int main(void) { HAL_Init(); HAL_SYSTICK_Config(1); initGPIO(); - if (!uart_interface::init()) { + if (!log::init()) { // toggle status led or something } - - uart_interface::write("UART Initialized."); + log::set_loglevel(LogLevel::DEBUG); + log::info("logging Initialized"); + log::info("running PentaTrack v0.1.3"); if (!MX_SPI1_Init()) { // toggle status led or something @@ -95,28 +87,35 @@ int main(void) { HAL_GPIO_WritePin(LoRa_RESET_GPIO_Port, LoRa_RESET_Pin, GPIO_PIN_SET); HAL_Delay(10); - uart_interface::write("SPI1 Initialized."); - uart_interface::write("Initialization done."); + log::debug("SPI1 Initialized."); + log::debug("Initialization done."); char OP_Mode = 0x01; char buff = 0x7F & OP_Mode; char res = 0; HAL_GPIO_WritePin(LoRa_CS_GPIO_Port, LoRa_CS_Pin, GPIO_PIN_RESET); - auto result = HAL_SPI_Transmit(&hspi1, (uint8_t *)&buff, 1, 100); + [[maybe_unused]] auto result = + HAL_SPI_Transmit(&hspi1, (uint8_t *)&buff, 1, 100); HAL_SPI_Receive(&hspi1, (uint8_t *)&res, 1, 100); HAL_GPIO_WritePin(LoRa_CS_GPIO_Port, LoRa_CS_Pin, GPIO_PIN_SET); - RF95_Init(); + // RF95_Init(); + int i = 0; while (1) { - RF95_setModeRx_Continuous(); + // RF95_setModeRx_Continuous(); HAL_GPIO_WritePin(LED_PORT, LED1_PIN, GPIO_PIN_RESET); - RF95_receive(LoRa_buff); + HAL_GPIO_WritePin(LED_PORT, LED2_PIN, GPIO_PIN_RESET); + // RF95_receive(LoRa_buff); + HAL_Delay(500); HAL_GPIO_WritePin(LED_PORT, LED1_PIN, GPIO_PIN_SET); + HAL_GPIO_WritePin(LED_PORT, LED2_PIN, GPIO_PIN_SET); + HAL_Delay(500); - std::string_view msg{reinterpret_cast(LoRa_buff)}; - uart_interface::write("Received Message:"); - uart_interface::write(msg); + // std::string_view msg{reinterpret_cast(LoRa_buff)}; + // log::info("Received Message"); + // log::debug("Received Message"); + // log::debug(msg); // std::string_view foo{"Das ist ein test"}; // strcpy((char *)LoRa_buff, foo.data()); diff --git a/uart_handler.hpp b/uart_handler.hpp index 6571a9a..6f616a0 100644 --- a/uart_handler.hpp +++ b/uart_handler.hpp @@ -4,11 +4,15 @@ #include +constexpr auto UART_PORT = GPIOA_BASE; +#define UARTPORT GPIOA +#define UARTTX_PIN GPIO_PIN_9 +#define UARTRX_PIN GPIO_PIN_10 + /* * small uart wrapper * assumes USART1 with default pins/port */ - template struct uart_handler { static void enable_clocks() { @@ -31,6 +35,10 @@ struct uart_handler { HAL_GPIO_Init(reinterpret_cast(port), &ua_tx); HAL_GPIO_Init(reinterpret_cast(port), &ua_rx); + + /* USART1 interrupt Init */ + // HAL_NVIC_SetPriority(USART1_IRQn, 0, 0); + // HAL_NVIC_EnableIRQ(USART1_IRQn); } static bool enable_oscillators() { @@ -75,6 +83,12 @@ struct uart_handler { auto result = HAL_UART_Init(&s_UARTHandle); + if (result != HAL_OK) { + return false; + } + + // result = HAL_UART_Receive_IT(&s_UARTHandle, rx_buff, rx_buff_size); + return result == HAL_OK; } @@ -91,14 +105,14 @@ struct uart_handler { static HAL_StatusTypeDef write(const std::string_view& message, uint32_t timeout = HAL_MAX_DELAY) { - constexpr std::string_view carriage_return{"\r\n"}; + // constexpr std::string_view carriage_return{"\r\n"}; - write(reinterpret_cast(const_cast(message.data())), - message.size(), timeout); + return write(reinterpret_cast(const_cast(message.data())), + message.size(), timeout); - return write( - reinterpret_cast(const_cast(carriage_return.data())), - carriage_return.size(), timeout); + // return write( + // reinterpret_cast(const_cast(carriage_return.data())), + // carriage_return.size(), timeout); } static HAL_StatusTypeDef write(uint8_t* buf, uint16_t size, @@ -108,13 +122,29 @@ struct uart_handler { static UART_HandleTypeDef s_UARTHandle; + static uint8_t* get_buf() { return rx_buff; } + private: static constexpr auto pin_tx = PinTX; static constexpr auto pin_rx = PinRX; static constexpr auto port = Port; static constexpr auto usart_base = UsartBase; + static constexpr size_t rx_buff_size = 10; + static inline uint8_t* rx_buff; }; template UART_HandleTypeDef uart_handler::s_UARTHandle = UART_HandleTypeDef(); + +using uart_interface = + uart_handler; + +template <> +uint8_t* uart_interface::rx_buff = new uint8_t[10]; + +// inline void HAL_UART_RxCpltCallback(UART_HandleTypeDef* huart) { +// static int count = 0; +// count++; +// HAL_UART_Receive_IT(huart, uart_interface::get_buf(), 10); +// }