diff --git a/libraries/SPI/keywords.txt b/libraries/SPI/keywords.txt index 1906b7f..99c75df 100644 --- a/libraries/SPI/keywords.txt +++ b/libraries/SPI/keywords.txt @@ -24,6 +24,7 @@ setClockDivider KEYWORD2 ####################################### # Constants (LITERAL1) ####################################### +SPI_CLOCK_DIV2 LITERAL1 SPI_CLOCK_DIV4 LITERAL1 SPI_CLOCK_DIV8 LITERAL1 SPI_CLOCK_DIV16 LITERAL1 diff --git a/libraries/SPI/src/SPI.cpp b/libraries/SPI/src/SPI.cpp index afa308e..1670372 100644 --- a/libraries/SPI/src/SPI.cpp +++ b/libraries/SPI/src/SPI.cpp @@ -21,14 +21,14 @@ void SPISettings::spiUpdateSettings(uint32_t speedMaximum, uint8_t dataOrder, ui // Find the fastest clock that is less than or equal to the // given clock rate. If nothing is slow enough - use the slowest. // mik32v2 has the set of deviders, that can be calculate as: - // div = 2 << (1...7). Value in braсkets is a value for config register + // div = 2 << (0...7). Value in braсkets is a value for config register uint8_t divRegVal = 0; // start from minimal divider (maximum speed) - while(divRegVal < 7) + while(divRegVal <= 7) { - divRegVal++; // values from 1 to 7 if ((F_CPU/(2 << divRegVal)) <= speedMaximum) // find suitable divider break; + divRegVal++; } // if break didn't call in cycle, it will be the greatest divRegVal (and divider) @@ -270,10 +270,10 @@ void SPIClass::setClockDivider(uint8_t clockDiv) if (spiInUse) { // if divider is valid - if ((clockDiv == SPI_CLOCK_DIV4) || (clockDiv == SPI_CLOCK_DIV8) || - (clockDiv == SPI_CLOCK_DIV16) || (clockDiv == SPI_CLOCK_DIV32) || - (clockDiv == SPI_CLOCK_DIV64) || (clockDiv == SPI_CLOCK_DIV128) || - (clockDiv == SPI_CLOCK_DIV256)) + if ((clockDiv == SPI_CLOCK_DIV2) || (clockDiv == SPI_CLOCK_DIV4) || + (clockDiv == SPI_CLOCK_DIV8) || (clockDiv == SPI_CLOCK_DIV16) || + (clockDiv == SPI_CLOCK_DIV32) || (clockDiv == SPI_CLOCK_DIV64) || + (clockDiv == SPI_CLOCK_DIV128) || (clockDiv == SPI_CLOCK_DIV256)) { hspi.Init.BaudRateDiv = clockDiv; currentSpeed = F_CPU >> (clockDiv+1); diff --git a/libraries/SPI/src/SPI.h b/libraries/SPI/src/SPI.h index 91ae176..b54e6b2 100644 --- a/libraries/SPI/src/SPI.h +++ b/libraries/SPI/src/SPI.h @@ -34,6 +34,7 @@ // dividers for setClockDivider() +#define SPI_CLOCK_DIV2 0x00 // 16 MHz #define SPI_CLOCK_DIV4 0x01 // 8 MHz #define SPI_CLOCK_DIV8 0x02 // 4 MHz #define SPI_CLOCK_DIV16 0x03 // 2 MHz