1 /* 2 * Copyright (c) 2022 FuZhou Lockzhiner Electronic Co., Ltd. All rights reserved. 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 16 #include "los_task.h" // OpenHarmony LiteOS的任务管理头文件 17 18 /* 等待时间 */ 19 #define HELLOWORLD_WAIT_MSEC 1000 20 #define OPENHARMONY_WAIT_MSEC 2000 21 22 /* 任务的堆栈大小 */ 23 #define TASK_STACK_SIZE 2048 24 /* 任务的优先级 */ 25 #define HELLWORLD_TASK_PRIO 24 26 #define OPENHARMONY_TASK_PRIO 25 27 28 /*************************************************************** 29 * 函数名称: task_helloworld 30 * 说 明: 线程函数helloworld 31 * 参 数: 无 32 * 返 回 值: 无 33 ***************************************************************/ task_helloworld(void)34void task_helloworld(void) 35 { 36 while (1) { 37 printf("Hello World\n"); 38 /* 睡眠1秒。该函数为OpenHarmony LiteoS内核睡眠函数,单位为:毫秒 */ 39 LOS_Msleep(HELLOWORLD_WAIT_MSEC); 40 } 41 } 42 43 /*************************************************************** 44 * 函数名称: task_openharmony 45 * 说 明: 线程函数 46 * 参 数: 无 47 * 返 回 值: 无 48 ***************************************************************/ task_openharmony(void)49void task_openharmony(void) 50 { 51 while (1) { 52 printf("Hello OpenHarmony\n"); 53 /* 睡眠2秒。该函数为OpenHarmony内核睡眠函数,单位为:毫秒 */ 54 LOS_Msleep(OPENHARMONY_WAIT_MSEC); 55 } 56 } 57 58 /*************************************************************** 59 * 函数名称: helloworld_example 60 * 说 明: 内核任务创建例程 61 * 参 数: 无 62 * 返 回 值: 无 63 ***************************************************************/ helloworld_example(void)64void helloworld_example(void) 65 { 66 /* 任务id */ 67 unsigned int thread_id1; 68 unsigned int thread_id2; 69 /* 任务参数 */ 70 TSK_INIT_PARAM_S task1 = {0}; 71 TSK_INIT_PARAM_S task2 = {0}; 72 /* 返回值 */ 73 unsigned int ret = LOS_OK; 74 75 /* 创建HelloWorld任务 */ 76 task1.pfnTaskEntry = (TSK_ENTRY_FUNC)task_helloworld; // 运行函数入口 77 task1.uwStackSize = TASK_STACK_SIZE; // 堆栈大小 78 task1.pcName = "task_helloworld"; // 函数注册名称 79 task1.usTaskPrio = HELLWORLD_TASK_PRIO; // 任务的优先级,从0~63 80 ret = LOS_TaskCreate(&thread_id1, &task1); // 创建任务 81 if (ret != LOS_OK) { 82 printf("Falied to create task_helloworld ret:0x%x\n", ret); 83 return; 84 } 85 86 task2.pfnTaskEntry = (TSK_ENTRY_FUNC)task_openharmony; // 运行函数入口 87 task2.uwStackSize = TASK_STACK_SIZE; // 堆栈大小 88 task2.pcName = "task_openharmony"; // 函数注册名称 89 task2.usTaskPrio = OPENHARMONY_TASK_PRIO; // 任务的优先级,从0~63 90 ret = LOS_TaskCreate(&thread_id2, &task2); // 创建任务 91 if (ret != LOS_OK) { 92 printf("Falied to create task_openharmony ret:0x%x\n", ret); 93 return; 94 } 95 } 96 97