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