Finished project.

Dependencies:   X_NUCLEO_COMMON

Fork of ReferredCoursework2016 by Stage-1 Students SoCEM

Committer:
Wolfgang Betz
Date:
Wed Jun 10 16:44:54 2015 +0200
Revision:
52:54553fd15b50
Parent:
50:f507d4465c31
Child:
54:2a676c734b30
Enable free fall detection

Who changed what in which revision?

UserRevisionLine numberNew contents of line
Wolfgang Betz 44:d757094f6229 1 /**
Wolfgang Betz 44:d757094f6229 2 ******************************************************************************
Wolfgang Betz 44:d757094f6229 3 * @file lsm6ds3_class.h
Wolfgang Betz 44:d757094f6229 4 * @author AST / EST
Wolfgang Betz 44:d757094f6229 5 * @version V0.0.1
Wolfgang Betz 44:d757094f6229 6 * @date 14-April-2015
Wolfgang Betz 44:d757094f6229 7 * @brief Header file for component LSM6DS3
Wolfgang Betz 44:d757094f6229 8 ******************************************************************************
Wolfgang Betz 44:d757094f6229 9 * @attention
Wolfgang Betz 44:d757094f6229 10 *
Wolfgang Betz 44:d757094f6229 11 * <h2><center>&copy; COPYRIGHT(c) 2015 STMicroelectronics</center></h2>
Wolfgang Betz 44:d757094f6229 12 *
Wolfgang Betz 44:d757094f6229 13 * Redistribution and use in source and binary forms, with or without modification,
Wolfgang Betz 44:d757094f6229 14 * are permitted provided that the following conditions are met:
Wolfgang Betz 44:d757094f6229 15 * 1. Redistributions of source code must retain the above copyright notice,
Wolfgang Betz 44:d757094f6229 16 * this list of conditions and the following disclaimer.
Wolfgang Betz 44:d757094f6229 17 * 2. Redistributions in binary form must reproduce the above copyright notice,
Wolfgang Betz 44:d757094f6229 18 * this list of conditions and the following disclaimer in the documentation
Wolfgang Betz 44:d757094f6229 19 * and/or other materials provided with the distribution.
Wolfgang Betz 44:d757094f6229 20 * 3. Neither the name of STMicroelectronics nor the names of its contributors
Wolfgang Betz 44:d757094f6229 21 * may be used to endorse or promote products derived from this software
Wolfgang Betz 44:d757094f6229 22 * without specific prior written permission.
Wolfgang Betz 44:d757094f6229 23 *
Wolfgang Betz 44:d757094f6229 24 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
Wolfgang Betz 44:d757094f6229 25 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
Wolfgang Betz 44:d757094f6229 26 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
Wolfgang Betz 44:d757094f6229 27 * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
Wolfgang Betz 44:d757094f6229 28 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
Wolfgang Betz 44:d757094f6229 29 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
Wolfgang Betz 44:d757094f6229 30 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
Wolfgang Betz 44:d757094f6229 31 * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
Wolfgang Betz 44:d757094f6229 32 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
Wolfgang Betz 44:d757094f6229 33 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
Wolfgang Betz 44:d757094f6229 34 *
Wolfgang Betz 44:d757094f6229 35 ******************************************************************************
Wolfgang Betz 44:d757094f6229 36 */
Wolfgang Betz 44:d757094f6229 37
Wolfgang Betz 44:d757094f6229 38 #ifndef __LSM6DS3_CLASS_H
Wolfgang Betz 44:d757094f6229 39 #define __LSM6DS3_CLASS_H
Wolfgang Betz 44:d757094f6229 40
Wolfgang Betz 44:d757094f6229 41 /* Includes ------------------------------------------------------------------*/
Wolfgang Betz 44:d757094f6229 42 #include "mbed.h"
Wolfgang Betz 44:d757094f6229 43 #include "DevI2C.h"
Wolfgang Betz 44:d757094f6229 44 #include "../Interfaces/GyroSensor.h"
Wolfgang Betz 44:d757094f6229 45 #include "../Interfaces/MotionSensor.h"
Wolfgang Betz 44:d757094f6229 46 #include "lsm6ds3.h"
Wolfgang Betz 44:d757094f6229 47
Wolfgang Betz 44:d757094f6229 48 /* Classes -------------------------------------------------------------------*/
Wolfgang Betz 44:d757094f6229 49 /** Class representing a LSM6DS3 sensor component
Wolfgang Betz 44:d757094f6229 50 */
Wolfgang Betz 44:d757094f6229 51 class LSM6DS3 : public GyroSensor, public MotionSensor {
Wolfgang Betz 44:d757094f6229 52 public:
Wolfgang Betz 44:d757094f6229 53 /** Constructor
Wolfgang Betz 44:d757094f6229 54 * @param i2c device I2C to be used for communication
Wolfgang Betz 44:d757094f6229 55 */
Wolfgang Betz 52:54553fd15b50 56 LSM6DS3(DevI2C &i2c, PinName irq_pin) : GyroSensor(), MotionSensor(),
Wolfgang Betz 52:54553fd15b50 57 dev_i2c(i2c), free_fall(irq_pin) {
Wolfgang Betz 44:d757094f6229 58 }
Wolfgang Betz 44:d757094f6229 59
Wolfgang Betz 44:d757094f6229 60 /** Destructor
Wolfgang Betz 44:d757094f6229 61 */
Wolfgang Betz 44:d757094f6229 62 virtual ~LSM6DS3() {}
Wolfgang Betz 44:d757094f6229 63
Wolfgang Betz 44:d757094f6229 64 /*** Interface Methods ***/
Wolfgang Betz 44:d757094f6229 65 virtual int Init(void *init_struct) {
Wolfgang Betz 44:d757094f6229 66 return LSM6DS3_Init((IMU_6AXES_InitTypeDef*)init_struct);
Wolfgang Betz 44:d757094f6229 67 }
Wolfgang Betz 44:d757094f6229 68
Wolfgang Betz 44:d757094f6229 69 virtual int ReadID(uint8_t *xg_id) {
Wolfgang Betz 44:d757094f6229 70 return LSM6DS3_Read_XG_ID(xg_id);
Wolfgang Betz 44:d757094f6229 71 }
Wolfgang Betz 44:d757094f6229 72
Wolfgang Betz 44:d757094f6229 73 virtual int Get_X_Axes(int32_t *pData) {
Wolfgang Betz 44:d757094f6229 74 return LSM6DS3_X_GetAxes(pData);
Wolfgang Betz 44:d757094f6229 75 }
Wolfgang Betz 44:d757094f6229 76
Wolfgang Betz 44:d757094f6229 77 virtual int Get_X_AxesRaw(int16_t *pData) {
Wolfgang Betz 44:d757094f6229 78 return LSM6DS3_X_GetAxesRaw(pData);
Wolfgang Betz 44:d757094f6229 79 }
Wolfgang Betz 44:d757094f6229 80
Wolfgang Betz 44:d757094f6229 81 virtual int Get_G_Axes(int32_t *pData) {
Wolfgang Betz 44:d757094f6229 82 return LSM6DS3_G_GetAxes(pData);
Wolfgang Betz 44:d757094f6229 83 }
Wolfgang Betz 44:d757094f6229 84
Wolfgang Betz 44:d757094f6229 85 virtual int Get_G_AxesRaw(int16_t *pData) {
Wolfgang Betz 44:d757094f6229 86 return LSM6DS3_G_GetAxesRaw(pData);
Wolfgang Betz 44:d757094f6229 87 }
Wolfgang Betz 44:d757094f6229 88
Wolfgang Betz 44:d757094f6229 89 virtual int Get_X_ODR(float *odr) {
Wolfgang Betz 44:d757094f6229 90 return LSM6DS3_X_Get_ODR(odr);
Wolfgang Betz 44:d757094f6229 91 }
Wolfgang Betz 44:d757094f6229 92
Wolfgang Betz 44:d757094f6229 93 virtual int Set_X_ODR(float odr) {
Wolfgang Betz 44:d757094f6229 94 return LSM6DS3_X_Set_ODR(odr);
Wolfgang Betz 44:d757094f6229 95 }
Wolfgang Betz 44:d757094f6229 96
Wolfgang Betz 44:d757094f6229 97 virtual int Get_X_Sensitivity(float *pfData) {
Wolfgang Betz 44:d757094f6229 98 return LSM6DS3_X_GetSensitivity(pfData);
Wolfgang Betz 44:d757094f6229 99 }
Wolfgang Betz 44:d757094f6229 100
Wolfgang Betz 44:d757094f6229 101 virtual int Get_X_FS(float *fullScale) {
Wolfgang Betz 44:d757094f6229 102 return LSM6DS3_X_Get_FS(fullScale);
Wolfgang Betz 44:d757094f6229 103 }
Wolfgang Betz 44:d757094f6229 104
Wolfgang Betz 44:d757094f6229 105 virtual int Set_X_FS(float fullScale) {
Wolfgang Betz 44:d757094f6229 106 return LSM6DS3_X_Set_FS(fullScale);
Wolfgang Betz 44:d757094f6229 107 }
Wolfgang Betz 44:d757094f6229 108
Wolfgang Betz 44:d757094f6229 109 virtual int Get_G_ODR(float *odr) {
Wolfgang Betz 44:d757094f6229 110 return LSM6DS3_G_Get_ODR(odr);
Wolfgang Betz 44:d757094f6229 111 }
Wolfgang Betz 44:d757094f6229 112
Wolfgang Betz 44:d757094f6229 113 virtual int Set_G_ODR(float odr) {
Wolfgang Betz 44:d757094f6229 114 return LSM6DS3_G_Set_ODR(odr);
Wolfgang Betz 44:d757094f6229 115 }
Wolfgang Betz 44:d757094f6229 116
Wolfgang Betz 44:d757094f6229 117 virtual int Get_G_Sensitivity(float *pfData) {
Wolfgang Betz 44:d757094f6229 118 return LSM6DS3_G_GetSensitivity(pfData);
Wolfgang Betz 44:d757094f6229 119 }
Wolfgang Betz 44:d757094f6229 120
Wolfgang Betz 44:d757094f6229 121 virtual int Get_G_FS(float *fullScale) {
Wolfgang Betz 44:d757094f6229 122 return LSM6DS3_G_Get_FS(fullScale);
Wolfgang Betz 44:d757094f6229 123 }
Wolfgang Betz 44:d757094f6229 124
Wolfgang Betz 44:d757094f6229 125 virtual int Set_G_FS(float fullScale) {
Wolfgang Betz 44:d757094f6229 126 return LSM6DS3_G_Set_FS(fullScale);
Wolfgang Betz 44:d757094f6229 127 }
Wolfgang Betz 44:d757094f6229 128
Wolfgang Betz 44:d757094f6229 129 /* Additional Public Methods */
Wolfgang Betz 44:d757094f6229 130 IMU_6AXES_StatusTypeDef Enable_Free_Fall_Detection(void) {
Wolfgang Betz 44:d757094f6229 131 return LSM6DS3_Enable_Free_Fall_Detection();
Wolfgang Betz 44:d757094f6229 132 }
Wolfgang Betz 44:d757094f6229 133
Wolfgang Betz 44:d757094f6229 134 IMU_6AXES_StatusTypeDef Disable_Free_Fall_Detection(void) {
Wolfgang Betz 44:d757094f6229 135 return LSM6DS3_Disable_Free_Fall_Detection();
Wolfgang Betz 44:d757094f6229 136 }
Wolfgang Betz 44:d757094f6229 137
Wolfgang Betz 44:d757094f6229 138 IMU_6AXES_StatusTypeDef Get_Status_Free_Fall_Detection(uint8_t *status) {
Wolfgang Betz 44:d757094f6229 139 return LSM6DS3_Get_Status_Free_Fall_Detection(status);
Wolfgang Betz 44:d757094f6229 140 }
Wolfgang Betz 52:54553fd15b50 141
Wolfgang Betz 52:54553fd15b50 142 /** Attach a function to call when a free fall is detected
Wolfgang Betz 52:54553fd15b50 143 *
Wolfgang Betz 52:54553fd15b50 144 * @param fptr A pointer to a void function, or 0 to set as none
Wolfgang Betz 52:54553fd15b50 145 */
Wolfgang Betz 52:54553fd15b50 146 void Attach_Free_Fall_Detection_IRQ(void (*fptr)(void)) {
Wolfgang Betz 52:54553fd15b50 147 free_fall.mode(PullNone); /* be precise about pin mode */
Wolfgang Betz 52:54553fd15b50 148 free_fall.rise(fptr);
Wolfgang Betz 52:54553fd15b50 149 }
Wolfgang Betz 52:54553fd15b50 150
Wolfgang Betz 52:54553fd15b50 151 /** Enable Free Fall IRQ
Wolfgang Betz 52:54553fd15b50 152 */
Wolfgang Betz 52:54553fd15b50 153 void Enable_Free_Fall_Detection_IRQ(void) {
Wolfgang Betz 52:54553fd15b50 154 free_fall.enable_irq();
Wolfgang Betz 52:54553fd15b50 155 }
Wolfgang Betz 52:54553fd15b50 156
Wolfgang Betz 52:54553fd15b50 157 /** Disable free Fall IRQ
Wolfgang Betz 52:54553fd15b50 158 */
Wolfgang Betz 52:54553fd15b50 159 void Disable_Free_Fall_Detection_IRQ(void) {
Wolfgang Betz 52:54553fd15b50 160 free_fall.disable_irq();
Wolfgang Betz 52:54553fd15b50 161 }
Wolfgang Betz 44:d757094f6229 162
Wolfgang Betz 44:d757094f6229 163 protected:
Wolfgang Betz 44:d757094f6229 164 /*** Methods ***/
Wolfgang Betz 44:d757094f6229 165 IMU_6AXES_StatusTypeDef LSM6DS3_Init(IMU_6AXES_InitTypeDef *LSM6DS3_Init);
Wolfgang Betz 44:d757094f6229 166 IMU_6AXES_StatusTypeDef LSM6DS3_Read_XG_ID(uint8_t *xg_id);
Wolfgang Betz 44:d757094f6229 167 IMU_6AXES_StatusTypeDef LSM6DS3_X_GetAxes(int32_t *pData);
Wolfgang Betz 44:d757094f6229 168 IMU_6AXES_StatusTypeDef LSM6DS3_X_GetAxesRaw(int16_t *pData);
Wolfgang Betz 44:d757094f6229 169 IMU_6AXES_StatusTypeDef LSM6DS3_G_GetAxes(int32_t *pData);
Wolfgang Betz 44:d757094f6229 170 IMU_6AXES_StatusTypeDef LSM6DS3_G_GetAxesRaw(int16_t *pData);
Wolfgang Betz 44:d757094f6229 171 IMU_6AXES_StatusTypeDef LSM6DS3_X_Get_ODR( float *odr );
Wolfgang Betz 44:d757094f6229 172 IMU_6AXES_StatusTypeDef LSM6DS3_X_Set_ODR( float odr );
Wolfgang Betz 44:d757094f6229 173 IMU_6AXES_StatusTypeDef LSM6DS3_X_GetSensitivity( float *pfData );
Wolfgang Betz 44:d757094f6229 174 IMU_6AXES_StatusTypeDef LSM6DS3_X_Get_FS( float *fullScale );
Wolfgang Betz 44:d757094f6229 175 IMU_6AXES_StatusTypeDef LSM6DS3_X_Set_FS( float fullScale );
Wolfgang Betz 44:d757094f6229 176 IMU_6AXES_StatusTypeDef LSM6DS3_G_Get_ODR( float *odr );
Wolfgang Betz 44:d757094f6229 177 IMU_6AXES_StatusTypeDef LSM6DS3_G_Set_ODR( float odr );
Wolfgang Betz 44:d757094f6229 178 IMU_6AXES_StatusTypeDef LSM6DS3_G_GetSensitivity( float *pfData );
Wolfgang Betz 44:d757094f6229 179 IMU_6AXES_StatusTypeDef LSM6DS3_G_Get_FS( float *fullScale );
Wolfgang Betz 44:d757094f6229 180 IMU_6AXES_StatusTypeDef LSM6DS3_G_Set_FS( float fullScale );
Wolfgang Betz 44:d757094f6229 181 IMU_6AXES_StatusTypeDef LSM6DS3_Enable_Free_Fall_Detection( void );
Wolfgang Betz 44:d757094f6229 182 IMU_6AXES_StatusTypeDef LSM6DS3_Disable_Free_Fall_Detection( void );
Wolfgang Betz 44:d757094f6229 183 IMU_6AXES_StatusTypeDef LSM6DS3_Get_Status_Free_Fall_Detection( uint8_t *status );
Wolfgang Betz 44:d757094f6229 184
Wolfgang Betz 44:d757094f6229 185 IMU_6AXES_StatusTypeDef LSM6DS3_Common_Sensor_Enable(void);
Wolfgang Betz 44:d757094f6229 186 IMU_6AXES_StatusTypeDef LSM6DS3_X_Set_Axes_Status(uint8_t enableX, uint8_t enableY, uint8_t enableZ);
Wolfgang Betz 44:d757094f6229 187 IMU_6AXES_StatusTypeDef LSM6DS3_G_Set_Axes_Status(uint8_t enableX, uint8_t enableY, uint8_t enableZ);
Wolfgang Betz 44:d757094f6229 188
Wolfgang Betz 44:d757094f6229 189 /**
Wolfgang Betz 44:d757094f6229 190 * @brief Configures LSM6DS3 interrupt lines for NUCLEO boards
Wolfgang Betz 44:d757094f6229 191 * @param None
Wolfgang Betz 44:d757094f6229 192 * @retval None
Wolfgang Betz 44:d757094f6229 193 */
Wolfgang Betz 44:d757094f6229 194 void LSM6DS3_IO_ITConfig(void)
Wolfgang Betz 44:d757094f6229 195 {
Wolfgang Betz 52:54553fd15b50 196 /* done in constructor */
Wolfgang Betz 44:d757094f6229 197 }
Wolfgang Betz 44:d757094f6229 198
Wolfgang Betz 44:d757094f6229 199 /**
Wolfgang Betz 44:d757094f6229 200 * @brief Configures LSM6DS3 I2C interface
Wolfgang Betz 44:d757094f6229 201 * @param None
Wolfgang Betz 44:d757094f6229 202 * @retval IMU_6AXES_OK in case of success, an error code otherwise
Wolfgang Betz 44:d757094f6229 203 */
Wolfgang Betz 44:d757094f6229 204 IMU_6AXES_StatusTypeDef LSM6DS3_IO_Init(void)
Wolfgang Betz 44:d757094f6229 205 {
Wolfgang Betz 44:d757094f6229 206 return IMU_6AXES_OK; /* done in constructor */
Wolfgang Betz 44:d757094f6229 207 }
Wolfgang Betz 44:d757094f6229 208
Wolfgang Betz 44:d757094f6229 209 /**
Wolfgang Betz 44:d757094f6229 210 * @brief utility function to read data from STC3115
Wolfgang Betz 44:d757094f6229 211 * @param pBuffer: pointer to data to be read.
Wolfgang Betz 44:d757094f6229 212 * @param RegisterAddr: specifies internal address register to read from.
Wolfgang Betz 44:d757094f6229 213 * @param NumByteToRead: number of bytes to be read.
Wolfgang Betz 44:d757094f6229 214 * @retval IMU_6AXES_OK if ok, IMU_6AXES_ERROR if an I2C error has occured
Wolfgang Betz 44:d757094f6229 215 */
Wolfgang Betz 44:d757094f6229 216 IMU_6AXES_StatusTypeDef LSM6DS3_IO_Read(uint8_t* pBuffer,
Wolfgang Betz 44:d757094f6229 217 uint8_t RegisterAddr, uint16_t NumByteToRead)
Wolfgang Betz 44:d757094f6229 218 {
Wolfgang Betz 44:d757094f6229 219 int ret = dev_i2c.i2c_read(pBuffer,
Wolfgang Betz 44:d757094f6229 220 LSM6DS3_XG_MEMS_ADDRESS,
Wolfgang Betz 44:d757094f6229 221 RegisterAddr,
Wolfgang Betz 44:d757094f6229 222 NumByteToRead);
Wolfgang Betz 44:d757094f6229 223 if(ret != 0) {
Wolfgang Betz 44:d757094f6229 224 return IMU_6AXES_ERROR;
Wolfgang Betz 44:d757094f6229 225 }
Wolfgang Betz 44:d757094f6229 226 return IMU_6AXES_OK;
Wolfgang Betz 44:d757094f6229 227 }
Wolfgang Betz 44:d757094f6229 228
Wolfgang Betz 44:d757094f6229 229 /**
Wolfgang Betz 44:d757094f6229 230 * @brief utility function to write data to STC3115
Wolfgang Betz 44:d757094f6229 231 * @param pBuffer: pointer to buffer to be filled.
Wolfgang Betz 44:d757094f6229 232 * @param RegisterAddr: specifies internal address register to read from.
Wolfgang Betz 44:d757094f6229 233 * @param NumByteToWrite: number of bytes to write.
Wolfgang Betz 44:d757094f6229 234 * @retval 0 if ok, -1 if an I2C error has occured
Wolfgang Betz 44:d757094f6229 235 */
Wolfgang Betz 44:d757094f6229 236 IMU_6AXES_StatusTypeDef LSM6DS3_IO_Write(uint8_t* pBuffer,
Wolfgang Betz 44:d757094f6229 237 uint8_t RegisterAddr, uint16_t NumByteToWrite)
Wolfgang Betz 44:d757094f6229 238 {
Wolfgang Betz 44:d757094f6229 239 int ret = dev_i2c.i2c_write(pBuffer,
Wolfgang Betz 44:d757094f6229 240 LSM6DS3_XG_MEMS_ADDRESS,
Wolfgang Betz 44:d757094f6229 241 RegisterAddr,
Wolfgang Betz 44:d757094f6229 242 NumByteToWrite);
Wolfgang Betz 44:d757094f6229 243 if(ret != 0) {
Wolfgang Betz 44:d757094f6229 244 return IMU_6AXES_ERROR;
Wolfgang Betz 44:d757094f6229 245 }
Wolfgang Betz 44:d757094f6229 246 return IMU_6AXES_OK;
Wolfgang Betz 44:d757094f6229 247 }
Wolfgang Betz 44:d757094f6229 248
Wolfgang Betz 44:d757094f6229 249 /*** Instance Variables ***/
Wolfgang Betz 44:d757094f6229 250 /* IO Device */
Wolfgang Betz 44:d757094f6229 251 DevI2C &dev_i2c;
Wolfgang Betz 52:54553fd15b50 252
Wolfgang Betz 52:54553fd15b50 253 /* Free Fall Detection IRQ */
Wolfgang Betz 52:54553fd15b50 254 InterruptIn free_fall;
Wolfgang Betz 44:d757094f6229 255 };
Wolfgang Betz 44:d757094f6229 256
Wolfgang Betz 44:d757094f6229 257 #endif // __LSM6DS3_CLASS_H