2025-08-20 9:13 AM - last edited on 2025-08-21 12:46 AM by mƎALLEm
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
2025-08-21 12:49 AM
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.
2025-08-21 1:09 AM
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
2025-08-21 9:09 PM
@mƎALLEm , Waiting for your reply.