Skip to content

Commit

Permalink
cpu/samd5x/periph_can: fix RX
Browse files Browse the repository at this point in the history
CAN required CLK_CANx_APB and CLK_CANx_APB to be running and will not
request any clock by itself. We can ensure both clocks to be running
by preventing the MCU from entering IDLE state.

The SAMD5x/SAME5x Family Data Sheet says in Section
"39.6.9 Sleep Mode Operation" says:

> The CAN can be configured to operate in any idle sleep mode. The CAN
> cannot operate in Standby sleep mode.
>
> [...]
>
> To leave low power mode, CLK_CANx_APB and GCLK_CANx must be active
> before writing CCCR.CSR to '0'. The CAN will acknowledge this by
> resetting CCCR.CSA = 0. Afterwards, the application can restart CAN
> communication by resetting bit CCCR.INIT.

tl;dr: At most SAM0_PM_IDLE is allowed while not shutting down the CAN
controller, but even that will pause communication (including RX).

Apparently, the CAN controller was never tested without also using the
USB peripheral, which kept the clocks running as side effect.
  • Loading branch information
maribu committed Jan 31, 2025
1 parent 775f596 commit d93f0de
Showing 1 changed file with 26 additions and 1 deletion.
27 changes: 26 additions & 1 deletion cpu/samd5x/periph/can.c
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,10 @@
#include <assert.h>
#include <string.h>

#include "can/device.h"
#include "periph/can.h"
#include "periph/gpio.h"
#include "can/device.h"
#include "pm_layered.h"

#define ENABLE_DEBUG 0
#include "debug.h"
Expand Down Expand Up @@ -325,6 +326,30 @@ static void _dump_msg_ram_section(can_t *dev)

static int _init(candev_t *candev)
{
/* CAN required CLK_CANx_APB and CLK_CANx_APB to be running and will not
* request any clock by itself. We can ensure both clocks to be running
* by preventing the MCU from entering IDLE state.
*
* The SAMD5x/SAME5x Family Data Sheet says in Section
* "39.6.9 Sleep Mode Operation" says:
*
* > The CAN can be configured to operate in any idle sleep mode. The CAN
* > cannot operate in Standby sleep mode.
* >
* > [...]
* >
* > To leave low power mode, CLK_CANx_APB and GCLK_CANx must be active
* > before writing CCCR.CSR to '0'. The CAN will acknowledge this by
* > resetting CCCR.CSA = 0. Afterwards, the application can restart CAN
* > communication by resetting bit CCCR.INIT.
*
* tl;dr: At most SAM0_PM_IDLE is allowed while not shutting down the CAN
* controller, but even that will pause communication (including RX).
*/
if (IS_USED(MODULE_PM_LAYERED)) {
pm_block(SAM0_PM_IDLE);
}

can_t *dev = container_of(candev, can_t, candev);
int res = 0;

Expand Down

0 comments on commit d93f0de

Please sign in to comment.