- 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.
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
The PWM APIs provide a set of functions for operating a PWM device, including those for:
- Setting the PWM period, duty cycle, and polarity
- Obtaining and releasing a PWM device handle
- Enabling and disabling a PWM device
- Setting the PWM period, signal ON-state time, and polarity
- Obtaining and setting configuration parameters
- Enabling and disabling a PWM device
- Obtaining and setting PWM parameters
## Available APIs<a name="section3939192652418"></a>
| period | Time for a signal to complete an on-and-off cycle, in ns.|
</th>
| 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.|
</tr>
| polarity | PWM signal polarity, which can be **PWM\_NORMAL\_POLARITY** or **PWM\_INVERTED\_POLARITY**.|
</thead>
| status | PWM device status, which can be enabled or disabled.|
<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>
</tr>
</td>
<trheight="19"style="height:14.25pt">
</tr>
<tdrowspan="2"height="38"class="xl65"style="height:28.5pt">Enabling or disabling PWM</td>
<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>
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.
### How to Use<a name="section3.1_PWM_des"></a>
DevHandle PwmOpen\(uint32\_t num\);
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.
**Table 2** Description of PwmOpen
[Figure 1](#fig1_PWM_des) shows the general process of using the PWM module.
<tdclass="cellrowborder"valign="top"width="50%"><pid="p760471912388"><aname="p760471912388"></a><aname="p760471912388"></a>Failed to obtain the PWM device handle.</p>
<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>
```c
voidPwmClose(DevHandlehandle);
```
```
int32_t ret;
uint32_t period = 1000; /* Period: 1000 ns */
**Table 4** PwmClose
ret = PwmSetPeriod(pwm, period); /* Set the PWM period. */
if (ret != 0) {
<aname="table4_PWM_des"></a>
HDF_LOGE("PwmSetPeriod: failed, ret %d", ret);
}
| 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>
### Enabling a PWM Device<a name="section3.4_PWM_des"></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>
```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;
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){
if(ret!=0){
HDF_LOGE("PwmSetDuty: failed, ret %d", ret);
/* Error handling. */
}
}
```
```
### Setting the PWM Polarity<a name="section12383334115"></a>
### Disabling a PWM Device<a name="section3.5_PWM_des"></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>
<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>
<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>
```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;
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){
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.