"Sensors Reader" Sample Application for X-NUCLEO-IKS01A1 Expansion Board

Dependencies:   X_NUCLEO_IKS01A2 mbed

Fork of Sensors_Reader by ST

Committer:
Wolfgang Betz
Date:
Mon Jun 22 11:24:50 2015 +0200
Revision:
47:6a63161486cf
Parent:
40:f567538352e1
Child:
48:9631c81e1d87
Various versions for WFE tests

Who changed what in which revision?

UserRevisionLine numberNew contents of line
Wolfgang Betz 0:3e4f610a0689 1 /**
Wolfgang Betz 0:3e4f610a0689 2 ******************************************************************************
Wolfgang Betz 0:3e4f610a0689 3 * @file main.cpp
Wolfgang Betz 0:3e4f610a0689 4 * @author AST / EST
Wolfgang Betz 0:3e4f610a0689 5 * @version V0.0.1
Wolfgang Betz 0:3e4f610a0689 6 * @date 14-April-2015
Wolfgang Betz 0:3e4f610a0689 7 * @brief Example application for using the X_NUCLEO_IKS01A1
Wolfgang Betz 5:a1de9dcb0f41 8 * MEMS Inertial & Environmental Sensor Nucleo expansion board.
Wolfgang Betz 0:3e4f610a0689 9 ******************************************************************************
Wolfgang Betz 0:3e4f610a0689 10 * @attention
Wolfgang Betz 0:3e4f610a0689 11 *
Wolfgang Betz 0:3e4f610a0689 12 * <h2><center>&copy; COPYRIGHT(c) 2015 STMicroelectronics</center></h2>
Wolfgang Betz 0:3e4f610a0689 13 *
Wolfgang Betz 0:3e4f610a0689 14 * Redistribution and use in source and binary forms, with or without modification,
Wolfgang Betz 0:3e4f610a0689 15 * are permitted provided that the following conditions are met:
Wolfgang Betz 0:3e4f610a0689 16 * 1. Redistributions of source code must retain the above copyright notice,
Wolfgang Betz 0:3e4f610a0689 17 * this list of conditions and the following disclaimer.
Wolfgang Betz 0:3e4f610a0689 18 * 2. Redistributions in binary form must reproduce the above copyright notice,
Wolfgang Betz 0:3e4f610a0689 19 * this list of conditions and the following disclaimer in the documentation
Wolfgang Betz 0:3e4f610a0689 20 * and/or other materials provided with the distribution.
Wolfgang Betz 0:3e4f610a0689 21 * 3. Neither the name of STMicroelectronics nor the names of its contributors
Wolfgang Betz 0:3e4f610a0689 22 * may be used to endorse or promote products derived from this software
Wolfgang Betz 0:3e4f610a0689 23 * without specific prior written permission.
Wolfgang Betz 0:3e4f610a0689 24 *
Wolfgang Betz 0:3e4f610a0689 25 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
Wolfgang Betz 0:3e4f610a0689 26 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
Wolfgang Betz 0:3e4f610a0689 27 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
Wolfgang Betz 0:3e4f610a0689 28 * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
Wolfgang Betz 0:3e4f610a0689 29 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
Wolfgang Betz 0:3e4f610a0689 30 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
Wolfgang Betz 0:3e4f610a0689 31 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
Wolfgang Betz 0:3e4f610a0689 32 * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
Wolfgang Betz 0:3e4f610a0689 33 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
Wolfgang Betz 0:3e4f610a0689 34 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
Wolfgang Betz 0:3e4f610a0689 35 *
Wolfgang Betz 0:3e4f610a0689 36 ******************************************************************************
Wolfgang Betz 0:3e4f610a0689 37 */
Wolfgang Betz 0:3e4f610a0689 38
Wolfgang Betz 0:3e4f610a0689 39 /**
Wolfgang Betz 5:a1de9dcb0f41 40 * @mainpage X_NUCLEO_IKS01A1 MEMS Inertial & Environmental Sensor Nucleo Expansion Board Firmware Package
Wolfgang Betz 0:3e4f610a0689 41 *
Wolfgang Betz 0:3e4f610a0689 42 * <b>Introduction</b>
Wolfgang Betz 0:3e4f610a0689 43 *
Wolfgang Betz 0:3e4f610a0689 44 * This firmware package includes Components Device Drivers, Board Support Package
Wolfgang Betz 0:3e4f610a0689 45 * and example application for STMicroelectronics X_NUCLEO_IKS01A1 MEMS Inertial & Environmental Nucleo
Wolfgang Betz 0:3e4f610a0689 46 * Expansion Board
Wolfgang Betz 0:3e4f610a0689 47 *
Wolfgang Betz 0:3e4f610a0689 48 * <b>Example Application</b>
Wolfgang Betz 0:3e4f610a0689 49 *
Wolfgang Betz 0:3e4f610a0689 50 */
Wolfgang Betz 0:3e4f610a0689 51
Wolfgang Betz 0:3e4f610a0689 52
Wolfgang Betz 0:3e4f610a0689 53 /*** Includes ----------------------------------------------------------------- ***/
Wolfgang Betz 0:3e4f610a0689 54 #include "mbed.h"
Wolfgang Betz 33:7ba7fbf0503a 55 #include "assert.h"
Wolfgang Betz 0:3e4f610a0689 56 #include "x_nucleo_iks01a1.h"
Wolfgang Betz 0:3e4f610a0689 57
Wolfgang Betz 0:3e4f610a0689 58 #include <Ticker.h>
Wolfgang Betz 0:3e4f610a0689 59
Wolfgang Betz 0:3e4f610a0689 60
Wolfgang Betz 0:3e4f610a0689 61 /*** Constants ---------------------------------------------------------------- ***/
Wolfgang Betz 0:3e4f610a0689 62 namespace {
Wolfgang Betz 0:3e4f610a0689 63 const int MS_INTERVALS = 1000;
Wolfgang Betz 0:3e4f610a0689 64 }
Wolfgang Betz 0:3e4f610a0689 65
Wolfgang Betz 0:3e4f610a0689 66
Wolfgang Betz 0:3e4f610a0689 67 /*** Macros ------------------------------------------------------------------- ***/
Wolfgang Betz 40:f567538352e1 68 #define APP_LOOP_PERIOD 3000 // in ms
Wolfgang Betz 0:3e4f610a0689 69
Wolfgang Betz 4:81037ace7f27 70 #if defined(TARGET_STM)
Wolfgang Betz 4:81037ace7f27 71 #define LED_OFF (0)
Wolfgang Betz 4:81037ace7f27 72 #else
Wolfgang Betz 4:81037ace7f27 73 #define LED_OFF (1)
Wolfgang Betz 4:81037ace7f27 74 #endif
Wolfgang Betz 4:81037ace7f27 75 #define LED_ON (!LED_OFF)
Wolfgang Betz 4:81037ace7f27 76
Wolfgang Betz 0:3e4f610a0689 77
Wolfgang Betz 1:9458657e49ee 78 /*** Typedefs ----------------------------------------------------------------- ***/
Wolfgang Betz 1:9458657e49ee 79 typedef struct {
Wolfgang Betz 1:9458657e49ee 80 int32_t AXIS_X;
Wolfgang Betz 1:9458657e49ee 81 int32_t AXIS_Y;
Wolfgang Betz 1:9458657e49ee 82 int32_t AXIS_Z;
Wolfgang Betz 1:9458657e49ee 83 } AxesRaw_TypeDef;
Wolfgang Betz 1:9458657e49ee 84
Wolfgang Betz 1:9458657e49ee 85
Wolfgang Betz 0:3e4f610a0689 86 /*** Static variables --------------------------------------------------------- ***/
Wolfgang Betz 0:3e4f610a0689 87 #ifdef DBG_MCU
Wolfgang Betz 0:3e4f610a0689 88 /* betzw: enable debugging while using sleep modes */
Wolfgang Betz 0:3e4f610a0689 89 #include "DbgMCU.h"
Wolfgang Betz 0:3e4f610a0689 90 static DbgMCU enable_dbg;
Wolfgang Betz 0:3e4f610a0689 91 #endif // DBG_MCU
Wolfgang Betz 0:3e4f610a0689 92
Wolfgang Betz 1:9458657e49ee 93 static X_NUCLEO_IKS01A1 *mems_expansion_board = X_NUCLEO_IKS01A1::Instance();
Wolfgang Betz 33:7ba7fbf0503a 94 static GyroSensor *gyroscope = mems_expansion_board->GetGyroscope();
Wolfgang Betz 33:7ba7fbf0503a 95 static MotionSensor *accelerometer = mems_expansion_board->GetAccelerometer();
Wolfgang Betz 32:97bff5dadafd 96 static MagneticSensor *magnetometer = mems_expansion_board->magnetometer;
Wolfgang Betz 32:97bff5dadafd 97 static HumiditySensor *humidity_sensor = mems_expansion_board->ht_sensor;;
Wolfgang Betz 32:97bff5dadafd 98 static PressureSensor *pressure_sensor = mems_expansion_board->pressure_sensor;
Wolfgang Betz 32:97bff5dadafd 99 static TempSensor *temp_sensor1 = mems_expansion_board->ht_sensor;
Wolfgang Betz 32:97bff5dadafd 100 static TempSensor *temp_sensor2 = mems_expansion_board->pressure_sensor;
Wolfgang Betz 18:645c96f209c7 101
Wolfgang Betz 0:3e4f610a0689 102 static Ticker ticker;
Wolfgang Betz 40:f567538352e1 103 static DigitalOut myled(LED1, LED_OFF);
Wolfgang Betz 40:f567538352e1 104
Wolfgang Betz 0:3e4f610a0689 105 static volatile bool timer_irq_triggered = false;
Wolfgang Betz 40:f567538352e1 106 static volatile bool ff_irq_triggered = false;
Wolfgang Betz 4:81037ace7f27 107
Wolfgang Betz 0:3e4f610a0689 108
Wolfgang Betz 0:3e4f610a0689 109 /*** Helper Functions (1/2) ------------------------------------------------------------ ***/
Wolfgang Betz 0:3e4f610a0689 110
Wolfgang Betz 0:3e4f610a0689 111
Wolfgang Betz 0:3e4f610a0689 112 /*** Interrupt Handler Top-Halves ------------------------------------------------------ ***/
Wolfgang Betz 0:3e4f610a0689 113 /* Called in interrupt context, therefore just set a trigger variable */
Wolfgang Betz 0:3e4f610a0689 114 static void timer_irq(void) {
Wolfgang Betz 0:3e4f610a0689 115 timer_irq_triggered = true;
Wolfgang Betz 0:3e4f610a0689 116 }
Wolfgang Betz 0:3e4f610a0689 117
Wolfgang Betz 40:f567538352e1 118 /* Called in interrupt context, therefore just set a trigger variable */
Wolfgang Betz 40:f567538352e1 119 static void ff_irq(void) {
Wolfgang Betz 40:f567538352e1 120 ff_irq_triggered = true;
Wolfgang Betz 40:f567538352e1 121
Wolfgang Betz 40:f567538352e1 122 /* Disable IRQ until handled */
Wolfgang Betz 40:f567538352e1 123 mems_expansion_board->gyro_lsm6ds3->Disable_Free_Fall_Detection_IRQ();
Wolfgang Betz 40:f567538352e1 124 }
Wolfgang Betz 40:f567538352e1 125
Wolfgang Betz 0:3e4f610a0689 126
Wolfgang Betz 0:3e4f610a0689 127 /*** Interrupt Handler Bottom-Halves ------------------------------------------------- ***/
Wolfgang Betz 40:f567538352e1 128 /* Handle Free Fall Interrupt
Wolfgang Betz 40:f567538352e1 129 (here we are in "normal" context, i.e. not in IRQ context)
Wolfgang Betz 40:f567538352e1 130 */
Wolfgang Betz 40:f567538352e1 131 static void handle_ff_irq(void) {
Wolfgang Betz 40:f567538352e1 132 printf("\nFree Fall Detected!\n\n");
Wolfgang Betz 40:f567538352e1 133
Wolfgang Betz 40:f567538352e1 134 /* Re-enable IRQ */
Wolfgang Betz 40:f567538352e1 135 mems_expansion_board->gyro_lsm6ds3->Enable_Free_Fall_Detection_IRQ();
Wolfgang Betz 40:f567538352e1 136 }
Wolfgang Betz 0:3e4f610a0689 137
Wolfgang Betz 0:3e4f610a0689 138
Wolfgang Betz 0:3e4f610a0689 139 /*** Helper Functions (2/2) ------------------------------------------------------------ ***/
Wolfgang Betz 2:00f62b148a07 140 /* print floats & doubles */
Wolfgang Betz 2:00f62b148a07 141 static char *printDouble(char* str, double v, int decimalDigits=2)
Wolfgang Betz 2:00f62b148a07 142 {
Wolfgang Betz 2:00f62b148a07 143 int i = 1;
Wolfgang Betz 2:00f62b148a07 144 int intPart, fractPart;
Wolfgang Betz 2:00f62b148a07 145
Wolfgang Betz 2:00f62b148a07 146 for (;decimalDigits!=0; i*=10, decimalDigits--);
Wolfgang Betz 2:00f62b148a07 147 intPart = (int)v;
Wolfgang Betz 2:00f62b148a07 148 fractPart = (int)((v-(double)(int)v)*i);
Wolfgang Betz 2:00f62b148a07 149 sprintf(str, "%i.%i", intPart, fractPart);
Wolfgang Betz 2:00f62b148a07 150
Wolfgang Betz 2:00f62b148a07 151 return str;
Wolfgang Betz 2:00f62b148a07 152 }
Wolfgang Betz 2:00f62b148a07 153
Wolfgang Betz 0:3e4f610a0689 154 /* Initialization function */
Wolfgang Betz 0:3e4f610a0689 155 static void init(void) {
Wolfgang Betz 33:7ba7fbf0503a 156 uint8_t id1, id2;
Wolfgang Betz 1:9458657e49ee 157
Wolfgang Betz 11:e20efb4e8a98 158 /* Determine ID of Humidity & Temperature Sensor */
Wolfgang Betz 33:7ba7fbf0503a 159 CALL_METH(humidity_sensor, ReadID, &id1, 0x0);
Wolfgang Betz 33:7ba7fbf0503a 160 CALL_METH(temp_sensor1, ReadID, &id2, 0x0);
Wolfgang Betz 33:7ba7fbf0503a 161 printf("Humidity | Temperature Sensor ID = %s (0x%x | 0x%x)\n",
Wolfgang Betz 33:7ba7fbf0503a 162 ((id1 == I_AM_HTS221) ? "HTS221 " : "UNKNOWN"),
Wolfgang Betz 33:7ba7fbf0503a 163 id1, id2
Wolfgang Betz 33:7ba7fbf0503a 164 );
Wolfgang Betz 33:7ba7fbf0503a 165 assert(id1 == id2);
Wolfgang Betz 33:7ba7fbf0503a 166
Wolfgang Betz 33:7ba7fbf0503a 167 /* Determine ID of Gyro & Motion Sensor */
Wolfgang Betz 33:7ba7fbf0503a 168 assert((mems_expansion_board->gyro_lsm6ds0 == NULL) ||
Wolfgang Betz 33:7ba7fbf0503a 169 (mems_expansion_board->gyro_lsm6ds3 == NULL));
Wolfgang Betz 33:7ba7fbf0503a 170 CALL_METH(gyroscope, ReadID, &id1, 0x0);
Wolfgang Betz 33:7ba7fbf0503a 171 CALL_METH(accelerometer, ReadID, &id2, 0x0);
Wolfgang Betz 33:7ba7fbf0503a 172 printf("Gyroscope | Motion Sensor ID = %s (0x%x | 0x%x)\n",
Wolfgang Betz 33:7ba7fbf0503a 173 ((id1 == I_AM_LSM6DS3_XG) ? "LSM6DS3" :
Wolfgang Betz 33:7ba7fbf0503a 174 ((id1 == I_AM_LSM6DS0_XG) ? "LSM6DS0" : "UNKNOWN")),
Wolfgang Betz 33:7ba7fbf0503a 175 id1, id2
Wolfgang Betz 33:7ba7fbf0503a 176 );
Wolfgang Betz 33:7ba7fbf0503a 177 assert(id1 == id2);
Wolfgang Betz 33:7ba7fbf0503a 178
Wolfgang Betz 40:f567538352e1 179 /* Register Free Fall Detection IRQ Handler & Enable Detection */
Wolfgang Betz 40:f567538352e1 180 if(mems_expansion_board->gyro_lsm6ds3 != NULL) {
Wolfgang Betz 40:f567538352e1 181 mems_expansion_board->gyro_lsm6ds3->Attach_Free_Fall_Detection_IRQ(ff_irq);
Wolfgang Betz 40:f567538352e1 182 mems_expansion_board->gyro_lsm6ds3->Enable_Free_Fall_Detection();
Wolfgang Betz 40:f567538352e1 183 }
Wolfgang Betz 1:9458657e49ee 184 }
Wolfgang Betz 1:9458657e49ee 185
Wolfgang Betz 1:9458657e49ee 186 /* Main cycle function */
Wolfgang Betz 1:9458657e49ee 187 static void main_cycle(void) {
Wolfgang Betz 1:9458657e49ee 188 float TEMPERATURE_Value;
Wolfgang Betz 1:9458657e49ee 189 float HUMIDITY_Value;
Wolfgang Betz 1:9458657e49ee 190 float PRESSURE_Value;
Wolfgang Betz 11:e20efb4e8a98 191 float PRESSURE_Temp_Value;
Wolfgang Betz 1:9458657e49ee 192 AxesRaw_TypeDef MAG_Value;
Wolfgang Betz 1:9458657e49ee 193 AxesRaw_TypeDef ACC_Value;
Wolfgang Betz 1:9458657e49ee 194 AxesRaw_TypeDef GYR_Value;
Wolfgang Betz 2:00f62b148a07 195 char buffer1[32];
Wolfgang Betz 2:00f62b148a07 196 char buffer2[32];
Wolfgang Betz 2:00f62b148a07 197 char buffer3[32];
Wolfgang Betz 11:e20efb4e8a98 198 char buffer4[32];
Wolfgang Betz 2:00f62b148a07 199
Wolfgang Betz 1:9458657e49ee 200 /* Switch LED On */
Wolfgang Betz 4:81037ace7f27 201 myled = LED_ON;
Wolfgang Betz 2:00f62b148a07 202 printf("===\n");
Wolfgang Betz 1:9458657e49ee 203
Wolfgang Betz 1:9458657e49ee 204 /* Determine Environmental Values */
Wolfgang Betz 32:97bff5dadafd 205 CALL_METH(temp_sensor1, GetTemperature, &TEMPERATURE_Value, 0.0f);
Wolfgang Betz 32:97bff5dadafd 206 CALL_METH(humidity_sensor, GetHumidity, &HUMIDITY_Value, 0.0f);
Wolfgang Betz 32:97bff5dadafd 207 CALL_METH(pressure_sensor, GetPressure, &PRESSURE_Value, 0.0f);
Wolfgang Betz 32:97bff5dadafd 208 CALL_METH(temp_sensor2, GetFahrenheit, &PRESSURE_Temp_Value, 0.0f);
Wolfgang Betz 32:97bff5dadafd 209 CALL_METH(magnetometer, Get_M_Axes, (int32_t *)&MAG_Value, 0);
Wolfgang Betz 32:97bff5dadafd 210 CALL_METH(accelerometer, Get_X_Axes, (int32_t *)&ACC_Value, 0);
Wolfgang Betz 32:97bff5dadafd 211 CALL_METH(gyroscope, Get_G_Axes, (int32_t *)&GYR_Value, 0);
Wolfgang Betz 1:9458657e49ee 212
Wolfgang Betz 1:9458657e49ee 213 /* Print Values Out */
Wolfgang Betz 29:25c8f7d4515a 214 printf(" X Y Z\n");
Wolfgang Betz 29:25c8f7d4515a 215 printf("MAG [mgauss]: %9ld %9ld %9ld\n",
Wolfgang Betz 29:25c8f7d4515a 216 MAG_Value.AXIS_X, MAG_Value.AXIS_Y, MAG_Value.AXIS_Z);
Wolfgang Betz 29:25c8f7d4515a 217 printf("ACC [mg]: %9ld %9ld %9ld\n",
Wolfgang Betz 29:25c8f7d4515a 218 ACC_Value.AXIS_X, ACC_Value.AXIS_Y, ACC_Value.AXIS_Z);
Wolfgang Betz 29:25c8f7d4515a 219 printf("GYR [mdps]: %9ld %9ld %9ld\n",
Wolfgang Betz 29:25c8f7d4515a 220 GYR_Value.AXIS_X, GYR_Value.AXIS_Y, GYR_Value.AXIS_Z);
Wolfgang Betz 29:25c8f7d4515a 221 printf("---\nTEMP | HUMIDITY: %s°C | %s%%\nTEMP | PRESSURE: %s°F | %smbar\n",
Wolfgang Betz 2:00f62b148a07 222 printDouble(buffer1, TEMPERATURE_Value),
Wolfgang Betz 2:00f62b148a07 223 printDouble(buffer2, HUMIDITY_Value),
Wolfgang Betz 29:25c8f7d4515a 224 printDouble(buffer4, PRESSURE_Temp_Value),
Wolfgang Betz 29:25c8f7d4515a 225 printDouble(buffer3, PRESSURE_Value));
Wolfgang Betz 1:9458657e49ee 226
Wolfgang Betz 1:9458657e49ee 227 /* Switch LED Off */
Wolfgang Betz 4:81037ace7f27 228 myled = LED_OFF;
Wolfgang Betz 0:3e4f610a0689 229 }
Wolfgang Betz 0:3e4f610a0689 230
Wolfgang Betz 0:3e4f610a0689 231
Wolfgang Betz 0:3e4f610a0689 232 /*** Main function ------------------------------------------------------------- ***/
Wolfgang Betz 47:6a63161486cf 233 #define D_SLEEP_VERSION 1
Wolfgang Betz 47:6a63161486cf 234
Wolfgang Betz 0:3e4f610a0689 235 /* Generic main function/loop for enabling WFI in case of
Wolfgang Betz 0:3e4f610a0689 236 interrupt based cyclic execution
Wolfgang Betz 0:3e4f610a0689 237 */
Wolfgang Betz 0:3e4f610a0689 238 int main()
Wolfgang Betz 0:3e4f610a0689 239 {
Wolfgang Betz 0:3e4f610a0689 240 /* Start & initialize */
Wolfgang Betz 0:3e4f610a0689 241 printf("\n--- Starting new run ---\n");
Wolfgang Betz 0:3e4f610a0689 242 init();
Wolfgang Betz 0:3e4f610a0689 243
Wolfgang Betz 0:3e4f610a0689 244 /* Start timer irq */
Wolfgang Betz 0:3e4f610a0689 245 ticker.attach_us(timer_irq, MS_INTERVALS * APP_LOOP_PERIOD);
Wolfgang Betz 0:3e4f610a0689 246
Wolfgang Betz 47:6a63161486cf 247 #if D_SLEEP_VERSION == 1 // version without WFI/WFE and without IRQ synchronization
Wolfgang Betz 47:6a63161486cf 248 while (true) {
Wolfgang Betz 47:6a63161486cf 249 if(timer_irq_triggered) {
Wolfgang Betz 47:6a63161486cf 250 timer_irq_triggered = false;
Wolfgang Betz 47:6a63161486cf 251 main_cycle();
Wolfgang Betz 47:6a63161486cf 252 } else if(ff_irq_triggered) {
Wolfgang Betz 47:6a63161486cf 253 ff_irq_triggered = false;
Wolfgang Betz 47:6a63161486cf 254 handle_ff_irq();
Wolfgang Betz 47:6a63161486cf 255 }
Wolfgang Betz 47:6a63161486cf 256 }
Wolfgang Betz 47:6a63161486cf 257 #elif D_SLEEP_VERSION == 2 // classical version with WFI and with IRQ synchronization
Wolfgang Betz 0:3e4f610a0689 258 while (true) {
Wolfgang Betz 0:3e4f610a0689 259 __disable_irq();
Wolfgang Betz 0:3e4f610a0689 260 if(timer_irq_triggered) {
Wolfgang Betz 0:3e4f610a0689 261 timer_irq_triggered = false;
Wolfgang Betz 0:3e4f610a0689 262 __enable_irq();
Wolfgang Betz 1:9458657e49ee 263 main_cycle();
Wolfgang Betz 40:f567538352e1 264 } else if(ff_irq_triggered) {
Wolfgang Betz 40:f567538352e1 265 ff_irq_triggered = false;
Wolfgang Betz 40:f567538352e1 266 __enable_irq();
Wolfgang Betz 40:f567538352e1 267 handle_ff_irq();
Wolfgang Betz 0:3e4f610a0689 268 } else {
Wolfgang Betz 0:3e4f610a0689 269 __WFI();
Wolfgang Betz 40:f567538352e1 270 __enable_irq(); /* do NOT enable irqs before WFI to avoid
Wolfgang Betz 0:3e4f610a0689 271 opening a window in which you can loose
Wolfgang Betz 0:3e4f610a0689 272 irq arrivals before going into WFI */
Wolfgang Betz 0:3e4f610a0689 273 }
Wolfgang Betz 0:3e4f610a0689 274 }
Wolfgang Betz 47:6a63161486cf 275 #elif D_SLEEP_VERSION == 3 // version with WFE and with IRQ synchronization
Wolfgang Betz 47:6a63161486cf 276 while (true) {
Wolfgang Betz 47:6a63161486cf 277 __disable_irq();
Wolfgang Betz 47:6a63161486cf 278 if(timer_irq_triggered) {
Wolfgang Betz 47:6a63161486cf 279 timer_irq_triggered = false;
Wolfgang Betz 47:6a63161486cf 280 __enable_irq();
Wolfgang Betz 47:6a63161486cf 281 main_cycle();
Wolfgang Betz 47:6a63161486cf 282 } else if(ff_irq_triggered) {
Wolfgang Betz 47:6a63161486cf 283 ff_irq_triggered = false;
Wolfgang Betz 47:6a63161486cf 284 __enable_irq();
Wolfgang Betz 47:6a63161486cf 285 handle_ff_irq();
Wolfgang Betz 47:6a63161486cf 286 } else {
Wolfgang Betz 47:6a63161486cf 287 __enable_irq();
Wolfgang Betz 47:6a63161486cf 288 __WFE(); /* assuming that SEVONPEND in the System Control Register is NOT set */
Wolfgang Betz 47:6a63161486cf 289 }
Wolfgang Betz 47:6a63161486cf 290 }
Wolfgang Betz 47:6a63161486cf 291 #elif D_SLEEP_VERSION == 4 // version with WFE and without IRQ synchronization
Wolfgang Betz 47:6a63161486cf 292 while (true) {
Wolfgang Betz 47:6a63161486cf 293 if(timer_irq_triggered) {
Wolfgang Betz 47:6a63161486cf 294 timer_irq_triggered = false;
Wolfgang Betz 47:6a63161486cf 295 main_cycle();
Wolfgang Betz 47:6a63161486cf 296 } else if(ff_irq_triggered) {
Wolfgang Betz 47:6a63161486cf 297 ff_irq_triggered = false;
Wolfgang Betz 47:6a63161486cf 298 handle_ff_irq();
Wolfgang Betz 47:6a63161486cf 299 } else {
Wolfgang Betz 47:6a63161486cf 300 __WFE(); /* assuming that SEVONPEND in the System Control Register is NOT set */
Wolfgang Betz 47:6a63161486cf 301 }
Wolfgang Betz 47:6a63161486cf 302 }
Wolfgang Betz 47:6a63161486cf 303 #else // D_SLEEP_VERSION != [1|2|3|4]
Wolfgang Betz 47:6a63161486cf 304 #error "Unsupported D_SLEEP_VERSION selected!"
Wolfgang Betz 47:6a63161486cf 305 #endif // D_SLEEP_VERSION != [1|2|3|4]
Wolfgang Betz 0:3e4f610a0689 306 }