Skip to main content
Visitor II
May 5, 2024
Question

STM32F407 - CAN2 and TIM7 problem

  • May 5, 2024
  • 1 reply
  • 1046 views

Hello,

In my STM32F407G-DISCO based project I use CAN1 and CAN2 to communicate with two encoders. I read the position of the encoders every 4 ms. I use TIM6 to generate an interrupt every 4 ms. It all works beautifully - it sends the position of each of them via UART2. However, when I want to run timer 7 which will send the position of each of them via UART3 every 1 second, suddenly the program does not read the position via CAN2. It does not enter the HAL_CAN_RxFifo1MsgPendingCallback function at all.

 

Can anyone help me and tell me where I'm making a mistake?
Below are the timer and CAN functions

 

 

 

static void TIMER6_Init(void)
{
htimer6.Instance = TIM6;
htimer6.Init.Prescaler = 10;
htimer6.Init.Period = 56000-1;
if( HAL_TIM_Base_Init(&htimer6) != HAL_OK )
{
Error_Handler();
}
}

static void TIMER7_Init(void)
{
htimer7.Instance = TIM7;
htimer7.Init.Prescaler = 4999;
htimer7.Init.Period = 20000-1;
if( HAL_TIM_Base_Init(&htimer7) != HAL_OK )
{
Error_Handler();
}
}

void HAL_TIM_Base_MspInit(TIM_HandleTypeDef *htimer)
{
//1. enable the clock for the TIM6 and TIM7 peripheral
__HAL_RCC_TIM6_CLK_ENABLE();
__HAL_RCC_TIM7_CLK_ENABLE();

//2. Enable the IRQ of TIM6 and TIM7
HAL_NVIC_EnableIRQ(TIM6_DAC_IRQn);
HAL_NVIC_EnableIRQ(TIM7_IRQn);

//3. setup the priority for TIM6_DAC_IRQn and TIM7
HAL_NVIC_SetPriority(TIM6_DAC_IRQn,15,0);
HAL_NVIC_SetPriority(TIM7_IRQn,15,0);
}


void HAL_CAN_MspInit(CAN_HandleTypeDef *hcan)
{
GPIO_InitTypeDef GPIO_InitStruct;
GPIO_InitTypeDef GPIO_InitStruct2;
__HAL_RCC_CAN1_CLK_ENABLE();
__HAL_RCC_CAN2_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_NOPULL;
GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_VERY_HIGH;
GPIO_InitStruct.Alternate = GPIO_AF9_CAN1;
HAL_GPIO_Init(GPIOD, &GPIO_InitStruct);

//Enable IRQs for CAN
//Setting priority
HAL_NVIC_SetPriority(CAN1_TX_IRQn,15,1);
HAL_NVIC_SetPriority(CAN1_RX0_IRQn,15,1);
HAL_NVIC_SetPriority(CAN1_RX1_IRQn,15,1);
HAL_NVIC_SetPriority(CAN1_SCE_IRQn,15,1);

//Enabling IRQ
HAL_NVIC_EnableIRQ(CAN1_TX_IRQn);
HAL_NVIC_EnableIRQ(CAN1_RX0_IRQn);
HAL_NVIC_EnableIRQ(CAN1_RX1_IRQn);
HAL_NVIC_EnableIRQ(CAN1_SCE_IRQn);

/**CAN2 GPIO Configuration
PB12 ------> CAN2_RX
PB13 ------> CAN2_TX
*/
GPIO_InitStruct2.Pin = GPIO_PIN_12|GPIO_PIN_13;
GPIO_InitStruct2.Mode = GPIO_MODE_AF_PP;
GPIO_InitStruct2.Pull = GPIO_NOPULL;
GPIO_InitStruct2.Speed = GPIO_SPEED_FREQ_VERY_HIGH;
GPIO_InitStruct2.Alternate = GPIO_AF9_CAN2;
HAL_GPIO_Init(GPIOB, &GPIO_InitStruct2);

//Enable IRQs for CAN
//Setting priority
HAL_NVIC_SetPriority(CAN2_TX_IRQn,15,0);
HAL_NVIC_SetPriority(CAN2_RX0_IRQn,15,0);
HAL_NVIC_SetPriority(CAN2_RX1_IRQn,15,0);
HAL_NVIC_SetPriority(CAN2_SCE_IRQn,15,0);

//Enabling IRQ
HAL_NVIC_EnableIRQ(CAN2_TX_IRQn);
HAL_NVIC_EnableIRQ(CAN2_RX0_IRQn);
HAL_NVIC_EnableIRQ(CAN2_RX1_IRQn);
HAL_NVIC_EnableIRQ(CAN2_SCE_IRQn);
}

 

 

    This topic has been closed for replies.

    1 reply

    ST Employee
    May 6, 2024

    Hello @LukaszN, welcome to ST Community, 

    Maybe review NVIC priorities, you have the same priority level (15) for both TIM6 and TIM 7 interrupts, and for the CAN interrupts. It's possible that the NVIC configuration could be causing the CAN2 interrupt to be masked or preempted by the timer interrupts.

    Try using different priority levels to ensure that the CAN interrupts have higher priority.