WakeUp_LSM6DSO

Dependencies:   X_NUCLEO_IKS01A3

Committer:
peng103617
Date:
Thu Aug 06 06:57:04 2020 +0000
Revision:
5:507ef51a90be
Parent:
4:387ef72462e0
Wakeup_LSD6DSO

Who changed what in which revision?

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