Beta
Dependencies: ST_INTERFACES X_NUCLEO_COMMON
Fork of X_NUCLEO_IKS01A2 by
Components/LSM303AGRSensor/LSM303AGRMagSensor.cpp@9:038121268b07, 2017-03-14 (annotated)
- Committer:
- Davidroid
- Date:
- Tue Mar 14 13:30:55 2017 +0000
- Revision:
- 9:038121268b07
- Parent:
- 6:671fd10a51b7
Updated with the new version of the ST_INTERFACES library, and other typos corrected.
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
davide.aliprandi@st.com | 6:671fd10a51b7 | 1 | /** |
davide.aliprandi@st.com | 6:671fd10a51b7 | 2 | ****************************************************************************** |
davide.aliprandi@st.com | 6:671fd10a51b7 | 3 | * @file LSM303AGRMagSensor.cpp |
davide.aliprandi@st.com | 6:671fd10a51b7 | 4 | * @author CLab |
davide.aliprandi@st.com | 6:671fd10a51b7 | 5 | * @version V1.0.0 |
davide.aliprandi@st.com | 6:671fd10a51b7 | 6 | * @date 5 August 2016 |
davide.aliprandi@st.com | 6:671fd10a51b7 | 7 | * @brief Implementation an LSM303AGR magnetometer sensor. |
davide.aliprandi@st.com | 6:671fd10a51b7 | 8 | ****************************************************************************** |
davide.aliprandi@st.com | 6:671fd10a51b7 | 9 | * @attention |
davide.aliprandi@st.com | 6:671fd10a51b7 | 10 | * |
davide.aliprandi@st.com | 6:671fd10a51b7 | 11 | * <h2><center>© COPYRIGHT(c) 2016 STMicroelectronics</center></h2> |
davide.aliprandi@st.com | 6:671fd10a51b7 | 12 | * |
davide.aliprandi@st.com | 6:671fd10a51b7 | 13 | * Redistribution and use in source and binary forms, with or without modification, |
davide.aliprandi@st.com | 6:671fd10a51b7 | 14 | * are permitted provided that the following conditions are met: |
davide.aliprandi@st.com | 6:671fd10a51b7 | 15 | * 1. Redistributions of source code must retain the above copyright notice, |
davide.aliprandi@st.com | 6:671fd10a51b7 | 16 | * this list of conditions and the following disclaimer. |
davide.aliprandi@st.com | 6:671fd10a51b7 | 17 | * 2. Redistributions in binary form must reproduce the above copyright notice, |
davide.aliprandi@st.com | 6:671fd10a51b7 | 18 | * this list of conditions and the following disclaimer in the documentation |
davide.aliprandi@st.com | 6:671fd10a51b7 | 19 | * and/or other materials provided with the distribution. |
davide.aliprandi@st.com | 6:671fd10a51b7 | 20 | * 3. Neither the name of STMicroelectronics nor the names of its contributors |
davide.aliprandi@st.com | 6:671fd10a51b7 | 21 | * may be used to endorse or promote products derived from this software |
davide.aliprandi@st.com | 6:671fd10a51b7 | 22 | * without specific prior written permission. |
davide.aliprandi@st.com | 6:671fd10a51b7 | 23 | * |
davide.aliprandi@st.com | 6:671fd10a51b7 | 24 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" |
davide.aliprandi@st.com | 6:671fd10a51b7 | 25 | * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
davide.aliprandi@st.com | 6:671fd10a51b7 | 26 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE |
davide.aliprandi@st.com | 6:671fd10a51b7 | 27 | * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE |
davide.aliprandi@st.com | 6:671fd10a51b7 | 28 | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL |
davide.aliprandi@st.com | 6:671fd10a51b7 | 29 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR |
davide.aliprandi@st.com | 6:671fd10a51b7 | 30 | * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER |
davide.aliprandi@st.com | 6:671fd10a51b7 | 31 | * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, |
davide.aliprandi@st.com | 6:671fd10a51b7 | 32 | * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
davide.aliprandi@st.com | 6:671fd10a51b7 | 33 | * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
davide.aliprandi@st.com | 6:671fd10a51b7 | 34 | * |
davide.aliprandi@st.com | 6:671fd10a51b7 | 35 | ****************************************************************************** |
davide.aliprandi@st.com | 6:671fd10a51b7 | 36 | */ |
davide.aliprandi@st.com | 6:671fd10a51b7 | 37 | |
davide.aliprandi@st.com | 6:671fd10a51b7 | 38 | |
davide.aliprandi@st.com | 6:671fd10a51b7 | 39 | /* Includes ------------------------------------------------------------------*/ |
davide.aliprandi@st.com | 6:671fd10a51b7 | 40 | |
davide.aliprandi@st.com | 6:671fd10a51b7 | 41 | #include "mbed.h" |
davide.aliprandi@st.com | 6:671fd10a51b7 | 42 | #include "DevI2C.h" |
davide.aliprandi@st.com | 6:671fd10a51b7 | 43 | #include "LSM303AGRMagSensor.h" |
davide.aliprandi@st.com | 6:671fd10a51b7 | 44 | #include "LSM303AGR_mag_driver.h" |
davide.aliprandi@st.com | 6:671fd10a51b7 | 45 | |
davide.aliprandi@st.com | 6:671fd10a51b7 | 46 | |
davide.aliprandi@st.com | 6:671fd10a51b7 | 47 | /* Class Implementation ------------------------------------------------------*/ |
davide.aliprandi@st.com | 6:671fd10a51b7 | 48 | |
davide.aliprandi@st.com | 6:671fd10a51b7 | 49 | /** Constructor |
davide.aliprandi@st.com | 6:671fd10a51b7 | 50 | * @param i2c object of an helper class which handles the I2C peripheral |
davide.aliprandi@st.com | 6:671fd10a51b7 | 51 | * @param address the address of the component's instance |
davide.aliprandi@st.com | 6:671fd10a51b7 | 52 | */ |
Davidroid | 9:038121268b07 | 53 | LSM303AGRMagSensor::LSM303AGRMagSensor(DevI2C &i2c) : _dev_i2c(i2c) |
davide.aliprandi@st.com | 6:671fd10a51b7 | 54 | { |
Davidroid | 9:038121268b07 | 55 | _address = LSM303AGR_MAG_I2C_ADDRESS; |
davide.aliprandi@st.com | 6:671fd10a51b7 | 56 | }; |
davide.aliprandi@st.com | 6:671fd10a51b7 | 57 | |
davide.aliprandi@st.com | 6:671fd10a51b7 | 58 | /** Constructor |
davide.aliprandi@st.com | 6:671fd10a51b7 | 59 | * @param i2c object of an helper class which handles the I2C peripheral |
davide.aliprandi@st.com | 6:671fd10a51b7 | 60 | * @param address the address of the component's instance |
davide.aliprandi@st.com | 6:671fd10a51b7 | 61 | */ |
Davidroid | 9:038121268b07 | 62 | LSM303AGRMagSensor::LSM303AGRMagSensor(DevI2C &i2c, uint8_t address) : _dev_i2c(i2c), _address(address) |
davide.aliprandi@st.com | 6:671fd10a51b7 | 63 | { |
davide.aliprandi@st.com | 6:671fd10a51b7 | 64 | |
davide.aliprandi@st.com | 6:671fd10a51b7 | 65 | }; |
davide.aliprandi@st.com | 6:671fd10a51b7 | 66 | |
davide.aliprandi@st.com | 6:671fd10a51b7 | 67 | /** |
davide.aliprandi@st.com | 6:671fd10a51b7 | 68 | * @brief Initializing the component. |
davide.aliprandi@st.com | 6:671fd10a51b7 | 69 | * @param[in] init pointer to device specific initalization structure. |
davide.aliprandi@st.com | 6:671fd10a51b7 | 70 | * @retval "0" in case of success, an error code otherwise. |
davide.aliprandi@st.com | 6:671fd10a51b7 | 71 | */ |
davide.aliprandi@st.com | 6:671fd10a51b7 | 72 | int LSM303AGRMagSensor::init(void *init) |
davide.aliprandi@st.com | 6:671fd10a51b7 | 73 | { |
davide.aliprandi@st.com | 6:671fd10a51b7 | 74 | /* Operating mode selection - power down */ |
davide.aliprandi@st.com | 6:671fd10a51b7 | 75 | if ( LSM303AGR_MAG_W_MD( (void *)this, LSM303AGR_MAG_MD_IDLE1_MODE ) == MEMS_ERROR ) |
davide.aliprandi@st.com | 6:671fd10a51b7 | 76 | { |
davide.aliprandi@st.com | 6:671fd10a51b7 | 77 | return 1; |
davide.aliprandi@st.com | 6:671fd10a51b7 | 78 | } |
davide.aliprandi@st.com | 6:671fd10a51b7 | 79 | |
davide.aliprandi@st.com | 6:671fd10a51b7 | 80 | /* Enable BDU */ |
davide.aliprandi@st.com | 6:671fd10a51b7 | 81 | if ( LSM303AGR_MAG_W_BDU( (void *)this, LSM303AGR_MAG_BDU_ENABLED ) == MEMS_ERROR ) |
davide.aliprandi@st.com | 6:671fd10a51b7 | 82 | { |
davide.aliprandi@st.com | 6:671fd10a51b7 | 83 | return 1; |
davide.aliprandi@st.com | 6:671fd10a51b7 | 84 | } |
davide.aliprandi@st.com | 6:671fd10a51b7 | 85 | |
davide.aliprandi@st.com | 6:671fd10a51b7 | 86 | if ( set_m_odr( 100.0f ) == 1 ) |
davide.aliprandi@st.com | 6:671fd10a51b7 | 87 | { |
davide.aliprandi@st.com | 6:671fd10a51b7 | 88 | return 1; |
davide.aliprandi@st.com | 6:671fd10a51b7 | 89 | } |
davide.aliprandi@st.com | 6:671fd10a51b7 | 90 | |
davide.aliprandi@st.com | 6:671fd10a51b7 | 91 | if ( set_m_fs( 50.0f ) == 1 ) |
davide.aliprandi@st.com | 6:671fd10a51b7 | 92 | { |
davide.aliprandi@st.com | 6:671fd10a51b7 | 93 | return 1; |
davide.aliprandi@st.com | 6:671fd10a51b7 | 94 | } |
davide.aliprandi@st.com | 6:671fd10a51b7 | 95 | |
davide.aliprandi@st.com | 6:671fd10a51b7 | 96 | if ( LSM303AGR_MAG_W_ST( (void *)this, LSM303AGR_MAG_ST_DISABLED ) == MEMS_ERROR ) |
davide.aliprandi@st.com | 6:671fd10a51b7 | 97 | { |
davide.aliprandi@st.com | 6:671fd10a51b7 | 98 | return 1; |
davide.aliprandi@st.com | 6:671fd10a51b7 | 99 | } |
davide.aliprandi@st.com | 6:671fd10a51b7 | 100 | |
davide.aliprandi@st.com | 6:671fd10a51b7 | 101 | return 0; |
davide.aliprandi@st.com | 6:671fd10a51b7 | 102 | } |
davide.aliprandi@st.com | 6:671fd10a51b7 | 103 | |
davide.aliprandi@st.com | 6:671fd10a51b7 | 104 | /** |
davide.aliprandi@st.com | 6:671fd10a51b7 | 105 | * @brief Enable LSM303AGR magnetometer |
davide.aliprandi@st.com | 6:671fd10a51b7 | 106 | * @retval 0 in case of success, an error code otherwise |
davide.aliprandi@st.com | 6:671fd10a51b7 | 107 | */ |
Davidroid | 9:038121268b07 | 108 | int LSM303AGRMagSensor::enable(void) |
davide.aliprandi@st.com | 6:671fd10a51b7 | 109 | { |
davide.aliprandi@st.com | 6:671fd10a51b7 | 110 | /* Operating mode selection */ |
davide.aliprandi@st.com | 6:671fd10a51b7 | 111 | if ( LSM303AGR_MAG_W_MD( (void *)this, LSM303AGR_MAG_MD_CONTINUOS_MODE ) == MEMS_ERROR ) |
davide.aliprandi@st.com | 6:671fd10a51b7 | 112 | { |
davide.aliprandi@st.com | 6:671fd10a51b7 | 113 | return 1; |
davide.aliprandi@st.com | 6:671fd10a51b7 | 114 | } |
davide.aliprandi@st.com | 6:671fd10a51b7 | 115 | |
davide.aliprandi@st.com | 6:671fd10a51b7 | 116 | return 0; |
davide.aliprandi@st.com | 6:671fd10a51b7 | 117 | } |
davide.aliprandi@st.com | 6:671fd10a51b7 | 118 | |
davide.aliprandi@st.com | 6:671fd10a51b7 | 119 | /** |
davide.aliprandi@st.com | 6:671fd10a51b7 | 120 | * @brief Disable LSM303AGR magnetometer |
davide.aliprandi@st.com | 6:671fd10a51b7 | 121 | * @retval 0 in case of success, an error code otherwise |
davide.aliprandi@st.com | 6:671fd10a51b7 | 122 | */ |
Davidroid | 9:038121268b07 | 123 | int LSM303AGRMagSensor::disable(void) |
davide.aliprandi@st.com | 6:671fd10a51b7 | 124 | { |
davide.aliprandi@st.com | 6:671fd10a51b7 | 125 | /* Operating mode selection - power down */ |
davide.aliprandi@st.com | 6:671fd10a51b7 | 126 | if ( LSM303AGR_MAG_W_MD( (void *)this, LSM303AGR_MAG_MD_IDLE1_MODE ) == MEMS_ERROR ) |
davide.aliprandi@st.com | 6:671fd10a51b7 | 127 | { |
davide.aliprandi@st.com | 6:671fd10a51b7 | 128 | return 1; |
davide.aliprandi@st.com | 6:671fd10a51b7 | 129 | } |
davide.aliprandi@st.com | 6:671fd10a51b7 | 130 | |
davide.aliprandi@st.com | 6:671fd10a51b7 | 131 | return 0; |
davide.aliprandi@st.com | 6:671fd10a51b7 | 132 | } |
davide.aliprandi@st.com | 6:671fd10a51b7 | 133 | |
davide.aliprandi@st.com | 6:671fd10a51b7 | 134 | /** |
davide.aliprandi@st.com | 6:671fd10a51b7 | 135 | * @brief Read ID of LSM303AGR Magnetometer |
davide.aliprandi@st.com | 6:671fd10a51b7 | 136 | * @param p_id the pointer where the ID of the device is stored |
davide.aliprandi@st.com | 6:671fd10a51b7 | 137 | * @retval 0 in case of success, an error code otherwise |
davide.aliprandi@st.com | 6:671fd10a51b7 | 138 | */ |
davide.aliprandi@st.com | 6:671fd10a51b7 | 139 | int LSM303AGRMagSensor::read_id(uint8_t *id) |
davide.aliprandi@st.com | 6:671fd10a51b7 | 140 | { |
davide.aliprandi@st.com | 6:671fd10a51b7 | 141 | if(!id) |
davide.aliprandi@st.com | 6:671fd10a51b7 | 142 | { |
davide.aliprandi@st.com | 6:671fd10a51b7 | 143 | return 1; |
davide.aliprandi@st.com | 6:671fd10a51b7 | 144 | } |
davide.aliprandi@st.com | 6:671fd10a51b7 | 145 | |
davide.aliprandi@st.com | 6:671fd10a51b7 | 146 | /* Read WHO AM I register */ |
davide.aliprandi@st.com | 6:671fd10a51b7 | 147 | if ( LSM303AGR_MAG_R_WHO_AM_I( (void *)this, id ) == MEMS_ERROR ) |
davide.aliprandi@st.com | 6:671fd10a51b7 | 148 | { |
davide.aliprandi@st.com | 6:671fd10a51b7 | 149 | return 1; |
davide.aliprandi@st.com | 6:671fd10a51b7 | 150 | } |
davide.aliprandi@st.com | 6:671fd10a51b7 | 151 | |
davide.aliprandi@st.com | 6:671fd10a51b7 | 152 | return 0; |
davide.aliprandi@st.com | 6:671fd10a51b7 | 153 | } |
davide.aliprandi@st.com | 6:671fd10a51b7 | 154 | |
davide.aliprandi@st.com | 6:671fd10a51b7 | 155 | /** |
davide.aliprandi@st.com | 6:671fd10a51b7 | 156 | * @brief Read data from LSM303AGR Magnetometer |
davide.aliprandi@st.com | 6:671fd10a51b7 | 157 | * @param pData the pointer where the magnetometer data are stored |
davide.aliprandi@st.com | 6:671fd10a51b7 | 158 | * @retval 0 in case of success, an error code otherwise |
davide.aliprandi@st.com | 6:671fd10a51b7 | 159 | */ |
davide.aliprandi@st.com | 6:671fd10a51b7 | 160 | int LSM303AGRMagSensor::get_m_axes(int32_t *pData) |
davide.aliprandi@st.com | 6:671fd10a51b7 | 161 | { |
davide.aliprandi@st.com | 6:671fd10a51b7 | 162 | int16_t pDataRaw[3]; |
davide.aliprandi@st.com | 6:671fd10a51b7 | 163 | float sensitivity = 0; |
davide.aliprandi@st.com | 6:671fd10a51b7 | 164 | |
davide.aliprandi@st.com | 6:671fd10a51b7 | 165 | /* Read raw data from LSM303AGR output register. */ |
Davidroid | 9:038121268b07 | 166 | if ( get_m_axes_raw( pDataRaw ) == 1 ) |
davide.aliprandi@st.com | 6:671fd10a51b7 | 167 | { |
davide.aliprandi@st.com | 6:671fd10a51b7 | 168 | return 1; |
davide.aliprandi@st.com | 6:671fd10a51b7 | 169 | } |
davide.aliprandi@st.com | 6:671fd10a51b7 | 170 | |
davide.aliprandi@st.com | 6:671fd10a51b7 | 171 | /* Get LSM303AGR actual sensitivity. */ |
davide.aliprandi@st.com | 6:671fd10a51b7 | 172 | if ( get_m_sensitivity( &sensitivity ) == 1 ) |
davide.aliprandi@st.com | 6:671fd10a51b7 | 173 | { |
davide.aliprandi@st.com | 6:671fd10a51b7 | 174 | return 1; |
davide.aliprandi@st.com | 6:671fd10a51b7 | 175 | } |
davide.aliprandi@st.com | 6:671fd10a51b7 | 176 | |
davide.aliprandi@st.com | 6:671fd10a51b7 | 177 | /* Calculate the data. */ |
davide.aliprandi@st.com | 6:671fd10a51b7 | 178 | pData[0] = ( int32_t )( pDataRaw[0] * sensitivity ); |
davide.aliprandi@st.com | 6:671fd10a51b7 | 179 | pData[1] = ( int32_t )( pDataRaw[1] * sensitivity ); |
davide.aliprandi@st.com | 6:671fd10a51b7 | 180 | pData[2] = ( int32_t )( pDataRaw[2] * sensitivity ); |
davide.aliprandi@st.com | 6:671fd10a51b7 | 181 | |
davide.aliprandi@st.com | 6:671fd10a51b7 | 182 | return 0; |
davide.aliprandi@st.com | 6:671fd10a51b7 | 183 | } |
davide.aliprandi@st.com | 6:671fd10a51b7 | 184 | |
davide.aliprandi@st.com | 6:671fd10a51b7 | 185 | /** |
davide.aliprandi@st.com | 6:671fd10a51b7 | 186 | * @brief Read Magnetometer Sensitivity |
davide.aliprandi@st.com | 6:671fd10a51b7 | 187 | * @param pfData the pointer where the magnetometer sensitivity is stored |
davide.aliprandi@st.com | 6:671fd10a51b7 | 188 | * @retval 0 in case of success, an error code otherwise |
davide.aliprandi@st.com | 6:671fd10a51b7 | 189 | */ |
davide.aliprandi@st.com | 6:671fd10a51b7 | 190 | int LSM303AGRMagSensor::get_m_sensitivity(float *pfData) |
davide.aliprandi@st.com | 6:671fd10a51b7 | 191 | { |
davide.aliprandi@st.com | 6:671fd10a51b7 | 192 | *pfData = 1.5f; |
davide.aliprandi@st.com | 6:671fd10a51b7 | 193 | |
davide.aliprandi@st.com | 6:671fd10a51b7 | 194 | return 0; |
davide.aliprandi@st.com | 6:671fd10a51b7 | 195 | } |
davide.aliprandi@st.com | 6:671fd10a51b7 | 196 | |
davide.aliprandi@st.com | 6:671fd10a51b7 | 197 | /** |
davide.aliprandi@st.com | 6:671fd10a51b7 | 198 | * @brief Read raw data from LSM303AGR Magnetometer |
davide.aliprandi@st.com | 6:671fd10a51b7 | 199 | * @param pData the pointer where the magnetomer raw data are stored |
davide.aliprandi@st.com | 6:671fd10a51b7 | 200 | * @retval 0 in case of success, an error code otherwise |
davide.aliprandi@st.com | 6:671fd10a51b7 | 201 | */ |
Davidroid | 9:038121268b07 | 202 | int LSM303AGRMagSensor::get_m_axes_raw(int16_t *pData) |
davide.aliprandi@st.com | 6:671fd10a51b7 | 203 | { |
davide.aliprandi@st.com | 6:671fd10a51b7 | 204 | uint8_t regValue[6] = {0, 0, 0, 0, 0, 0}; |
davide.aliprandi@st.com | 6:671fd10a51b7 | 205 | int16_t *regValueInt16; |
davide.aliprandi@st.com | 6:671fd10a51b7 | 206 | |
davide.aliprandi@st.com | 6:671fd10a51b7 | 207 | /* Read output registers from LSM303AGR_MAG_OUTX_L to LSM303AGR_MAG_OUTZ_H. */ |
davide.aliprandi@st.com | 6:671fd10a51b7 | 208 | if ( LSM303AGR_MAG_Get_Raw_Magnetic( (void *)this, regValue ) == MEMS_ERROR ) |
davide.aliprandi@st.com | 6:671fd10a51b7 | 209 | { |
davide.aliprandi@st.com | 6:671fd10a51b7 | 210 | return 1; |
davide.aliprandi@st.com | 6:671fd10a51b7 | 211 | } |
davide.aliprandi@st.com | 6:671fd10a51b7 | 212 | |
davide.aliprandi@st.com | 6:671fd10a51b7 | 213 | regValueInt16 = (int16_t *)regValue; |
davide.aliprandi@st.com | 6:671fd10a51b7 | 214 | |
davide.aliprandi@st.com | 6:671fd10a51b7 | 215 | /* Format the data. */ |
davide.aliprandi@st.com | 6:671fd10a51b7 | 216 | pData[0] = regValueInt16[0]; |
davide.aliprandi@st.com | 6:671fd10a51b7 | 217 | pData[1] = regValueInt16[1]; |
davide.aliprandi@st.com | 6:671fd10a51b7 | 218 | pData[2] = regValueInt16[2]; |
davide.aliprandi@st.com | 6:671fd10a51b7 | 219 | |
davide.aliprandi@st.com | 6:671fd10a51b7 | 220 | return 0; |
davide.aliprandi@st.com | 6:671fd10a51b7 | 221 | } |
davide.aliprandi@st.com | 6:671fd10a51b7 | 222 | |
davide.aliprandi@st.com | 6:671fd10a51b7 | 223 | /** |
davide.aliprandi@st.com | 6:671fd10a51b7 | 224 | * @brief Read LSM303AGR Magnetometer output data rate |
davide.aliprandi@st.com | 6:671fd10a51b7 | 225 | * @param odr the pointer to the output data rate |
davide.aliprandi@st.com | 6:671fd10a51b7 | 226 | * @retval 0 in case of success, an error code otherwise |
davide.aliprandi@st.com | 6:671fd10a51b7 | 227 | */ |
davide.aliprandi@st.com | 6:671fd10a51b7 | 228 | int LSM303AGRMagSensor::get_m_odr(float* odr) |
davide.aliprandi@st.com | 6:671fd10a51b7 | 229 | { |
davide.aliprandi@st.com | 6:671fd10a51b7 | 230 | LSM303AGR_MAG_ODR_t odr_low_level; |
davide.aliprandi@st.com | 6:671fd10a51b7 | 231 | |
davide.aliprandi@st.com | 6:671fd10a51b7 | 232 | if ( LSM303AGR_MAG_R_ODR( (void *)this, &odr_low_level ) == MEMS_ERROR ) |
davide.aliprandi@st.com | 6:671fd10a51b7 | 233 | { |
davide.aliprandi@st.com | 6:671fd10a51b7 | 234 | return 1; |
davide.aliprandi@st.com | 6:671fd10a51b7 | 235 | } |
davide.aliprandi@st.com | 6:671fd10a51b7 | 236 | |
davide.aliprandi@st.com | 6:671fd10a51b7 | 237 | switch( odr_low_level ) |
davide.aliprandi@st.com | 6:671fd10a51b7 | 238 | { |
davide.aliprandi@st.com | 6:671fd10a51b7 | 239 | case LSM303AGR_MAG_ODR_10Hz: |
davide.aliprandi@st.com | 6:671fd10a51b7 | 240 | *odr = 10.000f; |
davide.aliprandi@st.com | 6:671fd10a51b7 | 241 | break; |
davide.aliprandi@st.com | 6:671fd10a51b7 | 242 | case LSM303AGR_MAG_ODR_20Hz: |
davide.aliprandi@st.com | 6:671fd10a51b7 | 243 | *odr = 20.000f; |
davide.aliprandi@st.com | 6:671fd10a51b7 | 244 | break; |
davide.aliprandi@st.com | 6:671fd10a51b7 | 245 | case LSM303AGR_MAG_ODR_50Hz: |
davide.aliprandi@st.com | 6:671fd10a51b7 | 246 | *odr = 50.000f; |
davide.aliprandi@st.com | 6:671fd10a51b7 | 247 | break; |
davide.aliprandi@st.com | 6:671fd10a51b7 | 248 | case LSM303AGR_MAG_ODR_100Hz: |
davide.aliprandi@st.com | 6:671fd10a51b7 | 249 | *odr = 100.000f; |
davide.aliprandi@st.com | 6:671fd10a51b7 | 250 | break; |
davide.aliprandi@st.com | 6:671fd10a51b7 | 251 | default: |
davide.aliprandi@st.com | 6:671fd10a51b7 | 252 | *odr = -1.000f; |
davide.aliprandi@st.com | 6:671fd10a51b7 | 253 | return 1; |
davide.aliprandi@st.com | 6:671fd10a51b7 | 254 | } |
davide.aliprandi@st.com | 6:671fd10a51b7 | 255 | return 0; |
davide.aliprandi@st.com | 6:671fd10a51b7 | 256 | } |
davide.aliprandi@st.com | 6:671fd10a51b7 | 257 | |
davide.aliprandi@st.com | 6:671fd10a51b7 | 258 | /** |
davide.aliprandi@st.com | 6:671fd10a51b7 | 259 | * @brief Set ODR |
davide.aliprandi@st.com | 6:671fd10a51b7 | 260 | * @param odr the output data rate to be set |
davide.aliprandi@st.com | 6:671fd10a51b7 | 261 | * @retval 0 in case of success, an error code otherwise |
davide.aliprandi@st.com | 6:671fd10a51b7 | 262 | */ |
davide.aliprandi@st.com | 6:671fd10a51b7 | 263 | int LSM303AGRMagSensor::set_m_odr(float odr) |
davide.aliprandi@st.com | 6:671fd10a51b7 | 264 | { |
davide.aliprandi@st.com | 6:671fd10a51b7 | 265 | LSM303AGR_MAG_ODR_t new_odr; |
davide.aliprandi@st.com | 6:671fd10a51b7 | 266 | |
davide.aliprandi@st.com | 6:671fd10a51b7 | 267 | new_odr = ( odr <= 10.000f ) ? LSM303AGR_MAG_ODR_10Hz |
davide.aliprandi@st.com | 6:671fd10a51b7 | 268 | : ( odr <= 20.000f ) ? LSM303AGR_MAG_ODR_20Hz |
davide.aliprandi@st.com | 6:671fd10a51b7 | 269 | : ( odr <= 50.000f ) ? LSM303AGR_MAG_ODR_50Hz |
davide.aliprandi@st.com | 6:671fd10a51b7 | 270 | : LSM303AGR_MAG_ODR_100Hz; |
davide.aliprandi@st.com | 6:671fd10a51b7 | 271 | |
davide.aliprandi@st.com | 6:671fd10a51b7 | 272 | if ( LSM303AGR_MAG_W_ODR( (void *)this, new_odr ) == MEMS_ERROR ) |
davide.aliprandi@st.com | 6:671fd10a51b7 | 273 | { |
davide.aliprandi@st.com | 6:671fd10a51b7 | 274 | return 1; |
davide.aliprandi@st.com | 6:671fd10a51b7 | 275 | } |
davide.aliprandi@st.com | 6:671fd10a51b7 | 276 | |
davide.aliprandi@st.com | 6:671fd10a51b7 | 277 | return 0; |
davide.aliprandi@st.com | 6:671fd10a51b7 | 278 | } |
davide.aliprandi@st.com | 6:671fd10a51b7 | 279 | |
davide.aliprandi@st.com | 6:671fd10a51b7 | 280 | |
davide.aliprandi@st.com | 6:671fd10a51b7 | 281 | /** |
davide.aliprandi@st.com | 6:671fd10a51b7 | 282 | * @brief Read LSM303AGR Magnetometer full scale |
davide.aliprandi@st.com | 6:671fd10a51b7 | 283 | * @param fullScale the pointer to the output data rate |
davide.aliprandi@st.com | 6:671fd10a51b7 | 284 | * @retval 0 in case of success, an error code otherwise |
davide.aliprandi@st.com | 6:671fd10a51b7 | 285 | */ |
davide.aliprandi@st.com | 6:671fd10a51b7 | 286 | int LSM303AGRMagSensor::get_m_fs(float* fullScale) |
davide.aliprandi@st.com | 6:671fd10a51b7 | 287 | { |
davide.aliprandi@st.com | 6:671fd10a51b7 | 288 | *fullScale = 50.0f; |
davide.aliprandi@st.com | 6:671fd10a51b7 | 289 | |
davide.aliprandi@st.com | 6:671fd10a51b7 | 290 | return 0; |
davide.aliprandi@st.com | 6:671fd10a51b7 | 291 | } |
davide.aliprandi@st.com | 6:671fd10a51b7 | 292 | |
davide.aliprandi@st.com | 6:671fd10a51b7 | 293 | /** |
davide.aliprandi@st.com | 6:671fd10a51b7 | 294 | * @brief Set full scale |
davide.aliprandi@st.com | 6:671fd10a51b7 | 295 | * @param fullScale the full scale to be set |
davide.aliprandi@st.com | 6:671fd10a51b7 | 296 | * @retval 0 in case of success, an error code otherwise |
davide.aliprandi@st.com | 6:671fd10a51b7 | 297 | */ |
davide.aliprandi@st.com | 6:671fd10a51b7 | 298 | int LSM303AGRMagSensor::set_m_fs(float fullScale) |
davide.aliprandi@st.com | 6:671fd10a51b7 | 299 | { |
davide.aliprandi@st.com | 6:671fd10a51b7 | 300 | return 0; |
davide.aliprandi@st.com | 6:671fd10a51b7 | 301 | } |
davide.aliprandi@st.com | 6:671fd10a51b7 | 302 | |
davide.aliprandi@st.com | 6:671fd10a51b7 | 303 | |
davide.aliprandi@st.com | 6:671fd10a51b7 | 304 | /** |
davide.aliprandi@st.com | 6:671fd10a51b7 | 305 | * @brief Read magnetometer data from register |
davide.aliprandi@st.com | 6:671fd10a51b7 | 306 | * @param reg register address |
davide.aliprandi@st.com | 6:671fd10a51b7 | 307 | * @param data register data |
davide.aliprandi@st.com | 6:671fd10a51b7 | 308 | * @retval 0 in case of success |
davide.aliprandi@st.com | 6:671fd10a51b7 | 309 | * @retval 1 in case of failure |
davide.aliprandi@st.com | 6:671fd10a51b7 | 310 | */ |
davide.aliprandi@st.com | 6:671fd10a51b7 | 311 | int LSM303AGRMagSensor::read_reg( uint8_t reg, uint8_t *data ) |
davide.aliprandi@st.com | 6:671fd10a51b7 | 312 | { |
davide.aliprandi@st.com | 6:671fd10a51b7 | 313 | if ( LSM303AGR_MAG_read_reg( (void *)this, reg, data ) == MEMS_ERROR ) |
davide.aliprandi@st.com | 6:671fd10a51b7 | 314 | { |
davide.aliprandi@st.com | 6:671fd10a51b7 | 315 | return 1; |
davide.aliprandi@st.com | 6:671fd10a51b7 | 316 | } |
davide.aliprandi@st.com | 6:671fd10a51b7 | 317 | |
davide.aliprandi@st.com | 6:671fd10a51b7 | 318 | return 0; |
davide.aliprandi@st.com | 6:671fd10a51b7 | 319 | } |
davide.aliprandi@st.com | 6:671fd10a51b7 | 320 | |
davide.aliprandi@st.com | 6:671fd10a51b7 | 321 | |
davide.aliprandi@st.com | 6:671fd10a51b7 | 322 | /** |
davide.aliprandi@st.com | 6:671fd10a51b7 | 323 | * @brief Write magnetometer data to register |
davide.aliprandi@st.com | 6:671fd10a51b7 | 324 | * @param reg register address |
davide.aliprandi@st.com | 6:671fd10a51b7 | 325 | * @param data register data |
davide.aliprandi@st.com | 6:671fd10a51b7 | 326 | * @retval 0 in case of success |
davide.aliprandi@st.com | 6:671fd10a51b7 | 327 | * @retval 1 in case of failure |
davide.aliprandi@st.com | 6:671fd10a51b7 | 328 | */ |
davide.aliprandi@st.com | 6:671fd10a51b7 | 329 | int LSM303AGRMagSensor::write_reg( uint8_t reg, uint8_t data ) |
davide.aliprandi@st.com | 6:671fd10a51b7 | 330 | { |
davide.aliprandi@st.com | 6:671fd10a51b7 | 331 | if ( LSM303AGR_MAG_write_reg( (void *)this, reg, data ) == MEMS_ERROR ) |
davide.aliprandi@st.com | 6:671fd10a51b7 | 332 | { |
davide.aliprandi@st.com | 6:671fd10a51b7 | 333 | return 1; |
davide.aliprandi@st.com | 6:671fd10a51b7 | 334 | } |
davide.aliprandi@st.com | 6:671fd10a51b7 | 335 | |
davide.aliprandi@st.com | 6:671fd10a51b7 | 336 | return 0; |
davide.aliprandi@st.com | 6:671fd10a51b7 | 337 | } |
davide.aliprandi@st.com | 6:671fd10a51b7 | 338 | |
davide.aliprandi@st.com | 6:671fd10a51b7 | 339 | uint8_t LSM303AGR_MAG_io_write( void *handle, uint8_t WriteAddr, uint8_t *pBuffer, uint16_t nBytesToWrite ) |
davide.aliprandi@st.com | 6:671fd10a51b7 | 340 | { |
davide.aliprandi@st.com | 6:671fd10a51b7 | 341 | return ((LSM303AGRMagSensor *)handle)->io_write(pBuffer, WriteAddr, nBytesToWrite); |
davide.aliprandi@st.com | 6:671fd10a51b7 | 342 | } |
davide.aliprandi@st.com | 6:671fd10a51b7 | 343 | |
davide.aliprandi@st.com | 6:671fd10a51b7 | 344 | uint8_t LSM303AGR_MAG_io_read( void *handle, uint8_t ReadAddr, uint8_t *pBuffer, uint16_t nBytesToRead ) |
davide.aliprandi@st.com | 6:671fd10a51b7 | 345 | { |
davide.aliprandi@st.com | 6:671fd10a51b7 | 346 | return ((LSM303AGRMagSensor *)handle)->io_read(pBuffer, ReadAddr, nBytesToRead); |
davide.aliprandi@st.com | 6:671fd10a51b7 | 347 | } |