From 91604d02173f424bbe112ab695c2ff6f06156c36 Mon Sep 17 00:00:00 2001 From: Benjamin Valentin Date: Fri, 2 Oct 2020 22:40:57 +0200 Subject: [PATCH] cpu/samd21: clean up 'disabled GCLK' magic It turns out hooking up an unused peripheral to a disabled GCLK leads to surprising power savings. Name the GCLK to be more explicit (and since not all members of the extended samd2x family have a GCLK7). Turns out we can just use a non-existing GCLK ID for this, this even saves us a real GCLK that we can use for something else. Also make sure to disable *all* peripherals by using `GCLK_CLKCTRL_ID_Msk` instead of relying on a magic value. Looks like we previously missed some, since this leads to some additional power savings: master: 4.22 mA this patch: 4.09 mA --- cpu/samd21/cpu.c | 4 ++-- cpu/samd21/include/periph_cpu.h | 1 + 2 files changed, 3 insertions(+), 2 deletions(-) diff --git a/cpu/samd21/cpu.c b/cpu/samd21/cpu.c index 383584f7b6..17151d9780 100644 --- a/cpu/samd21/cpu.c +++ b/cpu/samd21/cpu.c @@ -252,8 +252,8 @@ static void clk_init(void) #endif /* redirect all peripherals to a disabled clock generator (7) by default */ - for (int i = 0x3; i <= 0x22; i++) { - GCLK->CLKCTRL.reg = ( GCLK_CLKCTRL_ID(i) | GCLK_CLKCTRL_GEN_GCLK7 ); + for (unsigned i = 0x3; i <= GCLK_CLKCTRL_ID_Msk; i++) { + GCLK->CLKCTRL.reg = GCLK_CLKCTRL_ID(i) | GCLK_CLKCTRL_GEN(SAM0_GCLK_DISABLED); while (GCLK->STATUS.bit.SYNCBUSY) {} } } diff --git a/cpu/samd21/include/periph_cpu.h b/cpu/samd21/include/periph_cpu.h index 08abcbd8e5..cd1c76d1b1 100644 --- a/cpu/samd21/include/periph_cpu.h +++ b/cpu/samd21/include/periph_cpu.h @@ -60,6 +60,7 @@ enum { SAM0_GCLK_1MHZ, /**< 1 MHz clock for xTimer */ SAM0_GCLK_32KHZ, /**< 32 kHz clock */ SAM0_GCLK_1KHZ, /**< 1 kHz clock */ + SAM0_GCLK_DISABLED = 0xF, /**< disabled GCLK */ }; /** @} */