AnsweredAssumed Answered

STM32F0 SPI Read and Write

Question asked by Mena Mourice on Apr 14, 2017
Latest reply on Apr 17, 2017 by Mena Mourice

i'm trying to communicate with NRF24L01 RF module, it seems that spi init. is ok , but when i'm trying to read for example the REG_RF_CH Registry (0x05) it should return the reset value (0x02) but that does not happen !! it return 0x00 for the first time then at the second time i trying to read the same registry it return the correct value!! i assume that the RX FIFO store the value at first time then give me it in the second time !!
my code as below:

 

static void SPI1_Init(void){
RCC->AHBENR |= RCC_AHBENR_GPIOAEN; /* Enable GPIOA clock */
RCC->AHBENR |= RCC_AHBENR_GPIOBEN; /* Enable GPIOB clock */
RCC->APB2ENR |= RCC_APB2ENR_SPI1EN; /* Enable SPI1 clock */

/* Configure PA6 to MISO, PA7 to MOSI, PB3 to SCK */
GPIOA->AFR[0] &= ~((15ul << 4* 6) | (15ul << 4* 7));
GPIOA->AFR[0] |= (( 0ul << 4* 6) | ( 0ul << 4* 7));
GPIOB->AFR[0] &= ~(15ul << 4* 3);
GPIOB->AFR[0] |= ( 0ul << 4* 3);

GPIOA->MODER &= ~(( 3ul << 2* 6) | ( 3ul << 2* 7));
GPIOA->MODER |= (( 2ul << 2* 6) | ( 2ul << 2* 7));
GPIOB->MODER &= ~( 3ul << 2* 3);
GPIOB->MODER |= ( 2ul << 2* 3);

SPI1->CR1 = 0x00000000;    /* Reset SPI1 CR1 Registry*/
SPI1->CR1 = (( 0ul << 0) |       /* CPHA=0 */
( 0ul << 1) |                               /* CPOL=0 */
( 1ul << 2) |                               /* MSTR=1 */
( 4ul << 3) |                               /* BR (fPCLK/32) ~= 1.5 Mbit/sec */
( 0ul << 7) |                               /* LSBFIRST */
( 0ul << 8) |                               /* SSI */
( 1ul << 9));                               /* SSM */
SPI1->CR2 = 0x00000000;      /* Reset SPI1 CR2 Registry*/
SPI1->CR2 |= (SPI_CR2_NSSP | SPI_CR2_DS_0 | SPI_CR2_DS_1 | SPI_CR2_DS_2 | SPI_CR2_FRXTH);
SPI1->CRCPR = 0x0007;
SPI1->CR1 |= SPI_CR1_SPE; /* Enable SPI1 */
}

uint8_t SPI1_WriteRead_byte (uint8_t wr) {
uint8_t data=0;
while ((SPI1->SR & SPI_SR_TXE) != SPI_SR_TXE);
*(uint8_t *)&(SPI1->DR) = wr;
data = SPI1->DR;
return data;
}

Outcomes