]> granicus.if.org Git - esp-idf/blob - components/esp32/system_api.c
Merge branch 'feature/uart_add_api_get_cmd_position' into 'master'
[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/gpio_reg.h"
28 #include "soc/efuse_reg.h"
29 #include "soc/rtc_cntl_reg.h"
30 #include "soc/timer_group_reg.h"
31 #include "soc/timer_group_struct.h"
32 #include "soc/cpu.h"
33 #include "soc/rtc.h"
34 #include "freertos/FreeRTOS.h"
35 #include "freertos/task.h"
36 #include "freertos/xtensa_api.h"
37 #include "esp_heap_caps.h"
38
39 static const char* TAG = "system_api";
40
41 static uint8_t base_mac_addr[6] = { 0 };
42
43 #define SHUTDOWN_HANDLERS_NO 2
44 static shutdown_handler_t shutdown_handlers[SHUTDOWN_HANDLERS_NO];
45
46 void system_init()
47 {
48 }
49
50 esp_err_t esp_base_mac_addr_set(uint8_t *mac)
51 {
52     if (mac == NULL) {
53         ESP_LOGE(TAG, "Base MAC address is NULL");
54         abort();
55     }
56
57     memcpy(base_mac_addr, mac, 6);
58
59     return ESP_OK;
60 }
61
62 esp_err_t esp_base_mac_addr_get(uint8_t *mac)
63 {
64     uint8_t null_mac[6] = {0};
65
66     if (memcmp(base_mac_addr, null_mac, 6) == 0) {
67         ESP_LOGI(TAG, "Base MAC address is not set, read default base MAC address from BLK0 of EFUSE");
68         return ESP_ERR_INVALID_MAC;
69     }
70
71     memcpy(mac, base_mac_addr, 6);
72
73     return ESP_OK;
74 }
75
76 esp_err_t esp_efuse_mac_get_custom(uint8_t *mac)
77 {
78     uint32_t mac_low;
79     uint32_t mac_high;
80     uint8_t efuse_crc;
81     uint8_t calc_crc;
82
83     uint8_t version = REG_READ(EFUSE_BLK3_RDATA5_REG) >> 24;
84
85     if (version != 1) {
86         ESP_LOGE(TAG, "Base MAC address from BLK3 of EFUSE version error, version = %d", version);
87         return ESP_ERR_INVALID_VERSION;
88     }
89
90     mac_low = REG_READ(EFUSE_BLK3_RDATA1_REG);
91     mac_high = REG_READ(EFUSE_BLK3_RDATA0_REG);
92
93     mac[0] = mac_high >> 8;
94     mac[1] = mac_high >> 16;
95     mac[2] = mac_high >> 24;
96     mac[3] = mac_low;
97     mac[4] = mac_low >> 8;
98     mac[5] = mac_low >> 16;
99
100     efuse_crc = mac_high;
101
102     calc_crc = esp_crc8(mac, 6);
103
104     if (efuse_crc != calc_crc) {
105         ESP_LOGE(TAG, "Base MAC address from BLK3 of EFUSE CRC error, efuse_crc = 0x%02x; calc_crc = 0x%02x", efuse_crc, calc_crc);
106         return ESP_ERR_INVALID_CRC;
107     }
108     return ESP_OK;
109 }
110
111 esp_err_t esp_efuse_mac_get_default(uint8_t* mac)
112 {
113     uint32_t mac_low;
114     uint32_t mac_high;
115     uint8_t efuse_crc;
116     uint8_t calc_crc;
117
118     mac_low = REG_READ(EFUSE_BLK0_RDATA1_REG);
119     mac_high = REG_READ(EFUSE_BLK0_RDATA2_REG);
120
121     mac[0] = mac_high >> 8;
122     mac[1] = mac_high;
123     mac[2] = mac_low >> 24;
124     mac[3] = mac_low >> 16;
125     mac[4] = mac_low >> 8;
126     mac[5] = mac_low;
127
128     efuse_crc = mac_high >> 16;
129
130     calc_crc = esp_crc8(mac, 6);
131
132     if (efuse_crc != calc_crc) {
133          // Small range of MAC addresses are accepted even if CRC is invalid.
134          // These addresses are reserved for Espressif internal use.
135         if ((mac_high & 0xFFFF) == 0x18fe) {
136             if ((mac_low >= 0x346a85c7) && (mac_low <= 0x346a85f8)) {
137                 return ESP_OK;
138             }
139         } else {
140             ESP_LOGE(TAG, "Base MAC address from BLK0 of EFUSE CRC error, efuse_crc = 0x%02x; calc_crc = 0x%02x", efuse_crc, calc_crc);
141             abort();
142         }
143     }
144     return ESP_OK;
145 }
146
147 esp_err_t system_efuse_read_mac(uint8_t *mac) __attribute__((alias("esp_efuse_mac_get_default")));
148 esp_err_t esp_efuse_read_mac(uint8_t *mac) __attribute__((alias("esp_efuse_mac_get_default")));
149
150 esp_err_t esp_derive_mac(uint8_t* local_mac, const uint8_t* universal_mac)
151 {
152     uint8_t idx;
153
154     if (local_mac == NULL || universal_mac == NULL) {
155         ESP_LOGE(TAG, "mac address param is NULL");
156         return ESP_ERR_INVALID_ARG;
157     }
158
159     memcpy(local_mac, universal_mac, 6);
160     for (idx = 0; idx < 64; idx++) {
161         local_mac[0] = universal_mac[0] | 0x02;
162         local_mac[0] ^= idx << 2;
163
164         if (memcmp(local_mac, universal_mac, 6)) {
165             break;
166         }
167     }
168
169     return ESP_OK;
170 }
171
172 esp_err_t esp_read_mac(uint8_t* mac, esp_mac_type_t type)
173 {
174     uint8_t efuse_mac[6];
175
176     if (mac == NULL) {
177         ESP_LOGE(TAG, "mac address param is NULL");
178         return ESP_ERR_INVALID_ARG;
179     }
180
181     if (type < ESP_MAC_WIFI_STA || type > ESP_MAC_ETH) {
182         ESP_LOGE(TAG, "mac type is incorrect");
183         return ESP_ERR_INVALID_ARG;
184     }
185
186     _Static_assert(UNIVERSAL_MAC_ADDR_NUM == FOUR_UNIVERSAL_MAC_ADDR \
187             || UNIVERSAL_MAC_ADDR_NUM == TWO_UNIVERSAL_MAC_ADDR, \
188             "incorrect NUM_MAC_ADDRESS_FROM_EFUSE value");
189
190     if (esp_base_mac_addr_get(efuse_mac) != ESP_OK) {
191         esp_efuse_mac_get_default(efuse_mac);
192     }
193
194     switch (type) {
195     case ESP_MAC_WIFI_STA:
196         memcpy(mac, efuse_mac, 6);
197         break;
198     case ESP_MAC_WIFI_SOFTAP:
199         if (UNIVERSAL_MAC_ADDR_NUM == FOUR_UNIVERSAL_MAC_ADDR) {
200             memcpy(mac, efuse_mac, 6);
201             mac[5] += 1;
202         }
203         else if (UNIVERSAL_MAC_ADDR_NUM == TWO_UNIVERSAL_MAC_ADDR) {
204             esp_derive_mac(mac, efuse_mac);
205         }
206         break;
207     case ESP_MAC_BT:
208         memcpy(mac, efuse_mac, 6);
209         if (UNIVERSAL_MAC_ADDR_NUM == FOUR_UNIVERSAL_MAC_ADDR) {
210             mac[5] += 2;
211         }
212         else if (UNIVERSAL_MAC_ADDR_NUM == TWO_UNIVERSAL_MAC_ADDR) {
213             mac[5] += 1;
214         }
215         break;
216     case ESP_MAC_ETH:
217         if (UNIVERSAL_MAC_ADDR_NUM == FOUR_UNIVERSAL_MAC_ADDR) {
218             memcpy(mac, efuse_mac, 6);
219             mac[5] += 3;
220         }
221         else if (UNIVERSAL_MAC_ADDR_NUM == TWO_UNIVERSAL_MAC_ADDR) {
222             efuse_mac[5] += 1;
223             esp_derive_mac(mac, efuse_mac);
224         }
225         break;
226     default:
227         ESP_LOGW(TAG, "incorrect mac type");
228         break;
229     }
230   
231     return ESP_OK;
232 }
233
234 esp_err_t esp_register_shutdown_handler(shutdown_handler_t handler)
235 {
236      int i;
237      for (i = 0; i < SHUTDOWN_HANDLERS_NO; i++) {
238           if (shutdown_handlers[i] == NULL) {
239                shutdown_handlers[i] = handler;
240                return ESP_OK;
241           }
242      }
243      return ESP_FAIL;
244 }
245
246 void esp_restart_noos() __attribute__ ((noreturn));
247
248 void IRAM_ATTR esp_restart(void)
249 {
250      int i;
251      for (i = 0; i < SHUTDOWN_HANDLERS_NO; i++) {
252           if (shutdown_handlers[i]) {
253                shutdown_handlers[i]();
254           }
255      }
256
257     // Disable scheduler on this core.
258     vTaskSuspendAll();
259
260     esp_restart_noos();
261 }
262
263 /* "inner" restart function for after RTOS, interrupts & anything else on this
264  * core are already stopped. Stalls other core, resets hardware,
265  * triggers restart.
266 */
267 void IRAM_ATTR esp_restart_noos()
268 {
269     // Disable interrupts
270     xt_ints_off(0xFFFFFFFF);
271
272     // Enable RTC watchdog for 1 second
273     REG_WRITE(RTC_CNTL_WDTWPROTECT_REG, RTC_CNTL_WDT_WKEY_VALUE);
274     REG_WRITE(RTC_CNTL_WDTCONFIG0_REG,
275             RTC_CNTL_WDT_FLASHBOOT_MOD_EN_M |
276             (RTC_WDT_STG_SEL_RESET_SYSTEM << RTC_CNTL_WDT_STG0_S) |
277             (RTC_WDT_STG_SEL_RESET_RTC << RTC_CNTL_WDT_STG1_S) |
278             (1 << RTC_CNTL_WDT_SYS_RESET_LENGTH_S) |
279             (1 << RTC_CNTL_WDT_CPU_RESET_LENGTH_S) );
280     REG_WRITE(RTC_CNTL_WDTCONFIG1_REG, rtc_clk_slow_freq_get_hz() * 1);
281
282     // Reset and stall the other CPU.
283     // CPU must be reset before stalling, in case it was running a s32c1i
284     // instruction. This would cause memory pool to be locked by arbiter
285     // to the stalled CPU, preventing current CPU from accessing this pool.
286     const uint32_t core_id = xPortGetCoreID();
287     const uint32_t other_core_id = (core_id == 0) ? 1 : 0;
288     esp_cpu_reset(other_core_id);
289     esp_cpu_stall(other_core_id);
290
291     // Other core is now stalled, can access DPORT registers directly
292     esp_dport_access_int_abort();
293
294     // Disable TG0/TG1 watchdogs
295     TIMERG0.wdt_wprotect=TIMG_WDT_WKEY_VALUE;
296     TIMERG0.wdt_config0.en = 0;
297     TIMERG0.wdt_wprotect=0;
298     TIMERG1.wdt_wprotect=TIMG_WDT_WKEY_VALUE;
299     TIMERG1.wdt_config0.en = 0;
300     TIMERG1.wdt_wprotect=0;
301
302     // Flush any data left in UART FIFOs
303     uart_tx_wait_idle(0);
304     uart_tx_wait_idle(1);
305     uart_tx_wait_idle(2);
306
307     // Disable cache
308     Cache_Read_Disable(0);
309     Cache_Read_Disable(1);
310
311     // 2nd stage bootloader reconfigures SPI flash signals.
312     // Reset them to the defaults expected by ROM.
313     WRITE_PERI_REG(GPIO_FUNC0_IN_SEL_CFG_REG, 0x30);
314     WRITE_PERI_REG(GPIO_FUNC1_IN_SEL_CFG_REG, 0x30);
315     WRITE_PERI_REG(GPIO_FUNC2_IN_SEL_CFG_REG, 0x30);
316     WRITE_PERI_REG(GPIO_FUNC3_IN_SEL_CFG_REG, 0x30);
317     WRITE_PERI_REG(GPIO_FUNC4_IN_SEL_CFG_REG, 0x30);
318     WRITE_PERI_REG(GPIO_FUNC5_IN_SEL_CFG_REG, 0x30);
319
320     // Reset wifi/bluetooth/ethernet/sdio (bb/mac)
321     DPORT_SET_PERI_REG_MASK(DPORT_CORE_RST_EN_REG, 
322          DPORT_BB_RST | DPORT_FE_RST | DPORT_MAC_RST |
323          DPORT_BT_RST | DPORT_BTMAC_RST | DPORT_SDIO_RST |
324          DPORT_SDIO_HOST_RST | DPORT_EMAC_RST | DPORT_MACPWR_RST | 
325          DPORT_RW_BTMAC_RST | DPORT_RW_BTLP_RST);
326     DPORT_REG_WRITE(DPORT_CORE_RST_EN_REG, 0);
327
328     // Reset timer/spi/uart
329     DPORT_SET_PERI_REG_MASK(DPORT_PERIP_RST_EN_REG,
330             DPORT_TIMERS_RST | DPORT_SPI_RST_1 | DPORT_UART_RST);
331     DPORT_REG_WRITE(DPORT_PERIP_RST_EN_REG, 0);
332
333     // Set CPU back to XTAL source, no PLL, same as hard reset
334     rtc_clk_cpu_freq_set(RTC_CPU_FREQ_XTAL);
335
336     // Clear entry point for APP CPU
337     DPORT_REG_WRITE(DPORT_APPCPU_CTRL_D_REG, 0);
338
339     // Reset CPUs
340     if (core_id == 0) {
341         // Running on PRO CPU: APP CPU is stalled. Can reset both CPUs.
342         esp_cpu_reset(1);
343         esp_cpu_reset(0);
344     } else {
345         // Running on APP CPU: need to reset PRO CPU and unstall it,
346         // then reset APP CPU
347         esp_cpu_reset(0);
348         esp_cpu_unstall(0);
349         esp_cpu_reset(1);
350     }
351     while(true) {
352         ;
353     }
354 }
355
356 void system_restart(void) __attribute__((alias("esp_restart")));
357
358 uint32_t esp_get_free_heap_size( void )
359 {
360     return heap_caps_get_free_size( MALLOC_CAP_DEFAULT );
361 }
362
363 uint32_t esp_get_minimum_free_heap_size( void )
364 {
365     return heap_caps_get_minimum_free_size( MALLOC_CAP_DEFAULT );
366 }
367
368 uint32_t system_get_free_heap_size(void) __attribute__((alias("esp_get_free_heap_size")));
369
370 const char* system_get_sdk_version(void)
371 {
372     return "master";
373 }
374
375 const char* esp_get_idf_version(void)
376 {
377     return IDF_VER;
378 }
379
380 static void get_chip_info_esp32(esp_chip_info_t* out_info)
381 {
382     uint32_t reg = REG_READ(EFUSE_BLK0_RDATA3_REG);
383     memset(out_info, 0, sizeof(*out_info));
384     
385     out_info->model = CHIP_ESP32;
386     if ((reg & EFUSE_RD_CHIP_VER_REV1_M) != 0) {
387         out_info->revision = 1;
388     }
389     if ((reg & EFUSE_RD_CHIP_VER_DIS_APP_CPU_M) == 0) {
390         out_info->cores = 2;
391     } else {
392         out_info->cores = 1;
393     }
394     out_info->features = CHIP_FEATURE_WIFI_BGN;
395     if ((reg & EFUSE_RD_CHIP_VER_DIS_BT_M) == 0) {
396         out_info->features |= CHIP_FEATURE_BT | CHIP_FEATURE_BLE;
397     }
398     int package = (reg & EFUSE_RD_CHIP_VER_PKG_M) >> EFUSE_RD_CHIP_VER_PKG_S;
399     if (package == EFUSE_RD_CHIP_VER_PKG_ESP32D2WDQ5 ||
400         package == EFUSE_RD_CHIP_VER_PKG_ESP32PICOD2 ||
401         package == EFUSE_RD_CHIP_VER_PKG_ESP32PICOD4) {
402         out_info->features |= CHIP_FEATURE_EMB_FLASH;
403     }
404 }
405
406 void esp_chip_info(esp_chip_info_t* out_info)
407 {
408     // Only ESP32 is supported now, in the future call one of the
409     // chip-specific functions based on sdkconfig choice
410     return get_chip_info_esp32(out_info);
411 }