使用位锁替换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

@ -80,7 +80,7 @@ CANInstance *CANRegister(CAN_Init_Config_s *config)
LOGERROR("[}bsp_can] CAN id crash ,tx [%d] or rx [%d] already registered", &config->tx_id, &config->rx_id);
}
}
CANInstance *instance = (CANInstance *)malloc(sizeof(CANInstance)); // 分配空间
memset(instance, 0, sizeof(CANInstance)); // 分配的空间未必是0,所以要先清空
// 进行发送报文的配置

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