Home
JAQForum Ver 24.01
Log In or Join  
Active Topics
Local Time 13:40 10 May 2025 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: The Raspberry Pi GPIO thread

Author Message
thwill

Guru

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

Hi folks,

As an experiment and in a possibly vain attempt to maintain focus I've spun out this thread from the main MMB4L alpha thread.

I'd appreciate it if we could discuss Raspberry Pi GPIO with MMB4L and lament the death of the PiCRomite here rather than there.

Best wishes,

Tom
MMBasic for Linux, Game*Mite, CMM2 Welcome Tape, Creaky old text adventures
 
lizby
Guru

Joined: 17/05/2016
Location: United States
Posts: 3309
Posted: 12:20pm 28 Sep 2021
Copy link to clipboard 
Print this post

  thwill said  
  lizby said  Bingo! Adding PAUSE 500 after each system call makes it work. Thanks.

For that matter, it works with PAUSE 100.


Does this mean it now works without "sudo", just requiring the user to be a member of the "gpio" group, or is the problem that has been solved something else ?


Runs without "sudo". Start with "mmbasic", 'load "pinpi.bas"' (the program listed in the other thread, with PAUSE 100 added after the system calls), 'run', and it works.

(Sorry for so much time going down the rabbit hole of "sudo" and permissions on the pi.)
PicoMite, Armmite F4, SensorKits, MMBasic Hardware, Games, etc. on fruitoftheshed
 
lizby
Guru

Joined: 17/05/2016
Location: United States
Posts: 3309
Posted: 02:23pm 28 Sep 2021
Copy link to clipboard 
Print this post

  robert.rozee said  for many folks, just being able to set a few pins high/low or read a few switch inputs would likely suffice. there is also a serial port on a couple of the pins, which would then open up the opportunity to connect a slave mx170 (itself running mmbasic) to use as an 'intelligent peripheral'.

cheers,
rob   :-)

All coming (we hope) without the SYSTEM hack.

  JohnS said  
  thwill said  
  lizby said  Bingo! Adding PAUSE 500 after each system call makes it work. Thanks.

For that matter, it works with PAUSE 100.


Does this mean it now works without "sudo", just requiring the user to be a member of the "gpio" group, or is the problem that has been solved something else ?

Best wishes,

Tom
I understand you just need to be in gpio and to have a short delay after (each? most? some?) file system (well, gpio subsystem) change.

I don't know if anyone will want to fine tune the delays. It's handy to have a short  test program, regardless of delay length.

John

Works without "sudo". I was wrong about PAUSE 100 working--it worked after running with PAUSE 500 because that had created the directories and files. After power-cycling, it didn't work, and PAUSE 200 did.
' pinpi.bas performs digital I/O with MMB4L
'
dim integer i,j,k,p(4)=(0,12,16,20,21)
dim integer pState(16)
dim integer q(4)=(0,25,8,7,1), dout=1, din=0

for i=1 to 4
 setpin p(i),dout
 cmd$="echo '1' > /sys/class/gpio/gpio"+str$(p(i))+"/value"
 system(cmd$)
 pause 500
next
for i=1 to 4
 setpin q(i),din
 cmd$="echo '0' > /sys/class/gpio/gpio"+str$(p(i))+"/value"
 system(cmd$)
 pause 500
next

do
 for i=1 to 4
   open "/sys/class/gpio/gpio"+str$(q(i))+"/value" for input as #1
   a$=input$(1,#1): close #1 ' 0 or 1
   cmd$="echo '"+a$+"' > /sys/class/gpio/gpio"+str$(p(i))+"/value"
'    print cmd$
   system(cmd$)
 next i
 pause 2000
loop

sub setpin(pinno as integer, pinmode as integer)
 local string dirname$, cmd$
 dirname$="/sys/class/gpio/gpio"+str$(pinno)
 if not MM.INFO(EXISTS DIR dirname$) then
   cmd$="echo '"+str$(pinno)+"' > /sys/class/gpio/export 2>/dev/null"
'    print cmd$
   system(cmd$)
   pause 200
 endif
 dirname$=dirname$+"/direction"
 if pinmode then
   cmd$="echo 'out' > "+dirname$
 else
   cmd$="echo 'in' > "+dirname$
   system(cmd$)
   pause 200
   cmd$="gpio -g mode "+str$(pinno)+" up" ' set weak pullup
 endif
 ?pinno,cmd$
 system(cmd$)
 pause 200
end sub


You don't need to wire up all the pins to test--just gpio25 with a pushbutton switch to 0V, and gpio12 with an LED + appropriate resistor (680? 1k?) to 0V.

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

Guru

Joined: 16/09/2019
Location: United Kingdom
Posts: 4251
Posted: 02:47pm 28 Sep 2021
Copy link to clipboard 
Print this post

  lizby said  All coming (we hope) without the SYSTEM hack.


Which is as good a way as any to announce that MMB4L is now officially a two-man show .

  lizby said  I was wrong about PAUSE 100 working--it worked after running with PAUSE 500 because that had created the directories and files. After power-cycling, it didn't work, and PAUSE 200 did.


Local i%
For i% = 1 To 600
 If Not Mm.Info(Exists path$) Pause 10
Next
If Not Mm.Info(Exists path$) Error "I hate the GPIO"


It would be even more efficient to do all the "creation" up front and then separately wait for it all to complete.

Note that Mm.Info(Exists f$) is "unique" to MMB4L, I only take slight pleasure from having implemented it after Peter said he wouldn't on the CMM2 . There is also Mm.Info(Exists Symlink f$) to tell you whether an entry is a symbolic link or not and Mm.Info$(Arch) to find out which build/platform of MMB4L is running: "Android aarch64", "Linux x86_64", "Linux aarch64", "Linux armv7l" or "Linux i686".

Best wishes,

Tom
Edited 2021-09-29 01:34 by thwill
MMBasic for Linux, Game*Mite, CMM2 Welcome Tape, Creaky old text adventures
 
JohnS
Guru

Joined: 18/11/2011
Location: United Kingdom
Posts: 3998
Posted: 07:29pm 28 Sep 2021
Copy link to clipboard 
Print this post

  thwill said  It would be even more efficient to do all the "creation" up front and then separately wait for it all to complete.

I expect so. It wasn't at all obvious up front, though LOL

I _think_ the only reason a delay is needed is that system() is (by definition) spawning a sub-process and we need to give it a chance to run.

I guess that the code inside MMBasic does not wait for the sub-process to run to completion (i.e. does not wait for it to run & exit).   If it did, I think no delay would be needed.

The code could be changed to check that guess, of course.

(It's a form of software race.)

John
Edited 2021-09-29 05:39 by JohnS
 
thwill

Guru

Joined: 16/09/2019
Location: United Kingdom
Posts: 4251
Posted: 11:18am 29 Sep 2021
Copy link to clipboard 
Print this post

  JohnS said  I _think_ the only reason a delay is needed is that system() is (by definition) spawning a sub-process and we need to give it a chance to run.

I guess that the code inside MMBasic does not wait for the sub-process to run to completion (i.e. does not wait for it to run & exit).   If it did, I think no delay would be needed.

The code could be changed to check that guess, of course.

(It's a form of software race.)

John


This hypothesis certainly meets some of the facts ...

... however the SYSTEM command is a simple wrapper around the C system() function which isn't supposed to return until the command has been completed.

Posisbly in this particular case once system() has done its thing an independent O/S daemon has to come along and notice the change and that is what we need to wait for. If this is the case there is nothing that can be done in the general case by MMB4L.

Just guessing of course.

Best wishes,

Tom
MMBasic for Linux, Game*Mite, CMM2 Welcome Tape, Creaky old text adventures
 
lizby
Guru

Joined: 17/05/2016
Location: United States
Posts: 3309
Posted: 12:34pm 29 Sep 2021
Copy link to clipboard 
Print this post

Part of what really confounded me was that the code did not need the PAUSE 200 with "sudo mmbasic", but as we found out, did need it without the sudo. I couldn't speculate on what the reason for that would be, but perhaps someone else can.
PicoMite, Armmite F4, SensorKits, MMBasic Hardware, Games, etc. on fruitoftheshed
 
thwill

Guru

Joined: 16/09/2019
Location: United Kingdom
Posts: 4251
Posted: 02:17pm 29 Sep 2021
Copy link to clipboard 
Print this post

  lizby said  Part of what really confounded me was that the code did not need the PAUSE 200 with "sudo mmbasic", but as we found out, did need it without the sudo. I couldn't speculate on what the reason for that would be, but perhaps someone else can.


I agree that is still a condundrum.
MMBasic for Linux, Game*Mite, CMM2 Welcome Tape, Creaky old text adventures
 
JohnS
Guru

Joined: 18/11/2011
Location: United Kingdom
Posts: 3998
Posted: 02:47pm 29 Sep 2021
Copy link to clipboard 
Print this post

Er, me too.

I conclude the PAUSE can't work - yet it does.

My brain hurts.

I wish I had the hardware & quite a bit of time to mess with it (& maybe read the kernel code involved)!!

(I have non-RPi Linux-running ARM hardware with GPIOs I could try, too, with yet more time I don't have.)

John
 
lizby
Guru

Joined: 17/05/2016
Location: United States
Posts: 3309
Posted: 03:15pm 29 Sep 2021
Copy link to clipboard 
Print this post

  TassyJim said  Back before Peter's RPI basic had serial port support, I experimented with treating the serial as a file.
...
Not sure if the same code works on Tom's code. I am not in a position to play with Linux at the moment.

Jim

Jim--I will try that. Currently upgrading my piZW from stretch to buster--a long process. Will try when back up again. Interesting that Tom's compile was for Buster (apparently), but worked with no problem on Stretch.

... about 3 hours later and taking 1 minute to compile mmb4l on the PiZW (and having to use sudo mmbasic), trying your code and talking to /dev/serial1 with a loopback,  (after stty -F /dev/serial1 9600) I get "Illegal seek"
Changing "random" to "output" I get "Bad file descriptor" when it gets to the INPUT line. So there's some hope, but no joy yet.

Again, I'll fiddle with this if someone has ideas, but it's not the direction mmb4l will ultimately go in, so I don't want to spend a lot of time with it.

' linux serial test program
'  OPEN "/dev/ttyUSB0" FOR RANDOM AS #5 ' open the serial port
'  OPEN "/dev/serial1" FOR RANDOM AS #5 ' open the serial port
 OPEN "/dev/serial1" FOR output AS #5 ' open the serial port
 PRINT #5, "HELLO!!! Is anyone out there?"
 DO ' one way to receive serial data which might not be there!
   k$ = INPUT$(1,#5)
   IF k$="" THEN
     nodata=nodata+1
   ELSE
     result$=result$+k$
     PRINT result$
     nodata=0
   ENDIF
   PAUSE 20
 LOOP UNTIL k$=CHR$(10)OR nodata=50 ' 50*20ms = 1 second timeout
 CLOSE #5

I tried a number of other things which caused me to have to power-cycle, and with /dev/tty1 succeeded in writing to the console.
PicoMite, Armmite F4, SensorKits, MMBasic Hardware, Games, etc. on fruitoftheshed
 
thwill

Guru

Joined: 16/09/2019
Location: United Kingdom
Posts: 4251
Posted: 07:07pm 29 Sep 2021
Copy link to clipboard 
Print this post

Not sure what @TassyJim and @lizby are smoking but how are you supposed to perform RANDOM I/O on a serial port ... it's S E R I A L and RANDOM requires the ability to go forwards and backwards doesn't it ?

Best wishes,

Tom
MMBasic for Linux, Game*Mite, CMM2 Welcome Tape, Creaky old text adventures
 
lizby
Guru

Joined: 17/05/2016
Location: United States
Posts: 3309
Posted: 07:23pm 29 Sep 2021
Copy link to clipboard 
Print this post

  thwill said  @TassyJim and @lizby are smoking

Hey, I'm just being a copycat. I tried it, it didn't work. I tried something different.
PicoMite, Armmite F4, SensorKits, MMBasic Hardware, Games, etc. on fruitoftheshed
 
thwill

Guru

Joined: 16/09/2019
Location: United Kingdom
Posts: 4251
Posted: 07:33pm 29 Sep 2021
Copy link to clipboard 
Print this post

This works from my Ubuntu 20 talking to a CMM2 sitting on /dev/ttyS0.

(Actually the Ubuntu 20 is a guest sitting in a Virtual Box VM on a Win10 machine with COM3 of the host mapped to COM1 of the guest.)

You need to ADD YOUR USER TO THE dialout GROUP otherwise you have to use sudo:
sudo adduser thwill dialout


' I don't understand half this incantation, but whilst experimenting I
' discovered that 'screen /dev/ttyS0 115200' connected to the CMM2
' successfully and these correspond to the difference between the settings
' before and after 'screen' was run.
System "stty -F /dev/ttyS0 115200 min 100 time 2 cs8 -cstopb -parenb -hupcl brkint ignpar -icrnl -opost -onlcr -isig -icanon -echo"

Open "/dev/ttyS0" For Output As #1
Open "/dev/ttyS0" For Input As #2

Dim s$

Print #1, "Print " + Chr$(34) + "Hello MMB4L" + Chr$(34)
Line Input #2, s$
Print s$

Dim i
For i = 1 To 5
 Print #1, "Print " + Chr$(34) + Str$(i) + Chr$(34)
 Line Input #2, s$
 Print s$
 Line Input #2, s$
 Print s$
Next


And when run it generates the following in both the MMB4L console and on the CMM2 screen:

Print "Hello MMB4L"
Hello MMB4L
> Print "1"
1
> Print "2"
2
> Print "3"
3
> Print "4"
4
> Print "5"


That's not bad going for someone who has done no serial comms before and is literally making it up as he goes along .

Best wishes,

Tom
Edited 2021-09-30 05:34 by thwill
MMBasic for Linux, Game*Mite, CMM2 Welcome Tape, Creaky old text adventures
 
TassyJim

Guru

Joined: 07/08/2011
Location: Australia
Posts: 6220
Posted: 09:28pm 29 Sep 2021
Copy link to clipboard 
Print this post

  thwill said  Not sure what @TassyJim and @lizby are smoking but how are you supposed to perform RANDOM I/O on a serial port ... it's S E R I A L and RANDOM requires the ability to go forwards and backwards doesn't it ?

Best wishes,

Tom


Other way around.
If you want to seek back and forth, you need RANDOM.
But you can still do a normal read and write while in RANDOM.

RANDOM was needed for it to work in the original but I am not surprised that the current code behaves differently.

Jim
VK7JH
MMedit
 
lizby
Guru

Joined: 17/05/2016
Location: United States
Posts: 3309
Posted: 12:41am 30 Sep 2021
Copy link to clipboard 
Print this post

Running Tom's program with /dev/ttyS0 changed to /dev/serial1 causes the PiZW to lock up in a way that requires power-cycling.

$ ls -l /dev/serial1
lrwxrwxrwx 1 root root 7 Sep 29 21:17 /dev/serial1 -> ttyAMA0

Same if serial1 is replaced with ttyAMA0.

Following this link did not help: https://www.abelectronics.co.uk/kb/article/1035/raspberry-pi-3--4-and-zero-w-serial-port-usage

Same thing occurs with /dev/ttyS0.

minicom -b 9600 -o -D /dev/ttyS0 -- does not echo back when Tx is tied to Rx (but does not lock up--can be exited with Ctrl+A X; also, when connected to the laptop with a usb/serial device, nothing shows on either end when typing on either end.

(? is wiringPi getting in the way?)


~
Edited 2021-09-30 12:56 by lizby
PicoMite, Armmite F4, SensorKits, MMBasic Hardware, Games, etc. on fruitoftheshed
 
thwill

Guru

Joined: 16/09/2019
Location: United Kingdom
Posts: 4251
Posted: 01:21pm 30 Sep 2021
Copy link to clipboard 
Print this post

  TassyJim said  Other way around.
If you want to seek back and forth, you need RANDOM.
But you can still do a normal read and write while in RANDOM.

RANDOM was needed for it to work in the original but I am not surprised that the current code behaves differently.


Thanks for the correction Jim, I suspect it bears further investigation.

Best wishes,

Tom
MMBasic for Linux, Game*Mite, CMM2 Welcome Tape, Creaky old text adventures
 
Print this page


To reply to this topic, you need to log in.

The Back Shed's forum code is written, and hosted, in Australia.
© JAQ Software 2025