cancel
Showing results for 
Search instead for 
Did you mean: 

STM32F407VET CAN BUS interrupt does not trigger

JBond.1
Senior

Hi, I am trying to communicate on CAN BUS, I have previously succeeded in doing so with STM32F103C8 and STM32F091RC, but I cant make it work on STM32F407VET It does not trigger its interrupt on message receive. What could be the difference on this MCU? Do I need to enable something different?

MCU clock 168Mhz, APB1 42Mhz, CAN BUS speed is 100000bit/s, enabled "CAN1 RX0 interrupts" in NVIC, code:

hcan1.Instance = CAN1;
hcan1.Init.Prescaler = 28;
hcan1.Init.Mode = CAN_MODE_NORMAL;
hcan1.Init.SyncJumpWidth = CAN_SJW_1TQ;
hcan1.Init.TimeSeg1 = CAN_BS1_12TQ;
hcan1.Init.TimeSeg2 = CAN_BS2_2TQ;
hcan1.Init.TimeTriggeredMode = DISABLE;
hcan1.Init.AutoBusOff = ENABLE;
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();
}
 
CAN_FilterTypeDef  sFilterConfig;
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)
{
	Error_Handler();
}
 
if (HAL_CAN_Start(hcan1) != HAL_OK)
{
	Error_Handler();
}
 
if (HAL_CAN_ActivateNotification(hcan1, CAN_IT_RX_FIFO0_MSG_PENDING) != HAL_OK)
{
	Error_Handler();
}

I am expecting to get callback on:

void HAL_CAN_RxFifo0MsgPendingCallback(CAN_HandleTypeDef *hcan)
{
}

But its never executed. I tried to set break point on, but it does not brake there ether:

/**
  * @brief This function handles CAN1 RX0 interrupts.
  */
void CAN1_RX0_IRQHandler(void)
{
  /* USER CODE BEGIN CAN1_RX0_IRQn 0 */
 
  /* USER CODE END CAN1_RX0_IRQn 0 */
  HAL_CAN_IRQHandler(&hcan1);
  /* USER CODE BEGIN CAN1_RX0_IRQn 1 */
 
  /* USER CODE END CAN1_RX0_IRQn 1 */
}

 What could be the problem?

I dont know what to trouble shoot next.

1 ACCEPTED SOLUTION

Accepted Solutions

Hi,

Probably You have problem with settings of SystemClock.

You have on the board STM32F4VE 8 MHz external resonator?

If yes, please change value from RCC_OscInitStruct.PLL.PLLM = 25 to RCC_OscInitStruct.PLL.PLLM = 8,

Best Regards,

Slawek

View solution in original post

14 REPLIES 14
SKacp.1
Senior II

Hello,

Do You have correctly settings hardware (pins) and interrupt for CAN1?

Tried swithich RX TX, different pins allso same, tried sending can message, on sending I noticed that other devices wakeup but do not get a message, sending corrupted message? does it use correct APB1 mhz?

SKacp.1
Senior II

Hello,

Please send me Your code with hardware initialization of CAN1.

My code:

/**
* @brief CAN MSP Initialization
* This function configures the hardware resources used in this example
* @param hcan: CAN handle pointer
* @retval None
*/
void HAL_CAN_MspInit(CAN_HandleTypeDef* hcan)
{
  GPIO_InitTypeDef GPIO_InitStruct = {0};
  if(hcan->Instance==CAN1)
  {
  /* USER CODE BEGIN CAN1_MspInit 0 */
 
  /* USER CODE END CAN1_MspInit 0 */
    /* Peripheral clock enable */
    __HAL_RCC_CAN1_CLK_ENABLE();
 
    __HAL_RCC_GPIOD_CLK_ENABLE();
    /**CAN1 GPIO Configuration
    PD0     ------> CAN1_RX
    PD1     ------> CAN1_TX
    */
    GPIO_InitStruct.Pin = CAN1_RX_Pin|CAN1_TX_Pin;
    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_CAN1;
    HAL_GPIO_Init(CAN1_GPIO_Port, &GPIO_InitStruct);
 
    /* CAN1 interrupt Init */
    HAL_NVIC_SetPriority(CAN1_RX0_IRQn, 1, 0);
    HAL_NVIC_EnableIRQ(CAN1_RX0_IRQn);
  /* USER CODE BEGIN CAN1_MspInit 1 */
 
  /* USER CODE END CAN1_MspInit 1 */
  }
 
}

> Tried swithich RX TX, different pins allso same, tried sending can message, on sending I noticed that other devices wakeup but do not get a message, sending corrupted message?

Any (physical) receiver will send an acknowledge bit if a frame is received properly.

It seems this does not happen on your bus, so there is probably something wrong on bus level.

Did you try lower bitrates ?

And did you try loopback mode of your F407 node, which would validate your pin setup ?

You could check if the bus error counters on the F407 and other nodes go up, and use a scope to view the signal.

There should not be anything wrong on that bus as STM32F103 and STM32F091RC are working perfectly fine on that bus and get interrupts and are working on same bit rate only STM32F407 is misbehaving and cant figure-out why.

In loopback mode still not interrupt trigger but other modules seem to be communicating without losing any messages now.

Is the only way left is logical analyzer?

I have exact same initialization which was generated. Sent you PM with code. Attached it here too.

Hi,

Please see my code for initialization CAN1

void MX_CAN1_Init(void)
{
 
	CAN_FilterTypeDef  sFilterConfig;
 
	hcan1.Instance = CAN1;
	hcan1.Init.Prescaler = 16;
	hcan1.Init.Mode = CAN_MODE_NORMAL;
	hcan1.Init.SyncJumpWidth = CAN_SJW_1TQ;
	hcan1.Init.TimeSeg1 = CAN_BS1_13TQ;
	hcan1.Init.TimeSeg2 = CAN_BS2_7TQ;
	hcan1.Init.TimeTriggeredMode = DISABLE;
	hcan1.Init.AutoBusOff = DISABLE;
	hcan1.Init.AutoWakeUp = DISABLE;
	hcan1.Init.AutoRetransmission = ENABLE;
	hcan1.Init.ReceiveFifoLocked = DISABLE;
	hcan1.Init.TransmitFifoPriority = DISABLE;
	if (HAL_CAN_Init(&hcan1) != HAL_OK)
	{
		Error_Handler();
	}
 
  /* 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();
	}
 
	/* Activate CAN RX notification */
	if (HAL_CAN_ActivateNotification(&hcan1, CAN_IT_RX_FIFO0_MSG_PENDING) != HAL_OK)
	{
	/* Notification Error */
		Error_Handler();
	}
 
 
}

Please change in Your code:

hcan1.Init.AutoBusOff = DISABLE;

hcan1.Init.AutoRetransmission = ENABLE;

Best Regards,

Slawek

Does not seem to make any difference, interrupt does not trigger on message pending.

STM32F091RC working ok on same can bus...

0693W000008wZhwQAE.jpg0693W000008wZh9QAE.jpg

Hi,

Do You use external transceiver CAN?

You connect directly PIN CAN1 from STM32F407 to STM32F091?

Best Regards,

Slawek