1 /* main.c - Hello World demo */
2
3 /*
4 * Copyright (c) 2012-2014 Wind River Systems, Inc.
5 *
6 * SPDX-License-Identifier: Apache-2.0
7 */
8
9 #include <zephyr/kernel.h>
10 #include <zephyr/sys/printk.h>
11
12 /*
13 * The hello world demo has two threads that utilize semaphores and sleeping
14 * to take turns printing a greeting message at a controlled rate. The demo
15 * shows both the static and dynamic approaches for spawning a thread; a real
16 * world application would likely use the static approach for both threads.
17 */
18
19 #define PIN_THREADS (IS_ENABLED(CONFIG_SMP) && IS_ENABLED(CONFIG_SCHED_CPU_MASK))
20
21 /* size of stack area used by each thread */
22 #define STACKSIZE 1024
23
24 /* scheduling priority used by each thread */
25 #define PRIORITY 7
26
27 /* delay between greetings (in ms) */
28 #define SLEEPTIME 500
29
30
31 /*
32 * @param my_name thread identification string
33 * @param my_sem thread's own semaphore
34 * @param other_sem other thread's semaphore
35 */
helloLoop(const char * my_name,struct k_sem * my_sem,struct k_sem * other_sem)36 void helloLoop(const char *my_name,
37 struct k_sem *my_sem, struct k_sem *other_sem)
38 {
39 const char *tname;
40 uint8_t cpu;
41 struct k_thread *current_thread;
42
43 while (1) {
44 /* take my semaphore */
45 k_sem_take(my_sem, K_FOREVER);
46
47 current_thread = k_current_get();
48 tname = k_thread_name_get(current_thread);
49 #if CONFIG_SMP
50 cpu = arch_curr_cpu()->id;
51 #else
52 cpu = 0;
53 #endif
54 /* say "hello" */
55 if (tname == NULL) {
56 printk("%s: Hello World from cpu %d on %s!\n",
57 my_name, cpu, CONFIG_BOARD);
58 } else {
59 printk("%s: Hello World from cpu %d on %s!\n",
60 tname, cpu, CONFIG_BOARD);
61 }
62
63 /* wait a while, then let other thread have a turn */
64 k_busy_wait(100000);
65 k_msleep(SLEEPTIME);
66 k_sem_give(other_sem);
67 }
68 }
69
70 /* define semaphores */
71
72 K_SEM_DEFINE(threadA_sem, 1, 1); /* starts off "available" */
73 K_SEM_DEFINE(threadB_sem, 0, 1); /* starts off "not available" */
74
75
76 /* threadB is a dynamic thread that is spawned by threadA */
77
threadB(void * dummy1,void * dummy2,void * dummy3)78 void threadB(void *dummy1, void *dummy2, void *dummy3)
79 {
80 ARG_UNUSED(dummy1);
81 ARG_UNUSED(dummy2);
82 ARG_UNUSED(dummy3);
83
84 /* invoke routine to ping-pong hello messages with threadA */
85 helloLoop(__func__, &threadB_sem, &threadA_sem);
86 }
87
88 K_THREAD_STACK_DEFINE(threadA_stack_area, STACKSIZE);
89 static struct k_thread threadA_data;
90
91 K_THREAD_STACK_DEFINE(threadB_stack_area, STACKSIZE);
92 static struct k_thread threadB_data;
93
94 /* threadA is a static thread that is spawned automatically */
95
threadA(void * dummy1,void * dummy2,void * dummy3)96 void threadA(void *dummy1, void *dummy2, void *dummy3)
97 {
98 ARG_UNUSED(dummy1);
99 ARG_UNUSED(dummy2);
100 ARG_UNUSED(dummy3);
101
102 /* invoke routine to ping-pong hello messages with threadB */
103 helloLoop(__func__, &threadA_sem, &threadB_sem);
104 }
105
main(void)106 int main(void)
107 {
108 k_thread_create(&threadA_data, threadA_stack_area,
109 K_THREAD_STACK_SIZEOF(threadA_stack_area),
110 threadA, NULL, NULL, NULL,
111 PRIORITY, 0, K_FOREVER);
112 k_thread_name_set(&threadA_data, "thread_a");
113 #if PIN_THREADS
114 if (arch_num_cpus() > 1) {
115 k_thread_cpu_pin(&threadA_data, 0);
116 }
117 #endif
118
119 k_thread_create(&threadB_data, threadB_stack_area,
120 K_THREAD_STACK_SIZEOF(threadB_stack_area),
121 threadB, NULL, NULL, NULL,
122 PRIORITY, 0, K_FOREVER);
123 k_thread_name_set(&threadB_data, "thread_b");
124 #if PIN_THREADS
125 if (arch_num_cpus() > 1) {
126 k_thread_cpu_pin(&threadB_data, 1);
127 }
128 #endif
129
130 k_thread_start(&threadA_data);
131 k_thread_start(&threadB_data);
132 return 0;
133 }
134