1

What I have

Raspberry Pi 3 B

RF24L01

RF24 Library TMRh20

How I've Connected It

Based on the library indications:

RF24L01 pin | RPi 3B
1 GND       | 9 GND
2 VCC       | 1 (3.3V)
3 CE        | 15 (GPIO22)
4 CSN       | 24 (GPIO8) (CE0)
5 SCK       | 23 (GPIO11) (SPI_CLK)
6 MOSI      | 19 (GPIO10) (SPI_MOSI)
7 MISO      | 21 (GPIO9) (SPI_MISO)

Pic of Pins

The Code (Executable, built using TMRh20 library)

#include <RF24/RF24.h>

using namespace std;

// Pin 22 used for CE, as said before in pin connections
RF24 radio(22,0);

int main(int argc, char** argv)
{

  printf("Starting...\n");

  radio.begin();

  printf("Done\n");

  return 0;

}

Output

Output

Problem / Question

As you can see, a segmentation fault error (in Spanish) happened. I've also activated the SPI interface using:

sudo raspi-config

So, why do I get this execution error?

Isn't the initialization valid?

Update

I've also tried moving ping CE from RF24, that was connected to GPIO22 (pin 15) to pin 26 (CE1), because I saw this in the dependence BCM2835.

Then, I enabled SPI again, built and execute again, getting the same result.

5
  • You might as well start learning to use a debugger. Run gdb ./test (if gdb is not installed, sudo apt install gdb). When you get to a prompt, type run. The seg fault will be caught, and you will get another prompt. Enter bt (backtrace). If the output is not too long, cut and paste it here. Please don't use screenshots for text.
    – goldilocks
    Commented Mar 14, 2018 at 12:30
  • Already tried gdb @goldilocks, but it gives me "illegal instruction" after gdb ./test Commented Mar 14, 2018 at 12:33
  • "Illegal instruction" implies code that was compiled for the wrong architecture, although in that case it probably wouldn't link. If you compiled it on the pi, you may be out of luck at this point.
    – goldilocks
    Commented Mar 14, 2018 at 12:51
  • As you are compiling on the Pi I would replace the arm-linux- ... -abi=hard with the letters gcc
    – joan
    Commented Mar 14, 2018 at 14:01
  • ++ for the very well formatted question!
    – stevieb
    Commented Mar 14, 2018 at 15:02

2 Answers 2

2

I just ran into the same issue, and discovered that it will segfault if you do not run the program as root (sudo). It would be great if it could be more helpful, but alas. Try putting sudo in front of the ./test

Determined the problem by finding this comment in the source code:

If the library runs with any other effective UID (ie not root), then bcm2835_init() will attempt to open /dev/gpiomem, and, if successful, will only permit GPIO operations. In particular, bcm2835_spi_begin() and bcm2835_i2c_begin() will return false and all other non-gpio operations may fail silently or crash.

Source: https://github.com/nRF24/RF24/blob/95ef8bdadf9068796444151c162749eb5bad4c26/utility/RPi/bcm2835.h#L63-L67

3
  • You leave me totally stunned. I found yesterday a solution, based on some differences on compilation and installation. Will post on another answer. Commented Apr 4, 2018 at 7:18
  • 1
    @BtcSources Cool, I'm interested in seeing what it was. If my solution is also valid I'd appreciate accepting as answer ;-) I'm also considering contributing a PR to throw instead of segfaulting to help people that don't have time to dig through the source code and run gdb on it like I did.
    – Paul
    Commented Apr 5, 2018 at 13:50
  • Done Paul =) I'll upvote yours as it's a valid answer, but will mark as accepted answer mine, since these were the steps that worked for me to solve it. Thanks! Commented Apr 10, 2018 at 16:30
1

First, I noticed that building the library as told on the documentation with:

sudo make install -B

Run inside another configure, which deletes yours. That said, what I did was, from my $~ directory:

cd ./RF24/
sudo ./configure --driver=SPIDEV

Went to the file Makefile.inc and replace (as told by Joan in a comment):

CC=...-gcc --> CC=gcc
CXX=...-g++ --> CXX=g++

Then, install the library (notice no -B option, so my Makefile.inc file is not replaced by the new created by a new run of configure):

sudo make
sudo make install

Finally, go to the examples folder (where my program is) and compile it. Modifying first my makefile of course:

cd ./examples_linux/

Edit Makefile

PROGRAMS = receiver

Compile and run:

sudo make
./receiver

My program now executes fine without any error and without needing sudoers.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.