MilkV Duo256 PWM?

Hi!

I’m trying to get PWM on at least 2 pins of a MilkV Duo256. I have only been able to get 1 pin working.

I’m running the default OS image:

$ cat /etc/os-release 
NAME=Buildroot
VERSION=-g6b03c2762
ID=buildroot
VERSION_ID=2025.02
PRETTY_NAME="Buildroot 2025.02"

To get the one pin (pin#6 == GP4) working :

[root@milkv-duo]~# duo-pinmux -w GP4/PWM_5
pin GP4
func PWM_5
register: 30010d4
value: 7
[root@milkv-duo]~# echo 1 > /sys/class/pwm/pwmchip4/export
[root@milkv-duo]~# echo 256 > /sys/class/pwm/pwmchip4/pwm1/period
[root@milkv-duo]~# echo 128 > /sys/class/pwm/pwmchip4/pwm1/duty_cycle
[root@milkv-duo]~# echo 1 > /sys/class/pwm/pwmchip4/pwm1/enable

I am testing this with an LED and I can confirm I can change the brightness by changing the duty cycle.

However any other pins elude me. The Sophgo SG2002 Technical Reference Manual has a PWM section in the Peripherals Chapter. It says there are 4 PWM controllers PWM0, PWM1, PWM2 and PWM3. Each controller provides 4 independent PWM signal outputs:

• PWM0 includes PWM[0], PWM[1], PWM[2], PWM[3].
• PWM1 includes PWM[4], PWM[5], PWM[6], PWM[7].
• PWM2 includes PWM[8], PWM[9], PWM[10], PWM[11].
• PWM3 includes PWM[12], PWM[13], PWM[14], PWM[15].

duo-pinmux -l lists only 8 PWM_? pins.

Does anyone know the mapping from SG2002 PWM[??] to MilkV Duo256 PWM_? ? How can I use them?

2 Likes

I was able to get multiple pins to output a PWM signal. After a reddit user pointed out the mapping between SG2002 and duo-pinmux, I re-examined the commands Michael Jentsch had documented. Turns out echo 1 > /sys/class/pwm/pwmchip4/export can be modified to expose the other PWM[??] outputs.

So to get PWM on pin #7 == GP5

$ duo-pinmux -w GP5/PWM_6
$ echo 2 > /sys/class/pwm/pwmchip4/export
$ ls /sys/class/pwm/pwmchip4/
device  export  npwm  pwm1  pwm2  subsystem  uevent  unexport
$ echo 256 > /sys/class/pwm/pwmchip4/pwm2/period
$ echo 128 > /sys/class/pwm/pwmchip4/pwm2/duty_cycle
$ echo 1 > /sys/class/pwm/pwmchip4/pwm2/enable

So the valid values to send to /sys/class/pwm/pwmchip4/export are 0, 1, 2, 3 which respectively export pwm0, pwm1, pwm2, pwm3. These then can be manipulated using the period, duty_cycle and enable files.

2 Likes