From 896fcd43b074b43ef8bc2db9e860f12f2afcc3eb Mon Sep 17 00:00:00 2001 From: Benjamin Valentin Date: Fri, 13 Mar 2020 17:29:52 +0100 Subject: [PATCH 1/4] drivers/periph/spi: add periph_spi_reconfigure feature --- drivers/include/periph/spi.h | 69 +++++++++++++++++++++++++++++++++++- kconfigs/Kconfig.features | 5 +++ 2 files changed, 73 insertions(+), 1 deletion(-) diff --git a/drivers/include/periph/spi.h b/drivers/include/periph/spi.h index 1ee88bee29..f5b70aa21a 100644 --- a/drivers/include/periph/spi.h +++ b/drivers/include/periph/spi.h @@ -203,7 +203,6 @@ void spi_init(spi_t bus); /** * @brief Initialize the used SPI bus pins, i.e. MISO, MOSI, and CLK * - * * After calling spi_init, the pins must be initialized (i.e. spi_init is * calling this function internally). In normal cases, this function will not be * used. But there are some devices (e.g. CC110x), that use SPI bus lines also @@ -211,6 +210,9 @@ void spi_init(spi_t bus); * more of the used pins. So they can take control over certain pins and return * control back to the SPI driver using this function. * + * This function must be called after @ref spi_deinit_pins to return the pins to + * SPI operation. + * * The pins used are configured in the board's periph_conf.h. * * @param[in] bus SPI device the pins are configure for @@ -238,6 +240,71 @@ void spi_init_pins(spi_t bus); */ int spi_init_cs(spi_t bus, spi_cs_t cs); +#if defined(MODULE_PERIPH_SPI_RECONFIGURE) || DOXYGEN + +/** + * @brief Change the pins of the given SPI bus back to plain GPIO functionality + * + * The pin mux of the MISO, MOSI and CLK pins of the bus will be changed back to + * default (GPIO) mode and the SPI bus is powered off. + * This allows to use the SPI pins for another function and return to SPI + * functionality again by calling spi_init_pins() + * + * If you want the pin to be in a defined state, call gpio_init() on it. + * + * The bus MUST not be acquired before initializing it, as this is handled + * internally by the spi_deinit_pins() function! + * + * Calls to spi_acquire() will block until spi_init_pins() is called again. + * + * @note Until this is implemented on all platforms, this requires the + * periph_spi_reconfigure feature to be used. + * + * @param[in] dev the device to de-initialize + */ +void spi_deinit_pins(spi_t dev); + +#if DOXYGEN + +/** + * @brief Get the MISO pin of the given SPI bus. + * + * @param[in] dev The device to query + * + * @note Until this is implemented on all platforms, this requires the + * periph_spi_reconfigure feature to be used. + * + * @return The GPIO used for the SPI MISO line. + */ +gpio_t spi_pin_miso(spi_t dev); + +/** + * @brief Get the MOSI pin of the given SPI bus. + * + * @param[in] dev The device to query + * + * @note Until this is implemented on all platforms, this requires the + * periph_spi_reconfigure feature to be used. + * + * @return The GPIO used for the SPI MOSI line. + */ +gpio_t spi_pin_mosi(spi_t dev); + +/** + * @brief Get the CLK pin of the given SPI bus. + * + * @param[in] dev The device to query + * + * @note Until this is implemented on all platforms, this requires the + * periph_spi_reconfigure feature to be used. + * + * @return The GPIO used for the SPI CLK line. + */ +gpio_t spi_pin_clk(spi_t dev); + +#endif /* DOXYGEN */ +#endif /* MODULE_PERIPH_SPI_RECONFIGURE */ + #if defined(MODULE_PERIPH_SPI_GPIO_MODE) || DOXYGEN /** diff --git a/kconfigs/Kconfig.features b/kconfigs/Kconfig.features index 99325a000e..72663241ec 100644 --- a/kconfigs/Kconfig.features +++ b/kconfigs/Kconfig.features @@ -195,6 +195,11 @@ config HAS_PERIPH_SPI help Indicates that an SPI peripheral is present. +config HAS_PERIPH_SPI_RECONFIGURE + bool + help + Indicates that the SPI peripheral allows pin reconfiguration. + config HAS_PERIPH_SPI_GPIO_MODE bool help From 3e91914831273f781c2d9439da64dc8c12a3e020 Mon Sep 17 00:00:00 2001 From: Benjamin Valentin Date: Fri, 13 Mar 2020 17:33:43 +0100 Subject: [PATCH 2/4] cpu/sam0_common: spi: implement the periph_spi_reconfigure feature --- cpu/sam0_common/Kconfig | 1 + cpu/sam0_common/Makefile.features | 1 + cpu/sam0_common/include/periph_cpu_common.h | 10 ++++++++++ cpu/sam0_common/periph/spi.c | 10 ++++++++++ 4 files changed, 22 insertions(+) diff --git a/cpu/sam0_common/Kconfig b/cpu/sam0_common/Kconfig index fedf79cc31..c548bfe4d5 100644 --- a/cpu/sam0_common/Kconfig +++ b/cpu/sam0_common/Kconfig @@ -15,6 +15,7 @@ config CPU_COMMON_SAM0 select HAS_PERIPH_GPIO select HAS_PERIPH_GPIO_IRQ select HAS_PERIPH_I2C_RECONFIGURE + select HAS_PERIPH_SPI_RECONFIGURE select HAS_PERIPH_TIMER_PERIODIC select HAS_PERIPH_UART_MODECFG select HAS_PERIPH_UART_NONBLOCKING diff --git a/cpu/sam0_common/Makefile.features b/cpu/sam0_common/Makefile.features index a149991e88..89d252458a 100644 --- a/cpu/sam0_common/Makefile.features +++ b/cpu/sam0_common/Makefile.features @@ -5,6 +5,7 @@ FEATURES_PROVIDED += periph_flashpage_raw FEATURES_PROVIDED += periph_flashpage_rwee FEATURES_PROVIDED += periph_gpio periph_gpio_irq FEATURES_PROVIDED += periph_i2c_reconfigure +FEATURES_PROVIDED += periph_spi_reconfigure FEATURES_PROVIDED += periph_timer_periodic # implements timer_set_periodic() FEATURES_PROVIDED += periph_uart_modecfg FEATURES_PROVIDED += periph_uart_nonblocking diff --git a/cpu/sam0_common/include/periph_cpu_common.h b/cpu/sam0_common/include/periph_cpu_common.h index 9796867d6d..dab7a4a507 100644 --- a/cpu/sam0_common/include/periph_cpu_common.h +++ b/cpu/sam0_common/include/periph_cpu_common.h @@ -263,6 +263,16 @@ typedef enum { SPI_CLK_10MHZ = 10000000U /**< drive the SPI bus with 10MHz */ } spi_clk_t; /** @} */ + +/** + * @brief SPI pin getters + * @{ + */ +#define spi_pin_mosi(dev) spi_config[dev].mosi_pin +#define spi_pin_miso(dev) spi_config[dev].miso_pin +#define spi_pin_clk(dev) spi_config[dev].clk_pin +/** @} */ + #endif /* ndef DOXYGEN */ /** diff --git a/cpu/sam0_common/periph/spi.c b/cpu/sam0_common/periph/spi.c index b7dfc1cd85..934b0d1992 100644 --- a/cpu/sam0_common/periph/spi.c +++ b/cpu/sam0_common/periph/spi.c @@ -134,6 +134,16 @@ void spi_init_pins(spi_t bus) 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); /* clk_pin will be muxed during acquire / release */ + + mutex_unlock(&locks[bus]); +} + +void spi_deinit_pins(spi_t bus) +{ + mutex_lock(&locks[bus]); + + gpio_disable_mux(spi_config[bus].miso_pin); + gpio_disable_mux(spi_config[bus].mosi_pin); } int spi_acquire(spi_t bus, spi_cs_t cs, spi_mode_t mode, spi_clk_t clk) From f3e28c0350a86ab94f1fcb3a4aa19dbb77380b5a Mon Sep 17 00:00:00 2001 From: Benjamin Valentin Date: Wed, 27 May 2020 13:15:27 +0200 Subject: [PATCH 3/4] drivers/sdcard_spi: make use of spi_deinit_pins() Make sure to de-init the SPI bus before using the pins as GPIO. Once all archs implement periph_spi_reconfigure, we can drop mosi, miso and clk pins from the `card->params` and just use the bus getter functions. --- drivers/Makefile.dep | 1 + drivers/sdcard_spi/sdcard_spi.c | 3 +++ 2 files changed, 4 insertions(+) diff --git a/drivers/Makefile.dep b/drivers/Makefile.dep index e313b9d206..3a87c85038 100644 --- a/drivers/Makefile.dep +++ b/drivers/Makefile.dep @@ -651,6 +651,7 @@ endif ifneq (,$(filter sdcard_spi,$(USEMODULE))) FEATURES_REQUIRED += periph_gpio FEATURES_REQUIRED += periph_spi + FEATURES_OPTIONAL += periph_spi_reconfigure DEFAULT_MODULE += auto_init_storage USEMODULE += checksum USEMODULE += xtimer diff --git a/drivers/sdcard_spi/sdcard_spi.c b/drivers/sdcard_spi/sdcard_spi.c index d650d749cf..df5f03b8e1 100644 --- a/drivers/sdcard_spi/sdcard_spi.c +++ b/drivers/sdcard_spi/sdcard_spi.c @@ -84,6 +84,9 @@ static sd_init_fsm_state_t _init_sd_fsm_step(sdcard_spi_t *card, sd_init_fsm_sta case SD_INIT_START: DEBUG("SD_INIT_START\n"); +#ifdef MODULE_PERIPH_SPI_RECONFIGURE + spi_deinit_pins(card->params.spi_dev); +#endif if ((gpio_init(card->params.mosi, GPIO_OUT) == 0) && (gpio_init(card->params.clk, GPIO_OUT) == 0) && (gpio_init(card->params.cs, GPIO_OUT) == 0) && From bd7acf4adf89fa0373309049cfcdb0921497ce31 Mon Sep 17 00:00:00 2001 From: Benjamin Valentin Date: Thu, 11 Jun 2020 16:21:41 +0200 Subject: [PATCH 4/4] tests/periph_spi: add test for periph_spi_reconfigure feature --- tests/periph_spi/Makefile | 1 + tests/periph_spi/main.c | 49 +++++++++++++++++++++++++++++++++++++++ 2 files changed, 50 insertions(+) diff --git a/tests/periph_spi/Makefile b/tests/periph_spi/Makefile index 230f27c823..45352ab075 100644 --- a/tests/periph_spi/Makefile +++ b/tests/periph_spi/Makefile @@ -2,6 +2,7 @@ BOARD ?= samr21-xpro include ../Makefile.tests_common FEATURES_REQUIRED += periph_spi +FEATURES_OPTIONAL += periph_spi_reconfigure USEMODULE += xtimer USEMODULE += shell diff --git a/tests/periph_spi/main.c b/tests/periph_spi/main.c index adb9d7f919..d39f7b37d9 100644 --- a/tests/periph_spi/main.c +++ b/tests/periph_spi/main.c @@ -541,10 +541,59 @@ int cmd_bench(int argc, char **argv) return 0; } +#ifdef MODULE_PERIPH_SPI_RECONFIGURE +int cmd_spi_gpio(int argc, char **argv) +{ + int dev = -1; + + /* parse the given SPI device */ + if (argc > 1) { + dev = atoi(argv[1]); + } + + if (dev < 0 || dev >= (int)SPI_NUMOF) { + puts("error: invalid SPI device specified"); + return 1; + } + + gpio_t miso_pin = spi_pin_miso(dev); + gpio_t mosi_pin = spi_pin_mosi(dev); + + printf("Command: spi_deinit_pins(%i)\n", dev); + spi_deinit_pins(dev); + + gpio_init(miso_pin, GPIO_OUT); + gpio_init(mosi_pin, GPIO_OUT); + + xtimer_sleep(1); + + printf("Command: gpio_set()\n"); + gpio_set(miso_pin); + gpio_set(mosi_pin); + + xtimer_sleep(1); + + printf("Command: gpio_clear()\n"); + gpio_clear(miso_pin); + gpio_clear(mosi_pin); + + xtimer_sleep(1); + + printf("Command: spi_init_pins(%i)\n", dev); + spi_init_pins(dev); + + printf("Success: spi_%i re-init\n", dev); + return 0; +} +#endif + static const shell_command_t shell_commands[] = { { "init", "Setup a particular SPI configuration", cmd_init }, { "send", "Transfer string to slave", cmd_transfer }, { "bench", "Runs some benchmarks", cmd_bench }, +#ifdef MODULE_PERIPH_SPI_RECONFIGURE + { "spi_gpio", "Re-configures MISO & MOSI pins to GPIO mode and back.", cmd_spi_gpio }, +#endif { NULL, NULL, NULL } };