pi gpio bbc basic

on-topic acorn-related discussions not covered by the other forums
avtovaz
Posts: 81
Joined: Wed Jan 18, 2017 5:09 pm
Contact:

pi gpio bbc basic

Post by avtovaz » Mon Dec 23, 2019 7:02 pm

i know this is probably a simple question, but can you just point me in the right direction please, ive got the pi set up, its got risc os set up, i get into bbc basic, and now i want to control the input output pins.

is there a guide or example someone could show me please?

i cant seem to find anything relating to bbc basic on the pi

thanks! [-o<

User avatar
flaxcottage
Posts: 4256
Joined: Thu Dec 13, 2012 8:46 pm
Location: Derbyshire
Contact:

Re: pi gpio bbc basic

Post by flaxcottage » Mon Dec 23, 2019 8:41 pm

You could try here:

https://www.raspberrypi.org/forums/view ... hp?t=22250

Tank's GPIO module comes with its own documentation. The module has been updated for the later Pi variants too.

There is also a MCPIO module for use with the CISECO Slice of PI/O hat as well.

Failing that one could use the built-in assembler in BBC BASIC in RISCOS to program the GPIO pins directly. The book Raspberry Pi RISCOS System Programming Revealed by Bruce Smith is a good primer.
- John

Image

avtovaz
Posts: 81
Joined: Wed Jan 18, 2017 5:09 pm
Contact:

Re: pi gpio bbc basic

Post by avtovaz » Mon Dec 23, 2019 8:57 pm

thats it! thankyou i have somewhere to look and start now!!!

User avatar
Richard Russell
Posts: 1668
Joined: Sun Feb 27, 2011 10:35 am
Location: Downham Market, Norfolk
Contact:

Re: pi gpio bbc basic

Post by Richard Russell » Mon Dec 23, 2019 10:11 pm

avtovaz wrote:
Mon Dec 23, 2019 7:02 pm
i cant seem to find anything relating to bbc basic on the pi
I know you are asking principally about RISC OS, but for completeness the Raspberry Pi edition of BBC BASIC for SDL 2.0 (which runs under Raspbian, on a RPi 3 or 4) supports controlling the GPIO via the supplied library 'gpiolib.bbc'. Here is a demonstration of it in action, with the program listed below:

rpigpio.gif

Code: Select all

      REM Initialise GPIO:
      INSTALL @lib$ + "gpiolib"
      GPIO% = FN_gpio_setup

      REM Pin numbers to activate, in sequence:
      DATA 17, 23, 25, 12, 16, 26

      REM Set to input first:
      RESTORE
      FOR I% = 1 TO 6
        READ pin%
        PROC_gpio_inp(GPIO%, pin%)
      NEXT

      REM Set pins to output:
      RESTORE
      FOR I% = 1 TO 6
        READ pin%
        PROC_gpio_out(GPIO%, pin%)
      NEXT

      REM Cycle LEDs in sequence:
      REPEAT
        RESTORE
        FOR I% = 1 TO 6
          READ pin%
          PROC_gpio_set(GPIO%, 1<<pin%)
          WAIT 20
          PROC_gpio_clr(GPIO%, 1<<pin%)
        NEXT
      UNTIL FALSE
      END
Last edited by Richard Russell on Sun Feb 23, 2020 10:28 am, edited 1 time in total.

User avatar
helpful
Posts: 634
Joined: Tue Sep 22, 2009 1:18 pm
Location: London
Contact:

Re: pi gpio bbc basic

Post by helpful » Mon Dec 23, 2019 10:56 pm

Andrew Conroy has released some software using GPIO from BBC BASIC. They might be useful to look through for inspiration.

http://www.owlart.co.uk/Software/

Bryan.
RISC OS User Group Of London - http://www.rougol.jellybaby.net/
RISC OS London Show - http://www.riscoslondonshow.co.uk/

avtovaz
Posts: 81
Joined: Wed Jan 18, 2017 5:09 pm
Contact:

Re: pi gpio bbc basic

Post by avtovaz » Sun Dec 29, 2019 8:41 pm

Richard Russell wrote:
Mon Dec 23, 2019 10:11 pm
avtovaz wrote:
Mon Dec 23, 2019 7:02 pm
i cant seem to find anything relating to bbc basic on the pi
I know you are asking principally about RISC OS, but for completeness the Raspberry Pi edition of BBC BASIC for SDL 2.0 (which runs under Raspbian, on a RPi 3 or 4) supports controlling the GPIO via the supplied library 'gpiolib.bbc'. Here is a demonstration of it in action, with the program listed below:


rpigpio.gif

Code: Select all

      REM Initialise GPIO:
      INSTALL @lib$ + "gpiolib"
      GPIO% = FN_gpio_setup

      REM Pin numbers to activate, in sequence:
      DATA 17, 23, 25, 12, 16, 26

      REM Set to input first:
      RESTORE
      FOR I% = 1 TO 6
        READ pin%
        PROC_gpio_inp(GPIO%, pin%)
      NEXT

      REM Set pins to output:
      RESTORE
      FOR I% = 1 TO 6
        READ pin%
        PROC_gpio_out(GPIO%, pin%)
      NEXT

      REM Cycle LEDs in sequence:
      REPEAT
        RESTORE
        FOR I% = 1 TO 6
          READ pin%
          PROC_gpio_set(GPIO%, 2^pin%)
          WAIT 20
          PROC_gpio_clr(GPIO%, 2^pin%)
        NEXT
      UNTIL FALSE
      END

thanks for the replies!

So i have a raspberry pi 3, ist running rosc os pi, i typed that program into bbc basic and it failed at

INSTALL @lib$ + "gpiolib"

unknown or missing variable.

install isnt a command ive seen before? im i being completly stupid?




the point of what im doing is to controlla 8 channel relay board, and monitor some switches for a project in doing. I can program lightly in basic, but its when file needs to talk to this one and that one is not installed properly , and ... it just really confuses me.

thanks

User avatar
Richard Russell
Posts: 1668
Joined: Sun Feb 27, 2011 10:35 am
Location: Downham Market, Norfolk
Contact:

Re: pi gpio bbc basic

Post by Richard Russell » Sun Dec 29, 2019 9:08 pm

avtovaz wrote:
Sun Dec 29, 2019 8:41 pm
install isnt a command ive seen before? im i being completly stupid?
I said the program was for BBC BASIC for SDL 2.0 running under Raspbian, so it was never going to work under RISC OS. :?

INSTALL is a command in ARM BASIC (it is closely related to the LIBRARY and OVERLAY commands) but the gpiolib library is BBCSDL-specific and I've no idea if it would be possible to adapt it for RISC OS.

avtovaz
Posts: 81
Joined: Wed Jan 18, 2017 5:09 pm
Contact:

Re: pi gpio bbc basic

Post by avtovaz » Sun Dec 29, 2019 9:23 pm

right,thanks, thats why then, i was properly getting confused.

So ill install raspbian then maybe the easier option for me.

about the install bit, i looked in my BBC micro guide and could not see teh command, but i wont do will it lol #-o

User avatar
Richard Russell
Posts: 1668
Joined: Sun Feb 27, 2011 10:35 am
Location: Downham Market, Norfolk
Contact:

Re: pi gpio bbc basic

Post by Richard Russell » Mon Dec 30, 2019 10:06 am

avtovaz wrote:
Sun Dec 29, 2019 9:23 pm
So ill install raspbian then maybe the easier option for me.
If it's a RPi 3 or 4 that should work fine. BBC BASIC for SDL 2.0 will run on a RPi 2 but it's too slow to be 'comfortable' (especially the IDE).

avtovaz
Posts: 81
Joined: Wed Jan 18, 2017 5:09 pm
Contact:

Re: pi gpio bbc basic

Post by avtovaz » Sun Feb 16, 2020 9:00 pm

thanks for the reply, on your example, im struggling a bit.

you install the gpio lib file
gpio%= fn_gpio_setup - is that returning some true or false condition ?

you set the pins with the data line

set to input
set to output, why do you do this? i cant work that out either

then you set the pins and clear the pins. (GPIO,2^pin%) .. what does the gpio bit do, and what is the 2^pin% part?


sorry i am learning this, its been a while since i have used bbc basic, and when i did i never did this much detail.

User avatar
Richard Russell
Posts: 1668
Joined: Sun Feb 27, 2011 10:35 am
Location: Downham Market, Norfolk
Contact:

Re: pi gpio bbc basic

Post by Richard Russell » Sun Feb 16, 2020 10:21 pm

avtovaz wrote:
Sun Feb 16, 2020 9:00 pm
gpio%= fn_gpio_setup - is that returning some true or false condition ?
No, it's what is sometimes called a 'handle' or a 'pointer'; really you don't need to know what it actually is, you treat it as an 'opaque' value that you simply store in a variable and pass to the other functions as required. This is a very common technique.
set to input
set to output, why do you do this? i cant work that out either
I don't know why you have to set the pins to input first either, you just do. Sometimes it's better not to ask too many questions!
then you set the pins and clear the pins. (GPIO,2^pin%) .. what does the gpio bit do
As I explained above, GPIO% is just the value returned from FN_gpio_setup. If you know anything about code libraries you'll know that it's bad practice for them to use global variables. I won't bore you with the details, but one way of avoiding global variables can be for the 'calling' program to act as a go-between, and pass a value from one library function to another. That's what's happening here.
and what is the 2^pin% part?
It's a bit-mask. You can set or clear more than one GPIO pin at a time, by passing a 32-bit mask in which each '1' bit causes the relevant action (setting or clearing) to happen to that specific pin, and each '0' bit means that the pin should be ignored (left in its current state). In the specific code you are referring to just a single pin is set or cleared at a time, so the bit-mask corresponds to a single bit being '1' and the other 31 bits being '0'. This is the simplest case but it may occasionally be useful to set or clear multiple pins at the same time.

avtovaz
Posts: 81
Joined: Wed Jan 18, 2017 5:09 pm
Contact:

Re: pi gpio bbc basic

Post by avtovaz » Mon Feb 17, 2020 8:38 pm

right ive read that maybe 20 times since you posted it, and thankyou! i now understand a lot more.

the bit that really got me was about the 2^pin%, and then it clicked, 2^ converts decimal pin number into binary.

So for my program i can set the pins up like you did, and i can then use 2^10 if i want to set pin 10

PROC_gpio_set(GPIO%,2^10)

I understand it then there is a 32bit memory location for the pins, in assembler, i would read the location, compare the location with how i want to change it with bit masking?

one last question please!!! i cant work out the difference between def FN.... and a normal proc?


thankyou....

User avatar
Richard Russell
Posts: 1668
Joined: Sun Feb 27, 2011 10:35 am
Location: Downham Market, Norfolk
Contact:

Re: pi gpio bbc basic

Post by Richard Russell » Mon Feb 17, 2020 11:15 pm

avtovaz wrote:
Mon Feb 17, 2020 8:38 pm
the bit that really got me was about the 2^pin%, and then it clicked, 2^ converts decimal pin number into binary.
I'm actually not sure why I wrote 2^pin% when 1<<pin% is directly equivalent, easier to understand, and probably faster. :oops:
So for my program i can set the pins up like you did, and i can then use 2^10 if i want to set pin 10
2^10, 1<<10 and %10000000000 are all equivalent, and will work equally well. You could also use &400 or 1024 which have the same numeric value, but don't indicate the pin number as clearly.
in assembler, i would read the location, compare the location with how i want to change it with bit masking?
Yes, that should work.
i cant work out the difference between def FN.... and a normal proc?
FNs return a value, PROCs don't. This is an oversimplification but it's the way they are usually distinguished. I could go on about how FN is the only polymorphic function in BBC BASIC (it can return either a numeric or a string, possibly determined at run time) and how a PROC can return values using the RETURN keyword, but they are more advanced topics!

avtovaz
Posts: 81
Joined: Wed Jan 18, 2017 5:09 pm
Contact:

Re: pi gpio bbc basic

Post by avtovaz » Tue Feb 18, 2020 9:21 pm

right ok! i understand now thankyou! i just need my relay boards and i can start experimenting with programming.

:D

avtovaz
Posts: 81
Joined: Wed Jan 18, 2017 5:09 pm
Contact:

Re: pi gpio bbc basic

Post by avtovaz » Thu Feb 20, 2020 6:35 pm

I was looking at the amount of gpio the pi has , and was wondering if it can be expanded ? I saw a board, the IO Pi , can this be used with bbc basic? Or is there a way of expanding the gpio amount another way?

thanks!

User avatar
Richard Russell
Posts: 1668
Joined: Sun Feb 27, 2011 10:35 am
Location: Downham Market, Norfolk
Contact:

Re: pi gpio bbc basic

Post by Richard Russell » Thu Feb 20, 2020 7:15 pm

avtovaz wrote:
Thu Feb 20, 2020 6:35 pm
I was looking at the amount of gpio the pi has , and was wondering if it can be expanded ?
The GPIO expansion options that I know of rely on I²C communication, and I don't know how difficult it would be to support that from BBC BASIC. There is a WiringPi library that does it; I would need to study that to work out what is involved. I'm not keen on adding hardware to my RPi 4, so are there any built-in I²C peripherals that could be used for testing?

avtovaz
Posts: 81
Joined: Wed Jan 18, 2017 5:09 pm
Contact:

Re: pi gpio bbc basic

Post by avtovaz » Thu Feb 20, 2020 7:32 pm

right ok i can see that being a massive project, thanks for the reply again! i have just had a little snooze and when i woke up i was thinking, most of the signals i need for my window are to work a 16 relay module board. So, maybe i could do some form of logic set up with like 4 pins, giving me an extra 12 to play with. There must be some form of logic chip to do this type of thing already, or i could make something im sure. I know how and or not gates work, but making them physically im not so sure yet.


thanks again!

tjc
Posts: 2
Joined: Thu Feb 20, 2020 9:31 pm
Contact:

Re: pi gpio bbc basic

Post by tjc » Fri Feb 21, 2020 1:22 pm

I have used SYS to use the operating system to read and write to the i2c bus on the pi.

SYS "open",("/dev/i2c-1"),O_RDWR%TO F% :REM get handle for device
SYS "ioctl",F%,I2C_SLAVE,adr:REM set address of i2c bus
SYS "write",F%,c$,LEN(c$):REM write data to bus
SYS "read",F%,^A%,len:REM A% holds read value
SYS "close":REM close device

I should be able to get a return value from SYS "ioctl",F%,I2C_SLAVE,adr but have not found out how to do it.
Any comments appreciated.


REM write to a SAA1064 a 4 digit LED display
adr=&38
a$=CHR$(0)+CHR$(23)+CHR$(79)+CHR$(79)+CHR$(79)+CHR$(79)
REM adr is address of i2c device
REM a$ is a string used for write buffer
REM you can reserve memory and put buffer there
REM if you use a string the write length comes from basic LEN
REM len is number of bytes for read
A%=FNi2c(adr,a$,0)

END

DEF FNi2c(adr,c$,len)
O_RDWR%=2:REM from fcntl.h
I2C_SLAVE =&0703:REM from i2c-dev.h
A%=0
SYS "open",("/dev/i2c-1"),O_RDWR% TO F% :REM get handle for device
REM F% is -1 if unable to open
SYS "ioctl",F%,I2C_SLAVE,adr:REM set address of i2c bus
SYS "write",F%,c$,LEN(c$):REM write data to bus
IF len=0 THEN SYS "close":=A%
SYS "read",F%,^A%,len:REM A% holds read value
REM as for a write you could create a buffer in memory
SYS "close":REM close device
=A%

User avatar
Richard Russell
Posts: 1668
Joined: Sun Feb 27, 2011 10:35 am
Location: Downham Market, Norfolk
Contact:

Re: pi gpio bbc basic

Post by Richard Russell » Fri Feb 21, 2020 1:40 pm

tjc wrote:
Fri Feb 21, 2020 1:22 pm
I have used SYS to use the operating system to read and write to the i2c bus on the pi.
That's very good news. Did you need to run your program 'as root' for it to work?
I should be able to get a return value from SYS "ioctl",F%,I2C_SLAVE,adr but have not found out how to do it.
According to the documentation I can find, ioctl returns an 'int' (signed 32-bit value) so, by rights, it should be straightforward:

Code: Select all

      SYS "ioctl",F%,I2C_SLAVE,adr TO ret%
I assume you've tried this and it doesn't work, which leaves me mystified.

tjc
Posts: 2
Joined: Thu Feb 20, 2020 9:31 pm
Contact:

Re: pi gpio bbc basic

Post by tjc » Fri Feb 21, 2020 8:22 pm

Thank you for your reply.

I am running as a normal user.
I2C needs to be enabled in Raspberry Pi Configuration

SYS "ioctl",F%,I2C_SLAVE,adr TO ret% as far as I can tell ret% always returns 0.
SYS "write",F%,c$,LEN(c$) TO ret% returns the number of bytes written if successful and -1 if the write fails.

User avatar
Richard Russell
Posts: 1668
Joined: Sun Feb 27, 2011 10:35 am
Location: Downham Market, Norfolk
Contact:

Re: pi gpio bbc basic

Post by Richard Russell » Fri Feb 21, 2020 8:51 pm

tjc wrote:
Fri Feb 21, 2020 8:22 pm
SYS "ioctl",F%,I2C_SLAVE,adr TO ret% as far as I can tell ret% always returns 0.
That's good, isn't it? The returned value is defined thus: "Upon successful completion, ioctl() shall return a value other than -1" so returning zero presumably implies that the call succeeded. Since the purpose of the call is to set an address, I assume no other returned value is expected.

avtovaz
Posts: 81
Joined: Wed Jan 18, 2017 5:09 pm
Contact:

Re: pi gpio bbc basic

Post by avtovaz » Sun Feb 23, 2020 9:01 pm

interesting, i dont really understand what is said above, but i have found out about 8 bit Shift Registers SN74HC595N, this will need 3 wires to it, and i think will be easy to control with the pi. As far as i see it now, i link 3 wire s from the pi to a sn chip, then 8 outputs to 8 transistors to get from 3.3v to 5v triggers, and then to the relay board. Now, programming that in bbc basic should be easy i hope, and now instead of having 8 wires to the relay board, i have 3. Apparently too, you can chain 2 chips together to make a 16bit register. So even though that dones not give me input back, i can control 16 relays with 3 pins.


[-o<

avtovaz
Posts: 81
Joined: Wed Jan 18, 2017 5:09 pm
Contact:

Re: pi gpio bbc basic

Post by avtovaz » Fri Mar 13, 2020 9:52 pm

Richard Russell wrote:
Mon Dec 23, 2019 10:11 pm


I know you are asking principally about RISC OS, but for completeness the Raspberry Pi edition of BBC BASIC for SDL 2.0 (which runs under Raspbian, on a RPi 3 or 4) supports controlling the GPIO via the supplied library 'gpiolib.bbc'. Here is a demonstration of it in action, with the program listed below:
well, ive modded your code a little for 3 leds, ive got a ULN2803A 8 Darlington Array connected to the pi, and its now flashing 3 leds.

Now with those 3 lines [tomorrow hopefully] ill use that to program the 8 bit shift register. To say im impressed is an understatement.


thanks for the help

avtovaz
Posts: 81
Joined: Wed Jan 18, 2017 5:09 pm
Contact:

Re: pi gpio bbc basic

Post by avtovaz » Sat Mar 14, 2020 9:12 am

is there any way in bbc basic i can produce pwm around the 50 megahertz so i can control a stepper motor? thanks

User avatar
Richard Russell
Posts: 1668
Joined: Sun Feb 27, 2011 10:35 am
Location: Downham Market, Norfolk
Contact:

Re: pi gpio bbc basic

Post by Richard Russell » Sat Mar 14, 2020 10:03 am

avtovaz wrote:
Sat Mar 14, 2020 9:12 am
is there any way in bbc basic i can produce pwm around the 50 megahertz so i can control a stepper motor? thanks
You don't really mean 50 MHz, surely? In the context of a stepper motor, do you mean 50 Hz?

avtovaz
Posts: 81
Joined: Wed Jan 18, 2017 5:09 pm
Contact:

Re: pi gpio bbc basic

Post by avtovaz » Sat Mar 14, 2020 11:19 am

yes sorry!

User avatar
Richard Russell
Posts: 1668
Joined: Sun Feb 27, 2011 10:35 am
Location: Downham Market, Norfolk
Contact:

Re: pi gpio bbc basic

Post by Richard Russell » Sat Mar 14, 2020 11:39 am

avtovaz wrote:
Sat Mar 14, 2020 11:19 am
yes sorry!
50 Hz is probably within the range you could achieve in 'pure software' with care (certainly if you are prepared to use assembly language), but I'm unsure about the PWM aspect. Stepper motors I've encountered (and that's years ago) didn't use PWM, they were fed with two signals in quadrature, with the direction determined by the relative phase.

avtovaz
Posts: 81
Joined: Wed Jan 18, 2017 5:09 pm
Contact:

Re: pi gpio bbc basic

Post by avtovaz » Sat Mar 14, 2020 11:46 am

yes, thankyou for helping again, ive just realised the stepper motor takes a value some how to make the motor turn, and a servo motor which i thought i had uses pwm.

So if i can just figure out how to control this motor now, and also make use of some form of limit switch, itll be really good.

I was looking at a 12vdc motor but i would need a H bridge, and 2 limit switches. with the stepper, i can program it which way to go easily. So i just need one limit switch for home position, and will count the increments to the other extrem of the blind.

The little stepper motor and driver are £2 ea on ebay which is fantastic too..

thankyou for the help!

One other question please, when i run the BBC basic on the pi, say i press the delete key for 1 second it keep on going for ages, there is a lag, is there a way to sort that ? I think ive not set something when i installed the program. thanks

User avatar
Richard Russell
Posts: 1668
Joined: Sun Feb 27, 2011 10:35 am
Location: Downham Market, Norfolk
Contact:

Re: pi gpio bbc basic

Post by Richard Russell » Sat Mar 14, 2020 12:08 pm

avtovaz wrote:
Sat Mar 14, 2020 11:46 am
when i run the BBC basic on the pi, say i press the delete key for 1 second it keep on going for ages, there is a lag, is there a way to sort that ?
Sorry about that, it's because the auto-key-repeat is filling the keyboard buffer faster than the program editor is able to process those delete commands. The Raspberry Pi (particularly the RPi 3) is a very slow machine compared with most platforms on which BBC BASIC for SDL 2.0 runs; more typically the deletions can keep up with the key repeat rate.

There's not a lot I can do about it I'm afraid. I would suggest that if you are wanting to delete several characters you highlight (select) the block of text you want to delete and then press the delete key just once. There are several ways for quickly selecting text: double clicking selects a whole word, dragging with the mouse selects a block, as does holding down the shift key and moving the cursor using the arrow keys etc.

Incidentally double-clicking not only selects a word, it also highlights that word in the entire listing, making it easy to see where variables etc. are referenced.

avtovaz
Posts: 81
Joined: Wed Jan 18, 2017 5:09 pm
Contact:

Re: pi gpio bbc basic

Post by avtovaz » Sat Mar 14, 2020 12:14 pm

right ok thanks, i just wondered if it was something i had done. Looking forward to working this all out now, shoul dbe some fun. Will post pics or vid when its done.. thankyou for your help.

Post Reply

Return to “general”