cancel
Showing results for 
Search instead for 
Did you mean: 

STM32F103 CAN IT won't retrigger

B C
Associate II
Posted on June 05, 2018 at 09:20

Hello everyone, 

I am having a little issue with my STM32F103RBT6 code. I am trying to make the Receive CAN IT work.

The problem I am facing is that it is working for a small amount of time and then just stops (either it's because of the overflow I don't know how to deal with or because the IT just won't retrigger).

My code is looking like this : 

in main : 

int flag2 = 0;

while (1)

{

   if(!flag2) //  RECEIVE

   {

      HAL_CAN_Receive_IT(&hcan, CAN_FIFO0);

      flag2 = 1;

   }

   

   for(cpt_for = 0; cpt_for<CAN_nb_trames_Rx_cc ; cpt_for++) // Receive test

   { // This is for me to see if the received messages are what I was expecting

      TxMessage.DLC = CAN_Tab_RxMessages[cpt_for].DLC;

      TxMessage.Data[0] = CAN_Tab_RxMessages[cpt_for].Data[0];

      TxMessage.Data[1] = CAN_Tab_RxMessages[cpt_for].Data[1];

      TxMessage.Data[2] = CAN_Tab_RxMessages[cpt_for].Data[2];

      TxMessage.Data[3] = CAN_Tab_RxMessages[cpt_for].Data[3];

      TxMessage.Data[4] = CAN_Tab_RxMessages[cpt_for].Data[4];

      TxMessage.Data[5] = CAN_Tab_RxMessages[cpt_for].Data[5];

      TxMessage.Data[6] = CAN_Tab_RxMessages[cpt_for].Data[6];

      TxMessage.Data[7] = CAN_Tab_RxMessages[cpt_for].Data[7];

      TxMessage.ExtId = CAN_Tab_RxMessages[cpt_for].ExtId;

      TxMessage.IDE = CAN_Tab_RxMessages[cpt_for].IDE;

      TxMessage.RTR = CAN_Tab_RxMessages[cpt_for].RTR;

      TxMessage.StdId = CAN_Tab_RxMessages[cpt_for].StdId;

      hcan.pTxMsg = &TxMessage;

      HAL_CAN_Transmit(&hcan, 100);

   }

}

void HAL_CAN_RxCpltCallback(CAN_HandleTypeDef *hcan)

{

   CAN_Tab_RxMessages[hcan->pRxMsg->FMI]=*(hcan->pRxMsg);

   flag2 = 0;

}

When I compile and implement it, it works perfectly when I try to receive just one CAN frame. But if I try to receive 2 at the same time it will stop. By reducing the speed of the frames I try to receive (should be 1 every 10ms but I tried 1 every 1 second), it works. However, if I send these 2 frames (that I am supposed to receive) at the same time, my �C won't receive them anymore, and if I dive into the registers, I can see that the Receive interrupt is not active anymore (hence it is not in interrupt mode). In the end, when I tried to receive two frames that were coming at a higher speed (1 every 10 ms), the issue I was facing was that the overflow bit was set in the CAN register and I don't really know how to deal witht this issue either.

If you have any idea on how I can fix it !

In advance thanks !

PS : On a side note, I am also looking for a board with a STM32F4 on it (I'm interested in its 2 CAN) where the 2 CAN inputs/outputs are available (for example in the discovery board, one is connected to the USB, which doesn't suit me). So if anyone knows a board that could fit my needs !

#hal_can_receive_it #stm32f103-interrupt
1 REPLY 1
B C
Associate II
Posted on June 05, 2018 at 14:21

So, by trying different things in order to make my code work, I changed my RxCallback function to : 

void HAL_CAN_RxCpltCallback(CAN_HandleTypeDef *hcan)

{

   CAN_Tab_RxMessages[hcan->pRxMsg->FMI]=*(hcan->pRxMsg);

   __HAL_CAN_ENABLE_IT (hcan, CAN_IT_FMP0);

}

and using : 

   

__HAL_CAN_ENABLE_IT (&hcan, CAN_IT_FMP0);

just before my while(1). And now it seems to be working !

However, I am still interested in your feedback on this method, is it right ? Do you think there is a more proper way to do it ? Was my code supposed to work in the first place ?

In advance, thank you !