CAN packets are being lost

ChibiOS public support forum for topics related to the STMicroelectronics STM32 family of micro-controllers.

Moderator: RoccoMarco

wertex
Posts: 14
Joined: Wed Jul 10, 2013 2:36 pm

CAN packets are being lost

Post 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;
}
User avatar
Giovanni
Site Admin
Posts: 14892
Joined: Wed May 27, 2009 8:48 am
Has thanked: 1202 times
Been thanked: 996 times

Re: CAN packets are being lost

Post 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
wertex
Posts: 14
Joined: Wed Jul 10, 2013 2:36 pm

Re: CAN packets are being lost

Post 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;
User avatar
Giovanni
Site Admin
Posts: 14892
Joined: Wed May 27, 2009 8:48 am
Has thanked: 1202 times
Been thanked: 996 times

Re: CAN packets are being lost

Post 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
wertex
Posts: 14
Joined: Wed Jul 10, 2013 2:36 pm

Re: CAN packets are being lost

Post 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
User avatar
Giovanni
Site Admin
Posts: 14892
Joined: Wed May 27, 2009 8:48 am
Has thanked: 1202 times
Been thanked: 996 times

Re: CAN packets are being lost

Post 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
wertex
Posts: 14
Joined: Wed Jul 10, 2013 2:36 pm

Re: CAN packets are being lost

Post 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.
User avatar
Giovanni
Site Admin
Posts: 14892
Joined: Wed May 27, 2009 8:48 am
Has thanked: 1202 times
Been thanked: 996 times

Re: CAN packets are being lost

Post 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
wertex
Posts: 14
Joined: Wed Jul 10, 2013 2:36 pm

Re: CAN packets are being lost

Post 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);
    }
User avatar
Giovanni
Site Admin
Posts: 14892
Joined: Wed May 27, 2009 8:48 am
Has thanked: 1202 times
Been thanked: 996 times

Re: CAN packets are being lost

Post 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
Post Reply