Skip to content

Commit a372da1

Browse files
committed
fix SPI speed calculation @160Mhz Clock
1 parent 2cf1772 commit a372da1

File tree

2 files changed

+5
-3
lines changed

2 files changed

+5
-3
lines changed

hardware/esp8266com/esp8266/libraries/SPI/SPI.cpp

+3-3
Original file line numberDiff line numberDiff line change
@@ -121,14 +121,14 @@ void SPIClass::setBitOrder(uint8_t bitOrder) {
121121
* @return
122122
*/
123123
static uint32_t ClkRegToFreq(spiClk_t * reg) {
124-
return (F_CPU / ((reg->regPre + 1) * (reg->regN + 1)));
124+
return (SPI_MAX_SPEED / ((reg->regPre + 1) * (reg->regN + 1)));
125125
}
126126

127127
void SPIClass::setFrequency(uint32_t freq) {
128128
static uint32_t lastSetFrequency = 0;
129129
static uint32_t lastSetRegister = 0;
130130

131-
if(freq >= F_CPU) {
131+
if(freq >= SPI_MAX_SPEED) {
132132
setClockDivider(0x80000000);
133133
return;
134134
}
@@ -164,7 +164,7 @@ void SPIClass::setFrequency(uint32_t freq) {
164164
reg.regN = calN;
165165

166166
while(calPreVari++ <= 1) { // test different variants for Pre (we calculate in int so we miss the decimals, testing is the easyest and fastest way)
167-
calPre = (((F_CPU / (reg.regN + 1)) / freq) - 1) + calPreVari;
167+
calPre = (((SPI_MAX_SPEED / (reg.regN + 1)) / freq) - 1) + calPreVari;
168168
if(calPre > 0x1FFF) {
169169
reg.regPre = 0x1FFF; // 8191
170170
} else if(calPre <= 0) {

hardware/esp8266com/esp8266/libraries/SPI/SPI.h

+2
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,8 @@
4545
#define SPI_CLOCK_DIV64 0x04fc1001 //250 KHz
4646
#endif
4747

48+
#define SPI_MAX_SPEED (80000000L)
49+
4850
const uint8_t SPI_MODE0 = 0x00; ///< CPOL: 0 CPHA: 0
4951
const uint8_t SPI_MODE1 = 0x01; ///< CPOL: 0 CPHA: 1
5052
const uint8_t SPI_MODE2 = 0x10; ///< CPOL: 1 CPHA: 0

0 commit comments

Comments
 (0)