Workshop example

Dependencies:   X_NUCLEO_COMMON ST_INTERFACES

Committer:
JimCarver
Date:
Tue May 21 21:16:24 2019 +0000
Revision:
35:42b3fba640b1
Parent:
18:a15bfe7aaebd
Simple version for workshop

Who changed what in which revision?

UserRevisionLine numberNew contents of line
screamer 18:a15bfe7aaebd 1 /**
screamer 18:a15bfe7aaebd 2 ******************************************************************************
screamer 18:a15bfe7aaebd 3 * @file lis3mdl_class.cpp
screamer 18:a15bfe7aaebd 4 * @author AST / EST
screamer 18:a15bfe7aaebd 5 * @version V0.0.1
screamer 18:a15bfe7aaebd 6 * @date 14-April-2015
screamer 18:a15bfe7aaebd 7 * @brief Implementation file for the LIS3MDL driver class
screamer 18:a15bfe7aaebd 8 ******************************************************************************
screamer 18:a15bfe7aaebd 9 * @attention
screamer 18:a15bfe7aaebd 10 *
screamer 18:a15bfe7aaebd 11 * <h2><center>&copy; COPYRIGHT(c) 2015 STMicroelectronics</center></h2>
screamer 18:a15bfe7aaebd 12 *
screamer 18:a15bfe7aaebd 13 * Redistribution and use in source and binary forms, with or without modification,
screamer 18:a15bfe7aaebd 14 * are permitted provided that the following conditions are met:
screamer 18:a15bfe7aaebd 15 * 1. Redistributions of source code must retain the above copyright notice,
screamer 18:a15bfe7aaebd 16 * this list of conditions and the following disclaimer.
screamer 18:a15bfe7aaebd 17 * 2. Redistributions in binary form must reproduce the above copyright notice,
screamer 18:a15bfe7aaebd 18 * this list of conditions and the following disclaimer in the documentation
screamer 18:a15bfe7aaebd 19 * and/or other materials provided with the distribution.
screamer 18:a15bfe7aaebd 20 * 3. Neither the name of STMicroelectronics nor the names of its contributors
screamer 18:a15bfe7aaebd 21 * may be used to endorse or promote products derived from this software
screamer 18:a15bfe7aaebd 22 * without specific prior written permission.
screamer 18:a15bfe7aaebd 23 *
screamer 18:a15bfe7aaebd 24 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
screamer 18:a15bfe7aaebd 25 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
screamer 18:a15bfe7aaebd 26 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
screamer 18:a15bfe7aaebd 27 * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
screamer 18:a15bfe7aaebd 28 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
screamer 18:a15bfe7aaebd 29 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
screamer 18:a15bfe7aaebd 30 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
screamer 18:a15bfe7aaebd 31 * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
screamer 18:a15bfe7aaebd 32 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
screamer 18:a15bfe7aaebd 33 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
screamer 18:a15bfe7aaebd 34 *
screamer 18:a15bfe7aaebd 35 ******************************************************************************
screamer 18:a15bfe7aaebd 36 */
screamer 18:a15bfe7aaebd 37
screamer 18:a15bfe7aaebd 38 /* Includes ------------------------------------------------------------------*/
screamer 18:a15bfe7aaebd 39 #include "lis3mdl_class.h"
screamer 18:a15bfe7aaebd 40 #include "lis3mdl.h"
screamer 18:a15bfe7aaebd 41
screamer 18:a15bfe7aaebd 42 /* Methods -------------------------------------------------------------------*/
screamer 18:a15bfe7aaebd 43 /* betzw - based on:
screamer 18:a15bfe7aaebd 44 X-CUBE-MEMS1/trunk/Drivers/BSP/Components/lis3mdl/lis3mdl.c: revision #400,
screamer 18:a15bfe7aaebd 45 X-CUBE-MEMS1/trunk: revision #416
screamer 18:a15bfe7aaebd 46 */
screamer 18:a15bfe7aaebd 47
screamer 18:a15bfe7aaebd 48 LIS3MDL::LIS3MDL(SPI *spi, PinName cs_pin, PinName int_pin, SPI_type_t spi_type) :
screamer 18:a15bfe7aaebd 49 _dev_spi(spi), _cs_pin(cs_pin), _int_pin(int_pin), _spi_type(spi_type)
screamer 18:a15bfe7aaebd 50 {
screamer 18:a15bfe7aaebd 51 assert (spi);
screamer 18:a15bfe7aaebd 52 if (cs_pin == NC)
screamer 18:a15bfe7aaebd 53 {
screamer 18:a15bfe7aaebd 54 printf ("ERROR LIS3MDL CS MUST NOT BE NC\n\r");
screamer 18:a15bfe7aaebd 55 _dev_spi = NULL;
screamer 18:a15bfe7aaebd 56 _dev_i2c=NULL;
screamer 18:a15bfe7aaebd 57 return;
screamer 18:a15bfe7aaebd 58 }
screamer 18:a15bfe7aaebd 59
screamer 18:a15bfe7aaebd 60 _cs_pin = 0;
screamer 18:a15bfe7aaebd 61 _dev_i2c=NULL;
screamer 18:a15bfe7aaebd 62
screamer 18:a15bfe7aaebd 63 if (_spi_type == SPI3W) LIS3MDL_Set_SpiInterface ((void *)this, LIS3MDL_SPI_3_WIRE);
screamer 18:a15bfe7aaebd 64 else if (_spi_type == SPI4W) LIS3MDL_Set_SpiInterface ((void *)this, LIS3MDL_SPI_4_WIRE);
screamer 18:a15bfe7aaebd 65 }
screamer 18:a15bfe7aaebd 66
screamer 18:a15bfe7aaebd 67 LIS3MDL::LIS3MDL(DevI2C *i2c, uint8_t address, PinName int_pin) :
screamer 18:a15bfe7aaebd 68 _dev_i2c(i2c), _address(address), _cs_pin(NC), _int_pin(int_pin)
screamer 18:a15bfe7aaebd 69 {
screamer 18:a15bfe7aaebd 70 assert (i2c);
screamer 18:a15bfe7aaebd 71 _dev_spi = NULL;
screamer 18:a15bfe7aaebd 72 }
screamer 18:a15bfe7aaebd 73
screamer 18:a15bfe7aaebd 74
screamer 18:a15bfe7aaebd 75 MAGNETO_StatusTypeDef LIS3MDL::LIS3MDL_Set_SpiInterface (void *handle, LIS3MDL_SPIMode_t spimode)
screamer 18:a15bfe7aaebd 76 {
screamer 18:a15bfe7aaebd 77 uint8_t tmp=0x03; //deft LIS3MDL_CTRL_REG3 value
screamer 18:a15bfe7aaebd 78
screamer 18:a15bfe7aaebd 79 tmp |= (uint8_t)spimode;
screamer 18:a15bfe7aaebd 80 if (LIS3MDL_IO_Write(&tmp, LIS3MDL_M_CTRL_REG3_M, 1) != MAGNETO_OK) return MAGNETO_ERROR;
screamer 18:a15bfe7aaebd 81 return MAGNETO_OK;
screamer 18:a15bfe7aaebd 82 }
screamer 18:a15bfe7aaebd 83
screamer 18:a15bfe7aaebd 84 /**
screamer 18:a15bfe7aaebd 85 * @brief Set LIS3MDL Initialization
screamer 18:a15bfe7aaebd 86 * @param LIS3MDL_Init the configuration setting for the LIS3MDL
screamer 18:a15bfe7aaebd 87 * @retval MAGNETO_OK in case of success, an error code otherwise
screamer 18:a15bfe7aaebd 88 */
screamer 18:a15bfe7aaebd 89 MAGNETO_StatusTypeDef LIS3MDL::LIS3MDL_Init(MAGNETO_InitTypeDef *LIS3MDL_Init)
screamer 18:a15bfe7aaebd 90 {
screamer 18:a15bfe7aaebd 91 uint8_t tmp1 = 0x00;
screamer 18:a15bfe7aaebd 92 MAGNETO_InitTypeDef *initStructure = LIS3MDL_Init;
screamer 18:a15bfe7aaebd 93 MAGNETO_InitTypeDef tempInit;
screamer 18:a15bfe7aaebd 94
screamer 18:a15bfe7aaebd 95 if (initStructure == NULL) {// default params
screamer 18:a15bfe7aaebd 96 tempInit.M_FullScale = LIS3MDL_M_FS_4;
screamer 18:a15bfe7aaebd 97 tempInit.M_OperatingMode = LIS3MDL_M_MD_CONTINUOUS;
screamer 18:a15bfe7aaebd 98 tempInit.M_XYOperativeMode = LIS3MDL_M_OM_HP;
screamer 18:a15bfe7aaebd 99 tempInit.M_OutputDataRate = LIS3MDL_M_DO_80;
screamer 18:a15bfe7aaebd 100 initStructure = &tempInit;
screamer 18:a15bfe7aaebd 101 }
screamer 18:a15bfe7aaebd 102
screamer 18:a15bfe7aaebd 103
screamer 18:a15bfe7aaebd 104 /* Configure the low level interface ---------------------------------------*/
screamer 18:a15bfe7aaebd 105 if(LIS3MDL_IO_Init() != MAGNETO_OK)
screamer 18:a15bfe7aaebd 106 {
screamer 18:a15bfe7aaebd 107 return MAGNETO_ERROR;
screamer 18:a15bfe7aaebd 108 }
screamer 18:a15bfe7aaebd 109
screamer 18:a15bfe7aaebd 110 /****** Magnetic sensor *******/
screamer 18:a15bfe7aaebd 111
screamer 18:a15bfe7aaebd 112 if(LIS3MDL_IO_Read(&tmp1, LIS3MDL_M_CTRL_REG3_M, 1) != MAGNETO_OK)
screamer 18:a15bfe7aaebd 113 {
screamer 18:a15bfe7aaebd 114 return MAGNETO_ERROR;
screamer 18:a15bfe7aaebd 115 }
screamer 18:a15bfe7aaebd 116
screamer 18:a15bfe7aaebd 117 /* Conversion mode selection */
screamer 18:a15bfe7aaebd 118 tmp1 &= ~(LIS3MDL_M_MD_MASK);
screamer 18:a15bfe7aaebd 119 tmp1 |= initStructure->M_OperatingMode;
screamer 18:a15bfe7aaebd 120
screamer 18:a15bfe7aaebd 121 if(LIS3MDL_IO_Write(&tmp1, LIS3MDL_M_CTRL_REG3_M, 1) != MAGNETO_OK)
screamer 18:a15bfe7aaebd 122 {
screamer 18:a15bfe7aaebd 123 return MAGNETO_ERROR;
screamer 18:a15bfe7aaebd 124 }
screamer 18:a15bfe7aaebd 125
screamer 18:a15bfe7aaebd 126 if(LIS3MDL_IO_Read(&tmp1, LIS3MDL_M_CTRL_REG1_M, 1) != MAGNETO_OK)
screamer 18:a15bfe7aaebd 127 {
screamer 18:a15bfe7aaebd 128 return MAGNETO_ERROR;
screamer 18:a15bfe7aaebd 129 }
screamer 18:a15bfe7aaebd 130
screamer 18:a15bfe7aaebd 131 /* Output data rate selection */
screamer 18:a15bfe7aaebd 132 tmp1 &= ~(LIS3MDL_M_DO_MASK);
screamer 18:a15bfe7aaebd 133 tmp1 |= initStructure->M_OutputDataRate;
screamer 18:a15bfe7aaebd 134
screamer 18:a15bfe7aaebd 135 /* X and Y axes Operative mode selection */
screamer 18:a15bfe7aaebd 136 tmp1 &= ~(LIS3MDL_M_OM_MASK);
screamer 18:a15bfe7aaebd 137 tmp1 |= initStructure->M_XYOperativeMode;
screamer 18:a15bfe7aaebd 138
screamer 18:a15bfe7aaebd 139 if(LIS3MDL_IO_Write(&tmp1, LIS3MDL_M_CTRL_REG1_M, 1) != MAGNETO_OK)
screamer 18:a15bfe7aaebd 140 {
screamer 18:a15bfe7aaebd 141 return MAGNETO_ERROR;
screamer 18:a15bfe7aaebd 142 }
screamer 18:a15bfe7aaebd 143
screamer 18:a15bfe7aaebd 144 if(LIS3MDL_IO_Read(&tmp1, LIS3MDL_M_CTRL_REG2_M, 1) != MAGNETO_OK)
screamer 18:a15bfe7aaebd 145 {
screamer 18:a15bfe7aaebd 146 return MAGNETO_ERROR;
screamer 18:a15bfe7aaebd 147 }
screamer 18:a15bfe7aaebd 148
screamer 18:a15bfe7aaebd 149 /* Full scale selection */
screamer 18:a15bfe7aaebd 150 tmp1 &= ~(LIS3MDL_M_FS_MASK);
screamer 18:a15bfe7aaebd 151 tmp1 |= initStructure->M_FullScale;
screamer 18:a15bfe7aaebd 152
screamer 18:a15bfe7aaebd 153 if(LIS3MDL_IO_Write(&tmp1, LIS3MDL_M_CTRL_REG2_M, 1) != MAGNETO_OK)
screamer 18:a15bfe7aaebd 154 {
screamer 18:a15bfe7aaebd 155 return MAGNETO_ERROR;
screamer 18:a15bfe7aaebd 156 }
screamer 18:a15bfe7aaebd 157
screamer 18:a15bfe7aaebd 158 /* Configure interrupt lines */
screamer 18:a15bfe7aaebd 159 LIS3MDL_IO_ITConfig();
screamer 18:a15bfe7aaebd 160
screamer 18:a15bfe7aaebd 161 return MAGNETO_OK;
screamer 18:a15bfe7aaebd 162
screamer 18:a15bfe7aaebd 163 /******************************/
screamer 18:a15bfe7aaebd 164 }
screamer 18:a15bfe7aaebd 165
screamer 18:a15bfe7aaebd 166
screamer 18:a15bfe7aaebd 167 /**
screamer 18:a15bfe7aaebd 168 * @brief Read ID of LIS3MDL Magnetic sensor
screamer 18:a15bfe7aaebd 169 * @param m_id the pointer where the ID of the device is stored
screamer 18:a15bfe7aaebd 170 * @retval MAGNETO_OK in case of success, an error code otherwise
screamer 18:a15bfe7aaebd 171 */
screamer 18:a15bfe7aaebd 172 MAGNETO_StatusTypeDef LIS3MDL::LIS3MDL_Read_M_ID(uint8_t *m_id)
screamer 18:a15bfe7aaebd 173 {
screamer 18:a15bfe7aaebd 174 if(!m_id)
screamer 18:a15bfe7aaebd 175 {
screamer 18:a15bfe7aaebd 176 return MAGNETO_ERROR;
screamer 18:a15bfe7aaebd 177 }
screamer 18:a15bfe7aaebd 178
screamer 18:a15bfe7aaebd 179 return LIS3MDL_IO_Read(m_id, LIS3MDL_M_WHO_AM_I_ADDR, 1);
screamer 18:a15bfe7aaebd 180 }
screamer 18:a15bfe7aaebd 181
screamer 18:a15bfe7aaebd 182
screamer 18:a15bfe7aaebd 183 /**
screamer 18:a15bfe7aaebd 184 * @brief Read raw data from LIS3MDL Magnetic sensor output register
screamer 18:a15bfe7aaebd 185 * @param pData the pointer where the magnetometer raw data are stored
screamer 18:a15bfe7aaebd 186 * @retval MAGNETO_OK in case of success, an error code otherwise
screamer 18:a15bfe7aaebd 187 */
screamer 18:a15bfe7aaebd 188 MAGNETO_StatusTypeDef LIS3MDL::LIS3MDL_M_GetAxesRaw(int16_t *pData)
screamer 18:a15bfe7aaebd 189 {
screamer 18:a15bfe7aaebd 190 uint8_t tempReg[2] = {0, 0};
screamer 18:a15bfe7aaebd 191
screamer 18:a15bfe7aaebd 192 if(LIS3MDL_IO_Read(&tempReg[0], (LIS3MDL_M_OUT_X_L_M | LIS3MDL_I2C_MULTIPLEBYTE_CMD),
screamer 18:a15bfe7aaebd 193 2) != MAGNETO_OK)
screamer 18:a15bfe7aaebd 194 {
screamer 18:a15bfe7aaebd 195 return MAGNETO_ERROR;
screamer 18:a15bfe7aaebd 196 }
screamer 18:a15bfe7aaebd 197
screamer 18:a15bfe7aaebd 198 pData[0] = ((((int16_t)tempReg[1]) << 8) + (int16_t)tempReg[0]);
screamer 18:a15bfe7aaebd 199
screamer 18:a15bfe7aaebd 200 if(LIS3MDL_IO_Read(&tempReg[0], (LIS3MDL_M_OUT_Y_L_M | LIS3MDL_I2C_MULTIPLEBYTE_CMD),
screamer 18:a15bfe7aaebd 201 2) != MAGNETO_OK)
screamer 18:a15bfe7aaebd 202 {
screamer 18:a15bfe7aaebd 203 return MAGNETO_ERROR;
screamer 18:a15bfe7aaebd 204 }
screamer 18:a15bfe7aaebd 205
screamer 18:a15bfe7aaebd 206 pData[1] = ((((int16_t)tempReg[1]) << 8) + (int16_t)tempReg[0]);
screamer 18:a15bfe7aaebd 207
screamer 18:a15bfe7aaebd 208 if(LIS3MDL_IO_Read(&tempReg[0], (LIS3MDL_M_OUT_Z_L_M | LIS3MDL_I2C_MULTIPLEBYTE_CMD),
screamer 18:a15bfe7aaebd 209 2) != MAGNETO_OK)
screamer 18:a15bfe7aaebd 210 {
screamer 18:a15bfe7aaebd 211 return MAGNETO_ERROR;
screamer 18:a15bfe7aaebd 212 }
screamer 18:a15bfe7aaebd 213
screamer 18:a15bfe7aaebd 214 pData[2] = ((((int16_t)tempReg[1]) << 8) + (int16_t)tempReg[0]);
screamer 18:a15bfe7aaebd 215
screamer 18:a15bfe7aaebd 216 return MAGNETO_OK;
screamer 18:a15bfe7aaebd 217 }
screamer 18:a15bfe7aaebd 218
screamer 18:a15bfe7aaebd 219
screamer 18:a15bfe7aaebd 220 /**
screamer 18:a15bfe7aaebd 221 * @brief Read data from LIS3MDL Magnetic sensor and calculate Magnetic in mgauss
screamer 18:a15bfe7aaebd 222 * @param pData the pointer where the magnetometer data are stored
screamer 18:a15bfe7aaebd 223 * @retval MAGNETO_OK in case of success, an error code otherwise
screamer 18:a15bfe7aaebd 224 */
screamer 18:a15bfe7aaebd 225 MAGNETO_StatusTypeDef LIS3MDL::LIS3MDL_M_GetAxes(int32_t *pData)
screamer 18:a15bfe7aaebd 226 {
screamer 18:a15bfe7aaebd 227 uint8_t tempReg = 0x00;
screamer 18:a15bfe7aaebd 228 int16_t pDataRaw[3];
screamer 18:a15bfe7aaebd 229 float sensitivity = 0;
screamer 18:a15bfe7aaebd 230
screamer 18:a15bfe7aaebd 231 if(LIS3MDL_M_GetAxesRaw(pDataRaw) != MAGNETO_OK)
screamer 18:a15bfe7aaebd 232 {
screamer 18:a15bfe7aaebd 233 return MAGNETO_ERROR;
screamer 18:a15bfe7aaebd 234 }
screamer 18:a15bfe7aaebd 235
screamer 18:a15bfe7aaebd 236 if(LIS3MDL_IO_Read(&tempReg, LIS3MDL_M_CTRL_REG2_M, 1) != MAGNETO_OK)
screamer 18:a15bfe7aaebd 237 {
screamer 18:a15bfe7aaebd 238 return MAGNETO_ERROR;
screamer 18:a15bfe7aaebd 239 }
screamer 18:a15bfe7aaebd 240
screamer 18:a15bfe7aaebd 241 tempReg &= LIS3MDL_M_FS_MASK;
screamer 18:a15bfe7aaebd 242
screamer 18:a15bfe7aaebd 243 switch(tempReg)
screamer 18:a15bfe7aaebd 244 {
screamer 18:a15bfe7aaebd 245 case LIS3MDL_M_FS_4:
screamer 18:a15bfe7aaebd 246 sensitivity = 0.14;
screamer 18:a15bfe7aaebd 247 break;
screamer 18:a15bfe7aaebd 248 case LIS3MDL_M_FS_8:
screamer 18:a15bfe7aaebd 249 sensitivity = 0.29;
screamer 18:a15bfe7aaebd 250 break;
screamer 18:a15bfe7aaebd 251 case LIS3MDL_M_FS_12:
screamer 18:a15bfe7aaebd 252 sensitivity = 0.43;
screamer 18:a15bfe7aaebd 253 break;
screamer 18:a15bfe7aaebd 254 case LIS3MDL_M_FS_16:
screamer 18:a15bfe7aaebd 255 sensitivity = 0.58;
screamer 18:a15bfe7aaebd 256 break;
screamer 18:a15bfe7aaebd 257 }
screamer 18:a15bfe7aaebd 258
screamer 18:a15bfe7aaebd 259 pData[0] = (int32_t)(pDataRaw[0] * sensitivity);
screamer 18:a15bfe7aaebd 260 pData[1] = (int32_t)(pDataRaw[1] * sensitivity);
screamer 18:a15bfe7aaebd 261 pData[2] = (int32_t)(pDataRaw[2] * sensitivity);
screamer 18:a15bfe7aaebd 262
screamer 18:a15bfe7aaebd 263 return MAGNETO_OK;
screamer 18:a15bfe7aaebd 264 }
screamer 18:a15bfe7aaebd 265
screamer 18:a15bfe7aaebd 266 /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/