can

STM32F427IIHx使用CubeMxpe配置CAN,CAN接收中断突然挂了

STM32F427IIHx使用CubeMxpe配置后,滤波器之类的该配置的也配置了,一开始接收和发送中断都可以进去的,但是运行1分钟左右,CAN的接收中断直接挂了进不去了,但是发送中断还是正常的,并且显示ErrorCode=0x00200000,然后我发现这个错误码是因为CAN的9个发送邮箱有时候都是满的。只要这个错误码一出来了,接收中断就挂了,但是我给代码加上FREERTOS后,接收中断就不会挂了,但是CAN还是很奇怪,还是有发送错误码出来,这个该怎么解决?

正常情况下:

image.png

接收中断挂了之后:

image.png

我的配置代码是这样的:

void MX_CAN1_Init(void)

{


  hcan1.Instance = CAN1;

  hcan1.Init.Prescaler = 9;

  hcan1.Init.Mode = CAN_MODE_NORMAL;

  hcan1.Init.SyncJumpWidth = CAN_SJW_1TQ;

  hcan1.Init.TimeSeg1 = CAN_BS1_3TQ;

  hcan1.Init.TimeSeg2 = CAN_BS2_1TQ;//波特率是1Mbps,外设时钟是45MHz

  hcan1.Init.TimeTriggeredMode = DISABLE;

  hcan1.Init.AutoBusOff = DISABLE;

  hcan1.Init.AutoWakeUp = DISABLE;

  hcan1.Init.AutoRetransmission = ENABLE;

  hcan1.Init.ReceiveFifoLocked = DISABLE;

  hcan1.Init.TransmitFifoPriority = ENABLE;

  if (HAL_CAN_Init(&hcan1) != HAL_OK)

  {

    Error_Handler();

  }


}


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_GPIOD_CLK_ENABLE();

    /**CAN1 GPIO Configuration    

    PD0     ------> CAN1_RX

    PD1     ------> CAN1_TX 

    */

    GPIO_InitStruct.Pin = GPIO_PIN_0|GPIO_PIN_1;

    GPIO_InitStruct.Mode = GPIO_MODE_AF_PP;

    GPIO_InitStruct.Pull = GPIO_PULLUP;

    GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_VERY_HIGH;

    GPIO_InitStruct.Alternate = GPIO_AF9_CAN1;

    HAL_GPIO_Init(GPIOD, &GPIO_InitStruct);


    /* CAN1 interrupt Init */

    HAL_NVIC_SetPriority(CAN1_TX_IRQn, 2, 1);

    HAL_NVIC_EnableIRQ(CAN1_TX_IRQn);

    HAL_NVIC_SetPriority(CAN1_RX0_IRQn, 1, 1);

    HAL_NVIC_EnableIRQ(CAN1_RX0_IRQn);

  /* USER CODE BEGIN CAN1_MspInit 1 */


  /* USER CODE END CAN1_MspInit 1 */

  }

}

void CAN1_Filter_Init()

{

fcan1.FilterMode = CAN_FILTERMODE_IDMASK;

fcan1.FilterScale = CAN_FILTERSCALE_32BIT;

fcan1.FilterIdHigh = 0x0000;

fcan1.FilterIdLow = 0x0000;

fcan1.FilterMaskIdHigh = 0x0000;

fcan1.FilterMaskIdLow = 0x0000;

fcan1.FilterBank = 0;

fcan1.FilterFIFOAssignment = CAN_RX_FIFO0;

fcan1.FilterActivation = ENABLE;

HAL_CAN_ConfigFilter(&hcan1,&fcan1);

HAL_CAN_Start(&hcan1);

HAL_CAN_ActivateNotification(&hcan1, CAN_IT_RX_FIFO0_MSG_PENDING | CAN_IT_TX_MAILBOX_EMPTY);

}


已邀请:

fumoumou

赞同来自:

兄弟你好,我的现在无法发送数据,调试的时候一直在HAL_GetTick();循环,请问有什么解决办法吗?

要回复问题请先登录注册