实验目的
利用脉冲调制效应(PWM)等效改变输出功率大小控制LED,从而实现呼吸灯效果,需要用到RGB LED模块
模块连接
IO 插针接口上一共集成了两路PWM,分别是PWM2和PWM3,对应GPIO88、GPIO89
PWM2和PWM3对应Pin3和Pin5引脚,这里使用Pin1和Pin3分别连接LED的GND和R口
修改设备树
arch/loongarch/boot/dts/loongson/loongson_2k0300.dtsi
pwm2: pwm@0x1611b020 {compatible = "loongson,ls2k-pwm";reg = <0 0x1611b020 0 0xf>;interrupt-parent = <&icu>;interrupts = <17>;#pwm-cells = <2>;clock-frequency = <100000000>;status = "disabled";
};pwm3: pwm@0x1611b030 {compatible = "loongson,ls2k-pwm";reg = <0 0x1611b030 0 0xf>;interrupt-parent = <&icu>;interrupts = <17>;#pwm-cells = <2>;clock-frequency = <100000000>;status = "disabled";
};
arch/loongarch/boot/dts/loongson/2k0300-pinctrl.dtsi
pwm2_pin: pwm2-pin{pwm2_mux_m0: pwm2-mux-m0{loongson,pinmux = <&gpa4 2 2>;loongson,pinmux-funcsel = <PINCTL_FUNCTION1>;};pwm2_mux_m1: pwm2-mux-m1{loongson,pinmux = <&gpa5 8 8>; /* pwm2 have two multiplex methods */loongson,pinmux-funcsel = <PINCTL_FUNCTION2>;};
};pwm3_pin: pwm3-pin{pwm3_mux_m0: pwm3-mux-m0{loongson,pinmux = <&gpa4 3 3>;loongson,pinmux-funcsel = <PINCTL_FUNCTION1>;};pwm3_mux_m1: pwm3-mux-m1{ /* pwm3 have two multiplex methods */loongson,pinmux = <&gpa5 9 9>;loongson,pinmux-funcsel = <PINCTL_FUNCTION2>;};
};
arch/loongarch/boot/dts/loongson/loongson_2k0300_pai_99.dts
&pwm2{status = "okay";pinctrl-names = "default";pinctrl-0 = <&pwm2_mux_m1>;
};
&pwm3{status = "okay";pinctrl-names = "default";pinctrl-0 = <&pwm3_mux_m1>;
};
修改完毕后重新编译内核,将内核部署到开发板/boot
目录下,重启开发板发现/sys/class/pwm
下已经生成两个pwmchip节点
波形测试
运行测试脚本run.sh,周期和占空比脉冲的时间单位为ns
echo 0 > /sys/class/pwm/pwmchip0/export # 导出pwm节点
echo 0 > /sys/class/pwm/pwmchip0/pwm0/enable # 关闭pwm输出
echo 100000 > /sys/class/pwm/pwmchip0/pwm0/period # 设置pwm周期
echo 50000 > /sys/class/pwm/pwmchip0/pwm0/duty_cycle # 设置占空比脉冲数
echo "normal" > /sys/class/pwm/pwmchip0/pwm0/polarity # 设置极性,有normal和inversed两种
echo 1 > /sys/class/pwm/pwmchip0/pwm0/enable # 开启pwm输出
示波器查看PWM2输出波形:生成了10KHz的波形,占空比为50%
PWM LED模拟呼吸灯效果,为了简单起见就不写C语言代码了
num=10
peroid=100000
max=100000
min=10
flag=0echo $peroid > /sys/class/pwm/pwmchip0/pwm0/periodwhile true
doif [ $flag -eq 0 ]then if [ $num -lt $max ]then echo 0 > /sys/class/pwm/pwmchip0/pwm0/enable
# echo $peroid > /sys/class/pwm/pwmchip0/pwm0/periodecho $num > /sys/class/pwm/pwmchip0/pwm0/duty_cycleecho "normal" > /sys/class/pwm/pwmchip0/pwm0/polarityecho 1 > /sys/class/pwm/pwmchip0/pwm0/enablesleep 0.1let num+=10000 elseflag=1fielseif [ $num -gt $min ]then echo 0 > /sys/class/pwm/pwmchip0/pwm0/enable
# echo $peroid > /sys/class/pwm/pwmchip0/pwm0/periodecho $num > /sys/class/pwm/pwmchip0/pwm0/duty_cycleecho "normal" > /sys/class/pwm/pwmchip0/pwm0/polarityecho 1 > /sys/class/pwm/pwmchip0/pwm0/enablesleep 0.1let num-=10000elseflag=0fi fi
done
实验效果
PWM动态输出效果 + 呼吸灯