generated from Template/H563ZI-HAL-CMake-Template
Compare commits
8 Commits
dev-thread
...
be081086cb
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
be081086cb | ||
|
|
e9e08375fd | ||
|
|
5beaf15efd | ||
|
|
60b49d24c3 | ||
|
|
3f2e7bf019 | ||
|
|
b9a9165c71 | ||
|
|
92e9fc4864 | ||
|
|
559760be0c |
@@ -2,17 +2,11 @@
|
|||||||
|
|
||||||
#include "../Common/can_mv.hpp"
|
#include "../Common/can_mv.hpp"
|
||||||
#include "../Common/df_player.hpp"
|
#include "../Common/df_player.hpp"
|
||||||
#include "../Common/hc05.hpp"
|
|
||||||
#include "../Common/ultrasonic.hpp"
|
#include "../Common/ultrasonic.hpp"
|
||||||
#include "../config.hpp"
|
#include "../config.hpp"
|
||||||
#include "Common/serial_port.hpp"
|
|
||||||
|
|
||||||
class CommonCenter {
|
class CommonCenter {
|
||||||
public:
|
public:
|
||||||
CommonCenter() = delete;
|
|
||||||
CommonCenter(const CommonCenter&) = delete;
|
|
||||||
CommonCenter& operator=(const CommonCenter&) = delete;
|
|
||||||
|
|
||||||
static Ultrasonic& GetUltrasonic() {
|
static Ultrasonic& GetUltrasonic() {
|
||||||
static Ultrasonic* instance = nullptr;
|
static Ultrasonic* instance = nullptr;
|
||||||
if (instance == nullptr) {
|
if (instance == nullptr) {
|
||||||
@@ -37,23 +31,4 @@ public:
|
|||||||
}
|
}
|
||||||
return *instance;
|
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,24 +1,19 @@
|
|||||||
#pragma once
|
#pragma once
|
||||||
|
|
||||||
#include <cstdint>
|
#include <cstdint>
|
||||||
#include <memory>
|
|
||||||
|
|
||||||
#include "../Common/serial_port.hpp"
|
#include "../Common/serial_port.hpp"
|
||||||
|
|
||||||
class CanMv {
|
class CanMv {
|
||||||
public:
|
public:
|
||||||
explicit CanMv(UART_HandleTypeDef* uart)
|
explicit CanMv(UART_HandleTypeDef* uart) : serialPort(new SerialPort(uart, kLength, kTimeout)) {}
|
||||||
: serialPort(std::make_unique<SerialPort>(uart, kBaudRate, kLength, kTimeout)) {}
|
|
||||||
|
|
||||||
~CanMv() = default;
|
~CanMv() {
|
||||||
CanMv(const CanMv&) = delete;
|
delete serialPort;
|
||||||
CanMv& operator=(const CanMv&) = delete;
|
}
|
||||||
CanMv(CanMv&&) = delete;
|
|
||||||
CanMv& operator=(CanMv&&) = delete;
|
|
||||||
|
|
||||||
private:
|
private:
|
||||||
static constexpr uint32_t kBaudRate = 115200;
|
static constexpr uint32_t kLength = 128;
|
||||||
static constexpr uint32_t kLength = 128;
|
static constexpr uint32_t kTimeout = 10;
|
||||||
static constexpr uint32_t kTimeout = 10;
|
SerialPort* serialPort;
|
||||||
std::unique_ptr<SerialPort> serialPort;
|
|
||||||
};
|
};
|
||||||
|
|||||||
@@ -1,20 +1,16 @@
|
|||||||
#pragma once
|
#pragma once
|
||||||
|
|
||||||
#include <cstdint>
|
#include <cstdint>
|
||||||
#include <memory>
|
|
||||||
|
|
||||||
#include "../Common/serial_port.hpp"
|
#include "../Common/serial_port.hpp"
|
||||||
|
|
||||||
class DfPlayer {
|
class DfPlayer {
|
||||||
public:
|
public:
|
||||||
explicit DfPlayer(UART_HandleTypeDef* uart)
|
explicit DfPlayer(UART_HandleTypeDef* uart) : serialPort(new SerialPort(uart, kLength, kTimeout)) {}
|
||||||
: serialPort(std::make_unique<SerialPort>(uart, kBaudRate, kLength, kTimeout)) {}
|
|
||||||
|
|
||||||
~DfPlayer() = default;
|
~DfPlayer() {
|
||||||
DfPlayer(const DfPlayer&) = delete;
|
delete serialPort;
|
||||||
DfPlayer& operator=(const DfPlayer&) = delete;
|
}
|
||||||
DfPlayer(DfPlayer&&) = delete;
|
|
||||||
DfPlayer& operator=(DfPlayer&&) = delete;
|
|
||||||
|
|
||||||
void Play();
|
void Play();
|
||||||
void Stop();
|
void Stop();
|
||||||
@@ -54,10 +50,9 @@ private:
|
|||||||
kQueryFlashCurrentTrack = 0x4D,
|
kQueryFlashCurrentTrack = 0x4D,
|
||||||
};
|
};
|
||||||
|
|
||||||
static constexpr uint32_t kBaudRate = 9600;
|
static constexpr uint32_t kLength = 128;
|
||||||
static constexpr uint32_t kLength = 128;
|
static constexpr uint32_t kTimeout = 10;
|
||||||
static constexpr uint32_t kTimeout = 10;
|
SerialPort* serialPort;
|
||||||
std::unique_ptr<SerialPort> serialPort;
|
|
||||||
|
|
||||||
void SendCommand(Command cmd, uint16_t param = 0);
|
void SendCommand(Command cmd, uint16_t param = 0);
|
||||||
|
|
||||||
|
|||||||
@@ -1,47 +0,0 @@
|
|||||||
#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;
|
|
||||||
}
|
|
||||||
};
|
|
||||||
@@ -1,36 +0,0 @@
|
|||||||
#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,7 +1,6 @@
|
|||||||
#pragma once
|
#pragma once
|
||||||
|
|
||||||
#include "main.h"
|
#include "main.h"
|
||||||
#include "stm32h5xx_hal_uart.h"
|
|
||||||
#include <cstdarg>
|
#include <cstdarg>
|
||||||
#include <cstdint>
|
#include <cstdint>
|
||||||
#include <cstring>
|
#include <cstring>
|
||||||
@@ -14,35 +13,23 @@ private:
|
|||||||
uint32_t timeout;
|
uint32_t timeout;
|
||||||
std::unique_ptr<char[]> buffer;
|
std::unique_ptr<char[]> buffer;
|
||||||
|
|
||||||
uint8_t ReadByteForce() {
|
|
||||||
return handle->Instance->RDR;
|
|
||||||
}
|
|
||||||
|
|
||||||
public:
|
public:
|
||||||
SerialPort(UART_HandleTypeDef* uart, uint32_t baudRate, uint32_t length, uint32_t timeout)
|
SerialPort(UART_HandleTypeDef* uart, uint32_t length, uint32_t timeout)
|
||||||
: handle(uart), timeout(timeout), buffer(std::make_unique<char[]>(length)) {
|
: handle(uart), timeout(timeout), buffer(std::make_unique<char[]>(length)) {}
|
||||||
SetBaudRate(baudRate);
|
|
||||||
}
|
|
||||||
|
|
||||||
~SerialPort() = default;
|
~SerialPort() = default;
|
||||||
SerialPort(const SerialPort&) = delete;
|
|
||||||
SerialPort& operator=(const SerialPort&) = delete;
|
|
||||||
SerialPort(SerialPort&&) = delete;
|
|
||||||
SerialPort& operator=(SerialPort&&) = delete;
|
|
||||||
|
|
||||||
void SetBaudRate(uint32_t baudRate) {
|
uint8_t ReadByte() {
|
||||||
HAL_UART_DeInit(handle);
|
return handle->Instance->RDR;
|
||||||
handle->Init.BaudRate = baudRate;
|
|
||||||
HAL_UART_Init(handle);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
std::string ReadLine() {
|
std::string ReadLine() {
|
||||||
std::string result;
|
std::string result;
|
||||||
result.reserve(128);
|
result.reserve(128);
|
||||||
char c;
|
char c;
|
||||||
ReadByteForce();
|
ReadByte();
|
||||||
while (true) {
|
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) {
|
if (r != HAL_OK) {
|
||||||
throw std::runtime_error("UART receive error");
|
throw std::runtime_error("UART receive error");
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -19,12 +19,6 @@ public:
|
|||||||
captureFlag = GetCaptureFlag(channel);
|
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 GetDistance(uint32_t time = 1) {
|
||||||
double distance = 0;
|
double distance = 0;
|
||||||
for (uint32_t cnt = 0; cnt < time; ++cnt) {
|
for (uint32_t cnt = 0; cnt < time; ++cnt) {
|
||||||
@@ -52,7 +46,6 @@ private:
|
|||||||
double MeasureEchoTime() {
|
double MeasureEchoTime() {
|
||||||
timer->Instance->CNT = 0;
|
timer->Instance->CNT = 0;
|
||||||
uint32_t data[2];
|
uint32_t data[2];
|
||||||
__HAL_TIM_CLEAR_FLAG(timer, captureFlag);
|
|
||||||
HAL_TIM_IC_Start(timer, channel);
|
HAL_TIM_IC_Start(timer, channel);
|
||||||
try {
|
try {
|
||||||
for (auto& value : data) {
|
for (auto& value : data) {
|
||||||
|
|||||||
@@ -6,12 +6,6 @@
|
|||||||
|
|
||||||
class GpioHelper {
|
class GpioHelper {
|
||||||
public:
|
public:
|
||||||
GpioHelper() = delete;
|
|
||||||
GpioHelper(const GpioHelper&) = delete;
|
|
||||||
GpioHelper& operator=(const GpioHelper&) = delete;
|
|
||||||
GpioHelper(GpioHelper&&) = delete;
|
|
||||||
GpioHelper& operator=(GpioHelper&&) = delete;
|
|
||||||
|
|
||||||
struct Gpio {
|
struct Gpio {
|
||||||
GPIO_TypeDef* port;
|
GPIO_TypeDef* port;
|
||||||
uint16_t pin;
|
uint16_t pin;
|
||||||
@@ -62,8 +56,7 @@ public:
|
|||||||
return {port, pin};
|
return {port, pin};
|
||||||
}
|
}
|
||||||
|
|
||||||
static Gpio GpioInit(const Gpio& gpio, const uint32_t mode, const uint32_t pull,
|
static Gpio GpioInit(const Gpio& gpio, const uint32_t mode, const uint32_t pull, const GPIO_PinState state) {
|
||||||
const std::optional<GPIO_PinState> state = std::nullopt) {
|
|
||||||
return GpioInit(gpio.port, gpio.pin, mode, pull, state);
|
return GpioInit(gpio.port, gpio.pin, mode, pull, state);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -4,15 +4,16 @@
|
|||||||
#include <cstdint>
|
#include <cstdint>
|
||||||
#include <stdexcept>
|
#include <stdexcept>
|
||||||
|
|
||||||
namespace TaskHelper {
|
class TaskHelper {
|
||||||
|
public:
|
||||||
template <typename Func>
|
template <typename Func>
|
||||||
static void WaitFor(Func func, uint32_t timeoutMilliseconds) {
|
static void WaitFor(Func func, int timeoutMilliseconds) {
|
||||||
auto start = HAL_GetTick();
|
auto start = HAL_GetTick();
|
||||||
while (!func()) {
|
while (!func()) {
|
||||||
uint32_t now = HAL_GetTick();
|
uint32_t now = HAL_GetTick();
|
||||||
if ((now - start) > timeoutMilliseconds) {
|
if ((now - start) > static_cast<uint32_t>(timeoutMilliseconds)) {
|
||||||
throw std::runtime_error("Operation timed out");
|
throw std::runtime_error("Operation timed out");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}; // namespace TaskHelper
|
};
|
||||||
@@ -1,65 +1,36 @@
|
|||||||
#include "Helper/delay_helper.h"
|
#include "Helper/delay_helper.h"
|
||||||
#include "stm32h563xx.h"
|
#include "stm32h563xx.h"
|
||||||
#include <string>
|
|
||||||
|
|
||||||
#include "Center/common_center.hpp"
|
#include "Center/common_center.hpp"
|
||||||
#include "Common/serial_port.hpp"
|
|
||||||
#include "Helper/gpio_helper.hpp"
|
#include "Helper/gpio_helper.hpp"
|
||||||
#include "config.hpp"
|
#include "Helper/delay_helper.h"
|
||||||
|
|
||||||
|
|
||||||
void TerminateHandler() {
|
void TerminateHandler() {
|
||||||
auto led = Config::kLed;
|
auto led = GpioHelper::GpioInit(GPIOB, GPIO_PIN_0, GPIO_MODE_OUTPUT_PP, GPIO_NOPULL, GPIO_PIN_SET);
|
||||||
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) {
|
while (true) {
|
||||||
debugSerialPort.WriteLineBlocking("FATAL ERROR: %s", message.c_str());
|
|
||||||
led.Toggle();
|
led.Toggle();
|
||||||
DelayS(1);
|
DelayS(1);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
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() {
|
void Setup() {
|
||||||
GpioSetup();
|
GpioHelper::EnableAllGpioPeripheral();
|
||||||
DelaySetup();
|
DelaySetup();
|
||||||
CommonSetup();
|
auto ultrasonic = CommonCenter::GetUltrasonic();
|
||||||
std::set_terminate(TerminateHandler);
|
auto dfPlayer = CommonCenter::GetDfPlayer();
|
||||||
|
auto canMv = CommonCenter::GetCanMv();
|
||||||
}
|
}
|
||||||
|
|
||||||
extern "C" void AppStart() {
|
extern "C" void AppStart() {
|
||||||
Setup();
|
Setup();
|
||||||
auto& ultrasonic = CommonCenter::GetUltrasonic();
|
auto ultrasonic = CommonCenter::GetUltrasonic();
|
||||||
auto& debugSerialPort = CommonCenter::GetDebugSerialPort();
|
auto dfPlayer = CommonCenter::GetDfPlayer();
|
||||||
|
auto canMv = CommonCenter::GetCanMv();
|
||||||
while (true) {
|
while (true) {
|
||||||
// Example usage of the classes
|
// Example usage of the classes
|
||||||
auto disatnce = ultrasonic.GetDistance();
|
ultrasonic.GetDistance();
|
||||||
debugSerialPort.WriteLineBlocking("Distance: %.2f cm", disatnce);
|
dfPlayer.PlayTrack(1);
|
||||||
DelayS(1); // Delay for 1 second
|
|
||||||
|
DelayMs(1000); // Delay for 1 second
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -6,23 +6,13 @@
|
|||||||
#include "./Helper/gpio_helper.hpp"
|
#include "./Helper/gpio_helper.hpp"
|
||||||
|
|
||||||
struct Config {
|
struct Config {
|
||||||
Config() = delete;
|
|
||||||
Config(const Config&) = delete;
|
|
||||||
Config& operator=(const Config&) = delete;
|
|
||||||
|
|
||||||
static inline struct {
|
static inline struct {
|
||||||
GpioHelper::Gpio trigger;
|
GpioHelper::Gpio trigger;
|
||||||
TIM_HandleTypeDef* timer;
|
TIM_HandleTypeDef* timer;
|
||||||
uint32_t channel;
|
uint32_t channel;
|
||||||
} kCaptureConfig = {{GPIOA, GPIO_PIN_0}, &htim7, TIM_CHANNEL_1};
|
} 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* 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