I figure out how to use the 4 PWM from GPIO.
First I did have to enable the PWM in /boot/config.txt and reboot.To make thing simpler I create a small python3 script to enable the gpio for the pwm from /sys/class/pwm/pwmchip2 etc....
the PWM has four possible output which could be
PWM0 GPIO12,
PWM1 GPIO13,
PWM2 GPIO14 or GPIO18,
PWM3 GPIO15 or GPIO19
ex: of my scriptOf course this is just a proof of concept. I didn't bother to check all the possible error like export gpio already there , etc...
I used os.system but you should be able to write directly with file open.
N.B. I put a __del__ () to unexport and close everything.
this is my pi5RC class
First I did have to enable the PWM in /boot/config.txt and reboot.
Code:
[all]dtoverlay=pwm
the PWM has four possible output which could be
PWM0 GPIO12,
PWM1 GPIO13,
PWM2 GPIO14 or GPIO18,
PWM3 GPIO15 or GPIO19
ex: of my script
Code:
from pi5RC import pi5RCpwm0 = pi5RC(12)pwm1 = pi5RC(13)pwm2 = pi5RC(18)pwm3 = pi5RC(19)# set all r/c to the middle# the set command put timing in microsecond##pwm0.set(1500)pwm1.set(1500)pwm2.set(1500)pwm3.set(1500)....
I used os.system but you should be able to write directly with file open.
N.B. I put a __del__ () to unexport and close everything.
this is my pi5RC class
Code:
#!/usr/bin/python3import osimport timeclass pi5RC: def __init__(self,Pin): pins = [ 12,13,14,15,18,19] afunc= [ 'a0','a0','a0', 'a0', 'a3','a3']; self.pwmx = [ 0,1,2,3,2,3] self.enableFlag=False if Pin in pins: self.pin=Pin self.pinIdx = pins.index(Pin) # let's set pin ctrl os.system("/usr/bin/pinctrl set {} {}".format(self.pin,afunc[self.pinIdx])) # let export pin os.system("echo {} > /sys/class/pwm/pwmchip2/export".format(self.pwmx[self.pinIdx])) # CLOCK AT 1gHZ let put period to 20ms time.sleep(0.2) os.system("echo 20000000 > /sys/class/pwm/pwmchip2/pwm{}/period".format(self.pwmx[self.pinIdx])) time.sleep(0.1) self.enable(False) else: self.pin=None print("Error Invalid Pin") def enable(self,flag): self.enableFlag=flag os.system("echo {} > /sys/class/pwm/pwmchip2/pwm{}/enable".format( int(self.enableFlag),self.pwmx[self.pinIdx])) def __del__(self): if self.pin is not None: #ok take PWM out os.system("echo {} > /sys/class/pwm/pwmchip2/unexport".format(self.pwmx[self.pinIdx])) #disable PWM Pin os.system("/usr/bin/pinctrl set {} no".format(self.pin)) def set(self, onTime_us): if not self.enableFlag: self.enable(True) self.onTime_ns=onTime_us*1000 os.system("echo {} > /sys/class/pwm/pwmchip2/pwm{}/duty_cycle".format(self.onTime_ns,self.pwmx[self.pinIdx]))
Statistics: Posted by danjperron — Tue Jan 09, 2024 3:01 am