2. PWM
2. 1 Enabling PWM Interface
- Use luckfox-config to enable the relevant configuration:
-
Open the luckfox-config tool in the terminal:
sudo luckfox-config -
Select Interface Options:

-
Select PWM:

-
Select the PWM interface you want to enable, here we take PWM9 as an example:

-
After selecting Enter to confirm, there will be a prompt that UART4 is not available. This is because using the PWM function prevents the simultaneous use of the UART4 interface for serial communication.
-
View the PWM interface:
oot@linaro-alip:/home/linaro# ls /sys/class/pwmpwmchip0root@linaro-alip:/home/linaro# ls -l /sys/class/pwmtotal 0lrwxrwxrwx 1 root root 0 6月 6 07:03 pwmchip0 -> ../../devices/platform/fe6f0010.pwm/pwm/pwmchip0 -
The mapping between PWM controllers and register addresses is as follows:
PWM Controller Register Address pwm8 fe6f0000 pwm9 fe6f0010 pwm12 fe700000 pwm13 fe700010 pwm14 fe700020 pwm15 fe700030
2. 2 PWM Control (shell)
-
Ensure that PWM8 has been enabled. The following operations are based on PWM8.
-
You can observe the phenomenon by connecting an LED to the corresponding pin. The opening process is as follows:
# Export pwm3 to user spaceecho 0 > /sys/class/pwm/pwmchip1/export# Set PWM period in nanosecondsecho 1000000 > /sys/class/pwm/pwmchip1/pwm0/period# Set duty cycleecho 500000 > /sys/class/pwm/pwmchip1/pwm0/duty_cycle# Set PWM polarityecho "normal" > /sys/class/pwm/pwmchip1/pwm0/polarity# Enable PWMecho 1 > /sys/class/pwm/pwmchip1/pwm0/enable# Unexport pwm3 from user spaceecho 0 > /sys/class/pwm/pwmchip1/unexport
2. 3 Controlling PWM Using python-periphery
-
Ensure that PWM8 has been enabled. The following operations are based on PWM8.
-
The sample code for PWM output using the python-periphery library is as follows:
from periphery import PWMimport timeimport numpy as np# Open PWM chip 0, channel 0 pwm = PWM(0, 0)pwm = PWM(0, 0)# Set frequency to 100 Hzpwm.frequency = 1e2#set the PWM’s output polaritypwm.polarity = "normal"# Set duty cycle to 0pwm.duty_cycle = 0pwm.enable()# Change duty cyclefor i in np.arange(0.0, 1.0, 0.05):pwm.duty_cycle = itime.sleep(1)pwm.close() -
Run the program, and you will see the LED connected to Pin 33 gradually brighten.