Skip to content

Commit d7da7bd

Browse files
committed
Merge branch 'feature/load_old_phy_parameter' into 'release/v3.1'
Add feature to copy old RF parameters to new SDK partition See merge request sdk/ESP8266_RTOS_SDK!874
2 parents dfcabc8 + 1f6c258 commit d7da7bd

File tree

7 files changed

+167
-34
lines changed

7 files changed

+167
-34
lines changed

components/bootloader_support/include_priv/bootloader_config.h

+4
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ extern "C"
2222
{
2323
#endif
2424

25+
#include "sdkconfig.h"
2526
#include "esp_flash_data_types.h"
2627

2728
#define SPI_SEC_SIZE 0x1000
@@ -34,6 +35,9 @@ typedef struct {
3435
esp_partition_pos_t ota_info;
3536
esp_partition_pos_t factory;
3637
esp_partition_pos_t test;
38+
#ifdef CONFIG_LOAD_OLD_RF_PARAMETER
39+
esp_partition_pos_t rf;
40+
#endif
3741
esp_partition_pos_t ota[MAX_OTA_SLOTS];
3842
uint32_t app_count;
3943
uint32_t selected_subtype;

components/bootloader_support/src/bootloader_utility.c

+3
Original file line numberDiff line numberDiff line change
@@ -590,6 +590,9 @@ bool bootloader_utility_load_partition_table(bootloader_state_t* bs)
590590
partition_usage = "OTA data";
591591
break;
592592
case PART_SUBTYPE_DATA_RF:
593+
#ifdef CONFIG_LOAD_OLD_RF_PARAMETER
594+
bs->rf = partition->pos;
595+
#endif
593596
partition_usage = "RF data";
594597
break;
595598
case PART_SUBTYPE_DATA_WIFI:

components/esp8266/Kconfig

+13
Original file line numberDiff line numberDiff line change
@@ -226,6 +226,19 @@ config ESP8266_OTA_FROM_OLD
226226

227227
The old RTOS SDK(before V3.0) or NonOS SDK can download the firmware to its partition and run it as it self's application.
228228

229+
config LOAD_OLD_RF_PARAMETER
230+
bool "(**Expected**)Load old RF Parameters"
231+
default n
232+
depends on ESP8266_OTA_FROM_OLD
233+
select ESP_PHY_INIT_DATA_IN_PARTITION
234+
help
235+
The function is not released.
236+
237+
Enable this option, after updating from old SDK to new SDK, bootloader will copy RF
238+
parameters from old SDK partition to new SDK partition.
239+
240+
Then application can read the RF parameters from new partition directly.
241+
229242
config ESP8266_BOOT_COPY_APP
230243
bool "(**Expected**)Boot copy app"
231244
default n

components/esp8266/Makefile.projbuild

+3-3
Original file line numberDiff line numberDiff line change
@@ -40,9 +40,9 @@ ESPTOOL_ALL_FLASH_ARGS += $(PHY_DATA_OFFSET) $(PHY_INIT_DATA_BIN)
4040

4141
ESP8266_COMPONENT_PATH := $(COMPONENT_PATH)
4242

43-
$(PHY_INIT_DATA_OBJ): $(ESP8266_COMPONENT_PATH)/source/phy_init_data.h $(BUILD_DIR_BASE)/include/sdkconfig.h
43+
$(PHY_INIT_DATA_OBJ): $(ESP8266_COMPONENT_PATH)/include/internal/phy_init_data.h $(BUILD_DIR_BASE)/include/sdkconfig.h
4444
$(summary) CC $(notdir $@)
45-
printf "#include \"source/phy_init_data.h\"\n" | $(CC) -I $(BUILD_DIR_BASE)/include -I $(ESP8266_COMPONENT_PATH) -I $(ESP8266_COMPONENT_PATH)/include -c -o $@ -xc -
45+
printf "#include \"internal/phy_init_data.h\"\n" | $(CC) -I $(BUILD_DIR_BASE)/include -I $(ESP8266_COMPONENT_PATH) -I $(ESP8266_COMPONENT_PATH)/include -c -o $@ -xc -
4646

4747
$(PHY_INIT_DATA_BIN): $(PHY_INIT_DATA_OBJ)
4848
$(summary) BIN $(notdir $@)
@@ -118,7 +118,7 @@ endif
118118
@echo [GEN] $(OTA_BIN)
119119

120120
ifdef CONFIG_ESP8266_OTA_FROM_OLD
121-
$(OTA_V2_TO_V3_BIN): all_binaries
121+
$(OTA_V2_TO_V3_BIN): all
122122
@python $(IDF_PATH)/tools/pack_fw.py --output $(OTA_V2_TO_V3_BIN) --app $(PROJECT_NAME).bin pack3 $(ESPTOOL_ALL_FLASH_ARGS)
123123
@echo [GEN] $(OTA_V2_TO_V3_BIN)
124124
endif

components/esp8266/source/phy_init.c

+1-1
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@
2727
#include "nvs_flash.h"
2828
#include "sdkconfig.h"
2929

30-
#include "phy_init_data.h"
30+
#include "internal/phy_init_data.h"
3131
#include "phy.h"
3232

3333
static const char *TAG = "phy_init";

components/spi_flash/port/port.c

+143-30
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
#include "esp_libc.h"
2727
#include "esp8266/rom_functions.h"
2828
#include "esp8266/eagle_soc.h"
29+
#include "internal/phy_init_data.h"
2930

3031
#define PARTITION_DATA_OFFSET (s_v2_flash_bin_size / 2)
3132

@@ -65,6 +66,7 @@ typedef union s_boot_param {
6566
static const char *TAG = "partition_port";
6667
static uint32_t s_partition_offset;
6768
static ROM_FLASH_BUF_DECLARE(s_cache_buf, SPI_FLASH_SEC_SIZE);
69+
static ROM_FLASH_BUF_DECLARE(check_buf, SPI_FLASH_SEC_SIZE);
6870
static uint32_t s_v2_flash_bin_size;
6971
static uint32_t s_v2_flash_size;
7072
static sys_param_t s_sys_param;
@@ -109,23 +111,31 @@ static const uint32_t s_v2_flash_bin_map_table[] = {
109111

110112
static const uint32_t s_v2_flash_bin_size_map_size = sizeof(s_v2_flash_bin_map_table) / sizeof(s_v2_flash_bin_map_table[0]);
111113

112-
static inline void esp_hw_reset(void)
114+
static void esp_hw_reset(uint32_t t, int block)
113115
{
114116
CLEAR_WDT_REG_MASK(WDT_CTL_ADDRESS, BIT0);
115117

116-
WDT_REG_WRITE(WDT_OP_ADDRESS, 8);
117-
WDT_REG_WRITE(WDT_OP_ND_ADDRESS, 8);
118+
WDT_REG_WRITE(WDT_OP_ADDRESS, t);
119+
WDT_REG_WRITE(WDT_OP_ND_ADDRESS, t);
118120

119121
SET_PERI_REG_BITS(PERIPHS_WDT_BASEADDR + WDT_CTL_ADDRESS, WDT_CTL_RSTLEN_MASK, 7 << WDT_CTL_RSTLEN_LSB, 0);
120122
// interrupt then reset
121123
SET_PERI_REG_BITS(PERIPHS_WDT_BASEADDR + WDT_CTL_ADDRESS, WDT_CTL_RSPMOD_MASK, 0 << WDT_CTL_RSPMOD_LSB, 0);
122124
// start task watch dog1
123125
SET_PERI_REG_BITS(PERIPHS_WDT_BASEADDR + WDT_CTL_ADDRESS, WDT_CTL_EN_MASK, 1 << WDT_CTL_EN_LSB, 0);
124126

125-
while (1);
127+
WDT_FEED();
128+
129+
if (block)
130+
while (1);
126131
}
127132

128-
static inline int spi_flash_read_data(uint32_t addr, void *buf, size_t n)
133+
static void esp_hw_feed_wdt(void)
134+
{
135+
WDT_FEED();
136+
}
137+
138+
static int spi_flash_read_data(uint32_t addr, void *buf, size_t n)
129139
{
130140
int ret;
131141

@@ -138,10 +148,12 @@ static inline int spi_flash_read_data(uint32_t addr, void *buf, size_t n)
138148

139149
ret = SPI_read_data(&s_flash_chip, addr, buf, n);
140150

151+
esp_hw_feed_wdt();
152+
141153
return ret;
142154
}
143155

144-
static inline int spi_flash_write_data(uint32_t addr, const void *buf, uint32_t n)
156+
static int spi_flash_write_data(uint32_t addr, const void *buf, uint32_t n)
145157
{
146158
int ret;
147159

@@ -154,21 +166,25 @@ static inline int spi_flash_write_data(uint32_t addr, const void *buf, uint32_t
154166

155167
ret = SPIWrite(addr, (void *)buf, n);
156168

169+
esp_hw_feed_wdt();
170+
157171
return ret;
158172
}
159173

160-
static inline int spi_flash_erase(uint32_t addr)
174+
static int spi_flash_erase(uint32_t addr)
161175
{
162176
int ret;
163177

164178
ESP_LOGD(TAG, "erase addr is 0x%x", addr);
165179

166180
ret = SPIEraseSector(addr / SPI_FLASH_SEC_SIZE);
167181

182+
esp_hw_feed_wdt();
183+
168184
return ret;
169185
}
170186

171-
static inline int get_v2_flash_map_size(void)
187+
static int get_v2_flash_map_size(void)
172188
{
173189
int ret;
174190
esp_image_header_t header;
@@ -197,7 +213,7 @@ static inline int get_v2_flash_map_size(void)
197213
return 0;
198214
}
199215

200-
static inline uint32_t esp_get_updated_partition_table_addr(void)
216+
static uint32_t esp_get_updated_partition_table_addr(void)
201217
{
202218
int ret;
203219
size_t offset;
@@ -259,10 +275,9 @@ static inline uint32_t esp_get_updated_partition_table_addr(void)
259275
return s_partition_offset;
260276
}
261277

262-
static inline int spi_flash_write_data_safe(uint32_t addr, const void *buf, size_t n)
278+
static int spi_flash_write_data_safe(uint32_t addr, const void *buf, size_t n)
263279
{
264-
int ret;
265-
static ROM_FLASH_BUF_DECLARE(check_buf, SPI_FLASH_SEC_SIZE);
280+
int ret;
266281

267282
ret = spi_flash_erase(addr);
268283
if (ret) {
@@ -313,7 +328,79 @@ static int esp_flash_sector_copy(uint32_t dest, uint32_t src, uint32_t total_siz
313328
return 0;
314329
}
315330

316-
static inline int esp_set_v2boot_app1(void)
331+
#ifdef CONFIG_LOAD_OLD_RF_PARAMETER
332+
333+
static bool esp_rf_param_need_unpack(size_t rf_dest, size_t rf_src, size_t rf_size)
334+
{
335+
bool ret;
336+
const uint32_t rf_magic_size = sizeof(phy_init_magic_pre);
337+
const uint32_t rf_new_size = rf_size + rf_magic_size * 2;
338+
339+
ret = spi_flash_read_data(rf_dest, check_buf, rf_new_size);
340+
if (ret) {
341+
ESP_LOGE(TAG, "Fail to read RF parameters from 0x%x", rf_src);
342+
return -1;
343+
}
344+
345+
ret = spi_flash_read_data(rf_src, ((uint8_t *)s_cache_buf) + rf_magic_size, rf_size);
346+
if (ret) {
347+
ESP_LOGE(TAG, "Fail to read RF parameters from 0x%x", rf_src);
348+
return -1;
349+
}
350+
memcpy(s_cache_buf, phy_init_magic_pre, rf_magic_size);
351+
memcpy(((uint8_t *)s_cache_buf) + rf_magic_size + rf_size, phy_init_magic_pre, rf_magic_size);
352+
353+
if (!memcmp(check_buf, s_cache_buf, rf_new_size))
354+
ret = false;
355+
else
356+
ret = true;
357+
358+
return ret;
359+
}
360+
361+
static int esp_unpack_old_rf_param(size_t rf_dest, size_t rf_src, size_t rf_size)
362+
{
363+
int ret;
364+
const uint32_t rf_magic_size = sizeof(phy_init_magic_pre);
365+
const uint32_t rf_new_size = rf_size + rf_magic_size * 2;
366+
367+
ret = spi_flash_erase(rf_dest);
368+
if (ret) {
369+
ESP_LOGE(TAG, "Fail to erase new RF parameters from 0x%x", rf_src);
370+
return -1;
371+
}
372+
373+
ret = spi_flash_read_data(rf_src, ((uint8_t *)s_cache_buf) + rf_magic_size, rf_size);
374+
if (ret) {
375+
ESP_LOGE(TAG, "Fail to read old RF parameters from 0x%x", rf_src);
376+
return -1;
377+
}
378+
memcpy(s_cache_buf, phy_init_magic_pre, rf_magic_size);
379+
memcpy(((uint8_t *)s_cache_buf) + rf_magic_size + rf_size, phy_init_magic_pre, rf_magic_size);
380+
381+
ret = spi_flash_write_data(rf_dest, s_cache_buf , rf_new_size);
382+
if (ret) {
383+
ESP_LOGE(TAG, "Fail to write new RF parameters from 0x%x", rf_src);
384+
return -1;
385+
}
386+
387+
ret = spi_flash_read_data(rf_dest, check_buf, rf_new_size);
388+
if (ret) {
389+
ESP_LOGE(TAG, "Fail to read new RF parameters from 0x%x", rf_src);
390+
return -1;
391+
}
392+
393+
if (memcmp(check_buf, s_cache_buf, rf_new_size)) {
394+
ESP_LOGE(TAG, "Check RF parameters error");
395+
return -1;
396+
}
397+
398+
return 0;
399+
}
400+
401+
#endif
402+
403+
static int esp_set_v2boot_app1(void)
317404
{
318405
int ret;
319406
const size_t offset = s_sys_param.flag ? 1 : 0;
@@ -360,7 +447,7 @@ static inline int esp_set_v2boot_app1(void)
360447
return 0;
361448
}
362449

363-
static inline int esp_sdk_update_from_v2(void)
450+
static int esp_sdk_update_from_v2(void)
364451
{
365452
const int segment_cnt = 3;
366453
const size_t v2_max_size = 4096;
@@ -397,6 +484,8 @@ int esp_patition_table_init_location(void)
397484
{
398485
uint32_t addr;
399486

487+
esp_hw_reset(14, 0);
488+
400489
if (!esp_sdk_update_from_v2())
401490
return 0;
402491

@@ -433,36 +522,60 @@ int esp_patition_copy_ota1_to_ota0(const void *partition_info)
433522
int esp_patition_table_init_data(void *partition_info)
434523
{
435524
int ret;
525+
int reboot = 0;
436526
const uint32_t boot_base = 0x1000;
437527
const bootloader_state_t *bs = (const bootloader_state_t *)partition_info;
438528
const uint32_t boot_size = bs->ota[0].offset + bs->ota[0].size - boot_base;
439529

440530
if (!esp_sdk_update_from_v2())
441531
return 0;
442532

443-
if (esp_get_updated_partition_table_addr() == CONFIG_PARTITION_TABLE_OFFSET)
444-
return 0;
533+
if (esp_get_updated_partition_table_addr() != CONFIG_PARTITION_TABLE_OFFSET) {
534+
ESP_LOGD(TAG, "Copy firmware1 from %d total %d", boot_base + PARTITION_DATA_OFFSET, boot_size);
535+
536+
ESP_LOGI(TAG, "Start unpacking V3 firmware ...");
537+
538+
ret = esp_flash_sector_copy(boot_base, boot_base + PARTITION_DATA_OFFSET, boot_size);
539+
if (ret) {
540+
ESP_LOGE(TAG, "Fail to copy V3 firmware from 0x%x to 0x%x total %d", boot_base + PARTITION_DATA_OFFSET,
541+
boot_base, boot_size);
542+
return -1;
543+
}
445544

446-
ESP_LOGD(TAG, "Copy firmware1 from %d total %d", boot_base + PARTITION_DATA_OFFSET, boot_size);
545+
ret = esp_set_v2boot_app1();
546+
if (ret) {
547+
ESP_LOGE(TAG, "Fail to set V2 app1 default");
548+
return -1;
549+
}
447550

448-
ESP_LOGI(TAG, "Start unpacking V3 firmware ...");
551+
ESP_LOGI(TAG, "Pack V3 firmware successfully and start to reboot");
449552

450-
ret = esp_flash_sector_copy(boot_base, boot_base + PARTITION_DATA_OFFSET, boot_size);
451-
if (ret) {
452-
ESP_LOGE(TAG, "Fail to copy V3 firmware from 0x%x to 0x%x total %d", boot_base + PARTITION_DATA_OFFSET,
453-
boot_base, boot_size);
454-
return -1;
553+
reboot = 1;
455554
}
456555

457-
ret = esp_set_v2boot_app1();
458-
if (ret) {
459-
ESP_LOGE(TAG, "Fail to set V2 app1 default");
460-
return -1;
461-
}
556+
#ifdef CONFIG_LOAD_OLD_RF_PARAMETER
557+
const uint32_t rf_dest = bs->rf.offset;
558+
const uint32_t rf_src = s_v2_flash_bin_size - 4 * 4096;
559+
const uint32_t rf_size = 128;
560+
561+
if (esp_rf_param_need_unpack(rf_dest, rf_src, rf_size)) {
562+
563+
ESP_LOGI(TAG, "Start unpacking RF parameters from 0x%x to 0x%x total size 0x%x ...", rf_src, rf_dest, rf_size);
462564

463-
ESP_LOGI(TAG, "Pack V3 firmware successfully and start to reboot");
565+
ret = esp_unpack_old_rf_param(rf_dest, rf_src, rf_size);
566+
if (ret) {
567+
ESP_LOGE(TAG, "Fail to copy RF parameters from 0x%x to 0x%x total %d", rf_src, rf_dest, rf_size);
568+
return -1;
569+
}
570+
571+
reboot = 1;
572+
} else {
573+
ESP_LOGD(TAG, "RF parameters is same");
574+
}
575+
#endif
464576

465-
esp_hw_reset();
577+
if (reboot)
578+
esp_hw_reset(4, 1);
466579

467580
return 0;
468581
}

0 commit comments

Comments
 (0)