nRF24L01 using PIC18F2550 with Brennen lib

Hi guy’s I hope I’m not digging thread that’s too old but I spent bunch of hours with this and I’m starting to hit a brick wall with it :frowning:

I have one arduino sitting 1m from my table running ping_server ( [mirf library example) that looks like this

void setup(){
  Serial.begin(9600);
   Mirf.cePin = 8;
   Mirf.csnPin = 9;
  Mirf.spi = &MirfHardwareSpi;
  Mirf.init();
  Mirf.setRADDR((byte *)"serv1");
  Mirf.payload = sizeof(unsigned long);
  Mirf.config();
  Serial.println("Listening..."); 
}

void loop(){
  byte data[Mirf.payload];
  int i;
  if(!Mirf.isSending() && Mirf.dataReady()){
    Serial.print("Got packet: 0x");
    Mirf.getData(data);
    for(i=0;i<Mirf.payload;i++)
      Serial.print(data[i], HEX);
    Serial.println(".");
     
    Mirf.setTADDR((byte *)"clie1");
    Mirf.send(data);
    Serial.println("Reply sent.");
  }
}

And when I use the ping_client on another arduino everything works (client code looks like)

void setup(){
  Serial.begin(9600);
  Mirf.cePin  = 7;
  Mirf.csnPin = 53;
  Mirf.spi = &MirfHardwareSpi;
  Mirf.init();
  Mirf.setRADDR((byte *)"clie1");
  Mirf.payload = sizeof(unsigned long);
  Mirf.config();
  Serial.println("Beginning ... "); 
}

void loop(){
  unsigned long time = millis();
  Mirf.setTADDR((byte *)"serv1");
  Mirf.send((byte *)&time);
  while(Mirf.isSending()){}
  Serial.println("Finished sending");
  delay(10);
  while(!Mirf.dataReady()){
    if ( ( millis() - time ) > 1000 ) {
      Serial.println("Timeout on response from server!");
      return;
    }
  }
 
  Mirf.getData((byte *) &time);
  delay(1000);
}

And, as I say - this works … now, I don’t use arduino in my project but I use pic32.

I rewrote first the arduino code and it don’t work, now I tried Brennen code and it again don’t work …

The only changes to Brennen code to run on pic32 (pic32mx460F512L, c32, mplabx) are in the nrf24l01.c:

#define NRFL_SPI_CHN SPI_CHANNEL1
#define NRFL_cePin PORTAbits.RA15
#define NRFL_ceTris TRISAbits.TRISA15
#define NRFL_csnPin PORTAbits.RA14
#define NRFL_csnTris TRISAbits.TRISA14
#define NRFL_irqPin  PORTBbits.RB10
#define NRFL_irqTris TRISBbits.TRISB10

extern volatile unsigned int CoreDelay; //decreased every 10us

void delay10us(unsigned int i) {
    CoreDelay = i;
    while (CoreDelay);
}

unsigned char spi_send_read_byte(unsigned char b) {
    unsigned short txData, rxData; // transmit, receive characters

    txData = b;                        // take inputted byte and store into txData
    SpiChnPutC(NRFL_SPI_CHN, txData);  // send data
    rxData = SpiChnGetC(NRFL_SPI_CHN); // retreive over channel chn the received data into rxData
    return rxData;
}

void SpiInitDevice(int chn, int isMaster, int frmEn, int frmMaster) {
    unsigned int config = SPI_CON_MODE8 | SPI_CON_SMP | SPI_CON_ON;
    if (isMaster) {
        config |= SPI_CON_MSTEN;
    }
    if (frmEn) {
        config |= SPI_CON_FRMEN;
        if (!frmMaster) {
            config |= SPI_CON_FRMSYNC;
        }
    }
    SpiChnOpen(chn, config, 15);
}

//....
// This is added to beginning of the nrf24l01_initialize

        NRFL_ceTris  = 0;
        NRFL_csnTris = 0;
        NRFL_irqTris = 1;
        SpiInitDevice(NRFL_SPI_CHN, 1, 0, 0);
//...
void nrf24l01_clear_ce()
{
	//nrf24l01_CE_IOREGISTER &= ~nrf24l01_CE_PINMASK;
    NRFL_cePin = 0;
}

//sets the pin on the host microcontroller that is attached to the 24l01's CE pin
void nrf24l01_set_ce()
{
	//nrf24l01_CE_IOREGISTER |= nrf24l01_CE_PINMASK;
    NRFL_cePin = 1;
}

//returns true if CE is high, false if not
bool xnrf24l01_ce_pin_active()
{
/*
    if((nrf24l01_CE_IOREGISTER & nrf24l01_CE_PINMASK) != 0)
		return true;
	else
		return false;
 */
    return (NRFL_cePin)?true:false; //not sure how is this useful as CE pin is output from mcu?!
}

//sets the pin on the host microcontroller that is attached to the 24l01's CSN pin
void nrf24l01_clear_csn()
{
	//nrf24l01_CSN_IOREGISTER &= ~nrf24l01_CSN_PINMASK;
    NRFL_csnPin = 0;
}

//clears the pin on the host microcontroller that is attached to the 24l01's CSN pin
void nrf24l01_set_csn()
{
	//nrf24l01_CSN_IOREGISTER |= nrf24l01_CSN_PINMASK;
        NRFL_csnPin = 1;
}

//returns true if CSN is high, false if not
bool xnrf24l01_csn_pin_active()
{
    /*
	if((nrf24l01_CSN_IOREGISTER & nrf24l01_CSN_PINMASK) != 0)
		return true;
	else
		return false;
     */
    return (NRFL_csnPin)?true:false;
}
[/quote]

and that's all changes I made.

The main goes like this:

[code]
// config bits
//...

volatile unsigned int CoreDelay;

int main(void) {
    unsigned long ttt;

    SYSTEMConfigPerformance(GetSystemClock());
    while (!(OSCCON & 0x00000020)); 
    OpenCoreTimer(CORE_TICK_RATE);
    mConfigIntCoreTimer((CT_INT_ON | CT_INT_PRIOR_2 | CT_INT_SUB_PRIOR_0));
    INTEnableSystemMultiVectoredInt();
    CloseADC10();
    CoreDelay = 0;

    nrf24l01_initialize_debug(true, 4, false);
    nrf24l01_set_rx_addr("clie1", 5, 0);
    nrf24l01_set_rx_pw(4,0);
    nrf24l01_set_tx_addr("serv1", 5);

    ttt = 0;
    while (1) {
        ttt++;
        nrf24l01_set_as_tx();
        nrf24l01_write_tx_payload((unsigned char *)&ttt, 4, true);
        mLED_1_On();
        while (!(nrf24l01_irq_pin_active() && nrf24l01_irq_tx_ds_active()));

//////////////////////////////
//IT NEVER ARRIVES HERE!!!!!!!!!!!
//////////////////////////////

        mLED_2_On();
        nrf24l01_irq_clear_all(); //clear interrupts again
        nrf24l01_set_as_rx(true); //operation as a RX
        mLED_3_On();
        CoreDelay = 10000;
        while (CoreDelay);
    }
//...

I use arduino ping_server as receiver but there I receive nothing and pic32 is stuck in

while (!(nrf24l01_irq_pin_active() && nrf24l01_irq_tx_ds_active()));

Logic analyzer show that everything is perfectly going trough wires (SPI is 6MHz so below 8MHz limit of the module, slowing it down didn’t change anything).](Arduino Playground - HomePage)

Here’s also the SPI decoded by saleae logic analyzer … everything looks perfect for me, but the NRF24L01+ is constantly returning 0x0E :frowning:

If anyone can help …

SPI looks OK from what I can tell. It would be really helpful if you could get a zoomed in trace when you actually try to transmit a packet (preferrably one-byte payload to make it as short as possible) such that you can see the hex values over MISO and MOSI like in the first two images above.

That’s why I added the spi_decoded.csv, it’s comma separated values, each line is what’s on MOSI, what’s on MISO…

I’ll make more snapshots in few moments and upload the pics, thanks for checking it out

Here the pic’s are:

http://elco.crsndoo.com/files/nrfl_problem/

http://elco.crsndoo.com/files/nrfl_problem/spi1.png

http://elco.crsndoo.com/files/nrfl_problem/spi2.png

http://elco.crsndoo.com/files/nrfl_problem/spi3.png

http://elco.crsndoo.com/files/nrfl_problem/spi4.png

http://elco.crsndoo.com/files/nrfl_problem/spi5.png

http://elco.crsndoo.com/files/nrfl_problem/spi6.png

http://elco.crsndoo.com/files/nrfl_problem/spi7.png

http://elco.crsndoo.com/files/nrfl_problem/spi8.png

http://elco.crsndoo.com/files/nrfl_problem/spi9.png

SPI communication:

MOSI               MISO
! (0x21)               14 (0x0E)
0 (0x00)               0 (0x00)
" (0x22)               14 (0x0E)
3 (0x03)               0 (0x00)
# (0x23)               14 (0x0E)
3 (0x03)               0 (0x00)
$ (0x24)               14 (0x0E)
3 (0x03)               0 (0x00)
% (0x25)               14 (0x0E)
2 (0x02)               0 (0x00)
& (0x26)               14 (0x0E)
15 (0x0F)               0 (0x00)
: (0x3A)               14 (0x0E)
231 (0xE7)               0 (0x00)
231 (0xE7)               0 (0x00)
247 (0xF7)               0 (0x00)
231 (0xE7)               0 (0x00)
231 (0xE7)               0 (0x00)
171 (0xAB)               14 (0x0E)
194 (0xC2)               0 (0x00)
194 (0xC2)               0 (0x00)
194 (0xC2)               0 (0x00)
194 (0xC2)               0 (0x00)
194 (0xC2)               0 (0x00)
 (0x2C)               14 (0x0E)
195 (0xC3)               0 (0x00)
- (0x2D)               14 (0x0E)
196 (0xC4)               0 (0x00)
. (0x2E)               14 (0x0E)
197 (0xC5)               0 (0x00)
/ (0x2F)               14 (0x0E)
198 (0xC6)               0 (0x00)
0 (0x30)               14 (0x0E)
231 (0xE7)               0 (0x00)
231 (0xE7)               0 (0x00)
231 (0xE7)               0 (0x00)
231 (0xE7)               0 (0x00)
231 (0xE7)               0 (0x00)
1 (0x31)               14 (0x0E)
6 (0x06)               0 (0x00)
2 (0x32)               14 (0x0E)
0 (0x00)               0 (0x00)
3 (0x33)               14 (0x0E)
128 (0x80)               0 (0x00)
4 (0x34)               14 (0x0E)
0 (0x00)               0 (0x00)
7 (0x37)               14 (0x0E)
0 (0x00)               0 (0x00)
6 (0x36)               14 (0x0E)
0 (0x00)               0 (0x00)
  (0x20)               14 (0x0E)
11 (0x0B)               0 (0x00)

---INITIALISATION DONE---
--- 1.5ms pause ---
---Sending start---

* (0x2A)               14 (0x0E)
c (0x63)               0 (0x00)
l (0x6C)               0 (0x00)
i (0x69)               0 (0x00)
e (0x65)               0 (0x00)
1 (0x31)               0 (0x00)
1 (0x31)               14 (0x0E)
4 (0x04)               0 (0x00)
0 (0x30)               14 (0x0E)
s (0x73)               0 (0x00)
e (0x65)               0 (0x00)
r (0x72)               0 (0x00)
v (0x76)               0 (0x00)
1 (0x31)               0 (0x00)
128 (0x80)               14 (0x0E)
5 (0x05)               8 (0x08)
160 (0xA0)               14 (0x0E)
1 (0x01)               0 (0x00)
0 (0x00)               0 (0x00)
0 (0x00)               0 (0x00)
0 (0x00)               0 (0x00)
255 (0xFF)               14 (0x0E)
255 (0xFF)               14 (0x0E)
255 (0xFF)               14 (0x0E)
255 (0xFF)               14 (0x0E)
255 (0xFF)               14 (0x0E)
255 (0xFF)               14 (0x0E)
255 (0xFF)               14 (0x0E)
255 (0xFF)               14 (0x0E)
255 (0xFF)               14 (0x0E)
255 (0xFF)               14 (0x0E)
255 (0xFF)               14 (0x0E)
255 (0xFF)               14 (0x0E)
255 (0xFF)               14 (0x0E)
255 (0xFF)               14 (0x0E)
255 (0xFF)               14 (0x0E)
255 (0xFF)               14 (0x0E)
255 (0xFF)               14 (0x0E)
255 (0xFF)               14 (0x0E)
255 (0xFF)               14 (0x0E)
255 (0xFF)               14 (0x0E)
255 (0xFF)               14 (0x0E)
255 (0xFF)               14 (0x0E)
255 (0xFF)               14 (0x0E)
255 (0xFF)               14 (0x0E)
255 (0xFF)               14 (0x0E)

Interrupt is high all the time

CSN wraps around packets, sometimes 1 byte, sometimes more but imo looks like perfect SPI

CE is bit confusing, before first byte is sent (0x21) CE goes LOW, then is kept low during initialization and during 1.5ms pause after initialization, then, right before CSN goes LOW for the “*clie1” CE goes HIGH, and stay HIGH util the whole sending is done, and then goes LOW for the reading of the status reg that then continuously returns 0x0E :frowning:

I did manage to see few times glitches in the SPI, not sure if it was just issue with this Saleae LA interference with breadboard or error in hw spi of the 32mx460f512l … but I don’t believe that’s the problem as in 99.99% times all bytes are as expected… (few times for e.g. the *clie1 was +clie1, one time the 0x2A on the beginning of the stream was 0x2B…) but as I say, I don’t think that’s the problem

Try my simple SPI test code:

http://www.leonheller.com/Wireless%20Se … 24L01+.zip

Hi Leon, thanks, I already tried something similar and I see all register values to be correct :frowning: … lemme get some screen shots of that too …

I have some doubts about CE pin, the datasheet states some 10uS pulse on CE pin for sending data, here I see that CE pin is up during 20 byte transfer!! that’s way longer then 10uS…

wow, this is weird…

I push 11,22,33,44,55 and get back 22,44,66,88,AA ?! interesting … and the logic analyzer show proper values :smiley: :smiley: looks like SPI is reading data in the wrong place :frowning: … that should be easy fixable … back in a few with more results