cancel
Showing results for 
Search instead for 
Did you mean: 

How to properly configure the CAN2 RX Interrupt on STM32 ?

ALuca
Associate II

Hello,

My RX0 Interrupt is not called and I don't understand why.

If someone can help me, because I already tried lot of things.

The problem is I never go in my "CAN2_RX0_IRQHandler()".

I properly configured my CAN timings because the received frames are acknoledged by my microcontroller.

Find below My CAN Configuration :

    //MODULE CAN
    hcan2.Instance = CAN2;
    hcan2.Init.Prescaler = 21;
    hcan2.Init.Mode = CAN_MODE_NORMAL;
    hcan2.Init.SyncJumpWidth = CAN_SJW_1TQ;
    hcan2.Init.TimeSeg1 = CAN_BS1_6TQ;
    hcan2.Init.TimeSeg2 = CAN_BS2_1TQ;
    hcan2.Init.TimeTriggeredMode = DISABLE;
    hcan2.Init.AutoBusOff = DISABLE;
    hcan2.Init.AutoWakeUp = DISABLE;
    hcan2.Init.AutoRetransmission = DISABLE;
    hcan2.Init.ReceiveFifoLocked = DISABLE;
    hcan2.Init.TransmitFifoPriority = ENABLE;
    if (HAL_CAN_Init(&hcan2) != HAL_OK)
    {
            //_Error_Handler(__FILE__, __LINE__);
    }
 
    //FILTRES CAN
    CAN_FilterTypeDef  sFilterConfig;
 
    sFilterConfig.FilterBank = 0;
    sFilterConfig.FilterMode = CAN_FILTERMODE_IDMASK;
    sFilterConfig.FilterScale = CAN_FILTERSCALE_32BIT;
    sFilterConfig.FilterIdHigh = 0;//0x350 << 5;
    sFilterConfig.FilterIdLow = 0;
    sFilterConfig.FilterMaskIdHigh = 0;//0xFFF << 5;
    sFilterConfig.FilterMaskIdLow = 0;
    sFilterConfig.FilterFIFOAssignment = CAN_RX_FIFO0;
    sFilterConfig.FilterActivation = DISABLE;
    sFilterConfig.SlaveStartFilterBank = 14;
 
    HAL_CAN_ConfigFilter(&hcan2, &sFilterConfig);
 
    //Config CAN
    HAL_CAN_Start(&hcan2);
    HAL_CAN_ActivateNotification(&hcan2,  CAN_IER_FMPIE0|CAN_IER_FFIE0|CAN_IER_FOVIE021);

My CAN MSP config :

void HAL_CAN_MspInit(CAN_HandleTypeDef* hcan)
{
 
  GPIO_InitTypeDef GPIO_InitStruct;
  if(hcan->Instance==CAN2)
  {
  /* USER CODE BEGIN CAN2_MspInit 0 */
 
  /* USER CODE END CAN2_MspInit 0 */
    /* Peripheral clock enable */
    __HAL_RCC_CAN2_CLK_ENABLE();
    __HAL_RCC_CAN1_CLK_ENABLE();
  
    /**CAN2 GPIO Configuration    
    PB12     ------> CAN2_RX
    PB13     ------> CAN2_TX 
    */
    GPIO_InitStruct.Pin = GPIO_PIN_12|GPIO_PIN_13;
    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_CAN2;
    HAL_GPIO_Init(GPIOB, &GPIO_InitStruct);
 
    
    /* CAN2 interrupt Init */
    HAL_NVIC_SetPriority(CAN2_RX0_IRQn, 2, 3);
    HAL_NVIC_EnableIRQ(CAN2_RX0_IRQn);
  /* USER CODE BEGIN CAN2_MspInit 1 */
 
  /* USER CODE END CAN2_MspInit 1 */
  }
 
}

The IRQ function where I never go (placed in stm32xxxx_it.c) :

void CAN2_RX0_IRQHandler(void)
{
  fnRxInterrupt();
}

Thank you,

Alexis

0 REPLIES 0