]> granicus.if.org Git - esp-idf/blob - components/esp32/system_api.c
1b541f1cbffa7965f40941811dabb4227c3687cd
[esp-idf] / components / esp32 / system_api.c
1 // Copyright 2013-2016 Espressif Systems (Shanghai) PTE LTD
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 //     http://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
14
15 #include <string.h>
16
17 #include "esp_system.h"
18 #include "esp_attr.h"
19 #include "esp_wifi.h"
20 #include "esp_wifi_internal.h"
21 #include "esp_log.h"
22 #include "sdkconfig.h"
23 #include "rom/efuse.h"
24 #include "rom/cache.h"
25 #include "rom/uart.h"
26 #include "soc/dport_reg.h"
27 #include "soc/efuse_reg.h"
28 #include "soc/rtc_cntl_reg.h"
29 #include "soc/timer_group_reg.h"
30 #include "soc/timer_group_struct.h"
31 #include "soc/cpu.h"
32 #include "soc/rtc.h"
33 #include "freertos/FreeRTOS.h"
34 #include "freertos/task.h"
35 #include "freertos/xtensa_api.h"
36 #include "esp_heap_caps.h"
37
38 static const char* TAG = "system_api";
39
40 static uint8_t base_mac_addr[6] = { 0 };
41
42 void system_init()
43 {
44 }
45
46 esp_err_t esp_base_mac_addr_set(uint8_t *mac)
47 {
48     if (mac == NULL) {
49         ESP_LOGE(TAG, "Base MAC address is NULL");
50         abort();
51     }
52
53     memcpy(base_mac_addr, mac, 6);
54
55     return ESP_OK;
56 }
57
58 esp_err_t esp_base_mac_addr_get(uint8_t *mac)
59 {
60     uint8_t null_mac[6] = {0};
61
62     if (memcmp(base_mac_addr, null_mac, 6) == 0) {
63         ESP_LOGI(TAG, "Base MAC address is not set, read default base MAC address from BLK0 of EFUSE");
64         return ESP_ERR_INVALID_MAC;
65     }
66
67     memcpy(mac, base_mac_addr, 6);
68
69     return ESP_OK;
70 }
71
72 esp_err_t esp_efuse_mac_get_custom(uint8_t *mac)
73 {
74     uint32_t mac_low;
75     uint32_t mac_high;
76     uint8_t efuse_crc;
77     uint8_t calc_crc;
78
79     uint8_t version = REG_READ(EFUSE_BLK3_RDATA5_REG) >> 24;
80
81     if (version != 1) {
82         ESP_LOGE(TAG, "Base MAC address from BLK3 of EFUSE version error, version = %d", version);
83         return ESP_ERR_INVALID_VERSION;
84     }
85
86     mac_low = REG_READ(EFUSE_BLK3_RDATA1_REG);
87     mac_high = REG_READ(EFUSE_BLK3_RDATA0_REG);
88
89     mac[0] = mac_high >> 8;
90     mac[1] = mac_high >> 16;
91     mac[2] = mac_high >> 24;
92     mac[3] = mac_low;
93     mac[4] = mac_low >> 8;
94     mac[5] = mac_low >> 16;
95
96     efuse_crc = mac_high;
97
98     calc_crc = esp_crc8(mac, 6);
99
100     if (efuse_crc != calc_crc) {
101         ESP_LOGE(TAG, "Base MAC address from BLK3 of EFUSE CRC error, efuse_crc = 0x%02x; calc_crc = 0x%02x", efuse_crc, calc_crc);
102         return ESP_ERR_INVALID_CRC;
103     }
104     return ESP_OK;
105 }
106
107 esp_err_t esp_efuse_mac_get_default(uint8_t* mac)
108 {
109     uint32_t mac_low;
110     uint32_t mac_high;
111     uint8_t efuse_crc;
112     uint8_t calc_crc;
113
114     mac_low = REG_READ(EFUSE_BLK0_RDATA1_REG);
115     mac_high = REG_READ(EFUSE_BLK0_RDATA2_REG);
116
117     mac[0] = mac_high >> 8;
118     mac[1] = mac_high;
119     mac[2] = mac_low >> 24;
120     mac[3] = mac_low >> 16;
121     mac[4] = mac_low >> 8;
122     mac[5] = mac_low;
123
124     efuse_crc = mac_high >> 16;
125
126     calc_crc = esp_crc8(mac, 6);
127
128     if (efuse_crc != calc_crc) {
129          // Small range of MAC addresses are accepted even if CRC is invalid.
130          // These addresses are reserved for Espressif internal use.
131         if ((mac_high & 0xFFFF) == 0x18fe) {
132             if ((mac_low >= 0x346a85c7) && (mac_low <= 0x346a85f8)) {
133                 return ESP_OK;
134             }
135         } else {
136             ESP_LOGE(TAG, "Base MAC address from BLK0 of EFUSE CRC error, efuse_crc = 0x%02x; calc_crc = 0x%02x", efuse_crc, calc_crc);
137             abort();
138         }
139     }
140     return ESP_OK;
141 }
142
143 esp_err_t system_efuse_read_mac(uint8_t *mac) __attribute__((alias("esp_efuse_mac_get_default")));
144 esp_err_t esp_efuse_read_mac(uint8_t *mac) __attribute__((alias("esp_efuse_mac_get_default")));
145
146 esp_err_t esp_derive_mac(uint8_t* local_mac, const uint8_t* universal_mac)
147 {
148     uint8_t idx;
149
150     if (local_mac == NULL || universal_mac == NULL) {
151         ESP_LOGE(TAG, "mac address param is NULL");
152         return ESP_ERR_INVALID_ARG;
153     }
154
155     memcpy(local_mac, universal_mac, 6);
156     for (idx = 0; idx < 64; idx++) {
157         local_mac[0] = universal_mac[0] | 0x02;
158         local_mac[0] ^= idx << 2;
159
160         if (memcmp(local_mac, universal_mac, 6)) {
161             break;
162         }
163     }
164
165     return ESP_OK;
166 }
167
168 esp_err_t esp_read_mac(uint8_t* mac, esp_mac_type_t type)
169 {
170     uint8_t efuse_mac[6];
171
172     if (mac == NULL) {
173         ESP_LOGE(TAG, "mac address param is NULL");
174         return ESP_ERR_INVALID_ARG;
175     }
176
177     if (type < ESP_MAC_WIFI_STA || type > ESP_MAC_ETH) {
178         ESP_LOGE(TAG, "mac type is incorrect");
179         return ESP_ERR_INVALID_ARG;
180     }
181
182     _Static_assert(UNIVERSAL_MAC_ADDR_NUM == FOUR_UNIVERSAL_MAC_ADDR \
183             || UNIVERSAL_MAC_ADDR_NUM == TWO_UNIVERSAL_MAC_ADDR, \
184             "incorrect NUM_MAC_ADDRESS_FROM_EFUSE value");
185
186     if (esp_base_mac_addr_get(efuse_mac) != ESP_OK) {
187         esp_efuse_mac_get_default(efuse_mac);
188     }
189
190     switch (type) {
191     case ESP_MAC_WIFI_STA:
192         memcpy(mac, efuse_mac, 6);
193         break;
194     case ESP_MAC_WIFI_SOFTAP:
195         if (UNIVERSAL_MAC_ADDR_NUM == FOUR_UNIVERSAL_MAC_ADDR) {
196             memcpy(mac, efuse_mac, 6);
197             mac[5] += 1;
198         }
199         else if (UNIVERSAL_MAC_ADDR_NUM == TWO_UNIVERSAL_MAC_ADDR) {
200             esp_derive_mac(mac, efuse_mac);
201         }
202         break;
203     case ESP_MAC_BT:
204         memcpy(mac, efuse_mac, 6);
205         if (UNIVERSAL_MAC_ADDR_NUM == FOUR_UNIVERSAL_MAC_ADDR) {
206             mac[5] += 2;
207         }
208         else if (UNIVERSAL_MAC_ADDR_NUM == TWO_UNIVERSAL_MAC_ADDR) {
209             mac[5] += 1;
210         }
211         break;
212     case ESP_MAC_ETH:
213         if (UNIVERSAL_MAC_ADDR_NUM == FOUR_UNIVERSAL_MAC_ADDR) {
214             memcpy(mac, efuse_mac, 6);
215             mac[5] += 3;
216         }
217         else if (UNIVERSAL_MAC_ADDR_NUM == TWO_UNIVERSAL_MAC_ADDR) {
218             efuse_mac[5] += 1;
219             esp_derive_mac(mac, efuse_mac);
220         }
221         break;
222     default:
223         ESP_LOGW(TAG, "incorrect mac type");
224         break;
225     }
226   
227     return ESP_OK;
228 }
229
230 void esp_restart_noos() __attribute__ ((noreturn));
231
232 /* Dummy function to be used instead of esp_wifi_stop if WiFi stack is not
233  * linked in (even though CONFIG_WIFI_ENABLED is set).
234  */
235 esp_err_t wifi_stop_noop()
236 {
237     return ESP_OK;
238 }
239
240 esp_err_t esp_wifi_stop(void) __attribute((weak, alias("wifi_stop_noop")));
241
242 void IRAM_ATTR esp_restart(void)
243 {
244 #ifdef CONFIG_WIFI_ENABLED
245     esp_wifi_stop();
246 #endif
247
248     // Disable scheduler on this core.
249     vTaskSuspendAll();
250
251     esp_restart_noos();
252 }
253
254 /* "inner" restart function for after RTOS, interrupts & anything else on this
255  * core are already stopped. Stalls other core, resets hardware,
256  * triggers restart.
257 */
258 void IRAM_ATTR esp_restart_noos()
259 {
260     const uint32_t core_id = xPortGetCoreID();
261     const uint32_t other_core_id = core_id == 0 ? 1 : 0;
262     esp_cpu_stall(other_core_id);
263
264     // other core is now stalled, can access DPORT registers directly
265     esp_dport_access_int_deinit();
266
267     // We need to disable TG0/TG1 watchdogs
268     // First enable RTC watchdog for 1 second
269     REG_WRITE(RTC_CNTL_WDTWPROTECT_REG, RTC_CNTL_WDT_WKEY_VALUE);
270     REG_WRITE(RTC_CNTL_WDTCONFIG0_REG,
271             RTC_CNTL_WDT_FLASHBOOT_MOD_EN_M |
272             (RTC_WDT_STG_SEL_RESET_SYSTEM << RTC_CNTL_WDT_STG0_S) |
273             (RTC_WDT_STG_SEL_RESET_RTC << RTC_CNTL_WDT_STG1_S) |
274             (1 << RTC_CNTL_WDT_SYS_RESET_LENGTH_S) |
275             (1 << RTC_CNTL_WDT_CPU_RESET_LENGTH_S) );
276     REG_WRITE(RTC_CNTL_WDTCONFIG1_REG, rtc_clk_slow_freq_get_hz() * 1);
277
278     // Disable TG0/TG1 watchdogs
279     TIMERG0.wdt_wprotect=TIMG_WDT_WKEY_VALUE;
280     TIMERG0.wdt_config0.en = 0;
281     TIMERG0.wdt_wprotect=0;
282     TIMERG1.wdt_wprotect=TIMG_WDT_WKEY_VALUE;
283     TIMERG1.wdt_config0.en = 0;
284     TIMERG1.wdt_wprotect=0;
285
286     // Disable all interrupts
287     xt_ints_off(0xFFFFFFFF);
288
289     // Disable cache
290     Cache_Read_Disable(0);
291     Cache_Read_Disable(1);
292
293     // Flush any data left in UART FIFOs
294     uart_tx_wait_idle(0);
295     uart_tx_wait_idle(1);
296     uart_tx_wait_idle(2);
297
298     // Reset wifi/bluetooth/ethernet/sdio (bb/mac)
299     DPORT_SET_PERI_REG_MASK(DPORT_CORE_RST_EN_REG, 
300          DPORT_BB_RST | DPORT_FE_RST | DPORT_MAC_RST |
301          DPORT_BT_RST | DPORT_BTMAC_RST | DPORT_SDIO_RST |
302          DPORT_SDIO_HOST_RST | DPORT_EMAC_RST | DPORT_MACPWR_RST | 
303          DPORT_RW_BTMAC_RST | DPORT_RW_BTLP_RST);
304     DPORT_REG_WRITE(DPORT_CORE_RST_EN_REG, 0);
305
306     // Reset timer/spi/uart
307     DPORT_SET_PERI_REG_MASK(DPORT_PERIP_RST_EN_REG,
308             DPORT_TIMERS_RST | DPORT_SPI_RST_1 | DPORT_UART_RST);
309     DPORT_REG_WRITE(DPORT_PERIP_RST_EN_REG, 0);
310
311     // Set CPU back to XTAL source, no PLL, same as hard reset
312     rtc_clk_cpu_freq_set(RTC_CPU_FREQ_XTAL);
313
314     // Clear entry point for APP CPU
315     DPORT_REG_WRITE(DPORT_APPCPU_CTRL_D_REG, 0);
316
317     // Reset CPUs
318     if (core_id == 0) {
319         // Running on PRO CPU: APP CPU is stalled. Can reset both CPUs.
320         SET_PERI_REG_MASK(RTC_CNTL_OPTIONS0_REG,
321                 RTC_CNTL_SW_PROCPU_RST_M | RTC_CNTL_SW_APPCPU_RST_M);
322     } else {
323         // Running on APP CPU: need to reset PRO CPU and unstall it,
324         // then reset APP CPU
325         SET_PERI_REG_MASK(RTC_CNTL_OPTIONS0_REG, RTC_CNTL_SW_PROCPU_RST_M);
326         esp_cpu_unstall(0);
327         SET_PERI_REG_MASK(RTC_CNTL_OPTIONS0_REG, RTC_CNTL_SW_APPCPU_RST_M);
328     }
329     while(true) {
330         ;
331     }
332 }
333
334 void system_restart(void) __attribute__((alias("esp_restart")));
335
336 void system_restore(void)
337 {
338     esp_wifi_restore();
339 }
340
341 uint32_t esp_get_free_heap_size( void )
342 {
343     return heap_caps_get_free_size( MALLOC_CAP_8BIT );
344 }
345
346 uint32_t esp_get_minimum_free_heap_size( void )
347 {
348     return heap_caps_get_minimum_free_size( MALLOC_CAP_8BIT );
349 }
350
351 uint32_t system_get_free_heap_size(void) __attribute__((alias("esp_get_free_heap_size")));
352
353 const char* system_get_sdk_version(void)
354 {
355     return "master";
356 }
357
358 const char* esp_get_idf_version(void)
359 {
360     return IDF_VER;
361 }
362
363 static void get_chip_info_esp32(esp_chip_info_t* out_info)
364 {
365     out_info->model = CHIP_ESP32;
366     uint32_t reg = REG_READ(EFUSE_BLK0_RDATA3_REG);
367     memset(out_info, 0, sizeof(*out_info));
368     if ((reg & EFUSE_RD_CHIP_VER_REV1_M) != 0) {
369         out_info->revision = 1;
370     }
371     if ((reg & EFUSE_RD_CHIP_VER_DIS_APP_CPU_M) == 0) {
372         out_info->cores = 2;
373     } else {
374         out_info->cores = 1;
375     }
376     out_info->features = CHIP_FEATURE_WIFI_BGN;
377     if ((reg & EFUSE_RD_CHIP_VER_DIS_BT_M) == 0) {
378         out_info->features |= CHIP_FEATURE_BT | CHIP_FEATURE_BLE;
379     }
380     if (((reg & EFUSE_RD_CHIP_VER_PKG_M) >> EFUSE_RD_CHIP_VER_PKG_S) ==
381             EFUSE_RD_CHIP_VER_PKG_ESP32D2WDQ5) {
382         out_info->features |= CHIP_FEATURE_EMB_FLASH;
383     }
384 }
385
386 void esp_chip_info(esp_chip_info_t* out_info)
387 {
388     // Only ESP32 is supported now, in the future call one of the
389     // chip-specific functions based on sdkconfig choice
390     return get_chip_info_esp32(out_info);
391 }