diff --git a/zh-cn/device-dev/driver/Readme-CN.md b/zh-cn/device-dev/driver/Readme-CN.md index 27caa89536b5091ac5a8d7f24a568cc774d93da9..bd5a2ca5fdf9ce7b505833133fd8de0608c8e0fa 100755 --- a/zh-cn/device-dev/driver/Readme-CN.md +++ b/zh-cn/device-dev/driver/Readme-CN.md @@ -27,6 +27,7 @@ - [SPI](driver-platform-spi-des.md) - [UART](driver-platform-uart-des.md) - [WATCHDOG](driver-platform-watchdog-des.md) + - [PWM](driver-platform-pwm-des.md) - [MIPI DSI](driver-platform-mipidsi-des.md) - [外设驱动开发](driver-peripherals.md) - [LCD](driver-peripherals-lcd-des.md) diff --git a/zh-cn/device-dev/driver/driver-platform-pwm-des.md b/zh-cn/device-dev/driver/driver-platform-pwm-des.md new file mode 100755 index 0000000000000000000000000000000000000000..1f490e1b563f2bf84a476b9f89d24367933d5818 --- /dev/null +++ b/zh-cn/device-dev/driver/driver-platform-pwm-des.md @@ -0,0 +1,477 @@ +# PWM + +- [概述](#section1_PWM_des) + - [PwmConfig结构体](#section1.1_PWM_des) + - [接口说明](#section1.2_PWM_des) +- [使用指导](#section2_PWM_des) + - [使用流程](#section2.1_PWM_des) + - [打开PWM设备](#section2.2_PWM_des) + - [关闭PWM设备](#section2.3_PWM_des) + - [使能](#section2.4_PWM_des) + - [禁用](#section2.5_PWM_des) + - [设置PWM设备周期](#section2.6_PWM_des) + - [设置PWM设备占空时间](#section2.7_PWM_des) + - [设置PWM设备极性](#section2.8_PWM_des) + - [设置PWM设备参数](#section2.9_PWM_des) + - [获取PWM设备参数](#section2.10_PWM_des) + +- [使用实例](#section3_PWM_des) + +## 概述 + +- PWM是脉冲宽度调制(Pulse Width Modulation)的缩写,是一种对模拟信号电平进行数字编码,转换为脉冲的一种技术。常用于马达控制、背光亮度调节等。 + +- PWM接口定义了操作PWM设备的通用方法集合,包括: + - PWM设备句柄获取和释放。 + - PWM周期、占空比、极性的设置。 + - PWM使能和关闭。 + - PWM配置信息的获取和设置 + +### PwmConfig结构体 + +**表1** PwmConfig结构体介绍 + + + +| 名称 | 描述 | +| -------- | ------------------------------------------------------------ | +| duty | 占空时间,以纳秒为单位 | +| period | PWM周期,以纳秒为单位 | +| number | 要生成的方波数。正值表示将生成指定数量的方波,0表示方波将不断产生 | +| polarity | 极性:正极性/反极性 | +| status | 状态:启用状态/禁用状态 | + +### 接口说明 + +**表2** PWM设备API接口功能介绍 + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
功能分类接口名描述
PWM句柄操作PwmOpen获取PWM设备驱动句柄
PwmClose释放PWM设备驱动句柄
使能/禁用PWMPwmEnable使能PWM
PwmDisable禁用PWM
PWM配置操作PwmSetPeriod设置PWM周期
PwmSetDuty设置PWM占空时间
PwmSetPolarity设置PWM极性
设置/获取PWM配置信息PwmSetConfig设置PWM配置信息
PwmGetConfig获取PWM配置信息
+ + +>![](W:\doc\docs\zh-cn\device-dev\public_sys-resources\icon-note.gif) **说明:** +>PWM当前仅限内核态使用,不支持在用户态使用。 + +## 使用指导 + +### 使用流程 + +在操作系统启动过程中,驱动管理模块根据配置文件加载PWM驱动,PWM驱动会检测PWM器件并初始化驱动。 + +使用PWM设备的一般流程如[图1](#fig1_PWM_des)所示。 + +**图 1** PWM设备使用流程图 + + +![](figure/PWM使用流程图.png) + +### 打开PWM设备 + +PWM驱动加载成功后,驱动开发者使用驱动框架提供的查询接口并调用PWM设备驱动接口。 + +```c +DevHandle PwmOpen(uint32_t num); +``` + +**表3** PwmOpen参数和返回值描述 + + + +| 参数 | 参数描述 | +| ---------- | ----------------------- | +| void | NA | +| num | PWM设备编号 | +| **返回值** | **返回值描述** | +| handle | 获取成功返回PWM设备句柄 | +| NULL | 获取失败 | + + +```c +uint32_t num = 0; /* PWM设备号 */ +DevHandle handle = NULL; + +/* 获取PWM设备句柄 */ +handle = PwmOpen(num); +if (handle == NULL) { + /* 错误处理 */ +} +``` + +### 关闭PWM设备 + +关闭PWM设备,系统释放对应的资源。 + +```c +void PwmClose(DevHandle handle); +``` + +**表4** PwmClose参数描述 + + + +| 参数 | 参数描述 | +| ------ | ----------- | +| handle | PWM设备句柄 | + + +```c +/* 关闭PWM设备句柄 */ +PwmClose(handle); +``` + +### 使能 + +启用PWM设备。 + +```c +int32_t PwmEnable(DevHandle handle); +``` + +**表5** PwmEnable参数描述 + + + +| 参数 | 参数描述 | +| ---------- | -------------- | +| handle | PWM设备句柄 | +| **返回值** | **返回值描述** | +| 0 | 使能成功 | +| 负数 | 使能失败 | + +```c +int32_t ret; + +/*启用PWM设备*/ +ret = PwmEnable(handle); +if (ret != 0) { + /*错误处理*/ +} +``` + +### 禁用 + +禁用PWM设备。 + +```c +int32_t PwmDisable(DevHandle handle); +``` + +**表6** PwmDisable参数描述 + + + +| 参数 | 参数描述 | +| ---------- | -------------- | +| handle | PWM设备句柄 | +| **返回值** | **返回值描述** | +| 0 | 禁用成功 | +| 负数 | 禁用失败 | + +```c +int32_t ret; + +/*禁用PWM设备*/ +ret = PwmDisable(handle); +if (ret != 0) { + /*错误处理*/ +} +``` + +### 设置PWM设备周期 + +设置PWM设备周期。 + +```c +int32_t PwmSetPeriod(DevHandle handle, uint32_t period); +``` + +**表7** PwmSetPeriod参数描述 + + + +| 参数 | 参数描述 | +| ---------- | ------------------------ | +| handle | PWM设备句柄 | +| period | 要设置的周期,单位为纳秒 | +| **返回值** | **返回值描述** | +| 0 | 设置成功 | +| 负数 | 设置失败 | + +```c +int32_t ret; + +/*设置周期为50000000纳秒*/ +ret = PwmSetPeriod(handle, 50000000); +if (ret != 0) { + /*错误处理*/ +} +``` +### 设置PWM设备占空时间 + +设置PWM设备占空时间。 + +```c +int32_t PwmSetDuty(DevHandle handle, uint32_t duty); +``` + +**表8** PwmSetDuty参数描述 + + + +| 参数 | 参数描述 | +| ---------- | ---------------------------- | +| handle | PWM设备句柄 | +| duty | 要设置的占空时间,单位为纳秒 | +| **返回值** | **返回值描述** | +| 0 | 设置成功 | +| 负数 | 设置失败 | + +```c +int32_t ret; + +/*设置占空时间为25000000纳秒*/ +ret = PwmSetDuty(handle, 25000000); +if (ret != 0) { + /*错误处理*/ +} +``` +### 设置PWM设备极性 + +设置PWM设备极性。 + +```c +int32_t PwmSetPolarity(DevHandle handle, uint8_t polarity); +``` + +**表9** PwmSetPolarity参数描述 + + + +| 参数 | 参数描述 | +| ---------- | ------------------- | +| handle | PWM设备句柄 | +| polarity | 要设置的极性,正/反 | +| **返回值** | **返回值描述** | +| 0 | 设置成功 | +| 负数 | 设置失败 | + +```c +int32_t ret; + +/*设置极性为反*/ +ret = PwmSetPolarity(handle, PWM_INVERTED_POLARITY); +if (ret != 0) { + /*错误处理*/ +} +``` + + +### 设置PWM设备参数 + +设置PWM设备参数。 + +```c +int32_t PwmSetConfig(DevHandle handle, struct PwmConfig *config); +``` + +**表10** PwmSetConfig参数描述 + + + +| 参数 | 参数描述 | +| ---------- | -------------- | +| handle | PWM设备句柄 | +| *config | 参数指针 | +| **返回值** | **返回值描述** | +| 0 | 设置成功 | +| 负数 | 设置失败 | + +```c +int32_t ret; +struct PwmConfig pcfg; +pcfg.duty = 25000000; /*占空时间为25000000纳秒*/ +pcfg.period = 50000000; /*周期为50000000纳秒*/ +pcfg.number = 0; /*不断产生方波*/ +pcfg.polarity = PWM_INVERTED_POLARITY; /*极性为反*/ +pcfg.status = PWM_ENABLE_STATUS; /*运行状态为启用*/ + +/*设置PWM设备参数*/ +ret = PwmSetConfig(handle, &pcfg); +if (ret != 0) { + /*错误处理*/ +} +``` + +### 获取PWM设备参数 + +获取PWM设备参数。 + +```c +int32_t PwmGetConfig(DevHandle handle, struct PwmConfig *config); +``` + +**表11** PwmGetConfig参数描述 + + + +| 参数 | 参数描述 | +| ---------- | -------------- | +| handle | PWM设备句柄 | +| *config | 参数指针 | +| **返回值** | **返回值描述** | +| 0 | 获取成功 | +| 负数 | 获取失败 | + +```c +int32_t ret; +struct PwmConfig pcfg; + +/*获取PWM设备参数*/ +ret = PwmGetConfig(handle, &pcfg); +if (ret != 0) { + /*错误处理*/ +} +``` + +## 使用实例 + +PWM设备完整的使用示例如下所示,首先获取PWM设备句柄,接着使能,设置设备周期、占空时间、极性,设置设备参数,之后获取设备参数,禁用,最后销毁PWM设备句柄。 + +``` +void PwmTestSample(void) +{ + int32_t ret; + uint32_t num; + DevHandle handle = NULL; + + struct PwmConfig pcfg; + pcfg.duty = 20000000; /*占空时间为20000000纳秒*/ + pcfg.period = 40000000; /*周期为40000000纳秒*/ + pcfg.number = 100; /*生成100个方波*/ + pcfg.polarity = PWM_NORMAL_POLARITY; /*极性为正*/ + pcfg.status = PWM_ENABLE_STATUS; /*运行状态为启用*/ + + /* PWM设备编号,要填写实际平台上的编号 */ + num = 1; + + /* 获取PWM设备句柄 */ + handle = PwmOpen(num); + if (handle == NULL) { + HDF_LOGE("PwmOpen: failed!\n"); + return; + } + + /*启用PWM设备*/ + ret = PwmEnable(handle); + if (ret != 0) { + HDF_LOGE("PwmEnable: failed, ret %d\n", ret); + goto _ERR; + } + + /*设置周期为50000000纳秒*/ + ret = PwmSetPeriod(handle, 50000000); + if (ret != 0) { + HDF_LOGE("PwmSetPeriod: failed, ret %d\n", ret); + goto _ERR; + } + + /*设置占空时间为25000000纳秒*/ + ret = PwmSetDuty(handle, 25000000); + if (ret != 0) { + HDF_LOGE("PwmSetDuty: failed, ret %d\n", ret); + goto _ERR; + } + + /*设置极性为反*/ + ret = PwmSetPolarity(handle, PWM_INVERTED_POLARITY); + if (ret != 0) { + HDF_LOGE("PwmSetPolarity: failed, ret %d\n", ret); + goto _ERR; + } + + /*设置PWM设备参数*/ + ret = PwmSetConfig(handle, &pcfg); + if (ret != 0) { + HDF_LOGE("PwmSetConfig: failed, ret %d\n", ret); + goto _ERR; + } + + /*获取PWM设备参数*/ + ret = PwmGetConfig(handle, &pcfg); + if (ret != 0) { + HDF_LOGE("PwmGetConfig: failed, ret %d\n", ret); + goto _ERR; + } + + /*禁用PWM设备*/ + ret = PwmDisable(handle); + if (ret != 0) { + HDF_LOGE("PwmDisable: failed, ret %d\n", ret); + goto _ERR; + } + +_ERR: + /* 销毁PWM设备句柄 */ + PwmClose(handle); +} +``` + diff --git "a/zh-cn/device-dev/driver/figure/PWM\344\275\277\347\224\250\346\265\201\347\250\213\345\233\276.png" "b/zh-cn/device-dev/driver/figure/PWM\344\275\277\347\224\250\346\265\201\347\250\213\345\233\276.png" new file mode 100755 index 0000000000000000000000000000000000000000..b70d8d7d09bc30608529c06175183cf0cc523d41 Binary files /dev/null and "b/zh-cn/device-dev/driver/figure/PWM\344\275\277\347\224\250\346\265\201\347\250\213\345\233\276.png" differ