From ac3643e0061045206dea0630132b73c842ee9416 Mon Sep 17 00:00:00 2001 From: Matthijs Kooijman Date: Tue, 10 Dec 2019 16:52:28 +0100 Subject: [PATCH] Fix SPI pin matching check This looks up the SPI module corresponding to each of pins passed and then uses `pinmap_merge_peripheral()` to merge this in a single pin. If not all pins map to the same SPI module, this should abort. The `pinmap_merge_peripheral` function returns NP to indicate a mismatch between the passed peripherals. However, when one of the arguments is already NP, this is not propagated into the function result, instead the other argument is simplly returned. In this case, the merging happens in two levels: First MISO and MOSI are merged, then SCK and SS are merged and finally the merged results are again merged. That means that if MISO and MISO mismatch, but SCK and SS match (or the other way around), the NP returned due to the mismatch is ignored and the final result is not NP, so no error is raised. This changes the code to also check the intermediate merge results for NP, to properly detect mismatches. --- libraries/SPI/src/utility/spi_com.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/libraries/SPI/src/utility/spi_com.c b/libraries/SPI/src/utility/spi_com.c index 7a1fa006eb..b324ebebe1 100644 --- a/libraries/SPI/src/utility/spi_com.c +++ b/libraries/SPI/src/utility/spi_com.c @@ -181,7 +181,7 @@ void spi_init(spi_t *obj, uint32_t speed, spi_mode_e mode, uint8_t msb) obj->spi = pinmap_merge_peripheral(spi_data, spi_cntl); // Are all pins connected to the same SPI instance? - if (obj->spi == NP) { + if (spi_data == NP || spi_cntl == NP || obj->spi == NP) { core_debug("ERROR: SPI pins mismatch\n"); return; }