标题:
STM32H750单片机LPUART+BDMA控制程序
[打印本页]
作者:
辉耀石
时间:
2025-4-30 11:21
标题:
STM32H750单片机LPUART+BDMA控制程序
STM32H750 LPUART + BDMA 控制,认证 BDMA 使用
注意:BDMA 只能使用 SRAM4
#include "cspDwt.h"
#include <stdio.h>
#include "stm32h7xx_hal.h"
/* mdk 下载需要 勾选 Stop After Reset */
/* DWT 计时功能 */
typedef struct
{
unsigned char count;
unsigned int note[DWT_NOTE_MAX_NUMBER + 1];
}Ctrl_t;
static Ctrl_t s_ctrl = {0};
static Ctrl_t *p = &s_ctrl;
/* ----- DWT 延时 ----- */
#define DWT_CR *(volatile uint32_t *)0xE0001000
#define DWT_CYCCNT *(volatile uint32_t *)0xE0001004
#define DEM_CR *(volatile uint32_t *)0xE000EDFC
#define DEM_CR_TRCENA (1 << 24)
#define DWT_CR_CYCCNTENA (1 << 0)
/**
* @brief DWT 延时初始化
* @param void
* @return void
*/
void cDwt_Init(void)
{
DEM_CR |= DEM_CR_TRCENA;
DWT_CR |= DWT_CR_CYCCNTENA;
}
/**
* @brief DWT 微秒延时
* @param US 延时计数
* @return void
*/
void cDwt_DelayUs(unsigned int us)
{
unsigned int startTime = DWT_CYCCNT;
unsigned int time = us * (SystemCoreClock / (1000000));
unsigned int endTime = startTime + time;
if (endTime > startTime)
{
while ((DWT_CYCCNT > startTime) && (DWT_CYCCNT < endTime));
}
else
{
while ((DWT_CYCCNT > startTime) && (DWT_CYCCNT < endTime));
}
}
/**
* @brief DWT 毫秒延时
* @param MS 延时计数
* @return void
*/
void cDwt_DelayMs(unsigned int ms)
{
for(unsigned int i = 0; i < ms; i++)
{
cDwt_DelayUs(1000);
}
}
/* 计时结束清零 */
void cDwt_Start(void)
{
p->count = 1;
p->note[0] = DWT_CYCCNT;
}
/* 计时打点 */
void cDwt_Note(void)
{
if (p->count >= DWT_NOTE_MAX_NUMBER)
{
printf("%s 计时失败 计时点不足\r\n", __func__);
}
p->note[p->count] = DWT_CYCCNT;
p->count++;
}
/* 计时结束 + 打印时间参数 */
void cDwt_End(void)
{
unsigned int coreClock = HAL_RCC_GetSysClockFreq() / 1000000;
unsigned int realCount = 0;
unsigned int totalUs = 0;
unsigned int showUs = 0;
unsigned int showMs = 0;
unsigned int showS = 0;
for (int i = 0; i < p->count; i++)
{
/* 判断时钟是否重新计时 */
if (p->note[i] < p->note[0])
{
realCount = 0xFFFFFFFF - p->note[0] + p->note[i];
}
else
{
realCount = p->note[i] - p->note[0];
}
totalUs = realCount / coreClock;
showS = totalUs / 1000000;
showMs = (totalUs - showS * 1000000) / 1000;
showUs = (totalUs - showS * 1000000 - showMs * 1000);
printf("Time[%d]-[%d]s-[%d]ms-[%d]us\r\n", i, showS, showMs, showUs);
}
}
复制代码
原理图: 无
仿真: 无
代码:
STM32H750VB_CubeMx_LPUART_BDMA.7z
(812.65 KB, 下载次数: 0)
2025-5-1 14:47 上传
点击文件名下载附件
示例代码
下载积分: 黑币 -5
欢迎光临 (http://www.51hei.com/bbs/)
Powered by Discuz! X3.1