Page 1 of 2
CAN packets are being lost
Posted: Sat Aug 17, 2013 8:02 pm
by wertex
There is a task in my application to receive CAN packets by STM32F105 continuously. By using the code below I met the problem that
the function can_cmd_receive_can_packet_array loses packets that is actually it does not manage to receive the incoming CAN packets.
In which way is it possible to alter the function of receiving CAN packets by using ChibiOS so that it manages to follow up receiving of all the CAN packets?
Code: Select all
CANConfig cancfg = {
CAN_MCR_ABOM | CAN_MCR_AWUM | CAN_MCR_TXFP,
CAN_BTR_SJW(0) | CAN_BTR_TS2(4) | CAN_BTR_TS1(5) | CAN_BTR_BRP(5)
};
uint16_t can_cmd_receive_can_packet_array(uint32_t timeout, uint16_t *message_cnt, CANRxFrame (*msg)[])
{
uint16_t msg_cnt=0;
if (chEvtWaitAnyTimeout(CAN_MAILBOX_TO_MASK(1) | CAN_MAILBOX_TO_MASK(2), MS2ST(timeout)) == 0) return ERR_CAN_READ_TIMEOUT;
do {
while (canReceive(&CAND1, CAN_ANY_MAILBOX, (*msg+msg_cnt), TIME_IMMEDIATE) == RDY_OK) msg_cnt++;
}
while (chEvtWaitAnyTimeout(CAN_MAILBOX_TO_MASK(1) | CAN_MAILBOX_TO_MASK(2), MS2ST(5)) != 0);
*message_cnt=msg_cnt;
return ERR_SUCCESS;
}
Re: CAN packets are being lost
Posted: Sat Aug 17, 2013 8:19 pm
by Giovanni
Hi,
Hard to tell, the code looks correct. Does the driver report errors? could it be an HW problem or a problem in the sender? does it happen that canReceive() returns something different from RDY_OK?
Just a note, the function canReceive() is able to wait without having to use events.
Giovanni
Re: CAN packets are being lost
Posted: Sat Aug 17, 2013 9:23 pm
by wertex
Giovanni wrote:Hi,
Hard to tell, the code looks correct. Does the driver report errors? could it be an HW problem or a problem in the sender? does it happen that canReceive() returns something different from RDY_OK?
Just a note, the function canReceive() is able to wait without having to use events.
Giovanni
There is no problem with the HW, all the CAN packages are successfully received by the device of another producer. I simplified my function (please see below), but such an option led to the situation that there are more packages (times more) lost as a result.
Is t possibe that ChibiOS does not manage to work out the incoming CAN packets? The interval between the CAN packets which are being received is about 26 usec at 500000 bps.
Code: Select all
uint16_t msg_cnt=0;
if (chEvtWaitAnyTimeout(CAN_MAILBOX_TO_MASK(1) | CAN_MAILBOX_TO_MASK(2), MS2ST(timeout)) == 0) return ERR_CAN_READ_TIMEOUT;
while (canReceive(&CAND1, CAN_ANY_MAILBOX, (*msg+msg_cnt), MS2ST(1)) == RDY_OK) msg_cnt++;
*message_cnt=msg_cnt;
return 0;
Re: CAN packets are being lost
Posted: Sat Aug 17, 2013 9:28 pm
by Giovanni
Try putting a listener on the error event source (error_event field of CANDriver), if there is an error on the driver side it is there that you get the problem encoded into the flags.
Giovanni
Re: CAN packets are being lost
Posted: Sun Aug 18, 2013 1:23 pm
by wertex
Giovanni wrote:Try putting a listener on the error event source (error_event field of CANDriver), if there is an error on the driver side it is there that you get the problem encoded into the flags.
Giovanni
I have got error 0x19. As I understood it means CAN_OVERFLOW_ERROR | CAN_FRAMING_ERROR | CAN_LIMIT_WARNING
Re: CAN packets are being lost
Posted: Sun Aug 18, 2013 1:28 pm
by Giovanni
This seems to confirm that you are unable to consume incoming packets fast enough, do you have other threads at the same priority or higher priority of the CAN thread?
If not, could you try to reduce the rate of packets on the sender?
Giovanni
Re: CAN packets are being lost
Posted: Sun Aug 18, 2013 1:49 pm
by wertex
Giovanni wrote:This seems to confirm that you are unable to consume incoming packets fast enough, do you have other threads at the same priority or higher priority of the CAN thread?
If not, could you try to reduce the rate of packets on the sender?
Giovanni
I use in application one thread only with normal priority. I try to reduce rate of sender to 250000 and 125000, but problem still exists. Use this rates less CAN packets are lost.
Re: CAN packets are being lost
Posted: Sun Aug 18, 2013 1:51 pm
by Giovanni
Also the main() is a thread at normal priority, do you have other activity there? if so try moving the CAN thread to normal priority +1.
Giovanni
Re: CAN packets are being lost
Posted: Sun Aug 18, 2013 9:06 pm
by wertex
Giovanni wrote:Also the main() is a thread at normal priority, do you have other activity there? if so try moving the CAN thread to normal priority +1.
Giovanni
No activity in main(). Just start single thread and endless loop:
Code: Select all
for(;;)
{
chThdSleepMilliseconds(1000);
}
Re: CAN packets are being lost
Posted: Sun Aug 18, 2013 9:19 pm
by Giovanni
If you reduce speed and the problem still exists then I think it is not a problem of MCU not being able to consume packets fast enough. How low can you go with speed? there is a speed where the problem does not occur at all?
You may also try to send packets at intervals and see if the problem disappears.
Giovanni