使用位锁替换mutex和semaphore实现,仅支持单核

This commit is contained in:
NeoZng 2023-07-19 13:58:42 +08:00
parent 3faa9f1f8f
commit e938075e11
3 changed files with 32 additions and 13 deletions

20
.vscode/c_cpp_properties.json vendored Normal file
View File

@ -0,0 +1,20 @@
{
"configurations": [
{
"name": "Win32",
"includePath": [
"${workspaceFolder}/**"
],
"defines": [
"_DEBUG",
"UNICODE",
"_UNICODE"
],
"cStandard": "c17",
"cppStandard": "gnu++17",
"intelliSenseMode": "windows-gcc-arm",
"configurationProvider": "ms-vscode.makefile-tools"
}
],
"version": 4
}

View File

@ -16,7 +16,6 @@ static uint32_t CPU_FREQ_Hz, CPU_FREQ_Hz_ms, CPU_FREQ_Hz_us;
static uint32_t CYCCNT_RountCount; static uint32_t CYCCNT_RountCount;
static uint32_t CYCCNT_LAST; static uint32_t CYCCNT_LAST;
static uint64_t CYCCNT64; static uint64_t CYCCNT64;
static osMutexId DWT_MUTEX;
/** /**
* @brief ,DWT CYCCNT寄存器是否溢出,CYCCNT_RountCount * @brief ,DWT CYCCNT寄存器是否溢出,CYCCNT_RountCount
@ -28,16 +27,17 @@ static osMutexId DWT_MUTEX;
*/ */
static void DWT_CNT_Update(void) static void DWT_CNT_Update(void)
{ {
if (__get_CONTROL()) // 不在中断中,使用互斥锁;在中断则直接执行即可,本框架将所有中断优先级设置为相同,故不会被其他中断重入 static volatile uint8_t bit_locker = 0;
if (osOK != osMutexWait(DWT_MUTEX, 0)) if (!bit_locker)
return; {
bit_locker = 1;
volatile uint32_t cnt_now = DWT->CYCCNT; volatile uint32_t cnt_now = DWT->CYCCNT;
if (cnt_now < CYCCNT_LAST) if (cnt_now < CYCCNT_LAST)
CYCCNT_RountCount++; CYCCNT_RountCount++;
CYCCNT_LAST = DWT->CYCCNT; CYCCNT_LAST = DWT->CYCCNT;
osMutexRelease(DWT_MUTEX); bit_locker = 0;
}
} }
void DWT_Init(uint32_t CPU_Freq_mHz) void DWT_Init(uint32_t CPU_Freq_mHz)
@ -55,8 +55,7 @@ void DWT_Init(uint32_t CPU_Freq_mHz)
CPU_FREQ_Hz_ms = CPU_FREQ_Hz / 1000; CPU_FREQ_Hz_ms = CPU_FREQ_Hz / 1000;
CPU_FREQ_Hz_us = CPU_FREQ_Hz / 1000000; CPU_FREQ_Hz_us = CPU_FREQ_Hz / 1000000;
CYCCNT_RountCount = 0; CYCCNT_RountCount = 0;
osMutexDef(dwt_mutex);
DWT_MUTEX = osMutexCreate(osMutex(dwt_mutex));
DWT_CNT_Update(); DWT_CNT_Update();
} }