Example of free fall detection for LSM6DSL in X-NUCLEO-IKS01A2

Dependencies:   X_NUCLEO_IKS01A2 mbed

Fork of FreeFall_IKS01A2 by ST Expansion SW Team

Free Fall Detection Demo Application based on sensor expansion board X-NUCLEO-IKS01A2

Main function is to show how to detect the free fall event using the sensor expansion board and send a notification using UART to a connected PC or Desktop and display it on terminal applications like TeraTerm.
After connection has been established:
- the user can try to leave falling the board and then view the notification using an hyper terminal. When the free fall is detected, the LED is switched on for a while.
- the user button can be used to enable/disable the free fall detection feature.

Committer:
cparata
Date:
Fri Aug 12 13:39:06 2016 +0000
Revision:
0:6a670fda63c2
First release of Free Fall for LSM6DSL in IKS01A2

Who changed what in which revision?

UserRevisionLine numberNew contents of line
cparata 0:6a670fda63c2 1 /**
cparata 0:6a670fda63c2 2 ******************************************************************************
cparata 0:6a670fda63c2 3 * @file HTS221Sensor.cpp
cparata 0:6a670fda63c2 4 * @author AST
cparata 0:6a670fda63c2 5 * @version V1.0.0
cparata 0:6a670fda63c2 6 * @date 5 August 2016
cparata 0:6a670fda63c2 7 * @brief Implementation of an HTS221 Humidity and Temperature sensor.
cparata 0:6a670fda63c2 8 ******************************************************************************
cparata 0:6a670fda63c2 9 * @attention
cparata 0:6a670fda63c2 10 *
cparata 0:6a670fda63c2 11 * <h2><center>&copy; COPYRIGHT(c) 2016 STMicroelectronics</center></h2>
cparata 0:6a670fda63c2 12 *
cparata 0:6a670fda63c2 13 * Redistribution and use in source and binary forms, with or without modification,
cparata 0:6a670fda63c2 14 * are permitted provided that the following conditions are met:
cparata 0:6a670fda63c2 15 * 1. Redistributions of source code must retain the above copyright notice,
cparata 0:6a670fda63c2 16 * this list of conditions and the following disclaimer.
cparata 0:6a670fda63c2 17 * 2. Redistributions in binary form must reproduce the above copyright notice,
cparata 0:6a670fda63c2 18 * this list of conditions and the following disclaimer in the documentation
cparata 0:6a670fda63c2 19 * and/or other materials provided with the distribution.
cparata 0:6a670fda63c2 20 * 3. Neither the name of STMicroelectronics nor the names of its contributors
cparata 0:6a670fda63c2 21 * may be used to endorse or promote products derived from this software
cparata 0:6a670fda63c2 22 * without specific prior written permission.
cparata 0:6a670fda63c2 23 *
cparata 0:6a670fda63c2 24 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
cparata 0:6a670fda63c2 25 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
cparata 0:6a670fda63c2 26 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
cparata 0:6a670fda63c2 27 * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
cparata 0:6a670fda63c2 28 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
cparata 0:6a670fda63c2 29 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
cparata 0:6a670fda63c2 30 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
cparata 0:6a670fda63c2 31 * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
cparata 0:6a670fda63c2 32 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
cparata 0:6a670fda63c2 33 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
cparata 0:6a670fda63c2 34 *
cparata 0:6a670fda63c2 35 ******************************************************************************
cparata 0:6a670fda63c2 36 */
cparata 0:6a670fda63c2 37
cparata 0:6a670fda63c2 38
cparata 0:6a670fda63c2 39 /* Includes ------------------------------------------------------------------*/
cparata 0:6a670fda63c2 40
cparata 0:6a670fda63c2 41 #include "mbed.h"
cparata 0:6a670fda63c2 42 #include "DevI2C.h"
cparata 0:6a670fda63c2 43 #include "HTS221Sensor.h"
cparata 0:6a670fda63c2 44 #include "HTS221_Driver.h"
cparata 0:6a670fda63c2 45
cparata 0:6a670fda63c2 46
cparata 0:6a670fda63c2 47 /* Class Implementation ------------------------------------------------------*/
cparata 0:6a670fda63c2 48
cparata 0:6a670fda63c2 49 /** Constructor
cparata 0:6a670fda63c2 50 * @param i2c object of an helper class which handles the I2C peripheral
cparata 0:6a670fda63c2 51 * @param address the address of the component's instance
cparata 0:6a670fda63c2 52 */
cparata 0:6a670fda63c2 53 HTS221Sensor::HTS221Sensor(DevI2C &i2c) : dev_i2c(i2c)
cparata 0:6a670fda63c2 54 {
cparata 0:6a670fda63c2 55 address = HTS221_I2C_ADDRESS;
cparata 0:6a670fda63c2 56
cparata 0:6a670fda63c2 57 /* Power down the device */
cparata 0:6a670fda63c2 58 if ( HTS221_DeActivate( (void *)this ) == HTS221_ERROR )
cparata 0:6a670fda63c2 59 {
cparata 0:6a670fda63c2 60 return;
cparata 0:6a670fda63c2 61 }
cparata 0:6a670fda63c2 62
cparata 0:6a670fda63c2 63 /* Enable BDU */
cparata 0:6a670fda63c2 64 if ( HTS221_Set_BduMode( (void *)this, HTS221_ENABLE ) == HTS221_ERROR )
cparata 0:6a670fda63c2 65 {
cparata 0:6a670fda63c2 66 return;
cparata 0:6a670fda63c2 67 }
cparata 0:6a670fda63c2 68
cparata 0:6a670fda63c2 69 if(SetODR(1.0f) == HTS221_STATUS_ERROR)
cparata 0:6a670fda63c2 70 {
cparata 0:6a670fda63c2 71 return;
cparata 0:6a670fda63c2 72 }
cparata 0:6a670fda63c2 73 };
cparata 0:6a670fda63c2 74
cparata 0:6a670fda63c2 75
cparata 0:6a670fda63c2 76 /** Constructor
cparata 0:6a670fda63c2 77 * @param i2c object of an helper class which handles the I2C peripheral
cparata 0:6a670fda63c2 78 * @param address the address of the component's instance
cparata 0:6a670fda63c2 79 */
cparata 0:6a670fda63c2 80 HTS221Sensor::HTS221Sensor(DevI2C &i2c, uint8_t address) : dev_i2c(i2c), address(address)
cparata 0:6a670fda63c2 81 {
cparata 0:6a670fda63c2 82 /* Power down the device */
cparata 0:6a670fda63c2 83 if ( HTS221_DeActivate( (void *)this ) == HTS221_ERROR )
cparata 0:6a670fda63c2 84 {
cparata 0:6a670fda63c2 85 return;
cparata 0:6a670fda63c2 86 }
cparata 0:6a670fda63c2 87
cparata 0:6a670fda63c2 88 /* Enable BDU */
cparata 0:6a670fda63c2 89 if ( HTS221_Set_BduMode( (void *)this, HTS221_ENABLE ) == HTS221_ERROR )
cparata 0:6a670fda63c2 90 {
cparata 0:6a670fda63c2 91 return;
cparata 0:6a670fda63c2 92 }
cparata 0:6a670fda63c2 93
cparata 0:6a670fda63c2 94 if(SetODR(1.0f) == HTS221_STATUS_ERROR)
cparata 0:6a670fda63c2 95 {
cparata 0:6a670fda63c2 96 return;
cparata 0:6a670fda63c2 97 }
cparata 0:6a670fda63c2 98 };
cparata 0:6a670fda63c2 99
cparata 0:6a670fda63c2 100 /**
cparata 0:6a670fda63c2 101 * @brief Enable HTS221
cparata 0:6a670fda63c2 102 * @retval HTS221_STATUS_OK in case of success, an error code otherwise
cparata 0:6a670fda63c2 103 */
cparata 0:6a670fda63c2 104 HTS221StatusTypeDef HTS221Sensor::Enable(void)
cparata 0:6a670fda63c2 105 {
cparata 0:6a670fda63c2 106 /* Power up the device */
cparata 0:6a670fda63c2 107 if ( HTS221_Activate( (void *)this ) == HTS221_ERROR )
cparata 0:6a670fda63c2 108 {
cparata 0:6a670fda63c2 109 return HTS221_STATUS_ERROR;
cparata 0:6a670fda63c2 110 }
cparata 0:6a670fda63c2 111
cparata 0:6a670fda63c2 112 return HTS221_STATUS_OK;
cparata 0:6a670fda63c2 113 }
cparata 0:6a670fda63c2 114
cparata 0:6a670fda63c2 115 /**
cparata 0:6a670fda63c2 116 * @brief Disable HTS221
cparata 0:6a670fda63c2 117 * @retval HTS221_STATUS_OK in case of success, an error code otherwise
cparata 0:6a670fda63c2 118 */
cparata 0:6a670fda63c2 119 HTS221StatusTypeDef HTS221Sensor::Disable(void)
cparata 0:6a670fda63c2 120 {
cparata 0:6a670fda63c2 121 /* Power up the device */
cparata 0:6a670fda63c2 122 if ( HTS221_DeActivate( (void *)this ) == HTS221_ERROR )
cparata 0:6a670fda63c2 123 {
cparata 0:6a670fda63c2 124 return HTS221_STATUS_ERROR;
cparata 0:6a670fda63c2 125 }
cparata 0:6a670fda63c2 126
cparata 0:6a670fda63c2 127 return HTS221_STATUS_OK;
cparata 0:6a670fda63c2 128 }
cparata 0:6a670fda63c2 129
cparata 0:6a670fda63c2 130 /**
cparata 0:6a670fda63c2 131 * @brief Read ID address of HTS221
cparata 0:6a670fda63c2 132 * @param ht_id the pointer where the ID of the device is stored
cparata 0:6a670fda63c2 133 * @retval HTS221_STATUS_OK in case of success, an error code otherwise
cparata 0:6a670fda63c2 134 */
cparata 0:6a670fda63c2 135 HTS221StatusTypeDef HTS221Sensor::ReadID(uint8_t *ht_id)
cparata 0:6a670fda63c2 136 {
cparata 0:6a670fda63c2 137 /* Read WHO AM I register */
cparata 0:6a670fda63c2 138 if ( HTS221_Get_DeviceID( (void *)this, ht_id ) == HTS221_ERROR )
cparata 0:6a670fda63c2 139 {
cparata 0:6a670fda63c2 140 return HTS221_STATUS_ERROR;
cparata 0:6a670fda63c2 141 }
cparata 0:6a670fda63c2 142
cparata 0:6a670fda63c2 143 return HTS221_STATUS_OK;
cparata 0:6a670fda63c2 144 }
cparata 0:6a670fda63c2 145
cparata 0:6a670fda63c2 146 /**
cparata 0:6a670fda63c2 147 * @brief Reboot memory content of HTS221
cparata 0:6a670fda63c2 148 * @param None
cparata 0:6a670fda63c2 149 * @retval HTS221_STATUS_OK in case of success, an error code otherwise
cparata 0:6a670fda63c2 150 */
cparata 0:6a670fda63c2 151 HTS221StatusTypeDef HTS221Sensor::Reset(void)
cparata 0:6a670fda63c2 152 {
cparata 0:6a670fda63c2 153 uint8_t tmpreg;
cparata 0:6a670fda63c2 154
cparata 0:6a670fda63c2 155 /* Read CTRL_REG2 register */
cparata 0:6a670fda63c2 156 if (ReadReg(HTS221_CTRL_REG2, &tmpreg) != HTS221_STATUS_OK)
cparata 0:6a670fda63c2 157 {
cparata 0:6a670fda63c2 158 return HTS221_STATUS_ERROR;
cparata 0:6a670fda63c2 159 }
cparata 0:6a670fda63c2 160
cparata 0:6a670fda63c2 161 /* Enable or Disable the reboot memory */
cparata 0:6a670fda63c2 162 tmpreg |= (0x01 << HTS221_BOOT_BIT);
cparata 0:6a670fda63c2 163
cparata 0:6a670fda63c2 164 /* Write value to MEMS CTRL_REG2 regsister */
cparata 0:6a670fda63c2 165 if (WriteReg(HTS221_CTRL_REG2, tmpreg) != HTS221_STATUS_OK)
cparata 0:6a670fda63c2 166 {
cparata 0:6a670fda63c2 167 return HTS221_STATUS_ERROR;
cparata 0:6a670fda63c2 168 }
cparata 0:6a670fda63c2 169
cparata 0:6a670fda63c2 170 return HTS221_STATUS_OK;
cparata 0:6a670fda63c2 171 }
cparata 0:6a670fda63c2 172
cparata 0:6a670fda63c2 173 /**
cparata 0:6a670fda63c2 174 * @brief Read HTS221 output register, and calculate the humidity
cparata 0:6a670fda63c2 175 * @param pfData the pointer to data output
cparata 0:6a670fda63c2 176 * @retval HTS221_STATUS_OK in case of success, an error code otherwise
cparata 0:6a670fda63c2 177 */
cparata 0:6a670fda63c2 178 HTS221StatusTypeDef HTS221Sensor::GetHumidity(float* pfData)
cparata 0:6a670fda63c2 179 {
cparata 0:6a670fda63c2 180 uint16_t uint16data = 0;
cparata 0:6a670fda63c2 181
cparata 0:6a670fda63c2 182 /* Read data from HTS221. */
cparata 0:6a670fda63c2 183 if ( HTS221_Get_Humidity( (void *)this, &uint16data ) == HTS221_ERROR )
cparata 0:6a670fda63c2 184 {
cparata 0:6a670fda63c2 185 return HTS221_STATUS_ERROR;
cparata 0:6a670fda63c2 186 }
cparata 0:6a670fda63c2 187
cparata 0:6a670fda63c2 188 *pfData = ( float )uint16data / 10.0f;
cparata 0:6a670fda63c2 189
cparata 0:6a670fda63c2 190 return HTS221_STATUS_OK;
cparata 0:6a670fda63c2 191 }
cparata 0:6a670fda63c2 192
cparata 0:6a670fda63c2 193 /**
cparata 0:6a670fda63c2 194 * @brief Read HTS221 output register, and calculate the temperature
cparata 0:6a670fda63c2 195 * @param pfData the pointer to data output
cparata 0:6a670fda63c2 196 * @retval HTS221_STATUS_OK in case of success, an error code otherwise
cparata 0:6a670fda63c2 197 */
cparata 0:6a670fda63c2 198 HTS221StatusTypeDef HTS221Sensor::GetTemperature(float* pfData)
cparata 0:6a670fda63c2 199 {
cparata 0:6a670fda63c2 200 int16_t int16data = 0;
cparata 0:6a670fda63c2 201
cparata 0:6a670fda63c2 202 /* Read data from HTS221. */
cparata 0:6a670fda63c2 203 if ( HTS221_Get_Temperature( (void *)this, &int16data ) == HTS221_ERROR )
cparata 0:6a670fda63c2 204 {
cparata 0:6a670fda63c2 205 return HTS221_STATUS_ERROR;
cparata 0:6a670fda63c2 206 }
cparata 0:6a670fda63c2 207
cparata 0:6a670fda63c2 208 *pfData = ( float )int16data / 10.0f;
cparata 0:6a670fda63c2 209
cparata 0:6a670fda63c2 210 return HTS221_STATUS_OK;
cparata 0:6a670fda63c2 211 }
cparata 0:6a670fda63c2 212
cparata 0:6a670fda63c2 213 /**
cparata 0:6a670fda63c2 214 * @brief Read HTS221 output register, and calculate the humidity
cparata 0:6a670fda63c2 215 * @param odr the pointer to the output data rate
cparata 0:6a670fda63c2 216 * @retval HTS221_STATUS_OK in case of success, an error code otherwise
cparata 0:6a670fda63c2 217 */
cparata 0:6a670fda63c2 218 HTS221StatusTypeDef HTS221Sensor::GetODR(float* odr)
cparata 0:6a670fda63c2 219 {
cparata 0:6a670fda63c2 220 HTS221_Odr_et odr_low_level;
cparata 0:6a670fda63c2 221
cparata 0:6a670fda63c2 222 if ( HTS221_Get_Odr( (void *)this, &odr_low_level ) == HTS221_ERROR )
cparata 0:6a670fda63c2 223 {
cparata 0:6a670fda63c2 224 return HTS221_STATUS_ERROR;
cparata 0:6a670fda63c2 225 }
cparata 0:6a670fda63c2 226
cparata 0:6a670fda63c2 227 switch( odr_low_level )
cparata 0:6a670fda63c2 228 {
cparata 0:6a670fda63c2 229 case HTS221_ODR_ONE_SHOT:
cparata 0:6a670fda63c2 230 *odr = 0.0f;
cparata 0:6a670fda63c2 231 break;
cparata 0:6a670fda63c2 232 case HTS221_ODR_1HZ :
cparata 0:6a670fda63c2 233 *odr = 1.0f;
cparata 0:6a670fda63c2 234 break;
cparata 0:6a670fda63c2 235 case HTS221_ODR_7HZ :
cparata 0:6a670fda63c2 236 *odr = 7.0f;
cparata 0:6a670fda63c2 237 break;
cparata 0:6a670fda63c2 238 case HTS221_ODR_12_5HZ :
cparata 0:6a670fda63c2 239 *odr = 12.5f;
cparata 0:6a670fda63c2 240 break;
cparata 0:6a670fda63c2 241 default :
cparata 0:6a670fda63c2 242 *odr = -1.0f;
cparata 0:6a670fda63c2 243 return HTS221_STATUS_ERROR;
cparata 0:6a670fda63c2 244 }
cparata 0:6a670fda63c2 245
cparata 0:6a670fda63c2 246 return HTS221_STATUS_OK;
cparata 0:6a670fda63c2 247 }
cparata 0:6a670fda63c2 248
cparata 0:6a670fda63c2 249 /**
cparata 0:6a670fda63c2 250 * @brief Set ODR
cparata 0:6a670fda63c2 251 * @param odr the output data rate to be set
cparata 0:6a670fda63c2 252 * @retval HTS221_STATUS_OK in case of success, an error code otherwise
cparata 0:6a670fda63c2 253 */
cparata 0:6a670fda63c2 254 HTS221StatusTypeDef HTS221Sensor::SetODR(float odr)
cparata 0:6a670fda63c2 255 {
cparata 0:6a670fda63c2 256 HTS221_Odr_et new_odr;
cparata 0:6a670fda63c2 257
cparata 0:6a670fda63c2 258 new_odr = ( odr <= 1.0f ) ? HTS221_ODR_1HZ
cparata 0:6a670fda63c2 259 : ( odr <= 7.0f ) ? HTS221_ODR_7HZ
cparata 0:6a670fda63c2 260 : HTS221_ODR_12_5HZ;
cparata 0:6a670fda63c2 261
cparata 0:6a670fda63c2 262 if ( HTS221_Set_Odr( (void *)this, new_odr ) == HTS221_ERROR )
cparata 0:6a670fda63c2 263 {
cparata 0:6a670fda63c2 264 return HTS221_STATUS_ERROR;
cparata 0:6a670fda63c2 265 }
cparata 0:6a670fda63c2 266
cparata 0:6a670fda63c2 267 return HTS221_STATUS_OK;
cparata 0:6a670fda63c2 268 }
cparata 0:6a670fda63c2 269
cparata 0:6a670fda63c2 270
cparata 0:6a670fda63c2 271 /**
cparata 0:6a670fda63c2 272 * @brief Read the data from register
cparata 0:6a670fda63c2 273 * @param reg register address
cparata 0:6a670fda63c2 274 * @param data register data
cparata 0:6a670fda63c2 275 * @retval HTS221_STATUS_OK in case of success
cparata 0:6a670fda63c2 276 * @retval HTS221_STATUS_ERROR in case of failure
cparata 0:6a670fda63c2 277 */
cparata 0:6a670fda63c2 278 HTS221StatusTypeDef HTS221Sensor::ReadReg( uint8_t reg, uint8_t *data )
cparata 0:6a670fda63c2 279 {
cparata 0:6a670fda63c2 280
cparata 0:6a670fda63c2 281 if ( HTS221_ReadReg( (void *)this, reg, 1, data ) == HTS221_ERROR )
cparata 0:6a670fda63c2 282 {
cparata 0:6a670fda63c2 283 return HTS221_STATUS_ERROR;
cparata 0:6a670fda63c2 284 }
cparata 0:6a670fda63c2 285
cparata 0:6a670fda63c2 286 return HTS221_STATUS_OK;
cparata 0:6a670fda63c2 287 }
cparata 0:6a670fda63c2 288
cparata 0:6a670fda63c2 289 /**
cparata 0:6a670fda63c2 290 * @brief Write the data to register
cparata 0:6a670fda63c2 291 * @param reg register address
cparata 0:6a670fda63c2 292 * @param data register data
cparata 0:6a670fda63c2 293 * @retval HTS221_STATUS_OK in case of success
cparata 0:6a670fda63c2 294 * @retval HTS221_STATUS_ERROR in case of failure
cparata 0:6a670fda63c2 295 */
cparata 0:6a670fda63c2 296 HTS221StatusTypeDef HTS221Sensor::WriteReg( uint8_t reg, uint8_t data )
cparata 0:6a670fda63c2 297 {
cparata 0:6a670fda63c2 298
cparata 0:6a670fda63c2 299 if ( HTS221_WriteReg( (void *)this, reg, 1, &data ) == HTS221_ERROR )
cparata 0:6a670fda63c2 300 {
cparata 0:6a670fda63c2 301 return HTS221_STATUS_ERROR;
cparata 0:6a670fda63c2 302 }
cparata 0:6a670fda63c2 303
cparata 0:6a670fda63c2 304 return HTS221_STATUS_OK;
cparata 0:6a670fda63c2 305 }
cparata 0:6a670fda63c2 306
cparata 0:6a670fda63c2 307 uint8_t HTS221_IO_Write( void *handle, uint8_t WriteAddr, uint8_t *pBuffer, uint16_t nBytesToWrite )
cparata 0:6a670fda63c2 308 {
cparata 0:6a670fda63c2 309 return ((HTS221Sensor *)handle)->IO_Write(pBuffer, WriteAddr, nBytesToWrite);
cparata 0:6a670fda63c2 310 }
cparata 0:6a670fda63c2 311
cparata 0:6a670fda63c2 312 uint8_t HTS221_IO_Read( void *handle, uint8_t ReadAddr, uint8_t *pBuffer, uint16_t nBytesToRead )
cparata 0:6a670fda63c2 313 {
cparata 0:6a670fda63c2 314 return ((HTS221Sensor *)handle)->IO_Read(pBuffer, ReadAddr, nBytesToRead);
cparata 0:6a670fda63c2 315 }