Skip to content

Commit adb1132

Browse files
committed
Fix SPI::setClockDivider implementation
Fix stm32duino#299 Signed-off-by: Frederic.Pillon <[email protected]>
1 parent 0b05ea3 commit adb1132

File tree

2 files changed

+10
-27
lines changed

2 files changed

+10
-27
lines changed

libraries/SPI/src/SPI.cpp

+6-24
Original file line numberDiff line numberDiff line change
@@ -220,16 +220,9 @@ 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
233226
*/
234227
void SPIClass::setClockDivider(uint8_t _pin, uint8_t _divider)
235228
{
@@ -240,24 +233,13 @@ void SPIClass::setClockDivider(uint8_t _pin, uint8_t _divider)
240233
if(idx >= NB_SPI_SETTINGS) {
241234
return;
242235
}
243-
236+
if (_divider == 0) {
237+
return;
238+
}
244239
/* Get clk freq of the SPI instance */
245240
uint32_t spiClkFreq = spi_getClkFreq(&_spi);
246241

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;
260-
}
242+
spiSettings[idx].clk = spiClkFreq/_divider;
261243

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

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)