当前位置:   article > 正文

Hi3861 OpenHarmony嵌入式应用入门--LiteOS Thread_hi3861工具链

hi3861工具链

目录

Thread API

主要接口说明

测试代码编写

代码分析


hi3861使用的实时系统主要是基于Huawei LiteOS-M,这是华为针对物联网领域推出的轻量级物联网操作系统内核。LiteOS-M是Huawei LiteOS的一个分支,专为IoT领域构建,主要面向没有MMU(内存管理单元)的处理器。它具备轻量级、低功耗、组件丰富、快速开发等关键能力,为开发者提供“一站式”完整软件平台。

技术特点:

轻量级:LiteOS-M内核小巧,适合在资源受限的设备上运行。

低功耗:针对IoT设备的特点,LiteOS-M优化了功耗管理,使设备在运行时更省电。

快速开发:提供丰富的组件和API,降低开发门槛,缩短开发周期。

LiteOS-M通过优化任务调度和中断处理,保证了系统的实时响应能力。

结合hi3861的硬件性能,LiteOS-M能够确保在IoT设备中快速响应各种事件和数据变化。

Thread API

API名称

说明

osThreadNew

创建一个线程并将其加入活跃线程组中

osThreadGetName

返回指定线程的名字

osThreadGetId

返回当前运行线程的线程ID

osThreadGetState

返回当前线程的状态

osThreadSetPriority

设置指定线程的优先级

osThreadGetPriority

获取当前线程的优先级

osThreadYield

将运行控制转交给下一个处于READY状态的线程

osThreadSuspend

挂起指定线程的运行

osThreadResume

恢复指定线程的运行

osThreadDetach

分离指定的线程(当线程终止运行时,线程存储可以被回收)

osThreadJoin

等待指定线程终止运行

osThreadExit

终止当前线程的运行

osThreadTerminate

终止指定线程的运行

osThreadGetStackSize

获取指定线程的栈空间大小

osThreadGetStackSpace

获取指定线程的未使用的栈空间大小

osThreadGetCount

获取活跃线程数

osThreadEnumerate

获取线程组中的活跃线程数

主要接口说明

osThreadId_t osThreadNew(osThreadFunc_t func, void *argument,const osThreadAttr_t *attr )

注意 :不能在中断服务调用该函数

参数:

名字

描述

func

线程函数.

argument

作为启动参数传递给线程函数的指针

attr

线程属性

osStatus_t osThreadTerminate (osThreadId_t thread_id)

名字

描述

thread_id

指定线程id,该id是由osThreadNew或者osThreadGetId获得

测试代码编写

修改D:\DevEcoProjects\test\src\vendor\rtplay\rt_hi3861\demo\BUILD.gn文件

  1. # Copyright (c) 2023 Beijing HuaQing YuanJian Education Technology Co., Ltd
  2. # Licensed under the Apache License, Version 2.0 (the "License");
  3. # you may not use this file except in compliance with the License.
  4. # You may obtain a copy of the License at
  5. #
  6. # http://www.apache.org/licenses/LICENSE-2.0
  7. #
  8. # Unless required by applicable law or agreed to in writing, software
  9. # distributed under the License is distributed on an "AS IS" BASIS,
  10. # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  11. # See the License for the specific language governing permissions and
  12. # limitations under the License.
  13. import("//build/lite/config/component/lite_component.gni")
  14. lite_component("demo") {
  15. features = [
  16. #"base_00_helloworld:base_helloworld_example",
  17. #"base_01_led:base_led_example",
  18. #"base_02_loopkey:base_loopkey_example",
  19. #"base_03_irqkey:base_irqkey_example",
  20. #"base_04_adc:base_adc_example",
  21. #"base_05_pwm:base_pwm_example",
  22. #"base_06_ssd1306:base_ssd1306_example",
  23. "kernel_01_task:kernel_task_example",
  24. ]
  25. }

创建D:\DevEcoProjects\test\src\vendor\rtplay\rt_hi3861\demo\kernel_01_task文件夹

文件夹中创建D:\DevEcoProjects\test\src\vendor\rtplay\rt_hi3861\demo\kernel_01_task\kernel_task_example.c文件D:\DevEcoProjects\test\src\vendor\rtplay\rt_hi3861\demo\kernel_01_task\BUILD.gn文件

  1. # Copyright (c) 2023 Beijing HuaQing YuanJian Education Technology Co., Ltd
  2. # Licensed under the Apache License, Version 2.0 (the "License");
  3. # you may not use this file except in compliance with the License.
  4. # You may obtain a copy of the License at
  5. #
  6. # http://www.apache.org/licenses/LICENSE-2.0
  7. #
  8. # Unless required by applicable law or agreed to in writing, software
  9. # distributed under the License is distributed on an "AS IS" BASIS,
  10. # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  11. # See the License for the specific language governing permissions and
  12. # limitations under the License.
  13. static_library("kernel_task_example") {
  14. sources = [
  15. "kernel_task_example.c",
  16. ]
  17. include_dirs = [
  18. "//utils/native/lite/include",
  19. "//kernel/liteos_m/kal/cmsis",
  20. "//base/iot_hardware/peripheral/interfaces/kits",
  21. "//vendor/hqyj/fs_hi3861/common/bsp/include"
  22. ]
  23. }
  1. /*
  2. * Copyright (C) 2023 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 "ohos_init.h"
  18. #include "cmsis_os2.h"
  19. #define THREAD_NUM (1000)
  20. #define STACK_SIZE (1024)
  21. #define DELAY_TICKS_20 (20)
  22. #define DELAY_TICKS_100 (100)
  23. osThreadId_t newThread(char *name, osThreadFunc_t func, char *arg)
  24. {
  25. osThreadAttr_t attr = {
  26. name, 0, NULL, 0, NULL, STACK_SIZE*2, osPriorityNormal, 0, 0
  27. };
  28. osThreadId_t tid = osThreadNew(func, (void *)arg, &attr);
  29. if (tid == NULL) {
  30. printf("[Thread Test] osThreadNew(%s) failed.\r\n", name);
  31. } else {
  32. printf("[Thread Test] osThreadNew(%s) success, thread id: %d.\r\n", name, tid);
  33. }
  34. return tid;
  35. }
  36. void threadTest(char *arg)
  37. {
  38. static int count = 0;
  39. printf("%s\r\n", arg);
  40. osThreadId_t tid = osThreadGetId();
  41. printf("[Thread Test] threadTest osThreadGetId, thread id:%p\r\n", tid);
  42. while (count < THREAD_NUM) {
  43. count++;
  44. printf("[Thread Test] threadTest, count: %d.\r\n", count);
  45. osDelay(DELAY_TICKS_20);
  46. }
  47. }
  48. void rtosv2_thread_main(void)
  49. {
  50. osThreadId_t tid = newThread("test_thread", threadTest, "This is a test thread.");
  51. const char *t_name = osThreadGetName(tid);
  52. printf("[Thread Test] osThreadGetName, thread name: %s.\r\n", t_name);
  53. osThreadState_t state = osThreadGetState(tid);
  54. printf("[Thread Test] osThreadGetState, state :%d.\r\n", state);
  55. osStatus_t status = osThreadSetPriority(tid, osPriorityNormal4);
  56. printf("[Thread Test] osThreadSetPriority, status: %d.\r\n", status);
  57. osPriority_t pri = osThreadGetPriority(tid);
  58. printf("[Thread Test] osThreadGetPriority, priority: %d.\r\n", pri);
  59. status = osThreadSuspend(tid);
  60. printf("[Thread Test] osThreadSuspend, status: %d.\r\n", status);
  61. status = osThreadResume(tid);
  62. printf("[Thread Test] osThreadResume, status: %d.\r\n", status);
  63. uint32_t stacksize = osThreadGetStackSize(tid);
  64. printf("[Thread Test] osThreadGetStackSize, stacksize: %u.\r\n", stacksize);
  65. uint32_t stackspace = osThreadGetStackSpace(tid);
  66. printf("[Thread Test] osThreadGetStackSpace, stackspace: %u.\r\n", stackspace);
  67. uint32_t t_count = osThreadGetCount();
  68. printf("[Thread Test] osThreadGetCount, count: %u.\r\n", t_count);
  69. osDelay(DELAY_TICKS_100);
  70. status = osThreadTerminate(tid);
  71. printf("[Thread Test] osThreadTerminate, status: %d.\r\n", status);
  72. }
  73. static void ThreadTestTask(void)
  74. {
  75. osThreadAttr_t attr;
  76. attr.name = "rtosv2_thread_main";
  77. attr.attr_bits = 0U;
  78. attr.cb_mem = NULL;
  79. attr.cb_size = 0U;
  80. attr.stack_mem = NULL;
  81. attr.stack_size = STACK_SIZE;
  82. attr.priority = osPriorityNormal;
  83. if (osThreadNew((osThreadFunc_t)rtosv2_thread_main, NULL, &attr) == NULL) {
  84. printf("[ThreadTestTask] Falied to create rtosv2_thread_main!\n");
  85. }
  86. }
  87. APP_FEATURE_INIT(ThreadTestTask);

代码分析

创建线程,创建成功则打印线程名字和线程ID

  1. osThreadId_t newThread(char *name, osThreadFunc_t func, char *arg)
  2. {
  3. // 定义线程属性结构体
  4. osThreadAttr_t attr = {
  5. name, 0, NULL, 0, NULL, STACK_SIZE*2, osPriorityNormal, 0, 0
  6. };
  7. // 创建线程,并传入参数
  8. osThreadId_t tid = osThreadNew(func, (void *)arg, &attr);
  9. if (tid == NULL) {
  10. // 如果创建失败,输出提示信息
  11. printf("[Thread Test] osThreadNew(%s) failed.\r\n", name);
  12. } else {
  13. // 如果创建成功,输出提示信息
  14. printf("[Thread Test] osThreadNew(%s) success, thread id: %d.\r\n", name, tid);
  15. }
  16. return tid;
  17. }

该函数首先会打印自己的参数,然后对全局变量count进行循环+1操作,之后会打印count的值

  1. void threadTest(char *arg)
  2. {
  3. // 定义静态变量count,用于记录线程执行次数
  4. static int count = 0;
  5. // 打印传入参数
  6. printf("%s\r\n", arg);
  7. // 获取当前线程ID
  8. osThreadId_t tid = osThreadGetId();
  9. // 打印当前线程ID
  10. printf("[Thread Test] threadTest osThreadGetId, thread id:%p\r\n", tid);
  11. //count小于THREAD_NUM时,循环执行
  12. while (count < THREAD_NUM) {
  13. // count1
  14. count++;
  15. // 打印count
  16. printf("[Thread Test] threadTest, count: %d.\r\n", count);
  17. // 延时20个节拍
  18. osDelay(DELAY_TICKS_20);
  19. }
  20. }

主程序rtosv2_thread_main创建线程并运行,并使用上述API进行相关操作,最后终止所创建的线程。

  1. void rtosv2_thread_main(void)
  2. {
  3. // 创建一个新的线程
  4. osThreadId_t tid = newThread("test_thread", threadTest, "This is a test thread.");
  5. // 获取线程名称
  6. const char *t_name = osThreadGetName(tid);
  7. printf("[Thread Test] osThreadGetName, thread name: %s.\r\n", t_name);
  8. // 获取线程状态
  9. osThreadState_t state = osThreadGetState(tid);
  10. printf("[Thread Test] osThreadGetState, state :%d.\r\n", state);
  11. // 设置线程优先级
  12. osStatus_t status = osThreadSetPriority(tid, osPriorityNormal4);
  13. printf("[Thread Test] osThreadSetPriority, status: %d.\r\n", status);
  14. // 获取线程优先级
  15. osPriority_t pri = osThreadGetPriority(tid);
  16. printf("[Thread Test] osThreadGetPriority, priority: %d.\r\n", pri);
  17. // 暂停线程
  18. status = osThreadSuspend(tid);
  19. printf("[Thread Test] osThreadSuspend, status: %d.\r\n", status);
  20. // 恢复线程
  21. status = osThreadResume(tid);
  22. printf("[Thread Test] osThreadResume, status: %d.\r\n", status);
  23. // 获取线程栈大小
  24. uint32_t stacksize = osThreadGetStackSize(tid);
  25. printf("[Thread Test] osThreadGetStackSize, stacksize: %u.\r\n", stacksize);
  26. // 获取线程栈空间
  27. uint32_t stackspace = osThreadGetStackSpace(tid);
  28. printf("[Thread Test] osThreadGetStackSpace, stackspace: %u.\r\n", stackspace);
  29. // 获取活跃线程数
  30. uint32_t t_count = osThreadGetCount();
  31. printf("[Thread Test] osThreadGetCount, count: %u.\r\n", t_count);
  32. // 延时100个时钟周期
  33. osDelay(DELAY_TICKS_100);
  34. // 终止线程
  35. status = osThreadTerminate(tid);
  36. printf("[Thread Test] osThreadTerminate, status: %d.\r\n", status);
  37. }

使用build,编译成功后,使用upload进行烧录。

声明:本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:【wpsshop博客】
推荐阅读
相关标签
  

闽ICP备14008679号