From 45b7981ec12e7ad5c3dbdf5d4ddf6820bf9d98f6 Mon Sep 17 00:00:00 2001 From: klassents Date: Wed, 25 Dec 2024 09:43:48 +0700 Subject: [PATCH] =?UTF-8?q?=D0=B4=D0=BE=D0=B1=D0=B0=D0=B2=D0=BB=D0=B5?= =?UTF-8?q?=D0=BD=20=D1=852=20=D0=B4=D0=B5=D0=BB=D0=B8=D1=82=D0=B5=D0=BB?= =?UTF-8?q?=D1=8C=20=D1=81=D0=BA=D0=BE=D1=80=D0=BE=D1=81=D1=82=D0=B8=20spi?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- libraries/SPI/keywords.txt | 1 + libraries/SPI/src/SPI.cpp | 14 +++++++------- libraries/SPI/src/SPI.h | 1 + 3 files changed, 9 insertions(+), 7 deletions(-) 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