Example of multiple event detection for LSM6DSL in X-NUCLEO-IKS01A2

Dependencies:   X_NUCLEO_IKS01A2 mbed

Fork of MultiEvent_IKS01A2 by ST Expansion SW Team

Multi Event Demo Application based on sensor expansion board X-NUCLEO-IKS01A2

Main function is to show how to detect the free fall, tap, double tap, tilt, wake up, 6D Orientation and step events 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 simulate all the events and then view the notification using an hyper terminal.
- the user button can be used to enable/disable all hardware features.

Committer:
cparata
Date:
Thu Nov 24 15:42:24 2016 +0000
Revision:
0:763b80993db4
Add multi event example

Who changed what in which revision?

UserRevisionLine numberNew contents of line
cparata 0:763b80993db4 1 /**
cparata 0:763b80993db4 2 ******************************************************************************
cparata 0:763b80993db4 3 * @file DevSPI.h
cparata 0:763b80993db4 4 * @author AST / Software Platforms and Cloud / EST
cparata 0:763b80993db4 5 * @version V1.2.1
cparata 0:763b80993db4 6 * @date 19-February-2016
cparata 0:763b80993db4 7 * @brief Header file for a special SPI class DevSPI which provides some
cparata 0:763b80993db4 8 * helper functions for on-board communication.
cparata 0:763b80993db4 9 ******************************************************************************
cparata 0:763b80993db4 10 * @attention
cparata 0:763b80993db4 11 *
cparata 0:763b80993db4 12 * <h2><center>&copy; COPYRIGHT(c) 2016 STMicroelectronics</center></h2>
cparata 0:763b80993db4 13 *
cparata 0:763b80993db4 14 * Redistribution and use in source and binary forms, with or without modification,
cparata 0:763b80993db4 15 * are permitted provided that the following conditions are met:
cparata 0:763b80993db4 16 * 1. Redistributions of source code must retain the above copyright notice,
cparata 0:763b80993db4 17 * this list of conditions and the following disclaimer.
cparata 0:763b80993db4 18 * 2. Redistributions in binary form must reproduce the above copyright notice,
cparata 0:763b80993db4 19 * this list of conditions and the following disclaimer in the documentation
cparata 0:763b80993db4 20 * and/or other materials provided with the distribution.
cparata 0:763b80993db4 21 * 3. Neither the name of STMicroelectronics nor the names of its contributors
cparata 0:763b80993db4 22 * may be used to endorse or promote products derived from this software
cparata 0:763b80993db4 23 * without specific prior written permission.
cparata 0:763b80993db4 24 *
cparata 0:763b80993db4 25 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
cparata 0:763b80993db4 26 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
cparata 0:763b80993db4 27 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
cparata 0:763b80993db4 28 * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
cparata 0:763b80993db4 29 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
cparata 0:763b80993db4 30 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
cparata 0:763b80993db4 31 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
cparata 0:763b80993db4 32 * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
cparata 0:763b80993db4 33 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
cparata 0:763b80993db4 34 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
cparata 0:763b80993db4 35 *
cparata 0:763b80993db4 36 ******************************************************************************
cparata 0:763b80993db4 37 */
cparata 0:763b80993db4 38
cparata 0:763b80993db4 39 /* Define to prevent from recursive inclusion --------------------------------*/
cparata 0:763b80993db4 40 #ifndef __DEV_SPI_H
cparata 0:763b80993db4 41 #define __DEV_SPI_H
cparata 0:763b80993db4 42
cparata 0:763b80993db4 43 /* Includes ------------------------------------------------------------------*/
cparata 0:763b80993db4 44 #include "mbed.h"
cparata 0:763b80993db4 45
cparata 0:763b80993db4 46 /* Macros --------------------------------------------------------------------*/
cparata 0:763b80993db4 47 #if (defined(__BYTE_ORDER__) && (__BYTE_ORDER__ == __ORDER_BIG_ENDIAN__)) /* GCC */ || \
cparata 0:763b80993db4 48 (defined(G_BYTE_ORDER) && (G_BYTE_ORDER == G_BIG_ENDIAN)) /* IAR */ || \
cparata 0:763b80993db4 49 (defined(__BIG_ENDIAN)) /* ARM */
cparata 0:763b80993db4 50 #define __DEV_SPI_BIG_ENDIAN
cparata 0:763b80993db4 51 #endif
cparata 0:763b80993db4 52
cparata 0:763b80993db4 53 /* Classes -------------------------------------------------------------------*/
cparata 0:763b80993db4 54 /** Helper class DevSPI providing functions for synchronous SPI communication
cparata 0:763b80993db4 55 * common for a series of SPI devices.
cparata 0:763b80993db4 56 */
cparata 0:763b80993db4 57 class DevSPI : public SPI
cparata 0:763b80993db4 58 {
cparata 0:763b80993db4 59 public:
cparata 0:763b80993db4 60 /*
cparata 0:763b80993db4 61 * Create a DevSPI interface.
cparata 0:763b80993db4 62 * @param mosi pin name of the MOSI pin of the SPI device to be used for communication.
cparata 0:763b80993db4 63 * @param miso pin name of the MISO pin of the SPI device to be used for communication.
cparata 0:763b80993db4 64 * @param sclk pin name of the SCLK pin of the SPI device to be used for communication.
cparata 0:763b80993db4 65 */
cparata 0:763b80993db4 66 DevSPI(PinName mosi, PinName miso, PinName sclk) : SPI(mosi, miso, sclk)
cparata 0:763b80993db4 67 {
cparata 0:763b80993db4 68 /* Set default configuration. */
cparata 0:763b80993db4 69 setup(8, 3, 1E6);
cparata 0:763b80993db4 70 }
cparata 0:763b80993db4 71
cparata 0:763b80993db4 72 /*
cparata 0:763b80993db4 73 * Setup the spi.
cparata 0:763b80993db4 74 * Typically:
cparata 0:763b80993db4 75 * + 8 bit data;
cparata 0:763b80993db4 76 * + high steady state clock;
cparata 0:763b80993db4 77 * + second edge capture;
cparata 0:763b80993db4 78 * + 1MHz clock rate.
cparata 0:763b80993db4 79 *
cparata 0:763b80993db4 80 * @param bits Number of bits per SPI frame (4 - 16)
cparata 0:763b80993db4 81 * @param mode Clock polarity and phase mode (0 - 3)
cparata 0:763b80993db4 82 * @param frequency_hz SCLK frequency in hz (default = 1MHz)
cparata 0:763b80993db4 83 *
cparata 0:763b80993db4 84 * @code
cparata 0:763b80993db4 85 * mode | POL PHA
cparata 0:763b80993db4 86 * -----+--------
cparata 0:763b80993db4 87 * 0 | 0 0
cparata 0:763b80993db4 88 * 1 | 0 1
cparata 0:763b80993db4 89 * 2 | 1 0
cparata 0:763b80993db4 90 * 3 | 1 1
cparata 0:763b80993db4 91 * @endcode
cparata 0:763b80993db4 92 */
cparata 0:763b80993db4 93 void setup(int bits, int mode = 0, int frequency_hz = 1E6)
cparata 0:763b80993db4 94 {
cparata 0:763b80993db4 95 /* Set given configuration. */
cparata 0:763b80993db4 96 format(bits, mode);
cparata 0:763b80993db4 97 frequency(frequency_hz);
cparata 0:763b80993db4 98 }
cparata 0:763b80993db4 99
cparata 0:763b80993db4 100 /**
cparata 0:763b80993db4 101 * @brief Writes a buffer to the SPI peripheral device in 8-bit data mode
cparata 0:763b80993db4 102 * using synchronous SPI communication.
cparata 0:763b80993db4 103 * @param[in] pBuffer pointer to the buffer of data to send.
cparata 0:763b80993db4 104 * @param[in] ssel GPIO of the SSEL pin of the SPI device to be used for communication.
cparata 0:763b80993db4 105 * @param[in] NumBytesToWrite number of bytes to write.
cparata 0:763b80993db4 106 * @retval 0 if ok.
cparata 0:763b80993db4 107 * @retval -1 if data format error.
cparata 0:763b80993db4 108 * @note When using the SPI in Interrupt-mode, remember to disable interrupts
cparata 0:763b80993db4 109 * before calling this function and to enable them again after.
cparata 0:763b80993db4 110 */
cparata 0:763b80993db4 111 int spi_write(uint8_t* pBuffer, DigitalOut &ssel, uint16_t NumBytesToWrite)
cparata 0:763b80993db4 112 {
cparata 0:763b80993db4 113 /* Check data format */
cparata 0:763b80993db4 114 if(_bits != 8) return -1;
cparata 0:763b80993db4 115
cparata 0:763b80993db4 116 /* Select the chip. */
cparata 0:763b80993db4 117 ssel = 0;
cparata 0:763b80993db4 118
cparata 0:763b80993db4 119 /* Write data. */
cparata 0:763b80993db4 120 for (int i = 0; i < NumBytesToWrite; i++) {
cparata 0:763b80993db4 121 write(pBuffer[i]);
cparata 0:763b80993db4 122 }
cparata 0:763b80993db4 123
cparata 0:763b80993db4 124 /* Unselect the chip. */
cparata 0:763b80993db4 125 ssel = 1;
cparata 0:763b80993db4 126
cparata 0:763b80993db4 127 return 0;
cparata 0:763b80993db4 128 }
cparata 0:763b80993db4 129
cparata 0:763b80993db4 130 /**
cparata 0:763b80993db4 131 * @brief Reads a buffer from the SPI peripheral device in 8-bit data mode
cparata 0:763b80993db4 132 * using synchronous SPI communication.
cparata 0:763b80993db4 133 * @param[out] pBuffer pointer to the buffer to read data into.
cparata 0:763b80993db4 134 * @param[in] ssel GPIO of the SSEL pin of the SPI device to be used for communication.
cparata 0:763b80993db4 135 * @param[in] NumBytesToRead number of bytes to read.
cparata 0:763b80993db4 136 * @retval 0 if ok.
cparata 0:763b80993db4 137 * @retval -1 if data format error.
cparata 0:763b80993db4 138 * @note When using the SPI in Interrupt-mode, remember to disable interrupts
cparata 0:763b80993db4 139 * before calling this function and to enable them again after.
cparata 0:763b80993db4 140 */
cparata 0:763b80993db4 141 int spi_read(uint8_t* pBuffer, DigitalOut &ssel, uint16_t NumBytesToRead)
cparata 0:763b80993db4 142 {
cparata 0:763b80993db4 143 /* Check data format */
cparata 0:763b80993db4 144 if(_bits != 8) return -1;
cparata 0:763b80993db4 145
cparata 0:763b80993db4 146 /* Select the chip. */
cparata 0:763b80993db4 147 ssel = 0;
cparata 0:763b80993db4 148
cparata 0:763b80993db4 149 /* Read data. */
cparata 0:763b80993db4 150 for (int i = 0; i < NumBytesToRead; i++) {
cparata 0:763b80993db4 151 pBuffer[i] = write(0);
cparata 0:763b80993db4 152 }
cparata 0:763b80993db4 153
cparata 0:763b80993db4 154 /* Unselect the chip. */
cparata 0:763b80993db4 155 ssel = 1;
cparata 0:763b80993db4 156
cparata 0:763b80993db4 157 return 0;
cparata 0:763b80993db4 158 }
cparata 0:763b80993db4 159
cparata 0:763b80993db4 160 /**
cparata 0:763b80993db4 161 * @brief Reads and write a buffer from/to the SPI peripheral device at the same time
cparata 0:763b80993db4 162 * in 8-bit data mode using synchronous SPI communication.
cparata 0:763b80993db4 163 * @param[out] pBufferToRead pointer to the buffer to read data into.
cparata 0:763b80993db4 164 * @param[in] pBufferToWrite pointer to the buffer of data to send.
cparata 0:763b80993db4 165 * @param[in] ssel GPIO of the SSEL pin of the SPI device to be used for communication.
cparata 0:763b80993db4 166 * @param[in] NumBytes number of bytes to read and write.
cparata 0:763b80993db4 167 * @retval 0 if ok.
cparata 0:763b80993db4 168 * @retval -1 if data format error.
cparata 0:763b80993db4 169 * @note When using the SPI in Interrupt-mode, remember to disable interrupts
cparata 0:763b80993db4 170 * before calling this function and to enable them again after.
cparata 0:763b80993db4 171 */
cparata 0:763b80993db4 172 int spi_read_write(uint8_t* pBufferToRead, uint8_t* pBufferToWrite, DigitalOut &ssel, uint16_t NumBytes)
cparata 0:763b80993db4 173 {
cparata 0:763b80993db4 174 /* Check data format */
cparata 0:763b80993db4 175 if(_bits != 8) return -1;
cparata 0:763b80993db4 176
cparata 0:763b80993db4 177 /* Select the chip. */
cparata 0:763b80993db4 178 ssel = 0;
cparata 0:763b80993db4 179
cparata 0:763b80993db4 180 /* Read and write data at the same time. */
cparata 0:763b80993db4 181 for (int i = 0; i < NumBytes; i++) {
cparata 0:763b80993db4 182 pBufferToRead[i] = write(pBufferToWrite[i]);
cparata 0:763b80993db4 183 }
cparata 0:763b80993db4 184
cparata 0:763b80993db4 185 /* Unselect the chip. */
cparata 0:763b80993db4 186 ssel = 1;
cparata 0:763b80993db4 187
cparata 0:763b80993db4 188 return 0;
cparata 0:763b80993db4 189 }
cparata 0:763b80993db4 190
cparata 0:763b80993db4 191 /**
cparata 0:763b80993db4 192 * @brief Writes a buffer to the SPI peripheral device in 16-bit data mode
cparata 0:763b80993db4 193 * using synchronous SPI communication.
cparata 0:763b80993db4 194 * @param[in] pBuffer pointer to the buffer of data to send.
cparata 0:763b80993db4 195 * @param[in] ssel GPIO of the SSEL pin of the SPI device to be used for communication.
cparata 0:763b80993db4 196 * @param[in] NumValuesToWrite number of 16-bit values to write.
cparata 0:763b80993db4 197 * @retval 0 if ok.
cparata 0:763b80993db4 198 * @retval -1 if data format error.
cparata 0:763b80993db4 199 * @note When using the SPI in Interrupt-mode, remember to disable interrupts
cparata 0:763b80993db4 200 * before calling this function and to enable them again after.
cparata 0:763b80993db4 201 * @note In order to guarantee this method to work correctly you have to
cparata 0:763b80993db4 202 * pass buffers which are correctly aligned.
cparata 0:763b80993db4 203 */
cparata 0:763b80993db4 204 int spi_write(uint16_t* pBuffer, DigitalOut &ssel, uint16_t NumValuesToWrite)
cparata 0:763b80993db4 205 {
cparata 0:763b80993db4 206 /* Check data format */
cparata 0:763b80993db4 207 if(_bits != 16) return -1;
cparata 0:763b80993db4 208
cparata 0:763b80993db4 209 /* Select the chip. */
cparata 0:763b80993db4 210 ssel = 0;
cparata 0:763b80993db4 211
cparata 0:763b80993db4 212 /* Write data. */
cparata 0:763b80993db4 213 for (int i = 0; i < NumValuesToWrite; i++) {
cparata 0:763b80993db4 214 write(htons(pBuffer[i]));
cparata 0:763b80993db4 215 }
cparata 0:763b80993db4 216
cparata 0:763b80993db4 217 /* Unselect the chip. */
cparata 0:763b80993db4 218 ssel = 1;
cparata 0:763b80993db4 219
cparata 0:763b80993db4 220 return 0;
cparata 0:763b80993db4 221 }
cparata 0:763b80993db4 222
cparata 0:763b80993db4 223 /**
cparata 0:763b80993db4 224 * @brief Reads a buffer from the SPI peripheral device in 16-bit data mode
cparata 0:763b80993db4 225 * using synchronous SPI communication.
cparata 0:763b80993db4 226 * @param[out] pBuffer pointer to the buffer to read data into.
cparata 0:763b80993db4 227 * @param[in] ssel GPIO of the SSEL pin of the SPI device to be used for communication.
cparata 0:763b80993db4 228 * @param[in] NumValuesToRead number of 16-bit values to read.
cparata 0:763b80993db4 229 * @retval 0 if ok.
cparata 0:763b80993db4 230 * @retval -1 if data format error.
cparata 0:763b80993db4 231 * @note When using the SPI in Interrupt-mode, remember to disable interrupts
cparata 0:763b80993db4 232 * before calling this function and to enable them again after.
cparata 0:763b80993db4 233 * @note In order to guarantee this method to work correctly you have to
cparata 0:763b80993db4 234 * pass buffers which are correctly aligned.
cparata 0:763b80993db4 235 */
cparata 0:763b80993db4 236 int spi_read(uint16_t* pBuffer, DigitalOut &ssel, uint16_t NumValuesToRead)
cparata 0:763b80993db4 237 {
cparata 0:763b80993db4 238 /* Check data format */
cparata 0:763b80993db4 239 if(_bits != 16) return -1;
cparata 0:763b80993db4 240
cparata 0:763b80993db4 241 /* Select the chip. */
cparata 0:763b80993db4 242 ssel = 0;
cparata 0:763b80993db4 243
cparata 0:763b80993db4 244 /* Read data. */
cparata 0:763b80993db4 245 for (int i = 0; i < NumValuesToRead; i++) {
cparata 0:763b80993db4 246 pBuffer[i] = ntohs((uint16_t)write(0));
cparata 0:763b80993db4 247 }
cparata 0:763b80993db4 248
cparata 0:763b80993db4 249 /* Unselect the chip. */
cparata 0:763b80993db4 250 ssel = 1;
cparata 0:763b80993db4 251
cparata 0:763b80993db4 252 return 0;
cparata 0:763b80993db4 253 }
cparata 0:763b80993db4 254
cparata 0:763b80993db4 255 /**
cparata 0:763b80993db4 256 * @brief Reads and write a buffer from/to the SPI peripheral device at the same time
cparata 0:763b80993db4 257 * in 16-bit data mode using synchronous SPI communication.
cparata 0:763b80993db4 258 * @param[out] pBufferToRead pointer to the buffer to read data into.
cparata 0:763b80993db4 259 * @param[in] pBufferToWrite pointer to the buffer of data to send.
cparata 0:763b80993db4 260 * @param[in] ssel GPIO of the SSEL pin of the SPI device to be used for communication.
cparata 0:763b80993db4 261 * @param[in] NumValues number of 16-bit values to read and write.
cparata 0:763b80993db4 262 * @retval 0 if ok.
cparata 0:763b80993db4 263 * @retval -1 if data format error.
cparata 0:763b80993db4 264 * @note When using the SPI in Interrupt-mode, remember to disable interrupts
cparata 0:763b80993db4 265 * before calling this function and to enable them again after.
cparata 0:763b80993db4 266 * @note In order to guarantee this method to work correctly you have to
cparata 0:763b80993db4 267 * pass buffers which are correctly aligned.
cparata 0:763b80993db4 268 */
cparata 0:763b80993db4 269 int spi_read_write(uint16_t* pBufferToRead, uint16_t* pBufferToWrite, DigitalOut &ssel, uint16_t NumValues)
cparata 0:763b80993db4 270 {
cparata 0:763b80993db4 271 /* Check data format */
cparata 0:763b80993db4 272 if(_bits != 16) return -1;
cparata 0:763b80993db4 273
cparata 0:763b80993db4 274 /* Select the chip. */
cparata 0:763b80993db4 275 ssel = 0;
cparata 0:763b80993db4 276
cparata 0:763b80993db4 277 /* Read and write data at the same time. */
cparata 0:763b80993db4 278 for (int i = 0; i < NumValues; i++) {
cparata 0:763b80993db4 279 pBufferToRead[i] = ntohs((uint16_t)write(htons(pBufferToWrite[i])));
cparata 0:763b80993db4 280 }
cparata 0:763b80993db4 281
cparata 0:763b80993db4 282 /* Unselect the chip. */
cparata 0:763b80993db4 283 ssel = 1;
cparata 0:763b80993db4 284
cparata 0:763b80993db4 285 return 0;
cparata 0:763b80993db4 286 }
cparata 0:763b80993db4 287
cparata 0:763b80993db4 288 protected:
cparata 0:763b80993db4 289 inline uint16_t htons(uint16_t x) {
cparata 0:763b80993db4 290 #ifndef __DEV_SPI_BIG_ENDIAN
cparata 0:763b80993db4 291 return (((x)<<8)|((x)>>8));
cparata 0:763b80993db4 292 #else // __DEV_SPI_BIG_ENDIAN
cparata 0:763b80993db4 293 return (x);
cparata 0:763b80993db4 294 #endif // __DEV_SPI_BIG_ENDIAN
cparata 0:763b80993db4 295 }
cparata 0:763b80993db4 296
cparata 0:763b80993db4 297 inline uint16_t ntohs(uint16_t x) {
cparata 0:763b80993db4 298 return htons(x);
cparata 0:763b80993db4 299 }
cparata 0:763b80993db4 300 };
cparata 0:763b80993db4 301
cparata 0:763b80993db4 302 #endif /* __DEV_SPI_H */