• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1  /*
2  Copyright (C) 2024 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  limitations under the License.
14  */
15 #include <stdio.h>
16 #include <unistd.h>
17 #include "osal_debug.h"
18 #include "ohos_init.h"
19 #include "cmsis_os2.h"
20 
newThread(char * name,osThreadFunc_t func,void * arg)21 osThreadId_t newThread(char *name, osThreadFunc_t func, void *arg)
22 {
23     osThreadAttr_t attr = {
24         name, 0, NULL, 0, NULL, 1024*2, osPriorityNormal, 0, 0
25     };
26     osThreadId_t tid = osThreadNew(func, arg, &attr);
27     if (tid == NULL) {
28         printf("[Thread Test] osThreadNew(%s) failed.\r\n", name);
29     } else {
30         printf("[Thread Test] osThreadNew(%s) success, thread id: %d.\r\n", name, tid);
31     }
32     return tid;
33 }
34 
threadTest(void * arg)35 void threadTest(void *arg)
36 {
37     static int count = 0;
38     const uint32_t DelayTimeMs = 20;
39     printf("%s\r\n", (char *)arg);
40     osThreadId_t tid = osThreadGetId();
41     printf("[Thread Test] threadTest osThreadGetId, thread id:%p\r\n", tid);
42     while (1) {
43         count++;
44         printf("[Thread Test] threadTest, count: %d.\r\n", count);
45         osDelay(DelayTimeMs);
46     }
47 }
48 
rtosv2_thread_main(void * arg)49 void rtosv2_thread_main(void *arg)
50 {
51     (void)arg;
52     const uint32_t DelayTimeMs = 100;
53 
54     osThreadId_t tid = newThread("test_thread", threadTest, "This is a test thread.");
55 
56     const char *t_name = osThreadGetName(tid);
57     printf("[Thread Test] osThreadGetName, thread name: %s.\r\n", t_name);
58 
59     osThreadState_t state = osThreadGetState(tid);
60     printf("[Thread Test] osThreadGetState, state :%d.\r\n", state);
61 
62     osStatus_t status = osThreadSetPriority(tid, osPriorityNormal4);
63     printf("[Thread Test] osThreadSetPriority, status: %d.\r\n", status);
64 
65     osPriority_t pri = osThreadGetPriority (tid);
66     printf("[Thread Test] osThreadGetPriority, priority: %d.\r\n", pri);
67 
68     status = osThreadSuspend(tid);
69     printf("[Thread Test] osThreadSuspend, status: %d.\r\n", status);
70 
71     status = osThreadResume(tid);
72     printf("[Thread Test] osThreadResume, status: %d.\r\n", status);
73 
74     uint32_t stacksize = osThreadGetStackSize(tid);
75     printf("[Thread Test] osThreadGetStackSize, stacksize: %d.\r\n", stacksize);
76 
77     uint32_t stackspace = osThreadGetStackSpace(tid);
78     printf("[Thread Test] osThreadGetStackSpace, stackspace: %d.\r\n", stackspace);
79 
80     uint32_t t_count = osThreadGetCount();
81     printf("[Thread Test] osThreadGetCount, count: %d.\r\n", t_count);
82 
83     osDelay(DelayTimeMs);
84     status = osThreadTerminate(tid);
85     printf("[Thread Test] osThreadTerminate, status: %d.\r\n", status);
86 }
87 
ThreadTestTask(void)88 static void ThreadTestTask(void)
89 {
90     osThreadAttr_t attr;
91 
92     attr.name = "rtosv2_thread_main";
93     attr.attr_bits = 0U;
94     attr.cb_mem = NULL;
95     attr.cb_size = 0U;
96     attr.stack_mem = NULL;
97     attr.stack_size = 0x1000;
98     attr.priority = osPriorityNormal;
99 
100     if (osThreadNew((osThreadFunc_t)rtosv2_thread_main, NULL, &attr) == NULL) {
101         printf("[ThreadTestTask] Falied to create rtosv2_thread_main!\n");
102     }
103 }
104 
105 APP_FEATURE_INIT(ThreadTestTask);