基于小熊派Hi3861鸿蒙开发的IoT物联网学习【二】
案例 :cmsis_os2的API任务接口

#include <stdio.h>
#include <string.h>
#include <unistd.h> #include "ohos_init.h"
#include "cmsis_os2.h" osThreadId_t threadHiID;
osThreadId_t threadLoID; /*****任务一*****/
void threadHi(void)
{
// int sum = 0;
// while (1)
// {
// printf("This is BearPi Harmony Thread1----%d\r\n", sum++);
// usleep(1000000);
// }
printf("enter threadHi\r\n");
osDelay(1); //其作用是让任务阻塞
printf("threadHi delay done\r\n");
osThreadSuspend(threadHiID); //任务挂起
printf("threadHi osThreadResume success\r\n");
osThreadTerminate(threadHiID); //删除某个任务 } /*****任务二*****/
void threadLo(void)
{
// int sum = 0;
// while (1)
// {
// printf("This is BearPi Harmony Thread2----%d\r\n", sum++);
// usleep(500000);
// }
for (int i = 0;i<10;i++){
printf("enter threadLo\r\n");
}
printf("threadHi osThreadSuspend success\r\n");
osThreadResume(threadHiID); // 任务恢复
osThreadTerminate(threadLoID); //删除某个任务
} /*****任务创建*****/
static void Thread_example(void)
{
osThreadAttr_t attr; threadHiID = osThreadNew((osThreadFunc_t)threadHi,NULL,&attr);
if (threadHiID == NULL)
{
printf("Falied to create threadHi!\n");
} attr.name = "threadLo";
attr.priority = 24 ;
threadLoID =osThreadNew((osThreadFunc_t)threadLo,NULL,&attr); if (threadLoID== NULL)
{
printf("Falied to create threadLo!\n");
}
} APP_FEATURE_INIT(Thread_example);
// SYS_RUN(Thread_example);
#include <stdio.h>
#include <string.h>
#include <unistd.h> #include "ohos_init.h"
#include "cmsis_os2.h" osSemaphoreId_t sem1; void Thread_Semaphore1(void)
{
osStatus_t status;
while (1)
{
//申请两次sem1信号量,使得Thread_Semaphore2和Thread_Semaphore3能同步执行
status = osSemaphoreRelease(sem1);
if (status!=osOK){
printf("semaphore fail");
}else{
printf("semaphore success");
} //此处若只申请一次信号量,则Thread_Semaphore2和Thread_Semaphore3会交替运行。
// osSemaphoreRelease(sem1); // printf("Thread_Semaphore i %d \n",i);
// i=i+1;
// printf("Thread_Semaphore sem1 %d \n",sem1);
// printf("Thread_Semaphore1 Release Semap \n");
osDelay(100);
}
}
void Thread_Semaphore2(void)
{
osStatus_t status;
while (1)
{
//等待sem1信号量
status = osSemaphoreAcquire(sem1, 50U);
if (status!=osOK){
printf("semaphore2 fail");
}else{
printf("semaphore2 success");
}
}
} void Thread_Semaphore3(void)
{
osStatus_t status;
while (1)
{
//等待sem1信号量
status = osSemaphoreAcquire(sem1, osWaitForever);
if (status!=osOK){
printf("semaphore3 fail");
}else{
printf("semaphore3 success");
}
osDelay(1);
}
} void Semaphore_example(void)
{
osThreadAttr_t attr; attr.attr_bits = 0U;
attr.cb_mem = NULL;
attr.cb_size = 0U;
attr.stack_mem = NULL;
attr.stack_size = 1024 * 4;
attr.priority = 24; attr.name = "Thread_Semaphore1";
if (osThreadNew((osThreadFunc_t)Thread_Semaphore1, NULL, &attr) == NULL)
{
printf("Falied to create Thread_Semaphore1!\n");
}
attr.name = "Thread_Semaphore2";
if (osThreadNew((osThreadFunc_t)Thread_Semaphore2, NULL, &attr) == NULL)
{
printf("Falied to create Thread_Semaphore2!\n");
}
attr.name = "Thread_Semaphore3";
if (osThreadNew((osThreadFunc_t)Thread_Semaphore3, NULL, &attr) == NULL)
{
printf("Falied to create Thread_Semaphore3!\n");
}
sem1 = osSemaphoreNew(4, 0, NULL);
if (sem1 == NULL)
{
printf("Falied to create Semaphore1!\n");
}
}
APP_FEATURE_INIT(Semaphore_example);
BUILD.gn文件:
static_library("semaphore_example") {
sources = [
"Semaphore_example.c"
]
include_dirs = [
"//utils/native/lite/include",
"//kernel/liteos_m/components/cmsis/2.0",
]
}
BUILD.gn
import("//build/lite/config/component/lite_component.gni")
lite_component("app") {
features = [
"A5_kernel_semaphore:semaphore_example"
]
}
代码写完后用hpm dist命令来编译

烧录代码到开发板:

后台log

基于小熊派Hi3861鸿蒙开发的IoT物联网学习【二】的更多相关文章
- 基于小熊派Hi3861鸿蒙开发的IoT物联网学习【一】
基于小熊派鸿蒙季BearPi-HM_Nano HarmonyOS 鸿蒙系统Hi3861开发板NFC 开发步骤:1.购买开发板:某宝上购买就行 2.安装开发环境 3.下载源码 4.编写案例并执行 开发 ...
- 基于小熊派Hi3861鸿蒙开发的IoT物联网学习【五】
BearPi-HM_Nano开发板鸿蒙OS内核编程开发--消息队列 什么是消息队列? 答:消息队列中间件是分布式系统中重要的组件,主要解决应用耦合.异步消息.流量削锋等问题.实现高性能. ...
- 基于小熊派Hi3861鸿蒙开发的IoT物联网学习【三】
软件定时器:是基于系统Tick时钟中断且由软件来模拟的定时器,当经过设定的Tick时钟计数值后会触发用户定义的回调函数.定时精度与系统Tick时钟的周期有关. 定时器运行机制: cmsis_os2的A ...
- 基于小熊派Hi3861鸿蒙开发的IoT物联网学习【四】
一.互斥锁基本概念: 1.互斥锁又称互斥型信号量,是一种特殊的二值性信号量[二值型信号量可以理解为任务与中断间或者两个任务间的标志,该标志非"满"即"空"],用 ...
- 基于小程序云Serverless开发微信小程序
本文主要以使用小程序云Serverless服务开发一个记事本微信小程序为例介绍如何使用小程序云Serverless开发微信小程序.记事本小程序的开发涉及到云函数调用.云数据库存储.图片存储等功能,较好 ...
- 如何基于App SDK快速地开发一个IoT App?
一.背景及大纲介绍 在如今物联网DCM(Device.Connect.Manage)的大框架下,有一个应用层来分析和处理数据,是必备技能.但是,对于一个公司来说,因为研发能力或者研发时间的原因,可能很 ...
- 华为云MVP熊保松谈物联网开发:华为云IoT是首选,小熊派是神器
摘要:在AI.5G的技术驱动下,物联网行业的发展愈加如火如荼,开发者在技术的快速更迭间,也得乘风破浪跟上新技术的节奏. 在AI.5G的技术驱动下,物联网行业的发展愈加如火如荼,开发者在技术的快速更迭间 ...
- 开发实践丨用小熊派STM32开发板模拟自动售货机
摘要:本文内容是讲述用小熊派开发板模拟自动售货机,基于论坛提供的工程代码,通过云端开发和设备终端开发,实现终端数据在的华为云平台显示. 本文内容是讲述用小熊派开发板模拟自动售货机,基于论坛提供的工程代 ...
- 【资源下载】Linux下的Hi3861一站式鸿蒙开发烧录(附工具)
下载附件 2021春节前夕,华为发布了 HUAWEI DevEco Device Tool 2.0 Beta1,整体提供了异常强大的功能.得知消息后,我在第一时间带着无比兴奋的心情下载尝鲜,但结果却是 ...
随机推荐
- .NET平台系列24:从.NET Framework迁移到.NET Core/.NET5的技术指南
系列目录 [已更新最新开发文章,点击查看详细] 本文讲解了在将代码从 .NET Framework 移植到 .NET(旧称为 .NET Core)时应考虑的事项. 对于许多项目,从 .NET ...
- 【VBA】测试程序运行时间,延时方法
测试程序运行时间 Dim start As Date start = Now() Dim i As Long For i = 0 To 10000000 ' 10 million Next Debug ...
- vue根据变量值绑定src的路径
路径必须用require包裹起来才会起作用
- Shiro安全框架「快速入门」就这一篇
Shiro 简介 照例又去官网扒了扒介绍: Apache Shiro is a powerful and easy-to-use Java security framework that perfor ...
- Centos8.3、proxysql2.0读写分离实战记录
接着主从复制继续往下讲,这个项目中我是使用proxysql做读写分离的中间件,之前是使用mycat.老实说mycat属于比较重量级的中间件,1.0还好到了2.0配置变得很复杂而且文档不是很齐全,我看着 ...
- js笔记7
1.作用域链 作用域:浏览器给js的一个生存环境(栈内存) 作用域链:js中的关键字var和function都可以提前声明和定义,提前声明和定义的放在我们的内存地址(堆内存)中.然后js从上到下逐行执 ...
- python数字游戏
import random a=random.randint(1,10) b=0 num=3 while num>0: print("你还有"+str(num)+&qu ...
- python读取json文件制作股票价格走势
- 痞子衡嵌入式:以i.MXRT1xxx的GPIO模块为例谈谈中断处理函数(IRQHandler)的标准流程
大家好,我是痞子衡,是正经搞技术的痞子.今天痞子衡给大家介绍的是以i.MXRT的GPIO模块为例谈谈中断处理函数(IRQHandler)的标准流程. 在痞子衡旧文 <串口(UART)自动波特率识 ...
- http连接复用进化论
HTTP协议是应用层协议,它定义万维网客户端如何与服务器进行通信.它在传输层的TCP协议的基础上进行数据传输 HTTP 1.0 在HTTP 1.0时代,默认一个http请求对应一个TCP连接,没有任何 ...