AnsweredAssumed Answered

stm32f3 gyroscope

Question asked by saad.anas on Apr 24, 2014
Hi, I am trying to get access to the gyroscope included in the STM32F3 using the spi protocole. When I write data in the configuration register, the gyro seems not responding (after this instruction :      while(!SPI_I2S_GetFlagStatus(SPI1, SPI_I2S_FLAG_RXNE));)
please can you help me:
/* Includes */
#include <stddef.h>
#include "stm32f30x.h"
#include "stm32f30x_conf.h"






void mySPI_Init(void);
int8_t mySPI_GetData(uint8_t);
void mySPI_SendData(uint8_t, uint8_t);


uint32_t timerFlag=0;


int main(void)
{








       uint32_t ii;


       SysTick_Config(72000);


       SystemCoreClockUpdate();
       ii = SystemCoreClock;


       ii = 0;


       RCC_AHBPeriphClockCmd (RCC_AHBPeriph_GPIOE , ENABLE);


       GPIO_InitTypeDef GPIO_InitStructure;


       GPIO_InitStructure.GPIO_Pin = GPIO_Pin_All;
       GPIO_InitStructure.GPIO_Mode = GPIO_Mode_OUT;
       GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
       GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL;
       GPIO_InitStructure.GPIO_Speed = GPIO_Speed_2MHz;
       GPIO_Init(GPIOE, &GPIO_InitStructure);


     int8_t x=0;
     int8_t y = 0;
     int8_t z =0;
     mySPI_Init();


       GPIO_ResetBits(GPIOE, GPIO_Pin_All);


     mySPI_SendData(0x20, 0x0f); //LIS302D Config
       GPIO_SetBits(GPIOE, GPIO_Pin_All);




    while(1)
    {


        if (1==timerFlag)
        {
          timerFlag = 0;
          ii++;
        }


         x = mySPI_GetData(0x29);
         y = mySPI_GetData(0x2A);
         z = mySPI_GetData(0x2C);


         GPIO_SetBits(GPIOE, GPIO_Pin_All);
         /*if(x>0){
              GPIO_SetBits(GPIOE, GPIO_Pin_9);
         }
         if(x<0){
              GPIO_SetBits(GPIOE, GPIO_Pin_13);
         }*/
    }


}












int8_t mySPI_GetData(uint8_t adress){
     int16_t var;


     GPIO_ResetBits(GPIOE, GPIO_Pin_3);


     adress = 0x80 | adress;


     while(!SPI_I2S_GetFlagStatus(SPI1, SPI_I2S_FLAG_TXE));  //transmit buffer empty?
     SPI_SendData8(SPI1, adress);
     while(!SPI_I2S_GetFlagStatus(SPI1, SPI_I2S_FLAG_RXNE)); //data received?
     SPI_ReceiveData8(SPI1);     //Clear RXNE bit


     while(!SPI_I2S_GetFlagStatus(SPI1, SPI_I2S_FLAG_TXE));  //transmit buffer empty?
     SPI_SendData8(SPI1, 0x00);     //Dummy byte to generate clock
     while(!SPI_I2S_GetFlagStatus(SPI1, SPI_I2S_FLAG_RXNE)); //data received?
     GPIO_SetBits(GPIOE, GPIO_Pin_3);


     var =  SPI_ReceiveData8(SPI1); //return reveiced data
     return var;
}


void mySPI_SendData(uint8_t adress, uint8_t data){


     GPIO_ResetBits(GPIOE, GPIO_Pin_3);


     while(!SPI_I2S_GetFlagStatus(SPI1, SPI_I2S_FLAG_TXE));  //transmit buffer empty?




     SPI_SendData8(SPI1, adress);


     while(!SPI_I2S_GetFlagStatus(SPI1, SPI_I2S_FLAG_RXNE)); //data received?
     SPI_ReceiveData8(SPI1);


     while(!SPI_I2S_GetFlagStatus(SPI1, SPI_I2S_FLAG_TXE));  //transmit buffer empty?
     SPI_SendData8(SPI1, data);
     while(!SPI_I2S_GetFlagStatus(SPI1, SPI_I2S_FLAG_RXNE)); //data received?
     SPI_ReceiveData8(SPI1);


     GPIO_SetBits(GPIOE, GPIO_Pin_3);
     GPIO_SetBits(GPIOE, GPIO_Pin_9);
}




void mySPI_Init(void){




     RCC_APB2PeriphClockCmd(RCC_APB2Periph_SPI1, ENABLE);


     SPI_InitTypeDef SPI_InitTypeDefStruct;


     SPI_InitTypeDefStruct.SPI_BaudRatePrescaler = SPI_BaudRatePrescaler_2;
     SPI_InitTypeDefStruct.SPI_Direction = SPI_Direction_2Lines_FullDuplex;
     SPI_InitTypeDefStruct.SPI_Mode = SPI_Mode_Master;
     SPI_InitTypeDefStruct.SPI_DataSize = SPI_DataSize_8b;
     SPI_InitTypeDefStruct.SPI_NSS = SPI_NSS_Soft;
     SPI_InitTypeDefStruct.SPI_FirstBit = SPI_FirstBit_MSB;
     SPI_InitTypeDefStruct.SPI_CPOL = SPI_CPOL_High;
     SPI_InitTypeDefStruct.SPI_CPHA = SPI_CPHA_2Edge;


     SPI_Init(SPI1, &SPI_InitTypeDefStruct);




     RCC_AHBPeriphClockCmd(RCC_AHBPeriph_GPIOA  | RCC_AHBPeriph_GPIOE  , ENABLE);


     GPIO_InitTypeDef GPIO_InitTypeDefStruct;


     GPIO_InitTypeDefStruct.GPIO_Pin = GPIO_Pin_5 | GPIO_Pin_7 | GPIO_Pin_6;
     GPIO_InitTypeDefStruct.GPIO_Mode = GPIO_Mode_AF;
     GPIO_InitTypeDefStruct.GPIO_Speed = GPIO_Speed_50MHz;
     GPIO_InitTypeDefStruct.GPIO_OType = GPIO_OType_PP;
     GPIO_InitTypeDefStruct.GPIO_PuPd = GPIO_PuPd_NOPULL;
     GPIO_Init(GPIOA, &GPIO_InitTypeDefStruct);


     GPIO_InitTypeDefStruct.GPIO_Pin = GPIO_Pin_3;
     GPIO_InitTypeDefStruct.GPIO_Mode = GPIO_Mode_OUT;
     GPIO_InitTypeDefStruct.GPIO_Speed = GPIO_Speed_50MHz;
     GPIO_InitTypeDefStruct.GPIO_PuPd = GPIO_PuPd_UP;
     GPIO_InitTypeDefStruct.GPIO_OType = GPIO_OType_PP;
     GPIO_Init(GPIOE, &GPIO_InitTypeDefStruct);


     GPIO_PinAFConfig(GPIOA, GPIO_PinSource5, GPIO_AF_5);
     GPIO_PinAFConfig(GPIOA, GPIO_PinSource6, GPIO_AF_5);
     GPIO_PinAFConfig(GPIOA, GPIO_PinSource7, GPIO_AF_5);


     GPIO_SetBits(GPIOE, GPIO_Pin_3);




     SPI_Cmd(SPI1, ENABLE);


}














































#ifdef  USE_FULL_ASSERT


/**
  * @brief  Reports the name of the source file and the source line number
  *   where the assert_param error has occurred.
  * @param  file: pointer to the source file name
  * @param  line: assert_param error line source number
  * @retval None
  */
void assert_failed(uint8_t* file, uint32_t line)
{
  /* User can add his own implementation to report the file name and line number,
     ex: printf("Wrong parameters value: file %s on line %d\r\n", file, line) */


  /* Infinite loop */
  while (1)
  {
  }
}
#endif


/*
 * Minimal __assert_func used by the assert() macro
 * */
void __assert_func(const char *file, int line, const char *func, const char *failedexpr)
{
  while(1)
  {}
}


/*
 * Minimal __assert() uses __assert__func()
 * */
void __assert(const char *file, int line, const char *failedexpr)
{
   __assert_func (file, line, NULL, failedexpr);
}


#ifdef USE_SEE
#ifndef USE_DEFAULT_TIMEOUT_CALLBACK
/**
  * @brief  Basic management of the timeout situation.
  * @param  None.
  * @retval sEE_FAIL.
  */
uint32_t sEE_TIMEOUT_UserCallback(void)
{
  /* Return with error code */
  return sEE_FAIL;
}
#endif
#endif /* USE_SEE */




/**
  * @brief  Basic management of the timeout situation.
  * @param  None.
  * @retval sEE_FAIL.
  */
void EVAL_AUDIO_TransferComplete_CallBack(uint32_t pBuffer, uint32_t Size)


{
  /* TODO, implement your code here */
  return;
}


Outcomes