Skip to content

Fix SPI::setClockDivider implementation #379

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Dec 21, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 8 additions & 26 deletions libraries/SPI/src/SPI.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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)
{
Expand All @@ -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,
Expand Down
7 changes: 4 additions & 3 deletions libraries/SPI/src/SPI.h
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down