Home
JAQForum Ver 20.06
Log In or Join  
Active Topics
Local Time 15:26 26 Apr 2024 Privacy Policy
Jump to

Notice. New forum software under development. It's going to miss a few functions and look a bit ugly for a while, but I'm working on it full time now as the old forum was too unstable. Couple days, all good. If you notice any issues, please contact me.

Forum Index : Microcontroller and PC projects : MMB4L: MMBasic for Linux alpha release

     Page 3 of 11    
Author Message
robert.rozee
Guru

Joined: 31/12/2012
Location: New Zealand
Posts: 2287
Posted: 01:16pm 25 Sep 2021
Copy link to clipboard 
Print this post

  lizby said  I thought it might have been the "sudo"s in the code. Removing them all got further in the program, but it again died with a "permission" error. Oddly, if I reran, it got further, and after doing that 8 times, it ran fine


sudo is funny like that. once you've done a sudo and supplied the password, it can, for some short length of time thereafter, be run again without requiring the password to be entered.

i believe the 'correct' way of doing it from within a script or program is to use pkexec. for example, see the below shell script:

#!/bin/bash
cmdU=""
cmdB=""

for port in $(find /sys/bus/pci/drivers/ehci-pci/0000* -maxdepth 1 -type l)
do    
 cmdU=$cmdU"echo -n "$(basename "$port")" > $(dirname "$port")/unbind;"
 cmdB=$cmdB"echo -n "$(basename "$port")" > $(dirname "$port")/bind;"
done;

for port in $(find /sys/bus/pci/drivers/xhci_hcd/0000* -maxdepth 1 -type l)
do    
 cmdU=$cmdU"echo -n "$(basename "$port")" > $(dirname "$port")/unbind;"
 cmdB=$cmdB"echo -n "$(basename "$port")" > $(dirname "$port")/bind;"
done

cmdstr="sleep 1;"$cmdU"sleep 3;"$cmdB"sleep 1"
echo $cmdstr | sed 's/;/\n/g'

pkexec su -c "$cmdstr"

if [ $? -eq 0 ]
then
 paplay /usr/share/sounds/LinuxMint/stereo/system-ready.ogg
fi


this particular script brings the USB subsystem back up once a pico had knocked it over. can be very useful!


cheers,
rob   :-)
 
JohnS
Guru

Joined: 18/11/2011
Location: United Kingdom
Posts: 3656
Posted: 02:24pm 25 Sep 2021
Copy link to clipboard 
Print this post

  lizby said  
  JohnS said  Rob's idea looks good, but if it doesn't work look at the owner of the gpio ports.


Thanks. How do I do that?


Something like
ls -l /sys/class/gpio/gpio*

You probably want to see the group and become a member of it.  Commonly you'd have to re-login for an adduser (addgroup etc) to take effect.

John
 
lizby
Guru

Joined: 17/05/2016
Location: United States
Posts: 3015
Posted: 02:33pm 25 Sep 2021
Copy link to clipboard 
Print this post

  robert.rozee said  sudo is funny like that. once you've done a sudo and supplied the password, it can, for some short length of time thereafter, be run again without requiring the password to be entered.

Right, and I've seen that, but this didn't ask for a password (and I didn't provide one)--it just failed back the the MMB4L prompt. This was in attempting to write "out" or "in" to the direction file., e.g. /sys/class/gpio/gpio12/direction.

But apparently it did create the file, because pressing up-arrow and <Enter> to run again moved on to the direction file for the next pin, which failed. When I did this for all 8 pins (4 out and 4 in), it ran successfully.
PicoMite, Armmite F4, SensorKits, MMBasic Hardware, Games, etc. on fruitoftheshed
 
lizby
Guru

Joined: 17/05/2016
Location: United States
Posts: 3015
Posted: 02:37pm 25 Sep 2021
Copy link to clipboard 
Print this post

  JohnS said  Something like
ls -l /sys/class/gpio/gpio*


pi@raspberrypi:~ $ ls -l /sys/class/gpio/gpio*
lrwxrwxrwx 1 root gpio 0 Sep 25 09:17 /sys/class/gpio/gpiochip0 -> ../../devices/platform/soc/20200000.gpio/gpio/gpiochip0

~
Edited 2021-09-26 00:37 by lizby
PicoMite, Armmite F4, SensorKits, MMBasic Hardware, Games, etc. on fruitoftheshed
 
thwill

Guru

Joined: 16/09/2019
Location: United Kingdom
Posts: 3839
Posted: 03:35pm 25 Sep 2021
Copy link to clipboard 
Print this post

  lizby said  
  robert.rozee said  sudo is funny like that. once you've done a sudo and supplied the password, it can, for some short length of time thereafter, be run again without requiring the password to be entered.

Right, and I've seen that, but this didn't ask for a password (and I didn't provide one)


See https://www.raspberrypi.org/forums/viewtopic.php?t=97334

"The default policy for sudo within Raspbian is not the same as for Ubuntu etc. To change this you need to edit /etc/sudoers using visudo. More details can be found within the man page for sudo and within the file itself. Normally there is no root user and user pi has been given enough privileges to perform essential tasks w/o re-asking for a password."

Or to put it another way it appears tha even though Raspbian / Raspberry OS is a *nix it isn't configured/secured out of the box like a typical multi-user *nix system.

Regards,

Tom
Game*Mite, CMM2 Welcome Tape, Creaky old text adventures
 
JohnS
Guru

Joined: 18/11/2011
Location: United Kingdom
Posts: 3656
Posted: 04:11pm 25 Sep 2021
Copy link to clipboard 
Print this post

  lizby said  
  JohnS said  Something like
ls -l /sys/class/gpio/gpio*


pi@raspberrypi:~ $ ls -l /sys/class/gpio/gpio*
lrwxrwxrwx 1 root gpio 0 Sep 25 09:17 /sys/class/gpio/gpiochip0 -> ../../devices/platform/soc/20200000.gpio/gpio/gpiochip0

~

OK, so you're either not included in group gpio or need to re-login.

You can check with commands such as
id
or
groups

John
 
lizby
Guru

Joined: 17/05/2016
Location: United States
Posts: 3015
Posted: 05:47pm 25 Sep 2021
Copy link to clipboard 
Print this post

  JohnS said  OK, so you're either not included in group gpio or need to re-login.

You can check with commands such as
id
or
groups

John


After power-cycling:
pi@raspberrypi:~ $ groups pi
pi : pi adm dialout cdrom sudo audio video plugdev games users input netdev spi i2c gpio
pi@raspberrypi:~ $ id
uid=1000(pi) gid=1000(pi) groups=1000(pi),4(adm),20(dialout),24(cdrom),27(sudo),29(audio),44(video),46(plugdev),
60(games),100(users),101(input),108(netdev),997(gpio),998(i2c),999(spi)
pi@raspberrypi:~ $


~
Edited 2021-09-26 03:52 by lizby
PicoMite, Armmite F4, SensorKits, MMBasic Hardware, Games, etc. on fruitoftheshed
 
lizby
Guru

Joined: 17/05/2016
Location: United States
Posts: 3015
Posted: 06:02pm 25 Sep 2021
Copy link to clipboard 
Print this post

  robert.rozee said  pkexec su -c "$cmdstr"


Then I get:
> load "pinpi1.bas"
> run
==== AUTHENTICATING FOR org.freedesktop.policykit.exec ===
Authentication is needed to run `/bin/su' as the super user
Authenticating as: root
Password: Error in line 37: Command could not be run


If I run it with sudo mmbasic, I don't get the "AUTHENTICATING" error, but get:
pkexec su -c  echo '12' > /sys/class/gpio/export 2>/dev/null
Error in line 37: Command could not be run

PicoMite, Armmite F4, SensorKits, MMBasic Hardware, Games, etc. on fruitoftheshed
 
thwill

Guru

Joined: 16/09/2019
Location: United Kingdom
Posts: 3839
Posted: 06:15pm 25 Sep 2021
Copy link to clipboard 
Print this post

  lizby said  ...
Error in line 37: Command could not be run


I need to include the actual return value from system() in that error message.

Bit busy at the moment though, having to rebuild my mum's laptop

Best wishes,

Tom
Game*Mite, CMM2 Welcome Tape, Creaky old text adventures
 
scruss
Regular Member

Joined: 20/09/2021
Location: Canada
Posts: 83
Posted: 04:44pm 26 Sep 2021
Copy link to clipboard 
Print this post

  thwill said  It's not my area, but I've had the briefest of looks and it appears you are expected to have sudo privileges to access the GPIO on the Pi.


That used to be the case, but in the last couple of years (== aeons in Raspberry Pi time) it hasn't been necessary. Unfortunately, there's no one clear way of doing it:

* WiringPi, while still supplied as a system package, is no longer supported. The author walked away from it after a bad experience from Raspberry Pi clone manufacturers. For now, there's some community support, but it may not work on newer machines.

* pigpio is well supported and runs on all Raspberry Pis. It has a daemon that runs with root privileges, and user processes talk to that.

* the official/"correct" way is the kernel /dev/gpiochip* devices, but support for that is very limited so far.

Nice work on this port to all involved, btw. I'm glad it doesn't run a core flat out like the Raspberry pi version used to.
 
toml_12953
Guru

Joined: 13/02/2015
Location: United States
Posts: 326
Posted: 07:06pm 26 Sep 2021
Copy link to clipboard 
Print this post

  scruss said  
  thwill said  It's not my area, but I've had the briefest of looks and it appears you are expected to have sudo privileges to access the GPIO on the Pi.


That used to be the case, but in the last couple of years (== aeons in Raspberry Pi time) it hasn't been necessary. Unfortunately, there's no one clear way of doing it:


Nice work on this port to all involved, btw. I'm glad it doesn't run a core flat out like the Raspberry pi version used to.


It wouldn't hurt to try staring BASIC with sudo just to test it.
 
lizby
Guru

Joined: 17/05/2016
Location: United States
Posts: 3015
Posted: 10:11pm 26 Sep 2021
Copy link to clipboard 
Print this post

  toml_12953 said  It wouldn't hurt to try staring BASIC with sudo just to test it.

That's the only thing that has worked for me with this SYSTEM method of reading/setting pins.
PicoMite, Armmite F4, SensorKits, MMBasic Hardware, Games, etc. on fruitoftheshed
 
matherp
Guru

Joined: 11/12/2012
Location: United Kingdom
Posts: 8578
Posted: 06:52am 27 Sep 2021
Copy link to clipboard 
Print this post

If you want maximum speed then pigpio calls are the way to go but in that case the process must be started with sudo. If you use the pigpio daemon then the process won't need to be run with sudo but there are limitations to what the daemon supports. "File" access to I/O is very slow.

I recommend the book "Raspbery PI IOT in C" by Harry Fairhead (ISBN 9781871962468). He explains the various options with lots of code examples
 
thwill

Guru

Joined: 16/09/2019
Location: United Kingdom
Posts: 3839
Posted: 09:09am 27 Sep 2021
Copy link to clipboard 
Print this post

  scruss said  Nice work on this port to all involved, btw. I'm glad it doesn't run a core flat out like the Raspberry pi version used to.


Thanks, I'm please some people are having fun with it.

The MMB4L process takes brief naps when keyboard input is expected but not available (e.g. whilst waiting at the BASIC prompt) or when processing the PAUSE command (though before anyone shouts it doesn't naively just sleep for the length of the PAUSE). This does make PAUSE less accurate (both by limiting its resolution and I believe by increasing the likelyhood of another process grabbing the CPU). I imagine there will need to be some further titivation of this code but in the end Linux is not an RTOS so even if all the digital I/O is implemented there are many controller tasks for which MMB4L on the Pi (and presumably the PiCRomite) are unsuited.

  matherp said  I recommend the book "Raspbery PI IOT in C" by Harry Fairhead (ISBN 9781871962468). He explains the various options with lots of code examples.


Many thanks for the recommendation Peter, I'll add it to my Christmas list.

Best wishes,

Tom
Game*Mite, CMM2 Welcome Tape, Creaky old text adventures
 
JohnS
Guru

Joined: 18/11/2011
Location: United Kingdom
Posts: 3656
Posted: 10:59am 27 Sep 2021
Copy link to clipboard 
Print this post

  lizby said  After power-cycling:
pi@raspberrypi:~ $ groups pi
pi : pi adm dialout cdrom sudo audio video plugdev games users input netdev spi i2c gpio
pi@raspberrypi:~ $ id
uid=1000(pi) gid=1000(pi) groups=1000(pi),4(adm),20(dialout),24(cdrom),27(sudo),29(audio),44(video),46(plugdev),
60(games),100(users),101(input),108(netdev),997(gpio),998(i2c),999(spi)
pi@raspberrypi:~ $


~


Does it now work without needing to be root/sudo? The above looks good.

John
 
lizby
Guru

Joined: 17/05/2016
Location: United States
Posts: 3015
Posted: 03:09pm 27 Sep 2021
Copy link to clipboard 
Print this post

  JohnS said  Does it now work without needing to be root/sudo? The above looks good.

No. Still fails at "echo 'out' > /sys/class/gpio/gpio12/direction" with "sh: 1: cannot create /sys/class/gpio/gpio12/direction: Permission denied"

But when I immediately run it again, it works for gpio12 and fails at gpio16. If I run it 8 times, it progresses 1 pin further each time, and on the 9th run works. With "sudo mmbasic" it works from the beginning.

Puzzling.
PicoMite, Armmite F4, SensorKits, MMBasic Hardware, Games, etc. on fruitoftheshed
 
toml_12953
Guru

Joined: 13/02/2015
Location: United States
Posts: 326
Posted: 03:12pm 27 Sep 2021
Copy link to clipboard 
Print this post

  lizby said  
  JohnS said  Does it now work without needing to be root/sudo? The above looks good.

No. Still fails at "echo 'out' > /sys/class/gpio/gpio12/direction" with "sh: 1: cannot create /sys/class/gpio/gpio12/direction: Permission denied"

But when I immediately run it again, it works for gpio12 and fails at gpio16. If I run it 8 times, it progresses 1 pin further each time, and on the 9th run works. With "sudo mmbasic" it works from the beginning.

Puzzling.


Puzzling is an understatement! Once all pins work, do they keep working for runs 10 and up?
 
matherp
Guru

Joined: 11/12/2012
Location: United Kingdom
Posts: 8578
Posted: 03:43pm 27 Sep 2021
Copy link to clipboard 
Print this post

  Quote  But when I immediately run it again, it works for gpio12 and fails at gpio16. If I run it 8 times, it progresses 1 pin further each time, and on the 9th run works. With "sudo mmbasic" it works from the beginning.


I wonder why I gave up on the Raspberry Pi  
 
JohnS
Guru

Joined: 18/11/2011
Location: United Kingdom
Posts: 3656
Posted: 04:16pm 27 Sep 2021
Copy link to clipboard 
Print this post

  lizby said  
  JohnS said  Does it now work without needing to be root/sudo? The above looks good.

No. Still fails at "echo 'out' > /sys/class/gpio/gpio12/direction" with "sh: 1: cannot create /sys/class/gpio/gpio12/direction: Permission denied"

But when I immediately run it again, it works for gpio12 and fails at gpio16. If I run it 8 times, it progresses 1 pin further each time, and on the 9th run works. With "sudo mmbasic" it works from the beginning.

Puzzling.

Your previous response showed you had no gpio12 so please explain where it has come from.

You look to have left out important information, thus making assistance impossible.

Please try to provide the same level of detail that Peter would ask for.

BTW, despite Peter's unhelpful snipes (which you would be wise to ignore) I'm afraid it's something you're doing or not doing rather than something wrong with Linux.

John
Edited 2021-09-28 02:35 by JohnS
 
thwill

Guru

Joined: 16/09/2019
Location: United Kingdom
Posts: 3839
Posted: 04:44pm 27 Sep 2021
Copy link to clipboard 
Print this post

  JohnS said  BTW, despite Peter's unhelpful snipes (which you would be wise to ignore) I'm afraid it's something you're doing or not doing rather than something wrong with Linux.


Perhaps that is part of it, but the Pi does appear to be quirky and apocraphally GPIO seems to be in a constant state of flux, with different people (I'm not talking about the denziens of TBS) reporting different things on different versions and then writing incoherent solutions that looks like they've done a lot of random sh*t and then guessed what fixed their problem ... or maybe that is just me.

Is there something that needs to be done with /dev/mem ?

https://www.element14.com/community/thread/19995/l/anyone-know-how-to-access-raspi-gpio-without-sudo

Best wishes,

Tom
Edited 2021-09-28 02:47 by thwill
Game*Mite, CMM2 Welcome Tape, Creaky old text adventures
 
     Page 3 of 11    
Print this page
© JAQ Software 2024