AnsweredAssumed Answered

Getting 0xFF and correct value when read received SPI data

Question asked by arlen on Sep 21, 2016
Latest reply on Sep 24, 2016 by barton.michael
Hi, I started to work  on SPI, my intention is to read three gyroscope sensor by SPI bus,
Currently I testing SPI preipheral of my STM32 Discovery Board (STM32F407), this board has the sensor LIS302DL , when i read the "WHo am I" register I received 0x3F but it should be 0x7B, even more when I read continuously this register it got 0x3F,0x3F,
0x3F and more then i receive 0xFF,0xFF and several times and basicaly i receive groups of 0x3F and groups of 0xFF, other register read correctly the default values but it still appear 0xFFs.
I used some code I found in this forum, here below part of the code

#include "stm32f4xx.h"
#include "stdint.h"
#include <stdio.h>
 
 
 
#define Rst_NSS1    GPIO_ResetBits(GPIOE,GPIO_Pin_3)
#define Set_NSS1    GPIO_SetBits(GPIOE,GPIO_Pin_3)
 
/*More codes*/
 
static void SystemClock_Config(void)
{
 
    RCC_DeInit();
    RCC_HSEConfig(RCC_HSE_ON);
    while(RCC_GetFlagStatus(RCC_FLAG_HSERDY) == RESET);
    if(RCC_WaitForHSEStartUp()){
         
        RCC_PLLConfig(RCC_PLLSource_HSE,PLL_M,PLL_N,8,PLL_Q);
        RCC_PLLCmd(ENABLE);
        while(SET != RCC_GetFlagStatus(RCC_FLAG_PLLRDY));
        //Delay(900);
         
        RCC_SYSCLKConfig(RCC_SYSCLKSource_PLLCLK);
        //while (RCC_GetFlagStatus(RCC_FLAG_PLLRDY) == RESET);
         
        RCC_HCLKConfig(RCC_SYSCLK_Div1);    //AHB clock = SYSCLK (168MHz)
        RCC_PCLK1Config(RCC_HCLK_Div4);     //APB1 clock = HCLK/4
        RCC_PCLK2Config(RCC_HCLK_Div16);        //APB2 clock = HCLK/16
         
        RCC_GetClocksFreq(&Clocks);
     
        RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOA | RCC_AHB1Periph_GPIOB | RCC_AHB1Periph_GPIOC | RCC_AHB1Periph_DMA2, ENABLE);
        RCC_APB2PeriphClockCmd(RCC_APB2Periph_ADC1 | RCC_APB2Periph_ADC2 | RCC_APB2Periph_ADC3, ENABLE);
        RCC_APB2PeriphClockCmd(RCC_APB2Periph_USART1, ENABLE);// for USART1    
         
        RCC_APB2PeriphClockCmd(RCC_APB2Periph_SPI1, ENABLE);// for SPI1
    RCC_APB1PeriphClockCmd(RCC_APB1Periph_SPI2, ENABLE);// for SPI2
    RCC_APB1PeriphResetCmd(RCC_APB1Periph_SPI3, ENABLE);// for SPI3
         
         
        //RCC_MCO1Config(RCC_MCO1Source_HSE,RCC_MCO1Div_1);
        //RCC_MCO2Config(RCC_MCO2Source_PLLCLK,RCC_MCO2Div_1);
         
    }
}
 
void Init_SPI1(void){
     
    SPI_InitStructure.SPI_BaudRatePrescaler = SPI_BaudRatePrescaler_16;
    SPI_InitStructure.SPI_CPHA = SPI_CPHA_2Edge;
    SPI_InitStructure.SPI_CPOL = SPI_CPOL_High;
    SPI_InitStructure.SPI_CRCPolynomial = 7;
    SPI_InitStructure.SPI_DataSize = SPI_DataSize_8b;
    SPI_InitStructure.SPI_Direction = SPI_Direction_2Lines_FullDuplex;
    SPI_InitStructure.SPI_FirstBit = SPI_FirstBit_MSB;
    SPI_InitStructure.SPI_Mode = SPI_Mode_Master;
    SPI_InitStructure.SPI_NSS = SPI_NSS_Soft;
     
    SPI_Init(SPI1,&SPI_InitStructure);
    SPI_TIModeCmd(SPI1,DISABLE);
     
    SPI_Cmd(SPI1,ENABLE);
    SPI_CalculateCRC(SPI1, DISABLE);
     
void Init_GPIO(void){
     
        /*                          SPI1                        */
 
        GPIO_PinAFConfig(GPIOA, GPIO_PinSource5, GPIO_AF_SPI1);
        GPIO_PinAFConfig(GPIOA, GPIO_PinSource6, GPIO_AF_SPI1);
        GPIO_PinAFConfig(GPIOA, GPIO_PinSource7, GPIO_AF_SPI1);
         
        GPIO_InitStructure.GPIO_Pin = GPIO_Pin_5 | GPIO_Pin_6 | GPIO_Pin_7; //(SCK|MISO|MOSI)
        GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;
        GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL ;
        GPIO_Init(GPIOA, &GPIO_InitStructure);
         
        GPIO_InitStructure.GPIO_Pin = GPIO_Pin_3; //(NSS : customized)
        GPIO_InitStructure.GPIO_Mode = GPIO_Mode_OUT;
        GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL ;
        GPIO_Init(GPIOE, &GPIO_InitStructure);
 
        }
 
uint32_t Registro,i;
uint16_t tempo;
uint16_t ReadSPI = 0x7F;
uint16_t WriteSPI = 0x7F;
uint16_t Addr = 0x80;
 
#define RdAddrPlus(MemAdd) ((0x80 | MemAdd) | 0x40)
#define RdAddr(MemAdd)  ((0x80 | MemAdd) & 0xBF)
#define WrAddrPlus(MemAdd) ((0x7F & MemAdd) | 0x40)
#define WrAddr(MemAdd) (0x7F & MemAdd & 0xBF)
  
    uint16_t SPI_SendByte(uint16_t DataSpi)
    {
         
        while (SPI_I2S_GetFlagStatus(SPI1, SPI_I2S_FLAG_TXE) == 0){} //wait for transmit buffer to not be empty (ie data loaded in)
        //USART_puts(USART1,"SPI bus Free\n\r");
             
        SPI_I2S_SendData(SPI1,DataSpi);   
        while (SPI_I2S_GetFlagStatus(SPI1, SPI_I2S_FLAG_RXNE) == 0){} //wait for transmit buffer to not be empty (ie data loaded in)
        return(SPI_I2S_ReceiveData(SPI1)); // Clear out any pending RX data
 
    }
 
     
 int main()
    {
 
 
         
        SystemCoreClockUpdate();
        SystemClock_Config();
         
        if (SysTick_Config(SystemCoreClock / 168000)) { /* SysTick 1 msec interrupts  */
            while (1);                                  /* Capture error              */
        }
         
        Init_GPIO();
        Init_USART1();
                 
        USART_ITConfig(USART1, USART_IT_RXNE, ENABLE); // enable the USART1 receive interrupt 
      
        NVIC_InitStructure.NVIC_IRQChannel = USART1_IRQn;       
        NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0;
        NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0;      
        NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;         
        NVIC_Init(&NVIC_InitStructure);        
         
        USART_Cmd(USART1, ENABLE); // enable USART1
        USART_puts(USART1,"INICIO:D \n \r");
         
        NVIC_Configuration();
        Init_SPI1();
 
        /* SPI */
        Rst_NSS1;
        //Set_NSS1;
        Delay(10);
         
        //SPI_SendByte(RdAddr(0x0F));
        while(1){
        SPI_SendByte(RdAddr(0x20));
        tempo = SPI_SendByte(RdAddr(0x00));
             
         
        sprintf(texto,"SPI1 DR: %x  \n\r",tempo);
        USART_puts(USART1,texto);
         
     
    }
     
        return 0;
}

I hope someone can help me with this
thanks in advance

Outcomes