TonySterrett
Posts: 16
Joined: Thu Apr 04, 2019 6:58 pm
Contact: Facebook

Problems with Pi 4 Wiring Pi4 GPIO

Mon Sep 09, 2019 3:00 pm

I am writing code to access /dev/mem on the raspberry pi 4. I'm using nodejs with the package rpio. Rpio uses a "C" language interface. I have modified the *.h file to reflect changes in the hardware address for pi3 to pi4. like so:

#define BCM2711_RPI4_PERI_BASE 0xFE000000

I am aware that root permissions are required. I am running nodejs but the part of the code that accesses hardware is "C".
The problem is that when called from the Javascript via nodejs, it crashes with:
Error: Could not initialize bcm2711 GPIO library
--please see code and output below--

I have printf statement to check the values of all parameters, as you can see.
Surprisingly, I am able to compile and run the C code which is found in the src directory in the rpio code. It executes without problem. So when called from the nodejs program it crashes but run perfect from the C code. Or so it seems.
So being called from a nodejs program makes it fail?

Note: This is puzzling because the C code has no way of knowing how it was called. I have written many tests in the effort and the only difference is that when calling C directly the fd (file descriptor) is equal to 3. When called from the nodejs program the fd is 20 consistently.
As the last note, if I run as user and open /dev/gpio (and change the offset). I have no problems but running root and opening /dev/mem problem.

Thanks in advance for any hints. Please let me know if anyone has any questions.
I have written many test programs mainly to set debug statement, check parameters and to attempt different fixes.
Errors and debug for unsuccessful run:

[email protected]:~/rpio4-last
$ sudo node pin12.js

Board rev is 12561
***nan_method:rpio_init calling bcm2711_init
***bcm2711_init***
Base address is: FE000000
geteuid = 0
Root access to dev mem open
File describitor = 20
bcm2711_peripherals_size = 1800000,bcm2711_peripherals_base = FE000000
mapmem: map size = 1800000
mapmem: offset = 0
mapmem: fd = 20
mapmem:***********after mmap call****************
mapmem: map size = 1800000
mapmem: offset = 0
mapmem: fd = 20
mapmem: mem mmap failed: Invalid argument
bcm2711_init: Unable to open /dev/mem: Invalid argument
/home/pi/rpio4-last/node_modules/rpio4/lib/rpio4.js:106
return bindfunc(optarg);
^

Code:

static void *mapmem(const char *msg, size_t size, int fd, off_t off)
{

static int memfd;

memfd = fd;
if (fd < 0) {
printf("mapmem: file descriptor is invalid\n");
return MAP_FAILED ;
}

printf ("mapmem: map size = %X\n",size);
printf ("mapmem: offset = %X\n",off);
printf ("mapmem: fd = %d\n",memfd);
void *map = mmap(NULL, size, (PROT_READ | PROT_WRITE), MAP_SHARED, memfd, off);
if (map == MAP_FAILED) {
printf ("mapmem:***********after mmap call****************\n");
printf ("mapmem: map size = %X\n",size);
printf ("mapmem: offset = %X\n",off);
printf ("mapmem: fd = %d\n",memfd);
printf("mapmem: %s mmap failed: %s\n", msg, strerror(errno));
}
return map;
}

Unsuccessful run:

Error: Could not initialize bcm2711 GPIO library
at bindcall (/home/pi/rpio4-last/node_modules/rpio4/lib/rpio4.js:106:9)
at EventEmitter.rpio4.init (/home/pi/rpio4-last/node_modules/rpio4/lib/rpio4.js:488:2)
at rpio4.open (/home/pi/rpio4-last/node_modules/rpio4/lib/rpio4.js:498:19)
at Object.<anonymous> (/home/pi/rpio4-last/pin12.js:6:6)
at Module._compile (internal/modules/cjs/loader.js:689:30)
at Object.Module._extensions..js (internal/modules/cjs/loader.js:700:10)
at Module.load (internal/modules/cjs/loader.js:599:32)
at tryModuleLoad (internal/modules/cjs/loader.js:538:12)
at Function.Module._load (internal/modules/cjs/loader.js:530:3)
at Function.Module.runMain (internal/modules/cjs/loader.js:742:12)

The bindcall occurred because of the fall thru of the C code error.
When compiling just the C code and running successfully. the output is:
[email protected]:~/rpio4/src $ sudo ./test
***bcm2711_init***
Base address is: FE000000
geteuid = 0
Root access to dev mem open
File describitor = 3
bcm2711_peripherals_size = 1800000,bcm2711_peripherals_base = FE000000
and the correct led blinks.

User avatar
joan
Posts: 14175
Joined: Thu Jul 05, 2012 5:09 pm
Location: UK

Re: Problems with Pi 4 Wiring Pi4 GPIO

Mon Sep 09, 2019 3:21 pm

You don't say if the code runs on the Pi3.

Could you print the errno value as well as the string?

TonySterrett
Posts: 16
Joined: Thu Apr 04, 2019 6:58 pm
Contact: Facebook

Re: Problems with Pi 4 Wiring Pi4 GPIO

Mon Sep 09, 2019 3:42 pm

joan wrote:
Mon Sep 09, 2019 3:21 pm
You don't say if the code runs on the Pi3.


Could you print the errno value as well as the string?
No it does not. BCM2835_PERI_BASE has beed changed to 0xFE000000. Which is incorrect for the Pi3.
The Errno value is 22.

User avatar
joan
Posts: 14175
Joined: Thu Jul 05, 2012 5:09 pm
Location: UK

Re: Problems with Pi 4 Wiring Pi4 GPIO

Mon Sep 09, 2019 4:31 pm

The size of hex 1800000 seems suspicious. I tend to just map individual peripherals and the few bytes they need.

I meant does the code work on the Pi3 with the correct offset.

TonySterrett
Posts: 16
Joined: Thu Apr 04, 2019 6:58 pm
Contact: Facebook

Re: Problems with Pi 4 Wiring Pi4 GPIO

Mon Sep 09, 2019 5:21 pm

joan wrote:
Mon Sep 09, 2019 4:31 pm
The size of hex 1800000 seems suspicious. I tend to just map individual peripherals and the few bytes they need.

I meant does the code work on the Pi3 with the correct offset.
Yes it does work with the correct offset. Please note, This also work when opening /dev/gpiomem. It is the mmap() function that fail and I check all the augments.
The thing that baffles me is that it *will* run if I just execute the C which is the lowlevel interface and does the mmap() to /dev/mem.

TonySterrett
Posts: 16
Joined: Thu Apr 04, 2019 6:58 pm
Contact: Facebook

Re: Problems with Pi 4 Wiring Pi4 GPIO

Mon Sep 09, 2019 6:31 pm

joan wrote:
Mon Sep 09, 2019 4:31 pm
The size of hex 1800000 seems suspicious. I tend to just map individual peripherals and the few bytes they need.

I meant does the code work on the Pi3 with the correct offset.
Understood, regard the your approach to map what you need. I am in the process of upgrading some code to the Pi 4 for the Pi 3. This
code used the 1800000 address space and I must also support various other interfaces I2C. etc.

User avatar
joan
Posts: 14175
Joined: Thu Jul 05, 2012 5:09 pm
Location: UK

Re: Problems with Pi 4 Wiring Pi4 GPIO

Mon Sep 09, 2019 7:47 pm

For what it is worth have a look at

http://abyz.me.uk/rpi/pigpio/examples.h ... nimal_gpio

TonySterrett
Posts: 16
Joined: Thu Apr 04, 2019 6:58 pm
Contact: Facebook

Re: Problems with Pi 4 Wiring Pi4 GPIO

Mon Sep 09, 2019 8:15 pm

joan wrote:
Mon Sep 09, 2019 7:47 pm
For what it is worth have a look at

http://abyz.me.uk/rpi/pigpio/examples.h ... nimal_gpio
Thanks. The particular difficulty I am having requires access to the /dev/mem device. /dev/gpio won't do. the /dev/mem device requires root access btw. My main difficulty seems to be calling mmap() correctly. The error code says bad augment but I check all the parameter I pass. Help.

User avatar
joan
Posts: 14175
Joined: Thu Jul 05, 2012 5:09 pm
Location: UK

Re: Problems with Pi 4 Wiring Pi4 GPIO

Mon Sep 09, 2019 8:21 pm

The code I linked uses /dev/mem and works on all Pis.

TonySterrett
Posts: 16
Joined: Thu Apr 04, 2019 6:58 pm
Contact: Facebook

Re: Problems with Pi 4 Wiring Pi4 GPIO

Mon Sep 09, 2019 9:03 pm

joan wrote:
Mon Sep 09, 2019 8:21 pm
The code I linked uses /dev/mem and works on all Pis.

My C code does not fail either. The problem is when The C code is called from a Javascript (nodejs) program mmap() fails. Which is very odd because C doesn't know how its called. The only difference in the calls that I can see are that the file descriptors are different. This makes sense because the js program which called the C code (thru nodejs conventions) maybe opening some files. So the problem is why the C function containing mmap() fails when called from a JavaScript app.

BTW, the description of the linked in code says "The following examples show various ways pigpio may be used to communicate with sensors via the GPIO." I need the /dev/mem.

User avatar
joan
Posts: 14175
Joined: Thu Jul 05, 2012 5:09 pm
Location: UK

Re: Problems with Pi 4 Wiring Pi4 GPIO

Mon Sep 09, 2019 9:11 pm

Have you clicked on the link? The linked code DOES use /dev/mem.

TonySterrett
Posts: 16
Joined: Thu Apr 04, 2019 6:58 pm
Contact: Facebook

Re: Problems with Pi 4 Wiring Pi4 GPIO

Tue Sep 10, 2019 12:29 pm

joan wrote:
Mon Sep 09, 2019 9:11 pm
Have you clicked on the link? The linked code DOES use /dev/mem.
Oh the description is wrong then. These programs to not provide hardware access layer for Nodejs, as is my problem. None of them even mention providing such a binding to nodejs

TonySterrett
Posts: 16
Joined: Thu Apr 04, 2019 6:58 pm
Contact: Facebook

Re: Problems with Pi 4 Wiring Pi4 GPIO

Thu Sep 12, 2019 4:11 pm

joan wrote:
Mon Sep 09, 2019 9:11 pm
Have you clicked on the link? The linked code DOES use /dev/mem.
yes

TonySterrett
Posts: 16
Joined: Thu Apr 04, 2019 6:58 pm
Contact: Facebook

Re: Problems with Pi 4 Wiring Pi4 GPIO

Thu Sep 12, 2019 4:31 pm

So to sum it up, mmap() is failing when called from node js rpio. But not when the C code is complied and run on its own. The error says Invalid argument but all parameters are check (and displayed in latest debug). I am playing with the parameters however. I suspect a the file descriptor was/is bad but I have no data on that (i saw a fd = -1 in one of
my many runs).



Latest debug:

geteuid = 0
Root access to dev mem open
File describitor = 20
bcm2711_peripherals_size = 1800000,bcm2711_peripherals_base = FE000000
mapmem: map size = 1800000
mapmem: offset = 0
mapmem: fd = 20
mapmem:***********after mmap call****************
mapmem: map size = 1800000
mapmem: offset = 0
mapmem: fd = 20
mapmem: errno = 22
mapmem: mem mmap failed: Invalid argument
bcm2711_init: Unable to open /dev/mem: Invalid argument
/home/pi/rpio4-last/node_modules/rpio4/lib/rpio4.js:106
return bindfunc(optarg);
^

Return to “Troubleshooting”