Skip to main content
Visitor II
April 30, 2019
Question

registered CAN Interrupt callbacks not being called

  • April 30, 2019
  • 4 replies
  • 4384 views

I am using STM32CubeMX and HAL libs to send and receive CAN messages on an STM32F413ZH. stm32f4xx_hal_can.c tells me to #define USE_HAL_CAN_REGISTER_CALLBACKS = 1 which I have done in both main.h and stm32f4xx_hal_conf.h (if I don't put it in the latter, the function HAL_CAN_RegisterCallback() doesn't get built)

In main.c I have:

...

HAL_Init();

SystemClock_Config();

 MX_CAN1_Init();

...

static void MX_CAN1_Init(void)

{

 /* USER CODE BEGIN CAN1_Init 0 */

 /* USER CODE END CAN1_Init 0 */

 /* USER CODE BEGIN CAN1_Init 1 */

 /* USER CODE END CAN1_Init 1 */

 hcan1.Instance = CAN1;

 hcan1.Init.Prescaler = 8;

 hcan1.Init.Mode = CAN_MODE_NORMAL;

 hcan1.Init.SyncJumpWidth = CAN_SJW_2TQ;

 hcan1.Init.TimeSeg1 = CAN_BS1_8TQ;

 hcan1.Init.TimeSeg2 = CAN_BS2_3TQ;

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

 }

 /* USER CODE BEGIN CAN1_Init 2 */

for (filterNo = 0; filterNo < CAN1_FILTER_CNT; filterNo++){

 sFilterConfig.FilterBank = filterNo;

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

 }

}

if (HAL_CAN_Start (&hcan1) != HAL_OK) {

// start error

Error_Handler();

}

// Register callback funcs

HAL_CAN_RegisterCallback(&hcan1, HAL_CAN_TX_MAILBOX0_COMPLETE_CB_ID, HAL_CAN_TxMailbox0CompleteCallback);

HAL_CAN_RegisterCallback(&hcan1, HAL_CAN_RX_FIFO0_MSG_PENDING_CB_ID, HAL_CAN_RxFIFO0MsgPendingCallback);

HAL_CAN_RegisterCallback(&hcan1, HAL_CAN_RX_FIFO0_FULL_CB_ID, HAL_CAN_RxFifo0FullCallback);

  // Activate CAN Interrupts

  HAL_CAN_ActivateNotification(&hcan1, CAN_IT_TX_MAILBOX_EMPTY);

  HAL_CAN_ActivateNotification(&hcan1, CAN_IT_RX_FIFO0_MSG_PENDING);

  HAL_CAN_ActivateNotification(&hcan1, CAN_IT_RX_FIFO0_FULL);

 /* USER CODE END CAN1_Init 2 */

}

============

My HAL_CAN_TxMailbox0CompleteCallback function doesn't get called, the default one in stm32f4xx_hal_can.c does. My function is in main.c and the prototype is in main.h.

    This topic has been closed for replies.

    4 replies

    Visitor II
    April 30, 2019

    Does it send or receive anything - have you checked?

    And have you enabled the interrupts in the CAN NVIC-settings in CubeMX?

    GrantWAuthor
    Visitor II
    April 30, 2019

    Yes, I'm sending successfully, and receiving but the FIFO buffer is overflowing because the default callback function, which does nothing, is being called, not mine. This is because the controller is stuck in RESET state when I try to register my callback (as I mention below)

    GrantWAuthor
    Visitor II
    April 30, 2019

    I've found the cause of the problem: the controller is in HAL_CAN_STATE_RESET mode when I try to register my callbacks. It has to be in _READY mode. What would be causing this? Shouldn't a successful return from HAL_CAN_Start() ensure its in the _READY state?

    Visitor II
    October 15, 2023

    How to get this fixed, like the controller to be in ready state??

    Visitor II
    April 30, 2019

    I think it should. And if you define your own callback functions with the same name as the (empty) default ones, your functions should be used, because the default functions are defined as weak. As if your compiler didn't see your functions. Have you checked that your files are not "excluded from build"?

    GrantWAuthor
    Visitor II
    May 1, 2019

    The reason my controller was in HAL_CAN_STATE_RESET state was a problem with my hardware setup. The final problem was that I was calling HAL_CAN_RegisterCallback() after HAL_CAN_Start(). It must be called after initialization and config, but BEFORE activating the controller. My custom callbacks are now in the game!

    Visitor II
    November 9, 2019

    Hello, I am okay for sending and polling receiving but can not jump into the interrupt, could you have sometimes to check? Thanks!

    GrantWAuthor
    Visitor II
    November 10, 2019
    You need to show me something to check