components.c 5.9 KB
Newer Older
1
/*
2
 * Copyright (c) 2006-2018, RT-Thread Development Team
3
 *
4
 * SPDX-License-Identifier: Apache-2.0
5 6 7 8 9 10 11 12 13
 *
 * Change Logs:
 * Date           Author       Notes
 * 2012-09-20     Bernard      Change the name to components.c
 *                             And all components related header files.
 * 2012-12-23     Bernard      fix the pthread initialization issue.
 * 2013-06-23     Bernard      Add the init_call for components initialization.
 * 2013-07-05     Bernard      Remove initialization feature for MS VC++ compiler
 * 2015-02-06     Bernard      Remove the MS VC++ support and move to the kernel
14
 * 2015-05-04     Bernard      Rename it to components.c because compiling issue
15
 *                             in some IDEs.
16
 * 2015-07-29     Arda.Fu      Add support to use RT_USING_USER_MAIN with IAR
S
shaojinchun 已提交
17
 * 2018-11-22     Jesven       Add secondary cpu boot up
18 19
 */

20
#include <rthw.h>
21 22
#include <rtthread.h>

23 24
#ifdef RT_USING_USER_MAIN
#ifndef RT_MAIN_THREAD_STACK_SIZE
B
Bernard Xiong 已提交
25
#define RT_MAIN_THREAD_STACK_SIZE     2048
26
#endif
27 28 29
#ifndef RT_MAIN_THREAD_PRIORITY
#define RT_MAIN_THREAD_PRIORITY       (RT_THREAD_PRIORITY_MAX / 3)
#endif
30 31
#endif

32 33
#ifdef RT_USING_COMPONENTS_INIT
/*
34
 * Components Initialization will initialize some driver and components as following
35 36 37 38 39 40 41 42 43
 * order:
 * rti_start         --> 0
 * BOARD_EXPORT      --> 1
 * rti_board_end     --> 1.end
 *
 * DEVICE_EXPORT     --> 2
 * COMPONENT_EXPORT  --> 3
 * FS_EXPORT         --> 4
 * ENV_EXPORT        --> 5
44 45
 * APP_EXPORT        --> 6
 *
46 47
 * rti_end           --> 6.end
 *
B
Bernard Xiong 已提交
48
 * These automatically initialization, the driver or component initial function must
49 50 51 52 53
 * be defined with:
 * INIT_BOARD_EXPORT(fn);
 * INIT_DEVICE_EXPORT(fn);
 * ...
 * INIT_APP_EXPORT(fn);
54
 * etc.
55 56 57 58 59 60 61
 */
static int rti_start(void)
{
    return 0;
}
INIT_EXPORT(rti_start, "0");

62 63 64 65 66 67
static int rti_board_start(void)
{
    return 0;
}
INIT_EXPORT(rti_board_start, "0.end");

68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87
static int rti_board_end(void)
{
    return 0;
}
INIT_EXPORT(rti_board_end, "1.end");

static int rti_end(void)
{
    return 0;
}
INIT_EXPORT(rti_end, "6.end");

/**
 * RT-Thread Components Initialization for board
 */
void rt_components_board_init(void)
{
#if RT_DEBUG_INIT
    int result;
    const struct rt_init_desc *desc;
88
    for (desc = &__rt_init_desc_rti_board_start; desc < &__rt_init_desc_rti_board_end; desc ++)
89 90 91 92 93 94 95 96
    {
        rt_kprintf("initialize %s", desc->fn_name);
        result = desc->fn();
        rt_kprintf(":%d done\n", result);
    }
#else
    const init_fn_t *fn_ptr;

97
    for (fn_ptr = &__rt_init_rti_board_start; fn_ptr < &__rt_init_rti_board_end; fn_ptr++)
98 99 100 101 102 103 104 105 106 107 108 109 110 111 112
    {
        (*fn_ptr)();
    }
#endif
}

/**
 * RT-Thread Components Initialization
 */
void rt_components_init(void)
{
#if RT_DEBUG_INIT
    int result;
    const struct rt_init_desc *desc;

B
Bernard Xiong 已提交
113
    rt_kprintf("do components initialization.\n");
114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130
    for (desc = &__rt_init_desc_rti_board_end; desc < &__rt_init_desc_rti_end; desc ++)
    {
        rt_kprintf("initialize %s", desc->fn_name);
        result = desc->fn();
        rt_kprintf(":%d done\n", result);
    }
#else
    const init_fn_t *fn_ptr;

    for (fn_ptr = &__rt_init_rti_board_end; fn_ptr < &__rt_init_rti_end; fn_ptr ++)
    {
        (*fn_ptr)();
    }
#endif
}

#ifdef RT_USING_USER_MAIN
131

132 133
void rt_application_init(void);
void rt_hw_board_init(void);
134 135
int rtthread_startup(void);

B
Bernard Xiong 已提交
136
#if defined(__CC_ARM) || defined(__CLANG_ARM)
137
extern int $Super$$main(void);
138 139 140 141
/* re-define main function */
int $Sub$$main(void)
{
    rtthread_startup();
142 143 144 145 146
    return 0;
}
#elif defined(__ICCARM__)
extern int main(void);
/* __low_level_init will auto called by IAR cstartup */
147
extern void __iar_data_init3(void);
148 149
int __low_level_init(void)
{
150 151
    // call IAR table copy function.
    __iar_data_init3();
152 153 154 155 156 157 158 159
    rtthread_startup();
    return 0;
}
#elif defined(__GNUC__)
/* Add -eentry to arm-none-eabi-gcc argument */
int entry(void)
{
    rtthread_startup();
160 161 162 163
    return 0;
}
#endif

164
#ifndef RT_USING_HEAP
165
/* if there is not enable heap, we should use static thread and stack. */
166
ALIGN(8)
167
static rt_uint8_t main_stack[RT_MAIN_THREAD_STACK_SIZE];
168 169 170 171 172 173 174
struct rt_thread main_thread;
#endif

/* the system main thread */
void main_thread_entry(void *parameter)
{
    extern int main(void);
175
    extern int $Super$$main(void);
176 177 178 179

    /* RT-Thread components initialization */
    rt_components_init();

S
shaojinchun 已提交
180 181 182
#ifdef RT_USING_SMP
    rt_hw_secondary_cpu_up();
#endif
183
    /* invoke system main function */
B
Bernard Xiong 已提交
184
#if defined(__CC_ARM) || defined(__CLANG_ARM)
185
    $Super$$main(); /* for ARMCC. */
186
#elif defined(__ICCARM__) || defined(__GNUC__)
187
    main();
188
#endif
189 190 191 192 193 194 195 196
}

void rt_application_init(void)
{
    rt_thread_t tid;

#ifdef RT_USING_HEAP
    tid = rt_thread_create("main", main_thread_entry, RT_NULL,
197
                           RT_MAIN_THREAD_STACK_SIZE, RT_MAIN_THREAD_PRIORITY, 20);
198 199 200 201 202 203
    RT_ASSERT(tid != RT_NULL);
#else
    rt_err_t result;

    tid = &main_thread;
    result = rt_thread_init(tid, "main", main_thread_entry, RT_NULL,
204
                            main_stack, sizeof(main_stack), RT_MAIN_THREAD_PRIORITY, 20);
B
Bluebear233 已提交
205
    RT_ASSERT(result == RT_EOK);
206

207 208
    /* if not define RT_USING_HEAP, using to eliminate the warning */
    (void)result;
209 210 211 212 213 214 215
#endif

    rt_thread_startup(tid);
}

int rtthread_startup(void)
{
216
    rt_hw_interrupt_disable();
217

B
Bernard Xiong 已提交
218
    /* board level initialization
219 220 221 222 223 224 225 226 227 228 229 230 231
     * NOTE: please initialize heap inside board initialization.
     */
    rt_hw_board_init();

    /* show RT-Thread version */
    rt_show_version();

    /* timer system initialization */
    rt_system_timer_init();

    /* scheduler system initialization */
    rt_system_scheduler_init();

B
bernard 已提交
232 233 234 235 236
#ifdef RT_USING_SIGNALS
    /* signal system initialization */
    rt_system_signal_init();
#endif

237 238 239 240 241 242 243 244 245
    /* create init_thread */
    rt_application_init();

    /* timer thread initialization */
    rt_system_timer_thread_init();

    /* idle thread initialization */
    rt_thread_idle_init();

S
shaojinchun 已提交
246 247 248 249
#ifdef RT_USING_SMP
    rt_hw_spin_lock(&_cpus_lock);
#endif /*RT_USING_SMP*/

250 251 252 253
    /* start scheduler */
    rt_system_scheduler_start();

    /* never reach here */
254
    return 0;
255 256 257
}
#endif
#endif