system_api.c 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182
  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. #include "esp_system.h"
  15. #include "esp_attr.h"
  16. #include "esp_wifi.h"
  17. #include "esp_wifi_internal.h"
  18. #include "esp_log.h"
  19. #include "sdkconfig.h"
  20. #include "rom/efuse.h"
  21. #include "rom/cache.h"
  22. #include "rom/uart.h"
  23. #include "soc/dport_reg.h"
  24. #include "soc/efuse_reg.h"
  25. #include "soc/rtc_cntl_reg.h"
  26. #include "soc/timer_group_reg.h"
  27. #include "soc/timer_group_struct.h"
  28. #include "soc/cpu.h"
  29. #include "freertos/FreeRTOS.h"
  30. #include "freertos/task.h"
  31. #include "freertos/xtensa_api.h"
  32. #include "rtc.h"
  33. static const char* TAG = "system_api";
  34. void system_init()
  35. {
  36. }
  37. esp_err_t esp_efuse_read_mac(uint8_t* mac)
  38. {
  39. uint8_t efuse_crc;
  40. uint8_t calc_crc;
  41. uint32_t mac_low = REG_READ(EFUSE_BLK0_RDATA1_REG);
  42. uint32_t mac_high = REG_READ(EFUSE_BLK0_RDATA2_REG);
  43. mac[0] = mac_high >> 8;
  44. mac[1] = mac_high;
  45. mac[2] = mac_low >> 24;
  46. mac[3] = mac_low >> 16;
  47. mac[4] = mac_low >> 8;
  48. mac[5] = mac_low;
  49. efuse_crc = mac_high >> 16;
  50. calc_crc = esp_crc8(mac, 6);
  51. if (efuse_crc != calc_crc) {
  52. // Small range of MAC addresses are accepted even if CRC is invalid.
  53. // These addresses are reserved for Espressif internal use.
  54. if ((mac_high & 0xFFFF) == 0x18fe) {
  55. if ((mac_low >= 0x346a85c7) && (mac_low <= 0x346a85f8)) {
  56. return ESP_OK;
  57. }
  58. } else {
  59. ESP_LOGE(TAG, "MAC address CRC error, efuse_crc = 0x%02x; calc_crc = 0x%02x", efuse_crc, calc_crc);
  60. abort();
  61. }
  62. }
  63. return ESP_OK;
  64. }
  65. esp_err_t system_efuse_read_mac(uint8_t mac[6]) __attribute__((alias("esp_efuse_read_mac")));
  66. void esp_restart_noos() __attribute__ ((noreturn));
  67. void IRAM_ATTR esp_restart(void)
  68. {
  69. #ifdef CONFIG_WIFI_ENABLED
  70. esp_wifi_stop();
  71. #endif
  72. // Disable scheduler on this core.
  73. vTaskSuspendAll();
  74. esp_restart_noos();
  75. }
  76. /* "inner" restart function for after RTOS, interrupts & anything else on this
  77. * core are already stopped. Stalls other core, resets hardware,
  78. * triggers restart.
  79. */
  80. void IRAM_ATTR esp_restart_noos()
  81. {
  82. const uint32_t core_id = xPortGetCoreID();
  83. const uint32_t other_core_id = core_id == 0 ? 1 : 0;
  84. esp_cpu_stall(other_core_id);
  85. // We need to disable TG0/TG1 watchdogs
  86. // First enable RTC watchdog to be on the safe side
  87. REG_WRITE(RTC_CNTL_WDTWPROTECT_REG, RTC_CNTL_WDT_WKEY_VALUE);
  88. REG_WRITE(RTC_CNTL_WDTCONFIG0_REG,
  89. RTC_CNTL_WDT_FLASHBOOT_MOD_EN_M |
  90. (1 << RTC_CNTL_WDT_SYS_RESET_LENGTH_S) |
  91. (1 << RTC_CNTL_WDT_CPU_RESET_LENGTH_S) );
  92. REG_WRITE(RTC_CNTL_WDTCONFIG1_REG, 128000);
  93. // Disable TG0/TG1 watchdogs
  94. TIMERG0.wdt_wprotect=TIMG_WDT_WKEY_VALUE;
  95. TIMERG0.wdt_config0.en = 0;
  96. TIMERG0.wdt_wprotect=0;
  97. TIMERG1.wdt_wprotect=TIMG_WDT_WKEY_VALUE;
  98. TIMERG1.wdt_config0.en = 0;
  99. TIMERG1.wdt_wprotect=0;
  100. // Disable all interrupts
  101. xt_ints_off(0xFFFFFFFF);
  102. // Disable cache
  103. Cache_Read_Disable(0);
  104. Cache_Read_Disable(1);
  105. // Flush any data left in UART FIFOs
  106. uart_tx_wait_idle(0);
  107. uart_tx_wait_idle(1);
  108. uart_tx_wait_idle(2);
  109. // Reset wifi/bluetooth/ethernet/sdio (bb/mac)
  110. SET_PERI_REG_MASK(DPORT_CORE_RST_EN_REG,
  111. DPORT_BB_RST | DPORT_FE_RST | DPORT_MAC_RST |
  112. DPORT_BT_RST | DPORT_BTMAC_RST | DPORT_SDIO_RST |
  113. DPORT_SDIO_HOST_RST | DPORT_EMAC_RST | DPORT_MACPWR_RST |
  114. DPROT_RW_BTMAC_RST | DPROT_RW_BTLP_RST);
  115. REG_WRITE(DPORT_CORE_RST_EN_REG, 0);
  116. // Reset timer/spi/uart
  117. SET_PERI_REG_MASK(DPORT_PERIP_RST_EN_REG,
  118. DPORT_TIMERS_RST | DPORT_SPI_RST_1 | DPORT_UART_RST);
  119. REG_WRITE(DPORT_PERIP_RST_EN_REG, 0);
  120. // Set CPU back to XTAL source, no PLL, same as hard reset
  121. rtc_set_cpu_freq(CPU_XTAL);
  122. // Reset CPUs
  123. if (core_id == 0) {
  124. // Running on PRO CPU: APP CPU is stalled. Can reset both CPUs.
  125. SET_PERI_REG_MASK(RTC_CNTL_OPTIONS0_REG,
  126. RTC_CNTL_SW_PROCPU_RST_M | RTC_CNTL_SW_APPCPU_RST_M);
  127. } else {
  128. // Running on APP CPU: need to reset PRO CPU and unstall it,
  129. // then stall APP CPU
  130. SET_PERI_REG_MASK(RTC_CNTL_OPTIONS0_REG, RTC_CNTL_SW_PROCPU_RST_M);
  131. esp_cpu_unstall(0);
  132. esp_cpu_stall(1);
  133. }
  134. while(true) {
  135. ;
  136. }
  137. }
  138. void system_restart(void) __attribute__((alias("esp_restart")));
  139. void system_restore(void)
  140. {
  141. esp_wifi_restore();
  142. }
  143. uint32_t esp_get_free_heap_size(void)
  144. {
  145. return xPortGetFreeHeapSize();
  146. }
  147. uint32_t system_get_free_heap_size(void) __attribute__((alias("esp_get_free_heap_size")));
  148. const char* system_get_sdk_version(void)
  149. {
  150. return "master";
  151. }