driver-platform-pwm-des.md 12.2 KB
Newer Older
A
annie_wangli 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116
# PWM<a name="title_PWM_des"></a>

## Overview <a name="section1_PWM_des"></a>

Pulse width modulation (PWM) is a method used to digitally encode analog signal levels and convert them into pulses. It can be used for motor control and backlight brightness adjustment.

The PWM APIs provide a set of functions for operating a PWM device, including those for:
- Obtaining and releasing a PWM device handle
- Setting the PWM period, signal ON-state time, and polarity
- Enabling and disabling a PWM device
- Obtaining and setting PWM parameters

### PwmConfig Structure<a name="section1.1_PWM_des"></a>

**Table 1** PwmConfig structure

<a name="table1_PWM_des"></a>

| Parameter| Description|
| -------- | ------------------------------------------------------------ |
| duty     | Time that a signal is in the ON state, in ns.|
| period   | Time for a signal to complete an on-and-off cycle, in ns.|
| number   | Number of square waves to generate. A positive value indicates the number of square waves to generate. The value <b>0</b> means to generate square waves repeatedly.|
| polarity | PWM signal polarity, which can be **PWM\_NORMAL\_POLARITY** or **PWM\_INVERTED\_POLARITY**.|
| status   | PWM device status, which can be enabled or disabled.|

## Available APIs<a name="section2_PWM_des"></a>

**Table 2** PWM device APIs

<a name="table2_PWM_des"></a>

<table border="0" cellpadding="0" cellspacing="0" width="800" style="border-collapse:
 collapse;table-layout:fixed;width:700pt">
 <colgroup><col class="xl65" width="300" style="mso-width-source:userset;mso-width-alt:13728;
 width:300pt">
 <col width="250" style="mso-width-source:userset;mso-width-alt:13216;width:250pt">
 <col width="300" style="mso-width-source:userset;mso-width-alt:28352;width:300pt">
 </colgroup><tbody><tr height="19" style="height:14.25pt">
  <td height="19" class="xl66" width="300" style="height:14.25pt;width:300pt">Category</td>
  <td class="xl67" width="250" style="width:250pt">API</td>
  <td class="xl67" width="300" style="width:300pt">Description</td>
 </tr>
 <tr height="19" style="height:14.25pt">
  <td rowspan="2" height="38" class="xl65" style="height:28.5pt">Operating PWM handles</td>
  <td>PwmOpen</td>
  <td>Opens the handle of a PWM device.</td>
 </tr>
 <tr height="19" style="height:14.25pt">
  <td height="19" style="height:14.25pt">PwmClose</td>
  <td>Closes the handle of a PWM device.</td>
 </tr>
 <tr height="19" style="height:14.25pt">
  <td rowspan="2" height="38" class="xl65" style="height:28.5pt">Enabling or disabling PWM</td>
  <td>PwmEnable</td>
  <td>Enables a PWM device.</td>
 </tr>
 <tr height="19" style="height:14.25pt">
  <td height="19" style="height:14.25pt">PwmDisable</td>
  <td>Disables a PWM device.</td>
 </tr>
 <tr height="19" style="height:14.25pt">
  <td rowspan="3" height="57" class="xl65" style="height:42.75pt">Performing PWM configuration</td>
  <td>PwmSetPeriod</td>
  <td>Sets the PWM period.</td>
 </tr>
 <tr height="19" style="height:14.25pt">
  <td height="19" style="height:14.25pt">PwmSetDuty</td>
  <td>Sets the signal ON-state time.</td>
 </tr>
 <tr height="19" style="height:14.25pt">
  <td height="19" style="height:14.25pt">PwmSetPolarity</td>
  <td>Sets the PWM signal polarity.</td>
 </tr>
 <tr height="19" style="height:14.25pt">
  <td rowspan="2" height="38" class="xl65" style="height:28.5pt">Setting or obtaining the PWM configuration</td>
  <td>PwmSetConfig</td>
  <td>Sets PWM device parameters.</td>
 </tr>
 <tr height="19" style="height:14.25pt">
  <td height="19" style="height:14.25pt">PwmGetConfig</td>
  <td>Obtains PWM device parameters.</td>
 </tr>
 <!--[if supportMisalignedColumns]-->
 <tr height="0" style="display:none">
  <td width="429" style="width:322pt"></td>
  <td width="413" style="width:310pt"></td>
  <td width="886" style="width:665pt"></td>
 </tr>
 <!--[endif]-->
</tbody></table>



>![](W:\doc\docs\en\device-dev\public_sys-resources\icon-note.gif) **NOTE:**
>The PWM module can be used in kernel mode but not in user mode.

## Usage Guidelines<a name="section3_PWM_des"></a>

### How to Use<a name="section3.1_PWM_des"></a>

During the OS startup process, the driver management module loads the PWM driver based on the configuration file. Then, the PWM driver detects the PWM device and initializes the driver.

[Figure 1](#fig1_PWM_des) shows the general process of using the PWM module.

**Figure 1** Process of using the PWM module<a name="fig1_PWM_des"></a>


![](figures/process-of-using-PWM.png)

### Opening a PWM Device Handle<a name="section3.2_PWM_des"></a>

Before performing operations on a PWM device, call **PwmOpen** to open the device handle.

```c
DevHandle PwmOpen(uint32_t num);
D
duangavin123 已提交
117
```
A
annie_wangli 已提交
118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133

**Table 3** PwmOpen

<a name="table3_PWM_des"></a>

| Parameter| Description|
| ---------- | ----------------------- |
| num        | PWM device number.|
| **Return Value** | **Description**|
| handle     | Handle of the PWM device obtained.|
| NULL       | The operation fails.|


```c
uint32_t num = 0;             /* PWM device number. */
DevHandle handle = NULL;
D
duangavin123 已提交
134 135

/* Obtain the PWM device handle. */
A
annie_wangli 已提交
136 137 138
handle = PwmOpen(num);
if (handle  == NULL) {
    /* Error handling. */
D
duangavin123 已提交
139 140 141
}
```

A
annie_wangli 已提交
142 143 144
### Closing a PWM Device Handle<a name="section3.3_PWM_des"></a>

Close a PWM device to release resources.
D
duangavin123 已提交
145

A
annie_wangli 已提交
146 147
```c
void PwmClose(DevHandle handle);
D
duangavin123 已提交
148
```
A
annie_wangli 已提交
149 150 151 152 153 154 155 156 157 158 159 160 161

**Table 4** PwmClose

<a name="table4_PWM_des"></a>

| Parameter| Description|
| ------ | ----------- |
| handle | PWM device handle to close.|


```c
/* Close a PWM device handle. */
PwmClose(handle);
D
duangavin123 已提交
162 163
```

A
annie_wangli 已提交
164
### Enabling a PWM Device<a name="section3.4_PWM_des"></a>
D
duangavin123 已提交
165

A
annie_wangli 已提交
166 167 168 169
Enable a PWM device.

```c
int32_t PwmEnable(DevHandle handle);
D
duangavin123 已提交
170
```
A
annie_wangli 已提交
171 172 173 174 175 176 177 178 179 180 181 182 183

**Table 5** PwmEnable

<a name="table5_PWM_des"></a>

| Parameter| Description|
| ---------- | -------------- |
| handle     | PWM device handle.|
| **Return Value** | **Description** |
| 0          | The operation is successful.|
| Negative number| The operation fails.|

```c
D
duangavin123 已提交
184
int32_t ret;
A
annie_wangli 已提交
185 186 187

/* Enable a PWM device. */
ret = PwmEnable(handle);
D
duangavin123 已提交
188
if (ret != 0) {
A
annie_wangli 已提交
189
	/* Error handling. */
D
duangavin123 已提交
190 191 192
}
```

A
annie_wangli 已提交
193 194 195
### Disabling a PWM Device<a name="section3.5_PWM_des"></a>

Disable a PWM device.
D
duangavin123 已提交
196

A
annie_wangli 已提交
197 198
```c
int32_t PwmDisable(DevHandle handle);
D
duangavin123 已提交
199
```
A
annie_wangli 已提交
200 201 202 203 204 205 206 207 208 209 210 211 212

**Table 6** PwmDisable

<a name="table6_PWM_des"></a>

| Parameter| Description|
| ---------- | -------------- |
| handle     | PWM device handle.|
| **Return Value** | **Description** |
| 0          | The operation is successful.|
| Negative number| The operation fails.|

```c
D
duangavin123 已提交
213
int32_t ret;
A
annie_wangli 已提交
214 215 216

/* Disable a PWM device. */
ret = PwmDisable(handle);
D
duangavin123 已提交
217
if (ret != 0) {
A
annie_wangli 已提交
218
	/* Error handling. */
D
duangavin123 已提交
219 220 221
}
```

A
annie_wangli 已提交
222 223 224
### Setting the PWM Period<a name="section3.6_PWM_des"></a>

Set the PWM period.
D
duangavin123 已提交
225

A
annie_wangli 已提交
226 227
```c
int32_t PwmSetPeriod(DevHandle handle, uint32_t period);
D
duangavin123 已提交
228
```
A
annie_wangli 已提交
229 230 231 232 233 234 235 236 237 238 239 240 241 242

**Table 7** PwmSetPeriod

<a name="table7_PWM_des"></a>

| Parameter| Description|
| ---------- | ------------------------ |
| handle     | PWM device handle.|
| period     | PWM period to set, in ns.|
| **Return Value**| **Description**|
| 0          | The operation is successful.|
| Negative number| The operation fails.|

```c
D
duangavin123 已提交
243
int32_t ret;
A
annie_wangli 已提交
244 245 246

/* Set the PWM period to 50000000 ns.*/
ret = PwmSetPeriod(handle, 50000000);
D
duangavin123 已提交
247
if (ret != 0) {
A
annie_wangli 已提交
248
	/* Error handling. */
D
duangavin123 已提交
249 250
}
```
A
annie_wangli 已提交
251
### Setting the PWM Signal ON-State Time<a name="section3.7_PWM_des"></a>
D
duangavin123 已提交
252

A
annie_wangli 已提交
253
Set the time that the PWM signal is in the ON state.
D
duangavin123 已提交
254

A
annie_wangli 已提交
255 256
```c
int32_t PwmSetDuty(DevHandle handle, uint32_t duty);
D
duangavin123 已提交
257
```
A
annie_wangli 已提交
258 259 260 261 262 263 264 265 266 267 268 269 270 271

**Table 8** PwmSetDuty

<a name="table8_PWM_des"></a>

| Parameter| Description|
| ---------- | ---------------------------- |
| handle     | PWM device handle.|
| duty       | Time that the signal is in the ON state, in ns.|
| **Return Value**| **Description**|
| 0          | The operation is successful.|
| Negative number| The operation fails.|

```c
D
duangavin123 已提交
272
int32_t ret;
A
annie_wangli 已提交
273 274 275

/* Set the signal ON-state time to 25000000 ns. */
ret = PwmSetDuty(handle, 25000000);
D
duangavin123 已提交
276
if (ret != 0) {
A
annie_wangli 已提交
277
	/* Error handling. */
D
duangavin123 已提交
278 279
}
```
A
annie_wangli 已提交
280
### Setting the PWM Polarity<a name="section3.8_PWM_des"></a>
D
duangavin123 已提交
281

A
annie_wangli 已提交
282
Set the signal polarity for a PWM device.
D
duangavin123 已提交
283

A
annie_wangli 已提交
284 285
```c
int32_t PwmSetPolarity(DevHandle handle, uint8_t polarity);
D
duangavin123 已提交
286
```
A
annie_wangli 已提交
287 288 289 290 291 292 293 294 295 296 297 298 299 300

**Table 9** PwmSetPolarity

<a name="table9_PWM_des"></a>

| Parameter| Description|
| ---------- | ------------------- |
| handle     | PWM device handle.|
| polarity   | Polarity to set, which can be **PWM\_NORMAL\_POLARITY** or **PWM\_INVERTED\_POLARITY**.|
| **Return Value**| **Description**|
| 0          | The operation is successful.|
| Negative number| The operation fails.|

```c
D
duangavin123 已提交
301
int32_t ret;
A
annie_wangli 已提交
302 303 304

/* Set the PWM polarity to PWM_INVERTED_POLARITY. */
ret = PwmSetPolarity(handle, PWM_INVERTED_POLARITY);
D
duangavin123 已提交
305
if (ret != 0) {
A
annie_wangli 已提交
306
	/* Error handling. */
D
duangavin123 已提交
307 308 309 310
}
```


A
annie_wangli 已提交
311 312 313 314 315 316
### Setting PWM Device Parameters<a name="section3.9_PWM_des"></a>

Set PWM device parameters.

```c
int32_t PwmSetConfig(DevHandle handle, struct PwmConfig *config);
D
duangavin123 已提交
317
```
A
annie_wangli 已提交
318 319 320 321 322 323 324 325 326 327 328 329 330 331

**Table 10** PwmSetConfig

<a name="table10_PWM_des"></a>

| Parameter| Description|
| ---------- | -------------- |
| handle     | PWM device handle.|
| *config    | Pointer to PWM parameters.|
| **Return Value**| **Description**|
| 0          | The operation is successful.|
| Negative number| The operation fails.|

```c
D
duangavin123 已提交
332
int32_t ret;
A
annie_wangli 已提交
333 334 335 336 337 338 339 340 341
struct PwmConfig pcfg;
pcfg.duty = 25000000;					/* Set the signal ON-state time to 25000000 ns. */
pcfg.period = 50000000;					/* Set the PWM period to 50000000 ns. */
pcfg.number = 0;						/* Generate square waves repeatedly. */
pcfg.polarity = PWM_INVERTED_POLARITY;	/* Set the PWM polarity to PWM_INVERTED_POLARITY. */
pcfg.status = PWM_ENABLE_STATUS;		/* Set the running status to Enabled. */

/* Set PWM device parameters. */
ret = PwmSetConfig(handle, &pcfg);
D
duangavin123 已提交
342
if (ret != 0) {
A
annie_wangli 已提交
343
	/* Error handling. */
D
duangavin123 已提交
344 345 346
}
```

A
annie_wangli 已提交
347
### Obtaining PWM Device Parameters<a name="section3.10_PWM_des"></a>
D
duangavin123 已提交
348

A
annie_wangli 已提交
349
Obtain PWM device parameters.
D
duangavin123 已提交
350

A
annie_wangli 已提交
351 352 353
```c
int32_t PwmGetConfig(DevHandle handle, struct PwmConfig *config);
```
D
duangavin123 已提交
354

A
annie_wangli 已提交
355
**Table 11** PwmGetConfig
D
duangavin123 已提交
356

A
annie_wangli 已提交
357
<a name="table11_PWM_des"></a>
D
duangavin123 已提交
358

A
annie_wangli 已提交
359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375
| Parameter| Description|
| ---------- | -------------- |
| handle     | PWM device handle.|
| *config    | Pointer to PWM parameters.|
| **Return Value**| **Description**|
| 0          | The operation is successful.|
| Negative number| The operation fails.|

```c
int32_t ret;
struct PwmConfig pcfg;

/* Obtain PWM device parameters. */
ret = PwmGetConfig(handle, &pcfg);
if (ret != 0) {
	/* Error handling. */
}
D
duangavin123 已提交
376 377
```

A
annie_wangli 已提交
378
## Usage Example<a name="section4_PWM_des"></a>
D
duangavin123 已提交
379

A
annie_wangli 已提交
380
The following example shows how to use the APIs to implement a PWM driver and manage the PWM device.
D
duangavin123 已提交
381 382 383 384 385

```
void PwmTestSample(void)
{
    int32_t ret;
A
annie_wangli 已提交
386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402
    uint32_t num;
    DevHandle handle = NULL;

    struct PwmConfig pcfg;
    pcfg.duty = 20000000;					/* Set the signal ON-state time to 20000000 ns. */
    pcfg.period = 40000000;					/* Set the PWM period to 40000000 ns. */
    pcfg.number = 100;						/* Generate 100 square waves. */
    pcfg.polarity = PWM_NORMAL_POLARITY;	/* Set the polarity to PWM_NORMAL_POLARITY. */
    pcfg.status = PWM_ENABLE_STATUS;		/* Set the running status to Enabled. */
    
    /* Enter the PWM device number. */
    num = 1; 

    /* Open the PWM device handle. */
    handle = PwmOpen(num);
    if (handle == NULL) {
        HDF_LOGE("PwmOpen: failed!\n");
D
duangavin123 已提交
403 404
        return;
    }
A
annie_wangli 已提交
405 406 407

    /* Set the PWM period to 50000000 ns.*/
    ret = PwmSetPeriod(handle, 50000000);
D
duangavin123 已提交
408
    if (ret != 0) {
A
annie_wangli 已提交
409 410
        HDF_LOGE("PwmSetPeriod: failed, ret %d\n", ret);
        goto _ERR;
D
duangavin123 已提交
411
    }
A
annie_wangli 已提交
412 413 414

    /* Set the signal ON-state time to 25000000 ns. */
    ret = PwmSetDuty(handle, 25000000);
D
duangavin123 已提交
415
    if (ret != 0) {
A
annie_wangli 已提交
416 417 418 419 420 421 422 423 424
        HDF_LOGE("PwmSetDuty: failed, ret %d\n", ret);
        goto _ERR;
    }

    /* Set the PWM polarity to PWM_INVERTED_POLARITY. */
    ret = PwmSetPolarity(handle, PWM_INVERTED_POLARITY);
    if (ret != 0) {
        HDF_LOGE("PwmSetPolarity: failed, ret %d\n", ret);
        goto _ERR;
D
duangavin123 已提交
425
    }
A
annie_wangli 已提交
426 427 428 429 430 431 432 433
    
    /* Obtain PWM device parameters. */
    ret = PwmGetConfig(handle, &pcfg);
    if (ret != 0) {
        HDF_LOGE("PwmGetConfig: failed, ret %d\n", ret);
        goto _ERR;
    }
    
D
duangavin123 已提交
434
    /* Enable the PWM device. */
A
annie_wangli 已提交
435
    ret = PwmEnable(handle);
D
duangavin123 已提交
436
    if (ret != 0) {
A
annie_wangli 已提交
437 438
	    HDF_LOGE("PwmEnable: failed, ret %d\n", ret);
        goto _ERR;
D
duangavin123 已提交
439
    }
A
annie_wangli 已提交
440 441 442 443 444 445 446 447

    /* Set PWM device parameters. */
    ret = PwmSetConfig(handle, &pcfg);
    if (ret != 0) {
        HDF_LOGE("PwmSetConfig: failed, ret %d\n", ret);
        goto _ERR;
    }

D
duangavin123 已提交
448
    /* Disable the PWM device. */
A
annie_wangli 已提交
449
    ret = PwmDisable(handle);
D
duangavin123 已提交
450 451
    if (ret != 0) {
        HDF_LOGE("PwmDisable: failed, ret %d\n", ret);
A
annie_wangli 已提交
452
        goto _ERR;
D
duangavin123 已提交
453
    }
A
annie_wangli 已提交
454 455 456 457
    
_ERR:
    /* Close the PWM device handle. */
    PwmClose(handle); 
D
duangavin123 已提交
458 459
}
```