WIP adding uart2

This commit is contained in:
2023-02-23 11:03:57 +01:00
parent 045302cc96
commit 32eca023a5
3 changed files with 63 additions and 24 deletions
+45 -24
View File
@@ -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<command_t, Size>;
template <typename... Args>
consteval cmd_holder(Args... command_list)
constexpr cmd_holder(Args... command_list)
: commands{std::forward<Args>(command_list)...} {}
constexpr functionPointerType get_func(std::string_view message) const {
@@ -67,7 +71,6 @@ class cmd_holder {
std::array<command_t, Size> commands;
};
template <size_t Size>
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 <typename... Args>
consteval cmd_handler(Args &&...command_list)
: commands{std::forward<Args &&>(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<Size> 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<const char *>(&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);