1.在FreeRTOS内核启动前(即osKernelStart() 函数前),千万不能使用osDelay()来延时,否则会发生硬件错误
int main(void)
{
...
osKernelInitialize();
MX_FREERTOS_Init();
osKernelStart();
while(1)
{
}
...
}
2.使用常用公共资源时,该加锁的加锁,否则直接卡死,尤其像串口打印LOG这种会在多个任务中频繁使用的
(1)串口重定向
extern osMutexId_t printMutexHandle;
int fputc(int ch, FILE *f)
{
osSemaphoreAcquire( printMutexHandle, portMAX_DELAY );
{
HAL_UART_Transmit(&huart2,(uint8_t *)&ch,1,0xFFFF);
}
osSemaphoreRelease( printMutexHandle );
return (ch);
}
(2)IIC通信
static HAL_StatusTypeDef I2Cx_ReadMultiple(I2C_HandleTypeDef* I2Cx_HandleStruct,\
uint8_t Addr,\
uint16_t Reg,\
uint16_t MemAddress, \
uint8_t *Buffer,\
uint16_t Length)
{
osSemaphoreAcquire( eepromMutexHandle, portMAX_DELAY );
HAL_StatusTypeDef status = HAL_TIMEOUT;
status = HAL_I2C_Mem_Read(I2Cx_HandleStruct, Addr, (uint16_t)Reg, MemAddress, Buffer, Length, I2C_TIMEOUT);
if(status != HAL_OK)
{
if(I2Cx_HandleStruct->Instance == I2C1)
{
I2C1_Error();
}
else
{
I2C2_Error();
}
}
osSemaphoreRelease( eepromMutexHandle );
return status;
}
|