FreeBASIC access to Rpi GPIO pins

For issues with communication ports, protocols, etc.
BasicCoder2
Posts: 3906
Joined: Jan 01, 2009 7:03
Location: Australia

FreeBASIC access to Rpi GPIO pins

Post by BasicCoder2 »

My project (goal) is to access the Rpi pins via a function written in FreeBASIC as illustrated below,
I was unable to compile the example so if anyone is able to help?
Re: FreeBASIC and the Raspberry Pi ?
Postby D.J.Peters » Mar 20, 2018 8:56
For our new ARM Linux user :-)

Tip: using GNU ARM assembler with FreeBASIC

Special ARM GNU as directives

file: "function.s"
Code: Select all
.arm @ 32 bit mode
.global fb_add @ make 'fb_add' visible for FreeBASIC
.type fb_add, %function @ it's a function

.text @ begin of the code segment
.align 2 @ word boundary

fb_add: @ function fb_add(r0, r1) as integer
add r0, r0, r1 @ r0 = r0 + r1
bx lr @ return from function with result in r0

.end @ end of assembler listing

file: "test.bas"
Code: Select all
declare function fb_add cdecl alias "fb_add" (byval r0 as integer,byval r1 as integer) as integer

print " 3 + 7 = " & fb_add(3,7)

sleep
#> fbc test.bas -Wa function.s

Joshy
These were the steps I took to try and compile the function.

pi@raspberrypi:~ $ cd fbPrograms
pi@raspberrypi:~/fbPrograms $ cat > function.s
.arm @ 32 bit mode
.global fb_add @ make 'fb_add' visible for FreeBASIC
.type fb_add, %function @ it's a function

.text @ begin of the code segment
.align 2 @ word boundary

fb_add: @ function fb_add(r0, r1) as integer
add r0, r0, r1 @ r0 = r0 + r1
bx lr @ return from function with result in r0

.end @ end of assembler listing
pi@raspberrypi:~/fbPrograms $
pi@raspberrypi:~/fbPrograms $ cat function.s
.arm @ 32 bit mode
.global fb_add @ make 'fb_add' visible for FreeBASIC
.type fb_add, %function @ it's a function

.text @ begin of the code segment
.align 2 @ word boundary

fb_add: @ function fb_add(r0, r1) as integer
add r0, r0, r1 @ r0 = r0 + r1
bx lr @ return from function with result in r0

.end @ end of assembler listing
pi@raspberrypi:~/fbPrograms $ as -o function.o function.s
pi@raspberrypi:~/fbPrograms $ gcc -o function function.o
/usr/lib/gcc/arm-linux-gnueabihf/6/../../../arm-linux-gnueabihf/crt1.o: In function `_start':
(.text+0x34): undefined reference to `main'
collect2: error: ld returned 1 exit status
pi@raspberrypi:~/fbPrograms $
srvaldez
Posts: 3379
Joined: Sep 25, 2005 21:54

Re: FreeBASIC access to Rpi GPIO pins

Post by srvaldez »

@BasicCoder2
why use cat when you can geany?
just copy the code from D.J.Peters post viewtopic.php?p=244583#p244583 and paste into a new document in geany and save to the desired folder
I don't understand why you don't try the plain given instructions by D.J.Peters.
assuming you have function.s and test.bas in the same location then from the terminal issue the command
fbc test.bas -Wa function.s
that's it, your test program is ready.
BasicCoder2
Posts: 3906
Joined: Jan 01, 2009 7:03
Location: Australia

Re: FreeBASIC access to Rpi GPIO pins

Post by BasicCoder2 »

srvaldez wrote:why use cat when you can geany?
http://www.science.smith.edu/dftwiki/in ... spberry_Pi
Being new to Linux I need to understand as much as possible as to how someone is doing something both methods produce the function.s file ?
I don't understand why you don't try the plain given instructions by D.J.Peters.
I did try to follow the instructions given but without success.
When I tried to compile test.bas in Geany it failed to compile.

How plain (or complete) instructions are depends on the experience and knowledge of the reader.
(ref my other RPi problems with getting FB to work)

It is also useful to know what something means such as -Wa

Ok. The penny just dropped!!
Essentially I don't understand the code. The problem was following instructions without understanding.
The two files test.bas and function.s are apparently combined to produce an .exe file by the terminal instructions fbc test.bas -Wa function.s not by compiling the test.bas alone in Geany.

So now I do have an .exe file that does work. It was probably there all the time, I was just focused on the compiler error in the Geany editor and assumed nothing was working and never checked the folder for the .exe
St_W
Posts: 1626
Joined: Feb 11, 2009 14:24
Location: Austria
Contact:

Re: FreeBASIC access to Rpi GPIO pins

Post by St_W »

BasicCoder2 wrote:It is also useful to know what something means such as -Wa
That's what our wiki aims to describe. Additional explanation or instructions shouldn't be needed usually.
e.g. the mentioned "-Wa" argument is described here: https://freebasic.net/wiki/wikka.php?wa ... pilerOptWa
BasicCoder2
Posts: 3906
Joined: Jan 01, 2009 7:03
Location: Australia

Re: FreeBASIC access to Rpi GPIO pins

Post by BasicCoder2 »

@St_W,
Sorry but the description is too terse. I wouldn't understand the context in which to use it without the example given by Joshy.
Also the example,
fbc -Wa -o,output.o,--verbose
doesn't look like,
fbc test.bas -Wa function.s
I don't really learn well from manuals. At the moment I have been working my way through some ARM assembler tutorials which have a lot of additional explanations and examples to type up and compile for practice. Fortunately I have written a lot of simple assembler code a long time ago so I understand what is going on at the hardware level otherwise I suspect learning ARM assembler would be much harder.
MrSwiss complained that I don't seem to want to learn anything new but is more a case of I don't want to spend what little time I have learning stuff I would never use (and would thus promptly forget anyway) and until now I have never had to use ARM assembler with FreeBASIC.
St_W
Posts: 1626
Joined: Feb 11, 2009 14:24
Location: Austria
Contact:

Re: FreeBASIC access to Rpi GPIO pins

Post by St_W »

BasicCoder2 wrote:Sorry but the description is too terse. [...] Also the example,
fbc -Wa -o,output.o,--verbose
doesn't look like,
fbc test.bas -Wa function.s
I don't really learn well from manuals.
The description in the wiki doesn't explain the arguments for "-Wa" itself, because that depends on the used assembler. FreeBasic uses the GNU assembler ("as") by default. Its command line options are described in chapter 1.4 and chapter 2 here: https://sourceware.org/binutils/docs/as/ (sorry, again a manual)
The forum is probably a good place to learn by example, if that is easier for you. But be aware that there isn't an example for everything and sometimes you'll probably still have to look up information from a manual (how to do that efficiently, is another topic to learn).
BasicCoder2
Posts: 3906
Joined: Jan 01, 2009 7:03
Location: Australia

Re: FreeBASIC access to Rpi GPIO pins

Post by BasicCoder2 »

St_W wrote:The description in the wiki doesn't explain the arguments for "-Wa" itself, because that depends on the used assembler.
It is not my intention to go back to assembler programming as an option. It was more a need as a means to access the GPIO pins from FreeBASIC.
My intention was to write a FreeBASIC function to read and write to these pins and thus only need to know enough ARM asm to achieve this outcome.
It doesn't seem like a difficult task, here is a snippet of code

Code: Select all

 ldr r0, =0x20200000 ;# Get GPIO base address
ldr r1, [r0, #0x34] ;# Read state of GPIO 0-31
tst r1, #0x00020000 ;# Test GPIO 17 only.
dasyar
Posts: 372
Joined: Dec 04, 2008 15:31

Re: FreeBASIC access to Rpi GPIO pins

Post by dasyar »

What I'd suggest you do is get hold of my wiringPi librarys and look at the sources. It will have all you need - as well as an easy way to access the GPIO pins from a C program.

Start here:

https://projects.drogon.net/raspberry-pi/wiringpi/

Or fast forward to the code here:

https://projects.drogon.net/raspberry-p ... d-install/

and welcome to the dark side of low-level shenanigans (and just remember you need to do all this as root!)

-Gordon
@BasicCoder2 - Not sure if you have come accross this yet. He has a working C program for accesing the GPIO pins. I wonder if it would be easier to just convert his C program to a .bi file and go from there. Just presnting another option.
BasicCoder2
Posts: 3906
Joined: Jan 01, 2009 7:03
Location: Australia

Re: FreeBASIC access to Rpi GPIO pins

Post by BasicCoder2 »

dasyar wrote: Not sure if you have come across this yet. He has a working C program for accessing the GPIO pins. I wonder if it would be easier to just convert his C program to a .bi file and go from there. Just presenting another option.
I forgot that Linux is a multitasking system and as such would have the same programming issues with i/o as does Windows.
Ideally I would access the pins directly as i did with the old MSDOS machines or with Arduino so back to square one on that issue.
Converting C programs to a .bi file is beyond my technical know how.
https://learn.sparkfun.com/tutorials/raspberry-gpio
With FreeBASIC I just seem to hit one brick wall after another. I think the Universe is trying to tell me something!
I see there is FUZE BASIC that has been tailored for use with the Raspberry Pi and interfacing.
https://www.fuze.co.uk/
srvaldez
Posts: 3379
Joined: Sep 25, 2005 21:54

Re: FreeBASIC access to Rpi GPIO pins

Post by srvaldez »

BasicCoder2 wrote: I see there is FUZE BASIC that has been tailored for use with the Raspberry Pi and interfacing.
https://www.fuze.co.uk/
there's also a free version called RTB https://projects.drogon.net/rtb/
RTB supports the on-board GPIO hardware in the Raspberry Pi computer, as well as external devices using the wiringPi extensions.
you can install rtb via apt-get, sudo apt-get install rtb
srvaldez
Posts: 3379
Joined: Sep 25, 2005 21:54

Re: FreeBASIC access to Rpi GPIO pins

Post by srvaldez »

D.J.Peters
Posts: 8586
Joined: May 28, 2005 3:28
Contact:

Re: FreeBASIC access to Rpi GPIO pins

Post by D.J.Peters »

If you can access files via FreeBASIC (i'm sure you can)
than you can configure, read and write GPIO's also.
On UNIX / Linux all hardware are accessible via file system.

NOTE: not tested I'm not in near of any Raspberry PI ATM.
I can't remember only ROOT or a NORMAL user can access the pin's.

After booting no GPIO's are usable make pin 4 usable

Code: Select all

var hFile = FreeFile()
open "/sys/class/gpio/export" for output as #hFile
print #hFile,"4"
close #hFile
set pin 4 as output:

Code: Select all

var hFile = FreeFile()
open "/sys/class/gpio/gpio4/direction" for output as #hFile
print #hFile,"out"
close #hFile
write 0 or 1 to pin 4

Code: Select all

var hFile = FreeFile()
open "/sys/class/gpio/gpio4/value" for output as #hFile
print #hFile,"1"
sleep 1000
print #hFile,"0"
sleep 1000
close #hFile
Configure pin's as input and read the value is easy also ;-)

Joshy
Last edited by D.J.Peters on Apr 01, 2018 17:08, edited 1 time in total.
srvaldez
Posts: 3379
Joined: Sep 25, 2005 21:54

Re: FreeBASIC access to Rpi GPIO pins

Post by srvaldez »

thank you D.J.Peters for the tip :-)
BasicCoder2
Posts: 3906
Joined: Jan 01, 2009 7:03
Location: Australia

Re: FreeBASIC access to Rpi GPIO pins

Post by BasicCoder2 »

@Joshy,
Thanks for the suggestion I will give it a try.
My next piece of hardware will be the Raspberry Pi camera,
https://www.jaycar.com.au/5mp-camera-fo ... i/p/XC9020

@srvaldez,
Thanks for the utube suggestion. The speaker is clear and easy to listen to unlike some (most) utube tutorials.
D.J.Peters
Posts: 8586
Joined: May 28, 2005 3:28
Contact:

Re: FreeBASIC access to Rpi GPIO pins

Post by D.J.Peters »

On ebay you get the 5MP camera for Raspberry PI 3 for €4,30 ~ 5$
5MP camera for PI 3

Joshy
Post Reply