1 /*
2 * Copyright (C) 2022 HiHope Open Source Organization .
3 * Licensed under the Apache License, Version 2.0 (the "License");
4 * you may not use this file except in compliance with the License.
5 * You may obtain a copy of the License at
6 *
7 * http:// www.apache.org/licenses/LICENSE-2.0
8 *
9 * Unless required by applicable law or agreed to in writing, software
10 * distributed under the License is distributed on an "AS IS" BASIS,
11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 * See the License for the specific language governing permissions and
13 *
14 * limitations under the License.
15 */
16
17 #include <stdio.h>
18 #include <unistd.h>
19 #include "ohos_init.h"
20 #include "cmsis_os2.h"
21
22 #define STACK_SIZE 1024
23 #define OS_DELAY 100
24 #define OS_DELAYONE 20
25
newThread(char * name,osThreadFunc_t func,int * arg)26 osThreadId_t newThread(char *name, osThreadFunc_t func, int*arg)
27 {
28 osThreadAttr_t attr = {
29 name, 0, NULL, 0, NULL, 1024*2, osPriorityNormal, 0, 0
30 };
31 osThreadId_t tid = osThreadNew(func, arg, &attr);
32 if (tid == NULL) {
33 printf("[Thread Test] osThreadNew(%s) failed.\r\n", name);
34 } else {
35 printf("[Thread Test] osThreadNew(%s) success, thread id: %d.\r\n", name, tid);
36 }
37 return tid;
38 }
39
threadTest(int * arg)40 void threadTest(int *arg)
41 {
42 static int count = 0;
43 printf("%s\r\n", (char *)arg);
44 osThreadId_t tid = osThreadGetId();
45 printf("[Thread Test] threadTest osThreadGetId, thread id:%p\r\n", tid);
46 while (1) {
47 count++;
48 printf("[Thread Test] threadTest, count: %d.\r\n", count);
49 osDelay(OS_DELAYONE);
50 }
51 }
52
rtosv2_thread_main(int * arg)53 void rtosv2_thread_main(int *arg)
54 {
55 (void)arg;
56 osThreadId_t tid = newThread("test_thread", threadTest, "This is a test thread.");
57
58 const char *t_name = osThreadGetName(tid);
59 printf("[Thread Test] osThreadGetName, thread name: %s.\r\n", t_name);
60
61 osThreadState_t state = osThreadGetState(tid);
62 printf("[Thread Test] osThreadGetState, state :%d.\r\n", state);
63
64 osStatus_t status = osThreadSetPriority(tid, osPriorityNormal4);
65 printf("[Thread Test] osThreadSetPriority, status: %d.\r\n", status);
66
67 osPriority_t pri = osThreadGetPriority(tid);
68 printf("[Thread Test] osThreadGetPriority, priority: %d.\r\n", pri);
69
70 status = osThreadSuspend(tid);
71 printf("[Thread Test] osThreadSuspend, status: %d.\r\n", status);
72
73 status = osThreadResume(tid);
74 printf("[Thread Test] osThreadResume, status: %d.\r\n", status);
75
76 uint32_t stacksize = osThreadGetStackSize(tid);
77 printf("[Thread Test] osThreadGetStackSize, stacksize: %u.\r\n", stacksize);
78
79 uint32_t stackspace = osThreadGetStackSpace(tid);
80 printf("[Thread Test] osThreadGetStackSpace, stackspace: %u.\r\n", stackspace);
81
82 uint32_t t_count = osThreadGetCount();
83 printf("[Thread Test] osThreadGetCount, count: %u.\r\n", t_count);
84
85 osDelay(OS_DELAY);
86 status = osThreadTerminate(tid);
87 printf("[Thread Test] osThreadTerminate, status: %d.\r\n", status);
88 }
89
ThreadTestTask(void)90 static void ThreadTestTask(void)
91 {
92 osThreadAttr_t attr;
93 attr.name = "rtosv2_thread_main";
94 attr.attr_bits = 0U;
95 attr.cb_mem = NULL;
96 attr.cb_size = 0U;
97 attr.stack_mem = NULL;
98 attr.stack_size = STACK_SIZE;
99 attr.priority = osPriorityNormal;
100
101 if (osThreadNew((osThreadFunc_t)rtosv2_thread_main, NULL, &attr) == NULL) {
102 printf("[ThreadTestTask] Failed to create rtosv2_thread_main!\n");
103 }
104 }
105
106 APP_FEATURE_INIT(ThreadTestTask);