Work at SourceForge, help us to make it a better place! We have an immediate need for a Support Technician in our San Francisco or Denver office.


STM32 CAN error flags

  • Tibor Billes
    Tibor Billes


    I'm working on a project with Balázs Póka (bpoka) using STM32 microcontrollers. I'm working with CAN, and I found that the error reporting mechanism via CANDriver.cd_error_event aggregates several error conditions as "framing error". During development or debugging it would help if I could see the detailed error condition. To be more precise, I'm thinking of Last Error Code bit group in the Error Status Register.

    Right now, I just copied LEC into flags (as the diff shows below), but that isn't very portable and is somewhat confusing combining individual flags and a bit group from a register.

    Any suggestions how to do this in a portable and nice way?

    -- can_lld.c (revision 1908)
    +++ can_lld.c (working copy)
    @@ -140,8 +140,10 @@

         CAN1->ESR &= ~CAN_ESR_LEC;
         flags = (canstatus_t)(esr & 7);
    -    if ((esr & CAN_ESR_LEC) > 0)
    -      flags |= CAN_FRAMING_ERROR;
    +    if ((esr & CAN_ESR_LEC) > 0) {
    +     flags |= CAN_FRAMING_ERROR;
    +     flags |= (esr & CAN_ESR_LEC) << 1;
    +    }
         canAddFlagsI(&CAND1, flags);

  • Hello,

    I see, it is done in that way because it is supposed to be a generic CAN driver not an STM32 specific one. those bit are probably too specific.

    I would simply copy the 16 least significant bits of the ESR into the 16 most significant bits of "flags", those are not defined by the interface and you can use them as platform-specific flags. This way you can both access the standard flags and the plain ESR data when serving the event.

    If you find this solution acceptable I will also add this to next version.


  • Tibor Billes
    Tibor Billes


    That would be perfect, thank you!