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.
Fork of TUKS-COURSE-TIMER by
lsm303c.c
00001 /** 00002 ****************************************************************************** 00003 * @file lsm303c.c 00004 * @author MCD Application Team 00005 * @version V1.0.0 00006 * @date 24-June-2015 00007 * @brief This file provides a set of functions needed to manage the LSM303C 00008 * MEMS accelerometer. 00009 ****************************************************************************** 00010 * @attention 00011 * 00012 * <h2><center>© COPYRIGHT(c) 2015 STMicroelectronics</center></h2> 00013 * 00014 * Redistribution and use in source and binary forms, with or without modification, 00015 * are permitted provided that the following conditions are met: 00016 * 1. Redistributions of source code must retain the above copyright notice, 00017 * this list of conditions and the following disclaimer. 00018 * 2. Redistributions in binary form must reproduce the above copyright notice, 00019 * this list of conditions and the following disclaimer in the documentation 00020 * and/or other materials provided with the distribution. 00021 * 3. Neither the name of STMicroelectronics nor the names of its contributors 00022 * may be used to endorse or promote products derived from this software 00023 * without specific prior written permission. 00024 * 00025 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" 00026 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 00027 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE 00028 * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE 00029 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 00030 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR 00031 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER 00032 * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, 00033 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 00034 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 00035 * 00036 ****************************************************************************** 00037 */ 00038 /* Includes ------------------------------------------------------------------*/ 00039 #include "lsm303c.h" 00040 00041 /** @addtogroup BSP 00042 * @{ 00043 */ 00044 00045 /** @addtogroup Components 00046 * @{ 00047 */ 00048 00049 /** @addtogroup LSM303C 00050 * @{ 00051 */ 00052 00053 /** @defgroup LSM303C_Private_TypesDefinitions 00054 * @{ 00055 */ 00056 00057 /** 00058 * @} 00059 */ 00060 00061 /** @defgroup LSM303C_Private_Defines 00062 * @{ 00063 */ 00064 00065 /** 00066 * @} 00067 */ 00068 00069 /** @defgroup LSM303C_Private_Macros 00070 * @{ 00071 */ 00072 00073 /** 00074 * @} 00075 */ 00076 00077 /** @defgroup LSM303C_Private_Functions 00078 * @{ 00079 */ 00080 00081 /* ACCELEROMETER functions */ 00082 void LSM303C_AccInit(uint16_t InitStruct); 00083 void LSM303C_AccDeInit(void); 00084 uint8_t LSM303C_AccReadID(void); 00085 void LSM303C_AccLowPower(void); 00086 void LSM303C_AccFilterConfig(uint8_t FilterStruct); 00087 void LSM303C_AccFilterCmd(uint8_t HighPassFilterState); 00088 void LSM303C_AccReadXYZ(int16_t* pData); 00089 void LSM303C_AccFilterClickCmd(uint8_t HighPassFilterClickState); 00090 void LSM303C_AccIT1Enable(uint8_t LSM303C_IT); 00091 void LSM303C_AccIT1Disable(uint8_t LSM303C_IT); 00092 void LSM303C_AccIT2Enable(uint8_t LSM303C_IT); 00093 void LSM303C_AccIT2Disable(uint8_t LSM303C_IT); 00094 void LSM303C_AccClickITEnable(uint8_t ITClick); 00095 void LSM303C_AccClickITDisable(uint8_t ITClick); 00096 void LSM303C_AccZClickITConfig(void); 00097 00098 /* MAGNETOMETER functions */ 00099 void LSM303C_MagInit(MAGNETO_InitTypeDef LSM303C_InitStruct); 00100 void LSM303C_MagDeInit(void); 00101 uint8_t LSM303C_MagReadID(void); 00102 void LSM303C_MagLowPower(void); 00103 void LSM303C_MagReadXYZ(int16_t* pData); 00104 uint8_t LSM303C_MagGetDataStatus(void); 00105 00106 00107 /* COMPASS / ACCELERO IO functions */ 00108 extern void ACCELERO_IO_Init(void); 00109 extern void ACCELERO_IO_ITConfig(void); 00110 extern void ACCELERO_IO_Write(uint8_t RegisterAddr, uint8_t Value); 00111 extern uint8_t ACCELERO_IO_Read(uint8_t RegisterAddr); 00112 00113 /* COMPASS IO function */ 00114 extern void MAGNETO_IO_Init(void); 00115 extern void MAGNETO_IO_ITConfig(void); 00116 extern void MAGNETO_IO_Write(uint8_t RegisterAddr, uint8_t Value); 00117 extern uint8_t MAGNETO_IO_Read(uint8_t RegisterAddr); 00118 00119 /** 00120 * @} 00121 */ 00122 00123 00124 /** @defgroup LSM303C_Private_Variables 00125 * @{ 00126 */ 00127 ACCELERO_DrvTypeDef Lsm303cDrv_accelero = 00128 { 00129 LSM303C_AccInit, 00130 LSM303C_AccDeInit, 00131 LSM303C_AccReadID, 00132 0, 00133 LSM303C_AccLowPower, 00134 0, 00135 0, 00136 0, 00137 0, 00138 0, 00139 LSM303C_AccFilterConfig, 00140 0, 00141 LSM303C_AccReadXYZ 00142 }; 00143 00144 MAGNETO_DrvTypeDef Lsm303cDrv_magneto = 00145 { 00146 LSM303C_MagInit, 00147 LSM303C_MagDeInit, 00148 LSM303C_MagReadID, 00149 0, 00150 LSM303C_MagLowPower, 00151 0, 00152 0, 00153 0, 00154 0, 00155 0, 00156 0, 00157 0, 00158 LSM303C_MagReadXYZ 00159 00160 }; 00161 00162 /** 00163 * @} 00164 */ 00165 00166 00167 /** 00168 * @brief Set LSM303C Accelerometer Initialization. 00169 * @param InitStruct: Init parameters 00170 * @retval None 00171 */ 00172 void LSM303C_AccInit(uint16_t InitStruct) 00173 { 00174 uint8_t ctrl = 0x00; 00175 00176 /* Low level init */ 00177 ACCELERO_IO_Init(); 00178 00179 /* Write value to ACC MEMS CTRL_REG1 register */ 00180 ctrl = (uint8_t) InitStruct; 00181 ACCELERO_IO_Write(LSM303C_CTRL_REG1_A, ctrl); 00182 00183 /* Write value to ACC MEMS CTRL_REG4 register */ 00184 ctrl = ((uint8_t) (InitStruct >> 8)); 00185 ACCELERO_IO_Write(LSM303C_CTRL_REG4_A, ctrl); 00186 } 00187 00188 /** 00189 * @brief LSM303C Accelerometer De-initialization. 00190 * @param None 00191 * @retval None 00192 */ 00193 void LSM303C_AccDeInit(void) 00194 { 00195 } 00196 00197 /** 00198 * @brief Read LSM303C ID. 00199 * @param None 00200 * @retval ID 00201 */ 00202 uint8_t LSM303C_AccReadID(void) 00203 { 00204 uint8_t ctrl = 0x00; 00205 00206 /* Low level init */ 00207 ACCELERO_IO_Init(); 00208 00209 /* Enabled SPI/I2C read communication */ 00210 ACCELERO_IO_Write(LSM303C_CTRL_REG4_A, 0x5); 00211 00212 /* Read value at Who am I register address */ 00213 ctrl = ACCELERO_IO_Read(LSM303C_WHO_AM_I_ADDR); 00214 00215 return ctrl; 00216 } 00217 00218 /** 00219 * @brief Put Accelerometer in power down mode. 00220 * @param None 00221 * @retval None 00222 */ 00223 void LSM303C_AccLowPower(void) 00224 { 00225 uint8_t ctrl = 0x00; 00226 00227 /* Read control register 1 value */ 00228 ctrl = ACCELERO_IO_Read(LSM303C_CTRL_REG1_A); 00229 00230 /* Clear ODR bits */ 00231 ctrl &= ~(LSM303C_ACC_ODR_BITPOSITION); 00232 00233 /* Set Power down */ 00234 ctrl |= LSM303C_ACC_ODR_OFF; 00235 00236 /* write back control register */ 00237 ACCELERO_IO_Write(LSM303C_CTRL_REG1_A, ctrl); 00238 } 00239 00240 /** 00241 * @brief Set High Pass Filter Modality 00242 * @param FilterStruct: contains data for filter config 00243 * @retval None 00244 */ 00245 void LSM303C_AccFilterConfig(uint8_t FilterStruct) 00246 { 00247 uint8_t tmpreg; 00248 00249 // /* Read CTRL_REG2 register */ 00250 // tmpreg = ACCELERO_IO_Read(LSM303C_CTRL_REG2_A); 00251 // 00252 // tmpreg &= 0x0C; 00253 tmpreg = FilterStruct; 00254 00255 /* Write value to ACC MEMS CTRL_REG2 register */ 00256 ACCELERO_IO_Write(LSM303C_CTRL_REG2_A, tmpreg); 00257 } 00258 00259 /** 00260 * @brief Read X, Y & Z Acceleration values 00261 * @param pData: Data out pointer 00262 * @retval None 00263 */ 00264 void LSM303C_AccReadXYZ(int16_t* pData) 00265 { 00266 int16_t pnRawData[3]; 00267 uint8_t ctrlx[2]={0,0}; 00268 uint8_t buffer[6]; 00269 uint8_t i = 0; 00270 uint8_t sensitivity = LSM303C_ACC_SENSITIVITY_2G; 00271 00272 /* Read the acceleration control register content */ 00273 ctrlx[0] = ACCELERO_IO_Read(LSM303C_CTRL_REG4_A); 00274 ctrlx[1] = ACCELERO_IO_Read(LSM303C_CTRL_REG5_A); 00275 00276 /* Read output register X, Y & Z acceleration */ 00277 buffer[0] = ACCELERO_IO_Read(LSM303C_OUT_X_L_A); 00278 buffer[1] = ACCELERO_IO_Read(LSM303C_OUT_X_H_A); 00279 buffer[2] = ACCELERO_IO_Read(LSM303C_OUT_Y_L_A); 00280 buffer[3] = ACCELERO_IO_Read(LSM303C_OUT_Y_H_A); 00281 buffer[4] = ACCELERO_IO_Read(LSM303C_OUT_Z_L_A); 00282 buffer[5] = ACCELERO_IO_Read(LSM303C_OUT_Z_H_A); 00283 00284 for(i=0; i<3; i++) 00285 { 00286 pnRawData[i]=((int16_t)((uint16_t)buffer[2*i+1] << 8) + buffer[2*i]); 00287 } 00288 00289 /* Normal mode */ 00290 /* Switch the sensitivity value set in the CRTL4 */ 00291 switch(ctrlx[0] & LSM303C_ACC_FULLSCALE_8G) 00292 { 00293 case LSM303C_ACC_FULLSCALE_2G: 00294 sensitivity = LSM303C_ACC_SENSITIVITY_2G; 00295 break; 00296 case LSM303C_ACC_FULLSCALE_4G: 00297 sensitivity = LSM303C_ACC_SENSITIVITY_4G; 00298 break; 00299 case LSM303C_ACC_FULLSCALE_8G: 00300 sensitivity = LSM303C_ACC_SENSITIVITY_8G; 00301 break; 00302 } 00303 00304 /* Obtain the mg value for the three axis */ 00305 for(i=0; i<3; i++) 00306 { 00307 pData[i]=(pnRawData[i] * sensitivity); 00308 } 00309 } 00310 00311 /*********************************************************************************************** 00312 Magnetometer driver 00313 ***********************************************************************************************/ 00314 00315 /** 00316 * @brief Set LSM303C Magnetometer Initialization. 00317 * @param LSM303C_InitStruct: pointer to a LSM303C_MagInitTypeDef structure 00318 * that contains the configuration setting for the LSM303C. 00319 * @retval None 00320 */ 00321 void LSM303C_MagInit(MAGNETO_InitTypeDef LSM303C_InitStruct) 00322 { 00323 MAGNETO_IO_Write(LSM303C_CTRL_REG1_M, LSM303C_InitStruct.Register1); 00324 MAGNETO_IO_Write(LSM303C_CTRL_REG2_M, LSM303C_InitStruct.Register2); 00325 MAGNETO_IO_Write(LSM303C_CTRL_REG3_M, LSM303C_InitStruct.Register3); 00326 MAGNETO_IO_Write(LSM303C_CTRL_REG4_M, LSM303C_InitStruct.Register4); 00327 MAGNETO_IO_Write(LSM303C_CTRL_REG5_M, LSM303C_InitStruct.Register5); 00328 } 00329 00330 /** 00331 * @brief LSM303C Magnetometer De-initialization. 00332 * @param None 00333 * @retval None 00334 */ 00335 void LSM303C_MagDeInit(void) 00336 { 00337 } 00338 00339 /** 00340 * @brief Read LSM303C ID. 00341 * @param None 00342 * @retval ID 00343 */ 00344 uint8_t LSM303C_MagReadID(void) 00345 { 00346 /* Low level init */ 00347 MAGNETO_IO_Init(); 00348 00349 /* Enabled the SPI/I2C read operation */ 00350 MAGNETO_IO_Write(LSM303C_CTRL_REG3_M, 0x84); 00351 00352 /* Read value at Who am I register address */ 00353 return MAGNETO_IO_Read(LSM303C_WHO_AM_I_ADDR); 00354 } 00355 00356 /** 00357 * @brief Put Magnetometer in power down mode. 00358 * @param None 00359 * @retval None 00360 */ 00361 void LSM303C_MagLowPower(void) 00362 { 00363 uint8_t ctrl = 0x00; 00364 00365 /* Read control register 1 value */ 00366 ctrl = MAGNETO_IO_Read(LSM303C_CTRL_REG3_M); 00367 00368 /* Clear ODR bits */ 00369 ctrl &= ~(LSM303C_MAG_SELECTION_MODE); 00370 00371 /* Set Power down */ 00372 ctrl |= LSM303C_MAG_POWERDOWN2_MODE; 00373 00374 /* write back control register */ 00375 MAGNETO_IO_Write(LSM303C_CTRL_REG3_M, ctrl); 00376 } 00377 00378 /** 00379 * @brief Get status for Mag LSM303C data 00380 * @param None 00381 * @retval Data status in a LSM303C Data register 00382 */ 00383 uint8_t LSM303C_MagGetDataStatus(void) 00384 { 00385 /* Read Mag STATUS register */ 00386 return MAGNETO_IO_Read(LSM303C_STATUS_REG_M); 00387 } 00388 00389 /** 00390 * @brief Read X, Y & Z Magnetometer values 00391 * @param pData: Data out pointer 00392 * @retval None 00393 */ 00394 void LSM303C_MagReadXYZ(int16_t* pData) 00395 { 00396 uint8_t ctrlx; 00397 uint8_t buffer[6]; 00398 uint8_t i=0; 00399 00400 /* Read the magnetometer control register content */ 00401 ctrlx = MAGNETO_IO_Read(LSM303C_CTRL_REG4_M); 00402 00403 /* Read output register X, Y & Z magnetometer */ 00404 buffer[0] = MAGNETO_IO_Read(LSM303C_OUT_X_L_M); 00405 buffer[1] = MAGNETO_IO_Read(LSM303C_OUT_X_H_M); 00406 buffer[2] = MAGNETO_IO_Read(LSM303C_OUT_Y_L_M); 00407 buffer[3] = MAGNETO_IO_Read(LSM303C_OUT_Y_H_M); 00408 buffer[4] = MAGNETO_IO_Read(LSM303C_OUT_Z_L_M); 00409 buffer[5] = MAGNETO_IO_Read(LSM303C_OUT_Z_H_M); 00410 00411 /* Check in the control register4 the data alignment*/ 00412 if((ctrlx & LSM303C_MAG_BLE_MSB)) 00413 { 00414 for(i=0; i<3; i++) 00415 { 00416 pData[i]=((int16_t)((uint16_t)buffer[2*i] << 8) + buffer[2*i+1]); 00417 } 00418 } 00419 else 00420 { 00421 for(i=0; i<3; i++) 00422 { 00423 pData[i]=((int16_t)((uint16_t)buffer[2*i+1] << 8) + buffer[2*i]); 00424 } 00425 } 00426 } 00427 00428 /** 00429 * @} 00430 */ 00431 00432 /** 00433 * @} 00434 */ 00435 00436 /** 00437 * @} 00438 */ 00439 00440 /** 00441 * @} 00442 */ 00443 00444 /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/
Generated on Tue Jul 12 2022 17:38:48 by
