system_api.c 8.8 KB

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