generated from Template/H563ZI-HAL-CMake-Template
Compare commits
13 Commits
be081086cb
...
dev
| Author | SHA1 | Date | |
|---|---|---|---|
| 2ce600dc4d | |||
| 69155f3ce1 | |||
| c3e0edd72d | |||
| 38aaa9b8b3 | |||
| a7485fca40 | |||
| 21eec9089e | |||
| 72dcf53b3e | |||
| 83d9a263a4 | |||
| b4ecf03e95 | |||
| 52a4669c0d | |||
| f87404cba6 | |||
| f6541a5fbd | |||
| aa43e21103 |
@@ -2,11 +2,17 @@
|
||||
|
||||
#include "../Common/can_mv.hpp"
|
||||
#include "../Common/df_player.hpp"
|
||||
#include "../Common/hc05.hpp"
|
||||
#include "../Common/ultrasonic.hpp"
|
||||
#include "../config.hpp"
|
||||
#include "Common/serial_port.hpp"
|
||||
|
||||
class CommonCenter {
|
||||
public:
|
||||
CommonCenter() = delete;
|
||||
CommonCenter(const CommonCenter&) = delete;
|
||||
CommonCenter& operator=(const CommonCenter&) = delete;
|
||||
|
||||
static Ultrasonic& GetUltrasonic() {
|
||||
static Ultrasonic* instance = nullptr;
|
||||
if (instance == nullptr) {
|
||||
@@ -31,4 +37,23 @@ public:
|
||||
}
|
||||
return *instance;
|
||||
}
|
||||
|
||||
static Hc05& GetHc05() {
|
||||
static Hc05* instance = nullptr;
|
||||
if (instance == nullptr) {
|
||||
instance = new Hc05(Config::kHc05Uart);
|
||||
}
|
||||
return *instance;
|
||||
}
|
||||
|
||||
static SerialPort& GetDebugSerialPort() {
|
||||
static constexpr auto kBaudRate = 115200;
|
||||
static constexpr auto kLength = 256;
|
||||
static constexpr auto kTimeout = 1000;
|
||||
static SerialPort* instance = nullptr;
|
||||
if (instance == nullptr) {
|
||||
instance = new SerialPort(Config::kDebugUart, kBaudRate, kLength, kTimeout);
|
||||
}
|
||||
return *instance;
|
||||
}
|
||||
};
|
||||
|
||||
@@ -1,19 +1,24 @@
|
||||
#pragma once
|
||||
|
||||
#include <cstdint>
|
||||
#include <memory>
|
||||
|
||||
#include "../Common/serial_port.hpp"
|
||||
|
||||
class CanMv {
|
||||
public:
|
||||
explicit CanMv(UART_HandleTypeDef* uart) : serialPort(new SerialPort(uart, kLength, kTimeout)) {}
|
||||
explicit CanMv(UART_HandleTypeDef* uart)
|
||||
: serialPort(std::make_unique<SerialPort>(uart, kBaudRate, kLength, kTimeout)) {}
|
||||
|
||||
~CanMv() {
|
||||
delete serialPort;
|
||||
}
|
||||
~CanMv() = default;
|
||||
CanMv(const CanMv&) = delete;
|
||||
CanMv& operator=(const CanMv&) = delete;
|
||||
CanMv(CanMv&&) = delete;
|
||||
CanMv& operator=(CanMv&&) = delete;
|
||||
|
||||
private:
|
||||
static constexpr uint32_t kLength = 128;
|
||||
static constexpr uint32_t kTimeout = 10;
|
||||
SerialPort* serialPort;
|
||||
static constexpr uint32_t kBaudRate = 115200;
|
||||
static constexpr uint32_t kLength = 128;
|
||||
static constexpr uint32_t kTimeout = 10;
|
||||
std::unique_ptr<SerialPort> serialPort;
|
||||
};
|
||||
|
||||
@@ -1,16 +1,20 @@
|
||||
#pragma once
|
||||
|
||||
#include <cstdint>
|
||||
#include <memory>
|
||||
|
||||
#include "../Common/serial_port.hpp"
|
||||
|
||||
class DfPlayer {
|
||||
public:
|
||||
explicit DfPlayer(UART_HandleTypeDef* uart) : serialPort(new SerialPort(uart, kLength, kTimeout)) {}
|
||||
explicit DfPlayer(UART_HandleTypeDef* uart)
|
||||
: serialPort(std::make_unique<SerialPort>(uart, kBaudRate, kLength, kTimeout)) {}
|
||||
|
||||
~DfPlayer() {
|
||||
delete serialPort;
|
||||
}
|
||||
~DfPlayer() = default;
|
||||
DfPlayer(const DfPlayer&) = delete;
|
||||
DfPlayer& operator=(const DfPlayer&) = delete;
|
||||
DfPlayer(DfPlayer&&) = delete;
|
||||
DfPlayer& operator=(DfPlayer&&) = delete;
|
||||
|
||||
void Play();
|
||||
void Stop();
|
||||
@@ -50,9 +54,10 @@ private:
|
||||
kQueryFlashCurrentTrack = 0x4D,
|
||||
};
|
||||
|
||||
static constexpr uint32_t kLength = 128;
|
||||
static constexpr uint32_t kTimeout = 10;
|
||||
SerialPort* serialPort;
|
||||
static constexpr uint32_t kBaudRate = 9600;
|
||||
static constexpr uint32_t kLength = 128;
|
||||
static constexpr uint32_t kTimeout = 10;
|
||||
std::unique_ptr<SerialPort> serialPort;
|
||||
|
||||
void SendCommand(Command cmd, uint16_t param = 0);
|
||||
|
||||
|
||||
47
Core/App/Common/hc05.hpp
Normal file
47
Core/App/Common/hc05.hpp
Normal file
@@ -0,0 +1,47 @@
|
||||
#pragma once
|
||||
|
||||
#include <cerrno>
|
||||
#include <cstdint>
|
||||
|
||||
#include "../Common/serial_port.hpp"
|
||||
|
||||
class Hc05 {
|
||||
private:
|
||||
static constexpr uint32_t kBaudRate = 9600;
|
||||
static constexpr uint32_t kLength = 128;
|
||||
static constexpr uint32_t kTimeout = 10;
|
||||
std::unique_ptr<SerialPort> serialPort;
|
||||
|
||||
static constexpr uint8_t kCommand = 0xAA;
|
||||
|
||||
public:
|
||||
// App 返回单个字节 Command 结果
|
||||
enum class Response : uint8_t {
|
||||
kForward, // 前进
|
||||
kBackward, // 后退
|
||||
kTurnLeft, // 左转
|
||||
kTurnRight, // 右转
|
||||
kForwardLeft, // 左前方
|
||||
kForwardRight, // 右前方
|
||||
kBackwardLeft, // 左后方
|
||||
kBackwardRight, // 右后方
|
||||
kTurnAround, // 掉头/180度转向
|
||||
kStop, // 停止,到达目的地
|
||||
};
|
||||
|
||||
explicit Hc05(UART_HandleTypeDef* uart)
|
||||
: serialPort(std::make_unique<SerialPort>(uart, kBaudRate, kLength, kTimeout)) {}
|
||||
|
||||
~Hc05() = default;
|
||||
Hc05(const Hc05&) = delete;
|
||||
Hc05& operator=(const Hc05&) = delete;
|
||||
Hc05(Hc05&&) = delete;
|
||||
Hc05& operator=(Hc05&&) = delete;
|
||||
|
||||
Response SendCommand() {
|
||||
Response data;
|
||||
serialPort->WriteBytesBlocking(reinterpret_cast<const uint8_t*>(&kCommand), sizeof(kCommand));
|
||||
serialPort->ReadBytesBlocking(reinterpret_cast<uint8_t*>(&data), sizeof(data));
|
||||
return data;
|
||||
}
|
||||
};
|
||||
36
Core/App/Common/key.hpp
Normal file
36
Core/App/Common/key.hpp
Normal file
@@ -0,0 +1,36 @@
|
||||
#pragma once
|
||||
|
||||
#include "../Helper/delay_helper.h"
|
||||
#include "stm32h5xx_hal_gpio.h"
|
||||
#include <cstdint>
|
||||
|
||||
#include "../Helper/gpio_helper.hpp"
|
||||
|
||||
class Key {
|
||||
private:
|
||||
static constexpr uint32_t kDebounceDelay = 20;
|
||||
GpioHelper::Gpio gpio;
|
||||
GPIO_PinState normalStatus;
|
||||
|
||||
public:
|
||||
explicit Key(GpioHelper::Gpio gpio, GPIO_PinState normalStatus) : gpio(gpio), normalStatus(normalStatus) {
|
||||
GpioHelper::GpioInit(gpio, GPIO_MODE_INPUT, normalStatus ? GPIO_PULLUP : GPIO_PULLDOWN);
|
||||
}
|
||||
~Key() = default;
|
||||
Key(const Key&) = delete;
|
||||
Key& operator=(const Key&) = delete;
|
||||
Key(Key&&) = delete;
|
||||
Key& operator=(Key&&) = delete;
|
||||
|
||||
bool IsPressed() {
|
||||
auto reading = gpio.Read();
|
||||
if (reading != normalStatus) {
|
||||
DelayMs(kDebounceDelay);
|
||||
reading = gpio.Read();
|
||||
if (reading != normalStatus) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
return false;
|
||||
}
|
||||
};
|
||||
@@ -1,6 +1,7 @@
|
||||
#pragma once
|
||||
|
||||
#include "main.h"
|
||||
#include "stm32h5xx_hal_uart.h"
|
||||
#include <cstdarg>
|
||||
#include <cstdint>
|
||||
#include <cstring>
|
||||
@@ -13,23 +14,35 @@ private:
|
||||
uint32_t timeout;
|
||||
std::unique_ptr<char[]> buffer;
|
||||
|
||||
uint8_t ReadByteForce() {
|
||||
return handle->Instance->RDR;
|
||||
}
|
||||
|
||||
public:
|
||||
SerialPort(UART_HandleTypeDef* uart, uint32_t length, uint32_t timeout)
|
||||
: handle(uart), timeout(timeout), buffer(std::make_unique<char[]>(length)) {}
|
||||
SerialPort(UART_HandleTypeDef* uart, uint32_t baudRate, uint32_t length, uint32_t timeout)
|
||||
: handle(uart), timeout(timeout), buffer(std::make_unique<char[]>(length)) {
|
||||
SetBaudRate(baudRate);
|
||||
}
|
||||
|
||||
~SerialPort() = default;
|
||||
SerialPort(const SerialPort&) = delete;
|
||||
SerialPort& operator=(const SerialPort&) = delete;
|
||||
SerialPort(SerialPort&&) = delete;
|
||||
SerialPort& operator=(SerialPort&&) = delete;
|
||||
|
||||
uint8_t ReadByte() {
|
||||
return handle->Instance->RDR;
|
||||
void SetBaudRate(uint32_t baudRate) {
|
||||
HAL_UART_DeInit(handle);
|
||||
handle->Init.BaudRate = baudRate;
|
||||
HAL_UART_Init(handle);
|
||||
}
|
||||
|
||||
std::string ReadLine() {
|
||||
std::string result;
|
||||
result.reserve(128);
|
||||
char c;
|
||||
ReadByte();
|
||||
ReadByteForce();
|
||||
while (true) {
|
||||
auto r = HAL_UART_Receive(handle, reinterpret_cast<uint8_t*>(&c), sizeof(c), timeout);
|
||||
auto r = HAL_UART_Receive(handle, reinterpret_cast<uint8_t*>(&c), sizeof(c), timeout);
|
||||
if (r != HAL_OK) {
|
||||
throw std::runtime_error("UART receive error");
|
||||
}
|
||||
|
||||
@@ -19,6 +19,12 @@ public:
|
||||
captureFlag = GetCaptureFlag(channel);
|
||||
}
|
||||
|
||||
~Ultrasonic() = default;
|
||||
Ultrasonic(const Ultrasonic&) = delete;
|
||||
Ultrasonic& operator=(const Ultrasonic&) = delete;
|
||||
Ultrasonic(Ultrasonic&&) = delete;
|
||||
Ultrasonic& operator=(Ultrasonic&&) = delete;
|
||||
|
||||
double GetDistance(uint32_t time = 1) {
|
||||
double distance = 0;
|
||||
for (uint32_t cnt = 0; cnt < time; ++cnt) {
|
||||
@@ -46,6 +52,7 @@ private:
|
||||
double MeasureEchoTime() {
|
||||
timer->Instance->CNT = 0;
|
||||
uint32_t data[2];
|
||||
__HAL_TIM_CLEAR_FLAG(timer, captureFlag);
|
||||
HAL_TIM_IC_Start(timer, channel);
|
||||
try {
|
||||
for (auto& value : data) {
|
||||
|
||||
@@ -6,6 +6,12 @@
|
||||
|
||||
class GpioHelper {
|
||||
public:
|
||||
GpioHelper() = delete;
|
||||
GpioHelper(const GpioHelper&) = delete;
|
||||
GpioHelper& operator=(const GpioHelper&) = delete;
|
||||
GpioHelper(GpioHelper&&) = delete;
|
||||
GpioHelper& operator=(GpioHelper&&) = delete;
|
||||
|
||||
struct Gpio {
|
||||
GPIO_TypeDef* port;
|
||||
uint16_t pin;
|
||||
@@ -56,7 +62,8 @@ public:
|
||||
return {port, pin};
|
||||
}
|
||||
|
||||
static Gpio GpioInit(const Gpio& gpio, const uint32_t mode, const uint32_t pull, const GPIO_PinState state) {
|
||||
static Gpio GpioInit(const Gpio& gpio, const uint32_t mode, const uint32_t pull,
|
||||
const std::optional<GPIO_PinState> state = std::nullopt) {
|
||||
return GpioInit(gpio.port, gpio.pin, mode, pull, state);
|
||||
}
|
||||
|
||||
|
||||
@@ -4,16 +4,15 @@
|
||||
#include <cstdint>
|
||||
#include <stdexcept>
|
||||
|
||||
class TaskHelper {
|
||||
public:
|
||||
namespace TaskHelper {
|
||||
template <typename Func>
|
||||
static void WaitFor(Func func, int timeoutMilliseconds) {
|
||||
static void WaitFor(Func func, uint32_t timeoutMilliseconds) {
|
||||
auto start = HAL_GetTick();
|
||||
while (!func()) {
|
||||
uint32_t now = HAL_GetTick();
|
||||
if ((now - start) > static_cast<uint32_t>(timeoutMilliseconds)) {
|
||||
if ((now - start) > timeoutMilliseconds) {
|
||||
throw std::runtime_error("Operation timed out");
|
||||
}
|
||||
}
|
||||
}
|
||||
};
|
||||
}; // namespace TaskHelper
|
||||
|
||||
@@ -1,36 +1,65 @@
|
||||
#include "Helper/delay_helper.h"
|
||||
#include "stm32h563xx.h"
|
||||
#include <string>
|
||||
|
||||
#include "Center/common_center.hpp"
|
||||
#include "Common/serial_port.hpp"
|
||||
#include "Helper/gpio_helper.hpp"
|
||||
#include "Helper/delay_helper.h"
|
||||
#include "config.hpp"
|
||||
|
||||
|
||||
void TerminateHandler() {
|
||||
auto led = GpioHelper::GpioInit(GPIOB, GPIO_PIN_0, GPIO_MODE_OUTPUT_PP, GPIO_NOPULL, GPIO_PIN_SET);
|
||||
auto led = Config::kLed;
|
||||
auto& debugSerialPort = CommonCenter::GetDebugSerialPort();
|
||||
std::string message;
|
||||
std::exception_ptr exceptionPtr = std::current_exception();
|
||||
if (exceptionPtr) {
|
||||
try {
|
||||
std::rethrow_exception(exceptionPtr);
|
||||
} catch (const std::exception& e) {
|
||||
message = e.what();
|
||||
} catch (...) {
|
||||
message = "Unknown exception type";
|
||||
}
|
||||
} else {
|
||||
message = "No active exception, but terminate() was called";
|
||||
}
|
||||
|
||||
while (true) {
|
||||
debugSerialPort.WriteLineBlocking("FATAL ERROR: %s", message.c_str());
|
||||
led.Toggle();
|
||||
DelayS(1);
|
||||
}
|
||||
}
|
||||
|
||||
void Setup() {
|
||||
void GpioSetup() {
|
||||
GpioHelper::EnableAllGpioPeripheral();
|
||||
GpioHelper::GpioInit(GPIOB, GPIO_PIN_0, GPIO_MODE_OUTPUT_PP, GPIO_NOPULL, GPIO_PIN_RESET);
|
||||
}
|
||||
|
||||
void CommonSetup() {
|
||||
CommonCenter::GetUltrasonic();
|
||||
CommonCenter::GetDfPlayer();
|
||||
CommonCenter::GetCanMv();
|
||||
CommonCenter::GetDebugSerialPort();
|
||||
CommonCenter::GetHc05();
|
||||
}
|
||||
|
||||
void Setup() {
|
||||
GpioSetup();
|
||||
DelaySetup();
|
||||
auto ultrasonic = CommonCenter::GetUltrasonic();
|
||||
auto dfPlayer = CommonCenter::GetDfPlayer();
|
||||
auto canMv = CommonCenter::GetCanMv();
|
||||
CommonSetup();
|
||||
std::set_terminate(TerminateHandler);
|
||||
}
|
||||
|
||||
extern "C" void AppStart() {
|
||||
Setup();
|
||||
auto ultrasonic = CommonCenter::GetUltrasonic();
|
||||
auto dfPlayer = CommonCenter::GetDfPlayer();
|
||||
auto canMv = CommonCenter::GetCanMv();
|
||||
auto& ultrasonic = CommonCenter::GetUltrasonic();
|
||||
auto& debugSerialPort = CommonCenter::GetDebugSerialPort();
|
||||
while (true) {
|
||||
// Example usage of the classes
|
||||
ultrasonic.GetDistance();
|
||||
dfPlayer.PlayTrack(1);
|
||||
|
||||
DelayMs(1000); // Delay for 1 second
|
||||
auto disatnce = ultrasonic.GetDistance();
|
||||
debugSerialPort.WriteLineBlocking("Distance: %.2f cm", disatnce);
|
||||
DelayS(1); // Delay for 1 second
|
||||
}
|
||||
}
|
||||
|
||||
@@ -6,13 +6,23 @@
|
||||
#include "./Helper/gpio_helper.hpp"
|
||||
|
||||
struct Config {
|
||||
Config() = delete;
|
||||
Config(const Config&) = delete;
|
||||
Config& operator=(const Config&) = delete;
|
||||
|
||||
static inline struct {
|
||||
GpioHelper::Gpio trigger;
|
||||
TIM_HandleTypeDef* timer;
|
||||
uint32_t channel;
|
||||
} kCaptureConfig = {{GPIOA, GPIO_PIN_0}, &htim7, TIM_CHANNEL_1};
|
||||
|
||||
static inline UART_HandleTypeDef* kDebugUart = &hcom_uart[COM1];
|
||||
|
||||
static inline UART_HandleTypeDef* kDfPlayerUart = &hcom_uart[COM1];
|
||||
|
||||
static inline UART_HandleTypeDef* kCanMvUart = &hcom_uart[COM1];
|
||||
|
||||
static inline UART_HandleTypeDef* kCanMvUart = &hcom_uart[COM1];
|
||||
|
||||
static inline UART_HandleTypeDef* kHc05Uart = &hcom_uart[COM1];
|
||||
|
||||
static inline GpioHelper::Gpio kLed = {GPIOB, GPIO_PIN_0};
|
||||
};
|
||||
|
||||
Reference in New Issue
Block a user