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.
Dependencies: X_NUCLEO_COMMON ST_INTERFACES
Dependents: HelloWorld_ST_Sensors MOTENV_Mbed mbed-os-mqtt-client LSM303AGR_JS ... more
LSM303AGRMagSensor.cpp
00001 /** 00002 ****************************************************************************** 00003 * @file LSM303AGRMagSensor.cpp 00004 * @author CLab 00005 * @version V1.0.0 00006 * @date 5 August 2016 00007 * @brief Implementation an LSM303AGR magnetometer sensor. 00008 ****************************************************************************** 00009 * @attention 00010 * 00011 * <h2><center>© COPYRIGHT(c) 2016 STMicroelectronics</center></h2> 00012 * 00013 * Redistribution and use in source and binary forms, with or without modification, 00014 * are permitted provided that the following conditions are met: 00015 * 1. Redistributions of source code must retain the above copyright notice, 00016 * this list of conditions and the following disclaimer. 00017 * 2. Redistributions in binary form must reproduce the above copyright notice, 00018 * this list of conditions and the following disclaimer in the documentation 00019 * and/or other materials provided with the distribution. 00020 * 3. Neither the name of STMicroelectronics nor the names of its contributors 00021 * may be used to endorse or promote products derived from this software 00022 * without specific prior written permission. 00023 * 00024 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" 00025 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 00026 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE 00027 * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE 00028 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 00029 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR 00030 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER 00031 * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, 00032 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 00033 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 00034 * 00035 ****************************************************************************** 00036 */ 00037 00038 00039 /* Includes ------------------------------------------------------------------*/ 00040 00041 #include "LSM303AGRMagSensor.h" 00042 00043 00044 /* Class Implementation ------------------------------------------------------*/ 00045 LSM303AGRMagSensor::LSM303AGRMagSensor(SPI *spi, PinName cs_pin, PinName intmag_pin) : 00046 _dev_spi(spi), _cs_pin(cs_pin), _intmag_pin(intmag_pin) // SPI3W ONLY 00047 { 00048 assert (spi); 00049 if (cs_pin == NC) 00050 { 00051 printf ("ERROR LSM303AGRMagSensor CS MUST NOT BE NC\n\r"); 00052 _dev_spi = NULL; 00053 _dev_i2c=NULL; 00054 return; 00055 } 00056 _cs_pin = 0; // enable SPI3W disable I2C 00057 _dev_i2c=NULL; 00058 00059 LSM303AGR_ACC_W_SPI_mode((void *)this, LSM303AGR_ACC_SIM_3_WIRES); 00060 } 00061 /** Constructor 00062 * @param i2c object of an helper class which handles the I2C peripheral 00063 * @param address the address of the component's instance 00064 */ 00065 LSM303AGRMagSensor::LSM303AGRMagSensor(DevI2C *i2c, uint8_t address, PinName intmag_pin) : 00066 _dev_i2c(i2c), _address(address), _cs_pin(NC), _intmag_pin(intmag_pin) 00067 { 00068 assert (i2c); 00069 _dev_spi = NULL; 00070 } 00071 00072 /** 00073 * @brief Initializing the component. 00074 * @param[in] init pointer to device specific initalization structure. 00075 * @retval "0" in case of success, an error code otherwise. 00076 */ 00077 int LSM303AGRMagSensor::init(void *init) 00078 { 00079 /* Operating mode selection - power down */ 00080 if ( LSM303AGR_MAG_W_MD( (void *)this, LSM303AGR_MAG_MD_IDLE1_MODE ) == MEMS_ERROR ) 00081 { 00082 return 1; 00083 } 00084 00085 /* Enable BDU */ 00086 if ( LSM303AGR_MAG_W_BDU( (void *)this, LSM303AGR_MAG_BDU_ENABLED ) == MEMS_ERROR ) 00087 { 00088 return 1; 00089 } 00090 00091 if ( set_m_odr( 100.0f ) == 1 ) 00092 { 00093 return 1; 00094 } 00095 00096 if ( set_m_fs( 50.0f ) == 1 ) 00097 { 00098 return 1; 00099 } 00100 00101 if ( LSM303AGR_MAG_W_ST( (void *)this, LSM303AGR_MAG_ST_DISABLED ) == MEMS_ERROR ) 00102 { 00103 return 1; 00104 } 00105 00106 return 0; 00107 } 00108 00109 /** 00110 * @brief Enable LSM303AGR magnetometer 00111 * @retval 0 in case of success, an error code otherwise 00112 */ 00113 int LSM303AGRMagSensor::enable(void) 00114 { 00115 /* Operating mode selection */ 00116 if ( LSM303AGR_MAG_W_MD( (void *)this, LSM303AGR_MAG_MD_CONTINUOS_MODE ) == MEMS_ERROR ) 00117 { 00118 return 1; 00119 } 00120 00121 return 0; 00122 } 00123 00124 /** 00125 * @brief Disable LSM303AGR magnetometer 00126 * @retval 0 in case of success, an error code otherwise 00127 */ 00128 int LSM303AGRMagSensor::disable(void) 00129 { 00130 /* Operating mode selection - power down */ 00131 if ( LSM303AGR_MAG_W_MD( (void *)this, LSM303AGR_MAG_MD_IDLE1_MODE ) == MEMS_ERROR ) 00132 { 00133 return 1; 00134 } 00135 00136 return 0; 00137 } 00138 00139 /** 00140 * @brief Read ID of LSM303AGR Magnetometer 00141 * @param p_id the pointer where the ID of the device is stored 00142 * @retval 0 in case of success, an error code otherwise 00143 */ 00144 int LSM303AGRMagSensor::read_id(uint8_t *id) 00145 { 00146 if(!id) 00147 { 00148 return 1; 00149 } 00150 00151 /* Read WHO AM I register */ 00152 if ( LSM303AGR_MAG_R_WHO_AM_I( (void *)this, id ) == MEMS_ERROR ) 00153 { 00154 return 1; 00155 } 00156 00157 return 0; 00158 } 00159 00160 /** 00161 * @brief Read data from LSM303AGR Magnetometer 00162 * @param pData the pointer where the magnetometer data are stored 00163 * @retval 0 in case of success, an error code otherwise 00164 */ 00165 int LSM303AGRMagSensor::get_m_axes(int32_t *pData) 00166 { 00167 int16_t pDataRaw[3]; 00168 float sensitivity = 0; 00169 00170 /* Read raw data from LSM303AGR output register. */ 00171 if ( get_m_axes_raw( pDataRaw ) == 1 ) 00172 { 00173 return 1; 00174 } 00175 00176 /* Get LSM303AGR actual sensitivity. */ 00177 if ( get_m_sensitivity( &sensitivity ) == 1 ) 00178 { 00179 return 1; 00180 } 00181 00182 /* Calculate the data. */ 00183 pData[0] = ( int32_t )( pDataRaw[0] * sensitivity ); 00184 pData[1] = ( int32_t )( pDataRaw[1] * sensitivity ); 00185 pData[2] = ( int32_t )( pDataRaw[2] * sensitivity ); 00186 00187 return 0; 00188 } 00189 00190 /** 00191 * @brief Read Magnetometer Sensitivity 00192 * @param pfData the pointer where the magnetometer sensitivity is stored 00193 * @retval 0 in case of success, an error code otherwise 00194 */ 00195 int LSM303AGRMagSensor::get_m_sensitivity(float *pfData) 00196 { 00197 *pfData = 1.5f; 00198 00199 return 0; 00200 } 00201 00202 /** 00203 * @brief Read raw data from LSM303AGR Magnetometer 00204 * @param pData the pointer where the magnetomer raw data are stored 00205 * @retval 0 in case of success, an error code otherwise 00206 */ 00207 int LSM303AGRMagSensor::get_m_axes_raw(int16_t *pData) 00208 { 00209 uint8_t regValue[6] = {0, 0, 0, 0, 0, 0}; 00210 int16_t *regValueInt16; 00211 00212 /* Read output registers from LSM303AGR_MAG_OUTX_L to LSM303AGR_MAG_OUTZ_H. */ 00213 if ( LSM303AGR_MAG_Get_Raw_Magnetic( (void *)this, regValue ) == MEMS_ERROR ) 00214 { 00215 return 1; 00216 } 00217 00218 regValueInt16 = (int16_t *)regValue; 00219 00220 /* Format the data. */ 00221 pData[0] = regValueInt16[0]; 00222 pData[1] = regValueInt16[1]; 00223 pData[2] = regValueInt16[2]; 00224 00225 return 0; 00226 } 00227 00228 /** 00229 * @brief Read LSM303AGR Magnetometer output data rate 00230 * @param odr the pointer to the output data rate 00231 * @retval 0 in case of success, an error code otherwise 00232 */ 00233 int LSM303AGRMagSensor::get_m_odr(float* odr) 00234 { 00235 LSM303AGR_MAG_ODR_t odr_low_level; 00236 00237 if ( LSM303AGR_MAG_R_ODR( (void *)this, &odr_low_level ) == MEMS_ERROR ) 00238 { 00239 return 1; 00240 } 00241 00242 switch( odr_low_level ) 00243 { 00244 case LSM303AGR_MAG_ODR_10Hz: 00245 *odr = 10.000f; 00246 break; 00247 case LSM303AGR_MAG_ODR_20Hz: 00248 *odr = 20.000f; 00249 break; 00250 case LSM303AGR_MAG_ODR_50Hz: 00251 *odr = 50.000f; 00252 break; 00253 case LSM303AGR_MAG_ODR_100Hz: 00254 *odr = 100.000f; 00255 break; 00256 default: 00257 *odr = -1.000f; 00258 return 1; 00259 } 00260 return 0; 00261 } 00262 00263 /** 00264 * @brief Set ODR 00265 * @param odr the output data rate to be set 00266 * @retval 0 in case of success, an error code otherwise 00267 */ 00268 int LSM303AGRMagSensor::set_m_odr(float odr) 00269 { 00270 LSM303AGR_MAG_ODR_t new_odr; 00271 00272 new_odr = ( odr <= 10.000f ) ? LSM303AGR_MAG_ODR_10Hz 00273 : ( odr <= 20.000f ) ? LSM303AGR_MAG_ODR_20Hz 00274 : ( odr <= 50.000f ) ? LSM303AGR_MAG_ODR_50Hz 00275 : LSM303AGR_MAG_ODR_100Hz; 00276 00277 if ( LSM303AGR_MAG_W_ODR( (void *)this, new_odr ) == MEMS_ERROR ) 00278 { 00279 return 1; 00280 } 00281 00282 return 0; 00283 } 00284 00285 00286 /** 00287 * @brief Read LSM303AGR Magnetometer full scale 00288 * @param fullScale the pointer to the output data rate 00289 * @retval 0 in case of success, an error code otherwise 00290 */ 00291 int LSM303AGRMagSensor::get_m_fs(float* fullScale) 00292 { 00293 *fullScale = 50.0f; 00294 00295 return 0; 00296 } 00297 00298 /** 00299 * @brief Set full scale 00300 * @param fullScale the full scale to be set 00301 * @retval 0 in case of success, an error code otherwise 00302 */ 00303 int LSM303AGRMagSensor::set_m_fs(float fullScale) 00304 { 00305 return 0; 00306 } 00307 00308 00309 /** 00310 * @brief Read magnetometer data from register 00311 * @param reg register address 00312 * @param data register data 00313 * @retval 0 in case of success 00314 * @retval 1 in case of failure 00315 */ 00316 int LSM303AGRMagSensor::read_reg( uint8_t reg, uint8_t *data ) 00317 { 00318 if ( LSM303AGR_MAG_read_reg( (void *)this, reg, data ) == MEMS_ERROR ) 00319 { 00320 return 1; 00321 } 00322 00323 return 0; 00324 } 00325 00326 00327 /** 00328 * @brief Write magnetometer data to register 00329 * @param reg register address 00330 * @param data register data 00331 * @retval 0 in case of success 00332 * @retval 1 in case of failure 00333 */ 00334 int LSM303AGRMagSensor::write_reg( uint8_t reg, uint8_t data ) 00335 { 00336 if ( LSM303AGR_MAG_write_reg( (void *)this, reg, data ) == MEMS_ERROR ) 00337 { 00338 return 1; 00339 } 00340 00341 return 0; 00342 } 00343 00344 uint8_t LSM303AGR_MAG_io_write( void *handle, uint8_t WriteAddr, uint8_t *pBuffer, uint16_t nBytesToWrite ) 00345 { 00346 return ((LSM303AGRMagSensor *)handle)->io_write(pBuffer, WriteAddr, nBytesToWrite); 00347 } 00348 00349 uint8_t LSM303AGR_MAG_io_read( void *handle, uint8_t ReadAddr, uint8_t *pBuffer, uint16_t nBytesToRead ) 00350 { 00351 return ((LSM303AGRMagSensor *)handle)->io_read(pBuffer, ReadAddr, nBytesToRead); 00352 }
Generated on Tue Jul 12 2022 23:32:14 by
1.7.2