problem with nrf24l01 and stm32 wrong address value is sent
hi I used the code in this link
for communicating with nrf24l01 with stm32 and it worked fine but I wanted to rewrite it in register level so I rewrote the code but I encountered a problem. the default transmitter address should be E7,E7,E7,E7,E7 based on the datasheet but the address that I am getting with uart is something different like 0B,E0,00,C6,30. the RF power is shown 0 dB which is correct and also 4 of receiver pipe addresses are correct. but I don't get the sent characters from the transmitter microcontroller. I attached my code below. what do you think might caused this problem?
the nrf24l01 driver code is about 1000 lines so i can't copy all of it here but the most important functions in this driver code is as follows :
void GPIO_config(void)
{
//spi config
RCC->APB2ENR |= (1<<12); /* Enable SPI1 clock */
RCC->APB2ENR |= (1<<2); // enable GPIOA clock
RCC->APB2ENR |= (1<<3);
GPIOA->CRL&=~0xFFF00000; // clear A.5 , A.6 , A.7
GPIOA->CRL |=0xB4B00000; // A.5 and A.7 alternate function output A.6 input
/* Enable SPI in Master Mode, CPOL=0, CPHA=0. */
/* Clock speed = fPCLK1 / 256 = 280 kHz at 72 MHz PCLK1 clk. */
SPI1->CR1 = (0 << 0) | // Clock phase
(0 << 1) | // Clock polarity
(1 << 2) | // Master selection
(5 << 3) | // Baud rate control clock/256
(1 << 6) | // SPI enable
(0 << 7) | // Frame format : 0: MSB transmitted first
(1 << 8) | // Internal slave select
(1 << 9) | // Software slave management enabled
(0 << 11); // Data frame format : 0: 8-bit
SPI1->CR2 = 0x0000;
//other GPIO config
// enable GPIOB clock
GPIOB->CRL &= ~0xF; // GPIOB.0 clear
GPIOB->CRL |= 0x3; // GPIOB.0 output push pull
GPIOB->CRL &= ~(0xF00F0000); // GPIOB.4 and GPIOB.7 clearing
GPIOB->CRL |= 0x10010000; // GPIOB.4 and GPIOB.7 output pushpull 10 MHz
}
uint8_t SendBuf (uint8_t *buf, int sz)
{
/* Send buffer to SPI interface. */
int i;
for (i = 0; i < sz; i++)
{
SPI1->DR = buf[i];
/* Wait if TXE cleared, Tx FIFO is full. */
while (!(SPI1->SR & TXE));
SPI1->DR;
}
/* Wait until Tx finished, drain Rx FIFO. */
while (SPI1->SR & (BSY | RXNE))
{
SPI1->DR;
}
return (1);
}
uint8_t RecBuf (uint8_t *buf, int sz) {
/* Receive SPI data to buffer. */
int i;
for (i = 0; i < sz; i++)
{
SPI1->DR = 0xFF;
/* Wait if RNE cleared, Rx FIFO is empty. */
while (!(SPI1->SR & RXNE));
buf[i] = SPI1->DR;
}
return (1);
}
//3. Read single byte from a register
uint8_t NRF24_read_register(uint8_t reg)
{
uint8_t spiBuf[3];
uint8_t retData;
//Put CSN low
NRF24_csn(0);
//Transmit register address
spiBuf[0] = reg&0x1F;
SendBuf(spiBuf,1);
//Receive data
RecBuf(&spiBuf[1],1);
retData = spiBuf[1];
//Bring CSN high
NRF24_csn(1);
return retData;
}
//4. Read multiple bytes register
void NRF24_read_registerN(uint8_t reg, uint8_t *buf, uint8_t len)
{
uint8_t spiBuf[3];
//Put CSN low
NRF24_csn(0);
//Transmit register address
spiBuf[0] = reg&0x1F;
//spiStatus = NRF24_SPI_Write(spiBuf, 1);
SendBuf(spiBuf,1);
//Receive data
RecBuf(buf,len);
//Bring CSN high
NRF24_csn(1);
}
//5. Write single byte register
void NRF24_write_register(uint8_t reg, uint8_t value)
{
uint8_t spiBuf[3];
//Put CSN low
NRF24_csn(0);
//Transmit register address and data
spiBuf[0] = reg|0x20;
spiBuf[1] = value;
SendBuf(spiBuf,2);
//Bring CSN high
NRF24_csn(1);
}
//6. Write multipl bytes register
void NRF24_write_registerN(uint8_t reg, const uint8_t* buf, uint8_t len)
{
uint8_t spiBuf[3];
//Put CSN low
NRF24_csn(0);
//Transmit register address and data
spiBuf[0] = reg|0x20;
SendBuf(spiBuf,1);
SendBuf(spiBuf,len);
//Bring CSN high
NRF24_csn(1);
}
void NRF24_begin( void)
{
uint8_t pipeAddrVar[6];
//Copy SPI handle variable
//Copy Pins and Port variables
//Put pins to idle state
NRF24_csn(1);
NRF24_ce(0);
//5 ms initial delay
Delay_ms(5);
//**** Soft Reset Registers default values ****//
NRF24_write_register(0x00, 0x08);
NRF24_write_register(0x01, 0x3f);
NRF24_write_register(0x02, 0x03);
NRF24_write_register(0x03, 0x03);
NRF24_write_register(0x04, 0x03);
NRF24_write_register(0x05, 0x02);
NRF24_write_register(0x06, 0x0f);
NRF24_write_register(0x07, 0x0e);
NRF24_write_register(0x08, 0x00);
NRF24_write_register(0x09, 0x00);
pipeAddrVar[4]=0xE7; pipeAddrVar[3]=0xE7; pipeAddrVar[2]=0xE7; pipeAddrVar[1]=0xE7; pipeAddrVar[0]=0xE7;
NRF24_write_registerN(0x0A, pipeAddrVar, 5);
pipeAddrVar[4]=0xC2; pipeAddrVar[3]=0xC2; pipeAddrVar[2]=0xC2; pipeAddrVar[1]=0xC2; pipeAddrVar[0]=0xC2;
NRF24_write_registerN(0x0B, pipeAddrVar, 5);
NRF24_write_register(0x0C, 0xC3);
NRF24_write_register(0x0D, 0xC4);
NRF24_write_register(0x0E, 0xC5);
NRF24_write_register(0x0F, 0xC6);
pipeAddrVar[4]=0xE7; pipeAddrVar[3]=0xE7; pipeAddrVar[2]=0xE7; pipeAddrVar[1]=0xE7; pipeAddrVar[0]=0xE7;
NRF24_write_registerN(0x10, pipeAddrVar, 5);
NRF24_write_register(0x11, 0);
NRF24_write_register(0x12, 0);
NRF24_write_register(0x13, 0);
NRF24_write_register(0x14, 0);
NRF24_write_register(0x15, 0);
NRF24_write_register(0x16, 0);
NRF24_ACTIVATE_cmd();
NRF24_write_register(0x1c, 0);
NRF24_write_register(0x1d, 0);
printRadioSettings();
//Initialise retries 15 and delay 1250 usec
NRF24_setRetries(15, 15);
//Initialise PA level to max (0dB)
NRF24_setPALevel(RF24_PA_0dB);
//Initialise data rate to 1Mbps
NRF24_setDataRate(RF24_2MBPS);
//Initalise CRC length to 16-bit (2 bytes)
NRF24_setCRCLength(RF24_CRC_16);
//Disable dynamic payload
NRF24_disableDynamicPayloads();
//Set payload size
NRF24_setPayloadSize(32);
//Reset status register
NRF24_resetStatus();
//Initialise channel to 76
NRF24_setChannel(76);
//Flush buffers
NRF24_flush_tx();
NRF24_flush_rx();
NRF24_powerDown();
}the main code of the receiver :
#include "MY_NRF24.h"
#include "nRF24L01.h"
#include "usart.h"
#include <stm32f10x.h>
uint64_t Rxpipeaddrs=0x11223344AA;
char myRxData[50];
char * buffer;
int main()
{
usart_init();
usart_sendstring("salam");
GPIO_config();
SysTick_Config(SystemCoreClock/1000); // setup systick timer for 1ms interrupts
NRF24_begin();
printRadioSettings();
NRF24_setAutoAck(false);
NRF24_setChannel(52);
NRF24_setPayloadSize(32);
NRF24_openReadingPipe(1,Rxpipeaddrs);
NRF24_startListening();
while(1)
{
if (NRF24_available())
{
NRF24_read(myRxData,32);
// myRxData[32]='\r'; myRxData[32+1]='\n';
usart_sendstring(myRxData);
buffer=strstr(myRxData,"123456789");
if (buffer)
{
usart_sendstring("successful");
}
}
}
}the transmitter main code :
#include "usart.h"
#include "MY_NRF24.h"
#include "nRF24L01.h"
#include <stm32f10x.h>
uint64_t Txpipeaddrs=0x11223344AA;
uint8_t myTxdata[32]="123456789";
int main ()
{
usart_init();
GPIO_config();
SysTick_Config(SystemCoreClock/1000); // setup systick timer for 1ms interrupts
NRF24_begin();
printRadioSettings();
NRF24_stopListening();
NRF24_openWritingPipe(Txpipeaddrs);
NRF24_setAutoAck(false);
NRF24_setChannel(52);
NRF24_setPayloadSize(32);
while (1)
{
if (NRF24_write(myTxdata,32))
{
usart_sendstring("transmitted successfully");
}
Delay_ms(1000);
}