Java nRF24L01

Active today
Viewed 36 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 23 hours ago
  • 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 21 hours ago
  • (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 21 hours ago
  • (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 21 hours ago
  • (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 21 hours ago
  • (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 21 hours ago
  • 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 20 hours ago
  • 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 20 hours ago
  • 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 19 hours ago
  • 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 8 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 8 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 12 mins 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 26 secs ago   Edit

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: