esp_system.c 2.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  1. /*
  2. * SPDX-FileCopyrightText: 2015-2022 Espressif Systems (Shanghai) CO LTD
  3. *
  4. * SPDX-License-Identifier: Apache-2.0
  5. */
  6. #include "esp_system.h"
  7. #include "esp_private/system_internal.h"
  8. #include "freertos/FreeRTOS.h"
  9. #include "freertos/task.h"
  10. #if CONFIG_ESP_SYSTEM_MEMPROT_FEATURE
  11. #if CONFIG_IDF_TARGET_ESP32S2
  12. #include "esp32s2/memprot.h"
  13. #elif CONFIG_IDF_TARGET_ESP32C2
  14. #include "esp32c2/memprot.h"
  15. #else
  16. #include "esp_memprot.h"
  17. #endif
  18. #endif
  19. #define SHUTDOWN_HANDLERS_NO 5
  20. static shutdown_handler_t shutdown_handlers[SHUTDOWN_HANDLERS_NO];
  21. esp_err_t esp_register_shutdown_handler(shutdown_handler_t handler)
  22. {
  23. for (int i = 0; i < SHUTDOWN_HANDLERS_NO; i++) {
  24. if (shutdown_handlers[i] == handler) {
  25. return ESP_ERR_INVALID_STATE;
  26. } else if (shutdown_handlers[i] == NULL) {
  27. shutdown_handlers[i] = handler;
  28. return ESP_OK;
  29. }
  30. }
  31. return ESP_ERR_NO_MEM;
  32. }
  33. esp_err_t esp_unregister_shutdown_handler(shutdown_handler_t handler)
  34. {
  35. for (int i = 0; i < SHUTDOWN_HANDLERS_NO; i++) {
  36. if (shutdown_handlers[i] == handler) {
  37. shutdown_handlers[i] = NULL;
  38. return ESP_OK;
  39. }
  40. }
  41. return ESP_ERR_INVALID_STATE;
  42. }
  43. void IRAM_ATTR esp_restart(void)
  44. {
  45. for (int i = SHUTDOWN_HANDLERS_NO - 1; i >= 0; i--) {
  46. if (shutdown_handlers[i]) {
  47. shutdown_handlers[i]();
  48. }
  49. }
  50. #ifdef CONFIG_FREERTOS_SMP
  51. //Note: Scheduler suspension behavior changed in FreeRTOS SMP
  52. vTaskPreemptionDisable(NULL);
  53. #else
  54. // Disable scheduler on this core.
  55. vTaskSuspendAll();
  56. #endif // CONFIG_FREERTOS_SMP
  57. bool digital_reset_needed = false;
  58. #if CONFIG_ESP_SYSTEM_MEMPROT_FEATURE
  59. #if CONFIG_IDF_TARGET_ESP32S2
  60. if (esp_memprot_is_intr_ena_any() || esp_memprot_is_locked_any()) {
  61. digital_reset_needed = true;
  62. }
  63. #else
  64. bool is_on = false;
  65. if (esp_mprot_is_intr_ena_any(&is_on) != ESP_OK || is_on) {
  66. digital_reset_needed = true;
  67. } else if (esp_mprot_is_conf_locked_any(&is_on) != ESP_OK || is_on) {
  68. digital_reset_needed = true;
  69. }
  70. #endif
  71. #endif
  72. if (digital_reset_needed) {
  73. esp_restart_noos_dig();
  74. }
  75. esp_restart_noos();
  76. }