add Chapter14

This commit is contained in:
Amar Mahmutbegovic
2024-11-19 21:29:22 +01:00
parent 49b062908b
commit b378383cf5
1393 changed files with 1058616 additions and 0 deletions

View File

@@ -0,0 +1,21 @@
#pragma once
#include <stm32f0xx_hal.h>
#include <stm32f072xb.h>
namespace hal::stm32{
struct uart {
uart() = delete;
static inline HAL_StatusTypeDef init(UART_HandleTypeDef *huart) {
return HAL_UART_Init(huart);
}
static inline HAL_StatusTypeDef transmit(UART_HandleTypeDef *huart,
uint8_t *pData,
uint16_t Size,
uint32_t Timeout) {
return HAL_UART_Transmit(huart, pData, Size, Timeout);
}
};
};

View File

@@ -0,0 +1,14 @@
#pragma once
#include <cstdint>
#include <span>
namespace hal
{
class uart
{
public:
virtual void init(std::uint32_t baudrate) = 0;
virtual void write(std::span<const char> data) = 0;
};
}; // namespace hal

View File

@@ -0,0 +1,46 @@
#pragma once
#include <span>
#include <cstdint>
#include <uart.hpp>
#include <stm32f0xx_hal_uart.hpp>
namespace hal
{
template <typename HalUart>
class uart_stm32 : public uart
{
public:
uart_stm32(USART_TypeDef *inst) : instance_(inst) {}
void init(std::uint32_t baudrate = c_baudrate_default) override {
huart_.Instance = instance_;
huart_.Init.BaudRate = baudrate;
huart_.Init.WordLength = UART_WORDLENGTH_8B;
huart_.Init.StopBits = UART_STOPBITS_1;
huart_.Init.Parity = UART_PARITY_NONE;
huart_.Init.Mode = UART_MODE_TX_RX;
huart_.Init.HwFlowCtl = UART_HWCONTROL_NONE;
huart_.Init.OverSampling = UART_OVERSAMPLING_16;
huart_.Init.OneBitSampling = UART_ONE_BIT_SAMPLE_DISABLE;
huart_.AdvancedInit.AdvFeatureInit = UART_ADVFEATURE_NO_INIT;
// TODO: add GPIO initialization for real hardware
huart_.MspInitCallback = nullptr;
HalUart::init(&huart_);
}
void write(std::span<const char> data) override {
// we must cast away costness due to ST HAL's API
char * data_ptr = const_cast<char *>(data.data());
HalUart::transmit(&huart_, reinterpret_cast<uint8_t *>(data_ptr), data.size(),
HAL_MAX_DELAY);
}
private:
UART_HandleTypeDef huart_;
USART_TypeDef *instance_;
std::uint32_t baudrate_;
static constexpr std::uint32_t c_baudrate_default = 115200;
};
}; // namespace hal

View File

@@ -0,0 +1,34 @@
#include <uart_stm32.hpp>
template <typename HalUart>
hal::uart_stm32<HalUart>::uart_stm32(USART_TypeDef *inst)
: instance_(inst)
{
}
template <typename HalUart>
void hal::uart_stm32<HalUart>::init(std::uint32_t baudrate)
{
huart_.Instance = instance_;
huart_.Init.BaudRate = baudrate;
huart_.Init.WordLength = UART_WORDLENGTH_8B;
huart_.Init.StopBits = UART_STOPBITS_1;
huart_.Init.Parity = UART_PARITY_NONE;
huart_.Init.Mode = UART_MODE_TX_RX;
huart_.Init.HwFlowCtl = UART_HWCONTROL_NONE;
huart_.Init.OverSampling = UART_OVERSAMPLING_16;
huart_.Init.OneBitSampling = UART_ONE_BIT_SAMPLE_DISABLE;
huart_.AdvancedInit.AdvFeatureInit = UART_ADVFEATURE_NO_INIT;
// TODO: add GPIO initialization for real hardware
huart_.MspInitCallback = nullptr;
HalUart::init(&huart_);
}
template <typename HalUart>
void hal::uart_stm32<HalUart>::write(std::span<const char> data)
{
// we must cast away costness due to ST HAL's API
char * data_ptr = const_cast<char *>(data.data());
HalUart::transmit(&huart_, reinterpret_cast<uint8_t *>(data_ptr), data.size(),
HAL_MAX_DELAY);
}