system_api.c 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  1. #include "esp_system.h"
  2. #include "esp_private/system_internal.h"
  3. #include "esp_heap_caps.h"
  4. #include "freertos/FreeRTOS.h"
  5. #include "freertos/task.h"
  6. #include "esp_system.h"
  7. #include "panic_internal.h"
  8. #define SHUTDOWN_HANDLERS_NO 2
  9. static shutdown_handler_t shutdown_handlers[SHUTDOWN_HANDLERS_NO];
  10. esp_err_t esp_register_shutdown_handler(shutdown_handler_t handler)
  11. {
  12. for (int i = 0; i < SHUTDOWN_HANDLERS_NO; i++) {
  13. if (shutdown_handlers[i] == handler) {
  14. return ESP_ERR_INVALID_STATE;
  15. } else if (shutdown_handlers[i] == NULL) {
  16. shutdown_handlers[i] = handler;
  17. return ESP_OK;
  18. }
  19. }
  20. return ESP_ERR_NO_MEM;
  21. }
  22. esp_err_t esp_unregister_shutdown_handler(shutdown_handler_t handler)
  23. {
  24. for (int i = 0; i < SHUTDOWN_HANDLERS_NO; i++) {
  25. if (shutdown_handlers[i] == handler) {
  26. shutdown_handlers[i] = NULL;
  27. return ESP_OK;
  28. }
  29. }
  30. return ESP_ERR_INVALID_STATE;
  31. }
  32. void IRAM_ATTR esp_restart(void)
  33. {
  34. for (int i = SHUTDOWN_HANDLERS_NO - 1; i >= 0; i--) {
  35. if (shutdown_handlers[i]) {
  36. shutdown_handlers[i]();
  37. }
  38. }
  39. // Disable scheduler on this core.
  40. vTaskSuspendAll();
  41. esp_restart_noos();
  42. }
  43. uint32_t esp_get_free_heap_size( void )
  44. {
  45. return heap_caps_get_free_size( MALLOC_CAP_DEFAULT );
  46. }
  47. uint32_t esp_get_minimum_free_heap_size( void )
  48. {
  49. return heap_caps_get_minimum_free_size( MALLOC_CAP_DEFAULT );
  50. }
  51. const char* esp_get_idf_version(void)
  52. {
  53. return IDF_VER;
  54. }
  55. void __attribute__((noreturn)) esp_system_abort(const char* details)
  56. {
  57. panic_abort(details);
  58. }