system_api.c 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  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 "rom/efuse.h"
  20. #include "rom/cache.h"
  21. #include "rom/uart.h"
  22. #include "soc/dport_reg.h"
  23. #include "soc/efuse_reg.h"
  24. #include "soc/rtc_cntl_reg.h"
  25. #include "soc/timer_group_reg.h"
  26. #include "soc/timer_group_struct.h"
  27. #include "soc/cpu.h"
  28. #include "freertos/FreeRTOS.h"
  29. #include "freertos/task.h"
  30. #include "freertos/xtensa_api.h"
  31. static const char* TAG = "system_api";
  32. void system_init()
  33. {
  34. }
  35. esp_err_t esp_efuse_read_mac(uint8_t* mac)
  36. {
  37. uint8_t efuse_crc;
  38. uint8_t calc_crc;
  39. uint32_t mac_low = REG_READ(EFUSE_BLK0_RDATA1_REG);
  40. uint32_t mac_high = REG_READ(EFUSE_BLK0_RDATA2_REG);
  41. mac[0] = mac_high >> 8;
  42. mac[1] = mac_high;
  43. mac[2] = mac_low >> 24;
  44. mac[3] = mac_low >> 16;
  45. mac[4] = mac_low >> 8;
  46. mac[5] = mac_low;
  47. efuse_crc = mac_high >> 16;
  48. calc_crc = esp_crc8(mac, 6);
  49. if (efuse_crc != calc_crc) {
  50. // Small range of MAC addresses are accepted even if CRC is invalid.
  51. // These addresses are reserved for Espressif internal use.
  52. if ((mac_high & 0xFFFF) == 0x18fe) {
  53. if ((mac_low >= 0x346a85c7) && (mac_low <= 0x346a85f8)) {
  54. return ESP_OK;
  55. }
  56. } else {
  57. ESP_LOGE(TAG, "MAC address CRC error, efuse_crc = 0x%02x; calc_crc = 0x%02x", efuse_crc, calc_crc);
  58. abort();
  59. }
  60. }
  61. return ESP_OK;
  62. }
  63. esp_err_t system_efuse_read_mac(uint8_t mac[6]) __attribute__((alias("esp_efuse_read_mac")));
  64. void IRAM_ATTR esp_restart(void)
  65. {
  66. esp_wifi_stop();
  67. // Disable scheduler on this core.
  68. vTaskSuspendAll();
  69. const uint32_t core_id = xPortGetCoreID();
  70. const uint32_t other_core_id = core_id == 0 ? 1 : 0;
  71. esp_cpu_stall(other_core_id);
  72. // We need to disable TG0/TG1 watchdogs
  73. // First enable RTC watchdog to be on the safe side
  74. REG_WRITE(RTC_CNTL_WDTWPROTECT_REG, RTC_CNTL_WDT_WKEY_VALUE);
  75. REG_WRITE(RTC_CNTL_WDTCONFIG0_REG,
  76. RTC_CNTL_WDT_FLASHBOOT_MOD_EN_M |
  77. (1 << RTC_CNTL_WDT_SYS_RESET_LENGTH_S) |
  78. (1 << RTC_CNTL_WDT_CPU_RESET_LENGTH_S) );
  79. REG_WRITE(RTC_CNTL_WDTCONFIG1_REG, 128000);
  80. // Disable TG0/TG1 watchdogs
  81. TIMERG0.wdt_wprotect=TIMG_WDT_WKEY_VALUE;
  82. TIMERG0.wdt_config0.en = 0;
  83. TIMERG0.wdt_wprotect=0;
  84. TIMERG1.wdt_wprotect=TIMG_WDT_WKEY_VALUE;
  85. TIMERG1.wdt_config0.en = 0;
  86. TIMERG1.wdt_wprotect=0;
  87. // Disable all interrupts
  88. xt_ints_off(0xFFFFFFFF);
  89. // Disable cache
  90. Cache_Read_Disable(0);
  91. Cache_Read_Disable(1);
  92. // Flush any data left in UART FIFO
  93. uart_tx_flush(0);
  94. uart_tx_flush(1);
  95. uart_tx_flush(2);
  96. // Reset wifi/bluetooth (bb/mac)
  97. SET_PERI_REG_MASK(DPORT_WIFI_RST_EN_REG, 0x1f);
  98. REG_WRITE(DPORT_WIFI_RST_EN_REG, 0);
  99. // Reset timer/spi/uart
  100. SET_PERI_REG_MASK(DPORT_PERIP_RST_EN_REG,
  101. DPORT_TIMERS_RST | DPORT_SPI_RST_1 | DPORT_UART_RST);
  102. REG_WRITE(DPORT_PERIP_RST_EN_REG, 0);
  103. // Reset CPUs
  104. if (core_id == 0) {
  105. // Running on PRO CPU: APP CPU is stalled. Can reset both CPUs.
  106. SET_PERI_REG_MASK(RTC_CNTL_OPTIONS0_REG,
  107. RTC_CNTL_SW_PROCPU_RST_M | RTC_CNTL_SW_APPCPU_RST_M);
  108. } else {
  109. // Running on APP CPU: need to reset PRO CPU and unstall it,
  110. // then stall APP CPU
  111. SET_PERI_REG_MASK(RTC_CNTL_OPTIONS0_REG, RTC_CNTL_SW_PROCPU_RST_M);
  112. esp_cpu_unstall(0);
  113. esp_cpu_stall(1);
  114. }
  115. while(true) {
  116. ;
  117. }
  118. }
  119. void system_restart(void) __attribute__((alias("esp_restart")));
  120. void system_restore(void)
  121. {
  122. esp_wifi_restore();
  123. }
  124. uint32_t esp_get_free_heap_size(void)
  125. {
  126. return xPortGetFreeHeapSize();
  127. }
  128. uint32_t system_get_free_heap_size(void) __attribute__((alias("esp_get_free_heap_size")));
  129. const char* system_get_sdk_version(void)
  130. {
  131. return "master";
  132. }