This post is older than 2 years and might not be relevant anymore
More Info: Consider searching for newer posts

WRITE/READ COMMAND FOR NRF24L01 VIA SPI

Hi, i'm using the nrf24l01 with a zynq board to use it as radiocontrol; i'm trying to configure the nrf as receiver so that i'm able to send data from another nrf connected to an arduino. The first step i had to do, as i understood frome the datasheet, is being able to write and read register from nrf, so that i can configure nrf as receiver and also i can modify parameters for the communication (as bit rate, frequency,ecc). i tried to write the CF_REG with 00000011 (that are the setting for PRIM_RX and PWR_UP) using spi, and then reading the new content of the register. As i discovered i had to send the command and then the data i must overwrite, so i first sent 00100000 (that should be write register CF_REG), and then i sent 00000011, but it seems not to work. The first question i'd like to ask is if i have to send the 2 bytes with two different commands or if i can send a single command with a 2 bytes data (00100000-00000011 in this case).
The second question is if anyone already worked with zynq and is able to determine if i'm configuring properly the sdk. the code i wrote is below, thanks

void write(XSpiPs *myinstanceptr){
u8 * commandptr;
u16 command = 0b0010001000000011;
commandptr = &command;
u8 * dataptr;
u8 data = 0b00000011;
dataptr = &data;
XSpiPs_SetSlaveSelect(myinstanceptr,0);
(myinstanceptr)->IsBusy=0x00000000;
XSpiPs_Transfer(myinstanceptr,commandptr,NULL,2);
(myinstanceptr)->IsBusy=0x00000000;
//XSpiPs_Transfer(myinstanceptr,dataptr,NULL,1);
//(myinstanceptr)->IsBusy=0x00000000;
XSpiPs_SetSlaveSelect(myinstanceptr,0X0F);

}


u8 read(XSpiPs *myinstanceptr){
u8 * commandptr;
u8 command = 0x00;
commandptr = &command;
u8 * dataptr;
u8 data;
u8 *rcvptr2;
u8 rcv2;
rcvptr2 = &rcv2;
dataptr = &data;
u8 * rcvptr;
u8 rcv;
rcvptr = &rcv;
XSpiPs_SetSlaveSelect(myinstanceptr,0);
(myinstanceptr)->IsBusy=0x00000000;
XSpiPs_Transfer(myinstanceptr,commandptr,rcvptr2,1);
(myinstanceptr)->IsBusy=0x00000000;
XSpiPs_Transfer(myinstanceptr,dataptr,rcvptr,1);
(myinstanceptr)->IsBusy=0x00000000;
XSpiPs_SetSlaveSelect(myinstanceptr,0x0F);
return rcv;
}

int main(){
init_platform();
Xil_Out8(IRQ_BASE_ADDR,0x01);
XSpiPs *myinstanceptr;
XSpiPs_Config *myinstanceptr2;
XSpiPs instanceptr;
XSpiPs_Config instanceptr2;
myinstanceptr = &instanceptr;
myinstanceptr2 = &instanceptr2;
myinstanceptr2->InputClockHz = XPAR_PS7_SPI_0_SPI_CLK_FREQ_HZ;
XSpiPs_CfgInitialize(myinstanceptr,myinstanceptr2,XPAR_PS7_SPI_0_BASEADDR);
XSpiPs_SetOptions(myinstanceptr,XSPIPS_MASTER_OPTION);
//XSpiPs_SetClkPrescaler(myinstanceptr,XSPIPS_CLK_PRESCALE_256);
u8 result;
result = XSpiPs_IsManualChipSelect(myinstanceptr);
result = XSpiPs_IsDecodeSSelect(myinstanceptr);
result = XSpiPs_IsManualStart(myinstanceptr);
u8 *txbuf;
u8 *rxbuf;
u16 tx = 0b11001111;
u8 rx;
txbuf = &tx;
rxbuf = ℞
while(1){
write(myinstanceptr);
result = read(myinstanceptr);

}


return 0;

}



EDIT. the problem was due to the ZYNQ configuration that deasserted the csn between the end of a word and the following one, so that every byte was considered a new command for the nrf; this can easily be solved changing the function XSpiPs_SetOptions(myinstanceptr,XSPIPS_MASTER_OPTION); with the XSpiPs_SetOptions(myinstanceptr,XSPIPS_MASTER_OPTION | XSPIPS_CLK_PHASE_1_OPTION); (the clk phase option allows csn to keep asserted even during the transition of 2 word)

Parents
  • I recommend to get a logic analyzer trace to observe the CSN, SCK, MISO and MOSI pins follow the Figure 26 and 27 in the datasheet: https://infocenter.nordicsemi.com/topic/struct_nrf24/struct/nrf24L01p_ps.html

    You may share the traces here so we can take a look.

  • i'd like to have a logic analyzer; for what i undersrood the 4 spi signals follows that figures (i only have a doubt about the sck behaviour between lsb of a word and msb of the next one); the main problem is that during the commands the nrf does neither receive the data nor send the status register i'ts like it isn't receiving any command via spi

  • You will need a logic analyzer trace, and make sure that you have not switched MISO and MOSI somehow, and that the chip is powered properly and ground is also connected between host MCU and L01.

  • As noted in the  other thread

    These chips are very widely used - so there  are a lot of examples & tutorials out there.

    For example:

    https://www.avrfreaks.net/comment/2758091#comment-2758091

    So getting a logic analyzer trace from one of those, and comparing against your implementation would be a good idea.

    i'ts like it isn't receiving any command via spi

    Beware that there are fakes out there:

    https://www.avrfreaks.net/comment/2764341#comment-2764341

    Are you sure your chip isn't a fake?

    And that it is correctly wired, and hasn't been damaged?

  • i don't think they are fake, they were given to me from university (or at least i hope so), and they shouldn't be damaged since if connected with two arduino they communicate properly, i'll try to check these codes but from what i saw they are written for arduino and i don't think they would help me, thank you anyway

  • If you're at a University then, surely, you should be able to get a teacher or supervisor - or even fellow student - to help you?

    That would be far easier and more efficient than trying to get remote debugging from complete strangers who have no idea what you're doing, or what equipment you have, no access to your hardware, etc, etc, ...

    if connected with two arduino they communicate properly

    a prime example: we have no way of knowing that!

    Anyhow, again, if you have comms working with an Arduino, then get logic analyser traces of that, and compare to what's happening in your system.

Reply
  • If you're at a University then, surely, you should be able to get a teacher or supervisor - or even fellow student - to help you?

    That would be far easier and more efficient than trying to get remote debugging from complete strangers who have no idea what you're doing, or what equipment you have, no access to your hardware, etc, etc, ...

    if connected with two arduino they communicate properly

    a prime example: we have no way of knowing that!

    Anyhow, again, if you have comms working with an Arduino, then get logic analyser traces of that, and compare to what's happening in your system.

Children
No Data