add logging structure

This commit is contained in:
2023-02-17 15:18:10 +01:00
parent 514670e897
commit b78396e8e5
5 changed files with 157 additions and 29 deletions

View File

@@ -4,11 +4,15 @@
#include <string_view>
constexpr auto UART_PORT = GPIOA_BASE;
#define UARTPORT GPIOA
#define UARTTX_PIN GPIO_PIN_9
#define UARTRX_PIN GPIO_PIN_10
/*
* small uart wrapper
* assumes USART1 with default pins/port
*/
template <uint16_t PinTX, uint16_t PinRX, uint32_t Port, uint32_t UsartBase>
struct uart_handler {
static void enable_clocks() {
@@ -31,6 +35,10 @@ struct uart_handler {
HAL_GPIO_Init(reinterpret_cast<GPIO_TypeDef*>(port), &ua_tx);
HAL_GPIO_Init(reinterpret_cast<GPIO_TypeDef*>(port), &ua_rx);
/* USART1 interrupt Init */
// HAL_NVIC_SetPriority(USART1_IRQn, 0, 0);
// HAL_NVIC_EnableIRQ(USART1_IRQn);
}
static bool enable_oscillators() {
@@ -75,6 +83,12 @@ struct uart_handler {
auto result = HAL_UART_Init(&s_UARTHandle);
if (result != HAL_OK) {
return false;
}
// result = HAL_UART_Receive_IT(&s_UARTHandle, rx_buff, rx_buff_size);
return result == HAL_OK;
}
@@ -91,14 +105,14 @@ struct uart_handler {
static HAL_StatusTypeDef write(const std::string_view& message,
uint32_t timeout = HAL_MAX_DELAY) {
constexpr std::string_view carriage_return{"\r\n"};
// constexpr std::string_view carriage_return{"\r\n"};
write(reinterpret_cast<uint8_t*>(const_cast<char*>(message.data())),
message.size(), timeout);
return write(reinterpret_cast<uint8_t*>(const_cast<char*>(message.data())),
message.size(), timeout);
return write(
reinterpret_cast<uint8_t*>(const_cast<char*>(carriage_return.data())),
carriage_return.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,
@@ -108,13 +122,29 @@ struct uart_handler {
static UART_HandleTypeDef s_UARTHandle;
static uint8_t* get_buf() { 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;
};
template <uint16_t PinTX, uint16_t PinRX, uint32_t Port, uint32_t UsartBase>
UART_HandleTypeDef uart_handler<PinTX, PinRX, Port, UsartBase>::s_UARTHandle =
UART_HandleTypeDef();
using uart_interface =
uart_handler<UARTTX_PIN, UARTRX_PIN, UART_PORT, USART1_BASE>;
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);
// }