Example of wake up detection for LIS2DW12 in X-NUCLEO-IKS01A3

Dependencies:   X_NUCLEO_IKS01A3

Wake up Demo Application with LIS2DW12 based on sensor expansion board X-NUCLEO-IKS01A3

Main function is to show how to detect the wake up 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 shake the board and then view the notification using an hyper terminal. When the wake up event is detected, the LED is switched on for a while.
- the user button can be used to enable/disable the wake up detection feature.

Committer:
cparata
Date:
Thu Jun 04 17:17:05 2020 +0000
Revision:
6:57d0454ddd40
Parent:
5:69cd10d5ca51
Child:
8:51f2d4e6a3e9
Update the code to be compatible with mbed OS 6

Who changed what in which revision?

UserRevisionLine numberNew contents of line
cparata 0:276fc25c1c41 1 /**
cparata 0:276fc25c1c41 2 ******************************************************************************
cparata 0:276fc25c1c41 3 * @file main.cpp
cparata 0:276fc25c1c41 4 * @author SRA
cparata 0:276fc25c1c41 5 * @version V1.0.0
cparata 0:276fc25c1c41 6 * @date 5-March-2019
cparata 5:69cd10d5ca51 7 * @brief Simple Example application for using the X_NUCLEO_IKS01A3
cparata 0:276fc25c1c41 8 * MEMS Inertial & Environmental Sensor Nucleo expansion board.
cparata 0:276fc25c1c41 9 ******************************************************************************
cparata 0:276fc25c1c41 10 * @attention
cparata 0:276fc25c1c41 11 *
cparata 0:276fc25c1c41 12 * <h2><center>&copy; COPYRIGHT(c) 2019 STMicroelectronics</center></h2>
cparata 0:276fc25c1c41 13 *
cparata 0:276fc25c1c41 14 * Redistribution and use in source and binary forms, with or without modification,
cparata 0:276fc25c1c41 15 * are permitted provided that the following conditions are met:
cparata 0:276fc25c1c41 16 * 1. Redistributions of source code must retain the above copyright notice,
cparata 0:276fc25c1c41 17 * this list of conditions and the following disclaimer.
cparata 0:276fc25c1c41 18 * 2. Redistributions in binary form must reproduce the above copyright notice,
cparata 0:276fc25c1c41 19 * this list of conditions and the following disclaimer in the documentation
cparata 0:276fc25c1c41 20 * and/or other materials provided with the distribution.
cparata 0:276fc25c1c41 21 * 3. Neither the name of STMicroelectronics nor the names of its contributors
cparata 0:276fc25c1c41 22 * may be used to endorse or promote products derived from this software
cparata 0:276fc25c1c41 23 * without specific prior written permission.
cparata 0:276fc25c1c41 24 *
cparata 0:276fc25c1c41 25 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
cparata 0:276fc25c1c41 26 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
cparata 0:276fc25c1c41 27 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
cparata 0:276fc25c1c41 28 * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
cparata 0:276fc25c1c41 29 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
cparata 0:276fc25c1c41 30 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
cparata 0:276fc25c1c41 31 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
cparata 0:276fc25c1c41 32 * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
cparata 0:276fc25c1c41 33 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
cparata 0:276fc25c1c41 34 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
cparata 0:276fc25c1c41 35 *
cparata 0:276fc25c1c41 36 ******************************************************************************
cparata 5:69cd10d5ca51 37 */
cparata 5:69cd10d5ca51 38
cparata 0:276fc25c1c41 39 /* Includes */
cparata 0:276fc25c1c41 40 #include "mbed.h"
cparata 6:57d0454ddd40 41 #include "rtos.h"
cparata 0:276fc25c1c41 42 #include "XNucleoIKS01A3.h"
cparata 5:69cd10d5ca51 43
cparata 0:276fc25c1c41 44 /* Instantiate the expansion board */
cparata 0:276fc25c1c41 45 static XNucleoIKS01A3 *mems_expansion_board = XNucleoIKS01A3::instance(D14, D15, D4, D5, A3, D6, A4);
cparata 5:69cd10d5ca51 46
cparata 0:276fc25c1c41 47 /* Retrieve the composing elements of the expansion board */
cparata 0:276fc25c1c41 48 static LIS2DW12Sensor *accelerometer = mems_expansion_board->accelerometer;
cparata 5:69cd10d5ca51 49
cparata 0:276fc25c1c41 50 InterruptIn mybutton(USER_BUTTON);
cparata 0:276fc25c1c41 51 DigitalOut myled(LED1);
cparata 5:69cd10d5ca51 52
cparata 0:276fc25c1c41 53 volatile int mems_event = 0;
cparata 0:276fc25c1c41 54 volatile int toggle_wake_up_enable = 0;
cparata 0:276fc25c1c41 55 static int wake_up_is_enabled = 1;
cparata 5:69cd10d5ca51 56
cparata 0:276fc25c1c41 57 void pressed_cb();
cparata 0:276fc25c1c41 58 void INT1_cb();
cparata 5:69cd10d5ca51 59
cparata 0:276fc25c1c41 60 /* Simple main function */
cparata 5:69cd10d5ca51 61 int main()
cparata 5:69cd10d5ca51 62 {
cparata 5:69cd10d5ca51 63 /* Attach callback to User button press */
cparata 5:69cd10d5ca51 64 mybutton.fall(&pressed_cb);
cparata 5:69cd10d5ca51 65 /* Attach callback to LIS2DW12 INT1 */
cparata 5:69cd10d5ca51 66 accelerometer->attach_int1_irq(&INT1_cb);
cparata 5:69cd10d5ca51 67
cparata 5:69cd10d5ca51 68 /* Enable LIS2DW12 accelerometer */
cparata 5:69cd10d5ca51 69 accelerometer->enable_x();
cparata 5:69cd10d5ca51 70 /* Enable Wake-Up Detection. */
cparata 5:69cd10d5ca51 71 accelerometer->enable_wake_up_detection();
cparata 5:69cd10d5ca51 72
cparata 5:69cd10d5ca51 73 printf("\r\n--- Starting new run ---\r\n");
cparata 5:69cd10d5ca51 74
cparata 5:69cd10d5ca51 75 while (1) {
cparata 5:69cd10d5ca51 76 if (toggle_wake_up_enable) {
cparata 5:69cd10d5ca51 77 toggle_wake_up_enable = 0;
cparata 5:69cd10d5ca51 78 if (wake_up_is_enabled == 0) {
cparata 5:69cd10d5ca51 79 accelerometer->enable_wake_up_detection();
cparata 5:69cd10d5ca51 80 wake_up_is_enabled = 1;
cparata 5:69cd10d5ca51 81 } else {
cparata 5:69cd10d5ca51 82 accelerometer->disable_wake_up_detection();
cparata 5:69cd10d5ca51 83 wake_up_is_enabled = 0;
cparata 5:69cd10d5ca51 84 }
cparata 5:69cd10d5ca51 85 }
cparata 5:69cd10d5ca51 86
cparata 5:69cd10d5ca51 87 if (mems_event) {
cparata 5:69cd10d5ca51 88 mems_event = 0;
cparata 5:69cd10d5ca51 89 LIS2DW12_Event_Status_t status;
cparata 5:69cd10d5ca51 90 accelerometer->get_event_status(&status);
cparata 5:69cd10d5ca51 91 if (status.WakeUpStatus) {
cparata 5:69cd10d5ca51 92 /* Led blinking. */
cparata 5:69cd10d5ca51 93 myled = 1;
cparata 6:57d0454ddd40 94 ThisThread::sleep_for(200);
cparata 5:69cd10d5ca51 95 myled = 0;
cparata 5:69cd10d5ca51 96
cparata 5:69cd10d5ca51 97 /* Output data. */
cparata 5:69cd10d5ca51 98 printf("Wake Up Detected!\r\n");
cparata 5:69cd10d5ca51 99 }
cparata 5:69cd10d5ca51 100 }
cparata 0:276fc25c1c41 101 }
cparata 5:69cd10d5ca51 102 }
cparata 5:69cd10d5ca51 103
cparata 5:69cd10d5ca51 104 void pressed_cb()
cparata 5:69cd10d5ca51 105 {
cparata 5:69cd10d5ca51 106 toggle_wake_up_enable = 1;
cparata 0:276fc25c1c41 107 }
cparata 5:69cd10d5ca51 108
cparata 5:69cd10d5ca51 109 void INT1_cb()
cparata 5:69cd10d5ca51 110 {
cparata 5:69cd10d5ca51 111 mems_event = 1;
cparata 0:276fc25c1c41 112 }