Skip to main content
Explorer II
August 14, 2025
Solved

interrupt on can bus with STM32f446

  • August 14, 2025
  • 1 reply
  • 482 views

Hi everybody,

I'm trying to set up a communication with a can bus.

I'm using a STM32F446. I use PB12 & PB13 (CAN2). I can see RX frames on PB12.

I'd like to read frames that I received by using interrupts.

I've found this example : https://github.com/STMicroelectronics/STM32CubeL4/blob/master/Projects/STM32L476G-EVAL/Examples/CAN/CAN_Networking/Src/main.c

But, I have put a breakpoint on the interrupt, but it never goes in.

static void MX_CAN2_Init(void)
{

 /* USER CODE BEGIN CAN2_Init 0 */
	CAN_FilterTypeDef sFilterConfig2;
 /* USER CODE END CAN2_Init 0 */

 /* USER CODE BEGIN CAN2_Init 1 */

 /* USER CODE END CAN2_Init 1 */
 hcan2.Instance = CAN2;
 hcan2.Init.Prescaler = 12;
 hcan2.Init.Mode = CAN_MODE_NORMAL;
 hcan2.Init.SyncJumpWidth = CAN_SJW_1TQ;
 hcan2.Init.TimeSeg1 = CAN_BS1_8TQ;
 hcan2.Init.TimeSeg2 = CAN_BS2_6TQ;
 hcan2.Init.TimeTriggeredMode = DISABLE;
 hcan2.Init.AutoBusOff = DISABLE;
 hcan2.Init.AutoWakeUp = DISABLE;
 hcan2.Init.AutoRetransmission = DISABLE;
 hcan2.Init.ReceiveFifoLocked = DISABLE;
 hcan2.Init.TransmitFifoPriority = DISABLE;
 if (HAL_CAN_Init(&hcan2) != HAL_OK)
 {
 Error_Handler();
 }
 /* USER CODE BEGIN CAN2_Init 2 */
 /*##-2- Configure the CAN Filter ###########################################*/
 sFilterConfig2.FilterBank = 0;
 sFilterConfig2.FilterMode = CAN_FILTERMODE_IDMASK;
 sFilterConfig2.FilterScale = CAN_FILTERSCALE_32BIT;
 sFilterConfig2.FilterIdHigh = 0x0000;
 sFilterConfig2.FilterIdLow = 0x0000;
 sFilterConfig2.FilterMaskIdHigh = 0x0000;
 sFilterConfig2.FilterMaskIdLow = 0x0000;
 sFilterConfig2.FilterFIFOAssignment = CAN_RX_FIFO0;
 sFilterConfig2.FilterActivation = ENABLE;
 sFilterConfig2.SlaveStartFilterBank = 14;

 if (HAL_CAN_ConfigFilter(&hcan2, &sFilterConfig2) != HAL_OK)
 {
 /* Filter configuration Error */
 Error_Handler();
 }

 /*##-3- Start the CAN peripheral ###########################################*/
 if (HAL_CAN_Start(&hcan2) != HAL_OK)
 {
 /* Start Error */
 Error_Handler();
 }

 /*##-4- Activate CAN RX notification #######################################*/
 if (HAL_CAN_ActivateNotification(&hcan2, CAN_IT_RX_FIFO0_MSG_PENDING) != HAL_OK)
 {
 /* Notification Error */
 Error_Handler();
 }
 /* USER CODE END CAN2_Init 2 */

 

/**
 * @brief Rx Fifo 0 message pending callback
 * @PAram hcan: pointer to a CAN_HandleTypeDef structure that contains
 * the configuration information for the specified CAN.
 * @retval None
 */
void HAL_CAN_RxFifo0MsgPendingCallback(CAN_HandleTypeDef *hcan2)
{
 /* Get RX message */
 if (HAL_CAN_GetRxMessage(hcan2, CAN_RX_FIFO0, &RxHeader, RxData) != HAL_OK)
 {
 /* Reception Error */
 Error_Handler();
 }
}
/**
 * @brief This function handles CAN2 RX0 interrupt.
 */
void CAN2_RX0_IRQHandler(void)
{
 /* USER CODE BEGIN CAN2_RX0_IRQn 0 */

 /* USER CODE END CAN2_RX0_IRQn 0 */
 HAL_CAN_IRQHandler(&hcan2);
 /* USER CODE BEGIN CAN2_RX0_IRQn 1 */

 /* USER CODE END CAN2_RX0_IRQn 1 */
}

 

If you have any suggestions, I'll be very happy.

Thank you.

    This topic has been closed for replies.
    Best answer by fab04

    Thank you for your reply.

    I've checked some points on my board.

    The baudrate of my can bus is 250kbits (I've well measured this speed on the differential pairs), but when I do the measure on my transceiver (MCP2542), I haven't the right baudrate. After checking, I forgot to put STBY pin @ 0V. So now, this point is OK.

    2nd thing, I've modify the filterbank (I need to spend time to well understand how it works).

    And now, it works well.

    Thank you.

     

     

    1 reply

    Graduate II
    August 14, 2025

    CAN2 would typically want to start with FilterBank 14 not zero.

    If bit rate wrong you won't receive anything. Check if you need an error call back too.

    fab04AuthorAnswer
    Explorer II
    August 14, 2025

    Thank you for your reply.

    I've checked some points on my board.

    The baudrate of my can bus is 250kbits (I've well measured this speed on the differential pairs), but when I do the measure on my transceiver (MCP2542), I haven't the right baudrate. After checking, I forgot to put STBY pin @ 0V. So now, this point is OK.

    2nd thing, I've modify the filterbank (I need to spend time to well understand how it works).

    And now, it works well.

    Thank you.

     

     

    Technical Moderator
    August 18, 2025

    Hello,


    @fab04 wrote:

    2nd thing, I've modify the filterbank (I need to spend time to well understand how it works).


    You can refer to this article: STM32 in dual CAN configuration: bxCAN Filter bank explanation and relation with CAN2 Start Bank parameter