-[Releasing the PWM Device Handle](#section12987111511450)
-[Usage Example](#section138636719469)
## Overview<a name="section1043395117296"></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 define a set of common functions for operating a PWM device, including those for:
- Obtaining and releasing a PWM device handle
- Setting the PWM period, duty cycle, and polarity
- Enabling and disabling a PWM device
- Obtaining and setting configuration parameters
## Available APIs<a name="section3939192652418"></a>
<tdclass="cellrowborder"valign="top"headers="mcps1.2.4.1.2 "><pid="p829111362306"><aname="p829111362306"></a><aname="p829111362306"></a>Releases a PWM device handle.</p>
</td>
</tr>
<trid="row34145016535"><tdclass="cellrowborder"rowspan="3"valign="top"width="20.857914208579142%"headers="mcps1.2.4.1.1 "><pid="p229610227124"><aname="p229610227124"></a><aname="p229610227124"></a>Setting the PWM period, duty cycle, and polarity</p>
<tdclass="cellrowborder"valign="top"width="55.77442255774422%"headers="mcps1.2.4.1.3 "><pid="p1360544321811"><aname="p1360544321811"></a><aname="p1360544321811"></a>Sets the PWM period.</p>
<tdclass="cellrowborder"valign="top"headers="mcps1.2.4.1.2 "><pid="p448643019188"><aname="p448643019188"></a><aname="p448643019188"></a>Sets the PWM polarity.</p>
<tdclass="cellrowborder"valign="top"width="55.77442255774422%"headers="mcps1.2.4.1.3 "><pid="p855303118314"><aname="p855303118314"></a><aname="p855303118314"></a>Enables a PWM device.</p>
<tdclass="cellrowborder"valign="top"headers="mcps1.2.4.1.2 "><pid="p261518281139"><aname="p261518281139"></a><aname="p261518281139"></a>Disables a PWM device.</p>
[Figure 1](#fig23885455594) shows the process of using a PWM device.
**Figure 1** Process of using a PWM device<aname="fig23885455594"></a>

### Obtaining a PWM Device Handle<a name="section17816586359"></a>
Before performing operations on a PWM device, obtain a PWM device handle by calling **PwmOpen**. This function returns a PWM device handle with the specified device number.
<tdclass="cellrowborder"valign="top"width="50%"><pid="p760471912388"><aname="p760471912388"></a><aname="p760471912388"></a>Failed to obtain the PWM device handle.</p>
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
| 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.|
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<aname="fig1_PWM_des"></a>

### 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
DevHandlePwmOpen(uint32_tnum);
```
uint32_t num = 0; /* PWM device number */
DevHandle pwm = NULL; /* PWM device handle */
**Table 3** PwmOpen
<aname="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_tnum=0;/* PWM device number. */
DevHandlehandle=NULL;
/* Obtain the PWM device handle. */
pwm = PwmOpen(num);
if (pwm == NULL) {
HDF_LOGE("PwmOpen: pwm%d failed", num);
return;
handle=PwmOpen(num);
if(handle==NULL){
/* Error handling. */
}
```
### Setting the PWM Period<a name="section920214812397"></a>
<tdclass="cellrowborder"valign="top"width="50%"><pid="p1312411126320"><aname="p1312411126320"></a><aname="p1312411126320"></a>Period, in nanoseconds.</p>
<tdclass="cellrowborder"valign="top"width="50%"><pid="p15125812193212"><aname="p15125812193212"></a><aname="p15125812193212"></a>Succeeded in setting the PWM period.</p>
<tdclass="cellrowborder"valign="top"width="50%"><pid="p81251512143217"><aname="p81251512143217"></a><aname="p81251512143217"></a>Failed to set the period.</p>
</td>
</tr>
</tbody>
</table>
### Closing a PWM Device Handle<a name="section3.3_PWM_des"></a>
Close a PWM device to release resources.
```c
voidPwmClose(DevHandlehandle);
```
int32_t ret;
uint32_t period = 1000; /* Period: 1000 ns */
ret = PwmSetPeriod(pwm, period); /* Set the PWM period. */
if (ret != 0) {
HDF_LOGE("PwmSetPeriod: failed, ret %d", ret);
}
**Table 4** PwmClose
<aname="table4_PWM_des"></a>
| Parameter| Description|
| ------ | ----------- |
| handle | PWM device handle to close.|
```c
/* Close a PWM device handle. */
PwmClose(handle);
```
### Setting the PWM Duty Cycle<a name="section519712820405"></a>
<tdclass="cellrowborder"valign="top"width="50%"><pid="p13107201263620"><aname="p13107201263620"></a><aname="p13107201263620"></a>Duty cycle, in nanoseconds.</p>
<tdclass="cellrowborder"valign="top"width="50%"><pid="p61081712203612"><aname="p61081712203612"></a><aname="p61081712203612"></a>Succeeded in setting the duty cycle.</p>
<tdclass="cellrowborder"valign="top"width="50%"><pid="p10108012143617"><aname="p10108012143617"></a><aname="p10108012143617"></a>Failed to set the duty cycle.</p>
</td>
</tr>
</tbody>
</table>
### Enabling a PWM Device<a name="section3.4_PWM_des"></a>
Enable a PWM device.
```c
int32_tPwmEnable(DevHandlehandle);
```
**Table 5** PwmEnable
<aname="table5_PWM_des"></a>
| Parameter| Description|
| ---------- | -------------- |
| handle | PWM device handle.|
| **Return Value** | **Description** |
| 0 | The operation is successful.|
| Negative number| The operation fails.|
```c
int32_tret;
uint32_t duty = 500; /* Duty cycle: 500 ns */
ret = PwmSetDuty(pwm, duty); /* Set the PWM duty cycle. */
/* Enable a PWM device. */
ret=PwmEnable(handle);
if(ret!=0){
HDF_LOGE("PwmSetDuty: failed, ret %d", ret);
/* Error handling. */
}
```
### Setting the PWM Polarity<a name="section12383334115"></a>
<tdclass="cellrowborder"valign="top"width="50%"><pid="p184181937123811"><aname="p184181937123811"></a><aname="p184181937123811"></a>Polarity, which can be <strongid="b31772419213"><aname="b31772419213"></a><aname="b31772419213"></a>PWM_NORMAL_POLARITY</strong> or <strongid="b31771840212"><aname="b31771840212"></a><aname="b31771840212"></a>PWM_INVERTED_POLARITY</strong>.</p>
<tdclass="cellrowborder"valign="top"width="50%"><pid="p241993723818"><aname="p241993723818"></a><aname="p241993723818"></a>Succeed in setting the polarity.</p>
<tdclass="cellrowborder"valign="top"width="50%"><pid="p84191237193817"><aname="p84191237193817"></a><aname="p84191237193817"></a>Failed to set the polarity.</p>
</td>
</tr>
</tbody>
</table>
### Disabling a PWM Device<a name="section3.5_PWM_des"></a>
<tdclass="cellrowborder"valign="top"width="50%"><pid="p6854452114416"><aname="p6854452114416"></a><aname="p6854452114416"></a>Succeeded in enabling the PWM device.</p>
<tdclass="cellrowborder"valign="top"width="50%"><pid="p2854052124416"><aname="p2854052124416"></a><aname="p2854052124416"></a>Failed to enable the PWM device.</p>
</td>
</tr>
</tbody>
</table>
### Setting the PWM Period<a name="section3.6_PWM_des"></a>
<tdclass="cellrowborder"valign="top"width="50%"><pid="p15550839174713"><aname="p15550839174713"></a><aname="p15550839174713"></a>Succeeded in disabling the PWM device.</p>
<tdclass="cellrowborder"valign="top"width="50%"><pid="p4550143919471"><aname="p4550143919471"></a><aname="p4550143919471"></a>Failed to disable the PWM device.</p>
</td>
</tr>
</tbody>
</table>
Set the time that the PWM signal is in the ON state.
```c
int32_tPwmSetDuty(DevHandlehandle,uint32_tduty);
```
**Table 8** PwmSetDuty
<aname="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
int32_tret;
ret = PwmDisable(pwm); /* Disable the PWM device. */
/* Set the signal ON-state time to 25000000 ns. */
ret=PwmSetDuty(handle,25000000);
if(ret!=0){
HDF_LOGE("PwmDisable: failed, ret %d", ret);
/* Error handling. */
}
```
### Setting the PWM Polarity<a name="section3.8_PWM_des"></a>
<tdclass="cellrowborder"valign="top"width="50%"><pid="p172091452131319"><aname="p172091452131319"></a><aname="p172091452131319"></a>Pointer to the <strongid="b1844333918213"><aname="b1844333918213"></a><aname="b1844333918213"></a>PwmConfig</strong> structure containing PWM device configuration parameters.</p>
<tdclass="cellrowborder"valign="top"width="50%"><pid="p1736120541323"><aname="p1736120541323"></a><aname="p1736120541323"></a>Pointer to the <strongid="b886814395214"><aname="b886814395214"></a><aname="b886814395214"></a>PwmConfig</strong> structure containing PWM device configuration parameters.</p>
<tdclass="cellrowborder"valign="top"width="50%"><pid="p83629546325"><aname="p83629546325"></a><aname="p83629546325"></a>Failed to set PWM configuration parameters.</p>
The following example shows how to obtain a PWM device handle, set the PWM configuration parameters, enable the PWM device, and finally release the PWM device handle.
The following example shows how to use the APIs to implement a PWM driver and manage the PWM device.