driver-platform-pwm-des.md 10.1 KB
Newer Older
A
annie_wangli 已提交
1
# PWM
A
annie_wangli 已提交
2 3


A
annie_wangli 已提交
4 5 6 7 8 9
## Overview

Pulse width modulation (PWM) is a technology that digitally encodes analog signal levels and converts 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:
- Opening or closing a PWM device handle
A
annie_wangli 已提交
10 11

- Setting the PWM period, signal ON-state time, and polarity
A
annie_wangli 已提交
12

A
annie_wangli 已提交
13
- Enabling and disabling a PWM device
A
annie_wangli 已提交
14

A
annie_wangli 已提交
15 16 17
- Obtaining and setting PWM parameters


A
annie_wangli 已提交
18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41
### PwmConfig Structure

  **Table 1** PwmConfig structure

| 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.<br>-&nbsp;Positive value: indicates the number of square waves to generate.<br>-&nbsp;**0**: indicates that square waves are generated continuously.| 
| polarity | PWM signal polarity, which can be positive or reverse.| 
| status | PWM device status, which can be enabled or disabled.| 


## Available APIs

  **Table 2** PWM driver APIs

| Category| Description| 
| -------- | -------- |
| Operating PWM handles| -&nbsp;**PwmOpen**: opens the device handle of a PWM device.<br>-&nbsp;**PwmClose**: closes the device handle of a PWM device.| 
| Enabling or disabling PWM| -&nbsp;**PwmEnable**: enables a PWM device.<br>-&nbsp;**PwmDisable**: disables a PWM device.| 
| Setting PWM| -&nbsp;**PwmSetPeriod**: sets the PWM period.<br>-&nbsp;**PwmSetDuty**: sets the signal ON-state time.<br>-&nbsp;**PwmSetPolarity**: sets the PWM signal polarity.| 
| Setting or obtaining PWM configuration| -&nbsp;**PwmSetConfig**: sets PWM device parameters.<br>-&nbsp;**PwmGetConfig**: obtains PWM device parameters.| 

A
annie_wangli 已提交
42
> ![icon-note.gif](../public_sys-resources/icon-note.gif) **NOTE**<br/>
A
annie_wangli 已提交
43 44 45 46 47 48 49 50 51 52 53
> All APIs described in this document can be called only in the kernel space.


## Usage Guidelines


### How to Use

The figure below shows the general process of using the PWM.

  **Figure 1** Process of using the PWM module
A
annie_wangli 已提交
54 55 56

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

A
annie_wangli 已提交
57 58

### Opening a PWM Device Handle
A
annie_wangli 已提交
59 60 61

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

A
annie_wangli 已提交
62 63

```
A
annie_wangli 已提交
64
DevHandle PwmOpen(uint32_t num);
D
duangavin123 已提交
65
```
A
annie_wangli 已提交
66

A
annie_wangli 已提交
67
  **Table 3** Description of PwmOpen
A
annie_wangli 已提交
68

A
annie_wangli 已提交
69 70 71 72
| **Parameter**| **Description**|
| -------- | -------- |
| num        | PWM device number.            |
| **Return Value** | **Description**         |
A
annie_wangli 已提交
73
| handle     | Handle of the PWM device obtained.|
A
annie_wangli 已提交
74 75 76
| NULL       | The operation fails.               |

Example: Open the device handle of PWM device 0.
A
annie_wangli 已提交
77 78


A
annie_wangli 已提交
79
```
A
annie_wangli 已提交
80 81
uint32_t num = 0;             /* PWM device number. */
DevHandle handle = NULL;
D
duangavin123 已提交
82 83

/* Obtain the PWM device handle. */
A
annie_wangli 已提交
84 85 86
handle = PwmOpen(num);
if (handle  == NULL) {
    /* Error handling. */
D
duangavin123 已提交
87 88 89
}
```

A
annie_wangli 已提交
90

A
annie_wangli 已提交
91
### Closing a PWM Device Handle
D
duangavin123 已提交
92

A
annie_wangli 已提交
93
Call **PwmClose()** to close a PWM device handle to release resources.
A
annie_wangli 已提交
94 95


A
annie_wangli 已提交
96 97 98
```
voidPwmClose(DevHandle handle);
```
A
annie_wangli 已提交
99

A
annie_wangli 已提交
100
  **Table 4** Description of PwmClose
A
annie_wangli 已提交
101

A
annie_wangli 已提交
102 103 104
| **Parameter**| **Description**|
| -------- | -------- |
| handle   | PWM device handle to close. |
A
annie_wangli 已提交
105

A
annie_wangli 已提交
106 107

```
A
annie_wangli 已提交
108 109
/* Close a PWM device handle. */
PwmClose(handle);
D
duangavin123 已提交
110 111 112
```


A
annie_wangli 已提交
113
### Enabling a PWM Device
A
annie_wangli 已提交
114

A
annie_wangli 已提交
115 116 117 118
Call **PwmEnable()** to enable a PWM device.


```
A
annie_wangli 已提交
119
int32_t PwmEnable(DevHandle handle);
D
duangavin123 已提交
120
```
A
annie_wangli 已提交
121

A
annie_wangli 已提交
122
  **Table 5** Description of PwmEnable
A
annie_wangli 已提交
123

A
annie_wangli 已提交
124 125 126
| **Parameter**| **Description**|
| -------- | -------- |
| handle     | PWM device handle.   |
A
annie_wangli 已提交
127
| **Return Value** | **Description** |
A
annie_wangli 已提交
128 129 130
| 0          | The operation is successful.      |
| Negative number      | The operation fails.      |

A
annie_wangli 已提交
131

A
annie_wangli 已提交
132
```
D
duangavin123 已提交
133
int32_t ret;
A
annie_wangli 已提交
134 135 136

/* Enable a PWM device. */
ret = PwmEnable(handle);
D
duangavin123 已提交
137
if (ret != 0) {
A
annie_wangli 已提交
138
	/* Error handling. */
D
duangavin123 已提交
139 140 141
}
```

A
annie_wangli 已提交
142

A
annie_wangli 已提交
143 144 145
### Disabling a PWM Device

Call **PwmDisable()** to disable a PWM device.
D
duangavin123 已提交
146

A
annie_wangli 已提交
147 148

```
A
annie_wangli 已提交
149
int32_t PwmDisable(DevHandle handle);
D
duangavin123 已提交
150
```
A
annie_wangli 已提交
151

A
annie_wangli 已提交
152
  **Table 6** Description of PwmDisable
A
annie_wangli 已提交
153

A
annie_wangli 已提交
154 155 156
| **Parameter**| **Description**|
| -------- | -------- |
| handle     | PWM device handle.   |
A
annie_wangli 已提交
157
| **Return Value** | **Description** |
A
annie_wangli 已提交
158 159 160
| 0          | The operation is successful.      |
| Negative number      | The operation fails.      |

A
annie_wangli 已提交
161

A
annie_wangli 已提交
162
```
D
duangavin123 已提交
163
int32_t ret;
A
annie_wangli 已提交
164 165 166

/* Disable a PWM device. */
ret = PwmDisable(handle);
D
duangavin123 已提交
167
if (ret != 0) {
A
annie_wangli 已提交
168
	/* Error handling. */
D
duangavin123 已提交
169 170 171
}
```

A
annie_wangli 已提交
172

A
annie_wangli 已提交
173 174 175 176
### Setting the PWM Period

Call **PwmSetPeriod()** to set the PWM period.

D
duangavin123 已提交
177

A
annie_wangli 已提交
178
```
A
annie_wangli 已提交
179
int32_t PwmSetPeriod(DevHandle handle, uint32_t period);
D
duangavin123 已提交
180
```
A
annie_wangli 已提交
181

A
annie_wangli 已提交
182
  **Table 7** Description of PwmSetPeriod
A
annie_wangli 已提交
183

A
annie_wangli 已提交
184 185 186
| **Parameter**| **Description**|
| -------- | -------- |
| handle     | PWM device handle.             |
A
annie_wangli 已提交
187
| period     | PWM period to set, in ns.|
A
annie_wangli 已提交
188 189 190
| **Return Value**| **Description**           |
| 0          | The operation is successful.                |
| Negative number      | The operation fails.                |
A
annie_wangli 已提交
191

A
annie_wangli 已提交
192 193

```
D
duangavin123 已提交
194
int32_t ret;
A
annie_wangli 已提交
195 196 197

/* Set the PWM period to 50000000 ns.*/
ret = PwmSetPeriod(handle, 50000000);
D
duangavin123 已提交
198
if (ret != 0) {
A
annie_wangli 已提交
199
	/* Error handling. */
D
duangavin123 已提交
200 201 202 203
}
```


A
annie_wangli 已提交
204 205 206 207 208 209
### Setting the PWM Signal ON-State Time

Call **PwmSetDuty()** to set the time that the PWM signal is in the ON state.


```
A
annie_wangli 已提交
210
int32_t PwmSetDuty(DevHandle handle, uint32_t duty);
D
duangavin123 已提交
211
```
A
annie_wangli 已提交
212

A
annie_wangli 已提交
213
  **Table 8** Description of PwmSetDuty
A
annie_wangli 已提交
214

A
annie_wangli 已提交
215 216 217
| **Parameter**| **Description**|
| -------- | -------- |
| handle     | PWM device handle.                 |
A
annie_wangli 已提交
218
| duty       | Time that the signal is in the ON state, in ns.|
A
annie_wangli 已提交
219 220 221
| **Return Value**| **Description**               |
| 0          | The operation is successful.                    |
| Negative number      | The operation fails.                    |
A
annie_wangli 已提交
222

A
annie_wangli 已提交
223 224

```
D
duangavin123 已提交
225
int32_t ret;
A
annie_wangli 已提交
226 227 228

/* Set the signal ON-state time to 25000000 ns. */
ret = PwmSetDuty(handle, 25000000);
D
duangavin123 已提交
229
if (ret != 0) {
A
annie_wangli 已提交
230
	/* Error handling. */
D
duangavin123 已提交
231 232 233 234
}
```


A
annie_wangli 已提交
235 236 237 238 239 240
### Setting the PWM Signal Polarity

Call **PwmSetPolarity()** to set the signal polarity for a PWM device.


```
A
annie_wangli 已提交
241
int32_t PwmSetPolarity(DevHandle handle, uint8_t polarity);
D
duangavin123 已提交
242
```
A
annie_wangli 已提交
243

A
annie_wangli 已提交
244
  **Table 9** Description of PwmSetPolarity
A
annie_wangli 已提交
245

A
annie_wangli 已提交
246 247 248
| **Parameter**| **Description**|
| -------- | -------- |
| handle     | PWM device handle.        |
A
annie_wangli 已提交
249
| polarity   | Polarity to set, which can be **PWM\_NORMAL\_POLARITY** or **PWM\_INVERTED\_POLARITY**.|
A
annie_wangli 已提交
250 251 252
| **Return Value**| **Description**      |
| 0          | The operation is successful.           |
| Negative number      | The operation fails.           |
A
annie_wangli 已提交
253

A
annie_wangli 已提交
254 255

```
D
duangavin123 已提交
256
int32_t ret;
A
annie_wangli 已提交
257 258 259

/* Set the PWM polarity to PWM_INVERTED_POLARITY. */
ret = PwmSetPolarity(handle, PWM_INVERTED_POLARITY);
D
duangavin123 已提交
260
if (ret != 0) {
A
annie_wangli 已提交
261
	/* Error handling. */
D
duangavin123 已提交
262 263 264 265
}
```


A
annie_wangli 已提交
266
### Setting PWM Device Parameters
A
annie_wangli 已提交
267

A
annie_wangli 已提交
268
Call **PwmSetConfig()** to set PWM device parameters.
A
annie_wangli 已提交
269

A
annie_wangli 已提交
270 271

```
A
annie_wangli 已提交
272
int32_t PwmSetConfig(DevHandle handle, struct PwmConfig *config);
D
duangavin123 已提交
273
```
A
annie_wangli 已提交
274

A
annie_wangli 已提交
275
  **Table 10** Description of PwmSetConfig
A
annie_wangli 已提交
276

A
annie_wangli 已提交
277 278 279 280 281 282 283
| **Parameter**| **Description**|
| -------- | -------- |
| handle     | PWM device handle to close.   |
| \*config   | Pointer to PWM parameters.      |
| **Return Value**| **Description** |
| 0          | The operation is successful.      |
| Negative number      | The operation fails.      |
A
annie_wangli 已提交
284 285


A
annie_wangli 已提交
286
```
D
duangavin123 已提交
287
int32_t ret;
A
annie_wangli 已提交
288
struct PwmConfig pcfg;
A
annie_wangli 已提交
289 290 291
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. */
A
annie_wangli 已提交
292
pcfg.polarity = PWM_INVERTED_POLARITY;	/* Set the PWM polarity to PWM_INVERTED_POLARITY. */
A
annie_wangli 已提交
293
pcfg.status = PWM_ENABLE_STATUS;	/* Set the running status to Enabled. */
A
annie_wangli 已提交
294 295 296

/* Set PWM device parameters. */
ret = PwmSetConfig(handle, &pcfg);
D
duangavin123 已提交
297
if (ret != 0) {
A
annie_wangli 已提交
298
	/* Error handling. */
D
duangavin123 已提交
299 300 301 302
}
```


A
annie_wangli 已提交
303 304 305
### Obtaining PWM Device Parameters

Call **PwmGetConfig()** to obtain PWM device parameters.
D
duangavin123 已提交
306

A
annie_wangli 已提交
307 308

```
A
annie_wangli 已提交
309 310
int32_t PwmGetConfig(DevHandle handle, struct PwmConfig *config);
```
D
duangavin123 已提交
311

A
annie_wangli 已提交
312
  **Table 11** Description of PwmGetConfig
D
duangavin123 已提交
313

A
annie_wangli 已提交
314 315 316 317 318 319 320
| **Parameter**| **Description**|
| -------- | -------- |
| handle     | PWM device handle to close.   |
| \*config   | Pointer to PWM parameters.      |
| **Return Value**| **Description** |
| 0          | The operation is successful.      |
| Negative number      | The operation fails.      |
D
duangavin123 已提交
321

A
annie_wangli 已提交
322

A
annie_wangli 已提交
323
```
A
annie_wangli 已提交
324 325 326 327 328 329 330 331
int32_t ret;
struct PwmConfig pcfg;

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

A
annie_wangli 已提交
334 335

## Development Example
D
duangavin123 已提交
336

A
annie_wangli 已提交
337
The following example shows how to use the APIs to implement a PWM driver and manage the PWM device.
D
duangavin123 已提交
338

A
annie_wangli 已提交
339

D
duangavin123 已提交
340 341 342 343
```
void PwmTestSample(void)
{
    int32_t ret;
A
annie_wangli 已提交
344 345 346 347
    uint32_t num;
    DevHandle handle = NULL;

    struct PwmConfig pcfg;
A
annie_wangli 已提交
348 349 350
    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. */
A
annie_wangli 已提交
351 352
    pcfg.polarity = PWM_NORMAL_POLARITY;	/* Set the polarity to PWM_NORMAL_POLARITY. */
    pcfg.status = PWM_ENABLE_STATUS;		/* Set the running status to Enabled. */
A
annie_wangli 已提交
353

A
annie_wangli 已提交
354 355 356 357 358 359 360
    /* 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 已提交
361 362
        return;
    }
A
annie_wangli 已提交
363 364 365

    /* Set the PWM period to 50000000 ns.*/
    ret = PwmSetPeriod(handle, 50000000);
D
duangavin123 已提交
366
    if (ret != 0) {
A
annie_wangli 已提交
367 368
        HDF_LOGE("PwmSetPeriod: failed, ret %d\n", ret);
        goto _ERR;
D
duangavin123 已提交
369
    }
A
annie_wangli 已提交
370 371 372

    /* Set the signal ON-state time to 25000000 ns. */
    ret = PwmSetDuty(handle, 25000000);
D
duangavin123 已提交
373
    if (ret != 0) {
A
annie_wangli 已提交
374 375 376 377 378 379 380 381 382
        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 已提交
383
    }
A
annie_wangli 已提交
384

A
annie_wangli 已提交
385 386 387 388 389 390
    /* Obtain PWM device parameters. */
    ret = PwmGetConfig(handle, &pcfg);
    if (ret != 0) {
        HDF_LOGE("PwmGetConfig: failed, ret %d\n", ret);
        goto _ERR;
    }
A
annie_wangli 已提交
391

D
duangavin123 已提交
392
    /* Enable the PWM device. */
A
annie_wangli 已提交
393
    ret = PwmEnable(handle);
D
duangavin123 已提交
394
    if (ret != 0) {
A
annie_wangli 已提交
395 396
	    HDF_LOGE("PwmEnable: failed, ret %d\n", ret);
        goto _ERR;
D
duangavin123 已提交
397
    }
A
annie_wangli 已提交
398 399 400 401 402 403 404 405

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

D
duangavin123 已提交
406
    /* Disable the PWM device. */
A
annie_wangli 已提交
407
    ret = PwmDisable(handle);
D
duangavin123 已提交
408 409
    if (ret != 0) {
        HDF_LOGE("PwmDisable: failed, ret %d\n", ret);
A
annie_wangli 已提交
410
        goto _ERR;
D
duangavin123 已提交
411
    }
A
annie_wangli 已提交
412

A
annie_wangli 已提交
413 414 415
_ERR:
    /* Close the PWM device handle. */
    PwmClose(handle); 
D
duangavin123 已提交
416 417
}
```