案例 :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