• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1# PWM<a name="title_PWM_des"></a>
2
3-   [概述](#section1_PWM_des)
4    -   [PwmConfig结构体](#section1.1_PWM_des)
5-   [接口说明](#section2_PWM_des)
6-   [使用指导](#section3_PWM_des)
7    -   [使用流程](#section3.1_PWM_des)
8    -   [获取PWM设备句柄](#section3.2_PWM_des)
9    -   [销毁PWM设备句柄](#section3.3_PWM_des)
10    -   [使能](#section3.4_PWM_des)
11    -   [禁用](#section3.5_PWM_des)
12    -   [设置PWM设备周期](#section3.6_PWM_des)
13    -   [设置PWM设备占空时间](#section3.7_PWM_des)
14    -   [设置PWM设备极性](#section3.8_PWM_des)
15    -   [设置PWM设备参数](#section3.9_PWM_des)
16    -   [获取PWM设备参数](#section3.10_PWM_des)
17
18-   [使用实例](#section3_PWM_des)
19
20## 概述<a name="section1_PWM_des"></a>
21
22- PWM是脉冲宽度调制(Pulse Width Modulation)的缩写,是一种对模拟信号电平进行数字编码,转换为脉冲的一种技术。常用于马达控制、背光亮度调节等。
23
24- PWM接口定义了操作PWM设备的通用方法集合,包括:
25  - PWM设备句柄获取和销毁。
26  - PWM周期、占空比、极性的设置。
27  - PWM使能和关闭。
28  - PWM配置信息的获取和设置
29
30### PwmConfig结构体<a name="section1.1_PWM_des"></a>
31
32**表1**  PwmConfig结构体介绍
33
34<a name="table1_PWM_des"></a>
35
36| 名称     | 描述                                                         |
37| -------- | ------------------------------------------------------------ |
38| duty     | 占空时间,以纳秒为单位                                       |
39| period   | PWM周期,以纳秒为单位                                        |
40| number   | 要生成的方波数。正值表示将生成指定数量的方波,<b>0</b>表示方波将不断产生 |
41| polarity | 极性:正极性/反极性                                          |
42| status   | 状态:启用状态/禁用状态                                      |
43
44## 接口说明<a name="section2_PWM_des"></a>
45
46**表2**  PWM设备API接口功能介绍
47
48<a name="table2_PWM_des"></a>
49
50<table border="0" cellpadding="0" cellspacing="0" width="800" style="border-collapse:
51 collapse;table-layout:fixed;width:700pt">
52  <td height="19" class="xl66" width="300" style="height:14.25pt;width:300pt">功能分类</td>
53  <td class="xl67" width="250" style="width:250pt">接口名</td>
54  <td class="xl67" width="300" style="width:300pt">描述</td>
55 </tr>
56 <tr height="19" style="height:14.25pt">
57  <td rowspan="2" height="38" class="xl65" style="height:28.5pt">PWM句柄操作</td>
58  <td>PwmOpen</td>
59  <td>获取PWM设备驱动句柄</td>
60 </tr>
61 <tr height="19" style="height:14.25pt">
62  <td height="19" style="height:14.25pt">PwmClose</td>
63  <td>释放PWM设备驱动句柄</td>
64 </tr>
65 <tr height="19" style="height:14.25pt">
66  <td rowspan="2" height="38" class="xl65" style="height:28.5pt">使能/禁用PWM</td>
67  <td>PwmEnable</td>
68  <td>使能PWM</td>
69 </tr>
70 <tr height="19" style="height:14.25pt">
71  <td height="19" style="height:14.25pt">PwmDisable</td>
72  <td>禁用PWM</td>
73 </tr>
74 <tr height="19" style="height:14.25pt">
75  <td rowspan="3" height="57" class="xl65" style="height:42.75pt">PWM配置操作</td>
76  <td>PwmSetPeriod</td>
77  <td>设置PWM周期</td>
78 </tr>
79 <tr height="19" style="height:14.25pt">
80  <td height="19" style="height:14.25pt">PwmSetDuty</td>
81  <td>设置PWM占空时间</td>
82 </tr>
83 <tr height="19" style="height:14.25pt">
84  <td height="19" style="height:14.25pt">PwmSetPolarity</td>
85  <td>设置PWM极性</td>
86 </tr>
87 <tr height="19" style="height:14.25pt">
88  <td rowspan="2" height="38" class="xl65" style="height:28.5pt">设置/获取PWM配置信息</td>
89  <td>PwmSetConfig</td>
90  <td>设置PWM设备参数</td>
91 </tr>
92 <tr height="19" style="height:14.25pt">
93  <td height="19" style="height:14.25pt">PwmGetConfig</td>
94  <td>获取PWM设备参数</td>
95 </tr>
96</tbody></table>
97
98
99
100> ![icon-note.gif](../public_sys-resources/icon-note.gif) **说明:**
101>PWM当前仅限内核态使用,不支持在用户态使用。
102
103## 使用指导<a name="section3_PWM_des"></a>
104
105### 使用流程<a name="section3.1_PWM_des"></a>
106
107在操作系统启动过程中,驱动管理模块根据配置文件加载PWM驱动,PWM驱动会检测PWM器件并初始化驱动。
108
109使用PWM设备的一般流程如[图1](#fig1_PWM_des)所示。
110
111**图 1**  PWM设备使用流程图<a name="fig1_PWM_des"></a>
112
113
114![](figures/PWM设备使用流程图.png)
115
116### 获取PWM设备句柄<a name="section3.2_PWM_des"></a>
117
118在操作PWM设备时,首先要调用PwmOpen获取PWM设备句柄,该函数会返回指定设备号的PWM设备句柄。
119
120```c
121DevHandle PwmOpen(uint32_t num);
122```
123
124**表3**  PwmOpen参数和返回值描述
125
126<a name="table3_PWM_des"></a>
127
128| 参数       | 参数描述                |
129| ---------- | ----------------------- |
130| num        | PWM设备编号             |
131| **返回值** | **返回值描述**          |
132| handle     | 获取成功返回PWM设备句柄 |
133| NULL       | 获取失败                |
134
135
136```c
137uint32_t num = 0;             /* PWM设备号 */
138DevHandle handle = NULL;
139
140/* 获取PWM设备句柄 */
141handle = PwmOpen(num);
142if (handle  == NULL) {
143    /* 错误处理 */
144}
145```
146
147### 销毁PWM设备句柄<a name="section3.3_PWM_des"></a>
148
149关闭PWM设备,系统释放对应的资源。
150
151```c
152void PwmClose(DevHandle handle);
153```
154
155**表4**  PwmClose参数描述
156
157<a name="table4_PWM_des"></a>
158
159| 参数   | 参数描述    |
160| ------ | ----------- |
161| handle | PWM设备句柄 |
162
163
164```c
165/* 销毁PWM设备句柄 */
166PwmClose(handle);
167```
168
169### 使能<a name="section3.4_PWM_des"></a>
170
171启用PWM设备。
172
173```c
174int32_t PwmEnable(DevHandle handle);
175```
176
177**表5** PwmEnable参数描述
178
179<a name="table5_PWM_des"></a>
180
181| 参数       | 参数描述       |
182| ---------- | -------------- |
183| handle     | PWM设备句柄    |
184| **返回值** | **返回值描述** |
185| 0          | 使能成功       |
186| 负数       | 使能失败       |
187
188```c
189int32_t ret;
190
191/*启用PWM设备*/
192ret = PwmEnable(handle);
193if (ret != 0) {
194	/*错误处理*/
195}
196```
197
198### 禁用<a name="section3.5_PWM_des"></a>
199
200禁用PWM设备。
201
202```c
203int32_t PwmDisable(DevHandle handle);
204```
205
206**表6** PwmDisable参数描述
207
208<a name="table6_PWM_des"></a>
209
210| 参数       | 参数描述       |
211| ---------- | -------------- |
212| handle     | PWM设备句柄    |
213| **返回值** | **返回值描述** |
214| 0          | 禁用成功       |
215| 负数       | 禁用失败       |
216
217```c
218int32_t ret;
219
220/*禁用PWM设备*/
221ret = PwmDisable(handle);
222if (ret != 0) {
223	/*错误处理*/
224}
225```
226
227### 设置PWM设备周期<a name="section3.6_PWM_des"></a>
228
229设置PWM设备周期。
230
231```c
232int32_t PwmSetPeriod(DevHandle handle, uint32_t period);
233```
234
235**表7** PwmSetPeriod参数描述
236
237<a name="table7_PWM_des"></a>
238
239| 参数       | 参数描述                 |
240| ---------- | ------------------------ |
241| handle     | PWM设备句柄              |
242| period     | 要设置的周期,单位为纳秒 |
243| **返回值** | **返回值描述**           |
244| 0          | 设置成功                 |
245| 负数       | 设置失败                 |
246
247```c
248int32_t ret;
249
250/*设置周期为50000000纳秒*/
251ret = PwmSetPeriod(handle, 50000000);
252if (ret != 0) {
253	/*错误处理*/
254}
255```
256### 设置PWM设备占空时间<a name="section3.7_PWM_des"></a>
257
258设置PWM设备占空时间。
259
260```c
261int32_t PwmSetDuty(DevHandle handle, uint32_t duty);
262```
263
264**表8** PwmSetDuty参数描述
265
266<a name="table8_PWM_des"></a>
267
268| 参数       | 参数描述                     |
269| ---------- | ---------------------------- |
270| handle     | PWM设备句柄                  |
271| duty       | 要设置的占空时间,单位为纳秒 |
272| **返回值** | **返回值描述**               |
273| 0          | 设置成功                     |
274| 负数       | 设置失败                     |
275
276```c
277int32_t ret;
278
279/*设置占空时间为25000000纳秒*/
280ret = PwmSetDuty(handle, 25000000);
281if (ret != 0) {
282	/*错误处理*/
283}
284```
285### 设置PWM设备极性<a name="section3.8_PWM_des"></a>
286
287设置PWM设备极性。
288
289```c
290int32_t PwmSetPolarity(DevHandle handle, uint8_t polarity);
291```
292
293**表9** PwmSetPolarity参数描述
294
295<a name="table9_PWM_des"></a>
296
297| 参数       | 参数描述            |
298| ---------- | ------------------- |
299| handle     | PWM设备句柄         |
300| polarity   | 要设置的极性,正/反 |
301| **返回值** | **返回值描述**      |
302| 0          | 设置成功            |
303| 负数       | 设置失败            |
304
305```c
306int32_t ret;
307
308/*设置极性为反*/
309ret = PwmSetPolarity(handle, PWM_INVERTED_POLARITY);
310if (ret != 0) {
311	/*错误处理*/
312}
313```
314
315
316### 设置PWM设备参数<a name="section3.9_PWM_des"></a>
317
318设置PWM设备参数。
319
320```c
321int32_t PwmSetConfig(DevHandle handle, struct PwmConfig *config);
322```
323
324**表10** PwmSetConfig参数描述
325
326<a name="table10_PWM_des"></a>
327
328| 参数       | 参数描述       |
329| ---------- | -------------- |
330| handle     | PWM设备句柄    |
331| *config    | 参数指针       |
332| **返回值** | **返回值描述** |
333| 0          | 设置成功       |
334| 负数       | 设置失败       |
335
336```c
337int32_t ret;
338struct PwmConfig pcfg;
339pcfg.duty = 25000000;					/*占空时间为25000000纳秒*/
340pcfg.period = 50000000;					/*周期为50000000纳秒*/
341pcfg.number = 0;						/*不断产生方波*/
342pcfg.polarity = PWM_INVERTED_POLARITY;	/*极性为反*/
343pcfg.status = PWM_ENABLE_STATUS;		/*运行状态为启用*/
344
345/*设置PWM设备参数*/
346ret = PwmSetConfig(handle, &pcfg);
347if (ret != 0) {
348	/*错误处理*/
349}
350```
351
352### 获取PWM设备参数<a name="section3.10_PWM_des"></a>
353
354获取PWM设备参数。
355
356```c
357int32_t PwmGetConfig(DevHandle handle, struct PwmConfig *config);
358```
359
360**表11** PwmGetConfig参数描述
361
362<a name="table11_PWM_des"></a>
363
364| 参数       | 参数描述       |
365| ---------- | -------------- |
366| handle     | PWM设备句柄    |
367| *config    | 参数指针       |
368| **返回值** | **返回值描述** |
369| 0          | 获取成功       |
370| 负数       | 获取失败       |
371
372```c
373int32_t ret;
374struct PwmConfig pcfg;
375
376/*获取PWM设备参数*/
377ret = PwmGetConfig(handle, &pcfg);
378if (ret != 0) {
379	/*错误处理*/
380}
381```
382
383## 使用实例<a name="section4_PWM_des"></a>
384
385PWM设备完整的使用示例如下所示,首先获取PWM设备句柄,然后设置设备周期、占空时间、极性,获取设备参数。使能,设置设备参数,禁用,最后销毁PWM设备句柄。
386
387```
388void PwmTestSample(void)
389{
390    int32_t ret;
391    uint32_t num;
392    DevHandle handle = NULL;
393
394    struct PwmConfig pcfg;
395    pcfg.duty = 20000000;					/*占空时间为20000000纳秒*/
396    pcfg.period = 40000000;					/*周期为40000000纳秒*/
397    pcfg.number = 100;						/*生成100个方波*/
398    pcfg.polarity = PWM_NORMAL_POLARITY;	/*极性为正*/
399    pcfg.status = PWM_ENABLE_STATUS;		/*运行状态为启用*/
400
401    /* PWM设备编号,要填写实际平台上的编号 */
402    num = 1;
403
404    /* 获取PWM设备句柄 */
405    handle = PwmOpen(num);
406    if (handle == NULL) {
407        HDF_LOGE("PwmOpen: failed!\n");
408        return;
409    }
410
411    /*设置周期为50000000纳秒*/
412    ret = PwmSetPeriod(handle, 50000000);
413    if (ret != 0) {
414        HDF_LOGE("PwmSetPeriod: failed, ret %d\n", ret);
415        goto _ERR;
416    }
417
418    /*设置占空时间为25000000纳秒*/
419    ret = PwmSetDuty(handle, 25000000);
420    if (ret != 0) {
421        HDF_LOGE("PwmSetDuty: failed, ret %d\n", ret);
422        goto _ERR;
423    }
424
425    /*设置极性为反*/
426    ret = PwmSetPolarity(handle, PWM_INVERTED_POLARITY);
427    if (ret != 0) {
428        HDF_LOGE("PwmSetPolarity: failed, ret %d\n", ret);
429        goto _ERR;
430    }
431
432    /*获取PWM设备参数*/
433    ret = PwmGetConfig(handle, &pcfg);
434    if (ret != 0) {
435        HDF_LOGE("PwmGetConfig: failed, ret %d\n", ret);
436        goto _ERR;
437    }
438
439    /*启用PWM设备*/
440    ret = PwmEnable(handle);
441    if (ret != 0) {
442	    HDF_LOGE("PwmEnable: failed, ret %d\n", ret);
443        goto _ERR;
444    }
445
446    /*设置PWM设备参数*/
447    ret = PwmSetConfig(handle, &pcfg);
448    if (ret != 0) {
449        HDF_LOGE("PwmSetConfig: failed, ret %d\n", ret);
450        goto _ERR;
451    }
452
453    /*禁用PWM设备*/
454    ret = PwmDisable(handle);
455    if (ret != 0) {
456        HDF_LOGE("PwmDisable: failed, ret %d\n", ret);
457        goto _ERR;
458    }
459
460_ERR:
461    /* 销毁PWM设备句柄 */
462    PwmClose(handle);
463}
464```
465