Skip to content

Commit c5dd194

Browse files
committed
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 stm32duino#299 Signed-off-by: Frederic.Pillon <[email protected]>
1 parent 18b8b10 commit c5dd194

File tree

2 files changed

+12
-29
lines changed

2 files changed

+12
-29
lines changed

libraries/SPI/src/SPI.cpp

+8-26
Original file line numberDiff line numberDiff line change
@@ -220,16 +220,10 @@ void SPIClass::setDataMode(uint8_t _pin, uint8_t _mode)
220220

221221
/**
222222
* @brief Deprecated function.
223-
* Configure the clock speed: 125kHz to 8MHz.
223+
* Configure the clock speed
224224
* @param _pin: CS pin associated to a configuration (optional).
225-
* @param _divider: can be one of the following parameters:
226-
* SPI_CLOCK_DIV2 (8MHz)
227-
* SPI_CLOCK_DIV4 (4MHz)
228-
* SPI_CLOCK_DIV8 (2MHz)
229-
* SPI_CLOCK_DIV16 (1MHz)
230-
* SPI_CLOCK_DIV32 (500kHz)
231-
* SPI_CLOCK_DIV64 (250kHz)
232-
* SPI_CLOCK_DIV128 (125kHz)
225+
* @param _divider: the SPI clock can be divided by values from 1 to 255.
226+
* If 0, default SPI speed is used.
233227
*/
234228
void SPIClass::setClockDivider(uint8_t _pin, uint8_t _divider)
235229
{
@@ -240,23 +234,11 @@ void SPIClass::setClockDivider(uint8_t _pin, uint8_t _divider)
240234
if(idx >= NB_SPI_SETTINGS) {
241235
return;
242236
}
243-
244-
/* Get clk freq of the SPI instance */
245-
uint32_t spiClkFreq = spi_getClkFreq(&_spi);
246-
247-
switch(_divider) {
248-
case (SPI_CLOCK_DIV2) :
249-
case (SPI_CLOCK_DIV4) :
250-
case (SPI_CLOCK_DIV8) :
251-
case (SPI_CLOCK_DIV16) :
252-
case (SPI_CLOCK_DIV32) :
253-
case (SPI_CLOCK_DIV64) :
254-
case (SPI_CLOCK_DIV128) :
255-
spiSettings[idx].clk = spiClkFreq/_divider;
256-
break;
257-
default:
258-
spiSettings[idx].clk = SPI_SPEED_CLOCK_DEFAULT;
259-
break;
237+
if (_divider == 0) {
238+
spiSettings[idx].clk = SPI_SPEED_CLOCK_DEFAULT;
239+
} else {
240+
/* Get clk freq of the SPI instance and compute it */
241+
spiSettings[idx].clk = spi_getClkFreq(&_spi)/_divider;
260242
}
261243

262244
spi_init(&_spi, spiSettings[idx].clk,

libraries/SPI/src/SPI.h

+4-3
Original file line numberDiff line numberDiff line change
@@ -22,9 +22,10 @@
2222
// - SPISetting(clock, bitOrder, dataMode)
2323
#define SPI_HAS_TRANSACTION 1
2424

25-
// For compatibility with sketches designed for AVR @ 16 MHz
26-
// need to go from 64MHz to 16 (/4)
27-
// This function should not be used in new projects.
25+
// Compatibility with sketches designed for AVR @ 16 MHz could not
26+
// be ensured as SPI frequency depends of system clock configuration.
27+
// user have to use appropriate divider for the SPI clock
28+
// This function should not be used in new project.
2829
// Use SPISettings with SPI.beginTransaction() to configure SPI parameters.
2930
#define SPI_CLOCK_DIV2 2
3031
#define SPI_CLOCK_DIV4 4

0 commit comments

Comments
 (0)