system_api.c 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269
  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 <string.h>
  15. #include "esp_system.h"
  16. #include "esp_attr.h"
  17. #include "esp_wifi.h"
  18. #include "esp_wifi_internal.h"
  19. #include "esp_log.h"
  20. #include "sdkconfig.h"
  21. #include "rom/efuse.h"
  22. #include "rom/cache.h"
  23. #include "rom/uart.h"
  24. #include "soc/dport_reg.h"
  25. #include "soc/efuse_reg.h"
  26. #include "soc/rtc_cntl_reg.h"
  27. #include "soc/timer_group_reg.h"
  28. #include "soc/timer_group_struct.h"
  29. #include "soc/cpu.h"
  30. #include "soc/rtc.h"
  31. #include "freertos/FreeRTOS.h"
  32. #include "freertos/task.h"
  33. #include "freertos/xtensa_api.h"
  34. static const char* TAG = "system_api";
  35. void system_init()
  36. {
  37. }
  38. esp_err_t esp_efuse_read_mac(uint8_t* mac)
  39. {
  40. uint8_t efuse_crc;
  41. uint8_t calc_crc;
  42. uint32_t mac_low = REG_READ(EFUSE_BLK0_RDATA1_REG);
  43. uint32_t mac_high = REG_READ(EFUSE_BLK0_RDATA2_REG);
  44. mac[0] = mac_high >> 8;
  45. mac[1] = mac_high;
  46. mac[2] = mac_low >> 24;
  47. mac[3] = mac_low >> 16;
  48. mac[4] = mac_low >> 8;
  49. mac[5] = mac_low;
  50. efuse_crc = mac_high >> 16;
  51. calc_crc = esp_crc8(mac, 6);
  52. if (efuse_crc != calc_crc) {
  53. // Small range of MAC addresses are accepted even if CRC is invalid.
  54. // These addresses are reserved for Espressif internal use.
  55. if ((mac_high & 0xFFFF) == 0x18fe) {
  56. if ((mac_low >= 0x346a85c7) && (mac_low <= 0x346a85f8)) {
  57. return ESP_OK;
  58. }
  59. } else {
  60. ESP_LOGE(TAG, "MAC address CRC error, efuse_crc = 0x%02x; calc_crc = 0x%02x", efuse_crc, calc_crc);
  61. abort();
  62. }
  63. }
  64. return ESP_OK;
  65. }
  66. esp_err_t system_efuse_read_mac(uint8_t mac[6]) __attribute__((alias("esp_efuse_read_mac")));
  67. esp_err_t esp_derive_mac(uint8_t* dst_mac, const uint8_t* src_mac)
  68. {
  69. uint8_t idx;
  70. if (dst_mac == NULL || src_mac == NULL) {
  71. ESP_LOGE(TAG, "mac address param is NULL");
  72. return ESP_ERR_INVALID_ARG;
  73. }
  74. memcpy(dst_mac, src_mac, 6);
  75. for (idx = 0; idx < 64; idx++) {
  76. dst_mac[0] = src_mac[0] | 0x02;
  77. dst_mac[0] ^= idx << 2;
  78. if (memcmp(dst_mac, src_mac, 6)) {
  79. break;
  80. }
  81. }
  82. return ESP_OK;
  83. }
  84. esp_err_t esp_read_mac(uint8_t* mac, esp_mac_type_t type)
  85. {
  86. uint8_t efuse_mac[6];
  87. if (mac == NULL) {
  88. ESP_LOGE(TAG, "mac address param is NULL");
  89. return ESP_ERR_INVALID_ARG;
  90. }
  91. if (type < ESP_MAC_WIFI_STA || type > ESP_MAC_ETH) {
  92. ESP_LOGE(TAG, "mac type is incorrect");
  93. return ESP_ERR_INVALID_ARG;
  94. }
  95. _Static_assert(NUM_MAC_ADDRESS_FROM_EFUSE == FOUR_MAC_ADDRESS_FROM_EFUSE \
  96. || NUM_MAC_ADDRESS_FROM_EFUSE == TWO_MAC_ADDRESS_FROM_EFUSE, \
  97. "incorrect NUM_MAC_ADDRESS_FROM_EFUSE value");
  98. esp_efuse_read_mac(efuse_mac);
  99. switch (type) {
  100. case ESP_MAC_WIFI_STA:
  101. memcpy(mac, efuse_mac, 6);
  102. break;
  103. case ESP_MAC_WIFI_SOFTAP:
  104. if (NUM_MAC_ADDRESS_FROM_EFUSE == FOUR_MAC_ADDRESS_FROM_EFUSE) {
  105. memcpy(mac, efuse_mac, 6);
  106. mac[5] += 1;
  107. }
  108. else if (NUM_MAC_ADDRESS_FROM_EFUSE == TWO_MAC_ADDRESS_FROM_EFUSE) {
  109. esp_derive_mac(mac, efuse_mac);
  110. }
  111. break;
  112. case ESP_MAC_BT:
  113. memcpy(mac, efuse_mac, 6);
  114. if (NUM_MAC_ADDRESS_FROM_EFUSE == FOUR_MAC_ADDRESS_FROM_EFUSE) {
  115. mac[5] += 2;
  116. }
  117. else if (NUM_MAC_ADDRESS_FROM_EFUSE == TWO_MAC_ADDRESS_FROM_EFUSE) {
  118. mac[5] += 1;
  119. }
  120. break;
  121. case ESP_MAC_ETH:
  122. if (NUM_MAC_ADDRESS_FROM_EFUSE == FOUR_MAC_ADDRESS_FROM_EFUSE) {
  123. memcpy(mac, efuse_mac, 6);
  124. mac[5] += 3;
  125. }
  126. else if (NUM_MAC_ADDRESS_FROM_EFUSE == TWO_MAC_ADDRESS_FROM_EFUSE) {
  127. efuse_mac[5] += 1;
  128. esp_derive_mac(mac, efuse_mac);
  129. }
  130. break;
  131. default:
  132. ESP_LOGW(TAG, "incorrect mac type");
  133. break;
  134. }
  135. return ESP_OK;
  136. }
  137. void esp_restart_noos() __attribute__ ((noreturn));
  138. void IRAM_ATTR esp_restart(void)
  139. {
  140. #ifdef CONFIG_WIFI_ENABLED
  141. esp_wifi_stop();
  142. #endif
  143. // Disable scheduler on this core.
  144. vTaskSuspendAll();
  145. esp_restart_noos();
  146. }
  147. /* "inner" restart function for after RTOS, interrupts & anything else on this
  148. * core are already stopped. Stalls other core, resets hardware,
  149. * triggers restart.
  150. */
  151. void IRAM_ATTR esp_restart_noos()
  152. {
  153. const uint32_t core_id = xPortGetCoreID();
  154. const uint32_t other_core_id = core_id == 0 ? 1 : 0;
  155. esp_cpu_stall(other_core_id);
  156. // We need to disable TG0/TG1 watchdogs
  157. // First enable RTC watchdog to be on the safe side
  158. REG_WRITE(RTC_CNTL_WDTWPROTECT_REG, RTC_CNTL_WDT_WKEY_VALUE);
  159. REG_WRITE(RTC_CNTL_WDTCONFIG0_REG,
  160. RTC_CNTL_WDT_FLASHBOOT_MOD_EN_M |
  161. (1 << RTC_CNTL_WDT_SYS_RESET_LENGTH_S) |
  162. (1 << RTC_CNTL_WDT_CPU_RESET_LENGTH_S) );
  163. REG_WRITE(RTC_CNTL_WDTCONFIG1_REG, 128000);
  164. // Disable TG0/TG1 watchdogs
  165. TIMERG0.wdt_wprotect=TIMG_WDT_WKEY_VALUE;
  166. TIMERG0.wdt_config0.en = 0;
  167. TIMERG0.wdt_wprotect=0;
  168. TIMERG1.wdt_wprotect=TIMG_WDT_WKEY_VALUE;
  169. TIMERG1.wdt_config0.en = 0;
  170. TIMERG1.wdt_wprotect=0;
  171. // Disable all interrupts
  172. xt_ints_off(0xFFFFFFFF);
  173. // Disable cache
  174. Cache_Read_Disable(0);
  175. Cache_Read_Disable(1);
  176. // Flush any data left in UART FIFOs
  177. uart_tx_wait_idle(0);
  178. uart_tx_wait_idle(1);
  179. uart_tx_wait_idle(2);
  180. // Reset wifi/bluetooth/ethernet/sdio (bb/mac)
  181. SET_PERI_REG_MASK(DPORT_CORE_RST_EN_REG,
  182. DPORT_BB_RST | DPORT_FE_RST | DPORT_MAC_RST |
  183. DPORT_BT_RST | DPORT_BTMAC_RST | DPORT_SDIO_RST |
  184. DPORT_SDIO_HOST_RST | DPORT_EMAC_RST | DPORT_MACPWR_RST |
  185. DPORT_RW_BTMAC_RST | DPORT_RW_BTLP_RST);
  186. REG_WRITE(DPORT_CORE_RST_EN_REG, 0);
  187. // Reset timer/spi/uart
  188. SET_PERI_REG_MASK(DPORT_PERIP_RST_EN_REG,
  189. DPORT_TIMERS_RST | DPORT_SPI_RST_1 | DPORT_UART_RST);
  190. REG_WRITE(DPORT_PERIP_RST_EN_REG, 0);
  191. // Set CPU back to XTAL source, no PLL, same as hard reset
  192. rtc_clk_cpu_freq_set(RTC_CPU_FREQ_XTAL);
  193. // Reset CPUs
  194. if (core_id == 0) {
  195. // Running on PRO CPU: APP CPU is stalled. Can reset both CPUs.
  196. SET_PERI_REG_MASK(RTC_CNTL_OPTIONS0_REG,
  197. RTC_CNTL_SW_PROCPU_RST_M | RTC_CNTL_SW_APPCPU_RST_M);
  198. } else {
  199. // Running on APP CPU: need to reset PRO CPU and unstall it,
  200. // then stall APP CPU
  201. SET_PERI_REG_MASK(RTC_CNTL_OPTIONS0_REG, RTC_CNTL_SW_PROCPU_RST_M);
  202. esp_cpu_unstall(0);
  203. esp_cpu_stall(1);
  204. }
  205. while(true) {
  206. ;
  207. }
  208. }
  209. void system_restart(void) __attribute__((alias("esp_restart")));
  210. void system_restore(void)
  211. {
  212. esp_wifi_restore();
  213. }
  214. uint32_t esp_get_free_heap_size(void)
  215. {
  216. return xPortGetFreeHeapSize();
  217. }
  218. uint32_t system_get_free_heap_size(void) __attribute__((alias("esp_get_free_heap_size")));
  219. const char* system_get_sdk_version(void)
  220. {
  221. return "master";
  222. }
  223. const char* esp_get_idf_version(void)
  224. {
  225. return IDF_VER;
  226. }