3

I am trying to use the hardware PWM module on my pi 3 by writing to the /sys/class/pwm/pwmchip0 directory (which I think is just a symbolic link to sys/devices/platform/soc/3f20c000.pwm/pwm/pwmchip0). When I do this using bash and sudo, everything works as expected. The problem is that I need to write to this same folder from within a C++ program which I cannot run as root. I have tried creating a group 'pwm', giving ownership of pwmchip0 to 'pwm', and changing permissions with chmod -R g+rw pwmchip0, but when I try to write to the folder without sudo, I get "Permission denied". When I run ls -l on the folder, I get the following permissions, which to be honest I do not really understand:

pi@raspberry:/sys/devices/platform/soc/3f20c000.pwm/pwm $ls -l pwmchip0
total 0
lrwxrwxrwx 1 root pwm    0 May  9 20:44 device -> ../../../3f20c000.pwm
--w-rwx--- 1 root pwm 4096 May  9 20:12 export
-r--rwxr-- 1 root pwm 4096 May  9 20:41 npwm
drwxrwxr-x 2 root pwm    0 May  9 20:41 power
drwxrwxr-x 3 root pwm    0 May  9 20:41 pwm0
drwxrwxr-x 3 root pwm    0 May  9 20:41 pwm1
lrwxrwxrwx 1 root pwm    0 May  9 20:44 subsystem -> ../../../../../../class/pwm
-rw-rwxr-- 1 root pwm 4096 May  9 20:41 uevent
--w-rwx--- 1 root pwm 4096 May  9 20:41 unexport

I was hoping someone else has been able to solve this problem and has some insight for me, or perhaps someone with a better understanding of permissions and groups can enlighten me. Thanks in advance for your help!

m_dubya
  • 31
  • 1
  • 4

1 Answers1

2

As per github issue Neal Ehardt mentioned in the comment, you need to edit /etc/udev/rules.d/99-com.rules and add the following lines there:

SUBSYSTEM=="pwm*", PROGRAM="/bin/sh -c '\
        chown -R root:gpio /sys/class/pwm && chmod -R 770 /sys/class/pwm;\
        chown -R root:gpio /sys/devices/platform/soc/*.pwm/pwm/pwmchip* && chmod -R 770 /sys/devices/platform/soc/*.pwm/pwm/pwmchip*\
'"

After reboot the default pi user will be able to access the files in /sys/class/pwm/pwmchip0.

alpha-mouse
  • 121
  • 3