2024-05-21 02:54 AM - edited 2024-05-21 07:31 AM
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
Solved! Go to Solution.
2024-05-21 11:51 AM
Do you have Automatic Retransmission enabled?
2024-05-21 04:00 AM - edited 2024-05-21 04:00 AM
Hello,
What is the status returned by HAL_CAN_AddTxMessage() ?
PS: there is no STM32F30EVET part number. Please check.
2024-05-21 04:29 AM - edited 2024-05-21 04:29 AM
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"
2024-05-21 04:48 AM
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);
2024-05-21 05:09 AM
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.
2024-05-21 11:51 AM
Do you have Automatic Retransmission enabled?
2024-05-21 11:32 PM - edited 2024-05-21 11:39 PM
2024-05-22 02:33 AM
You're welcome. Glad you got it working.