Skip to content

Commit d49a4ec

Browse files
Add initFlashQuirks() for any chip specific flash initialization.
Called from user_init().
1 parent 46578fe commit d49a4ec

File tree

4 files changed

+166
-0
lines changed

4 files changed

+166
-0
lines changed
Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
/*
2+
flash_quirks.cpp - Chip specific flash init
3+
Copyright (c) 2019 Mike Nix. All rights reserved.
4+
This file is part of the esp8266 core for Arduino environment.
5+
6+
This library is free software; you can redistribute it and/or
7+
modify it under the terms of the GNU Lesser General Public
8+
License as published by the Free Software Foundation; either
9+
version 2.1 of the License, or (at your option) any later version.
10+
11+
This library is distributed in the hope that it will be useful,
12+
but WITHOUT ANY WARRANTY; without even the implied warranty of
13+
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14+
Lesser General Public License for more details.
15+
16+
You should have received a copy of the GNU Lesser General Public
17+
License along with this library; if not, write to the Free Software
18+
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
19+
*/
20+
21+
#include <c_types.h>
22+
#include "spi_flash.h"
23+
24+
#include "spi_utils.h"
25+
#include "flash_quirks.h"
26+
27+
#ifdef __cplusplus
28+
extern "C" {
29+
#endif
30+
31+
static int get_flash_mhz() {
32+
// FIXME: copied from Esp.cpp - we really should define the magic values
33+
uint32_t data;
34+
uint8_t * bytes = (uint8_t *) &data;
35+
// read first 4 byte (magic byte + flash config)
36+
if(spi_flash_read(0x0000, &data, 4) == SPI_FLASH_RESULT_OK) {
37+
switch (bytes[3] & 0x0F) {
38+
case 0x0: // 40 MHz
39+
return 40;
40+
case 0x1: // 26 MHz
41+
return 26;
42+
case 0x2: // 20 MHz
43+
return 20;
44+
case 0xf: // 80 MHz
45+
return 80;
46+
default: // fail?
47+
return 0;
48+
}
49+
}
50+
return 0;
51+
}
52+
53+
/* initFlashQuirks()
54+
* Do any chip-specific initialization to improve performance and reliability.
55+
*/
56+
void initFlashQuirks() {
57+
using namespace experimental;
58+
uint32_t vendor = spi_flash_get_id() & 0x000000ff;
59+
60+
switch (vendor) {
61+
case SPI_FLASH_VENDOR_XMC:
62+
uint32_t SR3, newSR3;
63+
if (SPI0Command(SPI_FLASH_CMD_RSR3, &SR3, 0, 8)==SPI_RESULT_OK) { // read SR3
64+
newSR3=SR3;
65+
if (get_flash_mhz()>26) { // >26Mhz?
66+
// Set the output drive to 100%
67+
newSR3 &= ~(SPI_FLASH_SR3_XMC_DRV_MASK << SPI_FLASH_SR3_XMC_DRV_S);
68+
newSR3 |= (SPI_FLASH_SR3_XMC_DRV_100 << SPI_FLASH_SR3_XMC_DRV_S);
69+
}
70+
if (newSR3 != SR3) { // only write if changed
71+
if (SPI0Command(SPI_FLASH_CMD_WEVSR,NULL,0,0)==SPI_RESULT_OK) // write enable volatile SR
72+
SPI0Command(SPI_FLASH_CMD_WSR3,&newSR3,8,0); // write to SR3
73+
SPI0Command(SPI_FLASH_CMD_WRDI,NULL,0,0); // write disable - probably not needed
74+
}
75+
}
76+
}
77+
}
78+
79+
#ifdef __cplusplus
80+
}
81+
#endif

cores/esp8266/core_esp8266_main.cpp

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ extern "C" {
3434
}
3535
#include <core_version.h>
3636
#include "gdb_hooks.h"
37+
#include "flash_quirks.h"
3738

3839
#define LOOP_TASK_PRIORITY 1
3940
#define LOOP_QUEUE_SIZE 1
@@ -314,6 +315,8 @@ extern "C" void user_init(void) {
314315

315316
initVariant();
316317

318+
initFlashQuirks(); // Chip specific flash init.
319+
317320
cont_init(g_pcont);
318321

319322
preinit(); // Prior to C++ Dynamic Init (not related to above init() ). Meant to be user redefinable.

cores/esp8266/flash_quirks.h

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
/*
2+
flash_quirks.h
3+
Copyright (c) 2019 Mike Nix. All rights reserved.
4+
This file is part of the esp8266 core for Arduino environment.
5+
6+
This library is free software; you can redistribute it and/or
7+
modify it under the terms of the GNU Lesser General Public
8+
License as published by the Free Software Foundation; either
9+
version 2.1 of the License, or (at your option) any later version.
10+
11+
This library is distributed in the hope that it will be useful,
12+
but WITHOUT ANY WARRANTY; without even the implied warranty of
13+
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14+
Lesser General Public License for more details.
15+
16+
You should have received a copy of the GNU Lesser General Public
17+
License along with this library; if not, write to the Free Software
18+
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
19+
*/
20+
21+
#ifndef FLASH_QUIRKS_H
22+
#define FLASH_QUIRKS_H
23+
24+
#ifdef __cplusplus
25+
extern "C" {
26+
#endif
27+
28+
#include "spi_vendors.h"
29+
#include "spi_flash_defs.h"
30+
31+
void initFlashQuirks();
32+
33+
#ifdef __cplusplus
34+
}
35+
#endif
36+
37+
38+
#endif // FLASH_QUIRKS_H

cores/esp8266/spi_flash_defs.h

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
/*
2+
spi_flash_defs.h - SPI Flash chip commands and status registers
3+
Copyright (c) 2019 Mike Nix. All rights reserved.
4+
This file is part of the esp8266 core for Arduino environment.
5+
6+
This library is free software; you can redistribute it and/or
7+
modify it under the terms of the GNU Lesser General Public
8+
License as published by the Free Software Foundation; either
9+
version 2.1 of the License, or (at your option) any later version.
10+
11+
This library is distributed in the hope that it will be useful,
12+
but WITHOUT ANY WARRANTY; without even the implied warranty of
13+
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14+
Lesser General Public License for more details.
15+
16+
You should have received a copy of the GNU Lesser General Public
17+
License along with this library; if not, write to the Free Software
18+
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
19+
*/
20+
21+
#ifndef SPI_FLASH_DEFS_H
22+
#define SPI_FLASH_DEFS_H
23+
24+
// Flash chip Status Register 3: Vendor XMC Output Drive levels
25+
#define SPI_FLASH_SR3_XMC_DRV_25 1
26+
#define SPI_FLASH_SR3_XMC_DRV_50 0
27+
#define SPI_FLASH_SR3_XMC_DRV_75 2
28+
#define SPI_FLASH_SR3_XMC_DRV_100 3
29+
30+
#define SPI_FLASH_SR3_XMC_DRV_S 5
31+
#define SPI_FLASH_SR3_XMC_DRV_MASK 0x03
32+
33+
// Flash Chip commands
34+
#define SPI_FLASH_CMD_RSR1 0x05 //Read Flash Status Register...
35+
#define SPI_FLASH_CMD_RSR2 0x35
36+
#define SPI_FLASH_CMD_RSR3 0x15
37+
#define SPI_FLASH_CMD_WSR1 0x01 //Write Flash Status Register...
38+
#define SPI_FLASH_CMD_WSR2 0x31
39+
#define SPI_FLASH_CMD_WSR3 0x11
40+
#define SPI_FLASH_CMD_WEVSR 0x50 //Write Enable Volatile Status Registers
41+
#define SPI_FLASH_CMD_WREN 0x06 //Write Enable
42+
#define SPI_FLASH_CMD_WRDI 0x04 //Write Disable
43+
44+
#endif // SPI_FLASH_DEFS_H

0 commit comments

Comments
 (0)