Skip to main content
Visitor II
August 20, 2025
Question

stm32l452 timer interrupt and can tle9255w

  • August 20, 2025
  • 3 replies
  • 747 views

Hello, 

I am starting a timer interrupt to send data over can data at 50 ms below is the configuration for the same and callback function when pulse is complete

volatile uint8_t send_ch0_flag = 0;

volatile uint8_t send_chx_flag = 0;

volatile uint8_t send_relay_flag = 0;

void MX_TIM2_Init(void)

{

TIM_ClockConfigTypeDef sClockSourceConfig = {0};

TIM_MasterConfigTypeDef sMasterConfig = {0};



htim2.Instance = TIM2; 

htim2.Init.Prescaler = 3999; // 80 MHz / (3999+1) = 20 kHz 

htim2.Init.CounterMode = TIM_COUNTERMODE_UP; 

htim2.Init.Period = 999; // (999+1) / 20kHz = 0.05 s = 50 ms 

htim2.Init.ClockDivision = TIM_CLOCKDIVISION_DIV1; 

htim2.Init.AutoReloadPreload = TIM_AUTORELOAD_PRELOAD_DISABLE; 

__HAL_RCC_TIM2_CLK_ENABLE();

if (HAL_TIM_Base_Init(&htim2) != HAL_OK) 

{ 

 Error_Handler(); 

} 



sClockSourceConfig.ClockSource = TIM_CLOCKSOURCE_INTERNAL; 

if (HAL_TIM_ConfigClockSource(&htim2, &sClockSourceConfig) != HAL_OK) 

{ 

 Error_Handler(); 

} 



sMasterConfig.MasterOutputTrigger = TIM_TRGO_RESET; 

sMasterConfig.MasterSlaveMode = TIM_MASTERSLAVEMODE_DISABLE; 

if (HAL_TIMEx_MasterConfigSynchronization(&htim2, &sMasterConfig) != HAL_OK) 

{ 

 Error_Handler(); 

} 



HAL_NVIC_SetPriority(TIM2_IRQn, 1, 0); 

HAL_NVIC_EnableIRQ(TIM2_IRQn); 



HAL_TIM_Base_Start_IT(&htim2);



}

void HAL_TIM_PeriodElapsedCallback(TIM_HandleTypeDef *htim)

{

 if (htim->Instance == TIM2) 

 {

 ms_counter += 50;



 // Channel 0 every 50 ms

 send_ch0_flag = 1;



 // Channels 1–8 every 100 ms

 if (ms_counter % 100 == 0) {

 send_chx_flag = 1;

 }



 // Relay every 1000 ms

 if (ms_counter % 1000 == 0) {

 send_relay_flag = 1;

 ms_counter = 0; // reset counter

 }

 }

}

void process_CAN_messages(void) //called inside while loop

{

 if (send_ch0_flag) {

 send_ch0_flag = 0;

 CAN_SendChannel(0); // your wrapper function

 }



 if (send_chx_flag) {

 send_chx_flag = 0;

 for (int ch = 1; ch < 9; ch++) {

 CAN_SendChannel(ch);

 }

 }



 if (send_relay_flag) {

 send_relay_flag = 0;

 sendRelayMessages();

 }

}

Please let me know what configuration I am doing wrong or is it a code issue.

But when I send a command over can to the mcu I am not able to recieve the command but the same is working when I am starting the timer at 100ms. Below is the can reception logic. The CAN transrx ic i am using is TLE9255W.

Inside while I am monitoring my can transrx interrupt below is the interrupt

void HAL_CAN_RxFifo0MsgPendingCallback(CAN_HandleTypeDef *hcan){

HAL_CAN_GetRxMessage(&hcan1, CAN_RX_FIFO0, &RxHeader, RxData);

if(RxHeader.DLC == 8){

data_received = 1; //monitored inside while loop

}

}

Thanks

 

    This topic has been closed for replies.

    3 replies

    Technical Moderator
    August 21, 2025

    Hello,

    I'm not sure I understood the issue. Please elaborate more that statement: 


    @yashg25 wrote:

    But when I send a command over can to the mcu I am not able to recieve the command but the same is working when I am starting the timer at 100ms. Below is the can reception logic. The CAN transrx ic i am using is TLE9255W.


     

    yashg25Author
    Visitor II
    August 21, 2025

    Hello @mƎALLEm ,

    What I meant by that statement is that :
    Statement 1:

    When I set the timer period to generate interrupt at every 50ms (for sending data from stm32l452re to CAN device which is working but on my CAN device I see the reception time around 90ms even for a 50ms interrupt),
    I am not able to send the can command from my CAN device to stm32l452re CAN, the command is not executing.

     

    Statement 2:

    But when I set the timer period to generate interrupt at every 100ms(for sending data from stm32l452re to CAN device which is working and on my CAN device I see the reception time around 100ms),

    I am able to send the can commands from my CAN device to stm32l452re CAN, the command that I am sending are executing.


    Thanks,
    Yash

    yashg25Author
    Visitor II
    August 22, 2025

    @mƎALLEm , Waiting for your reply.

    ST Employee
    August 25, 2025

    Hi @yashg25

    Since CAN communication fails when the timer interrupt is set to 50 ms but works at 100 ms, this is likely due to interrupt timing conflicts or priority mismanagement; the timer interrupt can be preempting the CAN interrupt, can you share interrupt priorities? 

    yashg25Author
    Visitor II
    August 26, 2025

    Hello @Sarra.S,

    Below are the priorities :

    1. Timer interrupt

    HAL_NVIC_SetPriority(TIM2_IRQn, 1, 0);

    HAL_NVIC_EnableIRQ(TIM2_IRQn);

    2. CAN interrupt

    HAL_NVIC_SetPriority(CAN1_RX0_IRQn, 0, 0);

    HAL_NVIC_EnableIRQ(CAN1_RX0_IRQn);

    Regards,

    Yash Gupta

     

    Graduate II
    August 26, 2025

    You're not showing enough code. 

    What is going on in CAN_SendChannel and sendRelayMessages?