STM32 Rtthread启动流程基于GNU交叉编译' G( _2 l, \4 ]7 W: p- E
Rtthread并不是直接从main函数开始启动的,而是从componets.c中通过entry函数启动的:% O* w! B$ |2 h! |- E3 H% D
- //需要在board.c中开启这个宏RT_USING_USER_MAIN
& s6 L3 Y4 R3 S9 f - 4 c9 R2 |! M- Y$ [1 S6 |- N
- //我使用的时GNU编译器,所以入口函数在这,可以在ld命令中指定entry为入口地址,
, Q9 n3 i! \; X) K- a - //也可以在startup_stm32f411xe.s中指定入口地址为entry
! B7 B0 i: b% X K: ]2 X9 S - /* Add -eentry to arm-none-eabi-gcc argument *// p, I. t; C/ ?& I! Q
- int entry(void)7 x; v' U3 k9 ~$ z* h: w4 l) T
- {
, b' ^) n& g0 U2 X: z: k" Y - rtthread_startup();& r' l3 c5 R. S
- return 0;
+ p' r1 y! q6 J$ L8 `% A, p& a - }& @, ?+ o8 p$ R3 A. `/ k
$ \; i/ ~% l6 K' a. ]: P- int rtthread_startup(void)
# `/ I' m* C( u p' J+ O - {! g O$ i$ {3 n( f3 l
- rt_hw_interrupt_disable();; W: q( p6 m4 @, B
- * l5 ]4 H+ F4 @1 p3 B
- /* board level initialization7 h6 q* U7 V* A7 p1 i
- * NOTE: please initialize heap inside board initialization.& R6 \# `9 k! V% [, Q
- */
" {, ^1 |( |* L3 o& h. { - rt_hw_board_init();
5 v, b4 n/ k4 ~+ a
# \2 `7 \& _- @% l0 o% {2 d- /* show RT-Thread version */
5 o* p& K. c' f3 _3 K$ i - rt_show_version();
, L P+ k. {; H. b4 ~: g8 e
& O1 f Y% ?6 z2 e7 b) i- /* timer system initialization */) T# K' |6 j9 z o2 C9 ^
- rt_system_timer_init();
6 s. ?( l+ E1 q5 W9 T) e - , _2 {7 W' Z" p2 G3 g( O* ^6 t
- /* scheduler system initialization */
( d- \* Q. l, B - rt_system_scheduler_init();
5 L! G8 C3 m" d
1 c0 T/ c' Z8 j( c) ?% ]- /* create init_thread */
" u9 `! q+ g s, J7 Q8 k - rt_application_init();
; U L1 k2 O5 g - + ]/ P* D1 k, E9 t) t- i/ [0 m) C
- /* timer thread initialization */
5 v7 a; T, Q, U# a* F' Z - rt_system_timer_thread_init();/ m4 |# S8 A" i2 z
- ! f$ @: }0 I2 R) L K
- /* idle thread initialization */, Z! i7 a2 t. H. d H6 ~. N
- rt_thread_idle_init();
% @" l0 Q/ w5 a; k% m" V3 t
/ T( Q) c, ~# u' T* m- /* start scheduler */
8 ?" G' d8 |, ~ C# d - rt_system_scheduler_start();# A& t4 W5 X m
- ' I4 d! k& e3 C3 H" R8 Y% a
- /* never reach here */6 V1 _+ J1 c1 e* G+ l" a* ]
- return 0;2 E- i9 W8 ?* f( i3 N2 O
- } n1 E9 T2 i/ K9 ~
- ( `. r; d' G' J- ^9 b
- //rt_hw_board_init函数在board.c中定义,主要功能是做硬件初始化4 n5 X1 M& G! [5 A4 J/ d
- void rt_hw_board_init()% \' c7 B# p3 U* T6 f4 h6 p+ j8 P" z
- {/ |4 ~2 l* U! K* B! A4 u* Q% @
- USART1_Config(115200);//串口初始化( Y G6 R# j: Z/ n
- KEY_Init();//按键初始化" M$ ]# s/ W* _
- LED_init();//LED初始化
& K$ U0 F, H: e0 m - /* System Clock Update */
" d+ s4 L+ P4 C2 b+ z - //该函数在system_stm32f4xx.c中定义,主要功能是获取SystemCoreClock 的值。+ S% \) A/ N( l$ y4 G- j. L
- SystemCoreClockUpdate();( P7 t% U& a6 q7 ~+ M3 w
-
/ k C) M: L& z7 Y) w2 U" c& W - /* System Tick Configuration */8 r" I8 y' L K9 }
- _SysTick_Config(SystemCoreClock / RT_TICK_PER_SECOND);//RT_TICK_PER_SECOND 1000% w- ^' C% `) ?& b i, M
- s6 M4 w% C: w. x( G; Z. \- /* Call components board initial (use INIT_BOARD_EXPORT()) */+ x% U+ Y: b' q
- #ifdef RT_USING_COMPONENTS_INIT( X$ R+ n* G3 g0 U T# B
- rt_components_board_init();# `0 e Y# z3 H' C" f
- #endif
& i' {6 \0 r* N* a9 {7 K - ' T2 }8 ]/ ^* E6 X' q5 o7 u" Z0 I
- #if defined(RT_USING_CONSOLE) && defined(RT_USING_DEVICE)
6 I0 `4 s% c- B8 b% k# Z4 u - //rt_console_set_device(RT_CONSOLE_DEVICE_NAME);0 u1 D5 N3 g6 u
- #endif
6 @, p. [$ x& Q0 U+ o
1 C6 I& ^1 Y9 _/ I- M- #if defined(RT_USING_USER_MAIN) && defined(RT_USING_HEAP) //初始化堆
) G& h8 y8 b/ p: X9 `: j6 \ - rt_system_heap_init(rt_heap_begin_get(), rt_heap_end_get());! L$ i8 E5 @7 Y1 E* G% x
- #endif
: ^4 |' ?# @% K
2 }% N% O8 x) Z: e- }
2 Q" \% d% ^) o# U
& v- h3 f @8 w, A X/ ^: }( `- static uint32_t _SysTick_Config(rt_uint32_t ticks)
5 }: \( {& W5 N: Q% L/ a* ^) v6 Q, }. r - {; c4 I# _! s9 `/ r
- if ((ticks - 1) > 0xFFFFFF)7 O' Y1 Y" V9 U
- {
& h7 a2 ^* o2 l+ x - return 1;) B7 d& q- i2 J1 g9 v
- }
7 X1 j _5 b, O+ _4 ^8 f -
% A" v1 c6 k _0 z% s - _SYSTICK_LOAD = ticks - 1; //RELOAD VALUE
* n; ` J! f' Y2 N - _SYSTICK_PRI = 0xFF; //PRIORITY OF SYSTICK LOW
( m V4 Y7 F( v% I - _SYSTICK_VAL = 0; //CLEAR CURRENT VALUE OF SYSTICK COUNT( ?" r; X" n5 v2 N, g
- //systick ctrl寄存器bit2 为1时使用AHB作为systic频率,不分频/ W% f. x" a4 J3 `: F. e! e" [6 S) m
- _SYSTICK_CTRL = 0x07; //0x07 BIT2 ==1 使用AHB,0:使用内部时钟源,AHB/8% `( J0 t+ t1 k9 F3 P
- $ \& u4 e* L( d- o0 J0 \
- return 0;8 B% @9 r/ P9 v) F' @ `( \
- }
' O; V2 o3 [7 s: d1 X" P% P/ ~# B: [ - //rt_components_board_init函数会初始化INIT_EXPORT(fn, level) 宏包起来的函数3 `( N2 V5 \9 O) l: K5 y# n
- //可以看看这篇文章:https://blog.csdn.net/qq_42370291/article/details/103639349
+ F% Q" E8 G0 Q" e - void rt_components_board_init(void)/ ~" m7 a( y/ O- Z/ f; }
- {2 H$ `- j3 g6 @+ E
- #if RT_DEBUG_INIT //开了RT_DEBUG_INIT 将会打印出被调用的初始化的函数( j( x j7 @( I3 a/ a! q1 V
- int result;' u) H# ~* }# K/ x
- const struct rt_init_desc *desc;+ Z$ u; Z% f) [. t/ T1 U3 i2 e6 ~
- for (desc = &__rt_init_desc_rti_board_start; desc < &__rt_init_desc_rti_board_end; desc ++)
# K- v- J! n0 p; O w7 U8 c" ` - {
( L" h# K- B& b8 ^ g - rt_kprintf("initialize %s", desc->fn_name);% ^8 H1 M" Z2 Y- x3 R3 Z- V
- result = desc->fn();
6 l) U9 B4 }6 a - rt_kprintf(":%d done\n", result); T; c, X0 N% n
- }
% }" ?9 t# ^ Y/ B O; O4 p) ?8 ^ - #else# Q( p `, ?' _ Q
- volatile const init_fn_t *fn_ptr;% x" ?' n9 a: Z- c
8 E" k/ b! e! |! Y1 j. C- for (fn_ptr = &__rt_init_rti_board_start; fn_ptr < &__rt_init_rti_board_end; fn_ptr++)2 x- g: a! v+ \; N( x
- {
0 P3 o: A1 t- f' N& {3 Q# | - (*fn_ptr)();
% x# `5 |4 ~% R! f - }
. c3 t! f* u& O+ ]& t6 z+ I5 J - #endif
4 M) ?+ c. R$ V8 u. a; i$ [" U - }
: |# ~: z7 N- g% r5 a
复制代码 . F1 u: b8 n; H' A
在startup_stm32f411xe.s中指定入口地址为entry. p1 a, `/ S8 {( H H
- /* Call the application's entry point.*/
- C; B, W& E+ t! F; }) s& p - bl entry //这里之前是main& D: I. Y5 |( Z# k- D8 K' s
- bx lr
复制代码 ————————————————# Q8 A, H$ |6 h( G
版权声明:tony++
2 c5 t) d; R# I4 W5 y8 f( q3 I/ V# M7 K0 }8 e: e/ `" r
|