gpiolib and gpiomem, RPi

Discussions related specifically to the Windows, Linux (86), Mac OS-X and Raspberry Pi editions of BB4W and BBCSDL
Newlands
Posts: 29
Joined: Mon 29 Jan 2024, 16:18

gpiolib and gpiomem, RPi

Post by Newlands »

I am trying to re-learn BASIC - some years ago I wrote a Basic program to control a couple of stepper motors, which ran successfully on a BBC Master. I no longer have a copy of it, just some vague memories. I'm now trying to re-create the project using a RPi.
I am using a Raspberry Pi 5, and one obviously necessary part of the project will be to access the GPIO pins to output signals to the stepper motor drivers. I can see that BBCSDL has gpiolib installed, but when I try this sample program..

Code: Select all

10 REM blink.bbc  
20 REM Simple blink program in BBC BASIC
30 REM by Richard Russell, http://www.rtrussell.co.uk/
40
50 INSTALL @lib$+"gpiolib"
60
70 REM Initialise GPIO:
80 gpio% = FN_gpio_setup
90 
100 REM Set GPIO pin 7 to output mode:
110 PROC_gpio_inp(gpio%, 7) : REM Must use PROC_gpio_inp() before PROC_gpio_out()
120 PROC_gpio_out(gpio%, 7)
130 
140 pin7% = %10000000 : REM Bit mask
150 REPEAT
160		PROC_gpio_set(gpio%, pin7%)
170		WAIT 50 : REM 0.5 seconds
180		PROC_gpio_clr(gpio%, pin7%)
190		WAIT 50
200 UNTIL FALSE
210 END
..I get an error message 'Cannot open /dev/gpiomem'.
Here is where I admit that I am not a programmer, and also struggle with Linux. My limited research tells me that gpiomem is a device, and that it is possible that only the root user can open it, which might explain the error message, though I'm not certain about this. Any help in trying to understand this will be gratefully received, especially if written in simple layman's terms!
Newlands
Posts: 29
Joined: Mon 29 Jan 2024, 16:18

Re: gpiolib and gpiomem, RPi

Post by Newlands »

After reading through your post and the associated links I think I understand a little more.
I typed in:

Code: Select all

ls -l /dev
and in the list of results I can see:

Code: Select all

crw-rw---- 1 root gpio 234 0 Jan 29 17:38  gpiomem0
crw-rw---- 1 root gpio 238 0 Jan 29 17:38  gpiomem1
crw-rw---- 1 root gpio 237 0 Jan 29 17:38  gpiomem2
crw-rw---- 1 root gpio 236 0 Jan 29 17:38  gpiomem3
crw-rw---- 1 root gpio 235 0 Jan 29 17:38  gpiomem4
This ties in with the thinking that there are now 5 gpiomem's as described in your link (which also says that only gpiomem0 is going to be used by most users). So next questions:

Does the above ls -l result indicate that someone has already sorted out gpio on the RPi 5 distribution?

Is there a file somewhere that I can edit to point to gpiomem0 instead of gpiomem? Or is it either not that simple, or simply beyond the capabilities of a non-programmer like me?

edit - I just looked at the gpiomem.bbc file but it is obviously protected against people like me!
Newlands
Posts: 29
Joined: Mon 29 Jan 2024, 16:18

Re: gpiolib and gpiomem, RPi

Post by Newlands »

I managed to change the gpiolib.bbc file to read
"open", "/dev/gpiomem0"
but when I run it now it tells me 'Bad library at line 5' obviously referring to the library gpiolib "INSTALL @lib$+"gpiolib"

To be honest, I think I'm completely stumped now - its clear that the Pi 5 has a completely different way of talking to the GPIO pins and that I don't have the knowledge to either understand it or work round it.
Newlands
Posts: 29
Joined: Mon 29 Jan 2024, 16:18

Re: gpiolib and gpiomem, RPi

Post by Newlands »

It may be that part of the problem is that the only way I can edit the gpiolib file is on my MacBook. Opening it in text editor results in a file which looks very little like when its opened on the RPi OS, and presumably saving it on the Mac and transferring it to the Pi, even with the .bbc extension, is not the correct thing to do.
As for trying to edit on the Pi, I have changed the permissions of all files in the /lib folder on the Pi to rwx for user, group and others, but even then, I still can't edit it, either with the text editor (called Mousepad) in the desktop environment or with sudo nano in a terminal window. All this is complicated by the fact that every terminal operation has to be preceded with a Google search to find out what the appropriate Linux command and syntax is.
Simply put, I can't change gpiolib with gpiolib0
As for the Pi OS, I installed the recommended version which is Bookworm, 64 bit, so that's another complication! I guess I could remove the sd card and stick a 32 bit image on if that is going to help.
Newlands
Posts: 29
Joined: Mon 29 Jan 2024, 16:18

Re: gpiolib and gpiomem, RPi

Post by Newlands »

I have installed BBCSDL on both the Pi and on the Mac - I installed it on the Mac first as I had, at the time, yet to finish setting up the Pi. The Pi version seems to be working fine, and for example will run the sample programs banner.bbc, bezierfit.bbc, dlgdemo.bbc, bbcclock.bbc quite happily.
As I said before, a BBC BASIC library must be in internal, tokenised, format otherwise it won't be accepted by the INSTALL statement. You cannot edit it using a text editor (you could convert it to plain text, edit it, and then convert it back, but what would be the point?).
You've lost me here. If I shouldn't use a text editor, what application should I be using to edit the gpiolib.bbc file? I did try using sudo nano in a terminal window, and while it displayed the file, there was no way to edit it (and that was after changing the permissions to rwx for all users).

I installed BBC in /home/srp/Basic, srp obviously being my user name.
To adapt gpiolib.bbc to be compatible with 64-bit PiOS, do a global search-and-replace of G% to G%%
This sounds like more editing, which I have so far failed at completely!

I'm sorry if I'm being really dim here - I appreciate the help!
Newlands
Posts: 29
Joined: Mon 29 Jan 2024, 16:18

Re: gpiolib and gpiomem, RPi

Post by Newlands »

Yes, that was really daft of me, and it really should have been obvious. A case of not seeing the wood for the trees. As you said, it took about 30 seconds to edit the file. I have run the blink program again and don't get the same error message as before. A dialog does briefly come up, but it is extremely brief before it shrinks away and disappears, so tomorrow I will set up a breadboard and led to see what, if anything, is happening at pin 7.
I have also discovered pinctrl, which gives the status of all the pins - I was hoping it might help in monitoring the gpio pins but didn't get very far with it - an led is probably going to be a simpler way forward.
Newlands
Posts: 29
Joined: Mon 29 Jan 2024, 16:18

Re: gpiolib and gpiomem, RPi

Post by Newlands »

No luck connecting an led to Pin 7, which I'm not all that surprised about. The Blink.bbc program works as I described in the last post - a fleeting glimpse of a disappearing dialog box (far too quick to read) but otherwise nothing happens.

The Pi bistro has libgpiod installed, which allows access to the pins from a terminal window, with commands such as gpioset gpiochip4 21=1 to set a pin high and gpioset gpiochip4 21=0 to set it low, and that works. (The '21' refers to gpio21 which is pin no. 40).

As a side note, typing gpioinfo gives details of five 'chips' - gpiochip0 to gpiochip4 - and it this last one that controls the 40 pins on the header. That's about as far as I can get.

To be honest I think the only sensible thing I can do now is to get hold of a RPi 4 which doesn't have this RP1 chipset controlling the GPIO pins. If I'm the only one with a Pi 5 trying to do this it's clearly too much to ask that the app is re-written for my benefit! Thanks for the help though, if nothing else it has exercised my remaining brain cells for a while.
Newlands
Posts: 29
Joined: Mon 29 Jan 2024, 16:18

Re: gpiolib and gpiomem, RPi

Post by Newlands »

This is the program I downloaded:

Code: Select all

10 REM blink.bbc  
20 REM Simple blink program in BBC BASIC
30 REM by Richard Russell, http://www.rtrussell.co.uk/
40
50 INSTALL @lib$+"gpiolib"
60
70 REM Initialise GPIO:
80 gpio% = FN_gpio_setup
90 
100 REM Set GPIO pin 7 to output mode:
110 PROC_gpio_inp(gpio%, 7) : REM Must use PROC_gpio_inp() before PROC_gpio_out()
120 PROC_gpio_out(gpio%, 7)
130 
140 pin7% = %10000000 : REM Bit mask
150 REPEAT
160		PROC_gpio_set(gpio%, pin7%)
170		WAIT 50 : REM 0.5 seconds
180		PROC_gpio_clr(gpio%, pin7%)
190		WAIT 50
200 UNTIL FALSE
210 END
I'm going to play with it a bit more, as now that I have been able to access the gpio pins via a terminal command there is a bit of hope I guess.
Newlands
Posts: 29
Joined: Mon 29 Jan 2024, 16:18

Re: gpiolib and gpiomem, RPi

Post by Newlands »

I can't remember where I got it from, and that's despite trawling back through my history.

I have borrowed a Pi 4 now, which unfortunately has a 64 bit OS installed and no spare SD card either. So is it only the gpio% that needs changing to gpio%%, or do I have to double up on all the instances of the % sign, eg pin7% ?

What does this line do and does it need to be changed for different pin numbers?

Code: Select all

pin7% = %10000000 : REM Bit mask
Newlands
Posts: 29
Joined: Mon 29 Jan 2024, 16:18

Re: gpiolib and gpiomem, RPi

Post by Newlands »

In the past few days I have trawled through hundreds of pages related to this, most of them indecipherable Linux stuff. I just can't find the one that had your blink program on it!

I have wiped the SD card and installed a fresh 32 bit OS. I can now run the blink.bbc program successfully at last, which is not particularly an achievement (of mine) other than showing that your program runs exactly as intended on a 32 bit RPi OS.
I'm afraid I don't understand the bit mask thing at all. I tried changing the output pin to No 21, and nothing happened. Maybe pin 21 is not a normal gpio pin, or maybe this particular line:

Code: Select all

pin7% = %10000000 : REM Bit mask
is peculiar to pin 7, and I would need to change it for any other pin. To be quite honest, I have no idea where 10000000 comes from (presumably binary for 128???). Is there somewhere that I can refer to so I can find out how to write this line for other pins?

My thinking is that the mask acts like an AND operator, so
pin 7 is 00000111
mask is 10000000 (128 decimal)
result =00000000 ( or might it be 135 because its an OR operator, ie 10000111)
but that's as far as I've got.