Assembler Language - GPIO

Hello. I am attempting to write assembler code using as an example code for GPIO taken from the following publication.
Raspberry Pi Assembly Language Programming: ARM Processor Coding
Stephen Smith
Gibsons, BC, Canada

I believe the following section, which takes a memory map of a section of the GPIO registers required, results in an ‘illegal instruction’ error caused by user privileges when reading from this area of “memory”.

@ Macro to map memory for GPIO Registers
.macro mapMem
openFile devmem, S_RDWR @ open /dev/mem
movs r4, r0 @ fd for memmap
@ check for error and print error msg if necessary
BPL 1f @ pos number file opened ok
MOV R1, #1 @ stdout
LDR R2, =memOpnsz @ Error msg
LDR R2, [R2]
writeFile R1, memOpnErr, R2 @ print the error
B _end

The following is a disassembly of this section,
00000000 <_start>:
0: e59f0238 ldr r0, [pc, #568] ; 240 <_end+0xc>
4: e30011b6 movw r1, #438 ; 0x1b6
8: e30021b6 movw r2, #438 ; 0x1b6
c: e3a07005 mov r7, #5
10: ef000000 svc 0x00000000
14: e1b04000 movs r4, r0
18: 5a000008 bpl 40 <_start+0x40>
1c: e3a01001 mov r1, #1
20: e59f221c ldr r2, [pc, #540] ; 244 <_end+0x10>
24: e5922000 ldr r2, [r2]
28: e1a00001 mov r0, r1
2c: e59f1214 ldr r1, [pc, #532] ; 248 <_end+0x14>
30: e1a02002 mov r2, r2
34: e3a07004 mov r7, #4
38: ef000000 svc 0x00000000
3c: ea00007c b 234 <_end>
40: e59f5204 ldr r5, [pc, #516] ; 24c <_end+0x18>

Thought I would reach out for possible assistance?
In the meantime I will continue to search.
Regards to all.

Hey Bryan.

I think this line below is your problem.

ldr r0, [pc, #568] ; Load to the R0 register whatever is 240 off the progcount

That whatever being loaded must be the ~/dev/gpiomem from Linux PATH.
Maybe the kernel of the PI has noticed that the linux-user you are running the compiled machine code as is not in the GPIO GROUP. That’s just Raspberry Pi OS’ way of ensuring controlling who gets access to /dev/gpiomem.

A little bit of cleaning

In your terminal

#update the OS
sudo rpi-update
#update the kernel 
sudo apt-get update
sudo apt-get upgrade

Add your user to the GPIO group.

#Assuming your user is pi
sudo adduser pi gpio

Give that a try and let me know if it fixes your error.

Pix :heavy_heart_exclamation:

Jonny,
Hello. Thank you for your input. Unfortunately it did not correct the problem. I have run it under ‘su’ and ‘not su’. Will carry on with further debugging. If you have further suggestions please forward.
Bryan

1 Like

Ah thats a shame.
It was worth a shot.
Keen to have another crack at it.
Could you paste the error message from the terminal here so we can inspect further?

Hello, Still working on this one in between other projects.
I will post some info soon.
BC

1 Like