1e41f4b71Sopenharmony_ci# PWM 2e41f4b71Sopenharmony_ci 3e41f4b71Sopenharmony_ci## Overview 4e41f4b71Sopenharmony_ci 5e41f4b71Sopenharmony_ci### Function 6e41f4b71Sopenharmony_ci 7e41f4b71Sopenharmony_ciPulse width modulation (PWM) is a technology that digitally encodes analog signal levels and converts them into pulses. 8e41f4b71Sopenharmony_ci 9e41f4b71Sopenharmony_ciThe PWM module provides a set of APIs for operating a PWM device, including: 10e41f4b71Sopenharmony_ci 11e41f4b71Sopenharmony_ci- Opening or closing a PWM device 12e41f4b71Sopenharmony_ci- Setting the PWM period, signal ON-state time, and polarity 13e41f4b71Sopenharmony_ci- Enabling or disabling a PWM device 14e41f4b71Sopenharmony_ci- Obtaining and setting PWM parameters 15e41f4b71Sopenharmony_ci 16e41f4b71Sopenharmony_ci### Basic Concepts 17e41f4b71Sopenharmony_ci 18e41f4b71Sopenharmony_ciA pulse (electrical pulse) is a burst of current or voltage, characterized by sudden change and discontinuity. There are many types of pulses. Common pulses include triangular, sharp, rectangular, square, trapezoidal, and zigzag pulses. Main pulse parameters include the repetition period **T** (**T** = 1/**F**, where **F** is the pulse repetition frequency), pulse amplitude **U**, rise time **ts** at the leading edge, fall time **t** at the trailing edge, and pulse width **tk**. 19e41f4b71Sopenharmony_ci 20e41f4b71Sopenharmony_ci### Working Principles 21e41f4b71Sopenharmony_ci 22e41f4b71Sopenharmony_ciIn the Hardware Driver Foundation (HDF), the PWM uses the independent service mode (see Figure 1) for API adaptation. In this mode, each device independently publishes a service to process external access requests. When receiving an access request, the HDF DeviceManager extracts parameters from the request to call the internal APIs of the target device. In the independent service mode, the HDF DeviceManager provides service management capabilities. However, you need to configure a node for each device, which increases memory usage. 23e41f4b71Sopenharmony_ci 24e41f4b71Sopenharmony_ciIn the independent service mode, the core layer does not publish a service for the upper layer. Therefore, a service must be published for each controller. To achieve this purpose: 25e41f4b71Sopenharmony_ci 26e41f4b71Sopenharmony_ci- You need to implement the **Bind()** function in **HdfDriverEntry** to bind services. 27e41f4b71Sopenharmony_ci- The **policy** field of **deviceNode** in the **device_info.hcs** file can be **1** or **2**, but not **0**. 28e41f4b71Sopenharmony_ci 29e41f4b71Sopenharmony_ciThe PWM module is divided into the following layers: 30e41f4b71Sopenharmony_ci 31e41f4b71Sopenharmony_ci- Interface layer: provides APIs for opening or closing a PWM device, setting the PWM period, signal ON-state time, PWM device polarity, or PWM device parameters, obtaining PWM device parameters, and enabling or disabling a PWM device 32e41f4b71Sopenharmony_ci- Core layer: provides the capabilities of adding or removing a PWM controller and managing PWM devices. The core layer interacts with the adaptation layer through hook functions. 33e41f4b71Sopenharmony_ci- Adaptation layer: instantiates the hook functions to implement specific features. 34e41f4b71Sopenharmony_ci 35e41f4b71Sopenharmony_ci**Figure 1** Independent service mode 36e41f4b71Sopenharmony_ci 37e41f4b71Sopenharmony_ci 38e41f4b71Sopenharmony_ci 39e41f4b71Sopenharmony_ci## Usage Guidelines 40e41f4b71Sopenharmony_ci 41e41f4b71Sopenharmony_ci### When to Use 42e41f4b71Sopenharmony_ci 43e41f4b71Sopenharmony_ciThe PWM module is used for controlling vibrators and adjusting backlight brightness in smart devices. 44e41f4b71Sopenharmony_ci 45e41f4b71Sopenharmony_ci### Available APIs 46e41f4b71Sopenharmony_ci 47e41f4b71Sopenharmony_ci**Table 1** describes the **PwmConfig** structure, which defines the PWM device attributes. **Table 2** describes the APIs provided by the PWM module. 48e41f4b71Sopenharmony_ci 49e41f4b71Sopenharmony_ci**Table 1** PwmConfig structure 50e41f4b71Sopenharmony_ci 51e41f4b71Sopenharmony_ci| Parameter| Description| 52e41f4b71Sopenharmony_ci| -------- | -------- | 53e41f4b71Sopenharmony_ci| duty | Time that a signal is in the ON state, in ns.| 54e41f4b71Sopenharmony_ci| period | Time for a signal to complete an on-and-off cycle, in ns.| 55e41f4b71Sopenharmony_ci| number | Number of square waves to generate.<br>- Positive value: indicates the number of square waves to generate.<br>- **0**: indicates that square waves are generated continuously.| 56e41f4b71Sopenharmony_ci| polarity | PWM signal polarity, which can be normal or reverted. <br>A signal with normal polarity starts high for the duration of the duty cycle and goes low for the remaining of the period. <br>A signal with inverted polarity starts low for the duration of the duty cycle and goes high for the remaining of the period.| 57e41f4b71Sopenharmony_ci| status | PWM device status, which can be enabled or disabled.| 58e41f4b71Sopenharmony_ci 59e41f4b71Sopenharmony_ci**Table 2** PWM driver APIs 60e41f4b71Sopenharmony_ci 61e41f4b71Sopenharmony_ci| API | | 62e41f4b71Sopenharmony_ci| ------------------------------------------------------------ | ------------------- | 63e41f4b71Sopenharmony_ci| DevHandle PwmOpen(uint32_t num) | Opens a PWM device. | 64e41f4b71Sopenharmony_ci| void PwmClose(DevHandle handle) | Closes a PWM device. | 65e41f4b71Sopenharmony_ci| int32_t PwmSetPeriod(DevHandle handle, uint32_t period) | Sets the PWM period. | 66e41f4b71Sopenharmony_ci| int32_t PwmSetDuty(DevHandle handle, uint32_t duty) | Sets the signal ON-state time.| 67e41f4b71Sopenharmony_ci| int32_t PwmSetPolarity(DevHandle handle, uint8_t polarity) | Sets the PWM signal polarity. | 68e41f4b71Sopenharmony_ci| int32_t PwmEnable(DevHandle handle) | Enables a PWM device. | 69e41f4b71Sopenharmony_ci| int32_t PwmDisable(DevHandle handle) | Disables a PWM device. | 70e41f4b71Sopenharmony_ci| int32_t PwmSetConfig(DevHandle handle, struct PwmConfig *config) | Sets PWM device parameters. | 71e41f4b71Sopenharmony_ci| int32_t PwmGetConfig(DevHandle handle, struct PwmConfig *config) | Obtains PWM device parameters. | 72e41f4b71Sopenharmony_ci 73e41f4b71Sopenharmony_ci>  **NOTE** 74e41f4b71Sopenharmony_ci> 75e41f4b71Sopenharmony_ci> All the PWM APIs described in this document can be used in kernel mode and user mode. 76e41f4b71Sopenharmony_ci 77e41f4b71Sopenharmony_ci### How to Develop 78e41f4b71Sopenharmony_ci 79e41f4b71Sopenharmony_ciThe following figure shows how to use PWM APIs. 80e41f4b71Sopenharmony_ci 81e41f4b71Sopenharmony_ci**Figure 2** Using PWM APIs 82e41f4b71Sopenharmony_ci 83e41f4b71Sopenharmony_ci 84e41f4b71Sopenharmony_ci 85e41f4b71Sopenharmony_ci#### Opening a PWM Device 86e41f4b71Sopenharmony_ci 87e41f4b71Sopenharmony_ciBefore performing operations on a PWM device, use **PwmOpen()** to obtain the device handle. 88e41f4b71Sopenharmony_ci 89e41f4b71Sopenharmony_ci```c 90e41f4b71Sopenharmony_ciDevHandle PwmOpen(uint32_t num); 91e41f4b71Sopenharmony_ci``` 92e41f4b71Sopenharmony_ci 93e41f4b71Sopenharmony_ci**Table 3** Description of PwmOpen 94e41f4b71Sopenharmony_ci 95e41f4b71Sopenharmony_ci| **Parameter**| **Description**| 96e41f4b71Sopenharmony_ci| -------- | -------- | 97e41f4b71Sopenharmony_ci| num | PWM device number. | 98e41f4b71Sopenharmony_ci| **Return Value** | **Description** | 99e41f4b71Sopenharmony_ci| handle | The operation is successful. The PWM device handle is returned.| 100e41f4b71Sopenharmony_ci| NULL | The operation fails. | 101e41f4b71Sopenharmony_ci 102e41f4b71Sopenharmony_ciExample: Open PWM device 0. 103e41f4b71Sopenharmony_ci 104e41f4b71Sopenharmony_ci```c 105e41f4b71Sopenharmony_ciuint32_t num = 0; // PWM device number. 106e41f4b71Sopenharmony_ciDevHandle handle = NULL; 107e41f4b71Sopenharmony_ci 108e41f4b71Sopenharmony_cihandle = PwmOpen(num); // Open PWM device 0 and obtain the device handle. 109e41f4b71Sopenharmony_ciif (handle == NULL) { 110e41f4b71Sopenharmony_ci HDF_LOGE("PwmOpen: open pwm_%u failed.\n", num); 111e41f4b71Sopenharmony_ci return; 112e41f4b71Sopenharmony_ci} 113e41f4b71Sopenharmony_ci``` 114e41f4b71Sopenharmony_ci 115e41f4b71Sopenharmony_ci#### Closing a PWM Device 116e41f4b71Sopenharmony_ci 117e41f4b71Sopenharmony_ciUse **PwmClose()** to close a PWM device to release resources. 118e41f4b71Sopenharmony_ci 119e41f4b71Sopenharmony_ci```c 120e41f4b71Sopenharmony_civoid PwmClose(DevHandle handle); 121e41f4b71Sopenharmony_ci``` 122e41f4b71Sopenharmony_ci 123e41f4b71Sopenharmony_ci**Table 4** Description of PwmClose 124e41f4b71Sopenharmony_ci 125e41f4b71Sopenharmony_ci| **Parameter**| **Description**| 126e41f4b71Sopenharmony_ci| -------- | -------- | 127e41f4b71Sopenharmony_ci| handle | Handle of the PWM device to close. | 128e41f4b71Sopenharmony_ci 129e41f4b71Sopenharmony_ci```c 130e41f4b71Sopenharmony_ciPwmClose(handle); // Close the PWM device and destroy the PWM device handle. 131e41f4b71Sopenharmony_ci``` 132e41f4b71Sopenharmony_ci 133e41f4b71Sopenharmony_ci#### Enabling a PWM Device 134e41f4b71Sopenharmony_ci 135e41f4b71Sopenharmony_ci```c 136e41f4b71Sopenharmony_ciint32_t PwmEnable(DevHandle handle); 137e41f4b71Sopenharmony_ci``` 138e41f4b71Sopenharmony_ci 139e41f4b71Sopenharmony_ci**Table 5** Description of PwmEnable 140e41f4b71Sopenharmony_ci 141e41f4b71Sopenharmony_ci| **Parameter**| **Description**| 142e41f4b71Sopenharmony_ci| -------- | -------- | 143e41f4b71Sopenharmony_ci| handle | PWM device handle. | 144e41f4b71Sopenharmony_ci| **Return Value** | **Description**| 145e41f4b71Sopenharmony_ci| HDF_SUCCESS | The operation is successful. | 146e41f4b71Sopenharmony_ci| Negative number | The operation fails. | 147e41f4b71Sopenharmony_ci 148e41f4b71Sopenharmony_ci```c 149e41f4b71Sopenharmony_ciint32_t ret; 150e41f4b71Sopenharmony_ci 151e41f4b71Sopenharmony_ciret = PwmEnable(handle); // Enable the PWM device. 152e41f4b71Sopenharmony_ciif (ret != HDF_SUCCESS) { 153e41f4b71Sopenharmony_ci HDF_LOGE("PwmEnable: enable pwm failed, ret:%d\n", ret); 154e41f4b71Sopenharmony_ci return ret; 155e41f4b71Sopenharmony_ci} 156e41f4b71Sopenharmony_ci``` 157e41f4b71Sopenharmony_ci 158e41f4b71Sopenharmony_ci#### Disabling a PWM Device 159e41f4b71Sopenharmony_ci 160e41f4b71Sopenharmony_ci```c 161e41f4b71Sopenharmony_ciint32_t PwmDisable(DevHandle handle); 162e41f4b71Sopenharmony_ci``` 163e41f4b71Sopenharmony_ci 164e41f4b71Sopenharmony_ci**Table 6** Description of PwmDisable 165e41f4b71Sopenharmony_ci 166e41f4b71Sopenharmony_ci| **Parameter**| **Description**| 167e41f4b71Sopenharmony_ci| -------- | -------- | 168e41f4b71Sopenharmony_ci| handle | PWM device handle. | 169e41f4b71Sopenharmony_ci| **Return Value** | **Description**| 170e41f4b71Sopenharmony_ci| HDF_SUCCESS | The operation is successful. | 171e41f4b71Sopenharmony_ci| Negative number | The operation fails. | 172e41f4b71Sopenharmony_ci 173e41f4b71Sopenharmony_ci```c 174e41f4b71Sopenharmony_ciint32_t ret; 175e41f4b71Sopenharmony_ci 176e41f4b71Sopenharmony_ciret = PwmDisable(handle); // Disable the PWM device. 177e41f4b71Sopenharmony_ciif (ret != HDF_SUCCESS) { 178e41f4b71Sopenharmony_ci HDF_LOGE("PwmDisable: disable pwm failed, ret:%d\n", ret); 179e41f4b71Sopenharmony_ci return ret; 180e41f4b71Sopenharmony_ci} 181e41f4b71Sopenharmony_ci``` 182e41f4b71Sopenharmony_ci 183e41f4b71Sopenharmony_ci#### Setting the PWM Period 184e41f4b71Sopenharmony_ci 185e41f4b71Sopenharmony_ci```c 186e41f4b71Sopenharmony_ciint32_t PwmSetPeriod(DevHandle handle, uint32_t period); 187e41f4b71Sopenharmony_ci``` 188e41f4b71Sopenharmony_ci 189e41f4b71Sopenharmony_ci**Table 7** Description of PwmSetPeriod 190e41f4b71Sopenharmony_ci 191e41f4b71Sopenharmony_ci| **Parameter**| **Description**| 192e41f4b71Sopenharmony_ci| -------- | -------- | 193e41f4b71Sopenharmony_ci| handle | PWM device handle. | 194e41f4b71Sopenharmony_ci| period | PWM period to set, in ns.| 195e41f4b71Sopenharmony_ci| **Return Value**| **Description** | 196e41f4b71Sopenharmony_ci| HDF_SUCCESS | The operation is successful. | 197e41f4b71Sopenharmony_ci| Negative number | The operation fails. | 198e41f4b71Sopenharmony_ci 199e41f4b71Sopenharmony_ci```c 200e41f4b71Sopenharmony_ciint32_t ret; 201e41f4b71Sopenharmony_ci 202e41f4b71Sopenharmony_ciret = PwmSetPeriod(handle, 50000000); // Set the PWM period to 50,000,000 ns. 203e41f4b71Sopenharmony_ciif (ret != HDF_SUCCESS) { 204e41f4b71Sopenharmony_ci HDF_LOGE("PwmSetPeriod: pwm set period failed, ret:%d\n", ret); 205e41f4b71Sopenharmony_ci return ret; 206e41f4b71Sopenharmony_ci} 207e41f4b71Sopenharmony_ci``` 208e41f4b71Sopenharmony_ci 209e41f4b71Sopenharmony_ci#### Setting the Signal ON-State Time 210e41f4b71Sopenharmony_ci 211e41f4b71Sopenharmony_ci```c 212e41f4b71Sopenharmony_ciint32_t PwmSetDuty(DevHandle handle, uint32_t duty); 213e41f4b71Sopenharmony_ci``` 214e41f4b71Sopenharmony_ci 215e41f4b71Sopenharmony_ci**Table 8** Description of PwmSetDuty 216e41f4b71Sopenharmony_ci 217e41f4b71Sopenharmony_ci| **Parameter**| **Description**| 218e41f4b71Sopenharmony_ci| -------- | -------- | 219e41f4b71Sopenharmony_ci| handle | PWM device handle. | 220e41f4b71Sopenharmony_ci| duty | Time that a signal is in the ON state, in ns.| 221e41f4b71Sopenharmony_ci| **Return Value**| **Description** | 222e41f4b71Sopenharmony_ci| HDF_SUCCESS | The operation is successful. | 223e41f4b71Sopenharmony_ci| Negative number | The operation fails. | 224e41f4b71Sopenharmony_ci 225e41f4b71Sopenharmony_ci 226e41f4b71Sopenharmony_ci```c 227e41f4b71Sopenharmony_ciint32_t ret; 228e41f4b71Sopenharmony_ci 229e41f4b71Sopenharmony_ciret = PwmSetDuty(handle, 25000000); // Set the signal ON-state time to 25,000,000 ns. 230e41f4b71Sopenharmony_ciif (ret != HDF_SUCCESS) { 231e41f4b71Sopenharmony_ci HDF_LOGE("PwmSetDuty: pwm set duty failed, ret:%d\n", ret); 232e41f4b71Sopenharmony_ci return ret; 233e41f4b71Sopenharmony_ci} 234e41f4b71Sopenharmony_ci``` 235e41f4b71Sopenharmony_ci 236e41f4b71Sopenharmony_ci#### Setting the PWM Signal Polarity 237e41f4b71Sopenharmony_ci 238e41f4b71Sopenharmony_ci```c 239e41f4b71Sopenharmony_ciint32_t PwmSetPolarity(DevHandle handle, uint8_t polarity); 240e41f4b71Sopenharmony_ci``` 241e41f4b71Sopenharmony_ci 242e41f4b71Sopenharmony_ci**Table 9** Description of PwmSetPolarity 243e41f4b71Sopenharmony_ci 244e41f4b71Sopenharmony_ci| **Parameter**| **Description**| 245e41f4b71Sopenharmony_ci| -------- | -------- | 246e41f4b71Sopenharmony_ci| handle | PWM device handle. | 247e41f4b71Sopenharmony_ci| polarity | Polarity to set, which can be **PWM\_NORMAL\_POLARITY** or **PWM\_INVERTED\_POLARITY**.| 248e41f4b71Sopenharmony_ci| **Return Value**| **Description** | 249e41f4b71Sopenharmony_ci| HDF_SUCCESS | The operation is successful. | 250e41f4b71Sopenharmony_ci| Negative number | The operation fails. | 251e41f4b71Sopenharmony_ci 252e41f4b71Sopenharmony_ci 253e41f4b71Sopenharmony_ci```c 254e41f4b71Sopenharmony_ciint32_t ret; 255e41f4b71Sopenharmony_ci 256e41f4b71Sopenharmony_ciret = PwmSetPolarity(handle, PWM_INVERTED_POLARITY); // Set the PWM signal polarity to inverted. 257e41f4b71Sopenharmony_ciif (ret != HDF_SUCCESS) { 258e41f4b71Sopenharmony_ci HDF_LOGE("PwmSetPolarity: pwm set polarity failed, ret:%d\n", ret); 259e41f4b71Sopenharmony_ci return ret; 260e41f4b71Sopenharmony_ci} 261e41f4b71Sopenharmony_ci``` 262e41f4b71Sopenharmony_ci 263e41f4b71Sopenharmony_ci#### Setting PWM Device Parameters 264e41f4b71Sopenharmony_ci 265e41f4b71Sopenharmony_ci```c 266e41f4b71Sopenharmony_ciint32_t PwmSetConfig(DevHandle handle, struct PwmConfig *config); 267e41f4b71Sopenharmony_ci``` 268e41f4b71Sopenharmony_ci 269e41f4b71Sopenharmony_ci**Table 10** Description of PwmSetConfig 270e41f4b71Sopenharmony_ci 271e41f4b71Sopenharmony_ci| **Parameter**| **Description**| 272e41f4b71Sopenharmony_ci| -------- | -------- | 273e41f4b71Sopenharmony_ci| handle | PWM device handle. | 274e41f4b71Sopenharmony_ci| \*config | Pointer to the PWM parameters to set. | 275e41f4b71Sopenharmony_ci| **Return Value**| **Description**| 276e41f4b71Sopenharmony_ci| HDF_SUCCESS | The operation is successful. | 277e41f4b71Sopenharmony_ci| Negative number | The operation fails. | 278e41f4b71Sopenharmony_ci 279e41f4b71Sopenharmony_ci```c 280e41f4b71Sopenharmony_ciint32_t ret; 281e41f4b71Sopenharmony_cistruct PwmConfig pcfg; 282e41f4b71Sopenharmony_ci 283e41f4b71Sopenharmony_ciThe pcfg.duty = 25000000; // Set the signal ON-state time to 25,000,000 ns. 284e41f4b71Sopenharmony_cipcfg.period = 50000000; // Set the PWM period to 50,000,000 ns. 285e41f4b71Sopenharmony_cipcfg.number = 0; // Generate square waves continuously. 286e41f4b71Sopenharmony_cipcfg.polarity = PWM_INVERTED_POLARITY; // Set the PWM signal polarity to inverted. 287e41f4b71Sopenharmony_cipcfg.status = PWM_ENABLE_STATUS; // Enable PWM. 288e41f4b71Sopenharmony_ci 289e41f4b71Sopenharmony_ciret = PwmSetConfig(handle, &pcfg); // Set PWM device parameters. 290e41f4b71Sopenharmony_ciif (ret != HDF_SUCCESS) { 291e41f4b71Sopenharmony_ci HDF_LOGE("PwmSetConfig: pwm set config failed, ret:%d\n", ret); 292e41f4b71Sopenharmony_ci return ret; 293e41f4b71Sopenharmony_ci} 294e41f4b71Sopenharmony_ci``` 295e41f4b71Sopenharmony_ci 296e41f4b71Sopenharmony_ci#### Obtaining PWM Device Parameters 297e41f4b71Sopenharmony_ci 298e41f4b71Sopenharmony_ci```c 299e41f4b71Sopenharmony_ciint32_t PwmGetConfig(DevHandle handle, struct PwmConfig *config); 300e41f4b71Sopenharmony_ci``` 301e41f4b71Sopenharmony_ci 302e41f4b71Sopenharmony_ci**Table 11** Description of PwmGetConfig 303e41f4b71Sopenharmony_ci 304e41f4b71Sopenharmony_ci| **Parameter**| **Description**| 305e41f4b71Sopenharmony_ci| -------- | -------- | 306e41f4b71Sopenharmony_ci| handle | PWM device handle. | 307e41f4b71Sopenharmony_ci| \*config | Pointer to the PWM parameters obtained. | 308e41f4b71Sopenharmony_ci| **Return Value**| **Description**| 309e41f4b71Sopenharmony_ci| HDF_SUCCESS | The operation is successful. | 310e41f4b71Sopenharmony_ci| Negative number | The operation fails. | 311e41f4b71Sopenharmony_ci 312e41f4b71Sopenharmony_ci```c 313e41f4b71Sopenharmony_ciint32_t ret; 314e41f4b71Sopenharmony_cistruct PwmConfig pcfg; 315e41f4b71Sopenharmony_ci 316e41f4b71Sopenharmony_ciret = PwmGetConfig(handle, &pcfg); // Obtain PWM device parameters. 317e41f4b71Sopenharmony_ciif (ret != HDF_SUCCESS) { 318e41f4b71Sopenharmony_ci HDF_LOGE("PwmGetConfig: pwm get config failed, ret:%d\n", ret); 319e41f4b71Sopenharmony_ci return ret; 320e41f4b71Sopenharmony_ci} 321e41f4b71Sopenharmony_ci``` 322e41f4b71Sopenharmony_ci 323e41f4b71Sopenharmony_ci## Example 324e41f4b71Sopenharmony_ci 325e41f4b71Sopenharmony_ciThe following uses the Hi3516D V300 development board as an example to describe how to use the PWM. The procedure is as follows: 326e41f4b71Sopenharmony_ci 327e41f4b71Sopenharmony_ci1. Open a PWM device and obtain the PWM device handle. 328e41f4b71Sopenharmony_ci2. Set the PWM device period. 329e41f4b71Sopenharmony_ci3. Set the signal ON-state time for the PWM device. 330e41f4b71Sopenharmony_ci4. Set the signal polarity for the PWM device. 331e41f4b71Sopenharmony_ci5. Obtain the PWM device parameters. 332e41f4b71Sopenharmony_ci6. Enable the PWM device. 333e41f4b71Sopenharmony_ci7. Set the PWM device parameters. 334e41f4b71Sopenharmony_ci8. Disable the PWM device. 335e41f4b71Sopenharmony_ci9. Close the PWM device. 336e41f4b71Sopenharmony_ci 337e41f4b71Sopenharmony_ci```c 338e41f4b71Sopenharmony_ci#include "pwm_if.h" // Header file of PWM standard APIs. 339e41f4b71Sopenharmony_ci#include "hdf_log.h" // Header file of the HDF log APIs. 340e41f4b71Sopenharmony_ci 341e41f4b71Sopenharmony_cistatic int32_t PwmTestSample(void) 342e41f4b71Sopenharmony_ci{ 343e41f4b71Sopenharmony_ci int32_t ret; 344e41f4b71Sopenharmony_ci uint32_t num; 345e41f4b71Sopenharmony_ci uint32_t period 346e41f4b71Sopenharmony_ci DevHandle handle = NULL; 347e41f4b71Sopenharmony_ci 348e41f4b71Sopenharmony_ci struct PwmConfig pcfg; 349e41f4b71Sopenharmony_ci pcfg.duty = 20000000; // Set the signal ON-state time to 20,000,000 ns. 350e41f4b71Sopenharmony_ci pcfg.period = 40000000; // Set the PWM period to 40,000,000 ns. 351e41f4b71Sopenharmony_ci pcfg.number = 100; // Generate 100 square waves continuously. 352e41f4b71Sopenharmony_ci pcfg.polarity = PWM_NORMAL_POLARITY; // Set the PWM signal polarity to normal. 353e41f4b71Sopenharmony_ci pcfg.status = PWM_ENABLE_STATUS; // Enable the PWM device. 354e41f4b71Sopenharmony_ci 355e41f4b71Sopenharmony_ci num = 1; // PWM device number. 356e41f4b71Sopenharmony_ci 357e41f4b71Sopenharmony_ci handle = PwmOpen(num); // Open a PWM device. 358e41f4b71Sopenharmony_ci if (handle == NULL) { 359e41f4b71Sopenharmony_ci HDF_LOGE("PwmOpen: open pwm_%u failed!\n", num); 360e41f4b71Sopenharmony_ci return; 361e41f4b71Sopenharmony_ci } 362e41f4b71Sopenharmony_ci 363e41f4b71Sopenharmony_ci ret = PwmSetPeriod(handle, 50000000); // Set the PWM period to 50,000,000 ns. 364e41f4b71Sopenharmony_ci if (ret != HDF_SUCCESS) { 365e41f4b71Sopenharmony_ci HDF_LOGE("PwmSetPeriod: pwm set period failed, ret %d\n", ret); 366e41f4b71Sopenharmony_ci goto ERR; 367e41f4b71Sopenharmony_ci } 368e41f4b71Sopenharmony_ci 369e41f4b71Sopenharmony_ci ret = PwmSetDuty(handle, 25000000); // Set the signal ON-state time to 25,000,000 ns. 370e41f4b71Sopenharmony_ci if (ret != HDF_SUCCESS) { 371e41f4b71Sopenharmony_ci HDF_LOGE("PwmSetDuty: pwm set duty failed, ret %d\n", ret); 372e41f4b71Sopenharmony_ci goto ERR; 373e41f4b71Sopenharmony_ci } 374e41f4b71Sopenharmony_ci 375e41f4b71Sopenharmony_ci ret = PwmSetPolarity(handle, PWM_INVERTED_POLARITY); // Set the PWM signal polarity to inverted. 376e41f4b71Sopenharmony_ci if (ret != HDF_SUCCESS) { 377e41f4b71Sopenharmony_ci HDF_LOGE("PwmSetPolarity: pwm set polarity failed, ret %d\n", ret); 378e41f4b71Sopenharmony_ci goto ERR; 379e41f4b71Sopenharmony_ci } 380e41f4b71Sopenharmony_ci 381e41f4b71Sopenharmony_ci ret = PwmGetConfig(handle, &pcfg); // Obtain PWM device parameters. 382e41f4b71Sopenharmony_ci if (ret != HDF_SUCCESS) { 383e41f4b71Sopenharmony_ci HDF_LOGE("PwmGetConfig: get pwm config failed, ret %d\n", ret); 384e41f4b71Sopenharmony_ci goto ERR; 385e41f4b71Sopenharmony_ci } 386e41f4b71Sopenharmony_ci 387e41f4b71Sopenharmony_ci ret = PwmEnable(handle); // Enable the PWM device. 388e41f4b71Sopenharmony_ci if (ret != HDF_SUCCESS) { 389e41f4b71Sopenharmony_ci HDF_LOGE("PwmEnable: enable pwm failed, ret %d\n", ret); 390e41f4b71Sopenharmony_ci goto ERR; 391e41f4b71Sopenharmony_ci } 392e41f4b71Sopenharmony_ci 393e41f4b71Sopenharmony_ci ret = PwmSetConfig(handle, &pcfg); // Set PWM device parameters. 394e41f4b71Sopenharmony_ci if (ret != HDF_SUCCESS) { 395e41f4b71Sopenharmony_ci HDF_LOGE("PwmSetConfig: set pwm config failed, ret %d\n", ret); 396e41f4b71Sopenharmony_ci goto ERR; 397e41f4b71Sopenharmony_ci } 398e41f4b71Sopenharmony_ci 399e41f4b71Sopenharmony_ci ret = PwmDisable(handle); // Disable the PWM device. 400e41f4b71Sopenharmony_ci if (ret != HDF_SUCCESS) { 401e41f4b71Sopenharmony_ci HDF_LOGE("PwmDisable: disable pwm failed, ret %d\n", ret); 402e41f4b71Sopenharmony_ci goto ERR; 403e41f4b71Sopenharmony_ci } 404e41f4b71Sopenharmony_ci 405e41f4b71Sopenharmony_ciERR: 406e41f4b71Sopenharmony_ci PwmClose(handle); // Close the PWM device. 407e41f4b71Sopenharmony_ci return ret; 408e41f4b71Sopenharmony_ci} 409e41f4b71Sopenharmony_ci``` 410