Skip to content

Commit 0dc7599

Browse files
author
ficeto
committed
Merge pull request #15 from esp8266/esp8266
pull latest changes
2 parents 7e0a104 + e1b8017 commit 0dc7599

File tree

7 files changed

+211
-71
lines changed

7 files changed

+211
-71
lines changed

Diff for: README.md

+2-1
Original file line numberDiff line numberDiff line change
@@ -132,7 +132,8 @@ else they default to pins 4(SDA) and 5(SCL).
132132

133133
#### SPI ####
134134

135-
SPI library supports the entire Arduino SPI API including transactions, including setting phase and polarity.
135+
SPI library supports the entire Arduino SPI API including transactions, including setting phase (CPHA).
136+
Setting the Clock polarity (CPOL) is not supported, yet (SPI_MODE2 and SPI_MODE3 not working).
136137

137138
#### ESP-specific APIs ####
138139

Diff for: cores/esp8266/Arduino.h

+11-2
Original file line numberDiff line numberDiff line change
@@ -124,8 +124,17 @@ void timer1_write(uint32_t ticks); //maximum ticks 8388607
124124
void ets_intr_lock();
125125
void ets_intr_unlock();
126126

127-
#define interrupts() ets_intr_unlock();
128-
#define noInterrupts() ets_intr_lock();
127+
// level (0-15),
128+
// level 15 will disable ALL interrupts,
129+
// level 0 will disable most software interrupts
130+
//
131+
#define xt_disable_interrupts(state, level) __asm__ __volatile__("rsil %0," __STRINGIFY(level) "; esync; isync; dsync" : "=a" (state))
132+
#define xt_enable_interrupts(state) __asm__ __volatile__("wsr %0,ps; esync" :: "a" (state) : "memory")
133+
134+
extern uint32_t interruptsState;
135+
136+
#define interrupts() xt_enable_interrupts(interruptsState)
137+
#define noInterrupts() xt_disable_interrupts(interruptsState, 15)
129138

130139
#define clockCyclesPerMicrosecond() ( F_CPU / 1000000L )
131140
#define clockCyclesToMicroseconds(a) ( (a) / clockCyclesPerMicrosecond() )

Diff for: cores/esp8266/Esp.cpp

+7
Original file line numberDiff line numberDiff line change
@@ -227,6 +227,13 @@ FlashMode_t EspClass::getFlashChipMode(void)
227227
*/
228228
uint32_t EspClass::getFlashChipSizeByChipId(void) {
229229
uint32_t chipId = getFlashChipId();
230+
/**
231+
* Chip ID
232+
* 00 - always 00 (Chip ID use only 3 byte)
233+
* 17 - ? looks like 2^xx is size in Byte ? //todo: find docu to this
234+
* 40 - ? may be Speed ? //todo: find docu to this
235+
* C8 - manufacturer ID
236+
*/
230237
switch(chipId) {
231238

232239
// GigaDevice

Diff for: cores/esp8266/Esp.h

+8
Original file line numberDiff line numberDiff line change
@@ -95,8 +95,16 @@ class EspClass {
9595
FlashMode_t getFlashChipMode(void);
9696
uint32_t getFlashChipSizeByChipId(void);
9797

98+
inline uint32_t getCycleCount(void);
9899
};
99100

101+
uint32_t EspClass::getCycleCount(void)
102+
{
103+
uint32_t ccount;
104+
__asm__ __volatile__("rsr %0,ccount":"=a" (ccount));
105+
return ccount;
106+
}
107+
100108
extern EspClass ESP;
101109

102110
#endif //ESP_H

Diff for: cores/esp8266/core_esp8266_wiring_digital.c

+3
Original file line numberDiff line numberDiff line change
@@ -139,6 +139,9 @@ extern void __detachInterrupt(uint8_t pin) {
139139
}
140140
}
141141

142+
// stored state for the noInterrupts/interrupts methods
143+
uint32_t interruptsState = 0;
144+
142145
void initPins() {
143146
//Disable UART interrupts
144147
system_set_os_print(0);

Diff for: libraries/SPI/SPI.cpp

+171-47
Original file line numberDiff line numberDiff line change
@@ -1,79 +1,203 @@
11
/*
2-
SPI.cpp - SPI library for esp8266
2+
SPI.cpp - SPI library for esp8266
33
4-
Copyright (c) 2015 Hristo Gochkov. All rights reserved.
5-
This file is part of the esp8266 core for Arduino environment.
4+
Copyright (c) 2015 Hristo Gochkov. All rights reserved.
5+
This file is part of the esp8266 core for Arduino environment.
66
7-
This library is free software; you can redistribute it and/or
8-
modify it under the terms of the GNU Lesser General Public
9-
License as published by the Free Software Foundation; either
10-
version 2.1 of the License, or (at your option) any later version.
7+
This library is free software; you can redistribute it and/or
8+
modify it under the terms of the GNU Lesser General Public
9+
License as published by the Free Software Foundation; either
10+
version 2.1 of the License, or (at your option) any later version.
1111
12-
This library is distributed in the hope that it will be useful,
13-
but WITHOUT ANY WARRANTY; without even the implied warranty of
14-
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15-
Lesser General Public License for more details.
12+
This library is distributed in the hope that it will be useful,
13+
but WITHOUT ANY WARRANTY; without even the implied warranty of
14+
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15+
Lesser General Public License for more details.
1616
17-
You should have received a copy of the GNU Lesser General Public
18-
License along with this library; if not, write to the Free Software
19-
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
20-
*/
17+
You should have received a copy of the GNU Lesser General Public
18+
License along with this library; if not, write to the Free Software
19+
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
20+
*/
2121

2222
#include "SPI.h"
23+
#include "HardwareSerial.h"
24+
25+
typedef union {
26+
uint32_t regValue;
27+
struct {
28+
unsigned regL :6;
29+
unsigned regH :6;
30+
unsigned regN :6;
31+
unsigned regPre :13;
32+
unsigned regEQU :1;
33+
};
34+
} spiClk_t;
2335

2436
SPIClass SPI;
2537

26-
SPIClass::SPIClass(){}
27-
28-
void SPIClass::begin(){
29-
pinMode(SCK, SPECIAL);
30-
pinMode(MISO, SPECIAL);
31-
pinMode(MOSI, SPECIAL);
32-
33-
GPMUX = 0x105;
34-
SPI1C = 0;
35-
SPI1CLK = SPI_CLOCK_DIV16;//1MHz
36-
SPI1U = SPIUMOSI | SPIUDUPLEX | SPIUSSE;
37-
SPI1U1 = (7 << SPILMOSI) | (7 << SPILMISO);
38-
SPI1C1 = 0;
38+
SPIClass::SPIClass() {
39+
}
40+
41+
void SPIClass::begin() {
42+
pinMode(SCK, SPECIAL); ///< GPIO14
43+
pinMode(MISO, SPECIAL); ///< GPIO12
44+
pinMode(MOSI, SPECIAL); ///< GPIO13
45+
46+
GPMUX = 0x105; // note crash if SPI flash Frequency < 40MHz
47+
SPI1C = 0;
48+
setFrequency(1000000); ///< 1MHz
49+
SPI1U = SPIUMOSI | SPIUDUPLEX | SPIUSSE;
50+
SPI1U1 = (7 << SPILMOSI) | (7 << SPILMISO);
51+
SPI1C1 = 0;
3952
}
4053

4154
void SPIClass::end() {
42-
pinMode(SCK, INPUT);
43-
pinMode(MISO, INPUT);
44-
pinMode(MOSI, INPUT);
55+
pinMode(SCK, INPUT);
56+
pinMode(MISO, INPUT);
57+
pinMode(MOSI, INPUT);
4558
}
4659

4760
void SPIClass::beginTransaction(SPISettings settings) {
48-
setClockDivider(settings._clock);
49-
setBitOrder(settings._bitOrder);
50-
setDataMode(settings._dataMode);
61+
setFrequency(settings._clock);
62+
setBitOrder(settings._bitOrder);
63+
setDataMode(settings._dataMode);
5164
}
5265

53-
void SPIClass::endTransaction() {}
66+
void SPIClass::endTransaction() {
67+
}
5468

5569
void SPIClass::setDataMode(uint8_t dataMode) {
56-
70+
71+
/**
72+
SPI_MODE0 0x00 - CPOL: 0 CPHA: 0
73+
SPI_MODE1 0x01 - CPOL: 0 CPHA: 1
74+
SPI_MODE2 0x10 - CPOL: 1 CPHA: 0
75+
SPI_MODE3 0x11 - CPOL: 1 CPHA: 1
76+
*/
77+
78+
bool CPOL = (dataMode & 0x10); ///< CPOL (Clock Polarity)
79+
bool CPHA = (dataMode & 0x01); ///< CPHA (Clock Phase)
80+
81+
if(CPHA) {
82+
SPI1U |= (SPIUSME);
83+
} else {
84+
SPI1U &= ~(SPIUSME);
85+
}
86+
87+
if(CPOL) {
88+
//todo How set CPOL???
89+
}
90+
5791
}
5892

5993
void SPIClass::setBitOrder(uint8_t bitOrder) {
60-
if (bitOrder == MSBFIRST) {
61-
SPI1C &= ~(SPICWBO | SPICRBO);
62-
} else {
63-
SPI1C |= (SPICWBO | SPICRBO);
64-
}
94+
if(bitOrder == MSBFIRST) {
95+
SPI1C &= ~(SPICWBO | SPICRBO);
96+
} else {
97+
SPI1C |= (SPICWBO | SPICRBO);
98+
}
99+
}
100+
101+
/**
102+
* calculate the Frequency based on the register value
103+
* @param reg
104+
* @return
105+
*/
106+
static uint32_t ClkRegToFreq(spiClk_t * reg) {
107+
return (F_CPU / ((reg->regPre + 1) * (reg->regN + 1)));
108+
}
109+
110+
void SPIClass::setFrequency(uint32_t freq) {
111+
static uint32_t lastSetFrequency = 0;
112+
static uint32_t lastSetRegister = 0;
113+
114+
if(freq >= F_CPU) {
115+
setClockDivider(0x80000000);
116+
return;
117+
}
118+
119+
if(lastSetFrequency == freq && lastSetRegister == SPI1CLK) {
120+
// do nothing (speed optimization)
121+
return;
122+
}
123+
124+
const spiClk_t minFreqReg = { 0x7FFFF000 };
125+
uint32_t minFreq = ClkRegToFreq((spiClk_t*) &minFreqReg);
126+
if(freq < minFreq) {
127+
freq = minFreq;
128+
}
129+
130+
uint8_t calN = 1;
131+
132+
spiClk_t bestReg = { 0 };
133+
int32_t bestFreq = 0;
134+
135+
// find the best match
136+
while(calN <= 0x3F) { // 0x3F max for N
137+
138+
spiClk_t reg = { 0 };
139+
int32_t calFreq;
140+
int32_t calPre;
141+
int8_t calPreVari = -2;
142+
143+
reg.regN = calN;
144+
145+
while(calPreVari++ <= 1) { // test different variants for Pre (we calculate in int so we miss the decimals, testing is the easyest and fastest way)
146+
calPre = (((F_CPU / (reg.regN + 1)) / freq) - 1) + calPreVari;
147+
if(calPre > 0x1FFF) {
148+
reg.regPre = 0x1FFF; // 8191
149+
} else if(calPre <= 0) {
150+
reg.regPre = 0;
151+
} else {
152+
reg.regPre = calPre;
153+
}
154+
155+
reg.regL = ((reg.regN + 1) / 2);
156+
// reg.regH = (reg.regN - reg.regL);
157+
158+
// test calculation
159+
calFreq = ClkRegToFreq(&reg);
160+
//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);
161+
162+
if(calFreq == (int32_t) freq) {
163+
// accurate match use it!
164+
memcpy(&bestReg, &reg, sizeof(bestReg));
165+
break;
166+
} else if(calFreq < (int32_t) freq) {
167+
// never go over the requested frequency
168+
if(abs(freq - calFreq) < abs(freq - bestFreq)) {
169+
bestFreq = calFreq;
170+
memcpy(&bestReg, &reg, sizeof(bestReg));
171+
}
172+
}
173+
}
174+
if(calFreq == (int32_t) freq) {
175+
// accurate match use it!
176+
break;
177+
}
178+
calN++;
179+
}
180+
181+
// 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));
182+
183+
setClockDivider(bestReg.regValue);
184+
lastSetRegister = SPI1CLK;
185+
lastSetFrequency = freq;
186+
65187
}
66188

67189
void SPIClass::setClockDivider(uint32_t clockDiv) {
68-
SPI1CLK = clockDiv;
190+
SPI1CLK = clockDiv;
69191
}
70192

71193
uint8_t SPIClass::transfer(uint8_t data) {
72-
while(SPI1CMD & SPIBUSY);
73-
SPI1W0 = data;
74-
SPI1CMD |= SPIBUSY;
75-
while(SPI1CMD & SPIBUSY);
76-
return (uint8_t)(SPI1W0 & 0xff);
194+
while(SPI1CMD & SPIBUSY)
195+
;
196+
SPI1W0 = data;
197+
SPI1CMD |= SPIBUSY;
198+
while(SPI1CMD & SPIBUSY)
199+
;
200+
return (uint8_t) (SPI1W0 & 0xff);
77201
}
78202

79203
uint16_t SPIClass::transfer16(uint16_t data) {

Diff for: libraries/SPI/SPI.h

+9-21
Original file line numberDiff line numberDiff line change
@@ -24,30 +24,17 @@
2424
#include <Arduino.h>
2525
#include <stdlib.h>
2626

27-
#define FCPU80 80000000L
28-
29-
#if F_CPU == FCPU80
30-
#define SPI_CLOCK_DIV80M 0x80000000 //80 MHz
31-
#define SPI_CLOCK_DIV40M 0x00001001 //40 MHz
32-
#define SPI_CLOCK_DIV20M 0x00041001 //20 MHz
33-
#define SPI_CLOCK_DIV16M 0x000fffc0 //16 MHz
34-
#define SPI_CLOCK_DIV10M 0x000c1001 //10 MHz
27+
// This defines are not representing the real Divider of the ESP8266
28+
// the Defines match to an AVR Arduino on 16MHz for better compatibility
29+
#if F_CPU == 80000000L
3530
#define SPI_CLOCK_DIV2 0x00101001 //8 MHz
36-
#define SPI_CLOCK_DIV5M 0x001c1001 //5 MHz
3731
#define SPI_CLOCK_DIV4 0x00241001 //4 MHz
3832
#define SPI_CLOCK_DIV8 0x004c1001 //2 MHz
3933
#define SPI_CLOCK_DIV16 0x009c1001 //1 MHz
4034
#define SPI_CLOCK_DIV32 0x013c1001 //500 KHz
4135
#define SPI_CLOCK_DIV64 0x027c1001 //250 KHz
4236
#define SPI_CLOCK_DIV128 0x04fc1001 //125 KHz
4337
#else
44-
#define SPI_CLOCK_DIV160M 0x80000000 //160 MHz
45-
#define SPI_CLOCK_DIV80M 0x00001001 //80 MHz
46-
#define SPI_CLOCK_DIV40M 0x00041001 //40 MHz
47-
#define SPI_CLOCK_DIV32M 0x000fffc0 //32 MHz
48-
#define SPI_CLOCK_DIV20M 0x000c1001 //20 MHz
49-
#define SPI_CLOCK_DIV16M 0x00101001 //16 MHz
50-
#define SPI_CLOCK_DIV10M 0x001c1001 //10 MHz
5138
#define SPI_CLOCK_DIV2 0x00241001 //8 MHz
5239
#define SPI_CLOCK_DIV4 0x004c1001 //4 MHz
5340
#define SPI_CLOCK_DIV8 0x009c1001 //2 MHz
@@ -56,14 +43,14 @@
5643
#define SPI_CLOCK_DIV64 0x04fc1001 //250 KHz
5744
#endif
5845

59-
const uint8_t SPI_MODE0 = 0x00;
60-
const uint8_t SPI_MODE1 = 0x04;
61-
const uint8_t SPI_MODE2 = 0x08;
62-
const uint8_t SPI_MODE3 = 0x0C;
46+
const uint8_t SPI_MODE0 = 0x00; ///< CPOL: 0 CPHA: 0
47+
const uint8_t SPI_MODE1 = 0x01; ///< CPOL: 0 CPHA: 1
48+
const uint8_t SPI_MODE2 = 0x10; ///< CPOL: 1 CPHA: 0
49+
const uint8_t SPI_MODE3 = 0x11; ///< CPOL: 1 CPHA: 1
6350

6451
class SPISettings {
6552
public:
66-
SPISettings() :_clock(SPI_CLOCK_DIV16), _bitOrder(LSBFIRST), _dataMode(SPI_MODE0){}
53+
SPISettings() :_clock(1000000), _bitOrder(LSBFIRST), _dataMode(SPI_MODE0){}
6754
SPISettings(uint32_t clock, uint8_t bitOrder, uint8_t dataMode) :_clock(clock), _bitOrder(bitOrder), _dataMode(dataMode){}
6855
uint32_t _clock;
6956
uint8_t _bitOrder;
@@ -77,6 +64,7 @@ class SPIClass {
7764
void end();
7865
void setBitOrder(uint8_t bitOrder);
7966
void setDataMode(uint8_t dataMode);
67+
void setFrequency(uint32_t freq);
8068
void setClockDivider(uint32_t clockDiv);
8169
void beginTransaction(SPISettings settings);
8270
uint8_t transfer(uint8_t data);

0 commit comments

Comments
 (0)