WIP working buffer copy with simple locking
This commit is contained in:
@@ -31,6 +31,7 @@ add_executable(stm32-blinky-f1
|
|||||||
uart_handler.cpp
|
uart_handler.cpp
|
||||||
spi.cpp
|
spi.cpp
|
||||||
rfm95.cpp
|
rfm95.cpp
|
||||||
|
commons.cpp
|
||||||
stm32f1xx_it.c
|
stm32f1xx_it.c
|
||||||
stm32f1xx_hal_conf.h
|
stm32f1xx_hal_conf.h
|
||||||
)
|
)
|
||||||
|
|||||||
1
commons.cpp
Normal file
1
commons.cpp
Normal file
@@ -0,0 +1 @@
|
|||||||
|
#include "commons.hpp"
|
||||||
122
commons.hpp
Normal file
122
commons.hpp
Normal file
@@ -0,0 +1,122 @@
|
|||||||
|
#pragma once
|
||||||
|
|
||||||
|
#include <array>
|
||||||
|
#include <span>
|
||||||
|
#include <optional>
|
||||||
|
|
||||||
|
#include "logging.hpp"
|
||||||
|
|
||||||
|
namespace commons {
|
||||||
|
|
||||||
|
template<typename LockType>
|
||||||
|
class lock
|
||||||
|
{
|
||||||
|
public:
|
||||||
|
~lock() { locker.unlock(); }
|
||||||
|
|
||||||
|
static std::optional<lock> get(LockType& l)
|
||||||
|
{
|
||||||
|
if(!l.lock())
|
||||||
|
{
|
||||||
|
log::debug("Could not acquire lock.");
|
||||||
|
return std::nullopt;
|
||||||
|
}
|
||||||
|
return lock<LockType>{l};
|
||||||
|
}
|
||||||
|
|
||||||
|
private:
|
||||||
|
explicit lock(LockType& l) : locker{l} {}
|
||||||
|
LockType& locker;
|
||||||
|
};
|
||||||
|
|
||||||
|
class mutex
|
||||||
|
{
|
||||||
|
public:
|
||||||
|
bool lock()
|
||||||
|
{
|
||||||
|
if(locked)
|
||||||
|
{
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
locked = true;
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
void unlock()
|
||||||
|
{
|
||||||
|
locked = false;
|
||||||
|
}
|
||||||
|
|
||||||
|
bool is_locked() const {
|
||||||
|
return locked;
|
||||||
|
}
|
||||||
|
|
||||||
|
protected:
|
||||||
|
bool locked = false;
|
||||||
|
};
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
template <typename T, size_t S>
|
||||||
|
struct buffer {
|
||||||
|
constexpr buffer() {}
|
||||||
|
constexpr auto size() { return S; }
|
||||||
|
|
||||||
|
bool copy_from(const std::span<T>& input, uint16_t amount)
|
||||||
|
{
|
||||||
|
const auto lk = commons::lock<commons::mutex>::get(m);
|
||||||
|
if(!lk)
|
||||||
|
{
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
if(amount > max_size)
|
||||||
|
{
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
old_pos = new_pos;
|
||||||
|
|
||||||
|
if (old_pos + amount > max_size) {
|
||||||
|
const auto size_to_copy = max_size - old_pos;
|
||||||
|
std::copy(input.begin(),
|
||||||
|
std::next(input.begin(), size_to_copy),
|
||||||
|
std::next(buf.begin(), old_pos));
|
||||||
|
old_pos = 0;
|
||||||
|
|
||||||
|
//TODO: this only works if amount is not double the size of main_buf
|
||||||
|
std::copy(std::next(input.begin(), size_to_copy),
|
||||||
|
input.begin() + amount, buf.begin());
|
||||||
|
new_pos = amount - size_to_copy;
|
||||||
|
} else {
|
||||||
|
std::copy(input.begin(),
|
||||||
|
input.begin() + amount,
|
||||||
|
std::next(buf.begin(), old_pos));
|
||||||
|
new_pos = old_pos + amount;
|
||||||
|
}
|
||||||
|
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
bool print() const
|
||||||
|
{
|
||||||
|
const auto lk = commons::lock<commons::mutex>::get(m);
|
||||||
|
if(!lk)
|
||||||
|
{
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
uart_interface::write(
|
||||||
|
{reinterpret_cast<const char *>(buf.data()), new_pos});
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
private:
|
||||||
|
static constexpr auto max_size = S;
|
||||||
|
std::array<T, S> buf{};
|
||||||
|
size_t old_pos = 0;
|
||||||
|
size_t new_pos = 0;
|
||||||
|
mutable commons::mutex m;
|
||||||
|
};
|
||||||
|
|
||||||
76
main.cpp
76
main.cpp
@@ -1,9 +1,9 @@
|
|||||||
#include <stm32f1xx_hal.h>
|
#include <stm32f1xx_hal.h>
|
||||||
#include <stm32f1xx_hal_uart.h>
|
#include <stm32f1xx_hal_uart.h>
|
||||||
|
|
||||||
#include <array>
|
|
||||||
#include <tuple>
|
#include <tuple>
|
||||||
|
|
||||||
|
#include "commons.hpp"
|
||||||
#include "logging.hpp"
|
#include "logging.hpp"
|
||||||
#include "spi.hpp"
|
#include "spi.hpp"
|
||||||
|
|
||||||
@@ -71,6 +71,11 @@ class cmd_holder {
|
|||||||
std::array<command_t, Size> commands;
|
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 {
|
class cmd_handler {
|
||||||
public:
|
public:
|
||||||
static constexpr auto MaxCmdLength = 24;
|
static constexpr auto MaxCmdLength = 24;
|
||||||
@@ -110,6 +115,8 @@ class cmd_handler {
|
|||||||
iterator = symbols.begin();
|
iterator = symbols.begin();
|
||||||
|
|
||||||
if (func == nullptr) {
|
if (func == nullptr) {
|
||||||
|
log::info("Unknown Command: ", current_cmd);
|
||||||
|
log::info("Type 'help' to show available commands.");
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -128,11 +135,12 @@ class cmd_handler {
|
|||||||
void print_help_() const { commands.print_help(); }
|
void print_help_() const { commands.print_help(); }
|
||||||
|
|
||||||
private:
|
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("ver", "Prints current version.", &print_version),
|
||||||
std::make_tuple("error", "Set LogLevel to Error.", &loglevel_error),
|
std::make_tuple("error", "Set LogLevel to Error.", &loglevel_error),
|
||||||
std::make_tuple("info", "Set LogLevel to Info.", &loglevel_info),
|
std::make_tuple("info", "Set LogLevel to Info.", &loglevel_info),
|
||||||
std::make_tuple("debug", "Set LogLevel to Debug.", &loglevel_debug),
|
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)};
|
std::make_tuple("help", "Prints available commands", &print_help)};
|
||||||
|
|
||||||
array_t symbols;
|
array_t symbols;
|
||||||
@@ -162,7 +170,7 @@ void USART1_IRQHandler(void) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
void HAL_UART_RxCpltCallback(UART_HandleTypeDef *huart) {
|
void HAL_UART_RxCpltCallback(UART_HandleTypeDef *huart) {
|
||||||
if (huart != &gps_interface::s_UARTHandle) {
|
if (huart == &gps_interface::s_UARTHandle) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -174,9 +182,43 @@ void HAL_UART_RxCpltCallback(UART_HandleTypeDef *huart) {
|
|||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
gps_interface::write({reinterpret_cast<const char *>(&value), 1});
|
|
||||||
cmd_handler::get().add_symbol(value);
|
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() {
|
void initGPIO() {
|
||||||
@@ -217,6 +259,13 @@ void initGPIO() {
|
|||||||
|
|
||||||
extern "C" {
|
extern "C" {
|
||||||
#include <stdio.h>
|
#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) {
|
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_CS_GPIO_Port, LoRa_CS_Pin, GPIO_PIN_SET);
|
||||||
HAL_GPIO_WritePin(LoRa_RESET_GPIO_Port, LoRa_RESET_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("SPI1 Initialized.");
|
||||||
|
|
||||||
log::debug("Initialization done.");
|
log::debug("Initialization done.");
|
||||||
|
|
||||||
char OP_Mode = 0x01;
|
char OP_Mode = 0x01;
|
||||||
@@ -259,16 +307,22 @@ int main(void) {
|
|||||||
HAL_GPIO_WritePin(LoRa_CS_GPIO_Port, LoRa_CS_Pin, GPIO_PIN_SET);
|
HAL_GPIO_WritePin(LoRa_CS_GPIO_Port, LoRa_CS_Pin, GPIO_PIN_SET);
|
||||||
|
|
||||||
// RF95_Init();
|
// RF95_Init();
|
||||||
|
|
||||||
while (1) {
|
while (1) {
|
||||||
cmd_handler::get().run();
|
cmd_handler::get().run();
|
||||||
|
|
||||||
gps_interface::write("TEST");
|
// gps_interface::write("TEST");
|
||||||
// RF95_setModeRx_Continuous();
|
// RF95_setModeRx_Continuous();
|
||||||
HAL_GPIO_WritePin(LED_PORT, LED1_PIN, GPIO_PIN_RESET);
|
// HAL_GPIO_WritePin(LED_PORT, LED1_PIN, GPIO_PIN_RESET);
|
||||||
// RF95_receive(LoRa_buff);
|
// RF95_receive(LoRa_buff);
|
||||||
HAL_Delay(500);
|
// HAL_Delay(100);
|
||||||
HAL_GPIO_WritePin(LED_PORT, LED1_PIN, GPIO_PIN_SET);
|
// HAL_GPIO_WritePin(LED_PORT, LED1_PIN, GPIO_PIN_SET);
|
||||||
HAL_Delay(500);
|
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)};
|
// std::string_view msg{reinterpret_cast<char *>(LoRa_buff)};
|
||||||
// log::info("Received Message");
|
// log::info("Received Message");
|
||||||
|
|||||||
@@ -22,7 +22,7 @@
|
|||||||
#define __STM32F1xx_IT_H
|
#define __STM32F1xx_IT_H
|
||||||
|
|
||||||
#ifdef __cplusplus
|
#ifdef __cplusplus
|
||||||
extern "C" {
|
extern "C" {
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
/* Private includes ----------------------------------------------------------*/
|
/* Private includes ----------------------------------------------------------*/
|
||||||
@@ -55,6 +55,9 @@ void SVC_Handler(void);
|
|||||||
void DebugMon_Handler(void);
|
void DebugMon_Handler(void);
|
||||||
void PendSV_Handler(void);
|
void PendSV_Handler(void);
|
||||||
void SysTick_Handler(void);
|
void SysTick_Handler(void);
|
||||||
|
void DMA1_Channel6_IRQHandler(void);
|
||||||
|
void USART1_IRQHandler(void);
|
||||||
|
|
||||||
/* USER CODE BEGIN EFP */
|
/* USER CODE BEGIN EFP */
|
||||||
|
|
||||||
/* USER CODE END EFP */
|
/* USER CODE END EFP */
|
||||||
|
|||||||
@@ -3,6 +3,10 @@
|
|||||||
template <>
|
template <>
|
||||||
// uint8_t* uart_interface::rx_buff = new uint8_t[10];
|
// uint8_t* uart_interface::rx_buff = new uint8_t[10];
|
||||||
uint8_t uart_interface::rx_buff = 0;
|
uint8_t uart_interface::rx_buff = 0;
|
||||||
|
template <>
|
||||||
|
std::array<uint8_t, 1> uart_interface::new_rx_buf{};
|
||||||
|
|
||||||
template <>
|
template <>
|
||||||
uint8_t gps_interface::rx_buff = 0;
|
uint8_t gps_interface::rx_buff = 0;
|
||||||
|
template <>
|
||||||
|
std::array<uint8_t, 64> gps_interface::new_rx_buf{};
|
||||||
|
|||||||
127
uart_handler.hpp
127
uart_handler.hpp
@@ -2,6 +2,7 @@
|
|||||||
|
|
||||||
#include <stm32f1xx_hal.h>
|
#include <stm32f1xx_hal.h>
|
||||||
|
|
||||||
|
#include <array>
|
||||||
#include <string_view>
|
#include <string_view>
|
||||||
|
|
||||||
#include "stm32f1xx_it.h"
|
#include "stm32f1xx_it.h"
|
||||||
@@ -22,11 +23,22 @@ constexpr auto UART2_PORT = GPIOA_BASE;
|
|||||||
* small uart wrapper
|
* small uart wrapper
|
||||||
* assumes USART1 with default pins/port
|
* assumes USART1 with default pins/port
|
||||||
*/
|
*/
|
||||||
template <uint16_t PinTX, uint16_t PinRX, uint32_t Port, uint32_t UsartBase>
|
enum class UartMode : uint8_t {
|
||||||
|
Blocking = 0,
|
||||||
|
Interrupt,
|
||||||
|
DMA,
|
||||||
|
};
|
||||||
|
|
||||||
|
template <uint16_t PinTX, uint16_t PinRX, uint32_t Port, uint32_t UsartBase,
|
||||||
|
UartMode M = UartMode::Interrupt, uint16_t Size = 1>
|
||||||
struct uart_handler {
|
struct uart_handler {
|
||||||
static void enable_clocks() {
|
static void enable_clocks() {
|
||||||
|
if constexpr (UsartBase == USART1_BASE) {
|
||||||
__HAL_RCC_USART1_CLK_ENABLE();
|
__HAL_RCC_USART1_CLK_ENABLE();
|
||||||
|
} else if constexpr (UsartBase == USART2_BASE) {
|
||||||
__HAL_RCC_USART2_CLK_ENABLE();
|
__HAL_RCC_USART2_CLK_ENABLE();
|
||||||
|
}
|
||||||
|
|
||||||
__HAL_RCC_GPIOA_CLK_ENABLE();
|
__HAL_RCC_GPIOA_CLK_ENABLE();
|
||||||
__HAL_RCC_AFIO_CLK_ENABLE();
|
__HAL_RCC_AFIO_CLK_ENABLE();
|
||||||
}
|
}
|
||||||
@@ -46,13 +58,83 @@ struct uart_handler {
|
|||||||
HAL_GPIO_Init(reinterpret_cast<GPIO_TypeDef*>(port), &ua_tx);
|
HAL_GPIO_Init(reinterpret_cast<GPIO_TypeDef*>(port), &ua_tx);
|
||||||
HAL_GPIO_Init(reinterpret_cast<GPIO_TypeDef*>(port), &ua_rx);
|
HAL_GPIO_Init(reinterpret_cast<GPIO_TypeDef*>(port), &ua_rx);
|
||||||
|
|
||||||
/* USART1 interrupt Init */
|
// if constexpr (UsartBase == USART1_BASE) {
|
||||||
HAL_NVIC_SetPriority(USART1_IRQn, 0, 0);
|
// /* USART1 interrupt Init */
|
||||||
HAL_NVIC_EnableIRQ(USART1_IRQn);
|
// HAL_NVIC_SetPriority(USART1_IRQn, 0, 0);
|
||||||
|
// HAL_NVIC_EnableIRQ(USART1_IRQn);
|
||||||
|
// }
|
||||||
|
|
||||||
/* USART2 interrupt Init */
|
///* USART2 interrupt Init */
|
||||||
|
// HAL_NVIC_SetPriority(USART2_IRQn, 0, 0);
|
||||||
|
// HAL_NVIC_EnableIRQ(USART2_IRQn);
|
||||||
|
}
|
||||||
|
|
||||||
|
static void init_mode() {
|
||||||
|
if (M == UartMode::Blocking) {
|
||||||
|
return; // nothing to od
|
||||||
|
}
|
||||||
|
|
||||||
|
if constexpr (M == UartMode::Interrupt) {
|
||||||
|
constexpr auto GlobalInterrupt = [](auto Base) {
|
||||||
|
if (Base == USART1_BASE) {
|
||||||
|
return USART1_IRQn;
|
||||||
|
}
|
||||||
|
if (Base == USART2_BASE) {
|
||||||
|
return USART2_IRQn;
|
||||||
|
}
|
||||||
|
}(UsartBase);
|
||||||
|
|
||||||
|
HAL_NVIC_SetPriority(GlobalInterrupt, 0, 0);
|
||||||
|
HAL_NVIC_EnableIRQ(GlobalInterrupt);
|
||||||
|
|
||||||
|
HAL_UART_Receive_IT(&s_UARTHandle, &rx_buff, rx_buff_size);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
if constexpr (M == UartMode::DMA) {
|
||||||
|
/* DMA controller clock enable */
|
||||||
|
__HAL_RCC_DMA1_CLK_ENABLE();
|
||||||
|
__HAL_RCC_AFIO_CLK_ENABLE();
|
||||||
|
__HAL_RCC_PWR_CLK_ENABLE();
|
||||||
|
|
||||||
|
/* System interrupt init*/
|
||||||
|
|
||||||
|
/** NOJTAG: JTAG-DP Disabled and SW-DP Enabled
|
||||||
|
*/
|
||||||
|
__HAL_AFIO_REMAP_SWJ_NOJTAG();
|
||||||
|
|
||||||
|
/* DMA interrupt init */
|
||||||
|
/* DMA1_Channel6_IRQn interrupt configuration */
|
||||||
|
HAL_NVIC_SetPriority(DMA1_Channel6_IRQn, 0, 0);
|
||||||
|
HAL_NVIC_EnableIRQ(DMA1_Channel6_IRQn);
|
||||||
|
|
||||||
|
s_DMAHandle.Instance = DMA1_Channel6;
|
||||||
|
s_DMAHandle.Init.Direction = DMA_PERIPH_TO_MEMORY;
|
||||||
|
s_DMAHandle.Init.PeriphInc = DMA_PINC_DISABLE;
|
||||||
|
s_DMAHandle.Init.MemInc = DMA_MINC_ENABLE;
|
||||||
|
s_DMAHandle.Init.PeriphDataAlignment = DMA_PDATAALIGN_BYTE;
|
||||||
|
s_DMAHandle.Init.MemDataAlignment = DMA_MDATAALIGN_BYTE;
|
||||||
|
s_DMAHandle.Init.Mode = DMA_NORMAL;
|
||||||
|
s_DMAHandle.Init.Priority = DMA_PRIORITY_LOW;
|
||||||
|
write("TRY DMA INIT");
|
||||||
|
if (HAL_DMA_Init(&s_DMAHandle) != HAL_OK) {
|
||||||
|
write("FAILED");
|
||||||
|
// Error_Handler();
|
||||||
|
}
|
||||||
|
|
||||||
|
__HAL_LINKDMA(&s_UARTHandle, hdmarx, s_DMAHandle);
|
||||||
|
|
||||||
|
/* USART1 interrupt Init */
|
||||||
HAL_NVIC_SetPriority(USART2_IRQn, 0, 0);
|
HAL_NVIC_SetPriority(USART2_IRQn, 0, 0);
|
||||||
HAL_NVIC_EnableIRQ(USART2_IRQn);
|
HAL_NVIC_EnableIRQ(USART2_IRQn);
|
||||||
|
|
||||||
|
HAL_DMA_Start_IT(
|
||||||
|
&s_DMAHandle, reinterpret_cast<uint32_t>(&s_UARTHandle.Instance->DR),
|
||||||
|
reinterpret_cast<uint32_t>(new_rx_buf.data()), new_rx_buf.size());
|
||||||
|
|
||||||
|
HAL_UARTEx_ReceiveToIdle_DMA(&s_UARTHandle, new_rx_buf.data(),
|
||||||
|
new_rx_buf.size());
|
||||||
|
__HAL_DMA_DISABLE_IT(&s_DMAHandle, DMA_IT_HT);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
static bool enable_oscillators() {
|
static bool enable_oscillators() {
|
||||||
@@ -101,32 +183,25 @@ struct uart_handler {
|
|||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
result = HAL_UART_Receive_IT(&s_UARTHandle, &rx_buff, rx_buff_size);
|
|
||||||
|
|
||||||
return result == HAL_OK;
|
return result == HAL_OK;
|
||||||
}
|
}
|
||||||
|
|
||||||
static bool init() {
|
static bool init() {
|
||||||
enable_clocks();
|
enable_clocks();
|
||||||
init_gpio();
|
init_gpio();
|
||||||
|
|
||||||
if (!enable_oscillators()) {
|
if (!enable_oscillators()) {
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
return init_uart_handle();
|
auto result = init_uart_handle();
|
||||||
|
init_mode();
|
||||||
|
return result;
|
||||||
}
|
}
|
||||||
|
|
||||||
static HAL_StatusTypeDef write(const std::string_view& message,
|
static HAL_StatusTypeDef write(const std::string_view& message,
|
||||||
uint32_t timeout = HAL_MAX_DELAY) {
|
uint32_t timeout = HAL_MAX_DELAY) {
|
||||||
// constexpr std::string_view carriage_return{"\r\n"};
|
|
||||||
|
|
||||||
return write(reinterpret_cast<uint8_t*>(const_cast<char*>(message.data())),
|
return write(reinterpret_cast<uint8_t*>(const_cast<char*>(message.data())),
|
||||||
message.size(), timeout);
|
message.size(), timeout);
|
||||||
|
|
||||||
// return write(
|
|
||||||
// reinterpret_cast<uint8_t*>(const_cast<char*>(carriage_return.data())),
|
|
||||||
// carriage_return.size(), timeout);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
static HAL_StatusTypeDef write(uint8_t* buf, uint16_t size,
|
static HAL_StatusTypeDef write(uint8_t* buf, uint16_t size,
|
||||||
@@ -135,9 +210,11 @@ struct uart_handler {
|
|||||||
}
|
}
|
||||||
|
|
||||||
static UART_HandleTypeDef s_UARTHandle;
|
static UART_HandleTypeDef s_UARTHandle;
|
||||||
|
static DMA_HandleTypeDef s_DMAHandle;
|
||||||
|
|
||||||
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; }
|
static const uint8_t* get_buf_c() { return &rx_buff; }
|
||||||
|
static std::array<uint8_t, Size> new_rx_buf;
|
||||||
|
|
||||||
private:
|
private:
|
||||||
static constexpr auto pin_tx = PinTX;
|
static constexpr auto pin_tx = PinTX;
|
||||||
@@ -148,12 +225,18 @@ struct uart_handler {
|
|||||||
static uint8_t rx_buff;
|
static uint8_t rx_buff;
|
||||||
};
|
};
|
||||||
|
|
||||||
template <uint16_t PinTX, uint16_t PinRX, uint32_t Port, uint32_t UsartBase>
|
template <uint16_t PinTX, uint16_t PinRX, uint32_t Port, uint32_t UsartBase,
|
||||||
UART_HandleTypeDef uart_handler<PinTX, PinRX, Port, UsartBase>::s_UARTHandle =
|
UartMode M, uint16_t Size>
|
||||||
UART_HandleTypeDef();
|
DMA_HandleTypeDef uart_handler<PinTX, PinRX, Port, UsartBase, M,
|
||||||
|
Size>::s_DMAHandle = DMA_HandleTypeDef();
|
||||||
|
|
||||||
using uart_interface =
|
template <uint16_t PinTX, uint16_t PinRX, uint32_t Port, uint32_t UsartBase,
|
||||||
uart_handler<UARTTX_PIN, UARTRX_PIN, UART_PORT, USART1_BASE>;
|
UartMode M, uint16_t Size>
|
||||||
|
UART_HandleTypeDef uart_handler<PinTX, PinRX, Port, UsartBase, M,
|
||||||
|
Size>::s_UARTHandle = UART_HandleTypeDef();
|
||||||
|
|
||||||
using gps_interface =
|
using uart_interface = uart_handler<UARTTX_PIN, UARTRX_PIN, UART_PORT,
|
||||||
uart_handler<UART2TX_PIN, UART2RX_PIN, UART2_PORT, USART2_BASE>;
|
USART1_BASE, UartMode::Interrupt>;
|
||||||
|
|
||||||
|
using gps_interface = uart_handler<UART2TX_PIN, UART2RX_PIN, UART2_PORT,
|
||||||
|
USART2_BASE, UartMode::DMA, 64>;
|
||||||
|
|||||||
Reference in New Issue
Block a user