From 2d3a358dfee1d5b36d70db217d4586fae03c0a61 Mon Sep 17 00:00:00 2001 From: kalipso Date: Fri, 17 Feb 2023 19:19:11 +0100 Subject: [PATCH] working receive --- CMakeLists.txt | 6 +++--- logging.hpp | 6 +++--- main.cpp | 19 +++++++++++++++++-- uart_handler.cpp | 4 ++++ uart_handler.hpp | 24 +++++++++--------------- 5 files changed, 36 insertions(+), 23 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index 3d52b7e..1a02e3b 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -6,7 +6,7 @@ set(MAIN_SOURCE_FILE main.cpp) set(CMAKE_INCLUDE_CURRENT_DIR TRUE) -set(HAL_COMP_LIST RCC GPIO CORTEX UART SPI) +set(HAL_COMP_LIST RCC GPIO CORTEX UART SPI DMA) set(CMSIS_COMP_LIST "") list(APPEND CMSIS_COMP_LIST STM32F1) @@ -31,6 +31,7 @@ add_executable(stm32-blinky-f1 uart_handler.cpp spi.cpp rfm95.cpp + stm32f1xx_it.c stm32f1xx_hal_conf.h ) @@ -42,6 +43,7 @@ target_link_libraries(stm32-blinky-f1 HAL::STM32::F1::GPIO HAL::STM32::F1::CORTEX HAL::STM32::F1::SPI + HAL::STM32::F1::DMA CMSIS::STM32::F100RB STM32::NoSys ) @@ -62,5 +64,3 @@ stm32_print_size_of_target(stm32-blinky-f1) # STM32::NoSys #) #stm32_print_size_of_target(stm32-blinky-f1) - - diff --git a/logging.hpp b/logging.hpp index 1f6e5b0..db3c711 100644 --- a/logging.hpp +++ b/logging.hpp @@ -32,7 +32,7 @@ inline std::string_view get_loglevel_string(LogLevel level) { class logging_adapter { public: - virtual bool init(){} + virtual bool init() { return true; } virtual void log(std::string_view message) const = 0; }; @@ -79,8 +79,8 @@ class log { return; } - //std::stringstream msg; - //msg << get_loglevel_string(level) << ": " << message; + // std::stringstream msg; + // msg << get_loglevel_string(level) << ": " << message; logger->log(get_loglevel_string(level)); logger->log(": "); logger->log(message); diff --git a/main.cpp b/main.cpp index c9b088b..6828099 100644 --- a/main.cpp +++ b/main.cpp @@ -1,4 +1,5 @@ #include +#include #include "logging.hpp" #include "spi.hpp" @@ -27,6 +28,22 @@ void SysTick_Handler(void) { HAL_IncTick(); HAL_SYSTICK_IRQHandler(); } + +void USART1_IRQHandler(void) { + HAL_UART_IRQHandler(&uart_interface::s_UARTHandle); + HAL_UART_Receive_IT(&uart_interface::s_UARTHandle, uart_interface::get_buf(), + 1); +} + +void HAL_UART_RxCpltCallback(UART_HandleTypeDef *huart) { + const char value = *reinterpret_cast(uart_interface::get_buf()); + + if (value == '\r') { + HAL_GPIO_TogglePin(LED_PORT, LED2_PIN); + } + + log::debug({&value, 1}); +} } void initGPIO() { @@ -105,11 +122,9 @@ int main(void) { while (1) { // RF95_setModeRx_Continuous(); HAL_GPIO_WritePin(LED_PORT, LED1_PIN, GPIO_PIN_RESET); - 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)}; diff --git a/uart_handler.cpp b/uart_handler.cpp index 69e1d81..11d3529 100644 --- a/uart_handler.cpp +++ b/uart_handler.cpp @@ -1 +1,5 @@ #include "uart_handler.hpp" + +template <> +// uint8_t* uart_interface::rx_buff = new uint8_t[10]; +uint8_t uart_interface::rx_buff = 0; diff --git a/uart_handler.hpp b/uart_handler.hpp index 6f616a0..22311a8 100644 --- a/uart_handler.hpp +++ b/uart_handler.hpp @@ -4,6 +4,8 @@ #include +#include "stm32f1xx_it.h" + constexpr auto UART_PORT = GPIOA_BASE; #define UARTPORT GPIOA #define UARTTX_PIN GPIO_PIN_9 @@ -37,8 +39,8 @@ struct uart_handler { HAL_GPIO_Init(reinterpret_cast(port), &ua_rx); /* USART1 interrupt Init */ - // HAL_NVIC_SetPriority(USART1_IRQn, 0, 0); - // HAL_NVIC_EnableIRQ(USART1_IRQn); + HAL_NVIC_SetPriority(USART1_IRQn, 0, 0); + HAL_NVIC_EnableIRQ(USART1_IRQn); } static bool enable_oscillators() { @@ -87,7 +89,7 @@ struct uart_handler { return false; } - // result = HAL_UART_Receive_IT(&s_UARTHandle, rx_buff, rx_buff_size); + result = HAL_UART_Receive_IT(&s_UARTHandle, &rx_buff, rx_buff_size); return result == HAL_OK; } @@ -122,15 +124,16 @@ struct uart_handler { static UART_HandleTypeDef s_UARTHandle; - static uint8_t* get_buf() { return rx_buff; } + static uint8_t* get_buf() { return &rx_buff; } + static const uint8_t* get_buf_c() { 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; + static constexpr size_t rx_buff_size = 1; + static uint8_t rx_buff; }; template @@ -139,12 +142,3 @@ UART_HandleTypeDef uart_handler::s_UARTHandle = 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); -// }