Skip to content

Commit 7b48eff

Browse files
authored
Merge pull request #379 from fpistm/SPI_clock_div
Fix SPI::setClockDivider implementation
2 parents 18b8b10 + c5dd194 commit 7b48eff

File tree

2 files changed

+12
-29
lines changed

2 files changed

+12
-29
lines changed

Diff for: 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,

Diff for: 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)