PWM/zh

From FriendlyELEC WiKi
Revision as of 03:50, 6 January 2022 by Tzs (Talk | contribs) (updated by API)

Jump to: navigation, search

English

1 H3上配置SYS_PWM

1.1 先确定内核已经打了这个补丁

https://github.com/torvalds/linux/commit/179a79fd740b6b2f66b64bae5cb6ecd483987d20

注: FriendlyELEC的内核源代码分支 https://github.com/friendlyarm/linux/tree/sunxi-4.14.y 已包含此提交。

1.2 修改内核dts配置, 启用SYS_PWM (在NanoPi NEO上, 引脚与POWER LED复用)

参考以下补丁, 配置POWER LED对应的引脚为PWM功能,然后编译并测试:

diff --git a/arch/arm/boot/dts/sun8i-h3-nanopi-neo.dts b/arch/arm/boot/dts/sun8i-h3-nanopi-neo.dts
index b9826c150c0a..ca62502ad9a3 100644
--- a/arch/arm/boot/dts/sun8i-h3-nanopi-neo.dts
+++ b/arch/arm/boot/dts/sun8i-h3-nanopi-neo.dts
@@ -45,6 +45,10 @@
 / {
        model = "FriendlyElec NanoPi-NEO";
        compatible = "friendlyelec,nanopi-neo", "allwinner,sun8i-h3";
+
+       aliases {
+               pwm1 = &r_pwm;
+       };
 };
 
 &emac {
@@ -52,4 +56,14 @@
        phy-mode = "mii";
        allwinner,leds-active-low;
        status = "okay";
-};
\ No newline at end of file
+};
+
+&gpio_leds {
+       pinctrl-0 = <&leds_npi>;
+
+       /delete-node/ pwr;
+};
+
+&r_pwm {
+       status = "okay";
+};
diff --git a/arch/arm/boot/dts/sun8i-h3-nanopi.dtsi b/arch/arm/boot/dts/sun8i-h3-nanopi.dtsi
index 5eeb41744735..1802f8ca2c56 100644
--- a/arch/arm/boot/dts/sun8i-h3-nanopi.dtsi
+++ b/arch/arm/boot/dts/sun8i-h3-nanopi.dtsi
@@ -89,7 +89,7 @@
                };
        };
 
-       leds {
+       gpio_leds: leds {
                compatible = "gpio-leds";
                pinctrl-names = "default";
                pinctrl-0 = <&leds_npi>, <&leds_r_npi>;

1.3 测试脚本

在NanoPi NEO上测试, 可看到POWER LED以淡出效果闪烁10次:

#!/bin/bash
 
sys_pwm=/sys/class/pwm/pwmchip0
 
[ -d $sys_pwm ] || exit 1
 
if [ ! -d $sys_pwm/pwm0 ]; then
    echo 0 > $sys_pwm/export
fi
 
echo 1000 > $sys_pwm/pwm0/period
echo 1 > $sys_pwm/pwm0/enable
 
for n in $(seq 10); do
    for d in $(seq 0 50 1000); do
        echo $d > $sys_pwm/pwm0/duty_cycle
        sleep .02
    done
    sleep 1
done