"Sensors Reader" Sample Application for X-NUCLEO-IKS01A1 Expansion Board
Dependencies: X_NUCLEO_IKS01A2 mbed
Fork of Sensors_Reader by
main.cpp@11:e20efb4e8a98, 2015-05-27 (annotated)
- Committer:
- Wolfgang Betz
- Date:
- Wed May 27 11:56:24 2015 +0200
- Revision:
- 11:e20efb4e8a98
- Parent:
- 5:a1de9dcb0f41
- Child:
- 18:645c96f209c7
Refine board's component types
Who changed what in which revision?
User | Revision | Line number | New 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>© 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 |
0:3e4f610a0689 | 55 | #include "x_nucleo_iks01a1.h" |
Wolfgang Betz |
0:3e4f610a0689 | 56 | |
Wolfgang Betz |
0:3e4f610a0689 | 57 | #include <Ticker.h> |
Wolfgang Betz |
0:3e4f610a0689 | 58 | |
Wolfgang Betz |
0:3e4f610a0689 | 59 | |
Wolfgang Betz |
0:3e4f610a0689 | 60 | /*** Constants ---------------------------------------------------------------- ***/ |
Wolfgang Betz |
0:3e4f610a0689 | 61 | namespace { |
Wolfgang Betz |
0:3e4f610a0689 | 62 | const int MS_INTERVALS = 1000; |
Wolfgang Betz |
0:3e4f610a0689 | 63 | } |
Wolfgang Betz |
0:3e4f610a0689 | 64 | |
Wolfgang Betz |
0:3e4f610a0689 | 65 | |
Wolfgang Betz |
0:3e4f610a0689 | 66 | /*** Macros ------------------------------------------------------------------- ***/ |
Wolfgang Betz |
2:00f62b148a07 | 67 | #define APP_LOOP_PERIOD 1300 // in ms |
Wolfgang Betz |
0:3e4f610a0689 | 68 | |
Wolfgang Betz |
4:81037ace7f27 | 69 | #if defined(TARGET_STM) |
Wolfgang Betz |
4:81037ace7f27 | 70 | #define LED_OFF (0) |
Wolfgang Betz |
4:81037ace7f27 | 71 | #else |
Wolfgang Betz |
4:81037ace7f27 | 72 | #define LED_OFF (1) |
Wolfgang Betz |
4:81037ace7f27 | 73 | #endif |
Wolfgang Betz |
4:81037ace7f27 | 74 | #define LED_ON (!LED_OFF) |
Wolfgang Betz |
4:81037ace7f27 | 75 | |
Wolfgang Betz |
0:3e4f610a0689 | 76 | |
Wolfgang Betz |
1:9458657e49ee | 77 | /*** Typedefs ----------------------------------------------------------------- ***/ |
Wolfgang Betz |
1:9458657e49ee | 78 | typedef struct { |
Wolfgang Betz |
1:9458657e49ee | 79 | int32_t AXIS_X; |
Wolfgang Betz |
1:9458657e49ee | 80 | int32_t AXIS_Y; |
Wolfgang Betz |
1:9458657e49ee | 81 | int32_t AXIS_Z; |
Wolfgang Betz |
1:9458657e49ee | 82 | } AxesRaw_TypeDef; |
Wolfgang Betz |
1:9458657e49ee | 83 | |
Wolfgang Betz |
1:9458657e49ee | 84 | |
Wolfgang Betz |
0:3e4f610a0689 | 85 | /*** Static variables --------------------------------------------------------- ***/ |
Wolfgang Betz |
0:3e4f610a0689 | 86 | #ifdef DBG_MCU |
Wolfgang Betz |
0:3e4f610a0689 | 87 | /* betzw: enable debugging while using sleep modes */ |
Wolfgang Betz |
0:3e4f610a0689 | 88 | #include "DbgMCU.h" |
Wolfgang Betz |
0:3e4f610a0689 | 89 | static DbgMCU enable_dbg; |
Wolfgang Betz |
0:3e4f610a0689 | 90 | #endif // DBG_MCU |
Wolfgang Betz |
0:3e4f610a0689 | 91 | |
Wolfgang Betz |
1:9458657e49ee | 92 | static X_NUCLEO_IKS01A1 *mems_expansion_board = X_NUCLEO_IKS01A1::Instance(); |
Wolfgang Betz |
0:3e4f610a0689 | 93 | static Ticker ticker; |
Wolfgang Betz |
0:3e4f610a0689 | 94 | static volatile bool timer_irq_triggered = false; |
Wolfgang Betz |
4:81037ace7f27 | 95 | static DigitalOut myled(LED1, LED_OFF); |
Wolfgang Betz |
4:81037ace7f27 | 96 | |
Wolfgang Betz |
0:3e4f610a0689 | 97 | |
Wolfgang Betz |
0:3e4f610a0689 | 98 | /*** Helper Functions (1/2) ------------------------------------------------------------ ***/ |
Wolfgang Betz |
0:3e4f610a0689 | 99 | |
Wolfgang Betz |
0:3e4f610a0689 | 100 | |
Wolfgang Betz |
0:3e4f610a0689 | 101 | /*** Interrupt Handler Top-Halves ------------------------------------------------------ ***/ |
Wolfgang Betz |
0:3e4f610a0689 | 102 | /* Called in interrupt context, therefore just set a trigger variable */ |
Wolfgang Betz |
0:3e4f610a0689 | 103 | static void timer_irq(void) { |
Wolfgang Betz |
0:3e4f610a0689 | 104 | timer_irq_triggered = true; |
Wolfgang Betz |
0:3e4f610a0689 | 105 | } |
Wolfgang Betz |
0:3e4f610a0689 | 106 | |
Wolfgang Betz |
0:3e4f610a0689 | 107 | |
Wolfgang Betz |
0:3e4f610a0689 | 108 | /*** Interrupt Handler Bottom-Halves ------------------------------------------------- ***/ |
Wolfgang Betz |
0:3e4f610a0689 | 109 | |
Wolfgang Betz |
0:3e4f610a0689 | 110 | |
Wolfgang Betz |
0:3e4f610a0689 | 111 | /*** Helper Functions (2/2) ------------------------------------------------------------ ***/ |
Wolfgang Betz |
2:00f62b148a07 | 112 | /* print floats & doubles */ |
Wolfgang Betz |
2:00f62b148a07 | 113 | static char *printDouble(char* str, double v, int decimalDigits=2) |
Wolfgang Betz |
2:00f62b148a07 | 114 | { |
Wolfgang Betz |
2:00f62b148a07 | 115 | int i = 1; |
Wolfgang Betz |
2:00f62b148a07 | 116 | int intPart, fractPart; |
Wolfgang Betz |
2:00f62b148a07 | 117 | |
Wolfgang Betz |
2:00f62b148a07 | 118 | for (;decimalDigits!=0; i*=10, decimalDigits--); |
Wolfgang Betz |
2:00f62b148a07 | 119 | intPart = (int)v; |
Wolfgang Betz |
2:00f62b148a07 | 120 | fractPart = (int)((v-(double)(int)v)*i); |
Wolfgang Betz |
2:00f62b148a07 | 121 | sprintf(str, "%i.%i", intPart, fractPart); |
Wolfgang Betz |
2:00f62b148a07 | 122 | |
Wolfgang Betz |
2:00f62b148a07 | 123 | return str; |
Wolfgang Betz |
2:00f62b148a07 | 124 | } |
Wolfgang Betz |
2:00f62b148a07 | 125 | |
Wolfgang Betz |
0:3e4f610a0689 | 126 | /* Initialization function */ |
Wolfgang Betz |
0:3e4f610a0689 | 127 | static void init(void) { |
Wolfgang Betz |
1:9458657e49ee | 128 | uint8_t hts221_id; |
Wolfgang Betz |
1:9458657e49ee | 129 | |
Wolfgang Betz |
11:e20efb4e8a98 | 130 | /* Determine ID of Humidity & Temperature Sensor */ |
Wolfgang Betz |
1:9458657e49ee | 131 | mems_expansion_board->ht_sensor.ReadID(&hts221_id); |
Wolfgang Betz |
2:00f62b148a07 | 132 | printf("HTS221_ID = 0x%x (%u)\n", hts221_id, hts221_id); |
Wolfgang Betz |
1:9458657e49ee | 133 | } |
Wolfgang Betz |
1:9458657e49ee | 134 | |
Wolfgang Betz |
1:9458657e49ee | 135 | /* Main cycle function */ |
Wolfgang Betz |
1:9458657e49ee | 136 | static void main_cycle(void) { |
Wolfgang Betz |
1:9458657e49ee | 137 | float TEMPERATURE_Value; |
Wolfgang Betz |
1:9458657e49ee | 138 | float HUMIDITY_Value; |
Wolfgang Betz |
1:9458657e49ee | 139 | float PRESSURE_Value; |
Wolfgang Betz |
11:e20efb4e8a98 | 140 | float PRESSURE_Temp_Value; |
Wolfgang Betz |
1:9458657e49ee | 141 | AxesRaw_TypeDef MAG_Value; |
Wolfgang Betz |
1:9458657e49ee | 142 | AxesRaw_TypeDef ACC_Value; |
Wolfgang Betz |
1:9458657e49ee | 143 | AxesRaw_TypeDef GYR_Value; |
Wolfgang Betz |
2:00f62b148a07 | 144 | char buffer1[32]; |
Wolfgang Betz |
2:00f62b148a07 | 145 | char buffer2[32]; |
Wolfgang Betz |
2:00f62b148a07 | 146 | char buffer3[32]; |
Wolfgang Betz |
11:e20efb4e8a98 | 147 | char buffer4[32]; |
Wolfgang Betz |
2:00f62b148a07 | 148 | |
Wolfgang Betz |
1:9458657e49ee | 149 | /* Switch LED On */ |
Wolfgang Betz |
4:81037ace7f27 | 150 | myled = LED_ON; |
Wolfgang Betz |
2:00f62b148a07 | 151 | printf("===\n"); |
Wolfgang Betz |
1:9458657e49ee | 152 | |
Wolfgang Betz |
1:9458657e49ee | 153 | /* Determine Environmental Values */ |
Wolfgang Betz |
1:9458657e49ee | 154 | mems_expansion_board->ht_sensor.GetTemperature(&TEMPERATURE_Value); |
Wolfgang Betz |
1:9458657e49ee | 155 | mems_expansion_board->ht_sensor.GetHumidity(&HUMIDITY_Value); |
Wolfgang Betz |
1:9458657e49ee | 156 | mems_expansion_board->pressure_sensor.GetPressure(&PRESSURE_Value); |
Wolfgang Betz |
11:e20efb4e8a98 | 157 | mems_expansion_board->pressure_sensor.GetTemperature(&PRESSURE_Temp_Value); |
Wolfgang Betz |
1:9458657e49ee | 158 | mems_expansion_board->magnetometer.Get_M_Axes((int32_t *)&MAG_Value); |
Wolfgang Betz |
1:9458657e49ee | 159 | mems_expansion_board->gyroscope.Get_X_Axes((int32_t *)&ACC_Value); |
Wolfgang Betz |
1:9458657e49ee | 160 | mems_expansion_board->gyroscope.Get_G_Axes((int32_t *)&GYR_Value); |
Wolfgang Betz |
1:9458657e49ee | 161 | |
Wolfgang Betz |
1:9458657e49ee | 162 | /* Print Values Out */ |
Wolfgang Betz |
11:e20efb4e8a98 | 163 | printf("TEMP: %s, HUMIDITY: %s, PRESSURE (TEMP): %s (%s)\n", |
Wolfgang Betz |
2:00f62b148a07 | 164 | printDouble(buffer1, TEMPERATURE_Value), |
Wolfgang Betz |
2:00f62b148a07 | 165 | printDouble(buffer2, HUMIDITY_Value), |
Wolfgang Betz |
11:e20efb4e8a98 | 166 | printDouble(buffer3, PRESSURE_Value), |
Wolfgang Betz |
11:e20efb4e8a98 | 167 | printDouble(buffer4, PRESSURE_Temp_Value)); |
Wolfgang Betz |
2:00f62b148a07 | 168 | printf("X_MAG: %ld, Y_MAG: %ld, Z_MAG: %ld\n", |
Wolfgang Betz |
1:9458657e49ee | 169 | MAG_Value.AXIS_X, MAG_Value.AXIS_Y, MAG_Value.AXIS_Z); |
Wolfgang Betz |
2:00f62b148a07 | 170 | printf("X_ACC: %ld, Y_ACC: %ld, Z_ACC: %ld\n", |
Wolfgang Betz |
1:9458657e49ee | 171 | ACC_Value.AXIS_X, ACC_Value.AXIS_Y, ACC_Value.AXIS_Z); |
Wolfgang Betz |
2:00f62b148a07 | 172 | printf("X_GYR: %ld, Y_GYR: %ld, Z_GYR: %ld\n", |
Wolfgang Betz |
1:9458657e49ee | 173 | GYR_Value.AXIS_X, GYR_Value.AXIS_Y, GYR_Value.AXIS_Z); |
Wolfgang Betz |
1:9458657e49ee | 174 | |
Wolfgang Betz |
1:9458657e49ee | 175 | /* Switch LED Off */ |
Wolfgang Betz |
4:81037ace7f27 | 176 | myled = LED_OFF; |
Wolfgang Betz |
0:3e4f610a0689 | 177 | } |
Wolfgang Betz |
0:3e4f610a0689 | 178 | |
Wolfgang Betz |
0:3e4f610a0689 | 179 | |
Wolfgang Betz |
0:3e4f610a0689 | 180 | /*** Main function ------------------------------------------------------------- ***/ |
Wolfgang Betz |
0:3e4f610a0689 | 181 | /* Generic main function/loop for enabling WFI in case of |
Wolfgang Betz |
0:3e4f610a0689 | 182 | interrupt based cyclic execution |
Wolfgang Betz |
0:3e4f610a0689 | 183 | */ |
Wolfgang Betz |
0:3e4f610a0689 | 184 | int main() |
Wolfgang Betz |
0:3e4f610a0689 | 185 | { |
Wolfgang Betz |
0:3e4f610a0689 | 186 | /* Start & initialize */ |
Wolfgang Betz |
0:3e4f610a0689 | 187 | printf("\n--- Starting new run ---\n"); |
Wolfgang Betz |
0:3e4f610a0689 | 188 | init(); |
Wolfgang Betz |
0:3e4f610a0689 | 189 | |
Wolfgang Betz |
0:3e4f610a0689 | 190 | /* Start timer irq */ |
Wolfgang Betz |
0:3e4f610a0689 | 191 | ticker.attach_us(timer_irq, MS_INTERVALS * APP_LOOP_PERIOD); |
Wolfgang Betz |
0:3e4f610a0689 | 192 | |
Wolfgang Betz |
0:3e4f610a0689 | 193 | while (true) { |
Wolfgang Betz |
0:3e4f610a0689 | 194 | __disable_irq(); |
Wolfgang Betz |
0:3e4f610a0689 | 195 | if(timer_irq_triggered) { |
Wolfgang Betz |
0:3e4f610a0689 | 196 | timer_irq_triggered = false; |
Wolfgang Betz |
0:3e4f610a0689 | 197 | __enable_irq(); |
Wolfgang Betz |
1:9458657e49ee | 198 | main_cycle(); |
Wolfgang Betz |
0:3e4f610a0689 | 199 | } else { |
Wolfgang Betz |
0:3e4f610a0689 | 200 | __WFI(); |
Wolfgang Betz |
0:3e4f610a0689 | 201 | __enable_irq(); /* do NOT enable irqs before WFI to avoid |
Wolfgang Betz |
0:3e4f610a0689 | 202 | opening a window in which you can loose |
Wolfgang Betz |
0:3e4f610a0689 | 203 | irq arrivals before going into WFI */ |
Wolfgang Betz |
0:3e4f610a0689 | 204 | } |
Wolfgang Betz |
0:3e4f610a0689 | 205 | } |
Wolfgang Betz |
0:3e4f610a0689 | 206 | } |