Beta

Dependencies:   ST_INTERFACES X_NUCLEO_COMMON

Fork of X_NUCLEO_IKS01A2 by ST

Revision:
6:671fd10a51b7
Child:
9:038121268b07
diff -r 63b2b4c21092 -r 671fd10a51b7 Components/LSM303AGRSensor/LSM303AGRMagSensor.cpp
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/Components/LSM303AGRSensor/LSM303AGRMagSensor.cpp	Tue Mar 14 13:41:32 2017 +0100
@@ -0,0 +1,347 @@
+/**
+ ******************************************************************************
+ * @file    LSM303AGRMagSensor.cpp
+ * @author  CLab
+ * @version V1.0.0
+ * @date    5 August 2016
+ * @brief   Implementation an LSM303AGR magnetometer sensor.
+ ******************************************************************************
+ * @attention
+ *
+ * <h2><center>&copy; COPYRIGHT(c) 2016 STMicroelectronics</center></h2>
+ *
+ * Redistribution and use in source and binary forms, with or without modification,
+ * are permitted provided that the following conditions are met:
+ *   1. Redistributions of source code must retain the above copyright notice,
+ *      this list of conditions and the following disclaimer.
+ *   2. Redistributions in binary form must reproduce the above copyright notice,
+ *      this list of conditions and the following disclaimer in the documentation
+ *      and/or other materials provided with the distribution.
+ *   3. Neither the name of STMicroelectronics nor the names of its contributors
+ *      may be used to endorse or promote products derived from this software
+ *      without specific prior written permission.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
+ * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
+ * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
+ * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
+ * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
+ * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
+ * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
+ * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
+ * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
+ * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+ *
+ ******************************************************************************
+ */
+
+
+/* Includes ------------------------------------------------------------------*/
+
+#include "mbed.h"
+#include "DevI2C.h"
+#include "LSM303AGRMagSensor.h"
+#include "LSM303AGR_mag_driver.h"
+
+
+/* Class Implementation ------------------------------------------------------*/
+
+/** Constructor
+ * @param i2c object of an helper class which handles the I2C peripheral
+ * @param address the address of the component's instance
+ */
+LSM303AGRMagSensor::LSM303AGRMagSensor(DevI2C &i2c) : dev_i2c(i2c)
+{
+  address = LSM303AGR_MAG_I2C_ADDRESS;
+};
+
+/** Constructor
+ * @param i2c object of an helper class which handles the I2C peripheral
+ * @param address the address of the component's instance
+ */
+LSM303AGRMagSensor::LSM303AGRMagSensor(DevI2C &i2c, uint8_t address) : dev_i2c(i2c), address(address)
+{
+
+};
+
+/**
+ * @brief     Initializing the component.
+ * @param[in] init pointer to device specific initalization structure.
+ * @retval    "0" in case of success, an error code otherwise.
+ */
+int LSM303AGRMagSensor::init(void *init)
+{
+  /* Operating mode selection - power down */
+  if ( LSM303AGR_MAG_W_MD( (void *)this, LSM303AGR_MAG_MD_IDLE1_MODE ) == MEMS_ERROR )
+  {
+    return 1;
+  }
+  
+  /* Enable BDU */
+  if ( LSM303AGR_MAG_W_BDU( (void *)this, LSM303AGR_MAG_BDU_ENABLED ) == MEMS_ERROR )
+  {
+    return 1;
+  }
+  
+  if ( set_m_odr( 100.0f ) == 1 )
+  {
+    return 1;
+  }
+  
+  if ( set_m_fs( 50.0f ) == 1 )
+  {
+    return 1;
+  }
+
+  if ( LSM303AGR_MAG_W_ST( (void *)this, LSM303AGR_MAG_ST_DISABLED ) == MEMS_ERROR )
+  {
+    return 1;
+  }
+  
+  return 0;
+}
+
+/**
+ * @brief  Enable LSM303AGR magnetometer
+ * @retval 0 in case of success, an error code otherwise
+ */
+int LSM303AGRMagSensor::Enable(void)
+{
+  /* Operating mode selection */
+  if ( LSM303AGR_MAG_W_MD( (void *)this, LSM303AGR_MAG_MD_CONTINUOS_MODE ) == MEMS_ERROR )
+  {
+    return 1;
+  }
+  
+  return 0;
+}
+
+/**
+ * @brief  Disable LSM303AGR magnetometer
+ * @retval 0 in case of success, an error code otherwise
+ */
+int LSM303AGRMagSensor::Disable(void)
+{
+  /* Operating mode selection - power down */
+  if ( LSM303AGR_MAG_W_MD( (void *)this, LSM303AGR_MAG_MD_IDLE1_MODE ) == MEMS_ERROR )
+  {
+    return 1;
+  }
+  
+  return 0;
+}
+
+/**
+ * @brief  Read ID of LSM303AGR Magnetometer
+ * @param  p_id the pointer where the ID of the device is stored
+ * @retval 0 in case of success, an error code otherwise
+ */
+int LSM303AGRMagSensor::read_id(uint8_t *id)
+{
+  if(!id)
+  { 
+    return 1; 
+  }
+ 
+  /* Read WHO AM I register */
+  if ( LSM303AGR_MAG_R_WHO_AM_I( (void *)this, id ) == MEMS_ERROR )
+  {
+    return 1;
+  }
+  
+  return 0;
+}
+
+/**
+ * @brief  Read data from LSM303AGR Magnetometer
+ * @param  pData the pointer where the magnetometer data are stored
+ * @retval 0 in case of success, an error code otherwise
+ */
+int LSM303AGRMagSensor::get_m_axes(int32_t *pData)
+{
+  int16_t pDataRaw[3];
+  float sensitivity = 0;
+  
+  /* Read raw data from LSM303AGR output register. */
+  if ( get_m_axesRaw( pDataRaw ) == 1 )
+  {
+    return 1;
+  }
+  
+  /* Get LSM303AGR actual sensitivity. */
+  if ( get_m_sensitivity( &sensitivity ) == 1 )
+  {
+    return 1;
+  }
+  
+  /* Calculate the data. */
+  pData[0] = ( int32_t )( pDataRaw[0] * sensitivity );
+  pData[1] = ( int32_t )( pDataRaw[1] * sensitivity );
+  pData[2] = ( int32_t )( pDataRaw[2] * sensitivity );
+  
+  return 0;
+}
+
+/**
+ * @brief  Read Magnetometer Sensitivity
+ * @param  pfData the pointer where the magnetometer sensitivity is stored
+ * @retval 0 in case of success, an error code otherwise
+ */
+int LSM303AGRMagSensor::get_m_sensitivity(float *pfData)
+{
+  *pfData = 1.5f;
+  
+  return 0;
+}
+
+/**
+ * @brief  Read raw data from LSM303AGR Magnetometer
+ * @param  pData the pointer where the magnetomer raw data are stored
+ * @retval 0 in case of success, an error code otherwise
+ */
+int LSM303AGRMagSensor::get_m_axesRaw(int16_t *pData)
+{
+  uint8_t regValue[6] = {0, 0, 0, 0, 0, 0};
+  int16_t *regValueInt16;
+  
+  /* Read output registers from LSM303AGR_MAG_OUTX_L to LSM303AGR_MAG_OUTZ_H. */
+  if ( LSM303AGR_MAG_Get_Raw_Magnetic( (void *)this, regValue ) == MEMS_ERROR )
+  {
+    return 1;
+  }
+  
+  regValueInt16 = (int16_t *)regValue;
+  
+  /* Format the data. */
+  pData[0] = regValueInt16[0];
+  pData[1] = regValueInt16[1];
+  pData[2] = regValueInt16[2];
+  
+  return 0;
+}
+
+/**
+ * @brief  Read LSM303AGR Magnetometer output data rate
+ * @param  odr the pointer to the output data rate
+ * @retval 0 in case of success, an error code otherwise
+ */
+int LSM303AGRMagSensor::get_m_odr(float* odr)
+{
+  LSM303AGR_MAG_ODR_t odr_low_level;
+  
+  if ( LSM303AGR_MAG_R_ODR( (void *)this, &odr_low_level ) == MEMS_ERROR )
+  {
+    return 1;
+  }
+  
+  switch( odr_low_level )
+  {
+    case LSM303AGR_MAG_ODR_10Hz:
+      *odr = 10.000f;
+      break;
+    case LSM303AGR_MAG_ODR_20Hz:
+      *odr = 20.000f;
+      break;
+    case LSM303AGR_MAG_ODR_50Hz:
+      *odr = 50.000f;
+      break;
+    case LSM303AGR_MAG_ODR_100Hz:
+      *odr = 100.000f;
+      break;
+    default:
+      *odr = -1.000f;
+      return 1;
+  }  
+  return 0;
+}
+
+/**
+ * @brief  Set ODR
+ * @param  odr the output data rate to be set
+ * @retval 0 in case of success, an error code otherwise
+ */
+int LSM303AGRMagSensor::set_m_odr(float odr)
+{
+  LSM303AGR_MAG_ODR_t new_odr;
+  
+  new_odr = ( odr <= 10.000f ) ? LSM303AGR_MAG_ODR_10Hz
+          : ( odr <= 20.000f ) ? LSM303AGR_MAG_ODR_20Hz
+          : ( odr <= 50.000f ) ? LSM303AGR_MAG_ODR_50Hz
+          :                      LSM303AGR_MAG_ODR_100Hz;
+            
+  if ( LSM303AGR_MAG_W_ODR( (void *)this, new_odr ) == MEMS_ERROR )
+  {
+    return 1;
+  }
+  
+  return 0;
+}
+
+
+/**
+ * @brief  Read LSM303AGR Magnetometer full scale
+ * @param  fullScale the pointer to the output data rate
+ * @retval 0 in case of success, an error code otherwise
+ */
+int LSM303AGRMagSensor::get_m_fs(float* fullScale)
+{
+  *fullScale = 50.0f;
+  
+  return 0;
+}
+
+/**
+ * @brief  Set full scale
+ * @param  fullScale the full scale to be set
+ * @retval 0 in case of success, an error code otherwise
+ */
+int LSM303AGRMagSensor::set_m_fs(float fullScale)
+{
+  return 0;
+}
+
+
+/**
+ * @brief Read magnetometer data from register
+ * @param reg register address
+ * @param data register data
+ * @retval 0 in case of success
+ * @retval 1 in case of failure
+ */
+int LSM303AGRMagSensor::read_reg( uint8_t reg, uint8_t *data )
+{
+  if ( LSM303AGR_MAG_read_reg( (void *)this, reg, data ) == MEMS_ERROR )
+  {
+    return 1;
+  }
+
+  return 0;
+}
+
+
+/**
+ * @brief Write magnetometer data to register
+ * @param reg register address
+ * @param data register data
+ * @retval 0 in case of success
+ * @retval 1 in case of failure
+ */
+int LSM303AGRMagSensor::write_reg( uint8_t reg, uint8_t data )
+{
+  if ( LSM303AGR_MAG_write_reg( (void *)this, reg, data ) == MEMS_ERROR )
+  {
+    return 1;
+  }
+
+  return 0;
+}
+
+uint8_t LSM303AGR_MAG_io_write( void *handle, uint8_t WriteAddr, uint8_t *pBuffer, uint16_t nBytesToWrite )
+{
+  return ((LSM303AGRMagSensor *)handle)->io_write(pBuffer, WriteAddr, nBytesToWrite);
+}
+
+uint8_t LSM303AGR_MAG_io_read( void *handle, uint8_t ReadAddr, uint8_t *pBuffer, uint16_t nBytesToRead )
+{
+  return ((LSM303AGRMagSensor *)handle)->io_read(pBuffer, ReadAddr, nBytesToRead);
+}