Martin Johnson / STM32F3-Discovery

Dependents:   Space_Invaders_Demo neopixels gpio_test_stm32f3_discovery gpio_test_systimer ... more

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers stm32f3_discovery_lsm303dlhc.c Source File

stm32f3_discovery_lsm303dlhc.c

Go to the documentation of this file.
00001 /**
00002   ******************************************************************************
00003   * @file    stm32f3_discovery_lsm303dlhc.c
00004   * @author  MCD Application Team
00005   * @version V1.1.0
00006   * @date    20-September-2012
00007   * @brief   This file provides a set of functions needed to manage the lsm303dlhc
00008   *          MEMS accelerometer available on STM32F3-Discovery Kit.
00009   ******************************************************************************
00010   * @attention
00011   *
00012   * <h2><center>&copy; COPYRIGHT 2012 STMicroelectronics</center></h2>
00013   *
00014   * Licensed under MCD-ST Liberty SW License Agreement V2, (the "License");
00015   * You may not use this file except in compliance with the License.
00016   * You may obtain a copy of the License at:
00017   *
00018   *        http://www.st.com/software_license_agreement_liberty_v2
00019   *
00020   * Unless required by applicable law or agreed to in writing, software 
00021   * distributed under the License is distributed on an "AS IS" BASIS, 
00022   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
00023   * See the License for the specific language governing permissions and
00024   * limitations under the License.
00025   *
00026   ******************************************************************************
00027   */
00028 /* Includes ------------------------------------------------------------------*/
00029 #include "stm32f3_discovery_lsm303dlhc.h"
00030 
00031 /** @addtogroup Utilities
00032   * @{
00033   */ 
00034 
00035 /** @addtogroup STM32F3_DISCOVERY
00036   * @{
00037   */ 
00038 
00039 /** @addtogroup STM32F3_DISCOVERY_LSM303DLHC
00040   * @{
00041   */
00042 
00043 
00044 /** @defgroup STM32F3_DISCOVERY_LSM303DLHC_Private_TypesDefinitions
00045   * @{
00046   */
00047 
00048 /**
00049   * @}
00050   */
00051 
00052 /** @defgroup STM32F3_DISCOVERY_LSM303DLHC_Private_Defines
00053   * @{
00054   */
00055 
00056 /**
00057   * @}
00058   */
00059 
00060 /** @defgroup STM32F3_DISCOVERY_LSM303DLHC_Private_Macros
00061   * @{
00062   */
00063 
00064 /**
00065   * @}
00066   */ 
00067   
00068 /** @defgroup STM32F3_DISCOVERY_LSM303DLHC_Private_Variables
00069   * @{
00070   */ 
00071 __IO uint32_t  LSM303DLHC_Timeout = LSM303DLHC_LONG_TIMEOUT; 
00072 /**
00073   * @}
00074   */
00075 
00076 /** @defgroup STM32F3_DISCOVERY_LSM303DLHC_Private_FunctionPrototypes
00077   * @{
00078   */
00079 static void LSM303DLHC_LowLevel_Init(void);
00080 /**
00081   * @}
00082   */
00083 
00084 /** @defgroup STM32F3_DISCOVERY_LSM303DLHC_Private_Functions
00085   * @{
00086   */
00087 
00088 /**
00089   * @brief  Set LSM303DLHC Initialization.
00090   * @param  LSM303DLHC_InitStruct: pointer to a LSM303DLHC_InitTypeDef structure 
00091   *         that contains the configuration setting for the LSM303DLHC.
00092   * @retval None
00093   */
00094 void LSM303DLHC_AccInit(LSM303DLHCAcc_InitTypeDef *LSM303DLHC_InitStruct)
00095 {  
00096   uint8_t ctrl1 = 0x00, ctrl4 = 0x00;
00097   
00098   /* Configure the low level interface ---------------------------------------*/
00099   LSM303DLHC_LowLevel_Init();
00100   
00101   /* Configure MEMS: data rate, power mode, full scale and axes */
00102   ctrl1 |= (uint8_t) (LSM303DLHC_InitStruct->Power_Mode | LSM303DLHC_InitStruct->AccOutput_DataRate | \
00103                     LSM303DLHC_InitStruct->Axes_Enable);
00104   
00105   ctrl4 |= (uint8_t) (LSM303DLHC_InitStruct->BlockData_Update | LSM303DLHC_InitStruct->Endianness | \
00106                     LSM303DLHC_InitStruct->AccFull_Scale|LSM303DLHC_InitStruct->High_Resolution);
00107                     
00108   /* Write value to ACC MEMS CTRL_REG1 regsister */
00109   LSM303DLHC_Write(ACC_I2C_ADDRESS, LSM303DLHC_CTRL_REG1_A, &ctrl1);
00110   
00111   /* Write value to ACC MEMS CTRL_REG4 regsister */
00112   LSM303DLHC_Write(ACC_I2C_ADDRESS, LSM303DLHC_CTRL_REG4_A, &ctrl4);
00113 }
00114 
00115 /**
00116   * @brief  Reboot memory content of LSM303DLHC
00117   * @param  None
00118   * @retval None
00119   */
00120 void LSM303DLHC_AccRebootCmd(void)
00121 {
00122   uint8_t tmpreg;
00123   
00124   /* Read CTRL_REG5 register */
00125   LSM303DLHC_Read(ACC_I2C_ADDRESS, LSM303DLHC_CTRL_REG5_A, &tmpreg, 1);
00126   
00127   /* Enable or Disable the reboot memory */
00128   tmpreg |= LSM303DLHC_BOOT_REBOOTMEMORY;
00129   
00130   /* Write value to ACC MEMS CTRL_REG5 regsister */
00131   LSM303DLHC_Write(ACC_I2C_ADDRESS, LSM303DLHC_CTRL_REG5_A, &tmpreg);
00132 }
00133 
00134 /**
00135   * @brief  Set High Pass Filter Modality
00136   * @param  LSM303DLHC_FilterStruct: pointer to a LSM303DLHC_FilterConfigTypeDef structure 
00137   *         that contains the configuration setting for the LSM303DLHC.        
00138   * @retval None
00139   */
00140 void LSM303DLHC_AccFilterConfig(LSM303DLHCAcc_FilterConfigTypeDef *LSM303DLHC_FilterStruct) 
00141 {
00142   uint8_t tmpreg;
00143   
00144   /* Read CTRL_REG2 register */
00145   LSM303DLHC_Read(ACC_I2C_ADDRESS, LSM303DLHC_CTRL_REG2_A, &tmpreg, 1);
00146   
00147   tmpreg &= 0x0C;
00148   
00149   /* Configure MEMS: mode, cutoff frquency, Filter status, Click, AOI1 and AOI2 */
00150   tmpreg |= (uint8_t) (LSM303DLHC_FilterStruct->HighPassFilter_Mode_Selection |\
00151                       LSM303DLHC_FilterStruct->HighPassFilter_CutOff_Frequency|\
00152                       LSM303DLHC_FilterStruct->HighPassFilter_AOI1|\
00153                       LSM303DLHC_FilterStruct->HighPassFilter_AOI2);                             
00154   
00155   /* Write value to ACC MEMS CTRL_REG2 regsister */
00156   LSM303DLHC_Write(ACC_I2C_ADDRESS, LSM303DLHC_CTRL_REG2_A, &tmpreg);
00157 }
00158 
00159 /**
00160   * @brief  Enable or Disable High Pass Filter
00161   * @param  HighPassFilterState: new state of the High Pass Filter feature.
00162   *      This parameter can be: 
00163   *         @arg: LSM303DLHC_HighPassFilter_DISABLE 
00164   *         @arg: LSM303DLHC_HighPassFilter_ENABLE          
00165   * @retval None
00166   */
00167 void LSM303DLHC_AccFilterCmd(uint8_t HighPassFilterState)
00168  {
00169   uint8_t tmpreg;
00170   
00171   /* Read CTRL_REG2 register */
00172   LSM303DLHC_Read(ACC_I2C_ADDRESS, LSM303DLHC_CTRL_REG2_A, &tmpreg, 1);
00173                   
00174   tmpreg &= 0xF7;
00175 
00176   tmpreg |= HighPassFilterState;
00177 
00178   /* Write value to ACC MEMS CTRL_REG2 regsister */
00179   LSM303DLHC_Write(ACC_I2C_ADDRESS, LSM303DLHC_CTRL_REG2_A, &tmpreg);
00180 }
00181 
00182 /**
00183   * @brief  Enable or Disable High Pass Filter on CLick
00184   * @param  HighPassFilterState: new state of the High Pass Filter feature.
00185   *      This parameter can be: 
00186   *         @arg: LSM303DLHC_HighPassFilter_DISABLE 
00187   *         @arg: LSM303DLHC_HighPassFilter_ENABLE          
00188   * @retval None
00189   */
00190 void LSM303DLHC_AccFilterClickCmd(uint8_t HighPassFilterClickState)
00191  {
00192   uint8_t tmpreg;
00193   
00194   /* Read CTRL_REG2 register */
00195   LSM303DLHC_Read(ACC_I2C_ADDRESS, LSM303DLHC_CTRL_REG2_A, &tmpreg, 1);
00196                   
00197   tmpreg &= 0xFB;
00198 
00199   tmpreg |= HighPassFilterClickState;
00200 
00201   /* Write value to ACC MEMS CTRL_REG2 regsister */
00202   LSM303DLHC_Write(ACC_I2C_ADDRESS, LSM303DLHC_CTRL_REG2_A, &tmpreg);
00203 }
00204 
00205 /**
00206   * @brief Set LSM303DLHC Interrupt1 configuration
00207   * @param  LSM303DLHC_IT: specifies the LSM303DLHC interrupt source to be enabled.
00208   *           This parameter can be any combination of the following values: 
00209   *         @arg   LSM303DLHC_IT1_CLICK
00210   *         @arg   LSM303DLHC_IT1_AOI1
00211   *         @arg   LSM303DLHC_IT1_AOI2
00212   *         @arg   LSM303DLHC_IT1_DRY1
00213   *         @arg   LSM303DLHC_IT1_WTM
00214   *         @arg   LSM303DLHC_IT1_OVERRUN              
00215   * @param  NewState: new state of the selected LSM303DLHC interrupts.
00216   *          This parameter can be: ENABLE or DISABLE.
00217   * @retval None
00218   */
00219 void LSM303DLHC_AccIT1Config(uint8_t LSM303DLHC_IT, FunctionalState NewState)
00220 {
00221   uint8_t tmpval = 0x00;
00222   
00223   /* Read CTRL_REG3 register */
00224   LSM303DLHC_Read(ACC_I2C_ADDRESS, LSM303DLHC_CTRL_REG3_A, &tmpval, 1 );
00225     
00226   tmpval &= ~LSM303DLHC_IT;
00227   
00228   if (NewState != DISABLE)
00229   {
00230     tmpval |= LSM303DLHC_IT;
00231   }
00232   else
00233   {
00234     /* Disable the selected interrupt */
00235     tmpval =~ LSM303DLHC_IT;
00236   }
00237       
00238   /* Write value to MEMS CTRL_REG3 register */
00239   LSM303DLHC_Write(ACC_I2C_ADDRESS, LSM303DLHC_CTRL_REG3_A, &tmpval);  
00240 }
00241 
00242 /**
00243   * @brief Set LSM303DLHC Interrupt2 configuration
00244   * @param  LSM303DLHC_IT: specifies the LSM303DLHC interrupt source to be enabled.
00245   *           This parameter can be any combination of the following values: 
00246   *         @arg   LSM303DLHC_IT2_CLICK2
00247   *         @arg   LSM303DLHC_IT2_INT1
00248   *         @arg   LSM303DLHC_IT2_INT2
00249   *         @arg   LSM303DLHC_IT2_BOOT
00250   *         @arg   LSM303DLHC_IT2_ACT
00251   *         @arg   LSM303DLHC_IT2_HLACTIVE              
00252   * @param  NewState: new state of the selected LSM303DLHC interrupts.
00253   *          This parameter can be: ENABLE or DISABLE.
00254   * @retval None
00255   */
00256 void LSM303DLHC_AccIT2Config(uint8_t LSM303DLHC_IT, FunctionalState NewState)
00257 {
00258   uint8_t tmpval = 0x00;
00259   
00260   /* Read CTRL_REG3 register */
00261   LSM303DLHC_Read(ACC_I2C_ADDRESS, LSM303DLHC_CTRL_REG6_A, &tmpval, 1);
00262     
00263   tmpval &= ~LSM303DLHC_IT;
00264   
00265   if (NewState != DISABLE)
00266   {
00267     tmpval |= LSM303DLHC_IT;
00268   }
00269   else
00270   {
00271     /* Disable the selected interrupt */
00272     tmpval =~ LSM303DLHC_IT;
00273   }
00274       
00275   /* Write value to MEMS CTRL_REG3 register */
00276   LSM303DLHC_Write(ACC_I2C_ADDRESS, LSM303DLHC_CTRL_REG6_A, &tmpval);  
00277 }
00278 
00279 /**
00280   * @brief  INT1 interrupt config
00281   * @param  ITCombination: Or or And combination
00282   *         ITAxes: axes to be enabled 
00283   *         NewState: Enable or Disable    
00284   * @retval None
00285   */
00286 void LSM303DLHC_AccINT1InterruptConfig(uint8_t ITCombination, uint8_t ITAxes, FunctionalState NewState )
00287 {  
00288   uint8_t tmpval = ITCombination;
00289   
00290   /* Read INT1_CFR register */
00291   LSM303DLHC_Read(ACC_I2C_ADDRESS, LSM303DLHC_INT1_CFG_A, &tmpval, 1);
00292   
00293   if (NewState != DISABLE)
00294   {
00295     tmpval |= ITAxes;
00296   }
00297   else
00298   {
00299     /* Disable the selected interrupt */
00300     tmpval =(~ITAxes)|ITCombination;
00301   }
00302       
00303   /* Write value to MEMS INT1_CFR register */
00304   LSM303DLHC_Write(ACC_I2C_ADDRESS, LSM303DLHC_INT1_CFG_A, &tmpval);  
00305 }
00306 
00307 /**
00308   * @brief  INT1 interrupt config
00309   * @param  ITCombination: Or or And combination
00310   *         ITAxes: axes to be enabled 
00311   *         NewState: Enable or Disable    
00312   * @retval None
00313   */
00314 void LSM303DLHC_AccINT2InterruptConfig(uint8_t ITCombination, uint8_t ITAxes, FunctionalState NewState )
00315 {  
00316   uint8_t tmpval = ITCombination;
00317   
00318   /* Read INT2_CFR register */
00319   LSM303DLHC_Read(ACC_I2C_ADDRESS, LSM303DLHC_INT2_CFG_A, &tmpval, 1);
00320   
00321   if (NewState != DISABLE)
00322   {
00323     tmpval |= ITAxes;
00324   }
00325   else
00326   {
00327     /* Disable the selected interrupt */
00328     tmpval =(~ITAxes)|ITCombination;
00329   }
00330       
00331   /* Write value to MEMS INT2_CFR register */
00332   LSM303DLHC_Write(ACC_I2C_ADDRESS, LSM303DLHC_INT2_CFG_A, &tmpval);  
00333 }
00334 
00335 /**
00336   * @brief  INT1 interrupt config
00337   * @param  ITCombination: Or or And combination
00338   *         ITAxes: axes to be enabled 
00339   *         NewState: Enable or Disable    
00340   * @retval None
00341   */
00342 void LSM303DLHC_AccClickITConfig(uint8_t ITClick, FunctionalState NewState)
00343 {  
00344   uint8_t tmpval;
00345   
00346   /* Read CLICK_CFR register */
00347   LSM303DLHC_Read(ACC_I2C_ADDRESS, LSM303DLHC_CLICK_CFG_A, &tmpval, 1);
00348   
00349   if (NewState != DISABLE)
00350   {
00351     tmpval |= ITClick;
00352   }
00353   else
00354   {
00355     /* Disable the selected interrupt */
00356     tmpval =~ITClick;
00357   }
00358       
00359   /* Write value to MEMS CLICK_CFR register */
00360   LSM303DLHC_Write(ACC_I2C_ADDRESS, LSM303DLHC_CLICK_CFG_A, &tmpval);  
00361 }
00362 
00363 /**
00364   * @brief  Get status for Acc LSM303DLHC data
00365   * @param  None         
00366   * @retval Data status in a LSM303DLHC Data register
00367   */
00368 uint8_t LSM303DLHC_AccGetDataStatus(void)
00369 {
00370   uint8_t tmpreg;
00371   
00372   /* Read Mag STATUS register */
00373   LSM303DLHC_Read(ACC_I2C_ADDRESS, LSM303DLHC_STATUS_REG_A, &tmpreg, 1);
00374                   
00375   return tmpreg;
00376 }
00377 
00378 /**
00379   * @brief  Set LSM303DLHC Mag Initialization.
00380   * @param  LSM303DLHC_InitStruct: pointer to a LSM303DLHC_MagInitTypeDef structure 
00381   *         that contains the configuration setting for the LSM303DLHC.
00382   * @retval None
00383   */
00384 void LSM303DLHC_MagInit(LSM303DLHCMag_InitTypeDef *LSM303DLHC_InitStruct)
00385 {  
00386   uint8_t cra_regm = 0x00, crb_regm = 0x00, mr_regm = 0x00;
00387   
00388   /* Configure the low level interface ---------------------------------------*/
00389   LSM303DLHC_LowLevel_Init();
00390   
00391   /* Configure MEMS: temp and Data rate */
00392   cra_regm |= (uint8_t) (LSM303DLHC_InitStruct->Temperature_Sensor | LSM303DLHC_InitStruct->MagOutput_DataRate);
00393     
00394   /* Configure MEMS: full Scale */
00395   crb_regm |= (uint8_t) (LSM303DLHC_InitStruct->MagFull_Scale);
00396       
00397   /* Configure MEMS: working mode */
00398   mr_regm |= (uint8_t) (LSM303DLHC_InitStruct->Working_Mode);
00399                     
00400   /* Write value to Mag MEMS CRA_REG regsister */
00401   LSM303DLHC_Write(MAG_I2C_ADDRESS, LSM303DLHC_CRA_REG_M, &cra_regm);
00402   
00403   /* Write value to Mag MEMS CRB_REG regsister */
00404   LSM303DLHC_Write(MAG_I2C_ADDRESS, LSM303DLHC_CRB_REG_M, &crb_regm);
00405 
00406   /* Write value to Mag MEMS MR_REG regsister */
00407   LSM303DLHC_Write(MAG_I2C_ADDRESS, LSM303DLHC_MR_REG_M, &mr_regm);
00408 }
00409 
00410 /**
00411   * @brief  Get status for Mag LSM303DLHC data
00412   * @param  None         
00413   * @retval Data status in a LSM303DLHC Data register
00414   */
00415 uint8_t LSM303DLHC_MagGetDataStatus(void)
00416 {
00417   uint8_t tmpreg;
00418   
00419   /* Read Mag STATUS register */
00420   LSM303DLHC_Read(MAG_I2C_ADDRESS, LSM303DLHC_SR_REG_M, &tmpreg, 1 );
00421                   
00422   return tmpreg;
00423 }
00424 
00425 /**
00426   * @brief  Writes one byte to the LSM303DLHC.
00427   * @param  DeviceAddr : specifies the slave address to be programmed.
00428   * @param  RegAddr : specifies the LSM303DLHC register to be written.
00429   * @param  pBuffer : pointer to the buffer  containing the data to be written to the LSM303DLH.
00430   * @retval LSM303DLHC Status
00431   */
00432 uint16_t LSM303DLHC_Write(uint8_t DeviceAddr, uint8_t RegAddr, uint8_t* pBuffer)
00433 {  
00434   /* Test on BUSY Flag */
00435   LSM303DLHC_Timeout = LSM303DLHC_LONG_TIMEOUT;
00436   while(I2C_GetFlagStatus(LSM303DLHC_I2C, I2C_ISR_BUSY) != RESET)
00437   {
00438     if((LSM303DLHC_Timeout--) == 0) return LSM303DLHC_TIMEOUT_UserCallback();
00439   }
00440   
00441   /* Configure slave address, nbytes, reload, end mode and start or stop generation */
00442   I2C_TransferHandling(LSM303DLHC_I2C, DeviceAddr, 1, I2C_Reload_Mode, I2C_Generate_Start_Write);
00443   
00444   /* Wait until TXIS flag is set */
00445   LSM303DLHC_Timeout = LSM303DLHC_LONG_TIMEOUT;  
00446   while(I2C_GetFlagStatus(LSM303DLHC_I2C, I2C_ISR_TXIS) == RESET)   
00447   {
00448     if((LSM303DLHC_Timeout--) == 0) return LSM303DLHC_TIMEOUT_UserCallback();
00449   }
00450   
00451   /* Send Register address */
00452   I2C_SendData(LSM303DLHC_I2C, (uint8_t) RegAddr);
00453   
00454   /* Wait until TCR flag is set */
00455   LSM303DLHC_Timeout = LSM303DLHC_LONG_TIMEOUT;
00456   while(I2C_GetFlagStatus(LSM303DLHC_I2C, I2C_ISR_TCR) == RESET)
00457   {
00458     if((LSM303DLHC_Timeout--) == 0) return LSM303DLHC_TIMEOUT_UserCallback();
00459   }
00460   
00461   /* Configure slave address, nbytes, reload, end mode and start or stop generation */
00462   I2C_TransferHandling(LSM303DLHC_I2C, DeviceAddr, 1, I2C_AutoEnd_Mode, I2C_No_StartStop);
00463        
00464   /* Wait until TXIS flag is set */
00465   LSM303DLHC_Timeout = LSM303DLHC_LONG_TIMEOUT;
00466   while(I2C_GetFlagStatus(LSM303DLHC_I2C, I2C_ISR_TXIS) == RESET)
00467   {
00468     if((LSM303DLHC_Timeout--) == 0) return LSM303DLHC_TIMEOUT_UserCallback();
00469   }  
00470     
00471   /* Write data to TXDR */
00472   I2C_SendData(LSM303DLHC_I2C, *pBuffer);
00473       
00474   /* Wait until STOPF flag is set */
00475   LSM303DLHC_Timeout = LSM303DLHC_LONG_TIMEOUT;
00476   while(I2C_GetFlagStatus(LSM303DLHC_I2C, I2C_ISR_STOPF) == RESET)
00477   {
00478     if((LSM303DLHC_Timeout--) == 0) return LSM303DLHC_TIMEOUT_UserCallback();
00479   }   
00480   
00481   /* Clear STOPF flag */
00482   I2C_ClearFlag(LSM303DLHC_I2C, I2C_ICR_STOPCF);
00483   
00484   return LSM303DLHC_OK;
00485 }
00486 
00487 /**
00488   * @brief  Reads a block of data from the LSM303DLHC.
00489   * @param  DeviceAddr : specifies the slave address to be programmed(ACC_I2C_ADDRESS or MAG_I2C_ADDRESS).
00490   * @param  RegAddr : specifies the LSM303DLHC internal address register to read from.
00491   * @param  pBuffer : pointer to the buffer that receives the data read from the LSM303DLH.
00492   * @param  NumByteToRead : number of bytes to read from the LSM303DLH ( NumByteToRead >1  only for the Mgnetometer readinf).
00493   * @retval LSM303DLHC register value
00494   */
00495 uint16_t LSM303DLHC_Read(uint8_t DeviceAddr, uint8_t RegAddr, uint8_t* pBuffer, uint16_t NumByteToRead)
00496 {    
00497   /* Test on BUSY Flag */
00498   LSM303DLHC_Timeout = LSM303DLHC_LONG_TIMEOUT;
00499   while(I2C_GetFlagStatus(LSM303DLHC_I2C, I2C_ISR_BUSY) != RESET)
00500   {
00501     if((LSM303DLHC_Timeout--) == 0) return LSM303DLHC_TIMEOUT_UserCallback();
00502   }
00503   
00504   /* Configure slave address, nbytes, reload, end mode and start or stop generation */
00505   I2C_TransferHandling(LSM303DLHC_I2C, DeviceAddr, 1, I2C_SoftEnd_Mode, I2C_Generate_Start_Write);
00506   
00507   /* Wait until TXIS flag is set */
00508   LSM303DLHC_Timeout = LSM303DLHC_LONG_TIMEOUT;
00509   while(I2C_GetFlagStatus(LSM303DLHC_I2C, I2C_ISR_TXIS) == RESET)
00510   {
00511     if((LSM303DLHC_Timeout--) == 0) return LSM303DLHC_TIMEOUT_UserCallback();
00512   }
00513   
00514   if(NumByteToRead>1)
00515       RegAddr |= 0x80;
00516 
00517   
00518   /* Send Register address */
00519   I2C_SendData(LSM303DLHC_I2C, (uint8_t)RegAddr);
00520   
00521   /* Wait until TC flag is set */
00522   LSM303DLHC_Timeout = LSM303DLHC_LONG_TIMEOUT;
00523   while(I2C_GetFlagStatus(LSM303DLHC_I2C, I2C_ISR_TC) == RESET)
00524   {
00525     if((LSM303DLHC_Timeout--) == 0) return LSM303DLHC_TIMEOUT_UserCallback();
00526   }  
00527   
00528   /* Configure slave address, nbytes, reload, end mode and start or stop generation */
00529   I2C_TransferHandling(LSM303DLHC_I2C, DeviceAddr, NumByteToRead, I2C_AutoEnd_Mode, I2C_Generate_Start_Read);
00530   
00531   /* Wait until all data are received */
00532   while (NumByteToRead)
00533   {   
00534     /* Wait until RXNE flag is set */
00535     LSM303DLHC_Timeout = LSM303DLHC_LONG_TIMEOUT;
00536     while(I2C_GetFlagStatus(LSM303DLHC_I2C, I2C_ISR_RXNE) == RESET)    
00537     {
00538       if((LSM303DLHC_Timeout--) == 0) return LSM303DLHC_TIMEOUT_UserCallback();
00539     }
00540     
00541     /* Read data from RXDR */
00542     *pBuffer = I2C_ReceiveData(LSM303DLHC_I2C);
00543     /* Point to the next location where the byte read will be saved */
00544     pBuffer++;
00545     
00546     /* Decrement the read bytes counter */
00547     NumByteToRead--;
00548   } 
00549   
00550   /* Wait until STOPF flag is set */
00551   LSM303DLHC_Timeout = LSM303DLHC_LONG_TIMEOUT;
00552   while(I2C_GetFlagStatus(LSM303DLHC_I2C, I2C_ISR_STOPF) == RESET)   
00553   {
00554     if((LSM303DLHC_Timeout--) == 0) return LSM303DLHC_TIMEOUT_UserCallback();
00555   }
00556   
00557   /* Clear STOPF flag */
00558   I2C_ClearFlag(LSM303DLHC_I2C, I2C_ICR_STOPCF);
00559   
00560   /* If all operations OK */
00561   return LSM303DLHC_OK;  
00562 }  
00563 /**
00564 * @brief  Initializes the low level interface used to drive the LSM303DLHC
00565 * @param  None
00566 * @retval None
00567 */
00568 static void LSM303DLHC_LowLevel_Init(void)
00569 {
00570   GPIO_InitTypeDef GPIO_InitStructure;
00571   EXTI_InitTypeDef EXTI_InitStructure;
00572   I2C_InitTypeDef  I2C_InitStructure;
00573   
00574   /* Enable the I2C periph */
00575   RCC_APB1PeriphClockCmd(LSM303DLHC_I2C_CLK, ENABLE);
00576   
00577   /* Enable SCK and SDA GPIO clocks */
00578   RCC_AHBPeriphClockCmd(LSM303DLHC_I2C_SCK_GPIO_CLK | LSM303DLHC_I2C_SDA_GPIO_CLK , ENABLE);
00579   
00580   /* Enable INT1 GPIO clock */
00581   RCC_AHBPeriphClockCmd(LSM303DLHC_I2C_INT1_GPIO_CLK, ENABLE);
00582   
00583   /* Enable INT2 GPIO clock */
00584   RCC_AHBPeriphClockCmd(LSM303DLHC_I2C_INT2_GPIO_CLK, ENABLE);
00585   
00586   /* Enable DRDY clock */
00587   RCC_AHBPeriphClockCmd(LSM303DLHC_DRDY_GPIO_CLK, ENABLE);
00588   
00589   GPIO_PinAFConfig(LSM303DLHC_I2C_SCK_GPIO_PORT, LSM303DLHC_I2C_SCK_SOURCE, LSM303DLHC_I2C_SCK_AF);
00590   GPIO_PinAFConfig(LSM303DLHC_I2C_SDA_GPIO_PORT, LSM303DLHC_I2C_SDA_SOURCE, LSM303DLHC_I2C_SDA_AF);
00591   
00592   GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;
00593   GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
00594   GPIO_InitStructure.GPIO_PuPd  = GPIO_PuPd_DOWN;
00595   GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
00596   
00597   /* I2C SCK pin configuration */
00598   GPIO_InitStructure.GPIO_Pin = LSM303DLHC_I2C_SCK_PIN;
00599   GPIO_Init(LSM303DLHC_I2C_SCK_GPIO_PORT, &GPIO_InitStructure);
00600   
00601   /* I2C SDA pin configuration */
00602   GPIO_InitStructure.GPIO_Pin =  LSM303DLHC_I2C_SDA_PIN;
00603   GPIO_Init(LSM303DLHC_I2C_SDA_GPIO_PORT, &GPIO_InitStructure);
00604   
00605   /* Mems DRDY */
00606   GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN;
00607   GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
00608   GPIO_InitStructure.GPIO_PuPd  = GPIO_PuPd_NOPULL;
00609   GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
00610   
00611   /* Mems DRDY pin configuration */
00612   GPIO_InitStructure.GPIO_Pin = LSM303DLHC_DRDY_PIN;
00613   GPIO_Init(LSM303DLHC_DRDY_GPIO_PORT, &GPIO_InitStructure);
00614   
00615   /* Connect EXTI Line to Mems DRDY Pin */
00616   SYSCFG_EXTILineConfig(LSM303DLHC_DRDY_EXTI_PORT_SOURCE, LSM303DLHC_DRDY_EXTI_PIN_SOURCE);
00617   
00618   EXTI_InitStructure.EXTI_Line = LSM303DLHC_DRDY_EXTI_LINE;
00619   EXTI_InitStructure.EXTI_Mode = EXTI_Mode_Interrupt;
00620   EXTI_InitStructure.EXTI_Trigger = EXTI_Trigger_Rising;
00621   EXTI_InitStructure.EXTI_LineCmd = ENABLE;
00622   
00623   EXTI_Init(&EXTI_InitStructure);
00624   
00625   /* I2C configuration -------------------------------------------------------*/
00626   I2C_InitStructure.I2C_Mode = I2C_Mode_I2C;
00627   I2C_InitStructure.I2C_AnalogFilter = I2C_AnalogFilter_Enable;
00628   I2C_InitStructure.I2C_DigitalFilter = 0x00;
00629   I2C_InitStructure.I2C_OwnAddress1 = 0x00;
00630   I2C_InitStructure.I2C_Ack = I2C_Ack_Enable;
00631   I2C_InitStructure.I2C_AcknowledgedAddress = I2C_AcknowledgedAddress_7bit;
00632   I2C_InitStructure.I2C_Timing = 0x00902025;
00633   
00634   /* Apply LSM303DLHC_I2C configuration after enabling it */
00635   I2C_Init(LSM303DLHC_I2C, &I2C_InitStructure);
00636   
00637   /* LSM303DLHC_I2C Peripheral Enable */
00638   I2C_Cmd(LSM303DLHC_I2C, ENABLE);
00639   
00640   /* Configure GPIO PINs to detect Interrupts */
00641   GPIO_InitStructure.GPIO_Pin = LSM303DLHC_I2C_INT1_PIN;
00642   GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN;
00643   GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
00644   GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
00645   GPIO_InitStructure.GPIO_PuPd  = GPIO_PuPd_NOPULL;
00646   GPIO_Init(LSM303DLHC_I2C_INT1_GPIO_PORT, &GPIO_InitStructure);
00647   
00648   GPIO_InitStructure.GPIO_Pin = LSM303DLHC_I2C_INT2_PIN;
00649   GPIO_Init(LSM303DLHC_I2C_INT2_GPIO_PORT, &GPIO_InitStructure);
00650 }  
00651 
00652 #ifdef USE_DEFAULT_TIMEOUT_CALLBACK
00653 /**
00654   * @brief  Basic management of the timeout situation.
00655   * @param  None.
00656   * @retval None.
00657   */
00658 uint32_t LSM303DLHC_TIMEOUT_UserCallback(void)
00659 {
00660   /* Block communication and all processes */
00661   while (1)
00662   {   
00663   }
00664 }
00665 #endif /* USE_DEFAULT_TIMEOUT_CALLBACK */
00666 
00667 /**
00668   * @}
00669   */ 
00670 
00671 /**
00672   * @}
00673   */ 
00674   
00675 /**
00676   * @}
00677   */ 
00678 
00679 /**
00680   * @}
00681   */ 
00682   
00683 
00684 /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/