各位大佬,今天开始用HAL库写CAN通讯程序,就回环模式都始终不能发送数据,一调用HAL_CAN_AddTxMessage函数就进入HardFault_Handler,求各位大佬赐教,不胜感激!
初始化是由CubMX 5.1 (默认的固件库是STM32Cube_FW_F3_V1.10.0)自动生成的代码,在keil里边自动升级的固件库是Keil.STM32F3xx_DFP.2.1.0Cub.pack;并在CAN的初始化代码里边加了过滤器初始化代码:
void MX_CAN_Init(void)
{
HCAN1.Instance = CAN;
HCAN1.Init.Prescaler = 64;
HCAN1.Init.Mode = CAN_MODE_LOOPBACK ;//CAN_MODE_NORMAL;
HCAN1.Init.SyncJumpWidth = CAN_SJW_1TQ;
HCAN1.Init.TimeSeg1 = CAN_BS1_4TQ;
HCAN1.Init.TimeSeg2 = CAN_BS2_4TQ;
HCAN1.Init.TimeTriggeredMode = DISABLE;
HCAN1.Init.AutoBusOff = DISABLE;
HCAN1.Init.AutoWakeUp = DISABLE;
HCAN1.Init.AutoRetransmission = DISABLE;
HCAN1.Init.ReceiveFifoLocked = ENABLE;
HCAN1.Init.TransmitFifoPriority = ENABLE;
if (HAL_CAN_Init(&HCAN1) != HAL_OK)
{
Error_Handler();
}
/* USER CODE BEGIN 1 */
//Configure the CAN Filter
sFilterConfig.FilterBank = 0;
sFilterConfig.FilterMode = CAN_FILTERMODE_IDMASK;
sFilterConfig.FilterScale = CAN_FILTERSCALE_32BIT;
sFilterConfig.FilterIdHigh = 0x0000;
sFilterConfig.FilterIdLow = 0x0000;
sFilterConfig.FilterMaskIdHigh = 0x0000;
sFilterConfig.FilterMaskIdLow = 0x0000;
sFilterConfig.FilterFIFOAssignment = CAN_RX_FIFO0;
sFilterConfig.FilterActivation = ENABLE;
sFilterConfig.SlaveStartFilterBank = 14;
if (HAL_CAN_ConfigFilter(&HCAN1, &sFilterConfig) != HAL_OK)
{
//Filter configuration Error
Error_Handler();
}
/* Start the CAN peripheral */
if (HAL_CAN_Start(&HCAN1) != HAL_OK)
{
/* Start Error */
Error_Handler();
}
}
在调用库函数
HAL_RetVal=HAL_CAN_AddTxMessage(&HCAN1,&TxHeader,TxData,(uint32_t*)CAN_TX_MAILBOX0);
时进入硬件错误中断。根据单步调试,具体是在执行库函数里边的以下赋值语句出现错误。
hcan->Instance->sTxMailBox[transmitmailbox].TIR = ((pHeader->StdId << CAN_TI0R_STID_Pos) |pHeader->RTR);
在执行以上语句前时,数据如下:
执行过后
|