From df90176b1fd55a4c9b213066f5d594b4919b2c18 Mon Sep 17 00:00:00 2001 From: Juergen Fitschen Date: Wed, 20 May 2020 17:16:17 +0200 Subject: [PATCH] cpu/sam0_common/spi: move clk pin muxing into spi_acquire / spi_release When the SPI peripheral is disabled, the output lines will become HIGH-Z. If the clk pin is not pulled HIGH or LOW, connected SPI slaves will start drawing current expectedly. --- cpu/sam0_common/periph/spi.c | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/cpu/sam0_common/periph/spi.c b/cpu/sam0_common/periph/spi.c index ff6e858620..5848ef2341 100644 --- a/cpu/sam0_common/periph/spi.c +++ b/cpu/sam0_common/periph/spi.c @@ -94,7 +94,7 @@ void spi_init_pins(spi_t bus) gpio_init(spi_config[bus].clk_pin, GPIO_OUT); gpio_init_mux(spi_config[bus].miso_pin, spi_config[bus].miso_mux); gpio_init_mux(spi_config[bus].mosi_pin, spi_config[bus].mosi_mux); - gpio_init_mux(spi_config[bus].clk_pin, spi_config[bus].clk_mux); + /* clk_pin will be muxed during acquire / release */ } int spi_acquire(spi_t bus, spi_cs_t cs, spi_mode_t mode, spi_clk_t clk) @@ -141,11 +141,18 @@ int spi_acquire(spi_t bus, spi_cs_t cs, spi_mode_t mode, spi_clk_t clk) dev(bus)->CTRLA.reg |= SERCOM_SPI_CTRLA_ENABLE; while (dev(bus)->SYNCBUSY.reg & SERCOM_SPI_SYNCBUSY_ENABLE) {} + /* mux clk_pin to SPI peripheral */ + gpio_init_mux(spi_config[bus].clk_pin, spi_config[bus].clk_mux); + return SPI_OK; } void spi_release(spi_t bus) { + /* Demux clk_pin back to GPIO_OUT function. Otherwise it will get HIGH-Z + * and lead to unexpected current draw by SPI salves. */ + gpio_disable_mux(spi_config[bus].clk_pin); + /* disable the device */ dev(bus)->CTRLA.reg &= ~(SERCOM_SPI_CTRLA_ENABLE); while (dev(bus)->SYNCBUSY.reg & SERCOM_SPI_SYNCBUSY_ENABLE) {}