cancel
Showing results for 
Search instead for 
Did you mean: 

stm32l452 timer interrupt and can tle9255w

yashg25
Associate II

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();

    }

}

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

}

}

 

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

 

Thanks

 

0 REPLIES 0