Hi3861 OpenHarmony嵌入式应用入门--LiteOS Semaphore做计数

可以将其初始化为一个特定的值(比如你想要计数的最大值),然后每次计数增加或减少时,就调用 Semaphore 的 release() 或 acquire() 方法。但请注意,这样做可能不是最直观的,因为 acquire() 方法的调用可能会阻塞,直到 Semaphore 的内部计数器大于零。

Semaphore API

|---------------------|--------------------------------|
| API名称 | 说明 |
| osSemaphoreNew | 创建并初始化一个信号量 |
| osSemaphoreGetName | 获取一个信号量的名字 |
| osSemaphoreAcquire | 获取一个信号量的令牌,若获取不到,则会超时返回 |
| osSemaphoreRelease | 释放一个信号量的令牌,但是令牌的数量不超过初始定义的的令牌数 |
| osSemaphoreGetCount | 获取当前的信号量令牌数 |
| osSemaphoreDelete | 删除一个信号量 |

代码编写

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

# Copyright (c) 2023 Beijing HuaQing YuanJian Education Technology Co., Ltd
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
# 
#    http://www.apache.org/licenses/LICENSE-2.0
# 
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License. 

import("//build/lite/config/component/lite_component.gni")

lite_component("demo") {
  features = [
    #"base_00_helloworld:base_helloworld_example",
    #"base_01_led:base_led_example",
    #"base_02_loopkey:base_loopkey_example",
    #"base_03_irqkey:base_irqkey_example",
    #"base_04_adc:base_adc_example",
    #"base_05_pwm:base_pwm_example",
    #"base_06_ssd1306:base_ssd1306_example",
    #"kernel_01_task:kernel_task_example",
    #"kernel_02_timer:kernel_timer_example",
    #"kernel_03_event:kernel_event_example",
    #"kernel_04_mutex:kernel_mutex_example",
    #"kernel_05_semaphore_as_mutex:kernel_semaphore_as_mutex_example",
    #"kernel_06_semaphore_for_sync:kernel_semaphore_for_sync_example",
    "kernel_07_semaphore_for_count:kernel_semaphore_for_count_example",
  ]
}

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

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

# Copyright (c) 2023 Beijing HuaQing YuanJian Education Technology Co., Ltd
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
# 
#    http://www.apache.org/licenses/LICENSE-2.0
# 
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License. 

static_library("kernel_semaphore_for_count_example") {
    sources = [
        "kernel_semaphore_for_count_example.c"
    ]

    include_dirs = [
        "//utils/native/lite/include",
        "//kernel/liteos_m/kal/cmsis",
    ]
}

/*
 * Copyright (c) 2023 Beijing HuaQing YuanJian Education Technology Co., Ltd
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *    http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

#include <stdio.h>
#include <unistd.h>

#include "ohos_init.h"
#include "cmsis_os2.h"

osThreadId_t Task1_ID;        //  任务1 ID
osThreadId_t Task2_ID;        //  任务2 ID
osSemaphoreId_t Semaphore_ID; // 信号量ID
#define SEM_MAX_COUNT 10
#define TASK_STACK_SIZE (1024)
#define TASK1_DELAY_TIME 1
#define TASK2_DELAY_TIME 2

/**
 * @description: 任务1 模拟停车场的入口
 *  每隔一秒钟,停车场就会进入一辆车
 * @param {*}
 * @return {*}
 */
void Task1(void)
{
    while (1) {
        if (osSemaphoreGetCount(Semaphore_ID) < SEM_MAX_COUNT) {
            if (osSemaphoreRelease(Semaphore_ID) == osOK) {
                // 释放信号量 +1
                printf("[进入%d辆车, 停车场容量: %d] 信号量+1.\n", osSemaphoreGetCount(Semaphore_ID), SEM_MAX_COUNT);
            }
        } else {
            printf("[进入停车场失败, 请等待...]\n");
        }
        sleep(TASK1_DELAY_TIME);
    }
}
/**
 * @description: 任务2
 * @param {*}
 * @return {*}
 */
void Task2(void)
{
    while (1) {
        if (osSemaphoreAcquire(Semaphore_ID, osWaitForever) == osOK) {
             // 释放信号量 -1
            printf("[出去1辆车, 剩余停车场容量: %d] 信号量-1.\n", osSemaphoreGetCount(Semaphore_ID));
        } else {
            printf("[出停车场失败]\n");
        }

        sleep(TASK2_DELAY_TIME);
    }
}
/**
 * @description: 初始化并创建任务
 * @param {*}
 * @return {*}
 */
static void kernel_sync_semaphore_example(void)
{
    printf("Enter kernel_sync_semaphore_example()!\n");

    // 创建信号量    一个停车场最多可以停10辆车,
    Semaphore_ID = osSemaphoreNew(SEM_MAX_COUNT - 1, 0, NULL); // 参数: 最大计数值,初始计数值,参数配置
    if (Semaphore_ID != NULL) {
        printf("ID = %d, Create Semaphore_ID is OK!\n", Semaphore_ID);
    }

    osThreadAttr_t taskOptions;
    taskOptions.name = "Task1";              // 任务的名字
    taskOptions.attr_bits = 0;               // 属性位
    taskOptions.cb_mem = NULL;               // 堆空间地址
    taskOptions.cb_size = 0;                 // 堆空间大小
    taskOptions.stack_mem = NULL;            // 栈空间地址
    taskOptions.stack_size = TASK_STACK_SIZE; // 栈空间大小 单位:字节
    taskOptions.priority = osPriorityNormal; // 任务的优先级

    Task1_ID = osThreadNew((osThreadFunc_t)Task1, NULL, &taskOptions); // 创建任务1
    if (Task1_ID != NULL) {
        printf("ID = %d, Create Task1_ID is OK!\n", Task1_ID);
    }

    taskOptions.name = "Task2";                                        // 任务的名字
    taskOptions.priority = osPriorityNormal;                           // 任务的优先级
    Task2_ID = osThreadNew((osThreadFunc_t)Task2, NULL, &taskOptions); // 创建任务2
    if (Task2_ID != NULL) {
        printf("ID = %d, Create Task2_ID is OK!\n", Task2_ID);
    }
}
SYS_RUN(kernel_sync_semaphore_example);

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

相关推荐
逝灮3 分钟前
【蓝桥杯——物联网设计与开发】拓展模块4 - 脉冲模块
stm32·单片机·嵌入式硬件·mcu·物联网·蓝桥杯·脉冲测量
云山工作室41 分钟前
基于单片机的电梯声控系统设计(论文+源码)
stm32·单片机·嵌入式硬件·毕业设计·毕设
南城花随雪。1 小时前
单片机:实现自动下载程序(附带源码)
单片机·嵌入式硬件
憧憬一下1 小时前
PCIe_Host驱动分析_设备枚举
arm开发·嵌入式硬件·嵌入式·pcie·linux驱动开发
MARIN_shen3 小时前
Marin说PCB之POC电路layout设计仿真案例---06
网络·单片机·嵌入式硬件·硬件工程·pcb工艺
Asa3193 小时前
STM32-按键扫描配置
stm32·单片机·嵌入式硬件
南城花随雪。4 小时前
单片机:实现驱动超声波(附带源码)
单片机·嵌入式硬件
yutian060612 小时前
Keil MDK下载程序后MCU自动重启设置
单片机·嵌入式硬件·keil
析木不会编程15 小时前
【小白51单片机专用教程】protues仿真独立按键控制LED
单片机·嵌入式硬件·51单片机
枯无穷肉19 小时前
stm32制作CAN适配器4--WinUsb的使用
stm32·单片机·嵌入式硬件