Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit b26e72c

Browse files
committed
Merge branch 'feature/init_flash_at_boot' into 'master'
Initialize SPI flash clock and I/O mode at bootloader See merge request sdk/ESP8266_RTOS_SDK!606
2 parents e8a5a0f + d5b14d7 commit b26e72c

File tree

8 files changed

+108
-108
lines changed

8 files changed

+108
-108
lines changed

components/bootloader/Kconfig.projbuild

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,15 @@
11
menu "Bootloader config"
2+
3+
config BOOTLOADER_INIT_SPI_FLASH
4+
bool "Bootloader init SPI flash"
5+
default y
6+
help
7+
Enable this option, software will initialize SPI flash clock and I/O mode at bootloader instead of at APP.
8+
So it will speed up system starting and reduce the time cost at loading firmware.
9+
10+
If your system bootloader is based on v3.0, the option must not be enable, because the v3.0 bootloader don't support
11+
this function.
12+
213
choice LOG_BOOTLOADER_LEVEL
314
bool "Bootloader log verbosity"
415
default LOG_BOOTLOADER_LEVEL_INFO

components/bootloader_support/src/bootloader_init.c

Lines changed: 8 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -619,46 +619,26 @@ static esp_err_t bootloader_main()
619619
return ESP_FAIL;
620620
}
621621

622+
update_flash_config(&fhdr);
623+
622624
ESP_LOGI(TAG, "ESP-IDF %s 2nd stage bootloader", IDF_VER);
623625

624626
ESP_LOGI(TAG, "compile time " __TIME__ );
625627

626628
print_flash_info(&fhdr);
627629

628-
update_flash_config(&fhdr);
629-
630630
return ESP_OK;
631631
}
632632

633633
static void update_flash_config(const esp_image_header_t* pfhdr)
634634
{
635-
// uint32_t size;
636-
// switch(pfhdr->spi_size) {
637-
// case ESP_IMAGE_FLASH_SIZE_1MB:
638-
// size = 1;
639-
// break;
640-
// case ESP_IMAGE_FLASH_SIZE_2MB:
641-
// case ESP_IMAGE_FLASH_SIZE_2MB_C1:
642-
// size = 2;
643-
// break;
644-
// case ESP_IMAGE_FLASH_SIZE_4MB:
645-
// case ESP_IMAGE_FLASH_SIZE_4MB_C1:
646-
// size = 4;
647-
// break;
648-
// case ESP_IMAGE_FLASH_SIZE_8MB:
649-
// size = 8;
650-
// break;
651-
// case ESP_IMAGE_FLASH_SIZE_16MB:
652-
// size = 16;
653-
// break;
654-
// default:
655-
// size = 2;
656-
// }
635+
#ifdef CONFIG_BOOTLOADER_INIT_SPI_FLASH
636+
extern void esp_spi_flash_init(uint32_t spi_speed, uint32_t spi_mode);
657637

658-
// Set flash chip size
659-
// esp_rom_spiflash_config_param(g_rom_flashchip.device_id, size * 0x100000, 0x10000, 0x1000, 0x100, 0xffff);
660-
// TODO: set mode
661-
// TODO: set frequency
638+
esp_spi_flash_init(pfhdr->spi_speed, pfhdr->spi_mode);
639+
640+
ESP_LOGD(TAG, "bootloader initialize SPI flash clock and I/O");
641+
#endif /* CONFIG_BOOTLOADER_INIT_SPI_FLASH */
662642
}
663643

664644
static void print_flash_info(const esp_image_header_t* phdr)

components/esp8266/include/driver/spi_register.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -256,4 +256,8 @@
256256
#define PERIPHS_SPI_FLASH_CTRL SPI_CTRL(SPI)
257257
#define PERIPHS_SPI_FLASH_CMD SPI_CMD(SPI)
258258

259+
#define SPI0_CLK_EQU_SYSCLK BIT8
260+
261+
#define PERIPHS_SPI_FLASH_USRREG (0x60000200 + 0x1c)
262+
259263
#endif // SPI_REGISTER_H_INCLUDED

components/esp8266/source/chip_boot.c

Lines changed: 7 additions & 76 deletions
Original file line numberDiff line numberDiff line change
@@ -13,86 +13,17 @@
1313
// limitations under the License.
1414

1515
#include "sdkconfig.h"
16-
#include "esp_attr.h"
17-
#include "spi_flash.h"
18-
#include "esp_log.h"
19-
#include "esp_system.h"
20-
#include "esp8266/eagle_soc.h"
21-
#include "esp8266/rom_functions.h"
22-
#include "esp_image_format.h"
23-
24-
#define PERIPHS_SPI_FLASH_USRREG (0x60000200 + 0x1c)
25-
#define PERIPHS_SPI_FLASH_CTRL (0x60000200 + 0x08)
26-
#define PERIPHS_IO_MUX_CONF_U (0x60000800)
27-
28-
#define SPI0_CLK_EQU_SYSCLK BIT8
29-
#define SPI_FLASH_CLK_EQU_SYSCLK BIT12
3016

31-
static const char *TAG = "chip_boot";
17+
#ifndef CONFIG_BOOTLOADER_INIT_SPI_FLASH
18+
#include "spi_flash.h"
3219

3320
/*
34-
* @brief initialize the chip including flash I/O and chip cache according to
35-
* boot parameters which are stored at the flash
21+
* @brief initialize the chip
3622
*/
37-
void chip_boot(size_t start_addr)
23+
void chip_boot(void)
3824
{
39-
int ret;
40-
uint32_t freqdiv, flash_size;
41-
uint32_t freqbits;
42-
esp_image_header_t fhdr;
43-
44-
uint32_t flash_map_table[FALSH_SIZE_MAP_MAX] = {
45-
1 * 1024 * 1024,
46-
2 * 1024 * 1024,
47-
4 * 1024 * 1024,
48-
8 * 1024 * 1024,
49-
16 * 1024 * 1024
50-
};
51-
uint32_t flash_map_table_size = sizeof(flash_map_table) / sizeof(flash_map_table[0]);
52-
53-
extern esp_spi_flash_chip_t flashchip;
54-
extern void phy_get_bb_evm(void);
55-
extern void cache_init(uint8_t);
56-
extern void user_spi_flash_dio_to_qio_pre_init(void);
57-
58-
phy_get_bb_evm();
59-
60-
SET_PERI_REG_MASK(PERIPHS_SPI_FLASH_USRREG, BIT5);
61-
62-
ret = spi_flash_read(start_addr, &fhdr, sizeof(esp_image_header_t));
63-
if (ret) {
64-
ESP_EARLY_LOGE(TAG, "SPI flash read result %d\n", ret);
65-
}
66-
67-
if (3 > fhdr.spi_speed)
68-
freqdiv = fhdr.spi_speed + 2;
69-
else if (0x0F == fhdr.spi_speed)
70-
freqdiv = 1;
71-
else
72-
freqdiv = 2;
73-
74-
if (fhdr.spi_size < flash_map_table_size) {
75-
flash_size = flash_map_table[fhdr.spi_size];
76-
ESP_EARLY_LOGD(TAG, "SPI flash size is %d\n", flash_size);
77-
} else {
78-
flash_size = 0;
79-
ESP_EARLY_LOGE(TAG, "SPI size error is %d\n", fhdr.spi_size);
80-
}
81-
flashchip.chip_size = flash_size;
82-
83-
if (1 >= freqdiv) {
84-
freqbits = SPI_FLASH_CLK_EQU_SYSCLK;
85-
SET_PERI_REG_MASK(PERIPHS_SPI_FLASH_CTRL, SPI_FLASH_CLK_EQU_SYSCLK);
86-
SET_PERI_REG_MASK(PERIPHS_IO_MUX_CONF_U, SPI0_CLK_EQU_SYSCLK);
87-
} else {
88-
freqbits = ((freqdiv - 1) << 8) + ((freqdiv / 2 - 1) << 4) + (freqdiv - 1);
89-
CLEAR_PERI_REG_MASK(PERIPHS_SPI_FLASH_CTRL, SPI_FLASH_CLK_EQU_SYSCLK);
90-
CLEAR_PERI_REG_MASK(PERIPHS_IO_MUX_CONF_U, SPI0_CLK_EQU_SYSCLK);
91-
}
92-
SET_PERI_REG_BITS(PERIPHS_SPI_FLASH_CTRL, 0xfff, freqbits, 0);
25+
extern void esp_spi_flash_init(uint32_t spi_speed, uint32_t spi_mode);
9326

94-
if (fhdr.spi_mode == ESP_IMAGE_SPI_MODE_QIO) {
95-
ESP_EARLY_LOGD(TAG, "SPI flash enable QIO mode\n");
96-
user_spi_flash_dio_to_qio_pre_init();
97-
}
27+
esp_spi_flash_init(CONFIG_SPI_FLASH_FREQ, CONFIG_SPI_FLASH_MODE);
9828
}
29+
#endif /* CONFIG_BOOTLOADER_INIT_SPI_FLASH */

components/esp8266/source/startup.c

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,14 +33,15 @@
3333
#define FLASH_MAP_ADDR 0x40200000
3434
#define FLASH_MAP_SIZE 0x00100000
3535

36-
extern void chip_boot(size_t start_addr);
36+
extern void chip_boot(void);
3737
extern int rtc_init(void);
3838
extern int mac_init(void);
3939
extern int base_gpio_init(void);
4040
extern int watchdog_init(void);
4141
extern int wifi_timer_init(void);
4242
extern int wifi_nvs_init(void);
4343
extern esp_err_t esp_pthread_init(void);
44+
extern void phy_get_bb_evm(void);
4445

4546
static void user_init_entry(void *param)
4647
{
@@ -55,6 +56,8 @@ static void user_init_entry(void *param)
5556
for (func = &__init_array_start; func < &__init_array_end; func++)
5657
func[0]();
5758

59+
phy_get_bb_evm();
60+
5861
assert(nvs_flash_init() == 0);
5962
assert(wifi_nvs_init() == 0);
6063
assert(rtc_init() == 0);
@@ -108,7 +111,9 @@ void call_user_start(size_t start_addr)
108111
"wsr a0, vecbase\n"
109112
: : :"memory");
110113

111-
chip_boot(start_addr);
114+
#ifndef CONFIG_BOOTLOADER_INIT_SPI_FLASH
115+
chip_boot();
116+
#endif
112117

113118
/* clear bss data */
114119
for (p = &_bss_start; p < &_bss_end; p++)

components/esptool_py/Kconfig.projbuild

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,14 @@ config FLASHMODE_DOUT
6363
bool "DOUT"
6464
endchoice
6565

66+
config FLASHMODE_SWITCH_TO_QIO
67+
bool "Try to switch to QIO mode at startup"
68+
default y
69+
depends on FLASHMODE_DIO
70+
help
71+
If users use SPI flash DIO mode to download firmware, bootloader or application
72+
can switch to QIO mode by enable this option.
73+
6674
# Note: we use esptool.py to flash bootloader in
6775
# dio mode for QIO/QOUT, bootloader then upgrades
6876
# itself to quad mode during initialisation
@@ -73,6 +81,13 @@ config ESPTOOLPY_FLASHMODE
7381
default "dio" if FLASHMODE_DIO
7482
default "dout" if FLASHMODE_DOUT
7583

84+
config SPI_FLASH_MODE
85+
hex
86+
default 0x0 if FLASHMODE_QIO
87+
default 0x1 if FLASHMODE_QOUT
88+
default 0x2 if FLASHMODE_DIO
89+
default 0x3 if FLASHMODE_DOUT
90+
7691
choice ESPTOOLPY_FLASHFREQ
7792
prompt "Flash SPI speed"
7893
default ESPTOOLPY_FLASHFREQ_40M
@@ -96,6 +111,12 @@ config ESPTOOLPY_FLASHFREQ
96111
default "26m" if ESPTOOLPY_FLASHFREQ_26M
97112
default "20m" if ESPTOOLPY_FLASHFREQ_20M
98113

114+
config SPI_FLASH_FREQ
115+
hex
116+
default 0xf if ESPTOOLPY_FLASHFREQ_80M
117+
default 0x0 if ESPTOOLPY_FLASHFREQ_40M
118+
default 0x1 if ESPTOOLPY_FLASHFREQ_26M
119+
default 0x2 if ESPTOOLPY_FLASHFREQ_20M
99120

100121
choice ESPTOOLPY_FLASHSIZE
101122
prompt "Flash size"
@@ -122,6 +143,14 @@ config ESPTOOLPY_FLASHSIZE
122143
default "8MB" if ESPTOOLPY_FLASHSIZE_8MB
123144
default "16MB" if ESPTOOLPY_FLASHSIZE_16MB
124145

146+
config SPI_FLASH_SIZE
147+
hex
148+
default 0x100000 if ESPTOOLPY_FLASHSIZE_1MB
149+
default 0x200000 if ESPTOOLPY_FLASHSIZE_2MB
150+
default 0x400000 if ESPTOOLPY_FLASHSIZE_4MB
151+
default 0x800000 if ESPTOOLPY_FLASHSIZE_8MB
152+
default 0x1000000 if ESPTOOLPY_FLASHSIZE_16MB
153+
125154
choice ESPTOOLPY_BEFORE
126155
prompt "Before flashing"
127156
default ESPTOOLPY_BEFORE_RESET

components/spi_flash/component.mk

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,10 @@
22
# Component Makefile
33
#
44

5-
ifndef IS_BOOTLOADER_BUILD
65
COMPONENT_SRCDIRS := src
6+
7+
ifdef IS_BOOTLOADER_BUILD
8+
COMPONENT_OBJS := src/spi_flash.o src/spi_flash_raw.o
79
endif
810

911
CFLAGS += -DPARTITION_QUEUE_HEADER=\"sys/queue.h\"

components/spi_flash/src/spi_flash.c

Lines changed: 39 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,8 @@
1515
#include <string.h>
1616

1717
#include "spi_flash.h"
18+
#include "driver/spi_register.h"
19+
#include "esp8266/pin_mux_register.h"
1820
#include "priv/esp_spi_flash_raw.h"
1921

2022
#include "esp_attr.h"
@@ -43,9 +45,15 @@
4345
#define SPI_FLASH_RDSR2 0x35
4446
#define SPI_FLASH_PROTECT_STATUS (BIT(2)|BIT(3)|BIT(4)|BIT(5)|BIT(6)|BIT(14))
4547

48+
#ifndef BOOTLOADER_BUILD
4649
#define FLASH_INTR_DECLARE(t)
4750
#define FLASH_INTR_LOCK(t) vPortEnterCritical()
4851
#define FLASH_INTR_UNLOCK(t) vPortExitCritical()
52+
#else
53+
#define FLASH_INTR_DECLARE(t)
54+
#define FLASH_INTR_LOCK(t)
55+
#define FLASH_INTR_UNLOCK(t)
56+
#endif
4957

5058
#define FLASH_ALIGN_BYTES 4
5159
#define FLASH_ALIGN(addr) ((((size_t)addr) + (FLASH_ALIGN_BYTES - 1)) & (~(FLASH_ALIGN_BYTES - 1)))
@@ -61,7 +69,7 @@ extern void vPortExitCritical(void);
6169

6270
esp_spi_flash_chip_t flashchip = {
6371
0x1640ef,
64-
(32 / 8) * 1024 * 1024,
72+
CONFIG_SPI_FLASH_SIZE,
6573
64 * 1024,
6674
4 * 1024,
6775
256,
@@ -720,3 +728,33 @@ esp_err_t spi_flash_erase_range(size_t start_address, size_t size)
720728

721729
return ret;
722730
}
731+
732+
void esp_spi_flash_init(uint32_t spi_speed, uint32_t spi_mode)
733+
{
734+
uint32_t freqdiv, freqbits;
735+
736+
SET_PERI_REG_MASK(PERIPHS_SPI_FLASH_USRREG, BIT5);
737+
738+
if (spi_speed < 3)
739+
freqdiv = spi_speed + 2;
740+
else if (0x0F == spi_speed)
741+
freqdiv = 1;
742+
else
743+
freqdiv = 2;
744+
745+
if (1 >= freqdiv) {
746+
freqbits = SPI_FLASH_CLK_EQU_SYSCLK;
747+
SET_PERI_REG_MASK(PERIPHS_SPI_FLASH_CTRL, SPI_FLASH_CLK_EQU_SYSCLK);
748+
SET_PERI_REG_MASK(PERIPHS_IO_MUX_CONF_U, SPI0_CLK_EQU_SYSCLK);
749+
} else {
750+
freqbits = ((freqdiv - 1) << 8) + ((freqdiv / 2 - 1) << 4) + (freqdiv - 1);
751+
CLEAR_PERI_REG_MASK(PERIPHS_SPI_FLASH_CTRL, SPI_FLASH_CLK_EQU_SYSCLK);
752+
CLEAR_PERI_REG_MASK(PERIPHS_IO_MUX_CONF_U, SPI0_CLK_EQU_SYSCLK);
753+
}
754+
SET_PERI_REG_BITS(PERIPHS_SPI_FLASH_CTRL, 0xfff, freqbits, 0);
755+
756+
#ifndef CONFIG_FLASHMODE_SWITCH_TO_QIO
757+
if (spi_mode == 0)
758+
#endif
759+
user_spi_flash_dio_to_qio_pre_init();
760+
}

0 commit comments

Comments
 (0)