1
0
mirror of https://github.com/esp8266/Arduino.git synced 2025-04-24 08:45:10 +03:00
Markus Sattler d7a88c3ea3 use a function to calculate best match clock register for SPI
TEST:

Frequency: 1000000  -> [0x009C1001] EQU: 0	 Pre: 39     N: 1	 H: 0	 L: 1	 - Back Calculated Frequency: 1000000
Frequency: 4000000  -> [0x00001001] EQU: 0	 Pre: 0	     N: 1	 H: 0	 L: 1	 - Back Calculated Frequency: 40000000
Frequency: 2000000  -> [0x00041001] EQU: 0	 Pre: 1	     N: 1	 H: 0	 L: 1	 - Back Calculated Frequency: 20000000
Frequency: 1600000  -> [0x00004002] EQU: 0	 Pre: 0	     N: 4	 H: 0	 L: 2	 - Back Calculated Frequency: 16000000
Frequency: 1610000  -> [0x00004002] EQU: 0	 Pre: 0	     N: 4	 H: 0	 L: 2	 - Back Calculated Frequency: 16000000
Frequency: 1000000  -> [0x000C1001] EQU: 0	 Pre: 3	     N: 1	 H: 0	 L: 1	 - Back Calculated Frequency: 10000000
Frequency: 8000000  -> [0x00101001] EQU: 0	 Pre: 4	     N: 1	 H: 0	 L: 1	 - Back Calculated Frequency: 8000000
Frequency: 5000000  -> [0x001C1001] EQU: 0	 Pre: 7	     N: 1	 H: 0	 L: 1	 - Back Calculated Frequency: 5000000
Frequency: 4000000  -> [0x00241001] EQU: 0	 Pre: 9	     N: 1	 H: 0	 L: 1	 - Back Calculated Frequency: 4000000
Frequency: 2000000  -> [0x004C1001] EQU: 0	 Pre: 19	 N: 1	 H: 0	 L: 1	 - Back Calculated Frequency: 2000000
Frequency: 1000000  -> [0x009C1001] EQU: 0	 Pre: 39	 N: 1	 H: 0	 L: 1	 - Back Calculated Frequency: 1000000
Frequency: 500000   -> [0x013C1001] EQU: 0	 Pre: 79	 N: 1	 H: 0	 L: 1	 - Back Calculated Frequency: 500000
Frequency: 250000   -> [0x027C1001] EQU: 0	 Pre: 159	 N: 1	 H: 0	 L: 1	 - Back Calculated Frequency: 250000
Frequency: 125000   -> [0x04FC1001] EQU: 0	 Pre: 319	 N: 1	 H: 0	 L: 1	 - Back Calculated Frequency: 125000
Frequency: 6666666  -> [0x00141001] EQU: 0	 Pre: 5	 	 N: 1	 H: 0	 L: 1	 - Back Calculated Frequency: 6666666
Frequency: 6000000  -> [0x00181001] EQU: 0	 Pre: 6	 	 N: 1	 H: 0	 L: 1	 - Back Calculated Frequency: 5714285
Frequency: 3000000  -> [0x00202001] EQU: 0	 Pre: 8	 	 N: 2	 H: 0	 L: 1	 - Back Calculated Frequency: 2962962
Frequency: 100	    -> [0x7FFFF020] EQU: 0	 Pre: 8191	 N: 63	 H: 0	 L: 32	 - Back Calculated Frequency: 152
Frequency: 125000   -> [0x04FC1001] EQU: 0	 Pre: 319	 N: 1	 H: 0	 L: 1	 - Back Calculated Frequency: 125000
Frequency: 16457    -> [0x25F81001] EQU: 0	 Pre: 2430	 N: 1	 H: 0	 L: 1	 - Back Calculated Frequency: 16454
2015-05-08 00:48:08 +02:00

225 lines
5.9 KiB
C++

/*
SPI.cpp - SPI library for esp8266
Copyright (c) 2015 Hristo Gochkov. All rights reserved.
This file is part of the esp8266 core for Arduino environment.
This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either
version 2.1 of the License, or (at your option) any later version.
This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public
License along with this library; if not, write to the Free Software
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*/
#include "SPI.h"
#include "HardwareSerial.h"
typedef union {
uint32_t regValue;
struct {
unsigned regL :6;
unsigned regH :6;
unsigned regN :6;
unsigned regPre :13;
unsigned regEQU :1;
};
} spiClk_t;
SPIClass SPI;
SPIClass::SPIClass() {
}
void SPIClass::begin() {
pinMode(SCK, SPECIAL); ///< GPIO14
pinMode(MISO, SPECIAL); ///< GPIO12
pinMode(MOSI, SPECIAL); ///< GPIO13
GPMUX = 0x105; // note crash if SPI flash Frequency < 40MHz
SPI1C = 0;
setFrequency(1000000); ///< 1MHz
SPI1U = SPIUMOSI | SPIUDUPLEX | SPIUSSE;
SPI1U1 = (7 << SPILMOSI) | (7 << SPILMISO);
SPI1C1 = 0;
}
void SPIClass::end() {
pinMode(SCK, INPUT);
pinMode(MISO, INPUT);
pinMode(MOSI, INPUT);
}
void SPIClass::beginTransaction(SPISettings settings) {
setFrequency(settings._clock);
setBitOrder(settings._bitOrder);
setDataMode(settings._dataMode);
}
void SPIClass::endTransaction() {
}
void SPIClass::setDataMode(uint8_t dataMode) {
/**
SPI_MODE0 0x00 - CPOL: 0 CPHA: 0
SPI_MODE1 0x01 - CPOL: 0 CPHA: 1
SPI_MODE2 0x10 - CPOL: 1 CPHA: 0
SPI_MODE3 0x11 - CPOL: 1 CPHA: 1
*/
bool CPOL = (dataMode & 0x10); ///< CPOL (Clock Polarity)
bool CPHA = (dataMode & 0x01); ///< CPHA (Clock Phase)
if(CPHA) {
SPI1U |= (SPIUSME);
} else {
SPI1U &= ~(SPIUSME);
}
if(CPOL) {
//todo How set CPOL???
}
}
void SPIClass::setBitOrder(uint8_t bitOrder) {
if(bitOrder == MSBFIRST) {
SPI1C &= ~(SPICWBO | SPICRBO);
} else {
SPI1C |= (SPICWBO | SPICRBO);
}
}
/**
* calculate the Frequency based on the register value
* @param reg
* @return
*/
static uint32_t ClkRegToFreq(spiClk_t * reg) {
return (F_CPU / ((reg->regPre + 1) * (reg->regN + 1)));
}
void SPIClass::setFrequency(uint32_t freq) {
static uint32_t lastSetFrequency = 0;
static uint32_t lastSetRegister = 0;
if(freq >= F_CPU) {
setClockDivider(0x80000000);
return;
}
if(lastSetFrequency == freq && lastSetRegister == SPI1CLK) {
// do nothing (speed optimization)
return;
}
const spiClk_t minFreqReg = { 0x7FFFF000 };
uint32_t minFreq = ClkRegToFreq((spiClk_t*) &minFreqReg);
if(freq < minFreq) {
freq = minFreq;
}
uint8_t calN = 1;
spiClk_t bestReg = { 0 };
int32_t bestFreq = 0;
// find the best match
while(calN <= 0x3F) { // 0x3F max for N
spiClk_t reg = { 0 };
int32_t calFreq;
int32_t calPre;
int8_t calPreVari = -2;
reg.regN = calN;
while(calPreVari++ <= 1) { // test different variants for Pre (we calculate in int so we miss the decimals, testing is the easyest and fastest way)
calPre = (((F_CPU / (reg.regN + 1)) / freq) - 1) + calPreVari;
if(calPre > 0x1FFF) {
reg.regPre = 0x1FFF; // 8191
} else if(calPre <= 0) {
reg.regPre = 0;
} else {
reg.regPre = calPre;
}
reg.regL = ((reg.regN + 1) / 2);
// reg.regH = (reg.regN - reg.regL);
// test calculation
calFreq = ClkRegToFreq(&reg);
//os_printf("-----[0x%08X][%d]\t EQU: %d\t Pre: %d\t N: %d\t H: %d\t L: %d = %d\n", reg.regValue, freq, reg.regEQU, reg.regPre, reg.regN, reg.regH, reg.regL, calFreq);
if(calFreq == (int32_t) freq) {
// accurate match use it!
memcpy(&bestReg, &reg, sizeof(bestReg));
break;
} else if(calFreq < (int32_t) freq) {
// never go over the requested frequency
if(abs(freq - calFreq) < abs(freq - bestFreq)) {
bestFreq = calFreq;
memcpy(&bestReg, &reg, sizeof(bestReg));
}
}
}
if(calFreq == (int32_t) freq) {
// accurate match use it!
break;
}
calN++;
}
// os_printf("[0x%08X][%d]\t EQU: %d\t Pre: %d\t N: %d\t H: %d\t L: %d\t - Real Frequency: %d\n", bestReg.regValue, freq, bestReg.regEQU, bestReg.regPre, bestReg.regN, bestReg.regH, bestReg.regL, ClkRegToFreq(&bestReg));
setClockDivider(bestReg.regValue);
lastSetRegister = SPI1CLK;
lastSetFrequency = freq;
}
void SPIClass::setClockDivider(uint32_t clockDiv) {
SPI1CLK = clockDiv;
}
uint8_t SPIClass::transfer(uint8_t data) {
while(SPI1CMD & SPIBUSY)
;
SPI1W0 = data;
SPI1CMD |= SPIBUSY;
while(SPI1CMD & SPIBUSY)
;
return (uint8_t) (SPI1W0 & 0xff);
}
uint16_t SPIClass::transfer16(uint16_t data) {
union {
uint16_t val;
struct {
uint8_t lsb;
uint8_t msb;
};
} in, out;
in.val = data;
if((SPI1C & (SPICWBO | SPICRBO))) {
//MSBFIRST
out.msb = transfer(in.msb);
out.lsb = transfer(in.lsb);
} else {
//LSBFIRST
out.lsb = transfer(in.lsb);
out.msb = transfer(in.msb);
}
return out.val;
}