nRF24L01 Java notes

Active today
Viewed 44 times

I’m currently trying to make a NRF24L01 module work by using Java. I found this library that makes use of the JNI, but I can’t get it to work.

When I try to run the provided example code the Raspberry Pi just hangs and the CPU rises to 100% after (or rather while) running rf24.begin().

I digged a little bit into the code and found out that the reason it hangs is due to an endless while loop inside the bcm2835_spi_transfernb() function of this file.

The function is the following:

void bcm2835_spi_transfernb(char* tbuf, char* rbuf, uint32_t len)
    volatile uint32_t* paddr = bcm2835_spi0 + BCM2835_SPI0_CS/4;
    volatile uint32_t* fifo = bcm2835_spi0 + BCM2835_SPI0_FIFO/4;
    uint32_t TXCnt=0;
    uint32_t RXCnt=0;

    // This is Polled transfer as per section 10.6.1
    // BUG ALERT: what happens if we get interupted in this section, and someone else
    // accesses a different peripheral?

    // Clear TX and RX fifos
    bcm2835_peri_set_bits(paddr, BCM2835_SPI0_CS_CLEAR, BCM2835_SPI0_CS_CLEAR);

    // Set TA = 1
    bcm2835_peri_set_bits(paddr, BCM2835_SPI0_CS_TA, BCM2835_SPI0_CS_TA);

    // Use the FIFO's to reduce the interbyte times
    while((TXCnt < len)||(RXCnt < len))
        // TX fifo not full, so add some more bytes
        while( (bcm2835_peri_read(paddr) & BCM2835_SPI0_CS_TXD )  && (TXCnt < len )  && !(bcm2835_peri_read(paddr) & BCM2835_SPI0_CS_RXR) )
           bcm2835_peri_write_nb(fifo, tbuf[TXCnt]);

        //Rx fifo not empty, so get the next received bytes     
        while(((bcm2835_peri_read(paddr) & BCM2835_SPI0_CS_RXD))&&( RXCnt < len )){        
           rbuf[RXCnt] = bcm2835_peri_read_nb(fifo);
    while(! (bcm2835_peri_read(paddr) & BCM2835_SPI0_CS_DONE) ){}
    if(TXCnt == len){ bcm2835_peri_set_bits(paddr, 0, BCM2835_SPI0_CS_TA);}

The problem is that the two while loops inside the outer while loop are never entered and therefore the exit-condition cannot occur turning this into an endless while loop.

Unfortunately I have no idea why that happens. Is it possible that this is due to a SPI related problem, like me forgetting to setup something? I did enable the SPI interface and validated it multiple times and also tested it. It seems to be working correctly. I also checked my wiring multiple times and I’m confident that its correct.

I would appreciate a little input, since I am really lost regarding this right now.

  • Well, there are a couple of reasons that the SPI transfer goes into an infinite loop. Usually first thing first is to use a SPI loopback test to make sure the SPI software and hardware are properly setup and the wiring is good. In case you are not familiar with SPI loopback in general and nRFL2401+ specifically, you might like to skim the following chat record: chat.stackexchange.com/rooms/107068/…. – tlfong01 yesterday
  • Your references are out of date. You need to update the tools: (1) anvo/rf24-bcm-java Java wrapper for the nRF24L01 library – 2014 github.com/anvo/rf24-bcm-java (2) TMRh20 RF24/RPi/RF24/bcm2835.c – Mike McCauley 2013 github.com/stanleyseow/RF24/blob/… (3) Arduino and Raspberry Pi driver/libraries for nRF24L01 github.com/stanleyseow/RF24 – 2014 github.com/stanleyseow/RF24 (4) Optimized high speed nRF24L01+ driver class documentaion V1.3.4 – TMRh20 2020 tmrh20.github.io/RF24. – tlfong01 yesterday
  • 1
    I actually performed a SPI loopback test and it was successful. Could you please elaborate though what you mean with “updating” my tools? I saw that the submodule the Java wrapper uses is very old, since someone else is now maintaining the library. Do you mean I have to use that newer code? If so, how am I supposed to do it? I think “simply” changing the submodule will mess with the rest of the code. – Gereon99 yesterday
  • Just a couple of quick comments: (1) SPI loopback: I usually first do SPI loopback. As you might know, SPI loopback only makes sure MOSI and MISO is working more or less properly, but CE0 or CE1 is not tested. To make sure all three signals are tested, I usually do the ping test. For nRF24L01+, I do the following: (2) Read the nRF24L01+’s config register at address 0x00, to see if the POR value is 0x04. (3) I sometimes also do the read-after-write test: write random values 0x00, 0xff, 0x55, 0xas, 0x5a, 0xa5 etc to make sure I can read after write OK. – tlfong01 yesterday
  • (4) Lower SPI speed. I read that your code initializes SPI speed to 5MHz. One answer of the following questions says problem solved after lowering speed to 4MHz: (5) “Rpi SPI nRF24L01+ 2.4GHz Transceiver Module Send Message to Arduino Problem” raspberrypi.stackexchange.com/questions/107608/…. I do read from time to time tutorials recommending to lower speed from 5MHz. When debugging, I usually lower SPI speed to 1Mhz, and sometimes even 10kHz. – tlfong01 yesterday
  • (6) About outdated SPI and python modules: My recent experience on nRF24 and NFC modules are all python I2C/SPI/UART. So my python cases might not well applied to Java. You might like to skim some of my recent troubleshooting experience on messing around outdated SPIU and python modules: (a) CHAT Record of Rpi SPI nRF24L01+ 2.4GHz Transceiver Module Send Message to Arduino Problem chat.stackexchange.com/transcript/103645/2020/1/24 / to continue, … – tlfong01 yesterday
  • (b) CHAT Record of lib_nrf24 throws OSError (Errno 9) Bad file descriptor on radio.available(0) chat.stackexchange.com/rooms/107068/… (c) CHAT record on on How can Rpi python read a MFRC522/PN532 NFC/RFID MIFARE smart card/tag? chat.stackexchange.com/transcript/106073/2020/3/29 (d) How can Rpi python read a MFRC522/PN532 NFC/RFID MIFARE smart card/tag? raspberrypi.stackexchange.com/questions/109773/… – tlfong01 yesterday
  • (e) CHAT record on on How can Rpi python read a MFRC522/PN532 NFC/RFID MIFARE smart card/tag? chat.stackexchange.com/transcript/106073/2020/3/29. This is the end of my brainstorming comments on troubleshooting out dated software. I will read your Java code and see I can suggest something, perhaps tomorrow or day after. Cheers. – tlfong01 yesterday
  • Question: Do you have any experience in Pi4j? You can use Pi4j SPI to read and write nRF24L01’s config register to make sure Java SPI API is OK. This is what I am doing with python for libnrf24.py. Reference: pi4j.com/1.2/index.html – tlfong01 yesterday
  • One more thing. I think your problem is at the Java level NOT at the SPI transfer level. This is what I commented on another similar questioner using python SPI for nRF24L01. As you suggested, the problem is around/before the while loops, not related to SPI transfer. But I am not knowledgeable enough to go further. Good luck and cheers. – tlfong01 yesterday
  • 1
    Thank you for your effort. I might try reading e.g. the config register with Pi4J as suggested by you. Thats a good idea. – Gereon99 yesterday
  • You are welcome. As I indicated earlier, my Java knowledge is rather rusty. I only remeber the funny name JavaBeans but forgot what it is. I recommend Pi4J because only know this and not JNI. I also remember that I did NOT try Oracle because it is not open source. Perhaps I should spend some time this weekend to polish my rusty Java stuff. Just now I casually googled and found Java is more confusing and messy than Python 2/3. References: (1) “Installing Java on the Rpi (Rpi4 buster OK) – Emmet, PiMyLifeUp 2019dec12”: pimylifeup.com/raspberry-pi-java, / to continue, … : – tlfong01 23 hours ago
  • (2) “Efficient Java Development for the Raspberry Pi – GregF10 2018”: instructables.com/id/…, (3) “How to Install Java on Raspberry Pi – 2020feb24”: linuxize.com/post/install-java-on-raspberry-pi. – tlfong01 23 hours ago
  • I forgot to mention that it is important to most up to date software packages, because say, if your jdk/jni/ etc is out of date, some modules might be missing, cause infinite waiting. So you see now I am starting to install the most up to date Java stuff. – tlfong01 15 hours ago
  • I know I can run develop Java stuff using Eclipse or NetBean. I am thinking of installing NetBean in my Win10 PC (CORE i5, 6GB) as the developer workstation, and Rpi4B 4GB buster as the remote platform. Any comments or counter suggestions please? – tlfong01 15 hours ago
  • On second thought, perhaps I should start small and use BlueJ or GreenFoot IDE to refresh my memory on Java. Both these IDEs are small, and already preinstalled in Rpi4B buster, so I think they should be as newbie friendly and python Thonny or Geany. – tlfong01 13 hours ago
  • I skimmed the Wiki’s on BlueJ and GreenFoot and found BlueJ is better for my current toy program, because GreenFoot seems mainly for 2D graphical things. So I will go BlueJ. – tlfong01 13 hours ago
  • So I googled a couple of reviews by Java beginners and programmers. My conclusion is that BlueJ is definitely good for newbies to learn Java as a new language. I also read the 2003 paper by M Kölling, who “invented” BlueJ, on how to teach Java with BlueJ. Of course I am not going to teach Java, but this paper is indeed helpful for me the newbie to know how to learn Java, using BlueJ. This paper is 17 years old. I hope BlueJ should be much improved now, and so should be worth my time testing the waters. – tlfong01 12 hours ago
  • One thing I am surprised to hear from Kölling, is that he does not suggest to start with “Hello World”, but instead encourages students to (1) use of case studies in programming learning, (2) to read, study, modify and extend existing programs. So I am thinking of using the existing nRF24L01+ Python/Java library as a case study, and learn how to expand the libraries with more features. – tlfong01 12 hours ago
  • I dived deeper and found the following article is best to start with Rpi Java SPI programming: (1) “Rpi Java & Pi4J SPI Programming with Pi-Face – Robert Savage 2013feb25”: savagehomeautomation.com/projects/… The author Savage says the following: “The good news is that if you use the Pi4J libraries all the nitty-gritty hard work (SPI communication protocol for the chipset and state change monitoring threads) is already done for you.“. Ah bed time!. I call it a day and see you tomorrow. – tlfong01 11 hours ago


(1) After googling a bit, I have decided to start with Rpi BlueJ Java IDE, and the first Java programs will be on:

(a) GPIO LED/Button programming,

(b) SPI programming.

(2) Next step is to study existing nRF24L01+ Python/Java libraries and see how to expand the libraries with more features.

(3) I also found that Pi4J libraries include SPI. So I guess I should jump start to Pi4J and see if I can just use it to read nRF24L01’s config register, which is what I wanted to do in the very beginning.

/ to continue, …


(1) Installing Java on the Raspberry Pi (OK for Rpi4 buster) – Emmet, PiMyLifeUp 2019dec12

(2) Efficient Java Development for the Raspberry Pi – GregF10 2018

(3) How to Install Java on Raspberry Pi – 2020feb24

(4) BlueJ – Wikipedia

(5) GreenFoot

(6) The BlueJ system and its pedagogy – M Kölling etal 2003dec

(7) BlueJ on the Raspberry Pi – BlueJ.org

(8) BlueJ LED Tutorial – BlueJ.org

(9) Raspberry Pi – Programming Pi-Face with Java & Pi4J (SPI)

(10) Java SPI using com.pi4j.wiringpi Class


Appendix A – Install OpenJdk v11.0.6

(1) Installing Java on the Raspberry Pi (OK for Rpi4 buster) – Emmet, PiMyLifeUp 2019dec12

openjdk install

/ to continue, …


Categories: Uncategorized

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.

%d bloggers like this: