树莓派5 GPIO 输出 PWM
流程概要
-
配置
- 在"/boot/firmware/config.txt"中添加"dtoverlay=pwm"。
重启,会激活"GPIO18"——"PWM0_CHAN2"
- 在"/boot/firmware/config.txt"中添加"dtoverlay=pwm"。
-
使用
- BASH
于"/sys/class/pwm/class/pwmchip0",echo 2 > export cd pwm2 echo 1000000 > period #可配置,周期 echo 500000 > duty_cycle #可配置,占空比 echo 1 > enable #开始 #echo normal > polarity #可配置,极性,默认normal echo 0 > enable #停止 cd .. #回到pwmchip0 echo 2 > unexport - lua-peripheral
local PWM = Periphery.PWM local PWM_Chip_Channel=PWM(0,2) -- PWM_CHIP, PWM_CHANNEL PWM_Chip_Channel.frequency = 1000000 PWM_Chip_Channel.duty_cycle = 0.5 PWM_Chip_Channel:enable() -- PWM_Chip_Channel:disable() PWM_Chip_Channel:close()
- BASH
详情
- 环境:
- 内核版本=6.18.33+rpt-rpi-2712
- 系统版本=Debian GNU/Linux 13 (trixie)
- 关于"/boot/firmware/config.txt"的配置,
见"/boot/firmware/overlays/README",
包含:pwm、pwm-2chan、pwm-gpio等。 - lua-periphery参考:vsergeev/lua-periphery: A Lua library for peripheral I/O (GPIO, LED, PWM, SPI, I2C, MMIO, Serial) in Linux.
扩展
- 摘录"/boot/firmware/overlays/README"中"pwm"部分:
Name: pwm
Info: Configures a single PWM channel
Legal pin,function combinations for each channel:
PWM0: 12,4(Alt0) 18,2(Alt5) 40,4(Alt0) 52,5(Alt1)
PWM1: 13,4(Alt0) 19,2(Alt5) 41,4(Alt0) 45,4(Alt0) 53,5(Alt1)
N.B.:
1) Pin 18 is the only one available on all platforms, and
it is the one used by the I2S audio interface.
Pins 12 and 13 might be better choices on an A+, B+ or Pi2.
2) The onboard analogue audio output uses both PWM channels.
3) So be careful mixing audio and PWM.
4) Currently the clock must have been enabled and configured
by other means.
Load: dtoverlay=pwm,<param>=<val>
Params: pin Output pin (default 18) - see table
func Pin function (default 2 = Alt5) - see above
clock PWM clock frequency (informational)
Name: pwm-2chan
Info: Configures both PWM channels
Legal pin,function combinations for each channel:
PWM0: 12,4(Alt0) 18,2(Alt5) 40,4(Alt0) 52,5(Alt1)
PWM1: 13,4(Alt0) 19,2(Alt5) 41,4(Alt0) 45,4(Alt0) 53,5(Alt1)
N.B.:
1) Pin 18 is the only one available on all platforms, and
it is the one used by the I2S audio interface.
Pins 12 and 13 might be better choices on an A+, B+ or Pi2.
2) The onboard analogue audio output uses both PWM channels.
3) So be careful mixing audio and PWM.
4) Currently the clock must have been enabled and configured
by other means.
Load: dtoverlay=pwm-2chan,<param>=<val>
Params: pin Output pin (default 18) - see table
pin2 Output pin for other channel (default 19)
func Pin function (default 2 = Alt5) - see above
func2 Function for pin2 (default 2 = Alt5)
clock PWM clock frequency (informational)
Name: pwm-gpio
Info: Configures the software PWM GPIO driver
Load: dtoverlay=pwm-gpio,<param>=<val>
Params: gpio Output pin (default 4)
-
"/sys/class/pwm/pwmchip0/"为逻辑路径,
- 当配置,在"/boot/firmware/config.txt"中添加"dtoverlay=pwm"后时,其链接到"/sys/devices/platform/axi/1000120000.pcie/1f00098000.pwm/pwm/pwmchip0/"(对应以上
cat /sys/kernel/debug/pwm中的"1: platform/1f00098000.pwm"); - 当未配置时,其链接到"/sys/devices/platform/axi/1000120000.pcie/1f0009c000.pwm/pwm/pwmchip0/"(对应以上
cat /sys/kernel/debug/pwm中的"1: platform/1f0009c000.pwm")。
故,不可依赖路径名称"pwmchip0"。
- 当配置,在"/boot/firmware/config.txt"中添加"dtoverlay=pwm"后时,其链接到"/sys/devices/platform/axi/1000120000.pcie/1f00098000.pwm/pwm/pwmchip0/"(对应以上
-
cat /sys/kernel/debug/pwm可以查看所有pwm的配置,如0: platform/1f00098000.pwm, npwm: 4 pwm-0 ((null) ): requested configuration: disabled, 0/0 ns, normal polarity actual configuration: disabled, 0/0 ns, normal polarity pwm-1 ((null) ): requested configuration: disabled, 0/0 ns, normal polarity actual configuration: disabled, 0/0 ns, normal polarity pwm-2 (sysfs ): requested requested configuration: enabled, 121568/1000000 ns, normal polarity actual configuration: disabled, 0/0 ns, normal polarity pwm-3 ((null) ): requested configuration: disabled, 0/0 ns, normal polarity actual configuration: disabled, 0/0 ns, normal polarity 1: platform/1f0009c000.pwm, npwm: 4 pwm-0 ((null) ): requested configuration: disabled, 0/0 ns, normal polarity actual configuration: disabled, 0/0 ns, normal polarity pwm-1 ((null) ): requested configuration: disabled, 0/0 ns, normal polarity actual configuration: disabled, 0/0 ns, normal polarity pwm-2 ((null) ): requested configuration: disabled, 0/0 ns, normal polarity actual configuration: disabled, 0/0 ns, normal polarity pwm-3 (cooling_fan ): requested requested configuration: disabled, 0/41566 ns, inverse polarity, usage_power actual configuration: disabled, 0/0 ns, normal polarity其中:
- "0"-"pwm-2"是示例项
- "1"-"pwm-3"是风扇
-
用
gpioinfo -c 0 18检查"gpiochip0"\line 18——GPIO18的配置,有:"gpiochip0 18 "GPIO18" input"(而非'gpiochip0 18 "GPIO18" PWM0_CHAN2') -
用
pinctrl 18检查有:"18: a3 pd | lo // GPIO18 = PWM0_CHAN2"。 -
使用
pinctrl funcs查看GPIO支持的功能,(用pinctrl funcs | grep PWM筛选)。
其中GPIO12、13、14、15、18、19支持硬件PWM。(软PWM见"/boot/firmware/overlays/README"中的"pwm-gpio") -
"PWM0_CHAN2"对应
- bash中的"pwmchip0",lua-peripheral中的"PWM_CHIP=0";
- bash中的"pwm2",lua-peripheral中的"PWM_CHANNEL=2"。
-
支持的PWM针脚,可参考PWM at Raspberry Pi GPIO Pinout
浙公网安备 33010602011771号