Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Dependents: DiscoLogger DISCO_L476VG_GlassLCD DISCO_L476VG_MicrophoneRecorder DISCO_L476VG_UART ... more
lsm303c.c
00001 /** 00002 ****************************************************************************** 00003 * @file lsm303c.c 00004 * @author MCD Application Team 00005 * @brief This file provides a set of functions needed to manage the LSM303C 00006 * MEMS accelerometer. 00007 ****************************************************************************** 00008 * @attention 00009 * 00010 * <h2><center>© Copyright (c) 2015 STMicroelectronics. 00011 * All rights reserved.</center></h2> 00012 * 00013 * This software component is licensed by ST under BSD 3-Clause license, 00014 * the "License"; You may not use this file except in compliance with the 00015 * License. You may obtain a copy of the License at: 00016 * opensource.org/licenses/BSD-3-Clause 00017 * 00018 ****************************************************************************** 00019 */ 00020 /* Includes ------------------------------------------------------------------*/ 00021 #include "lsm303c.h" 00022 00023 /** @addtogroup BSP 00024 * @{ 00025 */ 00026 00027 /** @addtogroup Components 00028 * @{ 00029 */ 00030 00031 /** @addtogroup LSM303C 00032 * @{ 00033 */ 00034 00035 /** @defgroup LSM303C_Private_TypesDefinitions 00036 * @{ 00037 */ 00038 00039 /** 00040 * @} 00041 */ 00042 00043 /** @defgroup LSM303C_Private_Defines 00044 * @{ 00045 */ 00046 00047 /** 00048 * @} 00049 */ 00050 00051 /** @defgroup LSM303C_Private_Macros 00052 * @{ 00053 */ 00054 00055 /** 00056 * @} 00057 */ 00058 00059 /** @defgroup LSM303C_Private_Functions 00060 * @{ 00061 */ 00062 00063 /** 00064 * @} 00065 */ 00066 00067 00068 /** @defgroup LSM303C_Private_Variables 00069 * @{ 00070 */ 00071 ACCELERO_DrvTypeDef Lsm303cDrv_accelero = 00072 { 00073 LSM303C_AccInit, 00074 LSM303C_AccDeInit, 00075 LSM303C_AccReadID, 00076 0, 00077 LSM303C_AccLowPower, 00078 0, 00079 0, 00080 0, 00081 0, 00082 0, 00083 LSM303C_AccFilterConfig, 00084 0, 00085 LSM303C_AccReadXYZ 00086 }; 00087 00088 MAGNETO_DrvTypeDef Lsm303cDrv_magneto = 00089 { 00090 LSM303C_MagInit, 00091 LSM303C_MagDeInit, 00092 LSM303C_MagReadID, 00093 0, 00094 LSM303C_MagLowPower, 00095 0, 00096 0, 00097 0, 00098 0, 00099 0, 00100 0, 00101 0, 00102 LSM303C_MagReadXYZ 00103 00104 }; 00105 00106 /** 00107 * @} 00108 */ 00109 00110 00111 /** 00112 * @brief Set LSM303C Accelerometer Initialization. 00113 * @param InitStruct: Init parameters 00114 * @retval None 00115 */ 00116 void LSM303C_AccInit(uint16_t InitStruct) 00117 { 00118 uint8_t ctrl = 0x00; 00119 00120 /* Low level init */ 00121 ACCELERO_IO_Init(); 00122 00123 /* Write value to ACC MEMS CTRL_REG1 register */ 00124 ctrl = (uint8_t) InitStruct; 00125 ACCELERO_IO_Write(LSM303C_CTRL_REG1_A, ctrl); 00126 00127 /* Write value to ACC MEMS CTRL_REG4 register */ 00128 ctrl = ((uint8_t) (InitStruct >> 8)); 00129 ACCELERO_IO_Write(LSM303C_CTRL_REG4_A, ctrl); 00130 } 00131 00132 /** 00133 * @brief LSM303C Accelerometer De-initialization. 00134 * @param None 00135 * @retval None 00136 */ 00137 void LSM303C_AccDeInit(void) 00138 { 00139 } 00140 00141 /** 00142 * @brief Read LSM303C ID. 00143 * @param None 00144 * @retval ID 00145 */ 00146 uint8_t LSM303C_AccReadID(void) 00147 { 00148 uint8_t ctrl = 0x00; 00149 00150 /* Low level init */ 00151 ACCELERO_IO_Init(); 00152 00153 /* Enabled SPI/I2C read communication */ 00154 ACCELERO_IO_Write(LSM303C_CTRL_REG4_A, 0x5); 00155 00156 /* Read value at Who am I register address */ 00157 ctrl = ACCELERO_IO_Read(LSM303C_WHO_AM_I_ADDR); 00158 00159 return ctrl; 00160 } 00161 00162 /** 00163 * @brief Put Accelerometer in power down mode or not. 00164 * @param Mode equal to LSM303C_ACC_ODR_OFF means enable Low Power Mode, otherwise Output data rate is set. 00165 * This parameter can be a value of @ref Acc_OutPut_DataRate_Selection 00166 * @retval None 00167 */ 00168 void LSM303C_AccLowPower(uint16_t Mode) 00169 { 00170 uint8_t ctrl = 0x00; 00171 00172 /* Read control register 1 value */ 00173 ctrl = ACCELERO_IO_Read(LSM303C_CTRL_REG1_A); 00174 00175 /* Clear ODR bits */ 00176 ctrl &= ~(LSM303C_ACC_ODR_BITPOSITION); 00177 00178 /* Set Power down */ 00179 ctrl |= (uint8_t)Mode; 00180 00181 /* write back control register */ 00182 ACCELERO_IO_Write(LSM303C_CTRL_REG1_A, ctrl); 00183 } 00184 00185 /** 00186 * @brief Set High Pass Filter Modality 00187 * @param FilterStruct: contains data for filter config 00188 * @retval None 00189 */ 00190 void LSM303C_AccFilterConfig(uint8_t FilterStruct) 00191 { 00192 uint8_t tmpreg; 00193 00194 // /* Read CTRL_REG2 register */ 00195 // tmpreg = ACCELERO_IO_Read(LSM303C_CTRL_REG2_A); 00196 // 00197 // tmpreg &= 0x0C; 00198 tmpreg = FilterStruct; 00199 00200 /* Write value to ACC MEMS CTRL_REG2 register */ 00201 ACCELERO_IO_Write(LSM303C_CTRL_REG2_A, tmpreg); 00202 } 00203 00204 /** 00205 * @brief Read X, Y & Z Acceleration values 00206 * @param pData: Data out pointer 00207 * @retval None 00208 */ 00209 void LSM303C_AccReadXYZ(int16_t* pData) 00210 { 00211 int16_t pnRawData[3]; 00212 uint8_t ctrlx[2]={0,0}; 00213 uint8_t buffer[6]; 00214 uint8_t i = 0; 00215 uint8_t sensitivity = LSM303C_ACC_SENSITIVITY_2G; 00216 00217 /* Read the acceleration control register content */ 00218 ctrlx[0] = ACCELERO_IO_Read(LSM303C_CTRL_REG4_A); 00219 ctrlx[1] = ACCELERO_IO_Read(LSM303C_CTRL_REG5_A); 00220 00221 /* Read output register X, Y & Z acceleration */ 00222 buffer[0] = ACCELERO_IO_Read(LSM303C_OUT_X_L_A); 00223 buffer[1] = ACCELERO_IO_Read(LSM303C_OUT_X_H_A); 00224 buffer[2] = ACCELERO_IO_Read(LSM303C_OUT_Y_L_A); 00225 buffer[3] = ACCELERO_IO_Read(LSM303C_OUT_Y_H_A); 00226 buffer[4] = ACCELERO_IO_Read(LSM303C_OUT_Z_L_A); 00227 buffer[5] = ACCELERO_IO_Read(LSM303C_OUT_Z_H_A); 00228 00229 for(i=0; i<3; i++) 00230 { 00231 pnRawData[i]=((int16_t)((uint16_t)buffer[2*i+1] << 8) + buffer[2*i]); 00232 } 00233 00234 /* Normal mode */ 00235 /* Switch the sensitivity value set in the CRTL4 */ 00236 switch(ctrlx[0] & LSM303C_ACC_FULLSCALE_8G) 00237 { 00238 case LSM303C_ACC_FULLSCALE_2G: 00239 sensitivity = LSM303C_ACC_SENSITIVITY_2G; 00240 break; 00241 case LSM303C_ACC_FULLSCALE_4G: 00242 sensitivity = LSM303C_ACC_SENSITIVITY_4G; 00243 break; 00244 case LSM303C_ACC_FULLSCALE_8G: 00245 sensitivity = LSM303C_ACC_SENSITIVITY_8G; 00246 break; 00247 } 00248 00249 /* Obtain the mg value for the three axis */ 00250 for(i=0; i<3; i++) 00251 { 00252 pData[i]=(pnRawData[i] * sensitivity); 00253 } 00254 } 00255 00256 /*********************************************************************************************** 00257 Magnetometer driver 00258 ***********************************************************************************************/ 00259 00260 /** 00261 * @brief Set LSM303C Magnetometer Initialization. 00262 * @param LSM303C_InitStruct: pointer to a LSM303C_MagInitTypeDef structure 00263 * that contains the configuration setting for the LSM303C. 00264 * @retval None 00265 */ 00266 void LSM303C_MagInit(MAGNETO_InitTypeDef LSM303C_InitStruct) 00267 { 00268 MAGNETO_IO_Write(LSM303C_CTRL_REG1_M, LSM303C_InitStruct.Register1); 00269 MAGNETO_IO_Write(LSM303C_CTRL_REG2_M, LSM303C_InitStruct.Register2); 00270 MAGNETO_IO_Write(LSM303C_CTRL_REG3_M, LSM303C_InitStruct.Register3); 00271 MAGNETO_IO_Write(LSM303C_CTRL_REG4_M, LSM303C_InitStruct.Register4); 00272 MAGNETO_IO_Write(LSM303C_CTRL_REG5_M, LSM303C_InitStruct.Register5); 00273 } 00274 00275 /** 00276 * @brief LSM303C Magnetometer De-initialization. 00277 * @param None 00278 * @retval None 00279 */ 00280 void LSM303C_MagDeInit(void) 00281 { 00282 } 00283 00284 /** 00285 * @brief Read LSM303C ID. 00286 * @param None 00287 * @retval ID 00288 */ 00289 uint8_t LSM303C_MagReadID(void) 00290 { 00291 /* Low level init */ 00292 MAGNETO_IO_Init(); 00293 00294 /* Enabled the SPI/I2C read operation */ 00295 MAGNETO_IO_Write(LSM303C_CTRL_REG3_M, 0x84); 00296 00297 /* Read value at Who am I register address */ 00298 return MAGNETO_IO_Read(LSM303C_WHO_AM_I_ADDR); 00299 } 00300 00301 /** 00302 * @brief Put Magnetometer in power down mode or not. 00303 * @param Mode equal to LSM303C_MAG_POWERDOWN2_MODE means enable deepest Low Power Mode, otherwise other mode is set. 00304 * This parameter can be a value of @ref Mag_Operation_Mode 00305 * @retval None 00306 */ 00307 void LSM303C_MagLowPower(uint16_t Mode) 00308 { 00309 uint8_t ctrl = 0x00; 00310 00311 /* Read control register 1 value */ 00312 ctrl = MAGNETO_IO_Read(LSM303C_CTRL_REG3_M); 00313 00314 /* Clear ODR bits */ 00315 ctrl &= ~(LSM303C_MAG_SELECTION_MODE); 00316 00317 /* Set mode */ 00318 ctrl |= (uint8_t)Mode; 00319 00320 /* write back control register */ 00321 MAGNETO_IO_Write(LSM303C_CTRL_REG3_M, ctrl); 00322 } 00323 00324 /** 00325 * @brief Get status for Mag LSM303C data 00326 * @param None 00327 * @retval Data status in a LSM303C Data register 00328 */ 00329 uint8_t LSM303C_MagGetDataStatus(void) 00330 { 00331 /* Read Mag STATUS register */ 00332 return MAGNETO_IO_Read(LSM303C_STATUS_REG_M); 00333 } 00334 00335 /** 00336 * @brief Read X, Y & Z Magnetometer values 00337 * @param pData: Data out pointer 00338 * @retval None 00339 */ 00340 void LSM303C_MagReadXYZ(int16_t* pData) 00341 { 00342 uint8_t ctrlx; 00343 uint8_t buffer[6]; 00344 uint8_t i=0; 00345 00346 /* Read the magnetometer control register content */ 00347 ctrlx = MAGNETO_IO_Read(LSM303C_CTRL_REG4_M); 00348 00349 /* Read output register X, Y & Z magnetometer */ 00350 buffer[0] = MAGNETO_IO_Read(LSM303C_OUT_X_L_M); 00351 buffer[1] = MAGNETO_IO_Read(LSM303C_OUT_X_H_M); 00352 buffer[2] = MAGNETO_IO_Read(LSM303C_OUT_Y_L_M); 00353 buffer[3] = MAGNETO_IO_Read(LSM303C_OUT_Y_H_M); 00354 buffer[4] = MAGNETO_IO_Read(LSM303C_OUT_Z_L_M); 00355 buffer[5] = MAGNETO_IO_Read(LSM303C_OUT_Z_H_M); 00356 00357 /* Check in the control register4 the data alignment*/ 00358 if((ctrlx & LSM303C_MAG_BLE_MSB)) 00359 { 00360 for(i=0; i<3; i++) 00361 { 00362 pData[i]=((int16_t)((uint16_t)buffer[2*i] << 8) + buffer[2*i+1]); 00363 } 00364 } 00365 else 00366 { 00367 for(i=0; i<3; i++) 00368 { 00369 pData[i]=((int16_t)((uint16_t)buffer[2*i+1] << 8) + buffer[2*i]); 00370 } 00371 } 00372 } 00373 00374 /** 00375 * @} 00376 */ 00377 00378 /** 00379 * @} 00380 */ 00381 00382 /** 00383 * @} 00384 */ 00385 00386 /** 00387 * @} 00388 */ 00389 00390 /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/
Generated on Tue Jul 12 2022 18:37:21 by
1.7.2