driver-platform-pwm-des.md 11.7 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
# 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">
35
<tbody><tr height="19" style="height:14.25pt">
A
annie_wangli 已提交
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
  <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>
</tbody></table>



84
>![](../public_sys-resources/icon-note.gif) **NOTE:**
A
annie_wangli 已提交
85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105
>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 已提交
106
```
A
annie_wangli 已提交
107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122

**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 已提交
123 124

/* Obtain the PWM device handle. */
A
annie_wangli 已提交
125 126 127
handle = PwmOpen(num);
if (handle  == NULL) {
    /* Error handling. */
D
duangavin123 已提交
128 129 130
}
```

A
annie_wangli 已提交
131 132 133
### Closing a PWM Device Handle<a name="section3.3_PWM_des"></a>

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

A
annie_wangli 已提交
135 136
```c
void PwmClose(DevHandle handle);
D
duangavin123 已提交
137
```
A
annie_wangli 已提交
138 139 140 141 142 143 144 145 146 147 148 149 150

**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 已提交
151 152
```

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

A
annie_wangli 已提交
155 156 157 158
Enable a PWM device.

```c
int32_t PwmEnable(DevHandle handle);
D
duangavin123 已提交
159
```
A
annie_wangli 已提交
160 161 162 163 164 165 166 167 168 169 170 171 172

**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 已提交
173
int32_t ret;
A
annie_wangli 已提交
174 175 176

/* Enable a PWM device. */
ret = PwmEnable(handle);
D
duangavin123 已提交
177
if (ret != 0) {
A
annie_wangli 已提交
178
	/* Error handling. */
D
duangavin123 已提交
179 180 181
}
```

A
annie_wangli 已提交
182 183 184
### Disabling a PWM Device<a name="section3.5_PWM_des"></a>

Disable a PWM device.
D
duangavin123 已提交
185

A
annie_wangli 已提交
186 187
```c
int32_t PwmDisable(DevHandle handle);
D
duangavin123 已提交
188
```
A
annie_wangli 已提交
189 190 191 192 193 194 195 196 197 198 199 200 201

**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 已提交
202
int32_t ret;
A
annie_wangli 已提交
203 204 205

/* Disable a PWM device. */
ret = PwmDisable(handle);
D
duangavin123 已提交
206
if (ret != 0) {
A
annie_wangli 已提交
207
	/* Error handling. */
D
duangavin123 已提交
208 209 210
}
```

A
annie_wangli 已提交
211 212 213
### Setting the PWM Period<a name="section3.6_PWM_des"></a>

Set the PWM period.
D
duangavin123 已提交
214

A
annie_wangli 已提交
215 216
```c
int32_t PwmSetPeriod(DevHandle handle, uint32_t period);
D
duangavin123 已提交
217
```
A
annie_wangli 已提交
218 219 220 221 222 223 224 225 226 227 228 229 230 231

**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 已提交
232
int32_t ret;
A
annie_wangli 已提交
233 234 235

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

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

A
annie_wangli 已提交
244 245
```c
int32_t PwmSetDuty(DevHandle handle, uint32_t duty);
D
duangavin123 已提交
246
```
A
annie_wangli 已提交
247 248 249 250 251 252 253 254 255 256 257 258 259 260

**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 已提交
261
int32_t ret;
A
annie_wangli 已提交
262 263 264

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

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

A
annie_wangli 已提交
273 274
```c
int32_t PwmSetPolarity(DevHandle handle, uint8_t polarity);
D
duangavin123 已提交
275
```
A
annie_wangli 已提交
276 277 278 279 280 281 282 283 284 285 286 287 288 289

**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 已提交
290
int32_t ret;
A
annie_wangli 已提交
291 292 293

/* Set the PWM polarity to PWM_INVERTED_POLARITY. */
ret = PwmSetPolarity(handle, PWM_INVERTED_POLARITY);
D
duangavin123 已提交
294
if (ret != 0) {
A
annie_wangli 已提交
295
	/* Error handling. */
D
duangavin123 已提交
296 297 298 299
}
```


A
annie_wangli 已提交
300 301 302 303 304 305
### 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 已提交
306
```
A
annie_wangli 已提交
307 308 309 310 311 312 313 314 315 316 317 318 319 320

**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 已提交
321
int32_t ret;
A
annie_wangli 已提交
322 323 324 325 326 327 328 329 330
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 已提交
331
if (ret != 0) {
A
annie_wangli 已提交
332
	/* Error handling. */
D
duangavin123 已提交
333 334 335
}
```

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

A
annie_wangli 已提交
338
Obtain PWM device parameters.
D
duangavin123 已提交
339

A
annie_wangli 已提交
340 341 342
```c
int32_t PwmGetConfig(DevHandle handle, struct PwmConfig *config);
```
D
duangavin123 已提交
343

A
annie_wangli 已提交
344
**Table 11** PwmGetConfig
D
duangavin123 已提交
345

A
annie_wangli 已提交
346
<a name="table11_PWM_des"></a>
D
duangavin123 已提交
347

A
annie_wangli 已提交
348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364
| 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 已提交
365 366
```

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

A
annie_wangli 已提交
369
The following example shows how to use the APIs to implement a PWM driver and manage the PWM device.
D
duangavin123 已提交
370 371 372 373 374

```
void PwmTestSample(void)
{
    int32_t ret;
A
annie_wangli 已提交
375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391
    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 已提交
392 393
        return;
    }
A
annie_wangli 已提交
394 395 396

    /* Set the PWM period to 50000000 ns.*/
    ret = PwmSetPeriod(handle, 50000000);
D
duangavin123 已提交
397
    if (ret != 0) {
A
annie_wangli 已提交
398 399
        HDF_LOGE("PwmSetPeriod: failed, ret %d\n", ret);
        goto _ERR;
D
duangavin123 已提交
400
    }
A
annie_wangli 已提交
401 402 403

    /* Set the signal ON-state time to 25000000 ns. */
    ret = PwmSetDuty(handle, 25000000);
D
duangavin123 已提交
404
    if (ret != 0) {
A
annie_wangli 已提交
405 406 407 408 409 410 411 412 413
        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 已提交
414
    }
A
annie_wangli 已提交
415 416 417 418 419 420 421 422
    
    /* Obtain PWM device parameters. */
    ret = PwmGetConfig(handle, &pcfg);
    if (ret != 0) {
        HDF_LOGE("PwmGetConfig: failed, ret %d\n", ret);
        goto _ERR;
    }
    
D
duangavin123 已提交
423
    /* Enable the PWM device. */
A
annie_wangli 已提交
424
    ret = PwmEnable(handle);
D
duangavin123 已提交
425
    if (ret != 0) {
A
annie_wangli 已提交
426 427
	    HDF_LOGE("PwmEnable: failed, ret %d\n", ret);
        goto _ERR;
D
duangavin123 已提交
428
    }
A
annie_wangli 已提交
429 430 431 432 433 434 435 436

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

D
duangavin123 已提交
437
    /* Disable the PWM device. */
A
annie_wangli 已提交
438
    ret = PwmDisable(handle);
D
duangavin123 已提交
439 440
    if (ret != 0) {
        HDF_LOGE("PwmDisable: failed, ret %d\n", ret);
A
annie_wangli 已提交
441
        goto _ERR;
D
duangavin123 已提交
442
    }
A
annie_wangli 已提交
443 444 445 446
    
_ERR:
    /* Close the PWM device handle. */
    PwmClose(handle); 
D
duangavin123 已提交
447 448
}
```