未验证 提交 a595d964 编写于 作者: O openharmony_ci 提交者: Gitee

!1314 #I4MT5L完成,请审核

Merge pull request !1314 from Annie_wang/PR902
......@@ -11,7 +11,7 @@
- [ADC](driver-platform-adc-develop.md)
- [GPIO](driver-platform-gpio-develop.md)
- [I2C](driver-platform-i2c-develop.md)
- [MIPI-DSI](driver-platform-mipidsi-develop.md)
- [MIPI DSI](driver-platform-mipidsi-develop.md)
- [MMC](driver-platform-mmc-develop.md)
- [PWM](driver-platform-pwm-develop.md)
- [RTC](driver-platform-rtc-develop.md)
......@@ -19,6 +19,7 @@
- [SPI](driver-platform-spi-develop.md)
- [UART](driver-platform-uart-develop.md)
- [Watchdog](driver-platform-watchdog-develop.md)
- [MIPI CSI](driver-platform-mipicsi-develop.md)
- [Platform Driver Usage](driver-platform.md)
- [GPIO](driver-platform-gpio-des.md)
- [I2C](driver-platform-i2c-des.md)
......@@ -29,6 +30,7 @@
- [Watchdog](driver-platform-watchdog-des.md)
- [MIPI DSI](driver-platform-mipidsi-des.md)
- [PWM](driver-platform-pwm-des.md)
- [MIPI CSI](driver-platform-mipicsi-des.md)
- [Peripheral Driver Usage](driver-peripherals.md)
- [LCD](driver-peripherals-lcd-des.md)
- [Touchscreen](driver-peripherals-touch-des.md)
......
......@@ -4,7 +4,7 @@
- [Available APIs](#section12720125432316)
- [Usage Guidelines](#section037231715335)
- [How to Use](#section49299119344)
- [Obtains a MIPI DSI device handle.](#section5126155683811)
- [Obtaining a MIPI DSI Device Handle](#section5126155683811)
- [Setting MIPI DSI Configuration Parameters](#section201164274344)
- [Sending/Receiving the Pointer to a Command](#section199401342173415)
- [Releasing the MIPI DSI Device Handle](#section161011610357)
......@@ -96,9 +96,9 @@
[Figure 2](#fig129103491241) shows the process of using a MIPI DSI device.
**Figure 2** Process of using a MIPI DSI device<a name="fig129103491241"></a>
![](figures/process-of-using-a-mipi-dsi-device.png "process-of-using-a-mipi-dsi-device")
![](figures/process-of-using-a-mipi-dsi-device.png)
### Obtains a MIPI DSI device handle.<a name="section5126155683811"></a>
### Obtaining a MIPI DSI Device Handle<a name="section5126155683811"></a>
Before performing MIPI DSI communication, obtain a MIPI DSI device handle by calling **MipiDsiOpen**. This function returns a MIPI DSI device handle with a specified channel ID.
......@@ -213,7 +213,7 @@ cfg.timingInfo.vsaLines = 76;
cfg.timingInfo.vfpLines = 120;
cfg.timingInfo.xResPixels = 1342;
/* Set MIPI DSI configuration parameters. */
ret = MipiDsiSetCfg(g_handle, &cfg);
ret = MipiDsiSetCfg(mipiDsiHandle, &cfg);
if (ret != 0) {
HDF_LOGE("%s: SetMipiCfg fail! ret=%d\n", __func__, ret);
return -1;
......@@ -265,7 +265,7 @@ int32\_t MipiDsiGetCfg\(DevHandle handle, struct MipiCfg \*cfg\);
int32_t ret;
struct MipiCfg cfg;
memset(&cfg, 0, sizeof(struct MipiCfg));
ret = MipiDsiGetCfg(g_handle, &cfg);
ret = MipiDsiGetCfg(mipiDsiHandle, &cfg);
if (ret != HDF_SUCCESS) {
HDF_LOGE("%s: GetMipiCfg fail!\n", __func__);
return HDF_FAILURE;
......@@ -409,9 +409,9 @@ if (cmdRead->payload == NULL) {
return HDF_FAILURE;
}
*(cmdRead->payload) = DDIC_REG_STATUS;
MipiDsiSetLpMode(g_handle);
ret = MipiDsiRx(g_handle, cmdRead, sizeof(readVal), &readVal);
MipiDsiSetHsMode(g_handle);
MipiDsiSetLpMode(mipiDsiHandle);
ret = MipiDsiRx(mipiDsiHandle, cmdRead, sizeof(readVal), &readVal);
MipiDsiSetHsMode(mipiDsiHandle);
if (ret != HDF_SUCCESS) {
HDF_LOGE("%s: MipiDsiRx fail! ret=%d\n", __func__, ret);
HdfFree(cmdRead->payload);
......@@ -463,13 +463,13 @@ void PalMipiDsiTestSample(void)
{
uint8_t chnId;
int32_t ret;
DevHandle handle = NULL;
DevHandle mipiDsiHandle = NULL;
/* Device channel ID */
chnId = 0;
/* Obtain the MIPI DSI device handle based on a specified channel ID. */
handle = MipiDsiOpen(chnId);
if (handle == NULL) {
mipiDsiHandle = MipiDsiOpen(chnId);
if (mipiDsiHandle == NULL) {
HDF_LOGE("MipiDsiOpen: failed!\n");
return;
}
......@@ -490,7 +490,7 @@ void PalMipiDsiTestSample(void)
cfg.timingInfo.vfpLines = 120;
cfg.timingInfo.xResPixels = 1342;
/* Set MIPI DSI configuration parameters. */
ret = MipiDsiSetCfg(g_handle, &cfg);
ret = MipiDsiSetCfg(mipiDsiHandle, &cfg);
if (ret != 0) {
HDF_LOGE("%s: SetMipiCfg fail! ret=%d\n", __func__, ret);
return;
......@@ -533,9 +533,9 @@ void PalMipiDsiTestSample(void)
return;
}
*(cmdRead->payload) = DDIC_REG_STATUS;
MipiDsiSetLpMode(g_handle);
ret = MipiDsiRx(g_handle, cmdRead, sizeof(readVal), &readVal);
MipiDsiSetHsMode(g_handle);
MipiDsiSetLpMode(mipiDsiHandle);
ret = MipiDsiRx(mipiDsiHandle, cmdRead, sizeof(readVal), &readVal);
MipiDsiSetHsMode(mipiDsiHandle);
if (ret != HDF_SUCCESS) {
HDF_LOGE("%s: MipiDsiRx fail! ret=%d\n", __func__, ret);
HdfFree(cmdRead->payload);
......
# MIPI-DSI<a name="EN-US_TOPIC_0000001199825369"></a>
# MIPI DSI<a name="EN-US_TOPIC_0000001199825369"></a>
- [Overview](#section1266787503161538)
- [How to Develop](#section545182932161538)
......@@ -8,14 +8,14 @@
## Overview<a name="section1266787503161538"></a>
The Display Serial Interface \(DSI\) is a specification developed by the Mobile Industry Processor Interface \(MIPI\) Alliance to reduce the cost of display controllers in mobile devices. In the Hardware Driver Foundation (HDF) framework, the MIPI-DSI module uses the service-free mode for API adaptation. The service-free mode applies to the devices that do not provide user-mode APIs or the OS system that does not distinguish the user mode and the kernel mode. In the service-free mode, **DevHandle** \(a void pointer\) directly points to the kernel-mode address of the device object.
The Display Serial Interface \(DSI\) is a specification developed by the Mobile Industry Processor Interface \(MIPI\) Alliance to reduce the cost of display controllers in mobile devices. In the Hardware Driver Foundation (HDF) framework, the MIPI DSI module uses the service-free mode for API adaptation. The service-free mode applies to the devices that do not provide user-mode APIs or the OS system that does not distinguish the user mode and the kernel mode. In the service-free mode, **DevHandle** \(a void pointer\) directly points to the kernel-mode address of the device object.
**Figure 1** Service-free mode<a name="fig207610236189"></a>
![](figures/service-free-mode-9.png "service-free-mode-9")
![](figures/service-free-mode.png "service-free-mode")
## How to Develop<a name="section545182932161538"></a>
The MIPI-DSI module adaptation involves the following steps:
The MIPI DSI module adaptation involves the following steps:
1. Instantiate the driver entry.
- Instantiate the **HdfDriverEntry** structure.
......@@ -25,7 +25,7 @@ The MIPI-DSI module adaptation involves the following steps:
- Add the **deviceNode** information to the **device\_info.hcs** file.
- \(Optional\) Add the **mipidsi\_config.hcs** file.
3. Instantiate the MIPI-DSI controller object.
3. Instantiate the MIPI DSI controller object.
- Initialize **MipiDsiCntlr**.
- Instantiate **MipiDsiCntlrMethod** in the **MipiDsiCntlr** object.
......@@ -70,7 +70,7 @@ struct MipiDsiCntlrMethod {// Member functions of the core layer structure
</thead>
<tbody><tr id="row318920102175"><td class="cellrowborder" valign="top" width="20%" headers="mcps1.2.6.1.1 "><p id="p01891103178"><a name="p01891103178"></a><a name="p01891103178"></a>setCntlrCfg</p>
</td>
<td class="cellrowborder" valign="top" width="20%" headers="mcps1.2.6.1.2 "><p id="p1518931041716"><a name="p1518931041716"></a><a name="p1518931041716"></a><strong id="b14872821599"><a name="b14872821599"></a><a name="b14872821599"></a>cntlr</strong>: structure pointer to the MIPI-DSI controller.</p>
<td class="cellrowborder" valign="top" width="20%" headers="mcps1.2.6.1.2 "><p id="p1518931041716"><a name="p1518931041716"></a><a name="p1518931041716"></a><strong id="b14872821599"><a name="b14872821599"></a><a name="b14872821599"></a>cntlr</strong>: structure pointer to the MIPI DSI controller.</p>
</td>
<td class="cellrowborder" valign="top" width="20%" headers="mcps1.2.6.1.3 "><p id="p51893103174"><a name="p51893103174"></a><a name="p51893103174"></a></p>
</td>
......@@ -81,7 +81,7 @@ struct MipiDsiCntlrMethod {// Member functions of the core layer structure
</tr>
<tr id="row10189210151713"><td class="cellrowborder" valign="top" width="20%" headers="mcps1.2.6.1.1 "><p id="p518991017171"><a name="p518991017171"></a><a name="p518991017171"></a>setCmd</p>
</td>
<td class="cellrowborder" valign="top" width="20%" headers="mcps1.2.6.1.2 "><p id="p177917412215"><a name="p177917412215"></a><a name="p177917412215"></a><strong id="b4791144113213"><a name="b4791144113213"></a><a name="b4791144113213"></a>cntlr</strong>: structure pointer to the MIPI-DSI controller. </p>
<td class="cellrowborder" valign="top" width="20%" headers="mcps1.2.6.1.2 "><p id="p177917412215"><a name="p177917412215"></a><a name="p177917412215"></a><strong id="b4791144113213"><a name="b4791144113213"></a><a name="b4791144113213"></a>cntlr</strong>: structure pointer to the MIPI DSI controller. </p>
<p id="p12189131018174"><a name="p12189131018174"></a><a name="p12189131018174"></a><strong id="b192414338012"><a name="b192414338012"></a><a name="b192414338012"></a>cmd</strong>: structure pointer to the input instruction.</p>
</td>
<td class="cellrowborder" valign="top" width="20%" headers="mcps1.2.6.1.3 "><p id="p191891210111710"><a name="p191891210111710"></a><a name="p191891210111710"></a></p>
......@@ -93,7 +93,7 @@ struct MipiDsiCntlrMethod {// Member functions of the core layer structure
</tr>
<tr id="row2190161061714"><td class="cellrowborder" valign="top" width="20%" headers="mcps1.2.6.1.1 "><p id="p111901810181715"><a name="p111901810181715"></a><a name="p111901810181715"></a>getCmd</p>
</td>
<td class="cellrowborder" valign="top" width="20%" headers="mcps1.2.6.1.2 "><p id="p121902101179"><a name="p121902101179"></a><a name="p121902101179"></a><strong id="b516432716114"><a name="b516432716114"></a><a name="b516432716114"></a>cntlr</strong>: structure pointer to the MIPI-DSI controller.</p>
<td class="cellrowborder" valign="top" width="20%" headers="mcps1.2.6.1.2 "><p id="p121902101179"><a name="p121902101179"></a><a name="p121902101179"></a><strong id="b516432716114"><a name="b516432716114"></a><a name="b516432716114"></a>cntlr</strong>: structure pointer to the MIPI DSI controller.</p>
</td>
<td class="cellrowborder" valign="top" width="20%" headers="mcps1.2.6.1.3 "><p id="p1519018101171"><a name="p1519018101171"></a><a name="p1519018101171"></a>cmd: structure pointer to the instruction to output.</p>
</td>
......@@ -104,7 +104,7 @@ struct MipiDsiCntlrMethod {// Member functions of the core layer structure
</tr>
<tr id="row1919071016171"><td class="cellrowborder" valign="top" width="20%" headers="mcps1.2.6.1.1 "><p id="p16190131051710"><a name="p16190131051710"></a><a name="p16190131051710"></a>toHs</p>
</td>
<td class="cellrowborder" valign="top" width="20%" headers="mcps1.2.6.1.2 "><p id="p151906102178"><a name="p151906102178"></a><a name="p151906102178"></a><strong id="b159559351014"><a name="b159559351014"></a><a name="b159559351014"></a>cntlr</strong>: structure pointer to the MIPI-DSI controller.</p>
<td class="cellrowborder" valign="top" width="20%" headers="mcps1.2.6.1.2 "><p id="p151906102178"><a name="p151906102178"></a><a name="p151906102178"></a><strong id="b159559351014"><a name="b159559351014"></a><a name="b159559351014"></a>cntlr</strong>: structure pointer to the MIPI DSI controller.</p>
</td>
<td class="cellrowborder" valign="top" width="20%" headers="mcps1.2.6.1.3 "><p id="p10190181031720"><a name="p10190181031720"></a><a name="p10190181031720"></a></p>
</td>
......@@ -115,7 +115,7 @@ struct MipiDsiCntlrMethod {// Member functions of the core layer structure
</tr>
<tr id="row01911510161718"><td class="cellrowborder" valign="top" width="20%" headers="mcps1.2.6.1.1 "><p id="p10191141081717"><a name="p10191141081717"></a><a name="p10191141081717"></a>toLp</p>
</td>
<td class="cellrowborder" valign="top" width="20%" headers="mcps1.2.6.1.2 "><p id="p819121011714"><a name="p819121011714"></a><a name="p819121011714"></a><strong id="b189629351219"><a name="b189629351219"></a><a name="b189629351219"></a>cntlr</strong>: structure pointer to the MIPI-DSI controller.</p>
<td class="cellrowborder" valign="top" width="20%" headers="mcps1.2.6.1.2 "><p id="p819121011714"><a name="p819121011714"></a><a name="p819121011714"></a><strong id="b189629351219"><a name="b189629351219"></a><a name="b189629351219"></a>cntlr</strong>: structure pointer to the MIPI DSI controller.</p>
</td>
<td class="cellrowborder" valign="top" width="20%" headers="mcps1.2.6.1.3 "><p id="p3191510171717"><a name="p3191510171717"></a><a name="p3191510171717"></a></p>
</td>
......@@ -133,7 +133,7 @@ The following uses **mipi\_tx\_hi35xx.c** as an example to present the content
1. Generally, you need to configure the device attributes in **xx\_config.hcs** and add the **deviceNode** information to the **device\_info.hcs** file. The device attribute values are closely related to the default values or value range of the **MipiDsiCntlr** members at the core layer. The **deviceNode** information is related to the driver entry registration.
In this example, no additional attribute needs to be configured for the MIPI-DSI controller. If required, you need to add the **deviceMatchAttr** information to **deviceNode** in the **device\_info** file and add the **mipidsi\_config** file.
In this example, no additional attribute needs to be configured for the MIPI DSI controller. If required, you need to add the **deviceMatchAttr** information to **deviceNode** in the **device\_info** file and add the **mipidsi\_config** file.
- **device\_info.hcs** configuration reference
......@@ -162,7 +162,7 @@ The following uses **mipi\_tx\_hi35xx.c** as an example to present the content
Generally, HDF calls the **Bind** function and then the **Init** function to load a driver. If **Init** fails to be called, HDF calls **Release** to release driver resources and exits.
- MIPI-DSI driver entry reference
- MIPI DSI driver entry reference
```
struct HdfDriverEntry g_mipiTxDriverEntry = {
......@@ -177,7 +177,7 @@ The following uses **mipi\_tx\_hi35xx.c** as an example to present the content
3. Initialize the **MipiDsiCntlr** object at the core layer, including initializing the vendor custom structure \(transferring parameters and data\), instantiating **MipiDsiCntlrMethod** \(used to call underlying functions of the driver\) in **MipiDsiCntlr**, and implementing the **HdfDriverEntry** member functions \(**Bind**, **Init**, and **Release**\).
- Custom structure reference
To the driver, the custom structure carries parameters and data. The values in the **config** file are used to initialize the structure members. In this example, the MIPI-DSI has no device attribute file. Therefore, the basic member structure is similar to that of **MipiDsiCntlr**.
To the driver, the custom structure carries parameters and data. The values in the **config** file are used to initialize the structure members. In this example, the MIPI DSI has no device attribute file. Therefore, the basic member structure is similar to that of **MipiDsiCntlr**.
```
typedef struct {
......
# PWM<a name="EN-US_TOPIC_0000001160812684"></a>
- [Overview](#section1043395117296)
- [Available APIs](#section3939192652418)
- [Usage Guidelines](#section435718267334)
- [How to Use](#section113655616347)
- [Obtaining a PWM Device Handle](#section17816586359)
- [Setting the PWM Period](#section920214812397)
- [Setting the PWM Duty Cycle](#section519712820405)
- [Setting the PWM Polarity](#section12383334115)
- [Enabling the PWM Device](#section382684811414)
- [Disabling the PWM Device](#section16545114404218)
- [Obtaining PWM Device Configuration Parameters](#section117101243144311)
- [Setting PWM Device Configuration Parameters](#section13834163604414)
- [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>
**Table 1** APIs available for the PWM driver
<a name="table1731550155318"></a>
<table><thead align="left"><tr id="row4419501537"><th class="cellrowborder" align="left" valign="top" width="20.857914208579142%" id="mcps1.2.4.1.1"><p id="p641050105320"><a name="p641050105320"></a><a name="p641050105320"></a>Capability</p>
</th>
<th class="cellrowborder" align="left" valign="top" width="23.36766323367663%" id="mcps1.2.4.1.2"><p id="p54150165315"><a name="p54150165315"></a><a name="p54150165315"></a>Function</p>
</th>
<th class="cellrowborder" align="left" valign="top" width="55.77442255774422%" id="mcps1.2.4.1.3"><p id="p941150145313"><a name="p941150145313"></a><a name="p941150145313"></a>Description</p>
</th>
</tr>
</thead>
<tbody><tr id="row1651292212306"><td class="cellrowborder" rowspan="2" valign="top" width="20.857914208579142%" headers="mcps1.2.4.1.1 "><p id="p1387414255305"><a name="p1387414255305"></a><a name="p1387414255305"></a>Obtaining/Releasing device handles</p>
</td>
<td class="cellrowborder" valign="top" width="23.36766323367663%" headers="mcps1.2.4.1.2 "><p id="p8874825143014"><a name="p8874825143014"></a><a name="p8874825143014"></a>PwmOpen</p>
</td>
<td class="cellrowborder" valign="top" width="55.77442255774422%" headers="mcps1.2.4.1.3 "><p id="p1087432513307"><a name="p1087432513307"></a><a name="p1087432513307"></a>Obtains a PWM device handle.</p>
</td>
</tr>
<tr id="row1429083612305"><td class="cellrowborder" valign="top" headers="mcps1.2.4.1.1 "><p id="p1956614106311"><a name="p1956614106311"></a><a name="p1956614106311"></a>PwmClose</p>
</td>
<td class="cellrowborder" valign="top" headers="mcps1.2.4.1.2 "><p id="p829111362306"><a name="p829111362306"></a><a name="p829111362306"></a>Releases a PWM device handle.</p>
</td>
</tr>
<tr id="row34145016535"><td class="cellrowborder" rowspan="3" valign="top" width="20.857914208579142%" headers="mcps1.2.4.1.1 "><p id="p229610227124"><a name="p229610227124"></a><a name="p229610227124"></a>Setting the PWM period, duty cycle, and polarity</p>
</td>
<td class="cellrowborder" valign="top" width="23.36766323367663%" headers="mcps1.2.4.1.2 "><p id="p19389143041518"><a name="p19389143041518"></a><a name="p19389143041518"></a>PwmSetPeriod</p>
</td>
<td class="cellrowborder" valign="top" width="55.77442255774422%" headers="mcps1.2.4.1.3 "><p id="p1360544321811"><a name="p1360544321811"></a><a name="p1360544321811"></a>Sets the PWM period.</p>
</td>
</tr>
<tr id="row5632152611414"><td class="cellrowborder" valign="top" headers="mcps1.2.4.1.1 "><p id="p1392810111722"><a name="p1392810111722"></a><a name="p1392810111722"></a>PwmSetDuty</p>
</td>
<td class="cellrowborder" valign="top" headers="mcps1.2.4.1.2 "><p id="p10448193611188"><a name="p10448193611188"></a><a name="p10448193611188"></a>Sets the PWM duty cycle.</p>
</td>
</tr>
<tr id="row1766145611414"><td class="cellrowborder" valign="top" headers="mcps1.2.4.1.1 "><p id="p10445141911213"><a name="p10445141911213"></a><a name="p10445141911213"></a>PwmSetPolarity</p>
</td>
<td class="cellrowborder" valign="top" headers="mcps1.2.4.1.2 "><p id="p448643019188"><a name="p448643019188"></a><a name="p448643019188"></a>Sets the PWM polarity.</p>
</td>
</tr>
<tr id="row195531331436"><td class="cellrowborder" rowspan="2" valign="top" width="20.857914208579142%" headers="mcps1.2.4.1.1 "><p id="p166091535331"><a name="p166091535331"></a><a name="p166091535331"></a>Enabling/Disabling devices</p>
</td>
<td class="cellrowborder" valign="top" width="23.36766323367663%" headers="mcps1.2.4.1.2 "><p id="p85533315315"><a name="p85533315315"></a><a name="p85533315315"></a>PwmEnable</p>
</td>
<td class="cellrowborder" valign="top" width="55.77442255774422%" headers="mcps1.2.4.1.3 "><p id="p855303118314"><a name="p855303118314"></a><a name="p855303118314"></a>Enables a PWM device.</p>
</td>
</tr>
<tr id="row96141928233"><td class="cellrowborder" valign="top" headers="mcps1.2.4.1.1 "><p id="p66151828330"><a name="p66151828330"></a><a name="p66151828330"></a>PwmDisable</p>
</td>
<td class="cellrowborder" valign="top" headers="mcps1.2.4.1.2 "><p id="p261518281139"><a name="p261518281139"></a><a name="p261518281139"></a>Disables a PWM device.</p>
</td>
</tr>
<tr id="row1020919129159"><td class="cellrowborder" rowspan="2" valign="top" width="20.857914208579142%" headers="mcps1.2.4.1.1 "><p id="p82092126154"><a name="p82092126154"></a><a name="p82092126154"></a>Obtaining/Setting configuration parameters</p>
<p id="p6794153701111"><a name="p6794153701111"></a><a name="p6794153701111"></a></p>
</td>
<td class="cellrowborder" valign="top" width="23.36766323367663%" headers="mcps1.2.4.1.2 "><p id="p1739013012154"><a name="p1739013012154"></a><a name="p1739013012154"></a>PwmSetConfig</p>
</td>
<td class="cellrowborder" valign="top" width="55.77442255774422%" headers="mcps1.2.4.1.3 "><p id="p1152912417189"><a name="p1152912417189"></a><a name="p1152912417189"></a>Sets PWM device configuration parameters.</p>
</td>
</tr>
<tr id="row379443710118"><td class="cellrowborder" valign="top" headers="mcps1.2.4.1.1 "><p id="p4333154919111"><a name="p4333154919111"></a><a name="p4333154919111"></a>PwmGetConfig</p>
</td>
<td class="cellrowborder" valign="top" headers="mcps1.2.4.1.2 "><p id="p19575195601711"><a name="p19575195601711"></a><a name="p19575195601711"></a>Obtains PWM device configuration parameters.</p>
</td>
</tr>
</tbody>
</table>
>![](../public_sys-resources/icon-note.gif) **NOTE:**
>All functions described in this document can be called only in kernel space.
## Usage Guidelines<a name="section435718267334"></a>
### How to Use<a name="section113655616347"></a>
[Figure 1](#fig23885455594) shows the process of using a PWM device.
**Figure 1** Process of using a PWM device<a name="fig23885455594"></a>
![](figures/en-us_image_0000001206372673.png)
### 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.
DevHandle PwmOpen\(uint32\_t num\);
**Table 2** Description of PwmOpen
<a name="table7603619123820"></a>
<table><tbody><tr id="row1060351914386"><td class="cellrowborder" valign="top" width="50%"><p id="p14603181917382"><a name="p14603181917382"></a><a name="p14603181917382"></a><strong id="b8660143618196"><a name="b8660143618196"></a><a name="b8660143618196"></a>Parameter</strong></p>
</td>
<td class="cellrowborder" valign="top" width="50%"><p id="p36031519183819"><a name="p36031519183819"></a><a name="p36031519183819"></a><strong id="b3425537181912"><a name="b3425537181912"></a><a name="b3425537181912"></a>Description</strong></p>
</td>
</tr>
<tr id="row1960431983813"><td class="cellrowborder" valign="top" width="50%"><p id="p1114111239269"><a name="p1114111239269"></a><a name="p1114111239269"></a>num</p>
</td>
<td class="cellrowborder" valign="top" width="50%"><p id="p1560441923818"><a name="p1560441923818"></a><a name="p1560441923818"></a>PWM device number.</p>
</td>
</tr>
<tr id="row380484160"><td class="cellrowborder" valign="top" width="50%"><p id="p460381915385"><a name="p460381915385"></a><a name="p460381915385"></a><strong id="b38961340181918"><a name="b38961340181918"></a><a name="b38961340181918"></a>Return Value</strong></p>
</td>
<td class="cellrowborder" valign="top" width="50%"><p id="p96031619153812"><a name="p96031619153812"></a><a name="p96031619153812"></a><strong id="b1273144114193"><a name="b1273144114193"></a><a name="b1273144114193"></a>Description</strong></p>
</td>
</tr>
<tr id="row5793818161"><td class="cellrowborder" valign="top" width="50%"><p id="p1060418195389"><a name="p1060418195389"></a><a name="p1060418195389"></a>NULL</p>
</td>
<td class="cellrowborder" valign="top" width="50%"><p id="p760471912388"><a name="p760471912388"></a><a name="p760471912388"></a>Failed to obtain the PWM device handle.</p>
</td>
</tr>
<tr id="row187914871618"><td class="cellrowborder" valign="top" width="50%"><p id="p5604719133811"><a name="p5604719133811"></a><a name="p5604719133811"></a>Device handle</p>
</td>
<td class="cellrowborder" valign="top" width="50%"><p id="p3604181933818"><a name="p3604181933818"></a><a name="p3604181933818"></a>PWM device handle.</p>
</td>
</tr>
</tbody>
</table>
The following example shows how to obtain a PWM device handle with the device number **0**:
# PWM<a name="title_PWM_des"></a>
- [Overview](#section1_PWM_des)
- [PwmConfig Structure](#section1.1_PWM_des)
- [Available APIs](#section2_PWM_des)
- [Usage Guidelines](#section3_PWM_des)
- [How to Use](#section3.1_PWM_des)
- [Opening a PWM Device Handle](#section3.2_PWM_des)
- [Closing a PWM Device Handle](#section3.3_PWM_des)
- [Enabling a PWM Device](#section3.4_PWM_des)
- [Disabling a PWM Device](#section3.5_PWM_des)
- [Setting the PWM Period](#section3.6_PWM_des)
- [Setting the PWM Signal ON-State Time](#section3.7_PWM_des)
- [Setting the PWM Polarity](#section3.8_PWM_des)
- [Setting PWM Device Parameters](#section3.9_PWM_des)
- [Obtaining PWM Device Parameters](#section3.10_PWM_des)
- [Usage Example](#section3_PWM_des)
## 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">
<colgroup><col class="xl65" width="300" style="mso-width-source:userset;mso-width-alt:13728;
width:300pt">
<col width="250" style="mso-width-source:userset;mso-width-alt:13216;width:250pt">
<col width="300" style="mso-width-source:userset;mso-width-alt:28352;width:300pt">
</colgroup><tbody><tr height="19" style="height:14.25pt">
<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>
<!--[if supportMisalignedColumns]-->
<tr height="0" style="display:none">
<td width="429" style="width:322pt"></td>
<td width="413" style="width:310pt"></td>
<td width="886" style="width:665pt"></td>
</tr>
<!--[endif]-->
</tbody></table>
>![](W:\doc\docs\en\device-dev\public_sys-resources\icon-note.gif) **NOTE:**
>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);
```
uint32_t num = 0; /* PWM device number */
DevHandle pwm = NULL; /* PWM device handle */
**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;
/* 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>
int32\_t PwmSetPeriod\(DevHandle handle, uint32\_t period\);
**Table 3** Description of PwmSetPeriod
<a name="table16124612193214"></a>
<table><tbody><tr id="row4124212123217"><td class="cellrowborder" valign="top" width="50%"><p id="p11124131243218"><a name="p11124131243218"></a><a name="p11124131243218"></a><strong id="b1857203711910"><a name="b1857203711910"></a><a name="b1857203711910"></a>Parameter</strong></p>
</td>
<td class="cellrowborder" valign="top" width="50%"><p id="p14124191263210"><a name="p14124191263210"></a><a name="p14124191263210"></a><strong id="b17874123771911"><a name="b17874123771911"></a><a name="b17874123771911"></a>Description</strong></p>
</td>
</tr>
<tr id="row111241012133210"><td class="cellrowborder" valign="top" width="50%"><p id="p18124201220324"><a name="p18124201220324"></a><a name="p18124201220324"></a>handle</p>
</td>
<td class="cellrowborder" valign="top" width="50%"><p id="p20124191210321"><a name="p20124191210321"></a><a name="p20124191210321"></a>PWM device handle.</p>
</td>
</tr>
<tr id="row151241112163216"><td class="cellrowborder" valign="top" width="50%"><p id="p712481223212"><a name="p712481223212"></a><a name="p712481223212"></a>period</p>
</td>
<td class="cellrowborder" valign="top" width="50%"><p id="p1312411126320"><a name="p1312411126320"></a><a name="p1312411126320"></a>Period, in nanoseconds.</p>
</td>
</tr>
<tr id="row712471214320"><td class="cellrowborder" valign="top" width="50%"><p id="p1612421233217"><a name="p1612421233217"></a><a name="p1612421233217"></a><strong id="b13153419197"><a name="b13153419197"></a><a name="b13153419197"></a>Return Value</strong></p>
</td>
<td class="cellrowborder" valign="top" width="50%"><p id="p1712591211327"><a name="p1712591211327"></a><a name="p1712591211327"></a><strong id="b79834217190"><a name="b79834217190"></a><a name="b79834217190"></a>Description</strong></p>
</td>
</tr>
<tr id="row812581214326"><td class="cellrowborder" valign="top" width="50%"><p id="p13125812183214"><a name="p13125812183214"></a><a name="p13125812183214"></a>0</p>
</td>
<td class="cellrowborder" valign="top" width="50%"><p id="p15125812193212"><a name="p15125812193212"></a><a name="p15125812193212"></a>Succeeded in setting the PWM period.</p>
</td>
</tr>
<tr id="row2012571273216"><td class="cellrowborder" valign="top" width="50%"><p id="p17125181215327"><a name="p17125181215327"></a><a name="p17125181215327"></a>Negative value</p>
</td>
<td class="cellrowborder" valign="top" width="50%"><p id="p81251512143217"><a name="p81251512143217"></a><a name="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
void PwmClose(DevHandle handle);
```
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
<a name="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>
int32\_t PwmSetDuty\(DevHandle handle, uint32\_t duty\);
**Table 4** Description of PwmSetDuty
<a name="table2010731215368"></a>
<table><tbody><tr id="row51071812163614"><td class="cellrowborder" valign="top" width="50%"><p id="p21071412123617"><a name="p21071412123617"></a><a name="p21071412123617"></a><strong id="b1459173719195"><a name="b1459173719195"></a><a name="b1459173719195"></a>Parameter</strong></p>
</td>
<td class="cellrowborder" valign="top" width="50%"><p id="p910715125363"><a name="p910715125363"></a><a name="p910715125363"></a><strong id="b16877113781915"><a name="b16877113781915"></a><a name="b16877113781915"></a>Description</strong></p>
</td>
</tr>
<tr id="row5107912193611"><td class="cellrowborder" valign="top" width="50%"><p id="p15107141223618"><a name="p15107141223618"></a><a name="p15107141223618"></a>handle</p>
</td>
<td class="cellrowborder" valign="top" width="50%"><p id="p16107151293612"><a name="p16107151293612"></a><a name="p16107151293612"></a>PWM device handle.</p>
</td>
</tr>
<tr id="row131077122364"><td class="cellrowborder" valign="top" width="50%"><p id="p4107161218361"><a name="p4107161218361"></a><a name="p4107161218361"></a>duty</p>
</td>
<td class="cellrowborder" valign="top" width="50%"><p id="p13107201263620"><a name="p13107201263620"></a><a name="p13107201263620"></a>Duty cycle, in nanoseconds.</p>
</td>
</tr>
<tr id="row161071512143618"><td class="cellrowborder" valign="top" width="50%"><p id="p01071012123617"><a name="p01071012123617"></a><a name="p01071012123617"></a><strong id="b7318164151915"><a name="b7318164151915"></a><a name="b7318164151915"></a>Return Value</strong></p>
</td>
<td class="cellrowborder" valign="top" width="50%"><p id="p10107111273619"><a name="p10107111273619"></a><a name="p10107111273619"></a><strong id="b181001442121915"><a name="b181001442121915"></a><a name="b181001442121915"></a>Description</strong></p>
</td>
</tr>
<tr id="row15108101217363"><td class="cellrowborder" valign="top" width="50%"><p id="p1110811263616"><a name="p1110811263616"></a><a name="p1110811263616"></a>0</p>
</td>
<td class="cellrowborder" valign="top" width="50%"><p id="p61081712203612"><a name="p61081712203612"></a><a name="p61081712203612"></a>Succeeded in setting the duty cycle.</p>
</td>
</tr>
<tr id="row171081012183618"><td class="cellrowborder" valign="top" width="50%"><p id="p610816120365"><a name="p610816120365"></a><a name="p610816120365"></a>Negative value</p>
</td>
<td class="cellrowborder" valign="top" width="50%"><p id="p10108012143617"><a name="p10108012143617"></a><a name="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_t PwmEnable(DevHandle handle);
```
**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
int32_t ret;
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>
int32\_t PwmSetPolarity\(DevHandle handle, uint8\_t polarity\);
**Table 5** Description of PwmSetPolarity
<a name="table141853773813"></a>
<table><tbody><tr id="row6418837183810"><td class="cellrowborder" valign="top" width="50%"><p id="p14181637103817"><a name="p14181637103817"></a><a name="p14181637103817"></a><strong id="b16133714199"><a name="b16133714199"></a><a name="b16133714199"></a>Parameter</strong></p>
</td>
<td class="cellrowborder" valign="top" width="50%"><p id="p74181375386"><a name="p74181375386"></a><a name="p74181375386"></a><strong id="b1187918371191"><a name="b1187918371191"></a><a name="b1187918371191"></a>Description</strong></p>
</td>
</tr>
<tr id="row84181437163812"><td class="cellrowborder" valign="top" width="50%"><p id="p1141823715384"><a name="p1141823715384"></a><a name="p1141823715384"></a>handle</p>
</td>
<td class="cellrowborder" valign="top" width="50%"><p id="p7418143718385"><a name="p7418143718385"></a><a name="p7418143718385"></a>PWM device handle.</p>
</td>
</tr>
<tr id="row5418123711387"><td class="cellrowborder" valign="top" width="50%"><p id="p241893773817"><a name="p241893773817"></a><a name="p241893773817"></a>polarity</p>
</td>
<td class="cellrowborder" valign="top" width="50%"><p id="p184181937123811"><a name="p184181937123811"></a><a name="p184181937123811"></a>Polarity, which can be <strong id="b31772419213"><a name="b31772419213"></a><a name="b31772419213"></a>PWM_NORMAL_POLARITY</strong> or <strong id="b31771840212"><a name="b31771840212"></a><a name="b31771840212"></a>PWM_INVERTED_POLARITY</strong>.</p>
</td>
</tr>
<tr id="row841883773819"><td class="cellrowborder" valign="top" width="50%"><p id="p11418193763820"><a name="p11418193763820"></a><a name="p11418193763820"></a><strong id="b17320164117192"><a name="b17320164117192"></a><a name="b17320164117192"></a>Return Value</strong></p>
</td>
<td class="cellrowborder" valign="top" width="50%"><p id="p164185378387"><a name="p164185378387"></a><a name="p164185378387"></a><strong id="b61021427199"><a name="b61021427199"></a><a name="b61021427199"></a>Description</strong></p>
</td>
</tr>
<tr id="row1741919372382"><td class="cellrowborder" valign="top" width="50%"><p id="p2419103718389"><a name="p2419103718389"></a><a name="p2419103718389"></a>0</p>
</td>
<td class="cellrowborder" valign="top" width="50%"><p id="p241993723818"><a name="p241993723818"></a><a name="p241993723818"></a>Succeed in setting the polarity.</p>
</td>
</tr>
<tr id="row1441983711384"><td class="cellrowborder" valign="top" width="50%"><p id="p1441903793817"><a name="p1441903793817"></a><a name="p1441903793817"></a>Negative value</p>
</td>
<td class="cellrowborder" valign="top" width="50%"><p id="p84191237193817"><a name="p84191237193817"></a><a name="p84191237193817"></a>Failed to set the polarity.</p>
</td>
</tr>
</tbody>
</table>
### Disabling a PWM Device<a name="section3.5_PWM_des"></a>
Disable a PWM device.
```c
int32_t PwmDisable(DevHandle handle);
```
**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
int32_t ret;
uint8_t polarity = PWM_INVERTED_POLARITY; /* Inverted polarity */
ret = PwmSetPolarity(pwm, polarity); /* Set PWM polarity to inverted polarity. */
/* Disable a PWM device. */
ret = PwmDisable(handle);
if (ret != 0) {
HDF_LOGE("PwmSetPolarity: failed, ret %d", ret);
/* Error handling. */
}
```
### Enabling the PWM Device<a name="section382684811414"></a>
int32\_t PwmEnable\(DevHandle handle\);
**Table 6** Description of PwmEnable
<a name="table785385210447"></a>
<table><tbody><tr id="row9853185234417"><td class="cellrowborder" valign="top" width="50%"><p id="p7853195215444"><a name="p7853195215444"></a><a name="p7853195215444"></a><strong id="b36343761918"><a name="b36343761918"></a><a name="b36343761918"></a>Parameter</strong></p>
</td>
<td class="cellrowborder" valign="top" width="50%"><p id="p4853352104417"><a name="p4853352104417"></a><a name="p4853352104417"></a><strong id="b11881837131919"><a name="b11881837131919"></a><a name="b11881837131919"></a>Description</strong></p>
</td>
</tr>
<tr id="row6853165254419"><td class="cellrowborder" valign="top" width="50%"><p id="p085313525446"><a name="p085313525446"></a><a name="p085313525446"></a>handle</p>
</td>
<td class="cellrowborder" valign="top" width="50%"><p id="p185319529444"><a name="p185319529444"></a><a name="p185319529444"></a>PWM device handle.</p>
</td>
</tr>
<tr id="row585313527443"><td class="cellrowborder" valign="top" width="50%"><p id="p15853652174416"><a name="p15853652174416"></a><a name="p15853652174416"></a><strong id="b16323144113194"><a name="b16323144113194"></a><a name="b16323144113194"></a>Return Value</strong></p>
</td>
<td class="cellrowborder" valign="top" width="50%"><p id="p1385310525444"><a name="p1385310525444"></a><a name="p1385310525444"></a><strong id="b8104184291915"><a name="b8104184291915"></a><a name="b8104184291915"></a>Description</strong></p>
</td>
</tr>
<tr id="row9854252134410"><td class="cellrowborder" valign="top" width="50%"><p id="p13854155214416"><a name="p13854155214416"></a><a name="p13854155214416"></a>0</p>
</td>
<td class="cellrowborder" valign="top" width="50%"><p id="p6854452114416"><a name="p6854452114416"></a><a name="p6854452114416"></a>Succeeded in enabling the PWM device.</p>
</td>
</tr>
<tr id="row58545526443"><td class="cellrowborder" valign="top" width="50%"><p id="p1085412522449"><a name="p1085412522449"></a><a name="p1085412522449"></a>Negative value</p>
</td>
<td class="cellrowborder" valign="top" width="50%"><p id="p2854052124416"><a name="p2854052124416"></a><a name="p2854052124416"></a>Failed to enable the PWM device.</p>
</td>
</tr>
</tbody>
</table>
### Setting the PWM Period<a name="section3.6_PWM_des"></a>
Set the PWM period.
```c
int32_t PwmSetPeriod(DevHandle handle, uint32_t period);
```
**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
int32_t ret;
ret = PwmEnable(pwm); /* Enable the PWM device. */
/* Set the PWM period to 50000000 ns.*/
ret = PwmSetPeriod(handle, 50000000);
if (ret != 0) {
HDF_LOGE("PwmEnable: failed, ret %d", ret);
/* Error handling. */
}
```
### Setting the PWM Signal ON-State Time<a name="section3.7_PWM_des"></a>
### Disabling the PWM Device<a name="section16545114404218"></a>
int32\_t PwmDisable\(DevHandle handle\);
**Table 7** Description of PwmDisable
<a name="table1354973912475"></a>
<table><tbody><tr id="row115499392478"><td class="cellrowborder" valign="top" width="50%"><p id="p1554993912474"><a name="p1554993912474"></a><a name="p1554993912474"></a><strong id="b56503721918"><a name="b56503721918"></a><a name="b56503721918"></a>Parameter</strong></p>
</td>
<td class="cellrowborder" valign="top" width="50%"><p id="p055083915471"><a name="p055083915471"></a><a name="p055083915471"></a><strong id="b088343710192"><a name="b088343710192"></a><a name="b088343710192"></a>Description</strong></p>
</td>
</tr>
<tr id="row1855093974715"><td class="cellrowborder" valign="top" width="50%"><p id="p2550739114717"><a name="p2550739114717"></a><a name="p2550739114717"></a>handle</p>
</td>
<td class="cellrowborder" valign="top" width="50%"><p id="p1755018397479"><a name="p1755018397479"></a><a name="p1755018397479"></a>PWM device handle.</p>
</td>
</tr>
<tr id="row355043910472"><td class="cellrowborder" valign="top" width="50%"><p id="p1655053984714"><a name="p1655053984714"></a><a name="p1655053984714"></a><strong id="b16327041171912"><a name="b16327041171912"></a><a name="b16327041171912"></a>Return Value</strong></p>
</td>
<td class="cellrowborder" valign="top" width="50%"><p id="p16550439154720"><a name="p16550439154720"></a><a name="p16550439154720"></a><strong id="b101057422194"><a name="b101057422194"></a><a name="b101057422194"></a>Description</strong></p>
</td>
</tr>
<tr id="row25507391479"><td class="cellrowborder" valign="top" width="50%"><p id="p1555014394473"><a name="p1555014394473"></a><a name="p1555014394473"></a>0</p>
</td>
<td class="cellrowborder" valign="top" width="50%"><p id="p15550839174713"><a name="p15550839174713"></a><a name="p15550839174713"></a>Succeeded in disabling the PWM device.</p>
</td>
</tr>
<tr id="row1155093913470"><td class="cellrowborder" valign="top" width="50%"><p id="p1655015398476"><a name="p1655015398476"></a><a name="p1655015398476"></a>Negative value</p>
</td>
<td class="cellrowborder" valign="top" width="50%"><p id="p4550143919471"><a name="p4550143919471"></a><a name="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_t PwmSetDuty(DevHandle handle, uint32_t duty);
```
**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
int32_t ret;
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>
### Obtaining PWM Device Configuration Parameters<a name="section117101243144311"></a>
int32\_t PwmGetConfig\(DevHandle handle, struct PwmConfig \*config\);
**Table 8** Description of PwmGetConfig
<a name="table14209152141313"></a>
<table><tbody><tr id="row1420918529133"><td class="cellrowborder" valign="top" width="50%"><p id="p42091852141314"><a name="p42091852141314"></a><a name="p42091852141314"></a><strong id="b1866173710197"><a name="b1866173710197"></a><a name="b1866173710197"></a>Parameter</strong></p>
</td>
<td class="cellrowborder" valign="top" width="50%"><p id="p202099523137"><a name="p202099523137"></a><a name="p202099523137"></a><strong id="b19885123791911"><a name="b19885123791911"></a><a name="b19885123791911"></a>Description</strong></p>
</td>
</tr>
<tr id="row142091352171310"><td class="cellrowborder" valign="top" width="50%"><p id="p1520915529131"><a name="p1520915529131"></a><a name="p1520915529131"></a>handle</p>
</td>
<td class="cellrowborder" valign="top" width="50%"><p id="p720995291310"><a name="p720995291310"></a><a name="p720995291310"></a>PWM device handle.</p>
</td>
</tr>
<tr id="row6209152161314"><td class="cellrowborder" valign="top" width="50%"><p id="p720916522139"><a name="p720916522139"></a><a name="p720916522139"></a>config</p>
</td>
<td class="cellrowborder" valign="top" width="50%"><p id="p172091452131319"><a name="p172091452131319"></a><a name="p172091452131319"></a>Pointer to the <strong id="b1844333918213"><a name="b1844333918213"></a><a name="b1844333918213"></a>PwmConfig</strong> structure containing PWM device configuration parameters.</p>
</td>
</tr>
<tr id="row12092522139"><td class="cellrowborder" valign="top" width="50%"><p id="p18209125211134"><a name="p18209125211134"></a><a name="p18209125211134"></a><strong id="b4330164111912"><a name="b4330164111912"></a><a name="b4330164111912"></a>Return Value</strong></p>
</td>
<td class="cellrowborder" valign="top" width="50%"><p id="p420975231318"><a name="p420975231318"></a><a name="p420975231318"></a><strong id="b16107114241910"><a name="b16107114241910"></a><a name="b16107114241910"></a>Description</strong></p>
</td>
</tr>
<tr id="row8209155251310"><td class="cellrowborder" valign="top" width="50%"><p id="p13210145291312"><a name="p13210145291312"></a><a name="p13210145291312"></a>0</p>
</td>
<td class="cellrowborder" valign="top" width="50%"><p id="p152101952141315"><a name="p152101952141315"></a><a name="p152101952141315"></a>Succeeded in obtaining PWM device configuration parameters.</p>
</td>
</tr>
<tr id="row102101452121320"><td class="cellrowborder" valign="top" width="50%"><p id="p10210175219134"><a name="p10210175219134"></a><a name="p10210175219134"></a>Negative value</p>
</td>
<td class="cellrowborder" valign="top" width="50%"><p id="p72101252101312"><a name="p72101252101312"></a><a name="p72101252101312"></a>Failed to obtain PWM device configuration parameters.</p>
</td>
</tr>
</tbody>
</table>
Set the signal polarity for a PWM device.
```c
int32_t PwmSetPolarity(DevHandle handle, uint8_t polarity);
```
**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
int32_t ret;
struct PwmConfig config= {0}; /* PWM configuration parameters */
ret = PwmGetConfig(pwm, &config); /* Obtain PWM device configuration parameters. */
/* Set the PWM polarity to PWM_INVERTED_POLARITY. */
ret = PwmSetPolarity(handle, PWM_INVERTED_POLARITY);
if (ret != 0) {
HDF_LOGE("PwmGetConfig: failed, ret %d", ret);
/* Error handling. */
}
```
### Setting PWM Device Configuration Parameters<a name="section13834163604414"></a>
int32\_t PwmSetConfig\(DevHandle handle, struct PwmConfig \*config\);
**Table 9** Description of PwmSetConfig
<a name="table1836117542321"></a>
<table><tbody><tr id="row20361165453211"><td class="cellrowborder" valign="top" width="50%"><p id="p1736135418323"><a name="p1736135418323"></a><a name="p1736135418323"></a><strong id="b11682037161918"><a name="b11682037161918"></a><a name="b11682037161918"></a>Parameter</strong></p>
</td>
<td class="cellrowborder" valign="top" width="50%"><p id="p13361954103214"><a name="p13361954103214"></a><a name="p13361954103214"></a><strong id="b18888173791918"><a name="b18888173791918"></a><a name="b18888173791918"></a>Description</strong></p>
</td>
</tr>
<tr id="row336185416328"><td class="cellrowborder" valign="top" width="50%"><p id="p3361454153218"><a name="p3361454153218"></a><a name="p3361454153218"></a>handle</p>
</td>
<td class="cellrowborder" valign="top" width="50%"><p id="p836175418328"><a name="p836175418328"></a><a name="p836175418328"></a>PWM device handle.</p>
</td>
</tr>
<tr id="row18361135411322"><td class="cellrowborder" valign="top" width="50%"><p id="p2036175403217"><a name="p2036175403217"></a><a name="p2036175403217"></a>config</p>
</td>
<td class="cellrowborder" valign="top" width="50%"><p id="p1736120541323"><a name="p1736120541323"></a><a name="p1736120541323"></a>Pointer to the <strong id="b886814395214"><a name="b886814395214"></a><a name="b886814395214"></a>PwmConfig</strong> structure containing PWM device configuration parameters.</p>
</td>
</tr>
<tr id="row2361254133217"><td class="cellrowborder" valign="top" width="50%"><p id="p1636295463212"><a name="p1636295463212"></a><a name="p1636295463212"></a><strong id="b0333141131914"><a name="b0333141131914"></a><a name="b0333141131914"></a>Return Value</strong></p>
</td>
<td class="cellrowborder" valign="top" width="50%"><p id="p173621854153217"><a name="p173621854153217"></a><a name="p173621854153217"></a><strong id="b210914211914"><a name="b210914211914"></a><a name="b210914211914"></a>Description</strong></p>
</td>
</tr>
<tr id="row73628543328"><td class="cellrowborder" valign="top" width="50%"><p id="p15362205417325"><a name="p15362205417325"></a><a name="p15362205417325"></a>0</p>
</td>
<td class="cellrowborder" valign="top" width="50%"><p id="p436285433211"><a name="p436285433211"></a><a name="p436285433211"></a>Succeeded in setting PWM configuration parameters.</p>
</td>
</tr>
<tr id="row23621154153220"><td class="cellrowborder" valign="top" width="50%"><p id="p13362954113214"><a name="p13362954113214"></a><a name="p13362954113214"></a>Negative value</p>
</td>
<td class="cellrowborder" valign="top" width="50%"><p id="p83629546325"><a name="p83629546325"></a><a name="p83629546325"></a>Failed to set PWM configuration parameters.</p>
</td>
</tr>
</tbody>
</table>
### Setting PWM Device Parameters<a name="section3.9_PWM_des"></a>
Set PWM device parameters.
```c
int32_t PwmSetConfig(DevHandle handle, struct PwmConfig *config);
```
**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
int32_t ret;
struct PwmConfig config= {0}; /* PWM configuration parameters */
config.duty = 500; /* Duty cycle: 500 ns */
config.period = 1000; /* Period: 1000 ns */
config.number = 0; /* Continuous output of square waves */
config.polarity = PWM_NORMAL_POLARITY; /* Normal polarity */
ret = PwmSetConfig(pwm, &config); /* Set PWM device configuration parameters. */
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);
if (ret != 0) {
HDF_LOGE("PwmSetConfig: failed, ret %d\n", ret);
/* Error handling. */
}
```
### Releasing the PWM Device Handle<a name="section12987111511450"></a>
### Obtaining PWM Device Parameters<a name="section3.10_PWM_des"></a>
void PwmClose\(DevHandle handle\);
Obtain PWM device parameters.
This function will release the resources obtained by calling **PwmClose**.
```c
int32_t PwmGetConfig(DevHandle handle, struct PwmConfig *config);
```
**Table 10** Description of PwmClose
**Table 11** PwmGetConfig
<a name="table72517953115"></a>
<table><tbody><tr id="row1525793312"><td class="cellrowborder" valign="top" width="50%"><p id="p115402031153111"><a name="p115402031153111"></a><a name="p115402031153111"></a><strong id="b1669123714198"><a name="b1669123714198"></a><a name="b1669123714198"></a>Parameter</strong></p>
</td>
<td class="cellrowborder" valign="top" width="50%"><p id="p65406313319"><a name="p65406313319"></a><a name="p65406313319"></a><strong id="b8890113710195"><a name="b8890113710195"></a><a name="b8890113710195"></a>Description</strong></p>
</td>
</tr>
<tr id="row1926109193116"><td class="cellrowborder" valign="top" width="50%"><p id="p105419317318"><a name="p105419317318"></a><a name="p105419317318"></a>handle</p>
</td>
<td class="cellrowborder" valign="top" width="50%"><p id="p16541153110317"><a name="p16541153110317"></a><a name="p16541153110317"></a>PWM device handle.</p>
</td>
</tr>
</tbody>
</table>
<a name="table11_PWM_des"></a>
```
PwmClose(pwm); /* Release the PWM device handle */
| 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. */
}
```
## Usage Example<a name="section138636719469"></a>
## Usage Example<a name="section4_PWM_des"></a>
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.
```
#include "hdf_log.h"
#include "osal_time.h"
#include "pwm_if.h"
void PwmTestSample(void)
{
int32_t ret;
struct PwmConfig config; /* PWM configuration parameters */
DevHandle pwm = NULL; /* PWM device handle */
pwm = PwmOpen(0); /* Obtain the PWM device handle. */
if (pwm == NULL) {
HDF_LOGE("PwmOpen: pwm0 failed");
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");
return;
}
/* Obtain the PWM device configuration parameters. */
ret = PwmGetConfig(pwm, &config);
/* Set the PWM period to 50000000 ns.*/
ret = PwmSetPeriod(handle, 50000000);
if (ret != 0) {
HDF_LOGE("PwmGetConfig: failed, ret %d\n", ret);
goto err;
HDF_LOGE("PwmSetPeriod: failed, ret %d\n", ret);
goto _ERR;
}
config.duty = 500; /* Duty cycle: 500 ns */
config.period = 1000; /* Period: 1000 ns */
/* Set the PWM device configuration parameters. */
ret = PwmSetConfig(pwm, &config);
/* Set the signal ON-state time to 25000000 ns. */
ret = PwmSetDuty(handle, 25000000);
if (ret != 0) {
HDF_LOGE("PwmSetConfig: failed, ret %d\n", ret);
goto err;
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;
}
/* Obtain PWM device parameters. */
ret = PwmGetConfig(handle, &pcfg);
if (ret != 0) {
HDF_LOGE("PwmGetConfig: failed, ret %d\n", ret);
goto _ERR;
}
/* Enable the PWM device. */
ret = PwmEnable(pwm);
ret = PwmEnable(handle);
if (ret != 0) {
HDF_LOGE("PwmEnable: failed, ret %d\n", ret);
goto err;
HDF_LOGE("PwmEnable: failed, ret %d\n", ret);
goto _ERR;
}
/* Sleep for 10 seconds */
OsalSleep(10);
/* Set PWM device parameters. */
ret = PwmSetConfig(handle, &pcfg);
if (ret != 0) {
HDF_LOGE("PwmSetConfig: failed, ret %d\n", ret);
goto _ERR;
}
/* Disable the PWM device. */
ret = PwmDisable(pwm);
ret = PwmDisable(handle);
if (ret != 0) {
HDF_LOGE("PwmDisable: failed, ret %d\n", ret);
goto err;
goto _ERR;
}
err:
/* Release the PWM device handle. */
PwmClose(pwm);
_ERR:
/* Close the PWM device handle. */
PwmClose(handle);
}
```
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册