startup_internal.h 3.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  1. /*
  2. * SPDX-FileCopyrightText: 2015-2022 Espressif Systems (Shanghai) CO LTD
  3. *
  4. * SPDX-License-Identifier: Apache-2.0
  5. */
  6. #pragma once
  7. #include "esp_attr.h"
  8. #include "esp_err.h"
  9. #include "esp_bit_defs.h"
  10. #include "esp_cpu.h"
  11. #include "soc/soc_caps.h"
  12. #include "sdkconfig.h"
  13. #ifdef __cplusplus
  14. extern "C" {
  15. #endif
  16. // Port layer defines the entry point. It then transfer control to a `sys_startup_fn_t`, stored in this
  17. // array, one per core.
  18. typedef void (*sys_startup_fn_t)(void);
  19. /* This array of per-CPU system layer startup functions is initialized in the non-port part of esp_system */
  20. #if !CONFIG_ESP_SYSTEM_SINGLE_CORE_MODE
  21. extern sys_startup_fn_t const g_startup_fn[SOC_CPU_CORES_NUM];
  22. #else
  23. extern sys_startup_fn_t const g_startup_fn[1];
  24. #endif
  25. // Utility to execute `sys_startup_fn_t` for the current core.
  26. #define SYS_STARTUP_FN() ((*g_startup_fn[(esp_cpu_get_core_id())])())
  27. #if !CONFIG_ESP_SYSTEM_SINGLE_CORE_MODE
  28. void startup_resume_other_cores(void);
  29. #endif
  30. /**
  31. * Internal structure describing ESP_SYSTEM_INIT_FN startup functions
  32. */
  33. typedef struct {
  34. esp_err_t (*fn)(void); /*!< Pointer to the startup function */
  35. uint32_t cores; /*!< Bit map of cores where the function has to be called */
  36. } esp_system_init_fn_t;
  37. /**
  38. * @brief Define a system initialization function which will be executed on the specified cores
  39. *
  40. * @param f function name (identifier)
  41. * @param c bit mask of cores to execute the function on (ex. if BIT0 is set, the function
  42. * will be executed on CPU 0, if BIT1 is set - on CPU 1, and so on)
  43. * @param priority integer, priority of the initialization function. Higher values mean that
  44. * the function will be executed later in the process.
  45. * @param (varargs) optional, additional attributes for the function declaration (such as IRAM_ATTR)
  46. *
  47. * The function defined using this macro must return ESP_OK on success. Any other value will be
  48. * logged and the startup process will abort.
  49. *
  50. * Initialization functions should be placed in a compilation unit where at least one other
  51. * symbol is referenced in another compilation unit. This means that the reference should not itself
  52. * get optimized out by the compiler or discarded by the linker if the related feature is used.
  53. * It is, on the other hand, a good practice to make sure the initialization function does get
  54. * discarded if the related feature is not used.
  55. */
  56. #define ESP_SYSTEM_INIT_FN(f, c, priority, ...) \
  57. static esp_err_t __VA_ARGS__ __esp_system_init_fn_##f(void); \
  58. static __attribute__((used)) _SECTION_ATTR_IMPL(".esp_system_init_fn", priority) \
  59. esp_system_init_fn_t esp_system_init_fn_##f = { .fn = ( __esp_system_init_fn_##f), .cores = (c) }; \
  60. static esp_err_t __esp_system_init_fn_##f(void)
  61. #ifdef CONFIG_ESP_SYSTEM_SINGLE_CORE_MODE
  62. #define ESP_SYSTEM_INIT_ALL_CORES BIT(0)
  63. #else
  64. #define ESP_SYSTEM_INIT_ALL_CORES (BIT(SOC_CPU_CORES_NUM) - 1)
  65. #endif
  66. extern uint64_t g_startup_time; // Startup time that serves as the point of origin for system time. Should be set by the entry
  67. // function in the port layer. May be 0 as well if this is not backed by a persistent counter, in which case
  68. // startup time = system time = 0 at the point the entry function sets this variable.
  69. #ifdef __cplusplus
  70. }
  71. #endif