Skip to content

Commit 163858d

Browse files
committed
fix use SPI.beginTransaction crash
1 parent 8b114a5 commit 163858d

3 files changed

Lines changed: 96 additions & 77 deletions

File tree

‎cores/esp8266/Esp.cpp‎

Lines changed: 7 additions & 0 deletions
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

‎libraries/SPI/SPI.cpp‎

Lines changed: 85 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -1,79 +1,117 @@
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+
typedef struct {
25+
uint32_t divider;
26+
uint32_t regValue;
27+
} spiClockDiv_t;
28+
29+
static const spiClockDiv_t spiClockDiv[] = {
30+
{ 0, (0x80000000) }, ///< @80Mhz = 80 MHz @160Mhz = 160 MHz
31+
{ 2, (0x00001001) }, ///< @80Mhz = 40 MHz @160Mhz = 80 MHz
32+
{ 4, (0x00041001) }, ///< @80Mhz = 20 MHz @160Mhz = 40 MHz
33+
{ 6, (0x000fffc0) }, ///< @80Mhz = 16 MHz @160Mhz = 32 MHz
34+
{ 8, (0x000c1001) }, ///< @80Mhz = 10 MHz @160Mhz = 20 MHz
35+
{ 10, (0x00101001) }, ///< @80Mhz = 8 MHz @160Mhz = 16 MHz
36+
{ 16, (0x001c1001) }, ///< @80Mhz = 5 MHz @160Mhz = 10 MHz
37+
{ 20, (0x00241001) }, ///< @80Mhz = 4 MHz @160Mhz = 8 MHz
38+
{ 40, (0x004c1001) }, ///< @80Mhz = 2 MHz @160Mhz = 4 MHz
39+
{ 80, (0x009c1001) }, ///< @80Mhz = 1 MHz @160Mhz = 2 MHz
40+
{ 160, (0x013c1001) }, ///< @80Mhz = 500 KHz @160Mhz = 1 MHz
41+
{ 320, (0x027c1001) }, ///< @80Mhz = 250 KHz @160Mhz = 500 KHz
42+
{ 640, (0x04fc1001) } ///< @80Mhz = 125 KHz @160Mhz = 250 KHz
43+
};
44+
45+
static const uint8_t spiClockDiv_count = (sizeof(spiClockDiv) / sizeof(spiClockDiv_t));
2346

2447
SPIClass SPI;
2548

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;
49+
SPIClass::SPIClass() {
50+
}
51+
52+
void SPIClass::begin() {
53+
pinMode(SCK, SPECIAL); ///< GPIO14
54+
pinMode(MISO, SPECIAL); ///< GPIO12
55+
pinMode(MOSI, SPECIAL); ///< GPIO13
56+
57+
GPMUX = 0x105; // note crash if spi flash Frequency < 40MHz
58+
SPI1C = 0;
59+
setFrequency(1000000); ///< 1Mhz
60+
SPI1U = SPIUMOSI | SPIUDUPLEX | SPIUSSE;
61+
SPI1U1 = (7 << SPILMOSI) | (7 << SPILMISO);
62+
SPI1C1 = 0;
3963
}
4064

4165
void SPIClass::end() {
42-
pinMode(SCK, INPUT);
43-
pinMode(MISO, INPUT);
44-
pinMode(MOSI, INPUT);
66+
pinMode(SCK, INPUT);
67+
pinMode(MISO, INPUT);
68+
pinMode(MOSI, INPUT);
4569
}
4670

4771
void SPIClass::beginTransaction(SPISettings settings) {
48-
setClockDivider(settings._clock);
49-
setBitOrder(settings._bitOrder);
50-
setDataMode(settings._dataMode);
72+
setFrequency(settings._clock);
73+
setBitOrder(settings._bitOrder);
74+
setDataMode(settings._dataMode);
5175
}
5276

53-
void SPIClass::endTransaction() {}
77+
void SPIClass::endTransaction() {
78+
}
5479

5580
void SPIClass::setDataMode(uint8_t dataMode) {
56-
81+
// todo find way to set
5782
}
5883

5984
void SPIClass::setBitOrder(uint8_t bitOrder) {
60-
if (bitOrder == MSBFIRST) {
61-
SPI1C &= ~(SPICWBO | SPICRBO);
62-
} else {
63-
SPI1C |= (SPICWBO | SPICRBO);
64-
}
85+
if(bitOrder == MSBFIRST) {
86+
SPI1C &= ~(SPICWBO | SPICRBO);
87+
} else {
88+
SPI1C |= (SPICWBO | SPICRBO);
89+
}
90+
}
91+
92+
void SPIClass::setFrequency(uint32_t freq) {
93+
uint8_t i = 0;
94+
// find the best match
95+
if(freq < F_CPU) {
96+
for(i = 1; i < (spiClockDiv_count-1); i++) {
97+
if(freq >= (F_CPU/spiClockDiv[i].divider)) {
98+
break;
99+
}
100+
}
101+
}
102+
setClockDivider(spiClockDiv[i].regValue);
65103
}
66104

67105
void SPIClass::setClockDivider(uint32_t clockDiv) {
68-
SPI1CLK = clockDiv;
106+
SPI1CLK = clockDiv;
69107
}
70108

71109
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);
110+
while(SPI1CMD & SPIBUSY);
111+
SPI1W0 = data;
112+
SPI1CMD |= SPIBUSY;
113+
while(SPI1CMD & SPIBUSY);
114+
return (uint8_t) (SPI1W0 & 0xff);
77115
}
78116

79117
uint16_t SPIClass::transfer16(uint16_t data) {

‎libraries/SPI/SPI.h‎

Lines changed: 4 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -26,35 +26,8 @@
2626

2727
#define FCPU80 80000000L
2828

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
35-
#define SPI_CLOCK_DIV2 0x00101001 //8 MHz
36-
#define SPI_CLOCK_DIV5M 0x001c1001 //5 MHz
37-
#define SPI_CLOCK_DIV4 0x00241001 //4 MHz
38-
#define SPI_CLOCK_DIV8 0x004c1001 //2 MHz
39-
#define SPI_CLOCK_DIV16 0x009c1001 //1 MHz
40-
#define SPI_CLOCK_DIV32 0x013c1001 //500 KHz
41-
#define SPI_CLOCK_DIV64 0x027c1001 //250 KHz
42-
#define SPI_CLOCK_DIV128 0x04fc1001 //125 KHz
43-
#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
51-
#define SPI_CLOCK_DIV2 0x00241001 //8 MHz
52-
#define SPI_CLOCK_DIV4 0x004c1001 //4 MHz
53-
#define SPI_CLOCK_DIV8 0x009c1001 //2 MHz
54-
#define SPI_CLOCK_DIV16 0x013c1001 //1 MHz
55-
#define SPI_CLOCK_DIV32 0x027c1001 //500 KHz
56-
#define SPI_CLOCK_DIV64 0x04fc1001 //250 KHz
57-
#endif
29+
30+
5831

5932
const uint8_t SPI_MODE0 = 0x00;
6033
const uint8_t SPI_MODE1 = 0x04;
@@ -63,7 +36,7 @@ const uint8_t SPI_MODE3 = 0x0C;
6336

6437
class SPISettings {
6538
public:
66-
SPISettings() :_clock(SPI_CLOCK_DIV16), _bitOrder(LSBFIRST), _dataMode(SPI_MODE0){}
39+
SPISettings() :_clock(1000000), _bitOrder(LSBFIRST), _dataMode(SPI_MODE0){}
6740
SPISettings(uint32_t clock, uint8_t bitOrder, uint8_t dataMode) :_clock(clock), _bitOrder(bitOrder), _dataMode(dataMode){}
6841
uint32_t _clock;
6942
uint8_t _bitOrder;
@@ -77,6 +50,7 @@ class SPIClass {
7750
void end();
7851
void setBitOrder(uint8_t bitOrder);
7952
void setDataMode(uint8_t dataMode);
53+
void setFrequency(uint32_t freq);
8054
void setClockDivider(uint32_t clockDiv);
8155
void beginTransaction(SPISettings settings);
8256
uint8_t transfer(uint8_t data);

0 commit comments

Comments
 (0)