From 7403943814682738920a087b8cdd937cf8d10045 Mon Sep 17 00:00:00 2001 From: NeoZng Date: Sat, 20 May 2023 11:48:59 +0800 Subject: [PATCH] =?UTF-8?q?=E6=9B=B4=E6=96=B0dwt=E4=BA=92=E6=96=A5?= =?UTF-8?q?=E9=94=81,=E9=98=B2=E6=AD=A2=E5=A4=9A=E7=BA=BF=E7=A8=8B?= =?UTF-8?q?=E8=AE=BF=E9=97=AE=E5=AF=BC=E8=87=B4=E7=B3=BB=E7=BB=9F=E6=97=B6?= =?UTF-8?q?=E9=97=B4=E8=AE=A1=E7=AE=97=E5=BC=82=E5=B8=B8?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- bsp/dwt/bsp_dwt.c | 16 ++++++++++++---- 1 file changed, 12 insertions(+), 4 deletions(-) diff --git a/bsp/dwt/bsp_dwt.c b/bsp/dwt/bsp_dwt.c index 1ed3e09..58ab213 100644 --- a/bsp/dwt/bsp_dwt.c +++ b/bsp/dwt/bsp_dwt.c @@ -9,26 +9,32 @@ */ #include "bsp_dwt.h" +#include "cmsis_os.h" static DWT_Time_t SysTime; static uint32_t CPU_FREQ_Hz, CPU_FREQ_Hz_ms, CPU_FREQ_Hz_us; static uint32_t CYCCNT_RountCount; static uint32_t CYCCNT_LAST; static uint64_t CYCCNT64; +static osMutexId DWT_MUTEX; /** * @brief 私有函数,用于检查DWT CYCCNT寄存器是否溢出,并更新CYCCNT_RountCount * @attention 此函数假设两次调用之间的时间间隔不超过一次溢出 - * + * */ static void DWT_CNT_Update(void) { + if (__get_CONTROL()) // 不在中断中,使用互斥锁;在中断则直接执行即可 + if (osOK != osMutexWait(DWT_MUTEX, 0)) + return; + volatile uint32_t cnt_now = DWT->CYCCNT; - if (cnt_now < CYCCNT_LAST) CYCCNT_RountCount++; - CYCCNT_LAST = cnt_now; + CYCCNT_LAST = DWT->CYCCNT; + osMutexRelease(DWT_MUTEX); } void DWT_Init(uint32_t CPU_Freq_mHz) @@ -46,6 +52,9 @@ void DWT_Init(uint32_t CPU_Freq_mHz) CPU_FREQ_Hz_ms = CPU_FREQ_Hz / 1000; CPU_FREQ_Hz_us = CPU_FREQ_Hz / 1000000; CYCCNT_RountCount = 0; + osMutexDef(dwt_mutex); + DWT_MUTEX = osMutexCreate(osMutex(dwt_mutex)); + DWT_CNT_Update(); } float DWT_GetDeltaT(uint32_t *cnt_last) @@ -113,7 +122,6 @@ uint64_t DWT_GetTimeline_us(void) return DWT_Timelinef32; } - void DWT_Delay(float Delay) { uint32_t tickstart = DWT->CYCCNT;