Malc
unread,Jan 14, 2023, 6:08:44 AM1/14/23Sign in to reply to author
Sign in to forward
You do not have permission to delete messages in this group
Either email addresses are anonymous for this group or you need the view member email addresses permission to view the original message
to bcm2835
The SPI, I2C and SMI modules all return error codes to their respective bcm2835_xxx_begin() functions in the event that mapping /dev/mem fails (usually because the user is not root). But there isn't a corresponding bcm2835_pwm_begin(). The pwm calls are all void functions and just return (fail) silently.
I understand that this behaviour is as clearly documented. Nonetheless I find it somewhat inconsistent and annoying. I don't often use PWM, but I currently have a project which does, and I'm forever forgetting the "sudo" and then wondering why it doesn't work. In other cases, if I'm just using GPIO it doesn't matter because /dev/gpiomem is mapped, or I get 0 returned from the bcm2835_xxx_begin() which I can use to flag a warning.
Initially I implemented a call to geteuid() before calling bcm2835_init(), and if not root I bombed straight out with an error message. But this is inelegant, because it's repeating the exact same check that the library is going to do anyway in bcm2835_init(), and also it assumes that if UID = root the map will work, which may not always be true. It would be better if my program could find out the result of the library's check and mapping attempt.
So I have added a int bcm2835_pwm_begin(void) function to my copy of the library. It's trivial - all it does is test bcm2835_pwm and return 0 if it is MAP_FAILED, 1 otherwise:
int bcm2835_pwm_begin(void)
{
if (bcm2835_pwm == MAP_FAILED)
return 0; /* bcm2835_init() failed, or not root */
return 1;
}
Corresponding extern in bcm2835.h, of course. It allows a user program to check that /dev/mem has been successfully mapped, and take appropriate action if not. I'm happy to offer this as a very modest contribution if anyone else thinks it might be useful.