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 Reply Children
Related