|
6#
楼主 |
发表于 2020-8-27 14:22:03
|
只看该作者
本帖最后由 李维强-15级 于 2020-9-7 16:13 编辑
说明:调用前请先查看UserCan.h中的 “模块配置参数”部分,定义相应的过滤ID和波特率,其中波特率配置可参考楼上帖子
调用示例1:
第一步:
下面给出通过上面CAN总结而得到的代码
首先按照cube上面的配置,250K波特率使能,并且直接生成keil代码即可
第二步:
用下面的UserCan.h与UserCan.c文件的内容 替换cube生成的can.h与can.c即可
第三步:
1)在 main文件 内 添加#include "UserCan.h",
2)main函数初始化阶段,添加 CAN_Config_User(); 即可
第四步:
在UserCan.c的回调函数“用户代码区”添加相应用户代码即可
第五步(非必要):
在中断控制文件中“stm32fXXXX_it.c”文件中,
1)添加#include "UserCan.h"
2)替代所有HAL_CAN_IRQHandler(&hcan1) 为 My_HAL_CAN_IRQHandler(&hcan1)
调用示例2:
第一步:
1)在 main文件 内 添加#include "UserCan.h",
2)main函数初始化阶段,添加 CAN_Config_User(); MX_CAN1_Init();
第二步:
在UserCan.c的回调函数“用户代码区”添加相应用户代码即可
第三步(非必要):
在中断控制文件中“stm32fXXXX_it.c”文件中,
1)添加#include "UserCan.h"
2)替代所有HAL_CAN_IRQHandler(&hcan1) 为 My_HAL_CAN_IRQHandler(&hcan1)
源文件
can.h
- #ifndef __can_H
- #define __can_H
- #ifdef __cplusplus
- extern "C" {
- #endif
- #include "main.h"
- extern CAN_HandleTypeDef hcan1;
- /***************************** 模块配置参数 Begin********************************************/
- /* 该模块采用列表滤波器,可直接命中2个标准帧ID */
- #define ID1 (0x010) //定义一个滤波器ID
- #define ID2 (0x001) //定义一个滤波器ID
- //波特率设置开关 以下两个宏定义,只能设置一个
- #define CAN_APB1_42M //设置波特率,对应STM32F407-168M时钟-APB1-42M时钟
- //#define CAN_APB1_36M //设置波特率,对应STM32F302-72M时钟-APB1-36M时钟
- /***************************** 模块配置参数 End********************************************/
- /***************************** 结构体设计 Begin ********************************************/
- typedef struct
- {
- uint8_t data[8];
- uint8_t dataSize; //数据长度
- FlagStatus RxFlag; //接收标志
- } CAN_RecvMSG_Struct;
- typedef struct {
- uint8_t data[8];
- uint8_t dataSize; //数据长度
- FlagStatus TxFlag; //接收标志
- } CAN_TxMSG_Struct;
- typedef struct {
- CAN_TxHeaderTypeDef TxHandle;
- CAN_RxHeaderTypeDef RxHandle;
-
- CAN_RecvMSG_Struct CAN_RxMsg;
- CAN_TxMSG_Struct CAN_TxMsg;
- } CAN_User_Struct;
- /***************************** 结构体设计 End ********************************************/
- /***************************** 外部接口 Begin********************************************/
- void MX_CAN1_Init(void); //初始化CAN1(若需移植,则需要自行利用cube配置。当前配置APB1=42M -> CAN波特率为250K)
- void CAN_Config_User(void); //配置CAN1(设置滤波器,使能中断,开启CAN1)
- //以上两个函数在main函数中,一系列初始化开始调用
- HAL_StatusTypeDef CAN_TRANSMIT(uint32_t tx_StdId, uint8_t* tx_data_buff, uint8_t data_size); //发送函数
- void My_HAL_CAN_IRQHandler(CAN_HandleTypeDef *hcan); //自定义中断服务函数,可替换 void HAL_CAN_IRQHandler(CAN_HandleTypeDef *hcan)
- /***************************** 外部接口 End********************************************/
- /***************************** 内部函数 Begin********************************************/
- static HAL_StatusTypeDef CAN_Start_User(void);
- /***************************** 内部函数 End********************************************/
- /*
- ==============================================================================
- ##### 系统中断回调函数 #####
- ### 以下中断回调函数在UserCan.c中,并在函数“用户代码区”处做对应操作
- ==============================================================================
- void HAL_CAN_ErrorCallback(CAN_HandleTypeDef *hcan) //错误中断回调
- void HAL_CAN_RxFifo0FullCallback(CAN_HandleTypeDef *hcan) //FIFO0->接收消息->队列已满中断
- void HAL_CAN_RxFifo0MsgPendingCallback(CAN_HandleTypeDef *hcan) //FIFO0->接收消息中断
- void HAL_CAN_TxMailbox0CompleteCallback(CAN_HandleTypeDef *hcan)//发送邮箱0 发送完成中断
- void HAL_CAN_TxMailbox1CompleteCallback(CAN_HandleTypeDef *hcan)//发送邮箱1 发送完成中断
- void HAL_CAN_TxMailbox2CompleteCallback(CAN_HandleTypeDef *hcan)//发送邮箱2 发送完成中断
- */
- /***************************** CAN发送调用示例 Begin********************************************/
- void MyCANSend(uint32_t tx_StdId);
- /***************************** CAN发送调用示例 End********************************************/
- #ifdef __cplusplus
- }
- #endif
- #endif /*__ can_H */
- /************************ (C) COPYRIGHT DTWT *****END OF FILE****/
复制代码
下面在can.c里面对应位置(cube给出的用户代码添加位置)添加相应代码
- #include "string.h"
- #include "UserCan.h"
- CAN_HandleTypeDef hcan1;
- CAN_User_Struct CAN_User; //CAN数据结构体
- /***************************** 外部接口 Begin********************************************/
- /*
- *********************************************************************************************************
- * 功能说明: 初始化CAN1
- * 形 参: 无
- * 返 回 值: 无
- **********************************************************************************************************/
- void MX_CAN1_Init(void)
- {
- #ifdef CAN_APB1_42M
- hcan1.Instance = CAN1;
- hcan1.Init.Prescaler = 14;
- hcan1.Init.Mode = CAN_MODE_NORMAL;
- hcan1.Init.SyncJumpWidth = CAN_SJW_1TQ;
- hcan1.Init.TimeSeg1 = CAN_BS1_5TQ;
- hcan1.Init.TimeSeg2 = CAN_BS2_6TQ;
- hcan1.Init.TimeTriggeredMode = DISABLE;
- hcan1.Init.AutoBusOff = DISABLE;
- hcan1.Init.AutoWakeUp = DISABLE;
- hcan1.Init.AutoRetransmission = DISABLE;
- hcan1.Init.ReceiveFifoLocked = DISABLE;
- hcan1.Init.TransmitFifoPriority = DISABLE;
- if (HAL_CAN_Init(&hcan1) != HAL_OK)
- {
- Error_Handler();
- }
- #endif
-
- #ifdef CAN_APB1_36M
- hcan.Instance = CAN;
- hcan.Init.Prescaler = 12;
- hcan.Init.Mode = CAN_MODE_NORMAL;
- hcan.Init.SyncJumpWidth = CAN_SJW_1TQ;
- hcan.Init.TimeSeg1 = CAN_BS1_5TQ;
- hcan.Init.TimeSeg2 = CAN_BS2_6TQ;
- hcan.Init.TimeTriggeredMode = DISABLE;
- hcan.Init.AutoBusOff = DISABLE;
- hcan.Init.AutoWakeUp = DISABLE;
- hcan.Init.AutoRetransmission = DISABLE;
- hcan.Init.ReceiveFifoLocked = DISABLE;
- hcan.Init.TransmitFifoPriority = DISABLE;
- if (HAL_CAN_Init(&hcan) != HAL_OK)
- {
- Error_Handler();
- }
- #endif
-
- }
- /*
- *********************************************************************************************************
- * 功能说明: 配置CAN1(设置滤波器,使能中断,开启CAN1)
- * 形 参: 无
- * 返 回 值: 无
- **********************************************************************************************************/
- void CAN_Config_User(void)
- {
- CAN_FilterTypeDef CAN_FilterStructure;
-
- CAN_FilterStructure.FilterActivation = ENABLE;
- CAN_FilterStructure.FilterBank = 0;
- CAN_FilterStructure.FilterFIFOAssignment = CAN_FILTER_FIFO0;
- CAN_FilterStructure.FilterIdHigh = (ID1 << 5);
- CAN_FilterStructure.FilterIdLow = 0X0000;
- CAN_FilterStructure.FilterMaskIdHigh = (ID2 << 5);
- CAN_FilterStructure.FilterMaskIdLow = 0X0000;
- CAN_FilterStructure.FilterMode = CAN_FILTERMODE_IDLIST; // CAN_FILTERMODE_IDLIST; CAN_FILTERMODE_IDMASK
- CAN_FilterStructure.FilterScale = CAN_FILTERSCALE_32BIT;
- CAN_FilterStructure.SlaveStartFilterBank = 0;
-
- if(HAL_CAN_ConfigFilter(&hcan1, &CAN_FilterStructure) != HAL_OK)
- {
- Error_Handler();
- }
-
- // 发送数据的配置
- CAN_User.TxHandle.IDE = CAN_ID_STD; //设置为标准帧
- CAN_User.TxHandle.RTR = CAN_RTR_DATA; //设置为数据帧
- CAN_User.CAN_TxMsg.TxFlag=RESET;
- CAN_User.CAN_RxMsg.RxFlag=RESET;
-
- HAL_Delay(1);
-
- if(CAN_Start_User() != HAL_OK)
- {
- Error_Handler();
- }
- }
- /*
- *********************************************************************************************************
- * 功能说明: CAN发送
- * 形 参: tx_StdId 发送目的帧ID
- * 形 参: tx_data_buff 发送数据内容,数组BUFF指针
- * 形 参: data_size 发送数据长度数据长度
- * 返 回 值: HAL_StatusTypeDef 调用后,正确/错误标志
- **********************************************************************************************************/
- HAL_StatusTypeDef CAN_TRANSMIT(uint32_t tx_StdId, uint8_t* tx_data_buff, uint8_t data_size)
- {
- uint8_t ArrSize;
- ArrSize=sizeof(tx_data_buff);
- if(ArrSize>8) return HAL_ERROR;
-
- uint32_t CAN_Tx_MailBox;
- CAN_User.TxHandle.StdId = tx_StdId;
- CAN_User.TxHandle.DLC = data_size;
- if(HAL_CAN_AddTxMessage(&hcan1, &(CAN_User.TxHandle), tx_data_buff, &CAN_Tx_MailBox) != HAL_OK)
- {
- //这里表示队列3级缓冲已经满了,放不进去数据
- return HAL_ERROR;
- }
- return HAL_OK;
-
- }
- /***************************** 外部接口 End********************************************/
- /***************************** 回调函数 Begin********************************************/
- /*
- *********************************************************************************************************
- * 功能说明: CAN接收消息的回调函数,用户代码添加在“用户代码段”
- * 形 参: hcan CAN1指针句柄
- * 返 回 值: 无
- **********************************************************************************************************/
- void HAL_CAN_RxFifo0MsgPendingCallback(CAN_HandleTypeDef *hcan)
- {
- HAL_StatusTypeDef HAL_RetVal;
- uint8_t Count,i;
- if(hcan ==&hcan1)
- {
- Count=HAL_CAN_GetRxFifoFillLevel(hcan, CAN_RX_FIFO0);
- memset(CAN_User.CAN_RxMsg.data,0,8);
- if(Count == 1)//缓存中只有一帧数据
- {
- HAL_RetVal=HAL_CAN_GetRxMessage(hcan, CAN_RX_FIFO0, &CAN_User.RxHandle, CAN_User.CAN_RxMsg.data);
- if(HAL_RetVal == HAL_OK)
- {
- CAN_User.CAN_RxMsg.dataSize=CAN_User.RxHandle.DLC;
- //这里获取到数据放在CAN_User.CAN_RxMsg.data内,在此做相应的操作
- //用户代码段 开始
-
- //用户代码段 结束
- }
- }
- else if(Count > 1)//缓存中有多帧数据,需全部提取
- {
- for(i=0;i<Count;i++)
- {
- HAL_RetVal=HAL_CAN_GetRxMessage(hcan, CAN_RX_FIFO0, &CAN_User.RxHandle, CAN_User.CAN_RxMsg.data);
- if(HAL_RetVal == HAL_OK)
- {
- CAN_User.CAN_RxMsg.dataSize=CAN_User.RxHandle.DLC;
- //这里获取到数据放在CAN_User.CAN_RxMsg.data内,在此做相应的操作
- //用户代码段 开始
-
- //用户代码段 结束
- }
- }
- }
- }
- }
- /*
- *********************************************************************************************************
- * 功能说明: CAN接收消息,FIFO队列已满中断,用户代码添加在“用户代码段”
- * 形 参: hcan CAN1指针句柄
- * 返 回 值: 无
- **********************************************************************************************************/
- void HAL_CAN_RxFifo0FullCallback(CAN_HandleTypeDef *hcan)
- {
- HAL_StatusTypeDef HAL_RetVal;
- uint8_t Count,i;
-
- Count=HAL_CAN_GetRxFifoFillLevel(hcan, CAN_RX_FIFO0); //判断FIFO里面有多少帧数据 正常来说是3帧数据
-
- for(i=0;i<Count;i++) //这里表示大于1帧,故需要把全部数据都取出来
- {
- HAL_RetVal=HAL_CAN_GetRxMessage(hcan, CAN_RX_FIFO0, &CAN_User.RxHandle, CAN_User.CAN_RxMsg.data);
- if(HAL_RetVal == HAL_OK)
- {
- //用户代码段 开始
-
- //用户代码段 结束
- }
- }
-
- }
- /*
- *********************************************************************************************************
- * 功能说明: 错误中断回调,用户代码添加在“用户代码段”
- * 形 参: hcan CAN1指针句柄
- * 返 回 值: 无
- **********************************************************************************************************/
- void HAL_CAN_ErrorCallback(CAN_HandleTypeDef *hcan)
- {
- //进入此函数,就表示发送和接收肯定出错了
-
- /*发送方面的错误 **********************************************/
- //发送邮箱0 仲裁/发送失败
- if(hcan->ErrorCode & HAL_CAN_ERROR_TX_ALST0 || hcan->ErrorCode & HAL_CAN_ERROR_TX_TERR0)
- {
- //用户代码段 开始
-
- //用户代码段 结束
- }
- //发送邮箱1 仲裁/发送失败
- if(hcan->ErrorCode & HAL_CAN_ERROR_TX_ALST1 || hcan->ErrorCode & HAL_CAN_ERROR_TX_TERR1)
- {
- //用户代码段 开始
-
- //用户代码段 结束
- }
- //发送邮箱2 仲裁/发送失败
- if(hcan->ErrorCode & HAL_CAN_ERROR_TX_ALST2 || hcan->ErrorCode & HAL_CAN_ERROR_TX_TERR2)
- {
- //用户代码段 开始
-
- //用户代码段 结束
- }
- /**********************************************************************************************************/
-
- /*接收方面的错误 **********************************************/
- //FIFO-0接收缓冲溢出
- if(hcan->ErrorCode & HAL_CAN_ERROR_RX_FOV0)
- {
- //用户代码段 开始
-
- //用户代码段 结束
- }
- //FIFO-1接收缓冲溢出 该溢出前期没配置
- if(hcan->ErrorCode & HAL_CAN_ERROR_RX_FOV1)
- {
-
- }
- /**********************************************************************************************************/
-
- /**错误警告超过阈值 **********************************************/
- //CAN已经进入警告模式
- if(hcan->ErrorCode & HAL_CAN_ERROR_EWG)
- {
- //用户代码段 开始
-
- //用户代码段 结束
- }
- //CAN已经进入被动错误模式
- if(hcan->ErrorCode & HAL_CAN_ERROR_EPV)
- {
- //用户代码段 开始
-
- //用户代码段 结束
- }
-
- }
- /*
- *********************************************************************************************************
- * 功能说明: 邮箱0发送完成中断,用户代码添加在“用户代码段”
- * 形 参: hcan CAN1指针句柄
- * 返 回 值: 无
- **********************************************************************************************************/
- //
- void HAL_CAN_TxMailbox0CompleteCallback(CAN_HandleTypeDef *hcan)
- {
- //用户代码段 开始
-
- //用户代码段 结束
- }
- /*
- *********************************************************************************************************
- * 功能说明: 邮箱1发送完成中断,用户代码添加在“用户代码段”
- * 形 参: hcan CAN1指针句柄
- * 返 回 值: 无
- **********************************************************************************************************/
- void HAL_CAN_TxMailbox1CompleteCallback(CAN_HandleTypeDef *hcan)
- {
- //用户代码段 开始
-
- //用户代码段 结束
- }
- /*
- *********************************************************************************************************
- * 功能说明: 邮箱2发送完成中断,用户代码添加在“用户代码段”
- * 形 参: hcan CAN1指针句柄
- * 返 回 值: 无
- **********************************************************************************************************/
- void HAL_CAN_TxMailbox2CompleteCallback(CAN_HandleTypeDef *hcan)
- {
- //用户代码段 开始
-
- //用户代码段 结束
- }
- /***************************** 回调函数 End********************************************/
- /***************************** CAN发送调用示例 Begin********************************************/
- /*
- *********************************************************************************************************
- * 功能说明: CAN发送调用示例
- * 形 参: tx_StdId 目的帧ID
- * 返 回 值: 无
- **********************************************************************************************************/
- void MyCANSend(uint32_t tx_StdId)
- {
- uint8_t cmd_data[5] = {0x11, 0x22, 0x33,0x44, 0x55};
- if(HAL_ERROR == CAN_TRANSMIT(ID2,cmd_data,sizeof(cmd_data)))
- {
- //发送失败,做对应操作
- }
- else
- {
- //发送成功,做对应操作
- }
- }
- /***************************** CAN发送调用示例 End********************************************/
- /***************************** 内部函数 Begin********************************************/
- //CAN引脚配置,中断优先级配置
- void HAL_CAN_MspInit(CAN_HandleTypeDef* canHandle)
- {
- GPIO_InitTypeDef GPIO_InitStruct = {0};
- if(canHandle->Instance==CAN1)
- {
- /* USER CODE BEGIN CAN1_MspInit 0 */
- /* USER CODE END CAN1_MspInit 0 */
- /* CAN1 clock enable */
- __HAL_RCC_CAN1_CLK_ENABLE();
-
- __HAL_RCC_GPIOA_CLK_ENABLE();
- /**CAN1 GPIO Configuration
- PA11 ------> CAN1_RX
- PA12 ------> CAN1_TX
- */
- GPIO_InitStruct.Pin = GPIO_PIN_11|GPIO_PIN_12;
- GPIO_InitStruct.Mode = GPIO_MODE_AF_PP;
- GPIO_InitStruct.Pull = GPIO_NOPULL;
- GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_VERY_HIGH;
- GPIO_InitStruct.Alternate = GPIO_AF9_CAN1;
- HAL_GPIO_Init(GPIOA, &GPIO_InitStruct);
- /* CAN1 interrupt Init */
- HAL_NVIC_SetPriority(CAN1_TX_IRQn, 2, 3);
- HAL_NVIC_EnableIRQ(CAN1_TX_IRQn);
- HAL_NVIC_SetPriority(CAN1_RX0_IRQn, 2, 2);
- HAL_NVIC_EnableIRQ(CAN1_RX0_IRQn);
- HAL_NVIC_SetPriority(CAN1_SCE_IRQn, 2, 1);
- HAL_NVIC_EnableIRQ(CAN1_SCE_IRQn);
- /* USER CODE BEGIN CAN1_MspInit 1 */
- /* USER CODE END CAN1_MspInit 1 */
- }
- }
- //取消CAN引脚初始化,取消中断使能
- void HAL_CAN_MspDeInit(CAN_HandleTypeDef* canHandle)
- {
- if(canHandle->Instance==CAN1)
- {
- /* USER CODE BEGIN CAN1_MspDeInit 0 */
- /* USER CODE END CAN1_MspDeInit 0 */
- /* Peripheral clock disable */
- __HAL_RCC_CAN1_CLK_DISABLE();
-
- /**CAN1 GPIO Configuration
- PA11 ------> CAN1_RX
- PA12 ------> CAN1_TX
- */
- HAL_GPIO_DeInit(GPIOA, GPIO_PIN_11|GPIO_PIN_12);
- /* CAN1 interrupt Deinit */
- HAL_NVIC_DisableIRQ(CAN1_TX_IRQn);
- HAL_NVIC_DisableIRQ(CAN1_RX0_IRQn);
- HAL_NVIC_DisableIRQ(CAN1_SCE_IRQn);
- /* USER CODE BEGIN CAN1_MspDeInit 1 */
- /* USER CODE END CAN1_MspDeInit 1 */
- }
- }
- // 使能CAN中断、启动CAN
- static HAL_StatusTypeDef CAN_Start_User(void)
- {
- if(HAL_CAN_ActivateNotification(&hcan1, CAN_IT_RX_FIFO0_MSG_PENDING) != HAL_OK)
- {
- return HAL_ERROR;
- }
-
- if(HAL_CAN_ActivateNotification(&hcan1, CAN_IT_RX_FIFO0_FULL) != HAL_OK)
- {
- return HAL_ERROR;
- }
- if(HAL_CAN_ActivateNotification(&hcan1, CAN_IT_ERROR) != HAL_OK) //使能错误中断
- {
- return HAL_ERROR;
- }
- if(HAL_CAN_ActivateNotification(&hcan1, CAN_IT_ERROR_PASSIVE) != HAL_OK) //使能被动错误中断
- {
- return HAL_ERROR;
- }
- if(HAL_CAN_ActivateNotification(&hcan1, CAN_IT_BUSOFF) != HAL_OK) //使能BUSOFF中断
- {
- return HAL_ERROR;
- }
- if(HAL_CAN_ActivateNotification(&hcan1, CAN_IT_TX_MAILBOX_EMPTY) != HAL_OK) //使能发送中断
- {
- return HAL_ERROR;
- }
-
- if(HAL_CAN_Start(&hcan1) != HAL_OK)
- {
- return HAL_ERROR;
- }
-
- return HAL_OK;
- }
- /*
- *********************************************************************************************************
- * 功能说明: 重写 CAN中断处理函数 代替原来的 HAL_CAN_IRQHandler
- * 形 参: hcan CAN1指针句柄
- * 返 回 值: 无
- * 优化说明: 1、接收FIFO-1 溢出中断优化
- 2、接收FIFO-1 装满中断优化
- 3、接收FIFO-1 接收回调中断优化
- 4、CAN睡眠中断优化
- 5、CAN唤醒中断优化
- **********************************************************************************************************/
- void My_HAL_CAN_IRQHandler(CAN_HandleTypeDef *hcan)
- {
- uint32_t errorcode = HAL_CAN_ERROR_NONE;
- uint32_t interrupts = READ_REG(hcan->Instance->IER);
- uint32_t msrflags = READ_REG(hcan->Instance->MSR);
- uint32_t tsrflags = READ_REG(hcan->Instance->TSR);
- uint32_t rf0rflags = READ_REG(hcan->Instance->RF0R);
- //uint32_t rf1rflags = READ_REG(hcan->Instance->RF1R);
- uint32_t esrflags = READ_REG(hcan->Instance->ESR);
- /* Transmit Mailbox empty interrupt management *****************************/
- if ((interrupts & CAN_IT_TX_MAILBOX_EMPTY) != 0U)
- {
- /* Transmit Mailbox 0 management *****************************************/
- if ((tsrflags & CAN_TSR_RQCP0) != 0U)
- {
- /* Clear the Transmission Complete flag (and TXOK0,ALST0,TERR0 bits) */
- __HAL_CAN_CLEAR_FLAG(hcan, CAN_FLAG_RQCP0);
- if ((tsrflags & CAN_TSR_TXOK0) != 0U)
- {
- /* Transmission Mailbox 0 complete callback */
- #if USE_HAL_CAN_REGISTER_CALLBACKS == 1
- /* Call registered callback*/
- hcan->TxMailbox0CompleteCallback(hcan);
- #else
- /* Call weak (surcharged) callback */
- HAL_CAN_TxMailbox0CompleteCallback(hcan);
- #endif /* USE_HAL_CAN_REGISTER_CALLBACKS */
- }
- else
- {
- if ((tsrflags & CAN_TSR_ALST0) != 0U)
- {
- /* Update error code */
- errorcode |= HAL_CAN_ERROR_TX_ALST0;
- }
- else if ((tsrflags & CAN_TSR_TERR0) != 0U)
- {
- /* Update error code */
- errorcode |= HAL_CAN_ERROR_TX_TERR0;
- }
- else
- {
- /* Transmission Mailbox 0 abort callback */
- #if USE_HAL_CAN_REGISTER_CALLBACKS == 1
- /* Call registered callback*/
- hcan->TxMailbox0AbortCallback(hcan);
- #else
- /* Call weak (surcharged) callback */
- HAL_CAN_TxMailbox0AbortCallback(hcan);
- #endif /* USE_HAL_CAN_REGISTER_CALLBACKS */
- }
- }
- }
- /* Transmit Mailbox 1 management *****************************************/
- if ((tsrflags & CAN_TSR_RQCP1) != 0U)
- {
- /* Clear the Transmission Complete flag (and TXOK1,ALST1,TERR1 bits) */
- __HAL_CAN_CLEAR_FLAG(hcan, CAN_FLAG_RQCP1);
- if ((tsrflags & CAN_TSR_TXOK1) != 0U)
- {
- /* Transmission Mailbox 1 complete callback */
- #if USE_HAL_CAN_REGISTER_CALLBACKS == 1
- /* Call registered callback*/
- hcan->TxMailbox1CompleteCallback(hcan);
- #else
- /* Call weak (surcharged) callback */
- HAL_CAN_TxMailbox1CompleteCallback(hcan);
- #endif /* USE_HAL_CAN_REGISTER_CALLBACKS */
- }
- else
- {
- if ((tsrflags & CAN_TSR_ALST1) != 0U)
- {
- /* Update error code */
- errorcode |= HAL_CAN_ERROR_TX_ALST1;
- }
- else if ((tsrflags & CAN_TSR_TERR1) != 0U)
- {
- /* Update error code */
- errorcode |= HAL_CAN_ERROR_TX_TERR1;
- }
- else
- {
- /* Transmission Mailbox 1 abort callback */
- #if USE_HAL_CAN_REGISTER_CALLBACKS == 1
- /* Call registered callback*/
- hcan->TxMailbox1AbortCallback(hcan);
- #else
- /* Call weak (surcharged) callback */
- HAL_CAN_TxMailbox1AbortCallback(hcan);
- #endif /* USE_HAL_CAN_REGISTER_CALLBACKS */
- }
- }
- }
- /* Transmit Mailbox 2 management *****************************************/
- if ((tsrflags & CAN_TSR_RQCP2) != 0U)
- {
- /* Clear the Transmission Complete flag (and TXOK2,ALST2,TERR2 bits) */
- __HAL_CAN_CLEAR_FLAG(hcan, CAN_FLAG_RQCP2);
- if ((tsrflags & CAN_TSR_TXOK2) != 0U)
- {
- /* Transmission Mailbox 2 complete callback */
- #if USE_HAL_CAN_REGISTER_CALLBACKS == 1
- /* Call registered callback*/
- hcan->TxMailbox2CompleteCallback(hcan);
- #else
- /* Call weak (surcharged) callback */
- HAL_CAN_TxMailbox2CompleteCallback(hcan);
- #endif /* USE_HAL_CAN_REGISTER_CALLBACKS */
- }
- else
- {
- if ((tsrflags & CAN_TSR_ALST2) != 0U)
- {
- /* Update error code */
- errorcode |= HAL_CAN_ERROR_TX_ALST2;
- }
- else if ((tsrflags & CAN_TSR_TERR2) != 0U)
- {
- /* Update error code */
- errorcode |= HAL_CAN_ERROR_TX_TERR2;
- }
- else
- {
- /* Transmission Mailbox 2 abort callback */
- #if USE_HAL_CAN_REGISTER_CALLBACKS == 1
- /* Call registered callback*/
- hcan->TxMailbox2AbortCallback(hcan);
- #else
- /* Call weak (surcharged) callback */
- HAL_CAN_TxMailbox2AbortCallback(hcan);
- #endif /* USE_HAL_CAN_REGISTER_CALLBACKS */
- }
- }
- }
- }
- /* Receive FIFO 0 overrun interrupt management *****************************/
- if ((interrupts & CAN_IT_RX_FIFO0_OVERRUN) != 0U)
- {
- if ((rf0rflags & CAN_RF0R_FOVR0) != 0U)
- {
- /* Set CAN error code to Rx Fifo 0 overrun error */
- errorcode |= HAL_CAN_ERROR_RX_FOV0;
- /* Clear FIFO0 Overrun Flag */
- __HAL_CAN_CLEAR_FLAG(hcan, CAN_FLAG_FOV0);
- }
- }
- /* Receive FIFO 0 full interrupt management ********************************/
- if ((interrupts & CAN_IT_RX_FIFO0_FULL) != 0U)
- {
- if ((rf0rflags & CAN_RF0R_FULL0) != 0U)
- {
- /* Clear FIFO 0 full Flag */
- __HAL_CAN_CLEAR_FLAG(hcan, CAN_FLAG_FF0);
- /* Receive FIFO 0 full Callback */
- #if USE_HAL_CAN_REGISTER_CALLBACKS == 1
- /* Call registered callback*/
- hcan->RxFifo0FullCallback(hcan);
- #else
- /* Call weak (surcharged) callback */
- HAL_CAN_RxFifo0FullCallback(hcan);
- #endif /* USE_HAL_CAN_REGISTER_CALLBACKS */
- }
- }
- /* Receive FIFO 0 message pending interrupt management *********************/
- if ((interrupts & CAN_IT_RX_FIFO0_MSG_PENDING) != 0U)
- {
- /* Check if message is still pending */
- if ((hcan->Instance->RF0R & CAN_RF0R_FMP0) != 0U)
- {
- /* Receive FIFO 0 mesage pending Callback */
- #if USE_HAL_CAN_REGISTER_CALLBACKS == 1
- /* Call registered callback*/
- hcan->RxFifo0MsgPendingCallback(hcan);
- #else
- /* Call weak (surcharged) callback */
- HAL_CAN_RxFifo0MsgPendingCallback(hcan);
- #endif /* USE_HAL_CAN_REGISTER_CALLBACKS */
- }
- }
- /* Error interrupts management *********************************************/
- if ((interrupts & CAN_IT_ERROR) != 0U)
- {
- if ((msrflags & CAN_MSR_ERRI) != 0U)
- {
- /* Check Error Warning Flag */
- if (((interrupts & CAN_IT_ERROR_WARNING) != 0U) &&
- ((esrflags & CAN_ESR_EWGF) != 0U))
- {
- /* Set CAN error code to Error Warning */
- errorcode |= HAL_CAN_ERROR_EWG;
- /* No need for clear of Error Warning Flag as read-only */
- }
- /* Check Error Passive Flag */
- if (((interrupts & CAN_IT_ERROR_PASSIVE) != 0U) &&
- ((esrflags & CAN_ESR_EPVF) != 0U))
- {
- /* Set CAN error code to Error Passive */
- errorcode |= HAL_CAN_ERROR_EPV;
- /* No need for clear of Error Passive Flag as read-only */
- }
- /* Check Bus-off Flag */
- if (((interrupts & CAN_IT_BUSOFF) != 0U) &&
- ((esrflags & CAN_ESR_BOFF) != 0U))
- {
- /* Set CAN error code to Bus-Off */
- errorcode |= HAL_CAN_ERROR_BOF;
- /* No need for clear of Error Bus-Off as read-only */
- }
- /* Check Last Error Code Flag */
- if (((interrupts & CAN_IT_LAST_ERROR_CODE) != 0U) &&
- ((esrflags & CAN_ESR_LEC) != 0U))
- {
- switch (esrflags & CAN_ESR_LEC)
- {
- case (CAN_ESR_LEC_0):
- /* Set CAN error code to Stuff error */
- errorcode |= HAL_CAN_ERROR_STF;
- break;
- case (CAN_ESR_LEC_1):
- /* Set CAN error code to Form error */
- errorcode |= HAL_CAN_ERROR_FOR;
- break;
- case (CAN_ESR_LEC_1 | CAN_ESR_LEC_0):
- /* Set CAN error code to Acknowledgement error */
- errorcode |= HAL_CAN_ERROR_ACK;
- break;
- case (CAN_ESR_LEC_2):
- /* Set CAN error code to Bit recessive error */
- errorcode |= HAL_CAN_ERROR_BR;
- break;
- case (CAN_ESR_LEC_2 | CAN_ESR_LEC_0):
- /* Set CAN error code to Bit Dominant error */
- errorcode |= HAL_CAN_ERROR_BD;
- break;
- case (CAN_ESR_LEC_2 | CAN_ESR_LEC_1):
- /* Set CAN error code to CRC error */
- errorcode |= HAL_CAN_ERROR_CRC;
- break;
- default:
- break;
- }
- /* Clear Last error code Flag */
- CLEAR_BIT(hcan->Instance->ESR, CAN_ESR_LEC);
- }
- }
- /* Clear ERRI Flag */
- __HAL_CAN_CLEAR_FLAG(hcan, CAN_FLAG_ERRI);
- }
- /* Call the Error call Back in case of Errors */
- if (errorcode != HAL_CAN_ERROR_NONE)
- {
- /* Update error code in handle */
- hcan->ErrorCode |= errorcode;
- /* Call Error callback function */
- #if USE_HAL_CAN_REGISTER_CALLBACKS == 1
- /* Call registered callback*/
- hcan->ErrorCallback(hcan);
- #else
- /* Call weak (surcharged) callback */
- HAL_CAN_ErrorCallback(hcan);
- #endif /* USE_HAL_CAN_REGISTER_CALLBACKS */
- }
- }
- /***************************** 内部函数 End********************************************/
- /************************ (C) COPYRIGHT DTWT *****END OF FILE****/
复制代码
|
|