cancel
Showing results for 
Search instead for 
Did you mean: 

STM32F103C8T6 (Blue Pill) Can't Get the Register Value In First Read From NRF24L01+

Clyde_Xander
Associate II

Hello everyone,
I have a STM32F103C8T6 Blue Pill board and NRF24L01+ RF module with 5V to 3.3V NRF24 adapter. I wore a code for reading and writing NRF24 register via SPI serial communication. In order to read any register value from NRF24 we send a command signal first. when the command signal shifted out from MOSI pin, NRF24 shifts out the STATUS register value on MISO pin simultaneously. After that it shifts out the register value that specified in command register.

 
NRF24_Write_8Bit_Register function reads any 8 BIT register value and does some bitwise operation after that it sends the register value back to NRF24. I want to read CONFIG register in NRF24 and add 0x02 to that register. When i run this function NRF24 shifts out the STATUS register value and CONFIG register value. The values i get is 0x0E and 0x00. But its suppose to be 0x0E and 0x08. But if i run  NRF24_Read_8Bit_Register before the write function i can get 0x0E and 0x08.

Why i cannot receive correct values in my first read ?

NOTE: I used logic analyzer and i saw that NRF24 shifts out hte register values whitout any issue. I used debugger and looked at SPI1->DR register. In register there was 0x00 but  NRF24 sends 0x08. I used DMA but the problem repeats itself.


#include "stm32f10x.h"
#include "stm32f10x_rcc.h"
#include "stm32f10x_gpio.h"
#include "stm32f10x_exti.h"
#include "stm32f10x_usart.h"
#include "stm32f10x_spi.h"
#include "stm32f10x_dma.h"

/*Command Registers*/
#define R_REGISTER            ((uint8_t)0x00)
#define W_REGISTER            ((uint8_t)0x20)
#define R_RX_PAYLOAD          ((uint8_t)0x61)
#define W_TX_PAYLOAD          ((uint8_t)0xA0)
#define FLUSH_TX              ((uint8_t)0xE1)
#define FLUSH_RX              ((uint8_t)0xE2)
#define REFUSE_TX_PL          ((uint8_t)0xE3)
#define R_RX_PL_WID           ((uint8_t)0x60)
#define W_ACK_PAYLOAD_0       ((uint8_t)0x98)
#define W_ACK_PAYLOAD_1       ((uint8_t)0x99)
#define W_ACK_PAYLOAD_2       ((uint8_t)0x9A)
#define W_ACK_PAYLOAD_3       ((uint8_t)0x9B)
#define W_ACK_PAYLOAD_4       ((uint8_t)0x9C)
#define W_ACK_PAYLOAD_5       ((uint8_t)0x9D)
#define W_TX_PAYLOAD_NO_ACK   ((uint8_t)0xB0)
#define NOP                   ((uint8_t)0xFF) 

volatile uint32_t ntime = 0;
volatile uint32_t TimerDelay = 0;

/*******************************
********RX_Data_Pipes***********
********************************/

static uint8_t RX_ADDR_0_DataPipe_Value[5] = {0xE7U,0xE7U,0xE7U,0xE7U,0xE7U};
//static uint8_t RX_ADDR_1_DataPipe_Value[5] = {0xC2U,0xC2U,0xC2U,0xC2U,0xC2U};
//static uint8_t RX_ADDR_2_Value = 0x0CU;
//static uint8_t RX_ADDR_3_Value = 0x0DU;
//static uint8_t RX_ADDR_4_Value = 0x0EU;
//static uint8_t RX_ADDR_5_Value = 0x0FU;
//static uint8_t TX_ADDR_Value[5] = {0xE7U,0xE7U,0xE7U,0xE7U,0xE7U};

//volatile uint8_t NRF_Read_Payload[32] = {0};
//volatile uint8_t NRF_Write_Payload[32] = {0};


void GPIO_SETUP(void);
//void EXTI_SETUP(void);
void SPI_SETUP(void);
void USART_SETUP(void);
void SysTick_SETUP(void);


void SysTick_Delay_uS(uint16_t ntime);
void SysTick_Delay_mS (uint16_t ntime);

void NRF_Testing_Write_Function(uint8_t NRF24_Register, uint8_t NRF24_Register_Bit);
void NRF24_Read_Status(uint8_t *Data_Store);
void NRF24_Write_8Bit_Register(uint8_t NRF24_Register, uint8_t NRF24_Register_Bit, uint8_t NRF24_Set_Reset);
void NRF24_Read_8Bit_Register(uint8_t NRF24_Register, uint8_t *Data_Store);
void NRF24_40Bit_Write_Register(uint8_t NRF_Register, uint8_t *Register_Data);
void NRF24_40Bit_Read_Register(uint8_t NRF_Register, uint8_t *Read_Register_Data);
void NRF_Payload_Write(uint8_t *NRF_Payload);






int main(void){

    GPIO_SETUP();
    //EXTI_SETUP();
    SPI_SETUP();
    USART_SETUP();
    SysTick_SETUP();

    uint8_t testing_some = 0;
    //uint8_t testing_array[5] = {0xAA, 0xBB,0xCC, 0xDD, 0xEE};
    //uint8_t testing_array01[5] = {0};

    SysTick_Delay_mS(4000);
    NRF_Testing_Write_Function(CONFIG, 0x08);
    SysTick_Delay_uS(30);
    NRF24_Read_8Bit_Register(CONFIG, &testing_some);
    NRF24_Write_8Bit_Register(CONFIG, 0x02, 1);
    NRF24_Read_8Bit_Register(CONFIG, &testing_some);
    NRF24_Write_8Bit_Register(CONFIG, 0x02, 0);
    NRF24_Read_8Bit_Register(CONFIG, &testing_some);
    NRF24_Write_8Bit_Register(CONFIG, 0x02, 1);
    NRF24_Read_8Bit_Register(CONFIG, &testing_some);
    NRF24_Write_8Bit_Register(CONFIG, 0x02, 0);
    NRF24_Read_8Bit_Register(CONFIG, &testing_some);
    NRF24_Write_8Bit_Register(CONFIG, 0x02, 1);
    NRF24_Read_8Bit_Register(CONFIG, &testing_some);
    NRF24_Write_8Bit_Register(CONFIG, 0x02, 0);
    NRF24_Read_8Bit_Register(CONFIG, &testing_some);
    NRF24_Write_8Bit_Register(CONFIG, 0x02, 1);
    NRF24_Read_8Bit_Register(CONFIG, &testing_some);
    NRF24_Write_8Bit_Register(CONFIG, 0x02, 0);
    NRF24_Read_8Bit_Register(CONFIG, &testing_some);
    NRF24_Write_8Bit_Register(CONFIG, 0x02, 1);
    NRF24_Read_8Bit_Register(CONFIG, &testing_some);
    NRF24_Write_8Bit_Register(CONFIG, 0x02, 0);
    NRF24_Read_8Bit_Register(CONFIG, &testing_some);
    NRF24_Write_8Bit_Register(CONFIG, 0x02, 1);
    NRF24_Read_8Bit_Register(CONFIG, &testing_some);
    NRF24_Write_8Bit_Register(CONFIG, 0x02, 0);
    NRF24_Read_8Bit_Register(CONFIG, &testing_some);
    NRF24_Write_8Bit_Register(CONFIG, 0x02, 1);
    NRF24_Read_8Bit_Register(CONFIG, &testing_some);
    NRF24_Write_8Bit_Register(CONFIG, 0x02, 0);
    
    while(1){
        GPIO_ResetBits(GPIOC,GPIO_Pin_13);
        SysTick_Delay_mS(2000);
        GPIO_SetBits(GPIOC,GPIO_Pin_13);
        SysTick_Delay_mS(2000);
    }
}






void NRF_Testing_Write_Function(uint8_t NRF24_Register, uint8_t NRF24_Register_Bit){

    GPIO_ResetBits(GPIOA,GPIO_Pin_4);

    SPI_I2S_SendData(SPI1, (0x20 | NRF24_Register));
    (void)SPI_I2S_ReceiveData(SPI1);

    while(!(SPI_I2S_GetFlagStatus(SPI1, SPI_I2S_FLAG_TXE)));
    SPI_I2S_SendData(SPI1, NRF24_Register_Bit);

    (void)SPI_I2S_ReceiveData(SPI1);

    while(SPI_I2S_GetFlagStatus(SPI1,SPI_I2S_FLAG_BSY));
    GPIO_SetBits(GPIOA,GPIO_Pin_4);
}







void NRF24_Read_8Bit_Register(uint8_t NRF24_Register, uint8_t *Data_Store){

    uint8_t buffer_read = 0;

    GPIO_ResetBits(GPIOA,GPIO_Pin_4);
    
    SPI_I2S_SendData(SPI1, NRF24_Register);
    
    while(!(SPI_I2S_GetFlagStatus(SPI1, SPI_I2S_FLAG_RXNE)));
    buffer_read = SPI_I2S_ReceiveData(SPI1);


    while(!(SPI_I2S_GetFlagStatus(SPI1, SPI_I2S_FLAG_TXE))); //sending null data for spi reading
    SPI_I2S_SendData(SPI1, 0x00);


    while(!(SPI_I2S_GetFlagStatus(SPI1, SPI_I2S_FLAG_RXNE)));
    *Data_Store = SPI_I2S_ReceiveData(SPI1);

    while(SPI_I2S_GetFlagStatus(SPI1,SPI_I2S_FLAG_BSY));
    GPIO_SetBits(GPIOA,GPIO_Pin_4);

    SysTick_Delay_uS(20);
}






void NRF24_Write_8Bit_Register(uint8_t NRF24_Register, uint8_t NRF24_Register_Bit, uint8_t NRF24_Set_Reset){

    uint8_t buffer_write = 0xFF;
    uint8_t buffer_read = 0;

    NRF24_Read_8Bit_Register(NRF24_Register, &buffer_write);

    if(NRF24_Set_Reset == 1){
        buffer_write |= NRF24_Register_Bit;
    }else{
        buffer_write &=~ NRF24_Register_Bit;
    }

    SysTick_Delay_uS(10);

    GPIO_ResetBits(GPIOA,GPIO_Pin_4);

    SPI_I2S_SendData(SPI1, (0x20 | NRF24_Register));
    buffer_read = SPI_I2S_ReceiveData(SPI1);

    while(!(SPI_I2S_GetFlagStatus(SPI1, SPI_I2S_FLAG_TXE)));
    SPI_I2S_SendData(SPI1, buffer_write);

    while(!(SPI_I2S_GetFlagStatus(SPI1, SPI_I2S_FLAG_RXNE)));
    buffer_read = SPI_I2S_ReceiveData(SPI1);

    while(SPI_I2S_GetFlagStatus(SPI1,SPI_I2S_FLAG_BSY));
    GPIO_SetBits(GPIOA,GPIO_Pin_4);

    SysTick_Delay_uS(20);
}






void NRF24_40Bit_Write_Register(uint8_t NRF_Register, uint8_t *Register_Data){

    uint8_t buffer_read[5] = {0};

    GPIO_ResetBits(GPIOA,GPIO_Pin_4);

    SPI_I2S_SendData(SPI1, (0x20 | NRF_Register));
    (void)SPI_I2S_ReceiveData(SPI1);

    for(int i = 4; i >= 0 ; i--){
        while (!(SPI_I2S_GetFlagStatus(SPI1,SPI_I2S_FLAG_TXE)));
        SPI_I2S_SendData(SPI1,Register_Data[i]);

        while(!(SPI_I2S_GetFlagStatus(SPI1,SPI_I2S_FLAG_RXNE)));
        buffer_read[i] = SPI_I2S_ReceiveData(SPI1);
    }

    while(SPI_I2S_GetFlagStatus(SPI1,SPI_I2S_FLAG_BSY));
    GPIO_SetBits(GPIOA,GPIO_Pin_4);

    SysTick_Delay_uS(50);
}






void NRF24_40Bit_Read_Register(uint8_t NRF_Register, uint8_t *Read_Register_Data){

    uint8_t buffer_read = 0;

    GPIO_ResetBits(GPIOA,GPIO_Pin_4);

    SPI_I2S_SendData(SPI1, NRF_Register);
    SysTick_Delay_uS(8);
    buffer_read = SPI_I2S_ReceiveData(SPI1);

    for(int i = 4; i >= 0; i--){
        while (!(SPI_I2S_GetFlagStatus(SPI1,SPI_I2S_FLAG_TXE)));
        SPI_I2S_SendData(SPI1,0x00);

        while(!(SPI_I2S_GetFlagStatus(SPI1,SPI_I2S_FLAG_RXNE)));
        Read_Register_Data[i] = SPI_I2S_ReceiveData(SPI1);
    }

    while(SPI_I2S_GetFlagStatus(SPI1,SPI_I2S_FLAG_BSY));
    GPIO_SetBits(GPIOA,GPIO_Pin_4);

    SysTick_Delay_uS(50);
}







void NRF_Payload_Write(uint8_t *NRF_Payload){

    GPIO_ResetBits(GPIOA,GPIO_Pin_4);

    SPI_I2S_SendData(SPI1, W_TX_PAYLOAD_NO_ACK);
    SysTick_Delay_uS(10);
    (void)SPI_I2S_ReceiveData(SPI1);

    while(SPI_I2S_GetFlagStatus(SPI1,SPI_I2S_FLAG_BSY));
    GPIO_SetBits(GPIOA,GPIO_Pin_4);

}





//void USART2_IRQHandler(void){
//    if(USART_GetITStatus(USART2,USART_IT_RXNE)){
//
//        while(!(USART_GetFlagStatus(USART2,USART_FLAG_RXNE)));
//        buffer = USART_ReceiveData(USART2);
//
//        GPIO_ResetBits(GPIOA,GPIO_Pin_4);
//
//        NRF24_Command_Status(buffer);
//        data_spi = NRF24_Send_Dummy_Read_Register(8);
//
//        while(SPI_I2S_GetFlagStatus(SPI1,SPI_I2S_FLAG_BSY));
//        GPIO_SetBits(GPIOA,GPIO_Pin_4);
//
//        while(!(USART_GetFlagStatus(USART2,USART_FLAG_TXE)));
//        USART_SendData(USART2,data_spi);
//    }
//}

//void EXTI0_IRQHandler(void){
//    GPIO_ResetBits(GPIOC,GPIO_Pin_13);
//}





void SysTick_SETUP(void){
    SysTick->LOAD = 9;
    SysTick->VAL = 0;
}

void SysTick_Delay_uS (uint16_t ntime){
    
    SysTick->VAL = 0;
    SysTick->CTRL |= SysTick_CTRL_ENABLE;

    for(int i = ntime - 1 ; i > 0 ; i--){
        while(!(SysTick->CTRL & SysTick_CTRL_COUNTFLAG));
    }

    SysTick->CTRL &=~ SysTick_CTRL_ENABLE;
}

void SysTick_Delay_mS (uint16_t ntime){
    
    SysTick->VAL = 0;
    SysTick->CTRL |= SysTick_CTRL_ENABLE;

    for(int i = (ntime * 1000) - 1 ; i > 0 ; i--){
        while(!(SysTick->CTRL & SysTick_CTRL_COUNTFLAG));
    }

    SysTick->CTRL &=~ SysTick_CTRL_ENABLE;
}





void GPIO_SETUP(void){

    GPIO_InitTypeDef GPIO_InitStruct_A1;
    GPIO_InitStruct_A1.GPIO_Pin  = GPIO_Pin_1;
    GPIO_InitStruct_A1.GPIO_Speed = GPIO_Speed_2MHz;
    GPIO_InitStruct_A1.GPIO_Mode = GPIO_Mode_Out_PP;

    GPIO_InitTypeDef GPIO_InitStruct_A2;
    GPIO_InitStruct_A2.GPIO_Pin  = GPIO_Pin_2;
    GPIO_InitStruct_A2.GPIO_Speed = GPIO_Speed_50MHz;
    GPIO_InitStruct_A2.GPIO_Mode = GPIO_Mode_AF_PP;

    GPIO_InitTypeDef GPIO_InitStruct_A3;
    GPIO_InitStruct_A3.GPIO_Pin  = GPIO_Pin_3;
    GPIO_InitStruct_A3.GPIO_Speed = 0;
    GPIO_InitStruct_A3.GPIO_Mode = GPIO_Mode_IPU;

    GPIO_InitTypeDef GPIO_InitStruct_A4;
    GPIO_InitStruct_A4.GPIO_Pin  = GPIO_Pin_4;
    GPIO_InitStruct_A4.GPIO_Speed = GPIO_Speed_50MHz;
    GPIO_InitStruct_A4.GPIO_Mode = GPIO_Mode_Out_OD;
    
    GPIO_InitTypeDef GPIO_InitStruct_A5;
    GPIO_InitStruct_A5.GPIO_Pin  = GPIO_Pin_5;
    GPIO_InitStruct_A5.GPIO_Speed = GPIO_Speed_50MHz;
    GPIO_InitStruct_A5.GPIO_Mode = GPIO_Mode_AF_PP;
    
    GPIO_InitTypeDef GPIO_InitStruct_A6;
    GPIO_InitStruct_A6.GPIO_Pin  = GPIO_Pin_6;
    GPIO_InitStruct_A6.GPIO_Speed = 0;
    GPIO_InitStruct_A6.GPIO_Mode = GPIO_Mode_IPU;
    
    GPIO_InitTypeDef GPIO_InitStruct_A7;
    GPIO_InitStruct_A7.GPIO_Pin  = GPIO_Pin_7;
    GPIO_InitStruct_A7.GPIO_Speed = GPIO_Speed_50MHz;
    GPIO_InitStruct_A7.GPIO_Mode = GPIO_Mode_AF_PP;

    GPIO_InitTypeDef GPIO_InitStruct_B0;
    GPIO_InitStruct_B0.GPIO_Pin  = GPIO_Pin_0;
    GPIO_InitStruct_B0.GPIO_Speed = 0;
    GPIO_InitStruct_B0.GPIO_Mode = GPIO_Mode_IPD;

    GPIO_InitTypeDef GPIO_InitStruct_C13;
    GPIO_InitStruct_C13.GPIO_Pin  = GPIO_Pin_13;
    GPIO_InitStruct_C13.GPIO_Speed = GPIO_Speed_2MHz;
    GPIO_InitStruct_C13.GPIO_Mode = GPIO_Mode_Out_PP;

    RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA,ENABLE);
    RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB,ENABLE);
    RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOC,ENABLE);
    RCC_APB2PeriphClockCmd(RCC_APB2Periph_AFIO,ENABLE);

    GPIO_Init(GPIOA,&GPIO_InitStruct_A1);
    GPIO_Init(GPIOA,&GPIO_InitStruct_A2);
    GPIO_Init(GPIOA,&GPIO_InitStruct_A3);
    GPIO_Init(GPIOA,&GPIO_InitStruct_A4);
    GPIO_Init(GPIOA,&GPIO_InitStruct_A5);
    GPIO_Init(GPIOA,&GPIO_InitStruct_A6);
    GPIO_Init(GPIOA,&GPIO_InitStruct_A7);
    GPIO_Init(GPIOB,&GPIO_InitStruct_B0);
    GPIO_Init(GPIOC,&GPIO_InitStruct_C13);

    //GPIO_EXTILineConfig(GPIO_PortSourceGPIOB,GPIO_PinSource0);

    GPIO_SetBits(GPIOA,GPIO_Pin_1);
    GPIO_SetBits(GPIOA,GPIO_Pin_4);
    GPIO_SetBits(GPIOC,GPIO_Pin_13);

}

//void EXTI_SETUP(void){
//
//    EXTI_InitTypeDef EXTI_InitStruct_B0;
//    EXTI_InitStruct_B0.EXTI_Line = EXTI_Line0;
//    EXTI_InitStruct_B0.EXTI_Mode = EXTI_Mode_Interrupt;
//    EXTI_InitStruct_B0.EXTI_Trigger = EXTI_Trigger_Rising;
//    EXTI_InitStruct_B0.EXTI_LineCmd = ENABLE;
//    
//    EXTI_Init(&EXTI_InitStruct_B0);
//    __disable_irq();
//    NVIC_EnableIRQ(EXTI0_IRQn);
//    __enable_irq();
//
//}

void SPI_SETUP(void){

    SPI_InitTypeDef SPI_InitStruct_1;
    SPI_InitStruct_1.SPI_Direction = SPI_Direction_2Lines_FullDuplex;
    SPI_InitStruct_1.SPI_Mode = SPI_Mode_Master;
    SPI_InitStruct_1.SPI_DataSize = SPI_DataSize_8b;
    SPI_InitStruct_1.SPI_CPOL = SPI_CPOL_Low;
    SPI_InitStruct_1.SPI_CPHA = SPI_CPHA_1Edge;
    SPI_InitStruct_1.SPI_NSS = SPI_NSS_Soft;
    SPI_InitStruct_1.SPI_BaudRatePrescaler = SPI_BaudRatePrescaler_64;
    SPI_InitStruct_1.SPI_FirstBit = SPI_FirstBit_MSB;
    SPI_InitStruct_1.SPI_CRCPolynomial = 7;

    RCC_APB2PeriphClockCmd(RCC_APB2Periph_SPI1,ENABLE);
    SPI_Init(SPI1,&SPI_InitStruct_1);
    SPI_SSOutputCmd(SPI1,ENABLE);
    SPI_Cmd(SPI1,ENABLE);

}


void USART_SETUP(void){

    USART_InitTypeDef USART_InitStruct_2;
    USART_InitStruct_2.USART_BaudRate = 9600;
    USART_InitStruct_2.USART_WordLength = USART_WordLength_8b;
    USART_InitStruct_2.USART_StopBits = USART_StopBits_1;
    USART_InitStruct_2.USART_Parity = USART_Parity_No ;
    USART_InitStruct_2.USART_Mode = USART_Mode_Rx | USART_Mode_Tx;
    USART_InitStruct_2.USART_HardwareFlowControl = USART_HardwareFlowControl_None;

    RCC_APB1PeriphClockCmd(RCC_APB1Periph_USART2,ENABLE);
    USART_Init(USART2,&USART_InitStruct_2);
    //USART_ITConfig(USART2,USART_IT_RXNE,ENABLE);
    //NVIC_EnableIRQ(USART2_IRQn);
    USART_Cmd(USART2,ENABLE);
}
This discussion is locked. Please start a new topic to ask your question.
1 ACCEPTED SOLUTION

Accepted Solutions
mƎALLEm
ST Employee

ST resources are only dedicated to supporting genuine ST products. We are not committed to ensuring that clones/fakes products like your Blue Pill work properly with the firmware we provide.

However, if you face difficulties while using genuine ST products, we’re here to assist you. Please feel free to start a new thread, and our team, along with community members, will be ready to help you with any issues/questions you encounter.

To give better visibility on the answered topics, please click on "Accept as Solution" on the reply which solved your issue or answered your question.

View solution in original post

1 REPLY 1
mƎALLEm
ST Employee

ST resources are only dedicated to supporting genuine ST products. We are not committed to ensuring that clones/fakes products like your Blue Pill work properly with the firmware we provide.

However, if you face difficulties while using genuine ST products, we’re here to assist you. Please feel free to start a new thread, and our team, along with community members, will be ready to help you with any issues/questions you encounter.

To give better visibility on the answered topics, please click on "Accept as Solution" on the reply which solved your issue or answered your question.