README.md
1# 润和星闪派物联网开发套件--信号量(Semaphore)
2
3
4
5[润和星闪派物联网开发套件](https://item.taobao.com/item.htm?abbucket=16&id=816685710481&ns=1&priceTId=214783b117346662457694855ed644&skuId=5533042544092&spm=a21n57.sem.item.49.46a639031zWytE&utparam=%7B%22aplus_abtest%22%3A%22b28048df8f009463834be6bdac2a3713%22%7D&xxc=taobaoSearch) 基于海思WS63E解决方案的一套软硬件组合的综合性开发套件。
6
7
8
9## 一、Semaphore API
10
11| API名称 | 说明 |
12| ------------------- | ------------------------------------------------------------ |
13| osSemaphoreNew | 创建并初始化一个信号量 |
14| osSemaphoreGetName | 获取一个信号量的名字 |
15| osSemaphoreAcquire | 获取一个信号量的令牌,若获取不到,则会超时返回 |
16| osSemaphoreRelease | 释放一个信号量的令牌,但是令牌的数量不超过初始定义的的令牌数 |
17| osSemaphoreGetCount | 获取当前的信号量令牌数 |
18| osSemaphoreDelete | 删除一个信号量 |
19
20## 二、代码分析
21
22`osSemaphoreAcquire`获取共享资源的访问权限,若获取失败,则等待;访问成功后,可以通过`osSemaphoreRelease`释放对共享资源的访问
23
24本样例为经典的消费者与生产者问题,需要确保仓库满时,生产者需要进入等待状态,产品消费完时,消费者需要进入等待状态
25
26```c
27void producer_thread(void *arg) {
28 (void)arg;
29 empty_id = osSemaphoreNew(BUFFER_SIZE, BUFFER_SIZE, NULL);
30 filled_id = osSemaphoreNew(BUFFER_SIZE, 0U, NULL);
31 while(1) {
32 osSemaphoreAcquire(empty_id, osWaitForever);
33 product_number++;
34 printf("[Semp Test]%s produces a product, now product number: %d.\r\n", osThreadGetName(osThreadGetId()), product_number);
35 osDelay(4);
36 osSemaphoreRelease(filled_id);
37 }
38}
39
40void consumer_thread(void *arg) {
41 (void)arg;
42 while(1){
43 osSemaphoreAcquire(filled_id, osWaitForever);
44 product_number--;
45 printf("[Semp Test]%s consumes a product, now product number: %d.\r\n", osThreadGetName(osThreadGetId()), product_number);
46 osDelay(3);
47 osSemaphoreRelease(empty_id);
48 }
49}
50```
51
52由于消费产品的速度大于生产速度,所以定义了三个生产者,两个消费者
53
54```c
55void rtosv2_semp_main(void *arg) {
56 (void)arg;
57 empty_id = osSemaphoreNew(BUFFER_SIZE, BUFFER_SIZE, NULL);
58 filled_id = osSemaphoreNew(BUFFER_SIZE, 0U, NULL);
59
60 osThreadId_t ptid1 = newThread("producer1", producer_thread, NULL);
61 osThreadId_t ptid2 = newThread("producer2", producer_thread, NULL);
62 osThreadId_t ptid3 = newThread("producer3", producer_thread, NULL);
63 osThreadId_t ctid1 = newThread("consumer1", consumer_thread, NULL);
64 osThreadId_t ctid2 = newThread("consumer2", consumer_thread, NULL);
65
66 osDelay(50);
67
68 osThreadTerminate(ptid1);
69 osThreadTerminate(ptid2);
70 osThreadTerminate(ptid3);
71 osThreadTerminate(ctid1);
72 osThreadTerminate(ctid2);
73
74 osSemaphoreDelete(empty_id);
75 osSemaphoreDelete(filled_id);
76}
77```
78
79
80
81## 三、如何编译
82
831. 将00_thread目录复制到openharmony源码的`applications\sample\wifi-iot\app`目录下,
842. 修改openharmony源码的`applications\sample\wifi-iot\app\BUILD.gn`文件,将其中的 `features` 改为:
85
86```
87 features = [
88 ...
89 "04_semaphore:semp_demo",
90 ...
91 ]
92```
933. 在`device\soc\hisilicon\ws63v100\sdk\build\config\target_config\ws63\config.py`文件中,找到`'ws63-liteos-app'`部分,在其`'ram_component'`中,添加以下代码:
94```
95"semp_demo"
96```
97
984. 在`device\soc\hisilicon\ws63v100\sdk\libs_url\ws63\cmake\ohos.cmake`文件中,找到`"ws63-liteos-app"`部分,在其`set(COMPONENT_LIST`部分,添加以下代码:
99```
100"semp_demo"
101```
1025. 在openharmony sdk根目录目录执行:`rm -rf out && hb set -p nearlink_dk_3863 && hb build -f`
103
104## 四、运行结果
105
106截取部分运行结果
107
108```
109[Semp Test]osThreadNew(producer1) success, thread id: 10580248.
110[Semp Test]osThreadNew(producer2) success, thread id: 10580356.
111[Semp Test]osThreadNew(producer3) success, thread id: 10580464.
112[Semp Test]producer1 produces a product, now product number: 1.
113[Semp Test]producer2 produces a product, now product number: 2.
114[Semp Test]producer3 produces a product, now product number: 3.
115[Semp Test]osThreadNew(consumer1) success, thread id: 10580572.
116[Semp Test]osThreadNew(consumer2) success, thread id: 10580680.
117```
118
119### 【套件支持】
120
121##### 1. 套件购买 https://item.taobao.com/item.htm?abbucket=16&id=816685710481&ns=1&priceTId=214783b117346662457694855ed644&skuId=5533042544092&spm=a21n57.sem.item.49.46a639031zWytE&utparam=%7B%22aplus_abtest%22%3A%22b28048df8f009463834be6bdac2a3713%22%7D&xxc=taobaoSearch
122
123##### 2. 技术资料
124
125- Gitee码云网站(使用说明书、规格说明书、OpenHarmony开发案例等) **https://gitee.com/hihopeorg_group/near-link**
126- fbb_ws63代码仓(SDK包、技术文档下载)**https://gitee.com/HiSpark/fbb_ws63**
127
128##### 3. 互动交流
129- 海思社区星闪专区-论坛 **https://developer.hisilicon.com/forum/0133146886267870001**
130
131