使用位锁替换mutex和semaphore实现,仅支持单核
This commit is contained in:
parent
3faa9f1f8f
commit
e938075e11
|
@ -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
|
||||||
|
}
|
|
@ -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);
|
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)); // 分配空间
|
CANInstance *instance = (CANInstance *)malloc(sizeof(CANInstance)); // 分配空间
|
||||||
memset(instance, 0, sizeof(CANInstance)); // 分配的空间未必是0,所以要先清空
|
memset(instance, 0, sizeof(CANInstance)); // 分配的空间未必是0,所以要先清空
|
||||||
// 进行发送报文的配置
|
// 进行发送报文的配置
|
||||||
|
|
|
@ -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;
|
||||||
|
if (cnt_now < CYCCNT_LAST)
|
||||||
|
CYCCNT_RountCount++;
|
||||||
|
|
||||||
volatile uint32_t cnt_now = DWT->CYCCNT;
|
CYCCNT_LAST = DWT->CYCCNT;
|
||||||
if (cnt_now < CYCCNT_LAST)
|
bit_locker = 0;
|
||||||
CYCCNT_RountCount++;
|
}
|
||||||
|
|
||||||
CYCCNT_LAST = DWT->CYCCNT;
|
|
||||||
osMutexRelease(DWT_MUTEX);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
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();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue