startup_internal.h 3.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  1. // Copyright 2015-2018 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. #pragma once
  15. #include "esp_attr.h"
  16. #include "soc/soc_caps.h"
  17. #include "hal/cpu_hal.h"
  18. #include "sdkconfig.h"
  19. #ifdef __cplusplus
  20. extern "C" {
  21. #endif
  22. extern bool g_spiram_ok; // [refactor-todo] better way to communicate this from port layer to common startup code
  23. // Port layer defines the entry point. It then transfer control to a `sys_startup_fn_t`, stored in this
  24. // array, one per core.
  25. typedef void (*sys_startup_fn_t)(void);
  26. /* This array of per-CPU system layer startup functions is initialized in the non-port part of esp_system */
  27. #if !CONFIG_ESP_SYSTEM_SINGLE_CORE_MODE
  28. extern sys_startup_fn_t const g_startup_fn[SOC_CPU_CORES_NUM];
  29. #else
  30. extern sys_startup_fn_t const g_startup_fn[1];
  31. #endif
  32. // Utility to execute `sys_startup_fn_t` for the current core.
  33. #define SYS_STARTUP_FN() ((*g_startup_fn[(cpu_hal_get_core_id())])())
  34. #if !CONFIG_ESP_SYSTEM_SINGLE_CORE_MODE
  35. void startup_resume_other_cores(void);
  36. #endif
  37. typedef struct {
  38. void (*fn)(void);
  39. uint32_t cores;
  40. } esp_system_init_fn_t;
  41. /*
  42. * Declare an component initialization function that will execute on the specified cores (ex. if BIT0 == 1, will execute
  43. * on CORE0, CORE1 if BIT1 and so on).
  44. *
  45. * @note Initialization functions should be placed in a compilation unit where at least one other
  46. * symbol is referenced 'meaningfully' in another compilation unit, otherwise this gets discarded during linking. (By
  47. * 'meaningfully' we mean the reference should not itself get optimized out by the compiler/discarded by the linker).
  48. */
  49. #define ESP_SYSTEM_INIT_FN(f, c, ...) \
  50. static void __attribute__((used)) __VA_ARGS__ __esp_system_init_fn_##f(void); \
  51. static __attribute__((used)) esp_system_init_fn_t _SECTION_ATTR_IMPL(".esp_system_init_fn", f) \
  52. esp_system_init_fn_##f = { .fn = ( __esp_system_init_fn_##f), .cores = (c) }; \
  53. static __attribute__((used)) __VA_ARGS__ void __esp_system_init_fn_##f(void) // [refactor-todo] this can be made public API if we allow components to declare init functions,
  54. // instead of calling them explicitly
  55. extern uint64_t g_startup_time; // Startup time that serves as the point of origin for system time. Should be set by the entry
  56. // function in the port layer. May be 0 as well if this is not backed by a persistent counter, in which case
  57. // startup time = system time = 0 at the point the entry function sets this variable.
  58. #ifdef __cplusplus
  59. }
  60. #endif