add Chapter04 bare example
This commit is contained in:
21
Chapter04/bare/hal/inc/hal.hpp
Normal file
21
Chapter04/bare/hal/inc/hal.hpp
Normal file
@@ -0,0 +1,21 @@
|
||||
#pragma once
|
||||
|
||||
#include <cstdint>
|
||||
|
||||
#include <stm32f0xx_hal.h>
|
||||
|
||||
namespace hal
|
||||
{
|
||||
inline void init()
|
||||
{
|
||||
HAL_Init();
|
||||
}
|
||||
|
||||
struct time
|
||||
{
|
||||
inline static std::uint32_t get_ms()
|
||||
{
|
||||
return HAL_GetTick();
|
||||
}
|
||||
};
|
||||
}; // namespace hal
|
||||
28
Chapter04/bare/hal/uart/inc/uart.hpp
Normal file
28
Chapter04/bare/hal/uart/inc/uart.hpp
Normal file
@@ -0,0 +1,28 @@
|
||||
#pragma once
|
||||
|
||||
#include <cstdint>
|
||||
|
||||
namespace hal
|
||||
{
|
||||
class uart
|
||||
{
|
||||
public:
|
||||
virtual void init() = 0;
|
||||
|
||||
virtual void putchar(char c) = 0;
|
||||
|
||||
void puts(const char *str)
|
||||
{
|
||||
std::size_t ind = 0;
|
||||
|
||||
if(str != nullptr)
|
||||
{
|
||||
while(str[ind] != '\0')
|
||||
{
|
||||
putchar(str[ind]);
|
||||
ind++;
|
||||
}
|
||||
}
|
||||
}
|
||||
};
|
||||
}; // namespace hal
|
||||
25
Chapter04/bare/hal/uart/inc/uart_stm32.hpp
Normal file
25
Chapter04/bare/hal/uart/inc/uart_stm32.hpp
Normal file
@@ -0,0 +1,25 @@
|
||||
#pragma once
|
||||
|
||||
#include <cstdint>
|
||||
|
||||
#include <uart.hpp>
|
||||
|
||||
#include <stm32f0xx_hal.h>
|
||||
|
||||
namespace hal
|
||||
{
|
||||
class uart_stm32 : public uart
|
||||
{
|
||||
public:
|
||||
uart_stm32(USART_TypeDef *inst, std::uint32_t baud = 115200);
|
||||
|
||||
void init() override;
|
||||
|
||||
void putchar(char c) override;
|
||||
|
||||
private:
|
||||
UART_HandleTypeDef huart;
|
||||
USART_TypeDef *instance;
|
||||
std::uint32_t baudrate;
|
||||
};
|
||||
}; // namespace hal
|
||||
33
Chapter04/bare/hal/uart/src/uart_stm32.cpp
Normal file
33
Chapter04/bare/hal/uart/src/uart_stm32.cpp
Normal file
@@ -0,0 +1,33 @@
|
||||
#include <uart_stm32.hpp>
|
||||
|
||||
hal::uart_stm32::uart_stm32(USART_TypeDef *inst, std::uint32_t baud)
|
||||
: instance(inst), baudrate(baud)
|
||||
{
|
||||
}
|
||||
|
||||
void hal::uart_stm32::init()
|
||||
{
|
||||
huart.Instance = USART2;
|
||||
huart.Init.BaudRate = 115200;
|
||||
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;
|
||||
|
||||
// huart.MspInitCallback = mspInitCallback;
|
||||
|
||||
if(HAL_UART_Init(&huart) != HAL_OK)
|
||||
{
|
||||
// Error_Handler();
|
||||
}
|
||||
}
|
||||
|
||||
void hal::uart_stm32::putchar(char c)
|
||||
{
|
||||
HAL_UART_Transmit(&huart, reinterpret_cast<uint8_t *>(&c), 1,
|
||||
HAL_MAX_DELAY);
|
||||
}
|
||||
Reference in New Issue
Block a user