WIP working buffer copy with simple locking

This commit is contained in:
2023-02-27 23:17:11 +01:00
parent c3d62bba08
commit 9b22bc6acf
8 changed files with 322 additions and 53 deletions
+66 -12
View File
@@ -1,9 +1,9 @@
#include <stm32f1xx_hal.h>
#include <stm32f1xx_hal_uart.h>
#include <array>
#include <tuple>
#include "commons.hpp"
#include "logging.hpp"
#include "spi.hpp"
@@ -71,6 +71,11 @@ class cmd_holder {
std::array<command_t, Size> commands;
};
static bool updated_main_buf = false;
static buffer<uint8_t, 512> main_buffer;
void print_buf(void) { main_buffer.print(); }
class cmd_handler {
public:
static constexpr auto MaxCmdLength = 24;
@@ -110,6 +115,8 @@ class cmd_handler {
iterator = symbols.begin();
if (func == nullptr) {
log::info("Unknown Command: ", current_cmd);
log::info("Type 'help' to show available commands.");
return;
}
@@ -128,11 +135,12 @@ class cmd_handler {
void print_help_() const { commands.print_help(); }
private:
static constexpr cmd_holder<5> commands{
static constexpr cmd_holder<6> 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("buf", "Prints gps uart buffer", &print_buf),
std::make_tuple("help", "Prints available commands", &print_help)};
array_t symbols;
@@ -162,7 +170,7 @@ void USART1_IRQHandler(void) {
}
void HAL_UART_RxCpltCallback(UART_HandleTypeDef *huart) {
if (huart != &gps_interface::s_UARTHandle) {
if (huart == &gps_interface::s_UARTHandle) {
return;
}
@@ -174,9 +182,43 @@ void HAL_UART_RxCpltCallback(UART_HandleTypeDef *huart) {
return;
}
gps_interface::write({reinterpret_cast<const char *>(&value), 1});
cmd_handler::get().add_symbol(value);
}
void HAL_UART_TxCpltCallback(UART_HandleTypeDef *huart) {
// const uint8_t value = *gps_interface::get_buf();
// gps_interface::write({reinterpret_cast<const char *>(&value), 1});
}
void HAL_UARTEx_RxEventCallback(UART_HandleTypeDef *huart, uint16_t Size) {
// log::debug("DMA Callback");
HAL_GPIO_WritePin(LED_PORT, LED1_PIN, GPIO_PIN_SET);
if (main_buffer.copy_from(gps_interface::new_rx_buf, Size)) {
log::debug("Copied to main buff");
updated_main_buf = true;
}
HAL_GPIO_WritePin(LED_PORT, LED1_PIN, GPIO_PIN_RESET);
HAL_UARTEx_ReceiveToIdle_DMA(&gps_interface::s_UARTHandle,
gps_interface::new_rx_buf.data(),
gps_interface::new_rx_buf.size());
__HAL_DMA_DISABLE_IT(&gps_interface::s_DMAHandle, DMA_IT_HT);
}
/**
* @brief This function handles DMA1 channel6 global interrupt.
*/
void DMA1_Channel6_IRQHandler(void) {
/* USER CODE BEGIN DMA1_Channel6_IRQn 0 */
/* USER CODE END DMA1_Channel6_IRQn 0 */
HAL_DMA_IRQHandler(&gps_interface::s_DMAHandle);
/* USER CODE BEGIN DMA1_Channel6_IRQn 1 */
/* USER CODE END DMA1_Channel6_IRQn 1 */
}
}
void initGPIO() {
@@ -217,6 +259,13 @@ void initGPIO() {
extern "C" {
#include <stdio.h>
void start_interrupt() {
HAL_UARTEx_ReceiveToIdle_DMA(&gps_interface::s_UARTHandle,
gps_interface::new_rx_buf.data(),
gps_interface::new_rx_buf.size());
__HAL_DMA_DISABLE_IT(&gps_interface::s_DMAHandle, DMA_IT_HT);
}
}
int main(void) {
@@ -242,10 +291,9 @@ int main(void) {
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);
HAL_Delay(1000);
log::debug("SPI1 Initialized.");
log::debug("Initialization done.");
char OP_Mode = 0x01;
@@ -259,16 +307,22 @@ int main(void) {
HAL_GPIO_WritePin(LoRa_CS_GPIO_Port, LoRa_CS_Pin, GPIO_PIN_SET);
// RF95_Init();
while (1) {
cmd_handler::get().run();
gps_interface::write("TEST");
// RF95_setModeRx_Continuous();
HAL_GPIO_WritePin(LED_PORT, LED1_PIN, GPIO_PIN_RESET);
// gps_interface::write("TEST");
// RF95_setModeRx_Continuous();
// HAL_GPIO_WritePin(LED_PORT, LED1_PIN, GPIO_PIN_RESET);
// RF95_receive(LoRa_buff);
HAL_Delay(500);
HAL_GPIO_WritePin(LED_PORT, LED1_PIN, GPIO_PIN_SET);
HAL_Delay(500);
// HAL_Delay(100);
// HAL_GPIO_WritePin(LED_PORT, LED1_PIN, GPIO_PIN_SET);
HAL_Delay(100);
// if (updated_main_buf) {
// log::debug("Printing main buf");
// updated_main_buf = !main_buffer.print();
// }
// std::string_view msg{reinterpret_cast<char *>(LoRa_buff)};
// log::info("Received Message");