PWM Configuration
Configure Kernel Menu for PWM
-
Under the root directory of freelight-u-sdk, run the following command to enter the kernel menu configuration GUI.
make linux-menuconfig -
Enter the Device Drivers menu.
-
Enter the PWM support menu.
-
Select the StarFive PWM PTC Support option.
-
Save your change and exit the kernel configuration dialog.
Board-level Configuration: Configure PWM Pins
The following code example (in the jh7110-common.dtsi file)
defines two PWM output pins for the system. It sets up which pin to use, what
function the pin has, input/output settings, and how the PWM signal is
controlled.
pwm_pins: pwm-pins {
pwm_ch0-pins {
sf,pins = <PAD_GPIO46>;
sf,pinmux = <PAD_GPIO46_FUNC_SEL 0>;
sf,pin-ioconfig = <IO(GPIO_IE(1))>;
sf,pin-gpio-dout = <GPO_PTC0_PWM_0>;
sf,pin-gpio-doen = <OEN_PTC0_PWM_0_OE_N>;
};
pwm_ch1-pins {
sf,pins = <PAD_GPIO59>;
sf,pinmux = <PAD_GPIO59_FUNC_SEL 0>;
sf,pin-ioconfig = <IO(GPIO_IE(1))>;
sf,pin-gpio-dout = <GPO_PTC0_PWM_1>;
sf,pin-gpio-doen = <OEN_PTC0_PWM_1_OE_N>;
};
};Parameter explanations:
For PWM Channel 0 (GPIO46):
sf,pins = <PAD_GPIO46>;: Use physical pin GPIO46.sf,pinmux = <PAD_GPIO46_FUNC_SEL 0>;: Function 0 = PWM outputsf,pin-ioconfig = <IO(GPIO_IE(1))>;: Enables input.sf,pin-gpio-dout = <GPO_PTC0_PWM_0>;: Connects the pin to PWM channel 0 output.sf,pin-gpio-doen = <OEN_PTC0_PWM_0_OE_N>;: Enables output for PWM channel 0.
For PWM Channel 1 (GPIO59):
sf,pins = <PAD_GPIO59>;: Uses physical pin GPIO59.sf,pinmux = <PAD_GPIO59_FUNC_SEL 0>;: Function 0 = PWM outputsf,pin-ioconfig = <IO(GPIO_IE(1))>;: Enables input.sf,pin-gpio-dout = <GPO_PTC0_PWM_1>;: Connects to PWM channel 1 outputsf,pin-gpio-doen = <OEN_PTC0_PWM_1_OE_N>;: Enables output control for PWM channel 1
Configure a PWM Channel using sysfs
In the user space, you can configure a specific PWM channel using the
sysfs interface supported by Linux.
-
Run the following command to list all registeredd PWM chips.
# cd /sys/class/pwm/ # ls -
Enter the directory pwmchip* and run the following command:
where X is the PWM channel that you want to configure.echo X > exportThe command generates the pwmX directory as follows:# cd pwmchip0/ # ls device export npwm power subsystem uevent unex-port # echo 0 > export # ls device npwm pwm0 uevent export power subsystem unexport # echo 1 > export # ls device npwm pwm0 subsystem unexport export power pwm1 uevent -
Enter the directory pwmX, edit the target variables of the PWM channel.
# cd pwm0/ # ls capture enable polarity uevent duty_cycle period power # echo 10000 > period # echo 5000 > duty_cycle # cat period 10000 # cat duty_cycle 5000 # cat polarity normal # cat enable 1- enable: Enables or disables the channel.
1for enable, and0for disable. - polarity: Whether to invert polarity.
1to invert polarity and0not to invert (normal) - period: Specifies the period of the PWM signal in unit of ns.
- duty_cycle: Specifies the duty cycle of the PWM signal in unit of ns.
- enable: Enables or disables the channel.