更新dwt互斥锁,防止多线程访问导致系统时间计算异常

This commit is contained in:
NeoZng 2023-05-20 11:48:59 +08:00
parent a18a5091f4
commit 7403943814
1 changed files with 12 additions and 4 deletions

View File

@ -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;