From 31f88a2d0e23f1e27d0ef40427f271c2050249e8 Mon Sep 17 00:00:00 2001 From: Benjamin Valentin Date: Fri, 29 Mar 2019 13:39:34 +0100 Subject: [PATCH 1/3] sam0_common: periph/spi: use sercom_clk_en/dis() Use already existing functions to turn on / off SERCOM clocks instead of replicating the functionality in the driver. --- cpu/sam0_common/periph/spi.c | 12 ++---------- 1 file changed, 2 insertions(+), 10 deletions(-) diff --git a/cpu/sam0_common/periph/spi.c b/cpu/sam0_common/periph/spi.c index 551798c36f..cbd48ae1f1 100644 --- a/cpu/sam0_common/periph/spi.c +++ b/cpu/sam0_common/periph/spi.c @@ -48,20 +48,12 @@ static inline SercomSpi *dev(spi_t bus) static inline void poweron(spi_t bus) { -#if defined(CPU_FAM_SAMD21) - PM->APBCMASK.reg |= (PM_APBCMASK_SERCOM0 << sercom_id(dev(bus))); -#elif defined(CPU_SAML21) || defined(CPU_SAML1X) - MCLK->APBCMASK.reg |= (MCLK_APBCMASK_SERCOM0 << sercom_id(dev(bus))); -#endif + sercom_clk_en(dev(bus)); } static inline void poweroff(spi_t bus) { -#if defined(CPU_FAM_SAMD21) - PM->APBCMASK.reg &= ~(PM_APBCMASK_SERCOM0 << sercom_id(dev(bus))); -#elif defined(CPU_SAML21) || defined(CPU_SAML1X) - MCLK->APBCMASK.reg &= ~(MCLK_APBCMASK_SERCOM0 << sercom_id(dev(bus))); -#endif + sercom_clk_dis(dev(bus)); } void spi_init(spi_t bus) From 84233ce5d519aba8b3eeee3ec2083300b0de6d46 Mon Sep 17 00:00:00 2001 From: Benjamin Valentin Date: Fri, 29 Mar 2019 15:03:54 +0100 Subject: [PATCH 2/3] sam0_common: replace sercom_id() calculation with switch statement As the sercom_id() function grows it gets more unweidly. Let's replace it with a simple switch statement that is true for all sam0 parts. --- cpu/sam0_common/include/periph_cpu_common.h | 42 +++++++++++++++++---- 1 file changed, 34 insertions(+), 8 deletions(-) diff --git a/cpu/sam0_common/include/periph_cpu_common.h b/cpu/sam0_common/include/periph_cpu_common.h index ac3fe8edd0..8039b83109 100644 --- a/cpu/sam0_common/include/periph_cpu_common.h +++ b/cpu/sam0_common/include/periph_cpu_common.h @@ -323,16 +323,42 @@ void gpio_init_mux(gpio_t pin, gpio_mux_t mux); * * @return numeric id of the given SERCOM device */ -static inline int sercom_id(void *sercom) +static inline int sercom_id(const void *sercom) { -#if defined(CPU_FAM_SAMD21) - return ((((uint32_t)sercom) >> 10) & 0x7) - 2; -#elif defined (CPU_FAM_SAML10) || defined (CPU_FAM_SAML11) - return ((((uint32_t)sercom) >> 10) & 0x7) - 1; -#elif defined(CPU_FAM_SAML21) || defined(CPU_FAM_SAMR30) - /* Left side handles SERCOM0-4 while right side handles unaligned address of SERCOM5 */ - return ((((uint32_t)sercom) >> 10) & 0x7) + ((((uint32_t)sercom) >> 22) & 0x04); +#ifdef SERCOM0 + if (sercom == SERCOM0) + return 0; #endif +#ifdef SERCOM1 + if (sercom == SERCOM1) + return 1; +#endif +#ifdef SERCOM2 + if (sercom == SERCOM2) + return 2; +#endif +#ifdef SERCOM3 + if (sercom == SERCOM3) + return 3; +#endif +#ifdef SERCOM4 + if (sercom == SERCOM4) + return 4; +#endif +#ifdef SERCOM5 + if (sercom == SERCOM5) + return 5; +#endif +#ifdef SERCOM6 + if (sercom == SERCOM6) + return 6; +#endif +#ifdef SERCOM7 + if (sercom == SERCOM7) + return 7; +#endif + + return -1; } /** From 7415e0590ebc6ccbbb6d61145073b72704541392 Mon Sep 17 00:00:00 2001 From: Benjamin Valentin Date: Tue, 2 Apr 2019 18:01:24 +0200 Subject: [PATCH 3/3] sam0_common: spi: use sercom_set_gen() instead of re-implementing it Don't repeat yourself and introduce errors in doing so. --- cpu/sam0_common/periph/spi.c | 11 ++++------- 1 file changed, 4 insertions(+), 7 deletions(-) diff --git a/cpu/sam0_common/periph/spi.c b/cpu/sam0_common/periph/spi.c index cbd48ae1f1..43a9f105a7 100644 --- a/cpu/sam0_common/periph/spi.c +++ b/cpu/sam0_common/periph/spi.c @@ -76,13 +76,10 @@ void spi_init(spi_t bus) (dev(bus)->SYNCBUSY.reg & SERCOM_SPI_SYNCBUSY_SWRST)); /* configure base clock: using GLK GEN 0 */ -#if defined(CPU_FAM_SAMD21) - GCLK->CLKCTRL.reg = (GCLK_CLKCTRL_CLKEN | GCLK_CLKCTRL_GEN_GCLK0 | - (SERCOM0_GCLK_ID_CORE + sercom_id(dev(bus)))); - while (GCLK->STATUS.reg & GCLK_STATUS_SYNCBUSY) {} -#elif defined(CPU_SAML21) || defined(CPU_SAML1X) - GCLK->PCHCTRL[SERCOM0_GCLK_ID_CORE + sercom_id(dev(bus))].reg = - (GCLK_PCHCTRL_CHEN | GCLK_PCHCTRL_GEN_GCLK0); +#ifdef GCLK_CLKCTRL_GEN_GCLK0 + sercom_set_gen(dev(bus), GCLK_CLKCTRL_GEN_GCLK0); +#else + sercom_set_gen(dev(bus), GCLK_PCHCTRL_GEN_GCLK0); #endif /* enable receiver and configure character size to 8-bit