STM32-P407: CAN automatic wakeup mode not working?

ChibiOS public support forum for all topics not covered by a specific support forum.

Moderators: RoccoMarco, lbednarz, tfAteba

jeremie.delaitre
Posts: 26
Joined: Fri Oct 05, 2012 3:16 pm
Been thanked: 1 time

STM32-P407: CAN automatic wakeup mode not working?

Post by jeremie.delaitre »

Hi,

I've a STM32-P407 board with CAN1 connected to a real CAN bus. I can send and receive data without problem.
However, I tried the automatic wakeup mode and it doesn't seems to work.

I've configured my CAN1 with CAN_MCR_AWUM, and I put CAN1 in sleep mode with canSleep().
As I understand from the board documentation, the CAN1 should wakeup automatically and thus I should be able to receive the data sent on the CAN bus without other modification in my code.

Am I missing something?
User avatar
Giovanni
Site Admin
Posts: 14895
Joined: Wed May 27, 2009 8:48 am
Has thanked: 1202 times
Been thanked: 997 times

Re: STM32-P407: CAN automatic wakeup mode not working?

Post by Giovanni »

Hi,

Thant scenario has not been tested, I am unable to tell if it is a problem in the driver or some other thing.

The driver in itself does just enter the sleep mode by setting the SLEEP bit, the SLEEP bit is supposed to be cleared automatically when activity is detected on the bus.

The manual states:

On CAN bus activity detection, hardware automatically performs the wakeup sequence by clearing the SLEEP bit if the AWUM bit in the CAN_MCR register is set. If the AWUM bit is cleared, software has to clear the SLEEP bit when a wakeup interrupt occurs, in order to exit from Sleep mode.

You may try to not specify AWUM and add an explicit SLEEP bit clearing in the ISR.

Giovanni
jeremie.delaitre
Posts: 26
Joined: Fri Oct 05, 2012 3:16 pm
Been thanked: 1 time

Re: STM32-P407: CAN automatic wakeup mode not working?

Post by jeremie.delaitre »

Hi,

Thanks for your reply! It allowed me to find some bugs in ChibiOS.

The first one was easily fixed. The interrupt handler for CAN1 SCE was never called due to a typo in the interrupt number. Bug report and patch here : https://sourceforge.net/tracker/?func=d ... tid=995489

The second one needed a little more investigation but I've finally got wakeup mode almost working but I'm not really sure of the fix some I've not done any bug report/patch yet. In the CAN1 SCE interrupt handler, I had to restore the state of the CAN1 driver to CAN_READY in order to have canReceive() to work:

Code: Select all

  /* Wakeup event.*/
  if (msr & CAN_MSR_WKUI) {
    CAND1.state = CAN_READY;
    chSysLockFromIsr();
    chEvtBroadcastI(&CAND1.wakeup_event);
    chSysUnlockFromIsr();
  }


Without this, the

Code: Select all

canp->state == CAN_SLEEP
condition in canReceive() is still verified and thus the CAN frames are never read.

With this fix, I'm able to use automatic wakeup but the first frame is always dropped.

Thus, I have few questions:

1- Does restoring the state in the interrupt handler is the way to go? If so, the error event probably need adjustment too right?
2- Should I attach to the wakeup_event and call canWakeup() in my callback rather than changing the state in the interrupt handler?
3- Do you have any idea why the first CAN frame is missing?
User avatar
Giovanni
Site Admin
Posts: 14895
Joined: Wed May 27, 2009 8:48 am
Has thanked: 1202 times
Been thanked: 997 times

Re: STM32-P407: CAN automatic wakeup mode not working?

Post by Giovanni »

Hi,

Thanks for finding. About the first frame dropped after wakeup, probably it is how the HW works and not related to the driver, the CAN peripheral senses traffic on the bus and restarts but in the meanwhile the first frame is already gone, just an hypothesis of course.

Giovanni
jeremie.delaitre
Posts: 26
Joined: Fri Oct 05, 2012 3:16 pm
Been thanked: 1 time

Re: STM32-P407: CAN automatic wakeup mode not working?

Post by jeremie.delaitre »

Regarding my questions 1 and 2, I've done small other experiments.

If I don't change the state in the interrupt handler, but rather attach an EventListener to the CAND1 wakeup_event, I'm able to call canWakeup(&CAND1) when the event is triggered. Then, as the driver is putted in the CAN_READY state by canWakeup(), I receive my frames as expected with canReceive().

However, as canWakeup() emits the wakeup_event too, the event is triggered twice.

Moreover, the automatic wakeup flag seems a little bit odd since we still have something to do in ChibiOS to be able to receive the frames properly (that is, put the driver in the CAN_READY state).

Maybe one should restore the CAN_READY state in the interrupt handler if the AWUM bit is set as the hardware already cleared the SLEEP bit. However, if the AWUM is not set, we do nothing and this is the user job to call canWakeup() if he really wants to wakeup (but the double event problem remains in this case).
User avatar
Giovanni
Site Admin
Posts: 14895
Joined: Wed May 27, 2009 8:48 am
Has thanked: 1202 times
Been thanked: 997 times

Re: STM32-P407: CAN automatic wakeup mode not working?

Post by Giovanni »

The AWUM bit then should not be allowed because the driver requires its notification in order to change state and generate the event. Not a big deal probably.

Giovanni
jeremie.delaitre
Posts: 26
Joined: Fri Oct 05, 2012 3:16 pm
Been thanked: 1 time

Re: STM32-P407: CAN automatic wakeup mode not working?

Post by jeremie.delaitre »

But even if AWUM is not allowed, the wakeup_event doesn't really match what happen.

When broadcasted by the interrupt handler, it means "something happened on the CAN bus but I'm still sleeping, wake me up if you want". When broadcasted by the canWakeup() function, it means "I'm not sleeping anymore, I'm ready".

Maybe we need a busactivity_event (better name needed!) used in the interrupt handler, and the wakeup_event used in canWakeup().
User avatar
Giovanni
Site Admin
Posts: 14895
Joined: Wed May 27, 2009 8:48 am
Has thanked: 1202 times
Been thanked: 997 times

Re: STM32-P407: CAN automatic wakeup mode not working?

Post by Giovanni »

I will look into this. Simply there could be a single event meaning "something happened" and different flags added depending on what really happened.

Giovanni
jeremie.delaitre
Posts: 26
Joined: Fri Oct 05, 2012 3:16 pm
Been thanked: 1 time

Re: STM32-P407: CAN automatic wakeup mode not working?

Post by jeremie.delaitre »

Ok, just keep me informed when you have something please :)
User avatar
Giovanni
Site Admin
Posts: 14895
Joined: Wed May 27, 2009 8:48 am
Has thanked: 1202 times
Been thanked: 997 times

Re: STM32-P407: CAN automatic wakeup mode not working?

Post by Giovanni »

Hi,

I committed a change to the CAN driver, now the SCE interrupt handler resets the SLEEP bit and changes the driver state to READY by itself. This means that there is no need to call canWakeup() from the event handler.

Could you give it a try and confirm it fixes the problem? if so, I will backport it to 2.4.x too.

thanks,
Giovanni
Post Reply