1
0
mirror of https://github.com/RIOT-OS/RIOT.git synced 2025-01-18 12:52:44 +01:00

Merge pull request #20391 from maribu/cpu/stm32/periph_spi_reconfigure

cpu/stm32: implement periph_spi_reconfigure
This commit is contained in:
benpicco 2024-02-15 22:59:33 +00:00 committed by GitHub
commit ef0e3fb54e
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
3 changed files with 64 additions and 12 deletions

View File

@ -7,11 +7,6 @@ endif
FEATURES_PROVIDED += cpu_stm32$(CPU_FAM) FEATURES_PROVIDED += cpu_stm32$(CPU_FAM)
FEATURES_PROVIDED += periph_cpuid FEATURES_PROVIDED += periph_cpuid
FEATURES_PROVIDED += periph_gpio periph_gpio_irq FEATURES_PROVIDED += periph_gpio periph_gpio_irq
FEATURES_PROVIDED += periph_timer_periodic
FEATURES_PROVIDED += periph_timer_query_freqs
FEATURES_PROVIDED += periph_rtt_overflow
FEATURES_PROVIDED += periph_uart_modecfg
FEATURES_PROVIDED += periph_uart_nonblocking
FEATURES_PROVIDED += periph_gpio_ll FEATURES_PROVIDED += periph_gpio_ll
FEATURES_PROVIDED += periph_gpio_ll_disconnect FEATURES_PROVIDED += periph_gpio_ll_disconnect
FEATURES_PROVIDED += periph_gpio_ll_input_pull_down FEATURES_PROVIDED += periph_gpio_ll_input_pull_down
@ -20,6 +15,12 @@ FEATURES_PROVIDED += periph_gpio_ll_irq
FEATURES_PROVIDED += periph_gpio_ll_irq_level_triggered_high FEATURES_PROVIDED += periph_gpio_ll_irq_level_triggered_high
FEATURES_PROVIDED += periph_gpio_ll_irq_level_triggered_low FEATURES_PROVIDED += periph_gpio_ll_irq_level_triggered_low
FEATURES_PROVIDED += periph_gpio_ll_open_drain FEATURES_PROVIDED += periph_gpio_ll_open_drain
FEATURES_PROVIDED += periph_rtt_overflow
FEATURES_PROVIDED += periph_spi_reconfigure
FEATURES_PROVIDED += periph_timer_periodic
FEATURES_PROVIDED += periph_timer_query_freqs
FEATURES_PROVIDED += periph_uart_modecfg
FEATURES_PROVIDED += periph_uart_nonblocking
ifneq (f1,$(CPU_FAM)) ifneq (f1,$(CPU_FAM))
FEATURES_PROVIDED += periph_gpio_ll_open_drain_pull_up FEATURES_PROVIDED += periph_gpio_ll_open_drain_pull_up

View File

@ -32,6 +32,12 @@
extern "C" { extern "C" {
#endif #endif
#ifndef DOXYGEN
/* resolve circular dependency by declaring spi_t here */
#define HAVE_SPI_T
typedef uint_fast8_t spi_t;
#endif
/** /**
* @brief Define a magic number that tells us to use hardware chip select * @brief Define a magic number that tells us to use hardware chip select
* *
@ -119,6 +125,12 @@ typedef struct {
#endif #endif
} spi_conf_t; } spi_conf_t;
#ifndef DOXYGEN
gpio_t spi_pin_miso(spi_t bus);
gpio_t spi_pin_mosi(spi_t bus);
gpio_t spi_pin_clk(spi_t bus);
#endif
#ifdef __cplusplus #ifdef __cplusplus
} }
#endif #endif

View File

@ -28,6 +28,7 @@
#include <assert.h> #include <assert.h>
#include "compiler_hints.h"
#include "mutex.h" #include "mutex.h"
#include "periph/gpio.h" #include "periph/gpio.h"
#include "periph/spi.h" #include "periph/spi.h"
@ -94,17 +95,17 @@ static uint8_t _get_prescaler(const spi_conf_t *conf, uint32_t clock)
* happen if this cannot be met. So let's have a blown assertion * happen if this cannot be met. So let's have a blown assertion
* rather than runtime failures that require a logic analyzer to * rather than runtime failures that require a logic analyzer to
* debug. */ * debug. */
assert(prescaled_clock <= clock); assume(prescaled_clock <= clock);
return prescaler; return prescaler;
} }
void spi_init(spi_t bus) void spi_init(spi_t bus)
{ {
assert(bus < SPI_NUMOF); assume(bus < SPI_NUMOF);
/* initialize device lock */ /* initialize device lock (as locked, spi_init_pins() will unlock it */
mutex_init(&locks[bus]); locks[bus] = (mutex_t)MUTEX_INIT_LOCKED;
/* trigger pin initialization */ /* trigger pin initialization */
spi_init_pins(bus); spi_init_pins(bus);
@ -120,6 +121,7 @@ void spi_init(spi_t bus)
void spi_init_pins(spi_t bus) void spi_init_pins(spi_t bus)
{ {
assume(bus < SPI_NUMOF);
#ifdef CPU_FAM_STM32F1 #ifdef CPU_FAM_STM32F1
if (gpio_is_valid(spi_config[bus].sclk_pin)) { if (gpio_is_valid(spi_config[bus].sclk_pin)) {
@ -149,6 +151,43 @@ void spi_init_pins(spi_t bus)
gpio_init_af(spi_config[bus].sclk_pin, spi_config[bus].sclk_af); gpio_init_af(spi_config[bus].sclk_pin, spi_config[bus].sclk_af);
} }
#endif #endif
mutex_unlock(&locks[bus]);
}
void spi_deinit_pins(spi_t bus)
{
assume(bus < SPI_NUMOF);
mutex_lock(&locks[bus]);
if (gpio_is_valid(spi_config[bus].sclk_pin)) {
gpio_init(spi_config[bus].sclk_pin, GPIO_IN);
}
if (gpio_is_valid(spi_config[bus].mosi_pin)) {
gpio_init(spi_config[bus].mosi_pin, GPIO_IN);
}
if (gpio_is_valid(spi_config[bus].miso_pin)) {
gpio_init(spi_config[bus].miso_pin, GPIO_IN);
}
}
gpio_t spi_pin_miso(spi_t bus)
{
assume(bus < SPI_NUMOF);
return spi_config[bus].miso_pin;
}
gpio_t spi_pin_mosi(spi_t bus)
{
assume(bus < SPI_NUMOF);
return spi_config[bus].mosi_pin;
}
gpio_t spi_pin_clk(spi_t bus)
{
assume(bus < SPI_NUMOF);
return spi_config[bus].sclk_pin;
} }
int spi_init_cs(spi_t bus, spi_cs_t cs) int spi_init_cs(spi_t bus, spi_cs_t cs)
@ -183,7 +222,7 @@ int spi_init_cs(spi_t bus, spi_cs_t cs)
#ifdef MODULE_PERIPH_SPI_GPIO_MODE #ifdef MODULE_PERIPH_SPI_GPIO_MODE
int spi_init_with_gpio_mode(spi_t bus, const spi_gpio_mode_t* mode) int spi_init_with_gpio_mode(spi_t bus, const spi_gpio_mode_t* mode)
{ {
assert(bus < SPI_NUMOF); assume(bus < SPI_NUMOF);
int ret = 0; int ret = 0;
@ -212,7 +251,7 @@ int spi_init_with_gpio_mode(spi_t bus, const spi_gpio_mode_t* mode)
void spi_acquire(spi_t bus, spi_cs_t cs, spi_mode_t mode, spi_clk_t clk) void spi_acquire(spi_t bus, spi_cs_t cs, spi_mode_t mode, spi_clk_t clk)
{ {
assert((unsigned)bus < SPI_NUMOF); assume((unsigned)bus < SPI_NUMOF);
/* lock bus */ /* lock bus */
mutex_lock(&locks[bus]); mutex_lock(&locks[bus]);
@ -379,7 +418,7 @@ void spi_transfer_bytes(spi_t bus, spi_cs_t cs, bool cont,
const void *out, void *in, size_t len) const void *out, void *in, size_t len)
{ {
/* make sure at least one input or one output buffer is given */ /* make sure at least one input or one output buffer is given */
assert(out || in); assume(out || in);
/* active the given chip select line */ /* active the given chip select line */
if ((cs != SPI_HWCS_MASK) && gpio_is_valid(cs)) { if ((cs != SPI_HWCS_MASK) && gpio_is_valid(cs)) {