如何在BeagleBone Black Wireless(4.14内核)配置PWM?
Hey there, I've tackled this exact problem on the BBBW running kernel 4.14.x—those old bone_capemgr and slots file tricks are totally outdated now. Let's walk through the modern, working way to set up PWM:
1. First, Know Your PWM-Capable Pins
Not all pins support PWM, so start with a quick reference for common ones on BBBW:
- P8_13 → ehrpwm1A (maps to
pwmchip0channel 0) - P8_19 → ehrpwm1B (maps to
pwmchip0channel 1) - P9_14 → ehrpwm0A (maps to
pwmchip1channel 0) - P9_16 → ehrpwm0B (maps to
pwmchip1channel 1)
These are the most commonly used channels, but you can cross-reference the full BBBW pinout if you need others.
2. Configure the Pin for PWM (Using config-pin)
The config-pin tool is the new standard for pin muxing on BBB post-4.14 kernels—it replaces the old slots system. Here's how to use it:
- First, ensure the tool is installed (it’s default on most BBB images, but if missing):
sudo apt update && sudo apt install bb-cape-overlays - Set your target pin to PWM mode. For example, to enable P8_13:
sudo config-pin P8_13 pwm - Verify success by checking the PWM sysfs directory:
You should seels /sys/class/pwm/pwmchip0(for ehrpwm1) appear if you configured P8_13/P8_19.
3. Control PWM via SysFS Interface
Once the pin is muxed, adjust PWM parameters through the sysfs interface. Let’s use P8_13 (pwmchip0 channel 0) as an example:
- Export the PWM channel (make it accessible in sysfs):
sudo echo 0 > /sys/class/pwm/pwmchip0/export - Set the period (total cycle time in nanoseconds—1ms = 1,000,000ns):
sudo echo 1000000 > /sys/class/pwm/pwm0/period - Set the duty cycle (active time in nanoseconds—50% duty cycle here):
sudo echo 500000 > /sys/class/pwm/pwm0/duty_cycle - Enable the PWM output:
sudo echo 1 > /sys/class/pwm/pwm0/enable
To turn it off later:
sudo echo 0 > /sys/class/pwm/pwm0/enable sudo echo 0 > /sys/class/pwm/pwmchip0/unexport
4. Make PWM Settings Persist on Boot
If you want the PWM pin to be configured automatically every time the board starts:
Option 1: Use U-Boot overlays
Edit/boot/uEnv.txtwith sudo, uncomment or add these lines (adjust the overlay name based on your PWM channel):enable_uboot_overlays=1 uboot_overlay_addr0=/lib/firmware/am335x-pwm1.dtboThis loads the PWM1 overlay (for P8_13/P8_19) on boot. Then add your sysfs commands to
/etc/rc.local(beforeexit 0) to set period/duty cycle/enable.Option 2: Run config-pin on boot
Add theconfig-pincommand and sysfs steps to/etc/rc.local(run with sudo, or as root):sleep 5 # Give system time to initialize config-pin P8_13 pwm echo 0 > /sys/class/pwm/pwmchip0/export echo 1000000 > /sys/class/pwm/pwm0/period echo 500000 > /sys/class/pwm/pwm0/duty_cycle echo 1 > /sys/class/pwm/pwm0/enable
Troubleshooting Tips
- Permission denied errors: Always use
sudofor sysfs commands, or switch to root withsudo su. - PWM chip not appearing: Double-check that you’re using a PWM-capable pin, and that the
config-pincommand ran without errors. - Overlay not loading: Verify the overlay file exists at
/lib/firmware/am335x-pwmX.dtbo(replace X with your PWM channel number).
内容的提问来源于stack exchange,提问作者dgund

