From 0d760e0bcb272d1c2cee800b3b2c851e83959128 Mon Sep 17 00:00:00 2001 From: Frederic Pillon Date: Mon, 4 Nov 2024 16:29:46 +0100 Subject: [PATCH 1/2] refactor(spi): simplify constructor Signed-off-by: Frederic Pillon --- libraries/SPI/src/SPI.cpp | 26 +++++++++----------------- libraries/SPI/src/SPI.h | 3 +-- 2 files changed, 10 insertions(+), 19 deletions(-) diff --git a/libraries/SPI/src/SPI.cpp b/libraries/SPI/src/SPI.cpp index 2c6e798b33..98bfee234e 100644 --- a/libraries/SPI/src/SPI.cpp +++ b/libraries/SPI/src/SPI.cpp @@ -14,26 +14,18 @@ SPIClass SPI; /** - * @brief Default constructor. Uses pin configuration of variant.h. - */ -SPIClass::SPIClass() -{ - _spi.pin_miso = digitalPinToPinName(MISO); - _spi.pin_mosi = digitalPinToPinName(MOSI); - _spi.pin_sclk = digitalPinToPinName(SCK); - _spi.pin_ssel = NC; -} - -/** - * @brief Constructor to create another SPI instance attached to another SPI - * peripheral different of the default SPI. All pins must be attached to - * the same SPI peripheral. See datasheet of the microcontroller. + * @brief Default Constructor. Uses pin configuration of default SPI + * defined in the variant*.h. + * To create another SPI instance attached to another SPI + * peripheral gave the pins as parameters to the constructor. + * @note All pins must be attached to the same SPI peripheral. + * See datasheet of the microcontroller. * @param mosi: SPI mosi pin. Accepted format: number or Arduino format (Dx) - * or ST format (Pxy). + * or ST format (Pxy). Default is MOSI pin of the default SPI peripheral. * @param miso: SPI miso pin. Accepted format: number or Arduino format (Dx) - * or ST format (Pxy). + * or ST format (Pxy). Default is MISO pin of the default SPI peripheral. * @param sclk: SPI clock pin. Accepted format: number or Arduino format (Dx) - * or ST format (Pxy). + * or ST format (Pxy). Default is SCK pin of the default SPI peripheral. * @param ssel: SPI ssel pin (optional). Accepted format: number or * Arduino format (Dx) or ST format (Pxy). By default is set to NC. * This pin must correspond to a hardware CS pin which can be managed diff --git a/libraries/SPI/src/SPI.h b/libraries/SPI/src/SPI.h index 1105991dc7..21f8cf6e8f 100644 --- a/libraries/SPI/src/SPI.h +++ b/libraries/SPI/src/SPI.h @@ -84,8 +84,7 @@ class SPISettings { class SPIClass { public: - SPIClass(); - SPIClass(uint32_t mosi, uint32_t miso, uint32_t sclk, uint32_t ssel = PNUM_NOT_DEFINED); + SPIClass(uint32_t mosi = MISO, uint32_t miso = MOSI, uint32_t sclk = SCK, uint32_t ssel = PNUM_NOT_DEFINED); // setMISO/MOSI/SCLK/SSEL have to be called before begin() void setMISO(uint32_t miso) From 22157dbe6436d7d731883313fdd729eff3fab966 Mon Sep 17 00:00:00 2001 From: Frederic Pillon Date: Mon, 4 Nov 2024 16:31:02 +0100 Subject: [PATCH 2/2] fix(spi): ensure spi_t structure properly init Fixes #2543 Signed-off-by: Frederic Pillon --- libraries/SPI/src/SPI.cpp | 1 + 1 file changed, 1 insertion(+) diff --git a/libraries/SPI/src/SPI.cpp b/libraries/SPI/src/SPI.cpp index 98bfee234e..f77b48c59b 100644 --- a/libraries/SPI/src/SPI.cpp +++ b/libraries/SPI/src/SPI.cpp @@ -37,6 +37,7 @@ SPIClass SPI; */ SPIClass::SPIClass(uint32_t mosi, uint32_t miso, uint32_t sclk, uint32_t ssel) { + memset((void *)&_spi, 0, sizeof(_spi)); _spi.pin_miso = digitalPinToPinName(miso); _spi.pin_mosi = digitalPinToPinName(mosi); _spi.pin_sclk = digitalPinToPinName(sclk);