From 896fcd43b074b43ef8bc2db9e860f12f2afcc3eb Mon Sep 17 00:00:00 2001 From: Benjamin Valentin Date: Fri, 13 Mar 2020 17:29:52 +0100 Subject: [PATCH] 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