Contains the BSP driver for the B-L475E-IOT01 board.
Dependents: mbed-os-example-ble-Thermometer DISCO_L475VG_IOT01-Telegram-BOT DISCO_L475VG_IOT01-sche_cheveux DISCO_L475VG_IOT01-QSPI_FLASH_FILE_SYSTEM ... more
https://os.mbed.com/platforms/ST-Discovery-L475E-IOT01A/
Drivers/BSP/Components/lis3mdl/lis3mdl.c@4:137bb367ba50, 2017-04-14 (annotated)
- Committer:
- bcostm
- Date:
- Fri Apr 14 13:09:54 2017 +0200
- Revision:
- 4:137bb367ba50
- Child:
- 7:bfe8272ced90
Add remaining Components files
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
bcostm | 4:137bb367ba50 | 1 | /** |
bcostm | 4:137bb367ba50 | 2 | ****************************************************************************** |
bcostm | 4:137bb367ba50 | 3 | * @file lis3mdl.c |
bcostm | 4:137bb367ba50 | 4 | * @author MCD Application Team |
bcostm | 4:137bb367ba50 | 5 | * @version V1.0.0 |
bcostm | 4:137bb367ba50 | 6 | * @date 14-February-2017 |
bcostm | 4:137bb367ba50 | 7 | * @brief This file provides a set of functions needed to manage the LIS3MDL |
bcostm | 4:137bb367ba50 | 8 | * magnetometer devices |
bcostm | 4:137bb367ba50 | 9 | ****************************************************************************** |
bcostm | 4:137bb367ba50 | 10 | * @attention |
bcostm | 4:137bb367ba50 | 11 | * |
bcostm | 4:137bb367ba50 | 12 | * <h2><center>© COPYRIGHT(c) 2017 STMicroelectronics</center></h2> |
bcostm | 4:137bb367ba50 | 13 | * |
bcostm | 4:137bb367ba50 | 14 | * Redistribution and use in source and binary forms, with or without modification, |
bcostm | 4:137bb367ba50 | 15 | * are permitted provided that the following conditions are met: |
bcostm | 4:137bb367ba50 | 16 | * 1. Redistributions of source code must retain the above copyright notice, |
bcostm | 4:137bb367ba50 | 17 | * this list of conditions and the following disclaimer. |
bcostm | 4:137bb367ba50 | 18 | * 2. Redistributions in binary form must reproduce the above copyright notice, |
bcostm | 4:137bb367ba50 | 19 | * this list of conditions and the following disclaimer in the documentation |
bcostm | 4:137bb367ba50 | 20 | * and/or other materials provided with the distribution. |
bcostm | 4:137bb367ba50 | 21 | * 3. Neither the name of STMicroelectronics nor the names of its contributors |
bcostm | 4:137bb367ba50 | 22 | * may be used to endorse or promote products derived from this software |
bcostm | 4:137bb367ba50 | 23 | * without specific prior written permission. |
bcostm | 4:137bb367ba50 | 24 | * |
bcostm | 4:137bb367ba50 | 25 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" |
bcostm | 4:137bb367ba50 | 26 | * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
bcostm | 4:137bb367ba50 | 27 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE |
bcostm | 4:137bb367ba50 | 28 | * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE |
bcostm | 4:137bb367ba50 | 29 | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL |
bcostm | 4:137bb367ba50 | 30 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR |
bcostm | 4:137bb367ba50 | 31 | * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER |
bcostm | 4:137bb367ba50 | 32 | * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, |
bcostm | 4:137bb367ba50 | 33 | * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
bcostm | 4:137bb367ba50 | 34 | * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
bcostm | 4:137bb367ba50 | 35 | * |
bcostm | 4:137bb367ba50 | 36 | ****************************************************************************** |
bcostm | 4:137bb367ba50 | 37 | */ |
bcostm | 4:137bb367ba50 | 38 | |
bcostm | 4:137bb367ba50 | 39 | /* Includes ------------------------------------------------------------------*/ |
bcostm | 4:137bb367ba50 | 40 | #include "lis3mdl.h" |
bcostm | 4:137bb367ba50 | 41 | |
bcostm | 4:137bb367ba50 | 42 | /** @addtogroup BSP |
bcostm | 4:137bb367ba50 | 43 | * @{ |
bcostm | 4:137bb367ba50 | 44 | */ |
bcostm | 4:137bb367ba50 | 45 | |
bcostm | 4:137bb367ba50 | 46 | /** @addtogroup Component |
bcostm | 4:137bb367ba50 | 47 | * @{ |
bcostm | 4:137bb367ba50 | 48 | */ |
bcostm | 4:137bb367ba50 | 49 | |
bcostm | 4:137bb367ba50 | 50 | /** @defgroup LIS3MDL LIS3MDL |
bcostm | 4:137bb367ba50 | 51 | * @{ |
bcostm | 4:137bb367ba50 | 52 | */ |
bcostm | 4:137bb367ba50 | 53 | |
bcostm | 4:137bb367ba50 | 54 | /** @defgroup LIS3MDL_Mag_Private_Variables LIS3MDL Mag Private Variables |
bcostm | 4:137bb367ba50 | 55 | * @{ |
bcostm | 4:137bb367ba50 | 56 | */ |
bcostm | 4:137bb367ba50 | 57 | MAGNETO_DrvTypeDef Lis3mdlMagDrv = |
bcostm | 4:137bb367ba50 | 58 | { |
bcostm | 4:137bb367ba50 | 59 | LIS3MDL_MagInit, |
bcostm | 4:137bb367ba50 | 60 | LIS3MDL_MagDeInit, |
bcostm | 4:137bb367ba50 | 61 | LIS3MDL_MagReadID, |
bcostm | 4:137bb367ba50 | 62 | 0, |
bcostm | 4:137bb367ba50 | 63 | LIS3MDL_MagLowPower, |
bcostm | 4:137bb367ba50 | 64 | 0, |
bcostm | 4:137bb367ba50 | 65 | 0, |
bcostm | 4:137bb367ba50 | 66 | 0, |
bcostm | 4:137bb367ba50 | 67 | 0, |
bcostm | 4:137bb367ba50 | 68 | 0, |
bcostm | 4:137bb367ba50 | 69 | 0, |
bcostm | 4:137bb367ba50 | 70 | 0, |
bcostm | 4:137bb367ba50 | 71 | LIS3MDL_MagReadXYZ |
bcostm | 4:137bb367ba50 | 72 | }; |
bcostm | 4:137bb367ba50 | 73 | /** |
bcostm | 4:137bb367ba50 | 74 | * @} |
bcostm | 4:137bb367ba50 | 75 | */ |
bcostm | 4:137bb367ba50 | 76 | |
bcostm | 4:137bb367ba50 | 77 | |
bcostm | 4:137bb367ba50 | 78 | /** @defgroup LIS3MDL_Mag_Private_Functions LIS3MDL Mag Private Functions |
bcostm | 4:137bb367ba50 | 79 | * @{ |
bcostm | 4:137bb367ba50 | 80 | */ |
bcostm | 4:137bb367ba50 | 81 | /** |
bcostm | 4:137bb367ba50 | 82 | * @brief Set LIS3MDL Magnetometer Initialization. |
bcostm | 4:137bb367ba50 | 83 | * @param LIS3MDL_InitStruct: pointer to a LIS3MDL_MagInitTypeDef structure |
bcostm | 4:137bb367ba50 | 84 | * that contains the configuration setting for the LIS3MDL. |
bcostm | 4:137bb367ba50 | 85 | */ |
bcostm | 4:137bb367ba50 | 86 | void LIS3MDL_MagInit(MAGNETO_InitTypeDef LIS3MDL_InitStruct) |
bcostm | 4:137bb367ba50 | 87 | { |
bcostm | 4:137bb367ba50 | 88 | SENSOR_IO_Write(LIS3MDL_MAG_I2C_ADDRESS_HIGH, LIS3MDL_MAG_CTRL_REG1, LIS3MDL_InitStruct.Register1); |
bcostm | 4:137bb367ba50 | 89 | SENSOR_IO_Write(LIS3MDL_MAG_I2C_ADDRESS_HIGH, LIS3MDL_MAG_CTRL_REG2, LIS3MDL_InitStruct.Register2); |
bcostm | 4:137bb367ba50 | 90 | SENSOR_IO_Write(LIS3MDL_MAG_I2C_ADDRESS_HIGH, LIS3MDL_MAG_CTRL_REG3, LIS3MDL_InitStruct.Register3); |
bcostm | 4:137bb367ba50 | 91 | SENSOR_IO_Write(LIS3MDL_MAG_I2C_ADDRESS_HIGH, LIS3MDL_MAG_CTRL_REG4, LIS3MDL_InitStruct.Register4); |
bcostm | 4:137bb367ba50 | 92 | SENSOR_IO_Write(LIS3MDL_MAG_I2C_ADDRESS_HIGH, LIS3MDL_MAG_CTRL_REG5, LIS3MDL_InitStruct.Register5); |
bcostm | 4:137bb367ba50 | 93 | } |
bcostm | 4:137bb367ba50 | 94 | |
bcostm | 4:137bb367ba50 | 95 | /** |
bcostm | 4:137bb367ba50 | 96 | * @brief LIS3MDL Magnetometer De-initialization. |
bcostm | 4:137bb367ba50 | 97 | */ |
bcostm | 4:137bb367ba50 | 98 | void LIS3MDL_MagDeInit(void) |
bcostm | 4:137bb367ba50 | 99 | { |
bcostm | 4:137bb367ba50 | 100 | uint8_t ctrl = 0x00; |
bcostm | 4:137bb367ba50 | 101 | |
bcostm | 4:137bb367ba50 | 102 | /* Read control register 1 value */ |
bcostm | 4:137bb367ba50 | 103 | ctrl = SENSOR_IO_Read(LIS3MDL_MAG_I2C_ADDRESS_HIGH, LIS3MDL_MAG_CTRL_REG3); |
bcostm | 4:137bb367ba50 | 104 | |
bcostm | 4:137bb367ba50 | 105 | /* Clear Selection Mode bits */ |
bcostm | 4:137bb367ba50 | 106 | ctrl &= ~(LIS3MDL_MAG_SELECTION_MODE); |
bcostm | 4:137bb367ba50 | 107 | |
bcostm | 4:137bb367ba50 | 108 | /* Set Power down */ |
bcostm | 4:137bb367ba50 | 109 | ctrl |= LIS3MDL_MAG_POWERDOWN2_MODE; |
bcostm | 4:137bb367ba50 | 110 | |
bcostm | 4:137bb367ba50 | 111 | /* write back control register */ |
bcostm | 4:137bb367ba50 | 112 | SENSOR_IO_Write(LIS3MDL_MAG_I2C_ADDRESS_HIGH, LIS3MDL_MAG_CTRL_REG3, ctrl); |
bcostm | 4:137bb367ba50 | 113 | } |
bcostm | 4:137bb367ba50 | 114 | |
bcostm | 4:137bb367ba50 | 115 | /** |
bcostm | 4:137bb367ba50 | 116 | * @brief Read LIS3MDL ID. |
bcostm | 4:137bb367ba50 | 117 | * @retval ID |
bcostm | 4:137bb367ba50 | 118 | */ |
bcostm | 4:137bb367ba50 | 119 | uint8_t LIS3MDL_MagReadID(void) |
bcostm | 4:137bb367ba50 | 120 | { |
bcostm | 4:137bb367ba50 | 121 | /* IO interface initialization */ |
bcostm | 4:137bb367ba50 | 122 | SENSOR_IO_Init(); |
bcostm | 4:137bb367ba50 | 123 | /* Read value at Who am I register address */ |
bcostm | 4:137bb367ba50 | 124 | return (SENSOR_IO_Read(LIS3MDL_MAG_I2C_ADDRESS_HIGH, LIS3MDL_MAG_WHO_AM_I_REG)); |
bcostm | 4:137bb367ba50 | 125 | } |
bcostm | 4:137bb367ba50 | 126 | |
bcostm | 4:137bb367ba50 | 127 | /** |
bcostm | 4:137bb367ba50 | 128 | * @brief Set/Unset Magnetometer in low power mode. |
bcostm | 4:137bb367ba50 | 129 | * @param status 0 means disable Low Power Mode, otherwise Low Power Mode is enabled |
bcostm | 4:137bb367ba50 | 130 | */ |
bcostm | 4:137bb367ba50 | 131 | void LIS3MDL_MagLowPower(uint16_t status) |
bcostm | 4:137bb367ba50 | 132 | { |
bcostm | 4:137bb367ba50 | 133 | uint8_t ctrl = 0; |
bcostm | 4:137bb367ba50 | 134 | |
bcostm | 4:137bb367ba50 | 135 | /* Read control register 1 value */ |
bcostm | 4:137bb367ba50 | 136 | ctrl = SENSOR_IO_Read(LIS3MDL_MAG_I2C_ADDRESS_HIGH, LIS3MDL_MAG_CTRL_REG3); |
bcostm | 4:137bb367ba50 | 137 | |
bcostm | 4:137bb367ba50 | 138 | /* Clear Low Power Mode bit */ |
bcostm | 4:137bb367ba50 | 139 | ctrl &= ~(0x20); |
bcostm | 4:137bb367ba50 | 140 | |
bcostm | 4:137bb367ba50 | 141 | /* Set Low Power Mode */ |
bcostm | 4:137bb367ba50 | 142 | if(status) |
bcostm | 4:137bb367ba50 | 143 | { |
bcostm | 4:137bb367ba50 | 144 | ctrl |= LIS3MDL_MAG_CONFIG_LOWPOWER_MODE; |
bcostm | 4:137bb367ba50 | 145 | }else |
bcostm | 4:137bb367ba50 | 146 | { |
bcostm | 4:137bb367ba50 | 147 | ctrl |= LIS3MDL_MAG_CONFIG_NORMAL_MODE; |
bcostm | 4:137bb367ba50 | 148 | } |
bcostm | 4:137bb367ba50 | 149 | |
bcostm | 4:137bb367ba50 | 150 | /* write back control register */ |
bcostm | 4:137bb367ba50 | 151 | SENSOR_IO_Write(LIS3MDL_MAG_I2C_ADDRESS_HIGH, LIS3MDL_MAG_CTRL_REG3, ctrl); |
bcostm | 4:137bb367ba50 | 152 | } |
bcostm | 4:137bb367ba50 | 153 | |
bcostm | 4:137bb367ba50 | 154 | /** |
bcostm | 4:137bb367ba50 | 155 | * @brief Read X, Y & Z Magnetometer values |
bcostm | 4:137bb367ba50 | 156 | * @param pData: Data out pointer |
bcostm | 4:137bb367ba50 | 157 | */ |
bcostm | 4:137bb367ba50 | 158 | void LIS3MDL_MagReadXYZ(int16_t* pData) |
bcostm | 4:137bb367ba50 | 159 | { |
bcostm | 4:137bb367ba50 | 160 | int16_t pnRawData[3]; |
bcostm | 4:137bb367ba50 | 161 | uint8_t ctrlm= 0; |
bcostm | 4:137bb367ba50 | 162 | uint8_t buffer[6]; |
bcostm | 4:137bb367ba50 | 163 | uint8_t i = 0; |
bcostm | 4:137bb367ba50 | 164 | float sensitivity = 0; |
bcostm | 4:137bb367ba50 | 165 | |
bcostm | 4:137bb367ba50 | 166 | /* Read the magnetometer control register content */ |
bcostm | 4:137bb367ba50 | 167 | ctrlm = SENSOR_IO_Read(LIS3MDL_MAG_I2C_ADDRESS_HIGH, LIS3MDL_MAG_CTRL_REG2); |
bcostm | 4:137bb367ba50 | 168 | |
bcostm | 4:137bb367ba50 | 169 | /* Read output register X, Y & Z acceleration */ |
bcostm | 4:137bb367ba50 | 170 | SENSOR_IO_ReadMultiple(LIS3MDL_MAG_I2C_ADDRESS_HIGH, (LIS3MDL_MAG_OUTX_L | 0x80), buffer, 6); |
bcostm | 4:137bb367ba50 | 171 | |
bcostm | 4:137bb367ba50 | 172 | for(i=0; i<3; i++) |
bcostm | 4:137bb367ba50 | 173 | { |
bcostm | 4:137bb367ba50 | 174 | pnRawData[i]=((((uint16_t)buffer[2*i+1]) << 8) + (uint16_t)buffer[2*i]); |
bcostm | 4:137bb367ba50 | 175 | } |
bcostm | 4:137bb367ba50 | 176 | |
bcostm | 4:137bb367ba50 | 177 | /* Normal mode */ |
bcostm | 4:137bb367ba50 | 178 | /* Switch the sensitivity value set in the CRTL_REG2 */ |
bcostm | 4:137bb367ba50 | 179 | switch(ctrlm & 0x60) |
bcostm | 4:137bb367ba50 | 180 | { |
bcostm | 4:137bb367ba50 | 181 | case LIS3MDL_MAG_FS_4_GA: |
bcostm | 4:137bb367ba50 | 182 | sensitivity = LIS3MDL_MAG_SENSITIVITY_FOR_FS_4GA; |
bcostm | 4:137bb367ba50 | 183 | break; |
bcostm | 4:137bb367ba50 | 184 | case LIS3MDL_MAG_FS_8_GA: |
bcostm | 4:137bb367ba50 | 185 | sensitivity = LIS3MDL_MAG_SENSITIVITY_FOR_FS_8GA; |
bcostm | 4:137bb367ba50 | 186 | break; |
bcostm | 4:137bb367ba50 | 187 | case LIS3MDL_MAG_FS_12_GA: |
bcostm | 4:137bb367ba50 | 188 | sensitivity = LIS3MDL_MAG_SENSITIVITY_FOR_FS_12GA; |
bcostm | 4:137bb367ba50 | 189 | break; |
bcostm | 4:137bb367ba50 | 190 | case LIS3MDL_MAG_FS_16_GA: |
bcostm | 4:137bb367ba50 | 191 | sensitivity = LIS3MDL_MAG_SENSITIVITY_FOR_FS_16GA; |
bcostm | 4:137bb367ba50 | 192 | break; |
bcostm | 4:137bb367ba50 | 193 | } |
bcostm | 4:137bb367ba50 | 194 | |
bcostm | 4:137bb367ba50 | 195 | /* Obtain the mGauss value for the three axis */ |
bcostm | 4:137bb367ba50 | 196 | for(i=0; i<3; i++) |
bcostm | 4:137bb367ba50 | 197 | { |
bcostm | 4:137bb367ba50 | 198 | pData[i]=( int16_t )(pnRawData[i] * sensitivity); |
bcostm | 4:137bb367ba50 | 199 | } |
bcostm | 4:137bb367ba50 | 200 | } |
bcostm | 4:137bb367ba50 | 201 | |
bcostm | 4:137bb367ba50 | 202 | |
bcostm | 4:137bb367ba50 | 203 | /** |
bcostm | 4:137bb367ba50 | 204 | * @} |
bcostm | 4:137bb367ba50 | 205 | */ |
bcostm | 4:137bb367ba50 | 206 | |
bcostm | 4:137bb367ba50 | 207 | /** |
bcostm | 4:137bb367ba50 | 208 | * @} |
bcostm | 4:137bb367ba50 | 209 | */ |
bcostm | 4:137bb367ba50 | 210 | |
bcostm | 4:137bb367ba50 | 211 | /** |
bcostm | 4:137bb367ba50 | 212 | * @} |
bcostm | 4:137bb367ba50 | 213 | */ |
bcostm | 4:137bb367ba50 | 214 | |
bcostm | 4:137bb367ba50 | 215 | /** |
bcostm | 4:137bb367ba50 | 216 | * @} |
bcostm | 4:137bb367ba50 | 217 | */ |
bcostm | 4:137bb367ba50 | 218 | |
bcostm | 4:137bb367ba50 | 219 | /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/ |
bcostm | 4:137bb367ba50 | 220 |