Skip to content

Commit

Permalink
Merge pull request #536 from coddingtonbear/fix_spi_config_gpios_rema…
Browse files Browse the repository at this point in the history
…pped

Configure correct set of pins during SPI initialization  when using alternate SPI pins.
  • Loading branch information
stevstrong authored Feb 4, 2020
2 parents d8c83f0 + 6d6b87f commit 683e5a3
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 1 deletion.
9 changes: 9 additions & 0 deletions STM32F1/cores/maple/libmaple/gpio_f1.c
Original file line number Diff line number Diff line change
Expand Up @@ -180,3 +180,12 @@ void afio_remap(afio_remap_peripheral remapping) {
AFIO_BASE->MAPR |= remapping;
}
}

int8_t afio_is_remapped(afio_remap_peripheral remapping) {
if (remapping & AFIO_REMAP_USE_MAPR2) {
remapping &= ~AFIO_REMAP_USE_MAPR2;
return AFIO_BASE->MAPR2 & remapping;
} else {
return AFIO_BASE->MAPR & remapping;
}
}
17 changes: 16 additions & 1 deletion STM32F1/libraries/SPI/src/SPI.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,15 @@ static const spi_pins board_spi_pins[] __FLASH__ = {
#endif
};

static const spi_pins board_alt_spi_pins[] __FLASH__ = {
#if defined(BOARD_SPI1_ALT_MOSI_PIN)
{BOARD_SPI1_ALT_NSS_PIN,
BOARD_SPI1_ALT_SCK_PIN,
BOARD_SPI1_ALT_MISO_PIN,
BOARD_SPI1_ALT_MOSI_PIN}
#endif
};

#if BOARD_NR_SPI >= 1
static void (*_spi1_this);
#endif
Expand Down Expand Up @@ -711,7 +720,13 @@ void SPIClass::_spi3EventCallback() {
static const spi_pins* dev_to_spi_pins(spi_dev *dev) {
switch (dev->clk_id) {
#if BOARD_NR_SPI >= 1
case RCC_SPI1: return board_spi_pins;
case RCC_SPI1:
#if defined(BOARD_SPI1_ALT_MOSI_PIN)
if(afio_is_remapped(AFIO_REMAP_SPI1)) {
return board_alt_spi_pins;
}
#endif
return board_spi_pins;
#endif
#if BOARD_NR_SPI >= 2
case RCC_SPI2: return board_spi_pins + 1;
Expand Down
1 change: 1 addition & 0 deletions STM32F1/system/libmaple/stm32f1/include/series/gpio.h
Original file line number Diff line number Diff line change
Expand Up @@ -380,6 +380,7 @@ typedef enum afio_remap_peripheral {
} afio_remap_peripheral;

void afio_remap(afio_remap_peripheral p);
int8_t afio_is_remapped(afio_remap_peripheral p);

/**
* @brief Debug port configuration
Expand Down
5 changes: 5 additions & 0 deletions STM32F1/variants/generic_stm32f103c/board/board.h
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,11 @@
#define BOARD_SPI1_MISO_PIN PA6
#define BOARD_SPI1_SCK_PIN PA5

#define BOARD_SPI1_ALT_NSS_PIN PA15
#define BOARD_SPI1_ALT_MOSI_PIN PB5
#define BOARD_SPI1_ALT_MISO_PIN PB4
#define BOARD_SPI1_ALT_SCK_PIN PB3

#define BOARD_SPI2_NSS_PIN PB12
#define BOARD_SPI2_MOSI_PIN PB15
#define BOARD_SPI2_MISO_PIN PB14
Expand Down

0 comments on commit 683e5a3

Please sign in to comment.