From 32eca023a50cc9312b9a3c6cdc12a79ae5f9a960 Mon Sep 17 00:00:00 2001 From: kalipso Date: Thu, 23 Feb 2023 11:03:57 +0100 Subject: [PATCH] WIP adding uart2 --- main.cpp | 69 +++++++++++++++++++++++++++++++----------------- uart_handler.cpp | 3 +++ uart_handler.hpp | 15 +++++++++++ 3 files changed, 63 insertions(+), 24 deletions(-) diff --git a/main.cpp b/main.cpp index e3a09ca..4601bb1 100644 --- a/main.cpp +++ b/main.cpp @@ -7,6 +7,10 @@ #include "logging.hpp" #include "spi.hpp" +/* + * reserve PB11-PB15 for SPI2 + */ + // STM32VL-Discovery green led - PC9 #define BTN_PORT GPIOA #define LED_PORT GPIOC @@ -15,10 +19,10 @@ #define BTN_PIN GPIO_PIN_0 #define LED_PORT_CLK_ENABLE __HAL_RCC_GPIOC_CLK_ENABLE -#define LoRa_RESET_Pin GPIO_PIN_3 -#define LoRa_RESET_GPIO_Port GPIOA -#define LoRa_CS_Pin GPIO_PIN_4 -#define LoRa_CS_GPIO_Port GPIOA +#define LoRa_RESET_Pin GPIO_PIN_4 +#define LoRa_RESET_GPIO_Port GPIOC +#define LoRa_CS_Pin GPIO_PIN_5 +#define LoRa_CS_GPIO_Port GPIOC #include "rfm95.hpp" @@ -43,7 +47,7 @@ class cmd_holder { using command_array_t = std::array; template - consteval cmd_holder(Args... command_list) + constexpr cmd_holder(Args... command_list) : commands{std::forward(command_list)...} {} constexpr functionPointerType get_func(std::string_view message) const { @@ -67,7 +71,6 @@ class cmd_holder { std::array commands; }; -template class cmd_handler { public: static constexpr auto MaxCmdLength = 24; @@ -75,11 +78,12 @@ class cmd_handler { using iterator_t = array_t::iterator; using const_iterator_t = array_t::const_iterator; - template - consteval cmd_handler(Args &&...command_list) - : commands{std::forward(command_list)...}, - symbols{}, - iterator{symbols.begin()} {} + constexpr cmd_handler() : symbols{}, iterator{symbols.begin()} {} + + static cmd_handler &get() { + static auto c = cmd_handler{}; + return c; + }; void add_symbol(uint8_t symbol) { *iterator = symbol; @@ -124,20 +128,19 @@ class cmd_handler { void print_help_() const { commands.print_help(); } private: - cmd_holder commands; + static constexpr cmd_holder<5> commands{ + std::make_tuple("ver", "Prints current version.", &print_version), + std::make_tuple("error", "Set LogLevel to Error.", &loglevel_error), + std::make_tuple("info", "Set LogLevel to Info.", &loglevel_info), + std::make_tuple("debug", "Set LogLevel to Debug.", &loglevel_debug), + std::make_tuple("help", "Prints available commands", &print_help)}; + array_t symbols; iterator_t iterator; bool ShouldExecute = false; }; -static cmd_handler<5> commands{ - std::make_tuple("ver", "Prints current version.", &print_version), - std::make_tuple("error", "Set LogLevel to Error.", &loglevel_error), - std::make_tuple("info", "Set LogLevel to Info.", &loglevel_info), - std::make_tuple("debug", "Set LogLevel to Debug.", &loglevel_debug), - std::make_tuple("help", "Prints available commands", &print_help)}; - -void print_help(void) { commands.print_help_(); } +void print_help(void) { cmd_handler::get().print_help_(); } // This prevent name mangling for functions used in C/assembly files. extern "C" { @@ -146,6 +149,12 @@ void SysTick_Handler(void) { HAL_SYSTICK_IRQHandler(); } +void USART2_IRQHandler(void) { + HAL_UART_IRQHandler(&gps_interface::s_UARTHandle); + HAL_UART_Receive_IT(&gps_interface::s_UARTHandle, uart_interface::get_buf(), + 1); +} + void USART1_IRQHandler(void) { HAL_UART_IRQHandler(&uart_interface::s_UARTHandle); HAL_UART_Receive_IT(&uart_interface::s_UARTHandle, uart_interface::get_buf(), @@ -153,15 +162,20 @@ void USART1_IRQHandler(void) { } void HAL_UART_RxCpltCallback(UART_HandleTypeDef *huart) { + if (huart != &gps_interface::s_UARTHandle) { + return; + } + const uint8_t value = *uart_interface::get_buf(); if (value == '\r') { - commands.queue_execution(); + cmd_handler::get().queue_execution(); HAL_GPIO_TogglePin(LED_PORT, LED2_PIN); return; } - commands.add_symbol(value); + gps_interface::write({reinterpret_cast(&value), 1}); + cmd_handler::get().add_symbol(value); } } @@ -215,17 +229,23 @@ int main(void) { } 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 } + if (!gps_interface::init()) { + log::error("UART2 Initialization failed, needed for GPS"); + } else { + log::debug("Uart2 Initialized"); + } + HAL_GPIO_WritePin(LoRa_CS_GPIO_Port, LoRa_CS_Pin, GPIO_PIN_SET); HAL_GPIO_WritePin(LoRa_RESET_GPIO_Port, LoRa_RESET_Pin, GPIO_PIN_SET); HAL_Delay(10); log::debug("SPI1 Initialized."); + log::debug("Initialization done."); char OP_Mode = 0x01; @@ -240,8 +260,9 @@ int main(void) { // RF95_Init(); while (1) { - commands.run(); + cmd_handler::get().run(); + gps_interface::write("TEST"); // RF95_setModeRx_Continuous(); HAL_GPIO_WritePin(LED_PORT, LED1_PIN, GPIO_PIN_RESET); // RF95_receive(LoRa_buff); diff --git a/uart_handler.cpp b/uart_handler.cpp index 11d3529..ef72567 100644 --- a/uart_handler.cpp +++ b/uart_handler.cpp @@ -3,3 +3,6 @@ template <> // uint8_t* uart_interface::rx_buff = new uint8_t[10]; uint8_t uart_interface::rx_buff = 0; + +template <> +uint8_t gps_interface::rx_buff = 0; diff --git a/uart_handler.hpp b/uart_handler.hpp index 22311a8..f8b1971 100644 --- a/uart_handler.hpp +++ b/uart_handler.hpp @@ -6,11 +6,18 @@ #include "stm32f1xx_it.h" +// USART 1 constexpr auto UART_PORT = GPIOA_BASE; #define UARTPORT GPIOA #define UARTTX_PIN GPIO_PIN_9 #define UARTRX_PIN GPIO_PIN_10 +// USART 2 +constexpr auto UART2_PORT = GPIOA_BASE; +#define UART2PORT GPIOA +#define UART2TX_PIN GPIO_PIN_2 +#define UART2RX_PIN GPIO_PIN_3 + /* * small uart wrapper * assumes USART1 with default pins/port @@ -19,6 +26,7 @@ template struct uart_handler { static void enable_clocks() { __HAL_RCC_USART1_CLK_ENABLE(); + __HAL_RCC_USART2_CLK_ENABLE(); __HAL_RCC_GPIOA_CLK_ENABLE(); __HAL_RCC_AFIO_CLK_ENABLE(); } @@ -41,6 +49,10 @@ struct uart_handler { /* USART1 interrupt Init */ HAL_NVIC_SetPriority(USART1_IRQn, 0, 0); HAL_NVIC_EnableIRQ(USART1_IRQn); + + /* USART2 interrupt Init */ + HAL_NVIC_SetPriority(USART2_IRQn, 0, 0); + HAL_NVIC_EnableIRQ(USART2_IRQn); } static bool enable_oscillators() { @@ -142,3 +154,6 @@ UART_HandleTypeDef uart_handler::s_UARTHandle = using uart_interface = uart_handler; + +using gps_interface = + uart_handler;