惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

D
Docker
U
Unit 42
Google DeepMind News
Google DeepMind News
B
Blog RSS Feed
S
SegmentFault 最新的问题
阮一峰的网络日志
阮一峰的网络日志
雷峰网
雷峰网
Microsoft Security Blog
Microsoft Security Blog
爱范儿
爱范儿
Cyber Security Advisories - MS-ISAC
Cyber Security Advisories - MS-ISAC
博客园_首页
Apple Machine Learning Research
Apple Machine Learning Research
罗磊的独立博客
GbyAI
GbyAI
Stack Overflow Blog
Stack Overflow Blog
Martin Fowler
Martin Fowler
宝玉的分享
宝玉的分享
L
LangChain Blog
Engineering at Meta
Engineering at Meta
量子位
有赞技术团队
有赞技术团队
博客园 - 【当耐特】
A
About on SuperTechFans
Y
Y Combinator Blog

博客园 - 萝卜L

TIA 轨迹 Trace USB Device Tree Viewer & 设备管理器 - 通用串行总线控制器 树莓派 DT 西门子 PLC 1500 过程时间性能 PD 协议 E-Marker芯片、私有协议,联想 ThinkPad 135W 电源 Slim方口 ThinkPlus电源 树莓派 RaspberryPi 5 硬件 设备(dev) 芯片(SoC) BCM 线路编号(line) GPIO 针脚 gpioinfo pinout Solid Edge 仿真 Dynamic Designer Motion (DDM) Solid Edge PMI 智能尺寸 尺寸平面 Solid_Edge,尺寸、测量,从动、驱动,同步、顺序建模,变量表 变量类型 Dim Var SolidWorks & Solid Edge 装配体 零件 部件 移动 通过批处理快捷方式参数化调用 Lua 脚本:完整方案与备选方式 Win10 隐藏的 占用空间的 大文件 组态王 7.5 西门子 驱动 各版本 严重 异常 PLC、上位(HMI)接口约定 Modbus RTU TCP 网关 拓扑 TIA SIM 授权 iup "creation only"的时刻 lua 5.3 glue srlua/wsrlua console/windows CMake Luarocks OCX(ActiveX COM) 控件测试容器(tstcon32) 失败 问题 解决 Lua IUP Plot 柱状图 示例 GitHub Desktop 连接 Gitee 失败 EPLAN P8 学习笔记 配图 20250114 TIA Portal V19 STEP 7 Professional WinCC - Setup 安装过程中出错 失败 笔记 配图 20250110 windows zeroBrane Studio(zbstudio/zbs)配置 iuplua V3 Lua 找不到指定模块 找不到指定的程序 V2 powershell 作服务端 响应网络(socket tcp)连接 提供文件夹大小查询服务 V1.1 网络 主机名 地址 解析 记戴尔/Dell U2913WM 显示器故障
树莓派5 GPIO 输出 PWM
萝卜L · 2026-06-04 · via 博客园 - 萝卜L

流程概要

  • 配置

    • 在"/boot/firmware/config.txt"中添加"dtoverlay=pwm"。
      重启,会激活"GPIO18"——"PWM0_CHAN2"
  • 使用

    • 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()
      

详情

扩展

  • 摘录"/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"。

  • 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