1、RCC选择
2、sys配置
3、 时钟配置
4、CAN参数配置
5、CAN中断配置开启CAN1的收/发和SCE中断
6、添加freeRTOS和任务
7、主函数USER CODE 2中添加CAN启动和中断使能函数
MX_CAN_Init();
/* USER CODE BEGIN 2 */
HAL_CAN_Start(&hcan);
HAL_CAN_ActivateNotification(&hcan,CAN_IT_TX_MAILBOX_EMPTY | CAN_IT_RX_FIFO0_MSG_PENDING | CAN_IT_WAKEUP);
/* USER CODE END 2 */
8、在一个任务中将一直发送CAN报文
void StartDefaultTask(void *argument)
{
/* USER CODE BEGIN 5 */
Can_Tx.StdId = 0x100;
Can_Tx.ExtId = 0x123;
Can_Tx.IDE = 0;
Can_Tx.RTR = 0;
Can_Tx.DLC = 8;
/* Infinite loop */
for(;;)
{
osDelay(100);
HAL_CAN_AddTxMessage(&hcan,&Can_Tx,tdata,&pTxMailbox);
}
/* USER CODE END 5 */
}
8、任务中将CAN总线休眠
void APPStartTask(void *argument)
{
/* USER CODE BEGIN APPStartTask */
/* Infinite loop */
for(;;)
{
osDelay(15000);
HAL_CAN_RequestSleep(&hcan); //将CAN总线设置进入sleep mode
}
/* USER CODE END APPStartTask */
}
9、CAN总线休眠后,在CAN接收引脚上监视到帧起始位(SOF)后唤醒CAN,并触发中断,中断需要清除sleep位
void HAL_CAN_WakeUpFromRxMsgCallback(CAN_HandleTypeDef *hcan)
{
HAL_CAN_WakeUp(hcan);
}