cancel
Showing results for 
Search instead for 
Did you mean: 

STM32F303VET - issue in transmitting CAN messages

GComes
Associate II

I am having an issue in transmitting CAN messages, my situation is the following:

There is an interrupt function for RX messages from CAN1, when a message is received, it sets a flag. 

The baud rate of the RX message is 10ms and they are being sent over the software PCAN.

 

 

struct
{
	volatile uint8_t transmit_data;
}Flags;



void can1_RxCallback()
{
	Flags.transmit_data = 1;
}

 

 

In the main function in the while-loop, this flag is compared, if it is high, it executes a code to transmit data over CAN-Bus.

 

 

	
while (1)
{
	if (1 == Flags.transmit_data)
	{
		tx_data();
	}
}

void tx_data(void)
{	
     //delay(1);  //Non interrupt delay in ms
	can_transmit(&can, CanMessObj[nr].MessageID, 0, (uint8_t *)CANTrxBuffer[nr].Data, CanMessObj[nr].Length);		
	Flags.transmit_data = 0; //Reset Flag
}


void can_transmit(can_Interface_t* canInterface, uint32_t Id, uint32_t ide, uint8_t data[8], uint8_t length)
{
	uint32_t mailbox;
	canInterface->txMsg.IDE = ide;
	canInterface->txMsg.StdId = Id;
	canInterface->txMsg.ExtId = Id;
	canInterface->txMsg.DLC = length;
	
	HAL_CAN_AddTxMessage(canInterface->canChannel, &canInterface->txMsg, data, &mailbox);
}

 

 

The issue is some TX messages are not being sent, however, when I add the 1 ms delay, it works perfectly. But I want to avoid using delay.

So, to understand if the problem was in the transmission baud rate, I implemented a Timer interrupt for each 1ms and set the flag there instead of in the CAN interrupt and commented out the "delay(1)" in the function tx_data().

In this configuration, all TX messages were sent correctly.

 

 

void TIM7_IRQHandler(void) 
{	
	Flags.transmit_data = 1;
}

 

 

As the Timer interrupt is 10x faster than the CAN interrupt, I concluded that the issue is not how often the CAN messages are being transmitted. However, at the current point, I have no idea what could be leading to this issue, basically, I just changed where the flag is being set, nothing else.

 

Someone could help me to understand what might be causing this issue for transmitting?
CAN baud rate is: 250Kb

MCU: STM32F303VET

For checking the CAN messages and transmitting the CAN messages to the MCU I am using the software PCAN

1 ACCEPTED SOLUTION

Accepted Solutions

Do you have Automatic Retransmission enabled?

If you find my answers useful, click the accept button so that way others can see the solution.

View solution in original post

7 REPLIES 7
SofLit
ST Employee

Hello,

What is the status returned by HAL_CAN_AddTxMessage() ?

PS: there is no STM32F30EVET part number. Please check.

To give better visibility on the answered topics, please click on "Accept as Solution" on the reply which solved your issue or answered your question.

Hi @SofLit , thank you for the answer.

I updated the part number, the correct one is STM32F303VET.

The return from HAL_CAN_AddTxMessage() is always "HAL_OK"

OK, 

Try to check for free Tx mailbox before sending:

uint32_t freeTxMbox;
.
.
do
{
  freeTxMbox = HAL_CAN_GetTxMailboxesFreeLevel(canInterface->canChannel);
}while(freeTxMbox == 0);
HAL_CAN_AddTxMessage(canInterface->canChannel, &canInterface->txMsg, data, &mailbox);
To give better visibility on the answered topics, please click on "Accept as Solution" on the reply which solved your issue or answered your question.

Hi @SofLit , the result is still the same.

Meanwhile, I was doing another test, I sent two TX messages, instead of one, as shown below:

 

void tx_data(void)
{	
     //delay(1);  //Non interrupt delay in ms
	can_transmit(&can, CanMessObj[1].MessageID, 0, (uint8_t *)CANTrxBuffer[1].Data, CanMessObj[1].Length);	
	can_transmit(&can, CanMessObj[2].MessageID, 0, (uint8_t *)CANTrxBuffer[2].Data, CanMessObj[2].Length);		
	Flags.transmit_data = 0; //Reset Flag
}

 

Adding the code sample you provided the first message CanMessObj[1] missed some messages, while the CanMessObj[2] sent all them.

 

In PCAN-View I had the following result

                          |   ID    |  Length  |    Count
CanMessObj[1] | 0x30  |      8       |      464

CanMessObj[2] | 0x31  |      6       |      1096 


Setting the flag in the Timer interrupt or adding the 1 ms delay, the counter for both messages was the same. 

Do you have Automatic Retransmission enabled?

If you find my answers useful, click the accept button so that way others can see the solution.

Hi @Karl Yamashita

 

It was disabled. I have enabled it and it is working. Thank you!

You're welcome. Glad you got it working.

If you find my answers useful, click the accept button so that way others can see the solution.