1
0
mirror of https://github.com/RIOT-OS/RIOT.git synced 2024-12-29 04:50:03 +01:00

Merge pull request #13631 from benpicco/cpu/sam0_common/spi-deinit

drivers/periph/spi: add periph_spi_reconfigure feature & implementation for sam0
This commit is contained in:
Francisco 2020-06-30 15:34:53 +02:00 committed by GitHub
commit 325b7a8d8e
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
10 changed files with 149 additions and 1 deletions

View File

@ -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

View File

@ -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

View File

@ -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 */
/**

View File

@ -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)

View File

@ -683,6 +683,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

View File

@ -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
/**

View File

@ -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) &&

View File

@ -205,6 +205,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

View File

@ -2,6 +2,7 @@ BOARD ?= samr21-xpro
include ../Makefile.tests_common
FEATURES_REQUIRED += periph_spi
FEATURES_OPTIONAL += periph_spi_reconfigure
USEMODULE += xtimer
USEMODULE += shell

View File

@ -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 }
};