generated from Template/H563ZI-HAL-CMake-Template
Compare commits
8 Commits
dev
...
be081086cb
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
be081086cb | ||
|
|
e9e08375fd | ||
|
|
5beaf15efd | ||
|
|
60b49d24c3 | ||
|
|
3f2e7bf019 | ||
|
|
b9a9165c71 | ||
|
|
92e9fc4864 | ||
|
|
559760be0c |
@@ -94,7 +94,7 @@ NVIC.NonMaskableInt_IRQn=true\:0\:0\:false\:false\:true\:false\:false\:false
|
|||||||
NVIC.PendSV_IRQn=true\:0\:0\:false\:false\:true\:false\:false\:false
|
NVIC.PendSV_IRQn=true\:0\:0\:false\:false\:true\:false\:false\:false
|
||||||
NVIC.PriorityGroup=NVIC_PRIORITYGROUP_4
|
NVIC.PriorityGroup=NVIC_PRIORITYGROUP_4
|
||||||
NVIC.SVCall_IRQn=true\:0\:0\:false\:false\:true\:false\:false\:false
|
NVIC.SVCall_IRQn=true\:0\:0\:false\:false\:true\:false\:false\:false
|
||||||
NVIC.SysTick_IRQn=true\:15\:0\:false\:false\:true\:false\:true\:false
|
NVIC.SysTick_IRQn=true\:14\:0\:false\:false\:true\:false\:true\:false
|
||||||
NVIC.UsageFault_IRQn=true\:0\:0\:false\:false\:true\:false\:false\:false
|
NVIC.UsageFault_IRQn=true\:0\:0\:false\:false\:true\:false\:false\:false
|
||||||
PA1.GPIOParameters=GPIO_Speed,GPIO_PuPd,GPIO_Label,GPIO_Mode
|
PA1.GPIOParameters=GPIO_Speed,GPIO_PuPd,GPIO_Label,GPIO_Mode
|
||||||
PA1.GPIO_Label=RMII_REF_CLK
|
PA1.GPIO_Label=RMII_REF_CLK
|
||||||
@@ -286,7 +286,7 @@ ProjectManager.ToolChainLocation=
|
|||||||
ProjectManager.UAScriptAfterPath=
|
ProjectManager.UAScriptAfterPath=
|
||||||
ProjectManager.UAScriptBeforePath=
|
ProjectManager.UAScriptBeforePath=
|
||||||
ProjectManager.UnderRoot=false
|
ProjectManager.UnderRoot=false
|
||||||
ProjectManager.functionlistsort=1-SystemClock_Config-RCC-false-HAL-false,2-MX_GPIO_Init-GPIO-false-HAL-true,0-MX_CORTEX_M33_NS_Init-CORTEX_M33_NS-false-HAL-true,0-MX_PWR_Init-PWR-false-HAL-true,false-0--NUCLEO-H563ZI-true-HAL-true
|
ProjectManager.functionlistsort=1-SystemClock_Config-RCC-false-HAL-false,2-MX_GPIO_Init-GPIO-false-HAL-true,3-MX_TIM7_Init-TIM7-false-HAL-true,0-MX_CORTEX_M33_NS_Init-CORTEX_M33_NS-false-HAL-true,0-MX_PWR_Init-PWR-false-HAL-true,false-0--NUCLEO-H563ZI-true-HAL-true
|
||||||
RCC.ADCFreq_Value=250000000
|
RCC.ADCFreq_Value=250000000
|
||||||
RCC.AHBFreq_Value=250000000
|
RCC.AHBFreq_Value=250000000
|
||||||
RCC.APB1Freq_Value=250000000
|
RCC.APB1Freq_Value=250000000
|
||||||
|
|||||||
@@ -49,14 +49,17 @@ target_link_directories(${CMAKE_PROJECT_NAME} PRIVATE
|
|||||||
# Add user defined library search paths
|
# Add user defined library search paths
|
||||||
)
|
)
|
||||||
|
|
||||||
|
# Find Python3
|
||||||
|
find_package(Python3 REQUIRED)
|
||||||
|
|
||||||
# Add custom target to run MCUVersionGen.py script
|
# Add custom target to run MCUVersionGen.py script
|
||||||
execute_process(COMMAND python ${CMAKE_CURRENT_SOURCE_DIR}/Tools/GenVersion/McuVersionGen.py ${CMAKE_PROJECT_NAME} ${CMAKE_CURRENT_SOURCE_DIR}
|
execute_process(COMMAND ${Python3_EXECUTABLE} ${CMAKE_CURRENT_SOURCE_DIR}/Tools/GenVersion/McuVersionGen.py ${CMAKE_PROJECT_NAME} ${CMAKE_CURRENT_SOURCE_DIR}
|
||||||
RESULT_VARIABLE result
|
RESULT_VARIABLE result
|
||||||
OUTPUT_VARIABLE script_output)
|
OUTPUT_VARIABLE script_output)
|
||||||
message(${script_output})
|
message(${script_output})
|
||||||
|
|
||||||
add_custom_target(version_gen
|
add_custom_target(version_gen
|
||||||
COMMAND python ${CMAKE_CURRENT_SOURCE_DIR}/Tools/GenVersion/McuVersionGen.py ${CMAKE_PROJECT_NAME} ${CMAKE_CURRENT_SOURCE_DIR}
|
COMMAND ${Python3_EXECUTABLE} ${CMAKE_CURRENT_SOURCE_DIR}/Tools/GenVersion/McuVersionGen.py ${CMAKE_PROJECT_NAME} ${CMAKE_CURRENT_SOURCE_DIR}
|
||||||
COMMENT "Running MCUVersionGen.py script"
|
COMMENT "Running MCUVersionGen.py script"
|
||||||
)
|
)
|
||||||
add_dependencies(${CMAKE_PROJECT_NAME} version_gen)
|
add_dependencies(${CMAKE_PROJECT_NAME} version_gen)
|
||||||
|
|||||||
34
Core/App/Center/common_center.hpp
Normal file
34
Core/App/Center/common_center.hpp
Normal file
@@ -0,0 +1,34 @@
|
|||||||
|
#pragma once
|
||||||
|
|
||||||
|
#include "../Common/can_mv.hpp"
|
||||||
|
#include "../Common/df_player.hpp"
|
||||||
|
#include "../Common/ultrasonic.hpp"
|
||||||
|
#include "../config.hpp"
|
||||||
|
|
||||||
|
class CommonCenter {
|
||||||
|
public:
|
||||||
|
static Ultrasonic& GetUltrasonic() {
|
||||||
|
static Ultrasonic* instance = nullptr;
|
||||||
|
if (instance == nullptr) {
|
||||||
|
instance = new Ultrasonic(
|
||||||
|
Config::kCaptureConfig.trigger, Config::kCaptureConfig.timer, Config::kCaptureConfig.channel);
|
||||||
|
}
|
||||||
|
return *instance;
|
||||||
|
}
|
||||||
|
|
||||||
|
static DfPlayer& GetDfPlayer() {
|
||||||
|
static DfPlayer* instance = nullptr;
|
||||||
|
if (instance == nullptr) {
|
||||||
|
instance = new DfPlayer(Config::kDfPlayerUart);
|
||||||
|
}
|
||||||
|
return *instance;
|
||||||
|
}
|
||||||
|
|
||||||
|
static CanMv& GetCanMv() {
|
||||||
|
static CanMv* instance = nullptr;
|
||||||
|
if (instance == nullptr) {
|
||||||
|
instance = new CanMv(Config::kCanMvUart);
|
||||||
|
}
|
||||||
|
return *instance;
|
||||||
|
}
|
||||||
|
};
|
||||||
19
Core/App/Common/can_mv.hpp
Normal file
19
Core/App/Common/can_mv.hpp
Normal file
@@ -0,0 +1,19 @@
|
|||||||
|
#pragma once
|
||||||
|
|
||||||
|
#include <cstdint>
|
||||||
|
|
||||||
|
#include "../Common/serial_port.hpp"
|
||||||
|
|
||||||
|
class CanMv {
|
||||||
|
public:
|
||||||
|
explicit CanMv(UART_HandleTypeDef* uart) : serialPort(new SerialPort(uart, kLength, kTimeout)) {}
|
||||||
|
|
||||||
|
~CanMv() {
|
||||||
|
delete serialPort;
|
||||||
|
}
|
||||||
|
|
||||||
|
private:
|
||||||
|
static constexpr uint32_t kLength = 128;
|
||||||
|
static constexpr uint32_t kTimeout = 10;
|
||||||
|
SerialPort* serialPort;
|
||||||
|
};
|
||||||
41
Core/App/Common/df_player.cpp
Normal file
41
Core/App/Common/df_player.cpp
Normal file
@@ -0,0 +1,41 @@
|
|||||||
|
#include "df_player.hpp"
|
||||||
|
|
||||||
|
void DfPlayer::SendCommand(Command cmd, uint16_t param) {
|
||||||
|
uint8_t buffer[10];
|
||||||
|
buffer[0] = kStartByte;
|
||||||
|
buffer[1] = kVersion;
|
||||||
|
buffer[2] = 0x06;
|
||||||
|
buffer[3] = static_cast<uint8_t>(cmd);
|
||||||
|
buffer[4] = kFeedback;
|
||||||
|
buffer[5] = (param >> 8) & 0xFF;
|
||||||
|
buffer[6] = param & 0xFF;
|
||||||
|
|
||||||
|
uint16_t checksum = 0xFFFF - (kVersion + 0x06 + static_cast<uint8_t>(cmd) + kFeedback + buffer[5] + buffer[6]) + 1;
|
||||||
|
buffer[7] = (checksum >> 8) & 0xFF;
|
||||||
|
buffer[8] = checksum & 0xFF;
|
||||||
|
buffer[9] = kEndByte;
|
||||||
|
|
||||||
|
serialPort->WriteBytesBlocking(buffer, sizeof(buffer));
|
||||||
|
}
|
||||||
|
|
||||||
|
void DfPlayer::Play() {
|
||||||
|
SendCommand(Command::kPlay);
|
||||||
|
}
|
||||||
|
|
||||||
|
void DfPlayer::Stop() {
|
||||||
|
SendCommand(Command::kStop);
|
||||||
|
}
|
||||||
|
|
||||||
|
void DfPlayer::SetVolume(uint8_t volume) {
|
||||||
|
if (volume > 30) {
|
||||||
|
volume = 30;
|
||||||
|
}
|
||||||
|
SendCommand(Command::kSetVolume, volume);
|
||||||
|
}
|
||||||
|
|
||||||
|
void DfPlayer::PlayTrack(uint16_t track) {
|
||||||
|
if (track < 1 || track > 2999) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
SendCommand(Command::kPlayTrack, track);
|
||||||
|
}
|
||||||
63
Core/App/Common/df_player.hpp
Normal file
63
Core/App/Common/df_player.hpp
Normal file
@@ -0,0 +1,63 @@
|
|||||||
|
#pragma once
|
||||||
|
|
||||||
|
#include <cstdint>
|
||||||
|
|
||||||
|
#include "../Common/serial_port.hpp"
|
||||||
|
|
||||||
|
class DfPlayer {
|
||||||
|
public:
|
||||||
|
explicit DfPlayer(UART_HandleTypeDef* uart) : serialPort(new SerialPort(uart, kLength, kTimeout)) {}
|
||||||
|
|
||||||
|
~DfPlayer() {
|
||||||
|
delete serialPort;
|
||||||
|
}
|
||||||
|
|
||||||
|
void Play();
|
||||||
|
void Stop();
|
||||||
|
void SetVolume(uint8_t volume);
|
||||||
|
void PlayTrack(uint16_t track);
|
||||||
|
|
||||||
|
private:
|
||||||
|
enum class Command : uint8_t {
|
||||||
|
kNext = 0x01,
|
||||||
|
kPrevious = 0x02,
|
||||||
|
kPlayTrack = 0x03,
|
||||||
|
kVolumeUp = 0x04,
|
||||||
|
kVolumeDown = 0x05,
|
||||||
|
kSetVolume = 0x06,
|
||||||
|
kSetEQ = 0x07,
|
||||||
|
kSingleLoopPlayTrack = 0x08,
|
||||||
|
kSetPlaybackDevice = 0x09,
|
||||||
|
kEnterSleep = 0x0A,
|
||||||
|
kResetModule = 0x0C,
|
||||||
|
kPlay = 0x0D,
|
||||||
|
kPause = 0x0E,
|
||||||
|
kPlayFolderTrack = 0x0F,
|
||||||
|
kSetAmplification = 0x10,
|
||||||
|
kLoopAll = 0x11,
|
||||||
|
kPlayMp3FolderTrack = 0x12,
|
||||||
|
kInsertAd = 0x13,
|
||||||
|
kStopAdPlayBackground = 0x15,
|
||||||
|
kStop = 0x16,
|
||||||
|
kQueryStatus = 0x42,
|
||||||
|
kQueryVolume = 0x43,
|
||||||
|
kQueryEQ = 0x44,
|
||||||
|
kQueryUDiskTotalFiles = 0x47,
|
||||||
|
kQueryTFCardTotalFiles = 0x48,
|
||||||
|
kQueryFlashTotalFiles = 0x49,
|
||||||
|
kQueryUDiskCurrentTrack = 0x4B,
|
||||||
|
kQueryTFCardCurrentTrack = 0x4C,
|
||||||
|
kQueryFlashCurrentTrack = 0x4D,
|
||||||
|
};
|
||||||
|
|
||||||
|
static constexpr uint32_t kLength = 128;
|
||||||
|
static constexpr uint32_t kTimeout = 10;
|
||||||
|
SerialPort* serialPort;
|
||||||
|
|
||||||
|
void SendCommand(Command cmd, uint16_t param = 0);
|
||||||
|
|
||||||
|
static constexpr uint8_t kStartByte = 0x7E;
|
||||||
|
static constexpr uint8_t kVersion = 0xFF;
|
||||||
|
static constexpr uint8_t kFeedback = 0x00;
|
||||||
|
static constexpr uint8_t kEndByte = 0xEF;
|
||||||
|
};
|
||||||
@@ -1,5 +0,0 @@
|
|||||||
#include "serial_port.hpp"
|
|
||||||
|
|
||||||
extern "C" void HAL_UART_TxCpltCallback([[maybe_unused]] UART_HandleTypeDef* huart) {
|
|
||||||
SerialPort::isTransmitting = false;
|
|
||||||
}
|
|
||||||
@@ -1,43 +1,38 @@
|
|||||||
#pragma once
|
#pragma once
|
||||||
|
|
||||||
#include "stm32h5xx_nucleo.h"
|
#include "main.h"
|
||||||
#include <cstdarg>
|
#include <cstdarg>
|
||||||
#include <cstdint>
|
#include <cstdint>
|
||||||
#include <cstring>
|
#include <cstring>
|
||||||
|
#include <memory>
|
||||||
#include <string>
|
#include <string>
|
||||||
|
|
||||||
class SerialPort {
|
class SerialPort {
|
||||||
private:
|
private:
|
||||||
static constexpr uint32_t kTransmitTimeout = 1000;
|
UART_HandleTypeDef* handle;
|
||||||
static constexpr auto* kHandle = &hcom_uart[COM1];
|
uint32_t timeout;
|
||||||
|
std::unique_ptr<char[]> buffer;
|
||||||
static inline char buffer[256] = {0};
|
|
||||||
|
|
||||||
friend void HAL_UART_TxCpltCallback(UART_HandleTypeDef* huart);
|
|
||||||
static inline volatile bool isTransmitting = false;
|
|
||||||
|
|
||||||
static void WaitTransmit() {
|
|
||||||
const auto tick = HAL_GetTick();
|
|
||||||
while (isTransmitting) {
|
|
||||||
if (HAL_GetTick() - tick > kTransmitTimeout) {
|
|
||||||
HAL_UART_AbortTransmit(kHandle);
|
|
||||||
isTransmitting = false;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
public:
|
public:
|
||||||
static uint8_t ReadByte() {
|
SerialPort(UART_HandleTypeDef* uart, uint32_t length, uint32_t timeout)
|
||||||
return kHandle->Instance->RDR;
|
: handle(uart), timeout(timeout), buffer(std::make_unique<char[]>(length)) {}
|
||||||
|
|
||||||
|
~SerialPort() = default;
|
||||||
|
|
||||||
|
uint8_t ReadByte() {
|
||||||
|
return handle->Instance->RDR;
|
||||||
}
|
}
|
||||||
|
|
||||||
static std::string ReadLine() {
|
std::string ReadLine() {
|
||||||
std::string result;
|
std::string result;
|
||||||
result.reserve(128);
|
result.reserve(128);
|
||||||
char c;
|
char c;
|
||||||
ReadByte();
|
ReadByte();
|
||||||
while (true) {
|
while (true) {
|
||||||
HAL_UART_Receive(kHandle, reinterpret_cast<uint8_t*>(&c), 1, HAL_MAX_DELAY);
|
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");
|
||||||
|
}
|
||||||
if (c == '\n') {
|
if (c == '\n') {
|
||||||
if (!result.empty() && result.back() == '\r') {
|
if (!result.empty() && result.back() == '\r') {
|
||||||
result.pop_back();
|
result.pop_back();
|
||||||
@@ -49,70 +44,39 @@ public:
|
|||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
|
|
||||||
static void WriteLineDMA(const char* format, ...) {
|
void ReadBytesBlocking(uint8_t* data, size_t size) {
|
||||||
WaitTransmit();
|
auto r = HAL_UART_Receive(handle, data, size, timeout);
|
||||||
isTransmitting = true;
|
if (r != HAL_OK) {
|
||||||
va_list args;
|
throw std::runtime_error("UART receive error");
|
||||||
va_start(args, format);
|
|
||||||
int len = vsnprintf(buffer, sizeof(buffer) - 1, format, args);
|
|
||||||
va_end(args);
|
|
||||||
if (len < 0 || len > static_cast<int>(sizeof(buffer) - 1)) {
|
|
||||||
isTransmitting = false;
|
|
||||||
return;
|
|
||||||
}
|
}
|
||||||
buffer[len] = '\n';
|
|
||||||
len++;
|
|
||||||
HAL_UART_Transmit_DMA(kHandle, reinterpret_cast<uint8_t*>(buffer), len);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
static void WriteLineBlocking(const char* format, ...) {
|
void WriteBytesBlocking(const uint8_t* data, size_t size) {
|
||||||
WaitTransmit();
|
HAL_UART_Transmit(handle, const_cast<uint8_t*>(data), size, timeout);
|
||||||
|
}
|
||||||
|
|
||||||
|
void WriteBlocking(const char* format, ...) {
|
||||||
|
|
||||||
va_list args;
|
va_list args;
|
||||||
va_start(args, format);
|
va_start(args, format);
|
||||||
int len = vsnprintf(buffer, sizeof(buffer) - 1, format, args);
|
auto len = vsnprintf(buffer.get(), sizeof(buffer), format, args);
|
||||||
|
va_end(args);
|
||||||
|
if (len < 0 || len > static_cast<int>(sizeof(buffer))) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
HAL_UART_Transmit(handle, reinterpret_cast<uint8_t*>(buffer.get()), strlen(buffer.get()), timeout);
|
||||||
|
}
|
||||||
|
|
||||||
|
void WriteLineBlocking(const char* format, ...) {
|
||||||
|
va_list args;
|
||||||
|
va_start(args, format);
|
||||||
|
int len = vsnprintf(buffer.get(), sizeof(buffer) - 1, format, args);
|
||||||
va_end(args);
|
va_end(args);
|
||||||
if (len < 0 || len > static_cast<int>(sizeof(buffer) - 1)) {
|
if (len < 0 || len > static_cast<int>(sizeof(buffer) - 1)) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
buffer[len] = '\n';
|
buffer[len] = '\n';
|
||||||
len++;
|
len++;
|
||||||
HAL_UART_Transmit(kHandle, reinterpret_cast<uint8_t*>(buffer), len, kTransmitTimeout);
|
HAL_UART_Transmit(handle, reinterpret_cast<uint8_t*>(buffer.get()), len, timeout);
|
||||||
}
|
|
||||||
|
|
||||||
static void WriteDMA(const char* format, ...) {
|
|
||||||
WaitTransmit();
|
|
||||||
isTransmitting = true;
|
|
||||||
va_list args;
|
|
||||||
va_start(args, format);
|
|
||||||
auto len = vsnprintf(buffer, sizeof(buffer), format, args);
|
|
||||||
va_end(args);
|
|
||||||
if (len < 0 || len > static_cast<int>(sizeof(buffer))) {
|
|
||||||
isTransmitting = false;
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
HAL_UART_Transmit_DMA(kHandle, reinterpret_cast<uint8_t*>(buffer), len);
|
|
||||||
}
|
|
||||||
|
|
||||||
static void WriteBlocking(const char* format, ...) {
|
|
||||||
WaitTransmit();
|
|
||||||
va_list args;
|
|
||||||
va_start(args, format);
|
|
||||||
auto len = vsnprintf(buffer, sizeof(buffer), format, args);
|
|
||||||
va_end(args);
|
|
||||||
if (len < 0 || len > static_cast<int>(sizeof(buffer))) {
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
HAL_UART_Transmit(kHandle, reinterpret_cast<uint8_t*>(buffer), strlen(buffer), kTransmitTimeout);
|
|
||||||
}
|
|
||||||
|
|
||||||
static void WriteBytesDMA(const uint8_t* data, size_t size) {
|
|
||||||
WaitTransmit();
|
|
||||||
isTransmitting = true;
|
|
||||||
HAL_UART_Transmit_DMA(kHandle, const_cast<uint8_t*>(data), size);
|
|
||||||
}
|
|
||||||
|
|
||||||
static void WriteBytesBlocking(const uint8_t* data, size_t size) {
|
|
||||||
WaitTransmit();
|
|
||||||
HAL_UART_Transmit(kHandle, const_cast<uint8_t*>(data), size, kTransmitTimeout);
|
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|||||||
89
Core/App/Common/ultrasonic.hpp
Normal file
89
Core/App/Common/ultrasonic.hpp
Normal file
@@ -0,0 +1,89 @@
|
|||||||
|
#pragma once
|
||||||
|
|
||||||
|
#include "../Helper/delay_helper.h"
|
||||||
|
#include "stm32h563xx.h"
|
||||||
|
#include "stm32h5xx_hal_tim.h"
|
||||||
|
#include <cstdint>
|
||||||
|
#include <stdexcept>
|
||||||
|
|
||||||
|
#include "../Helper/gpio_helper.hpp"
|
||||||
|
#include "../Helper/task_helper.hpp"
|
||||||
|
|
||||||
|
class Ultrasonic {
|
||||||
|
public:
|
||||||
|
Ultrasonic(GPIO_TypeDef* gpio, uint16_t pin, TIM_HandleTypeDef* timer, uint32_t channel)
|
||||||
|
: Ultrasonic({gpio, pin}, timer, channel) {}
|
||||||
|
|
||||||
|
Ultrasonic(const GpioHelper::Gpio& trigger, TIM_HandleTypeDef* timer, uint32_t channel)
|
||||||
|
: trigger(trigger), timer(timer), channel(channel) {
|
||||||
|
captureFlag = GetCaptureFlag(channel);
|
||||||
|
}
|
||||||
|
|
||||||
|
double GetDistance(uint32_t time = 1) {
|
||||||
|
double distance = 0;
|
||||||
|
for (uint32_t cnt = 0; cnt < time; ++cnt) {
|
||||||
|
Trigger();
|
||||||
|
auto echoTime = MeasureEchoTime();
|
||||||
|
distance += CalculateDistance(echoTime);
|
||||||
|
}
|
||||||
|
return distance / time;
|
||||||
|
}
|
||||||
|
|
||||||
|
private:
|
||||||
|
constexpr static uint32_t kTimeout = 100;
|
||||||
|
|
||||||
|
GpioHelper::Gpio trigger;
|
||||||
|
TIM_HandleTypeDef* timer;
|
||||||
|
uint32_t channel;
|
||||||
|
uint32_t captureFlag;
|
||||||
|
|
||||||
|
void Trigger() {
|
||||||
|
trigger.Set();
|
||||||
|
DelayUs(10);
|
||||||
|
trigger.Reset();
|
||||||
|
}
|
||||||
|
|
||||||
|
double MeasureEchoTime() {
|
||||||
|
timer->Instance->CNT = 0;
|
||||||
|
uint32_t data[2];
|
||||||
|
HAL_TIM_IC_Start(timer, channel);
|
||||||
|
try {
|
||||||
|
for (auto& value : data) {
|
||||||
|
TaskHelper::WaitFor(
|
||||||
|
[&]() {
|
||||||
|
if (__HAL_TIM_GET_FLAG(timer, captureFlag)) {
|
||||||
|
__HAL_TIM_CLEAR_FLAG(timer, captureFlag);
|
||||||
|
value = HAL_TIM_ReadCapturedValue(timer, channel);
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
return false;
|
||||||
|
},
|
||||||
|
kTimeout);
|
||||||
|
}
|
||||||
|
HAL_TIM_IC_Stop(timer, channel);
|
||||||
|
return (data[1] - data[0]) / 250.0;
|
||||||
|
} catch (const std::exception& e) {
|
||||||
|
HAL_TIM_IC_Stop(timer, channel);
|
||||||
|
throw;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
[[nodiscard]] static double CalculateDistance(double echoTime) {
|
||||||
|
return (echoTime * 0.0343) / 2.0;
|
||||||
|
}
|
||||||
|
|
||||||
|
static uint32_t GetCaptureFlag(uint32_t channel) {
|
||||||
|
switch (channel) {
|
||||||
|
case TIM_CHANNEL_1:
|
||||||
|
return TIM_SR_CC1IF;
|
||||||
|
case TIM_CHANNEL_2:
|
||||||
|
return TIM_SR_CC2IF;
|
||||||
|
case TIM_CHANNEL_3:
|
||||||
|
return TIM_SR_CC3IF;
|
||||||
|
case TIM_CHANNEL_4:
|
||||||
|
return TIM_SR_CC4IF;
|
||||||
|
default:
|
||||||
|
throw std::invalid_argument("Invalid timer channel");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
};
|
||||||
19
Core/App/Helper/task_helper.hpp
Normal file
19
Core/App/Helper/task_helper.hpp
Normal file
@@ -0,0 +1,19 @@
|
|||||||
|
#pragma once
|
||||||
|
|
||||||
|
#include "stm32h5xx_hal.h"
|
||||||
|
#include <cstdint>
|
||||||
|
#include <stdexcept>
|
||||||
|
|
||||||
|
class TaskHelper {
|
||||||
|
public:
|
||||||
|
template <typename Func>
|
||||||
|
static void WaitFor(Func func, int timeoutMilliseconds) {
|
||||||
|
auto start = HAL_GetTick();
|
||||||
|
while (!func()) {
|
||||||
|
uint32_t now = HAL_GetTick();
|
||||||
|
if ((now - start) > static_cast<uint32_t>(timeoutMilliseconds)) {
|
||||||
|
throw std::runtime_error("Operation timed out");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
};
|
||||||
@@ -1,21 +1,36 @@
|
|||||||
#include "Helper/delay_helper.h"
|
#include "Helper/delay_helper.h"
|
||||||
#include "stm32h563xx.h"
|
#include "stm32h563xx.h"
|
||||||
|
|
||||||
#include "Common/serial_port.hpp"
|
#include "Center/common_center.hpp"
|
||||||
#include "Helper/gpio_helper.hpp"
|
#include "Helper/gpio_helper.hpp"
|
||||||
|
#include "Helper/delay_helper.h"
|
||||||
|
|
||||||
|
void TerminateHandler() {
|
||||||
|
auto led = GpioHelper::GpioInit(GPIOB, GPIO_PIN_0, GPIO_MODE_OUTPUT_PP, GPIO_NOPULL, GPIO_PIN_SET);
|
||||||
|
while (true) {
|
||||||
|
led.Toggle();
|
||||||
|
DelayS(1);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
void Setup() {
|
void Setup() {
|
||||||
GpioHelper::EnableAllGpioPeripheral();
|
GpioHelper::EnableAllGpioPeripheral();
|
||||||
DelaySetup();
|
DelaySetup();
|
||||||
|
auto ultrasonic = CommonCenter::GetUltrasonic();
|
||||||
|
auto dfPlayer = CommonCenter::GetDfPlayer();
|
||||||
|
auto canMv = CommonCenter::GetCanMv();
|
||||||
}
|
}
|
||||||
|
|
||||||
extern "C" void AppStart() {
|
extern "C" void AppStart() {
|
||||||
Setup();
|
Setup();
|
||||||
auto gpio = GpioHelper::GpioInit(GPIOB, GPIO_PIN_0, GPIO_MODE_OUTPUT_PP, GPIO_NOPULL, GPIO_PIN_SET);
|
auto ultrasonic = CommonCenter::GetUltrasonic();
|
||||||
|
auto dfPlayer = CommonCenter::GetDfPlayer();
|
||||||
|
auto canMv = CommonCenter::GetCanMv();
|
||||||
while (true) {
|
while (true) {
|
||||||
gpio.Toggle();
|
// Example usage of the classes
|
||||||
SerialPort::WriteBlocking("LED is toggled,current state: %s", gpio.Read() ? "OFF" : "ON");
|
ultrasonic.GetDistance();
|
||||||
DelayS(1);
|
dfPlayer.PlayTrack(1);
|
||||||
|
|
||||||
|
DelayMs(1000); // Delay for 1 second
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
18
Core/App/config.hpp
Normal file
18
Core/App/config.hpp
Normal file
@@ -0,0 +1,18 @@
|
|||||||
|
#pragma once
|
||||||
|
|
||||||
|
#include "stm32h563xx.h"
|
||||||
|
#include "tim.h"
|
||||||
|
|
||||||
|
#include "./Helper/gpio_helper.hpp"
|
||||||
|
|
||||||
|
struct Config {
|
||||||
|
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* kDfPlayerUart = &hcom_uart[COM1];
|
||||||
|
|
||||||
|
static inline UART_HandleTypeDef* kCanMvUart = &hcom_uart[COM1];
|
||||||
|
};
|
||||||
@@ -182,7 +182,7 @@
|
|||||||
*/
|
*/
|
||||||
|
|
||||||
#define VDD_VALUE 3300UL /*!< Value of VDD in mv */
|
#define VDD_VALUE 3300UL /*!< Value of VDD in mv */
|
||||||
#define TICK_INT_PRIORITY (15UL) /*!< tick interrupt priority (lowest by default) */
|
#define TICK_INT_PRIORITY (14UL) /*!< tick interrupt priority */
|
||||||
#define USE_RTOS 0U
|
#define USE_RTOS 0U
|
||||||
#define PREFETCH_ENABLE 0U /*!< Enable prefetch */
|
#define PREFETCH_ENABLE 0U /*!< Enable prefetch */
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user