From c5dd1947c017a1b63b9fff697576ccc555f58529 Mon Sep 17 00:00:00 2001 From: "Frederic.Pillon" Date: Fri, 23 Nov 2018 17:57:01 +0100 Subject: [PATCH] Fix SPI::setClockDivider implementation As said in Arduino API: https://www.arduino.cc/en/Reference/SPISetClockDivider "On the Due, the system clock can be divided by values from 1 to 255. The default value is 21, which sets the clock to 4 MHz like other Arduino boards." So in fact, it should be possible to divide the clock by any value. Then HAL will use the best prescaler to fit requested Freq. Moreover, it is not possible to define SPI_CLOCK_DIVx to match the 16MHz value for avr as SPI frequency depends of system clock configuration. Fix #299 Signed-off-by: Frederic.Pillon --- libraries/SPI/src/SPI.cpp | 34 ++++++++-------------------------- libraries/SPI/src/SPI.h | 7 ++++--- 2 files changed, 12 insertions(+), 29 deletions(-) diff --git a/libraries/SPI/src/SPI.cpp b/libraries/SPI/src/SPI.cpp index f77cd23c67..c07863aaa1 100644 --- a/libraries/SPI/src/SPI.cpp +++ b/libraries/SPI/src/SPI.cpp @@ -220,16 +220,10 @@ void SPIClass::setDataMode(uint8_t _pin, uint8_t _mode) /** * @brief Deprecated function. - * Configure the clock speed: 125kHz to 8MHz. + * Configure the clock speed * @param _pin: CS pin associated to a configuration (optional). - * @param _divider: can be one of the following parameters: - * SPI_CLOCK_DIV2 (8MHz) - * SPI_CLOCK_DIV4 (4MHz) - * SPI_CLOCK_DIV8 (2MHz) - * SPI_CLOCK_DIV16 (1MHz) - * SPI_CLOCK_DIV32 (500kHz) - * SPI_CLOCK_DIV64 (250kHz) - * SPI_CLOCK_DIV128 (125kHz) + * @param _divider: the SPI clock can be divided by values from 1 to 255. + * If 0, default SPI speed is used. */ void SPIClass::setClockDivider(uint8_t _pin, uint8_t _divider) { @@ -240,23 +234,11 @@ void SPIClass::setClockDivider(uint8_t _pin, uint8_t _divider) if(idx >= NB_SPI_SETTINGS) { return; } - - /* Get clk freq of the SPI instance */ - uint32_t spiClkFreq = spi_getClkFreq(&_spi); - - switch(_divider) { - case (SPI_CLOCK_DIV2) : - case (SPI_CLOCK_DIV4) : - case (SPI_CLOCK_DIV8) : - case (SPI_CLOCK_DIV16) : - case (SPI_CLOCK_DIV32) : - case (SPI_CLOCK_DIV64) : - case (SPI_CLOCK_DIV128) : - spiSettings[idx].clk = spiClkFreq/_divider; - break; - default: - spiSettings[idx].clk = SPI_SPEED_CLOCK_DEFAULT; - break; + if (_divider == 0) { + spiSettings[idx].clk = SPI_SPEED_CLOCK_DEFAULT; + } else { + /* Get clk freq of the SPI instance and compute it */ + spiSettings[idx].clk = spi_getClkFreq(&_spi)/_divider; } spi_init(&_spi, spiSettings[idx].clk, diff --git a/libraries/SPI/src/SPI.h b/libraries/SPI/src/SPI.h index b69dc194ac..a0760b9b98 100644 --- a/libraries/SPI/src/SPI.h +++ b/libraries/SPI/src/SPI.h @@ -22,9 +22,10 @@ // - SPISetting(clock, bitOrder, dataMode) #define SPI_HAS_TRANSACTION 1 -// For compatibility with sketches designed for AVR @ 16 MHz -// need to go from 64MHz to 16 (/4) -// This function should not be used in new projects. +// Compatibility with sketches designed for AVR @ 16 MHz could not +// be ensured as SPI frequency depends of system clock configuration. +// user have to use appropriate divider for the SPI clock +// This function should not be used in new project. // Use SPISettings with SPI.beginTransaction() to configure SPI parameters. #define SPI_CLOCK_DIV2 2 #define SPI_CLOCK_DIV4 4