Skip to main content
Associate II
May 21, 2024
Solved

STM32F303VET - issue in transmitting CAN messages

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

This topic has been closed for replies.
Karl Yamashita
Karl YamashitaBest answer
Principal
May 21, 2024

Do you have Automatic Retransmission enabled?

If a reply has proven helpful, click on Accept as Solution so that it'll show at top of the post.CAN Jammer an open source CAN bus hacking toolCANableV3 Open Source

7 replies

mƎALLEm
ST Technical Moderator
May 21, 2024

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 "Best answer" on the reply which solved your issue or answered your question.
GComesAuthor
Associate II
May 21, 2024

Hi @mƎALLEm , 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"

mƎALLEm
ST Technical Moderator
May 21, 2024

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 "Best answer" on the reply which solved your issue or answered your question.