system_api.c 1.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  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. #define SHUTDOWN_HANDLERS_NO 3
  7. static shutdown_handler_t shutdown_handlers[SHUTDOWN_HANDLERS_NO];
  8. esp_err_t esp_register_shutdown_handler(shutdown_handler_t handler)
  9. {
  10. for (int i = 0; i < SHUTDOWN_HANDLERS_NO; i++) {
  11. if (shutdown_handlers[i] == handler) {
  12. return ESP_ERR_INVALID_STATE;
  13. } else if (shutdown_handlers[i] == NULL) {
  14. shutdown_handlers[i] = handler;
  15. return ESP_OK;
  16. }
  17. }
  18. return ESP_ERR_NO_MEM;
  19. }
  20. esp_err_t esp_unregister_shutdown_handler(shutdown_handler_t handler)
  21. {
  22. for (int i = 0; i < SHUTDOWN_HANDLERS_NO; i++) {
  23. if (shutdown_handlers[i] == handler) {
  24. shutdown_handlers[i] = NULL;
  25. return ESP_OK;
  26. }
  27. }
  28. return ESP_ERR_INVALID_STATE;
  29. }
  30. void IRAM_ATTR esp_restart(void)
  31. {
  32. for (int i = SHUTDOWN_HANDLERS_NO - 1; i >= 0; i--) {
  33. if (shutdown_handlers[i]) {
  34. shutdown_handlers[i]();
  35. }
  36. }
  37. // Disable scheduler on this core.
  38. vTaskSuspendAll();
  39. esp_restart_noos();
  40. }
  41. uint32_t esp_get_free_heap_size( void )
  42. {
  43. return heap_caps_get_free_size( MALLOC_CAP_DEFAULT );
  44. }
  45. uint32_t esp_get_free_internal_heap_size( void )
  46. {
  47. return heap_caps_get_free_size( MALLOC_CAP_8BIT | MALLOC_CAP_DMA | MALLOC_CAP_INTERNAL );
  48. }
  49. uint32_t esp_get_minimum_free_heap_size( void )
  50. {
  51. return heap_caps_get_minimum_free_size( MALLOC_CAP_DEFAULT );
  52. }
  53. const char* esp_get_idf_version(void)
  54. {
  55. return IDF_VER;
  56. }