add Chapter07
This commit is contained in:
28
Chapter07/error_handling/hal/inc/hal.hpp
Normal file
28
Chapter07/error_handling/hal/inc/hal.hpp
Normal file
@@ -0,0 +1,28 @@
|
||||
#pragma once
|
||||
|
||||
#include <cstdint>
|
||||
|
||||
#include <stm32f0xx_hal.h>
|
||||
|
||||
namespace hal
|
||||
{
|
||||
inline void init()
|
||||
{
|
||||
HAL_Init();
|
||||
}
|
||||
|
||||
inline std::uint32_t get_pc()
|
||||
{
|
||||
std::uint32_t pc;
|
||||
__asm volatile ("mov %0, pc" : "=r" (pc) );
|
||||
return pc;
|
||||
}
|
||||
|
||||
struct time
|
||||
{
|
||||
static std::uint32_t get_ms()
|
||||
{
|
||||
return HAL_GetTick();
|
||||
}
|
||||
};
|
||||
}; // namespace hal
|
||||
20
Chapter07/error_handling/hal/uart/inc/uart.hpp
Normal file
20
Chapter07/error_handling/hal/uart/inc/uart.hpp
Normal file
@@ -0,0 +1,20 @@
|
||||
#pragma once
|
||||
|
||||
#include <cstdint>
|
||||
|
||||
#include <span>
|
||||
|
||||
namespace hal
|
||||
{
|
||||
class uart
|
||||
{
|
||||
public:
|
||||
virtual void init(std::uint32_t baudrate) = 0;
|
||||
virtual void write_array(const char * ptr, std::size_t len) = 0;
|
||||
|
||||
virtual void write(std::span<const char> data)
|
||||
{
|
||||
write_array(data.data(), data.size());
|
||||
}
|
||||
};
|
||||
}; // namespace hal
|
||||
29
Chapter07/error_handling/hal/uart/inc/uart_stm32.hpp
Normal file
29
Chapter07/error_handling/hal/uart/inc/uart_stm32.hpp
Normal file
@@ -0,0 +1,29 @@
|
||||
#pragma once
|
||||
|
||||
#include <span>
|
||||
|
||||
#include <cstdint>
|
||||
|
||||
#include <uart.hpp>
|
||||
|
||||
#include <stm32f0xx_hal.h>
|
||||
#include <stm32f072xb.h>
|
||||
|
||||
namespace hal
|
||||
{
|
||||
class uart_stm32 : public uart
|
||||
{
|
||||
public:
|
||||
uart_stm32(USART_TypeDef *inst);
|
||||
|
||||
void init(std::uint32_t baudrate = c_baudrate_default) override;
|
||||
|
||||
void write_array(const char * ptr, std::size_t len) override;
|
||||
|
||||
private:
|
||||
UART_HandleTypeDef huart_;
|
||||
USART_TypeDef *instance_;
|
||||
std::uint32_t baudrate_;
|
||||
static constexpr std::uint32_t c_baudrate_default = 115200;
|
||||
};
|
||||
}; // namespace hal
|
||||
31
Chapter07/error_handling/hal/uart/src/uart_stm32.cpp
Normal file
31
Chapter07/error_handling/hal/uart/src/uart_stm32.cpp
Normal file
@@ -0,0 +1,31 @@
|
||||
#include <uart_stm32.hpp>
|
||||
|
||||
hal::uart_stm32::uart_stm32(USART_TypeDef *inst)
|
||||
: instance_(inst)
|
||||
{
|
||||
}
|
||||
|
||||
void hal::uart_stm32::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;
|
||||
HAL_UART_Init(&huart_);
|
||||
}
|
||||
|
||||
void hal::uart_stm32::write_array(const char * ptr, std::size_t len)
|
||||
{
|
||||
// we must cast away costness due to ST HAL's API
|
||||
char * data_ptr = const_cast<char *>(ptr);
|
||||
HAL_UART_Transmit(&huart_, reinterpret_cast<uint8_t *>(data_ptr), len,
|
||||
HAL_MAX_DELAY);
|
||||
}
|
||||
Reference in New Issue
Block a user