Initial commit
This commit is contained in:
51
Core/Src/usart.cpp
Normal file
51
Core/Src/usart.cpp
Normal file
@@ -0,0 +1,51 @@
|
||||
#include "usart.hpp"
|
||||
|
||||
#include <algorithm>
|
||||
#include <stdint.h>
|
||||
|
||||
#include <ranges>
|
||||
#include <string_view>
|
||||
|
||||
#include "stm32f4xx_hal_uart.h"
|
||||
#include "usart.h"
|
||||
|
||||
namespace driver::usart {
|
||||
|
||||
constexpr const uint8_t TX_BUFSIZE{20};
|
||||
|
||||
Usart::Usart(USART_TypeDef* usart, uint32_t baudRate, uint32_t wordLength, uint32_t stopBits,
|
||||
uint32_t parity, uint32_t mode, uint32_t hwFlowCtl, uint32_t overSampling)
|
||||
: mHandle{.Instance = usart,
|
||||
.Init{.BaudRate = baudRate,
|
||||
.WordLength = wordLength,
|
||||
.StopBits = stopBits,
|
||||
.Parity = parity,
|
||||
.Mode = mode,
|
||||
.HwFlowCtl = hwFlowCtl,
|
||||
.OverSampling = overSampling}} {}
|
||||
|
||||
bool Usart::init() {
|
||||
return HAL_UART_Init(&mHandle) != HAL_OK;
|
||||
}
|
||||
|
||||
void Usart::print(const std::string_view str) {
|
||||
tx(str);
|
||||
};
|
||||
|
||||
void Usart::println(const std::string_view str) {
|
||||
print(str);
|
||||
tx("\r\n");
|
||||
}
|
||||
|
||||
void Usart::flush() {
|
||||
|
||||
};
|
||||
|
||||
void Usart::tx(const std::string_view range) {
|
||||
for (uint32_t pt{0}; pt <= range.size(); pt += TX_BUFSIZE) {
|
||||
HAL_UART_Transmit(&mHandle, reinterpret_cast<const uint8_t*>(range.begin() + pt),
|
||||
TX_BUFSIZE, 200);
|
||||
}
|
||||
};
|
||||
|
||||
} // namespace driver::usart
|
Reference in New Issue
Block a user