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

Skip to content

Commit eef781f

Browse files
donghengdongheng
authored andcommitted
fix(esp8266): Fix esp8266 load RTC segment when reset from deep sleep
Now only 1MB flash is mapped to SoC bus.
1 parent 3c68b66 commit eef781f

File tree

4 files changed

+56
-11
lines changed

4 files changed

+56
-11
lines changed

components/esp8266/include/esp8266/eagle_soc.h

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -178,10 +178,17 @@
178178
#define IRAM_SIZE (48 * 1024)
179179

180180
#define FLASH_BASE (0x40200000)
181-
#define FLASH_SIZE (16 * 1024 * 1024)
181+
#define FLASH_SIZE (1 * 1024 * 1024)
182+
183+
#define RTC_SYS_BASE (0x60001000)
184+
#define RTC_SYS_SIZE (0x200)
185+
186+
#define RTC_USER_BASE (0x60001200)
187+
#define RTC_USER_SIZE (0x200)
182188

183189
#define IS_DRAM(a) ((size_t)(a) >= DRAM_BASE && (size_t)(a) < (DRAM_BASE + DRAM_SIZE))
184190
#define IS_IRAM(a) ((size_t)(a) >= IRAM_BASE && (size_t)(a) < (IRAM_BASE + IRAM_SIZE))
185191
#define IS_FLASH(a) ((size_t)(a) >= FLASH_BASE && (size_t)(a) < (FLASH_BASE + FLASH_SIZE))
192+
#define IS_USR_RTC(a) ((size_t)(a) >= RTC_USER_BASE && (size_t)(a) < (RTC_USER_BASE + RTC_USER_SIZE))
186193

187194
#endif //_EAGLE_SOC_H_

components/esp8266/include/internal/esp_system_internal.h

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
#pragma once
1616

1717
#include <stdint.h>
18+
#include <esp_system.h>
1819

1920
#ifdef __cplusplus
2021
extern "C" {
@@ -48,6 +49,13 @@ void esp_reset_reason_init(void);
4849
*/
4950
void esp_reset_reason_set_hint(esp_reset_reason_t hint);
5051

52+
/**
53+
* @brief Get reason of last reset but not clear it for next reset
54+
*
55+
* @return See description of esp_reset_reason_t for explanation of each value.
56+
*/
57+
esp_reset_reason_t esp_reset_reason_early(void);
58+
5159
#ifdef __cplusplus
5260
}
5361
#endif

components/esp8266/source/reset_reason.c

Lines changed: 25 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,6 @@
2121
#include "esp_log.h"
2222
#include "esp_libc.h"
2323

24-
#if CONFIG_RESET_REASON
25-
2624
#define RTC_RESET_SW_CAUSE_REG RTC_STORE0
2725
#define RTC_RESET_HW_CAUSE_REG RTC_STATE1
2826
#define RTC_WAKEUP_HW_CAUSE_REG RTC_STATE2
@@ -96,9 +94,9 @@ static inline uint32_t get_reset_reason(uint32_t rtc_reset_reason, uint32_t rese
9694
}
9795

9896
/**
99-
* @brief Internal function to get SoC reset reason at system initialization
97+
* Internal function to initialize SoC reset reason at system initialization
10098
*/
101-
void esp_reset_reason_init(void)
99+
static void __esp_reset_reason_init(int init)
102100
{
103101
const uint32_t hw_reset = esp_rtc_get_reset_reason();
104102
#if CONFIG_RESET_REASON_CHECK_WAKEUP
@@ -109,13 +107,24 @@ void esp_reset_reason_init(void)
109107
const uint32_t hint = esp_reset_reason_get_hint(hw_reset);
110108

111109
s_reset_reason = get_reset_reason(hw_reset, hint);
112-
if (hint != ESP_RST_UNKNOWN) {
110+
if (init && hint != ESP_RST_UNKNOWN) {
113111
esp_reset_reason_clear_hint();
114112
}
115113

116-
ESP_LOGI(TAG, "RTC reset %u wakeup %u store %u, reason is %u", hw_reset, hw_wakeup, hint, s_reset_reason);
114+
if (init)
115+
ESP_LOGI(TAG, "RTC reset %u wakeup %u store %u, reason is %u", hw_reset, hw_wakeup, hint, s_reset_reason);
116+
}
117+
118+
/**
119+
* @brief Internal function to get SoC reset reason at system initialization
120+
*/
121+
void esp_reset_reason_init(void)
122+
{
123+
__esp_reset_reason_init(1);
117124
}
118125

126+
#if CONFIG_RESET_REASON
127+
119128
/**
120129
* @brief Internal function to set reset reason hint
121130
*/
@@ -143,3 +152,13 @@ void esp_reset_reason_set_hint(esp_reset_reason_t hint)
143152
}
144153

145154
#endif /* CONFIG_RESET_REASON */
155+
156+
/**
157+
* Get reason of last reset but not clear it for next reset
158+
*/
159+
esp_reset_reason_t esp_reset_reason_early(void)
160+
{
161+
__esp_reset_reason_init(0);
162+
163+
return (esp_reset_reason_t)s_reset_reason;
164+
}

components/esp8266/source/startup.c

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -30,9 +30,7 @@
3030
#include "esp_task_wdt.h"
3131
#include "internal/esp_wifi_internal.h"
3232
#include "internal/esp_system_internal.h"
33-
34-
#define FLASH_MAP_ADDR 0x40200000
35-
#define FLASH_MAP_SIZE 0x00100000
33+
#include "esp8266/eagle_soc.h"
3634

3735
extern void chip_boot(void);
3836
extern int rtc_init(void);
@@ -44,6 +42,16 @@ extern int wifi_nvs_init(void);
4442
extern esp_err_t esp_pthread_init(void);
4543
extern void phy_get_bb_evm(void);
4644

45+
static inline int should_load(uint32_t load_addr)
46+
{
47+
if (IS_USR_RTC(load_addr)) {
48+
if (esp_reset_reason_early() == ESP_RST_DEEPSLEEP)
49+
return 0;
50+
}
51+
52+
return 1;
53+
}
54+
4755
static void user_init_entry(void *param)
4856
{
4957
void (**func)(void);
@@ -97,7 +105,7 @@ void call_user_start(size_t start_addr)
97105

98106
extern int _bss_start, _bss_end;
99107

100-
esp_image_header_t *head = (esp_image_header_t *)(FLASH_MAP_ADDR + (start_addr & (FLASH_MAP_SIZE - 1)));
108+
esp_image_header_t *head = (esp_image_header_t *)(FLASH_BASE + (start_addr & (FLASH_SIZE - 1)));
101109
esp_image_segment_header_t *segment = (esp_image_segment_header_t *)((uintptr_t)head + sizeof(esp_image_header_t));
102110

103111
/* The data in flash cannot be accessed by byte in this stage, so just access by word and get the segment count. */
@@ -106,6 +114,9 @@ void call_user_start(size_t start_addr)
106114
for (i = 0; i < segment_count - 1; i++) {
107115
segment = (esp_image_segment_header_t *)((uintptr_t)segment + sizeof(esp_image_segment_header_t) + segment->data_len);
108116

117+
if (!should_load(segment->load_addr))
118+
continue;
119+
109120
uint32_t *dest = (uint32_t *)segment->load_addr;
110121
uint32_t *src = (uint32_t *)((uintptr_t)segment + sizeof(esp_image_segment_header_t));
111122
uint32_t size = segment->data_len / sizeof(uint32_t);

0 commit comments

Comments
 (0)