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