|
创建线程是RT_Thread最基本的操作之一。 1、在man.c主程序下面创建静态线程: - /* defined the LED1 pin: PC7 */
) e7 c0 X& M4 g/ i. Z5 L& t$ x - /* defined the LED0 pin: PG2 */' b* |( g& X% T. `
- 2 p9 w+ s9 l: \! h* I2 f' O4 v3 A
- #define LED0_PIN GET_PIN(G, 2)
. K# W6 D/ f/ t( V. k7 J) o - 1 u$ t6 E! _! Q9 [0 ?/ h
- #define LED1_PIN GET_PIN(C, 7)
# p+ K: Y& s) d1 B) l& {4 I - #define THREAD_PRIORITY 25
$ Q" k; t4 m3 @ M! h - #define THREAD_STACK_SIZE 512' C6 n& R5 e( ]
- #define THREAD_TIMESLICE 5
$ R" q1 J' L) E# M2 ?& M9 s - 0 e6 {+ _4 A0 U* F9 I4 {- J
- /* 线程led 的对象和运行时用到的栈 *// R3 `3 F- }2 _, o
- static struct rt_thread thread_led;, M; p: A3 O; `- c
- static rt_uint8_t thread_led_stack[512];
7 q" ?1 O. Z$ _ - $ p$ S# n% H$ B* I2 B) [
- /* 线程led 入口 */8 \- E6 b0 ^: U7 \
- void thread_led_entry(void* parameter)
) P- u" f/ o, X: @$ H - {7 `; ^0 Y3 a5 ~& g& M2 ]
- rt_pin_mode(LED1_PIN, PIN_MODE_OUTPUT);: c. s" \, N0 n
- while(1)
8 t% \5 B! y4 e( [% W; U' a - {( u! {" C+ Y: p$ ?# F/ }- r$ |1 |
- rt_pin_write(LED1_PIN,PIN_HIGH);' v3 K9 c" n% w$ x3 B
- rt_thread_mdelay(500);
2 T. P9 N/ [8 b8 r5 y - rt_pin_write(LED1_PIN, PIN_LOW);! u! Z9 p& P! h% @, [2 w
- rt_thread_mdelay(500);# l1 U# S3 U N; @. e: J& N
- }) q( A# V2 W! h* D! J2 ?5 k! {
- }1 l/ i% f# ^- G0 A
- 0 l7 X+ u N) k$ t9 N1 [1 h
- /* 线程LED1 初始化 */
: {! d1 m. O+ Q) b. R t: e - int thread_sample(void)
* i; A. B7 B$ I# j) W% Y5 G) s% Y" i - {) a( j, e8 h' v. C
- rt_thread_t threadled_ptr;
9 D1 _- C: {/ u/ o: h) D4 M! D% P+ b - rt_err_t result;, \; l5 v' L" R+ [
- 0 x( W' Q9 ?! B4 f" Y; l
- /*
3 e4 k! E1 x) m I9 t - * 初始化 线程LED1: K2 ^2 i9 {; E3 ^
- * 线程的入口是 thread_led_entry, 参数是RT_NULLL8 j: [) M# D) Q0 h; E
- * 线程栈是thread_led_stack
: h8 j+ J e" N5 Q& r( f& y - * 优先级是25,时间片是5 OS Tick
4 o9 u* v; Q6 x2 E, y3 Y - / q! |+ b6 W% l7 w* D% w" h
- */( } ?2 ?* M- r; u+ i4 R" c
- result = rt_thread_init(&thread_led,
6 k7 Q! x$ d% b2 j3 Y1 P/ y - "thread led",- B3 O. Q0 s, o; ^
- thread_led_entry, RT_NULL,
4 |* {. r! R$ Y2 I* j, `# {6 @ - &thread_led_stack[0], sizeof(thread_led_stack),$ o" |) d% B$ z9 h, W3 p- S
- THREAD_PRIORITY-1,THREAD_TIMESLICE);) v# Y# Y5 Y+ \6 f
- /* 启动线程 */
: @ W `8 W+ o5 H# ~3 a; |/ o - if(result == RT_EOK) rt_thread_startup(&thread_led);
7 u n# F' S9 w! h$ m8 X - return 0;
% m8 H9 j+ D7 u& g0 m/ ^ - }; c+ U$ K# \' h2 i+ @9 W% |# T: P
! T ?; H7 ^( k0 b+ r- int main(void)
) L @9 G# ?. _2 e7 O6 J+ h. f - {
2 F. h$ @! b" L0 d) p) W% {1 n - int count = 1; O& I1 M( W# |+ g( T. `: L3 y8 c
- /* set LED0 pin mode to output */, o2 [2 y0 a2 m9 q2 V1 ^ j1 t
- rt_pin_mode(LED0_PIN, PIN_MODE_OUTPUT);3 f* e: l1 c/ Y! h, W3 p
- thread_sample();% S3 A- ?0 k+ k% f
- while (count++)3 } W5 @8 `, b* j) i; D1 I/ @/ O
- {
$ W6 H* _& X2 d% l - rt_pin_write(LED0_PIN, PIN_HIGH);
2 m+ n# O) Y+ x: Q& j - rt_thread_mdelay(500);
& J& t5 ?/ K9 W# q, { - rt_pin_write(LED0_PIN, PIN_LOW);( K( m; f* d! `0 y/ H
- rt_thread_mdelay(500);6 G& k5 J. H, t/ Z
- }
8 L+ n$ u0 k0 p* ~ - & V( y% f$ a- P5 F3 Z
- return RT_EOK;
4 i- t7 n4 ?( S! G - }
复制代码编译下载后,可以看到LED3与LED1同时以500ms的频率闪烁。 在终端查看任务,看到led任务在运行中:
+ x* x r7 p) J5 t. j
8 c6 d0 @/ d" r6 g# q' b转载自:社区用户:lugl发布 & a2 g7 }- |5 ]4 f
|