"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:
Tue Apr 14 15:32:06 2015 +0200
Revision:
0:3e4f610a0689
Child:
1:9458657e49ee
X_NUCLEO_IKS01A1: first version of main & hts221 component

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 0:3e4f610a0689 8 * MEMS Inertial & Environmental 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 0:3e4f610a0689 40 * @mainpage X_NUCLEO_IKS01A1 MEMS Inertial & Environmental 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 0:3e4f610a0689 67 #define APP_LOOP_PERIOD 2000 // in ms
Wolfgang Betz 0:3e4f610a0689 68
Wolfgang Betz 0:3e4f610a0689 69 #if defined(TARGET_K64F)
Wolfgang Betz 0:3e4f610a0689 70 #define USER_BUTTON (SW2)
Wolfgang Betz 0:3e4f610a0689 71 #elif defined(TARGET_LPC11U68)
Wolfgang Betz 0:3e4f610a0689 72 #define USER_BUTTON (P0_16)
Wolfgang Betz 0:3e4f610a0689 73 #endif // !TARGET_MCU_K64F
Wolfgang Betz 0:3e4f610a0689 74
Wolfgang Betz 0:3e4f610a0689 75
Wolfgang Betz 0:3e4f610a0689 76 /*** Static variables --------------------------------------------------------- ***/
Wolfgang Betz 0:3e4f610a0689 77 #ifdef DBG_MCU
Wolfgang Betz 0:3e4f610a0689 78 /* betzw: enable debugging while using sleep modes */
Wolfgang Betz 0:3e4f610a0689 79 #include "DbgMCU.h"
Wolfgang Betz 0:3e4f610a0689 80 static DbgMCU enable_dbg;
Wolfgang Betz 0:3e4f610a0689 81 #endif // DBG_MCU
Wolfgang Betz 0:3e4f610a0689 82
Wolfgang Betz 0:3e4f610a0689 83 static X_NUCLEO_IKS01A1 *iks01a1_expansion_board = X_NUCLEO_IKS01A1::Instance();
Wolfgang Betz 0:3e4f610a0689 84
Wolfgang Betz 0:3e4f610a0689 85 static Ticker ticker;
Wolfgang Betz 0:3e4f610a0689 86 static InterruptIn button(USER_BUTTON);
Wolfgang Betz 0:3e4f610a0689 87
Wolfgang Betz 0:3e4f610a0689 88 static volatile bool timer_irq_triggered = false;
Wolfgang Betz 0:3e4f610a0689 89 static volatile bool button_irq_triggered = false;
Wolfgang Betz 0:3e4f610a0689 90
Wolfgang Betz 0:3e4f610a0689 91
Wolfgang Betz 0:3e4f610a0689 92 /*** Helper Functions (1/2) ------------------------------------------------------------ ***/
Wolfgang Betz 0:3e4f610a0689 93
Wolfgang Betz 0:3e4f610a0689 94
Wolfgang Betz 0:3e4f610a0689 95 /*** Interrupt Handler Top-Halves ------------------------------------------------------ ***/
Wolfgang Betz 0:3e4f610a0689 96 /* Called in interrupt context, therefore just set a trigger variable */
Wolfgang Betz 0:3e4f610a0689 97 static void timer_irq(void) {
Wolfgang Betz 0:3e4f610a0689 98 timer_irq_triggered = true;
Wolfgang Betz 0:3e4f610a0689 99 }
Wolfgang Betz 0:3e4f610a0689 100
Wolfgang Betz 0:3e4f610a0689 101 /* Called in interrupt context, therefore just set a trigger variable */
Wolfgang Betz 0:3e4f610a0689 102 static void button_irq(void) {
Wolfgang Betz 0:3e4f610a0689 103 button_irq_triggered = true;
Wolfgang Betz 0:3e4f610a0689 104 button.disable_irq();
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 /* Handle button irq
Wolfgang Betz 0:3e4f610a0689 110 (here we are in "normal" context, i.e. not in IRQ context)
Wolfgang Betz 0:3e4f610a0689 111 */
Wolfgang Betz 0:3e4f610a0689 112 static void handle_button_irq(void) {
Wolfgang Betz 0:3e4f610a0689 113 /* TODO */
Wolfgang Betz 0:3e4f610a0689 114
Wolfgang Betz 0:3e4f610a0689 115 /* Re-enable button irq */
Wolfgang Betz 0:3e4f610a0689 116 button.enable_irq();
Wolfgang Betz 0:3e4f610a0689 117 }
Wolfgang Betz 0:3e4f610a0689 118
Wolfgang Betz 0:3e4f610a0689 119
Wolfgang Betz 0:3e4f610a0689 120 /*** Helper Functions (2/2) ------------------------------------------------------------ ***/
Wolfgang Betz 0:3e4f610a0689 121 /* Initialization function */
Wolfgang Betz 0:3e4f610a0689 122 static void init(void) {
Wolfgang Betz 0:3e4f610a0689 123 /* Set mode & irq handler for button */
Wolfgang Betz 0:3e4f610a0689 124 button.mode(PullNone);
Wolfgang Betz 0:3e4f610a0689 125 button.fall(button_irq);
Wolfgang Betz 0:3e4f610a0689 126
Wolfgang Betz 0:3e4f610a0689 127 /* TODO */
Wolfgang Betz 0:3e4f610a0689 128 }
Wolfgang Betz 0:3e4f610a0689 129
Wolfgang Betz 0:3e4f610a0689 130
Wolfgang Betz 0:3e4f610a0689 131 /*** Main function ------------------------------------------------------------- ***/
Wolfgang Betz 0:3e4f610a0689 132 /* Generic main function/loop for enabling WFI in case of
Wolfgang Betz 0:3e4f610a0689 133 interrupt based cyclic execution
Wolfgang Betz 0:3e4f610a0689 134 */
Wolfgang Betz 0:3e4f610a0689 135 int main()
Wolfgang Betz 0:3e4f610a0689 136 {
Wolfgang Betz 0:3e4f610a0689 137 /* Start & initialize */
Wolfgang Betz 0:3e4f610a0689 138 printf("\n--- Starting new run ---\n");
Wolfgang Betz 0:3e4f610a0689 139 init();
Wolfgang Betz 0:3e4f610a0689 140
Wolfgang Betz 0:3e4f610a0689 141 /* Start timer irq */
Wolfgang Betz 0:3e4f610a0689 142 ticker.attach_us(timer_irq, MS_INTERVALS * APP_LOOP_PERIOD);
Wolfgang Betz 0:3e4f610a0689 143
Wolfgang Betz 0:3e4f610a0689 144 while (true) {
Wolfgang Betz 0:3e4f610a0689 145 __disable_irq();
Wolfgang Betz 0:3e4f610a0689 146 if(timer_irq_triggered) {
Wolfgang Betz 0:3e4f610a0689 147 timer_irq_triggered = false;
Wolfgang Betz 0:3e4f610a0689 148 __enable_irq();
Wolfgang Betz 0:3e4f610a0689 149 /* TODO */
Wolfgang Betz 0:3e4f610a0689 150 } else if(button_irq_triggered) {
Wolfgang Betz 0:3e4f610a0689 151 button_irq_triggered = false;
Wolfgang Betz 0:3e4f610a0689 152 __enable_irq();
Wolfgang Betz 0:3e4f610a0689 153 handle_button_irq();
Wolfgang Betz 0:3e4f610a0689 154 } else if(0 /* TODO */) {
Wolfgang Betz 0:3e4f610a0689 155 /* TODO */
Wolfgang Betz 0:3e4f610a0689 156 __enable_irq();
Wolfgang Betz 0:3e4f610a0689 157 /* TODO */
Wolfgang Betz 0:3e4f610a0689 158 } else {
Wolfgang Betz 0:3e4f610a0689 159 __WFI();
Wolfgang Betz 0:3e4f610a0689 160 __enable_irq(); /* do NOT enable irqs before WFI to avoid
Wolfgang Betz 0:3e4f610a0689 161 opening a window in which you can loose
Wolfgang Betz 0:3e4f610a0689 162 irq arrivals before going into WFI */
Wolfgang Betz 0:3e4f610a0689 163 }
Wolfgang Betz 0:3e4f610a0689 164 }
Wolfgang Betz 0:3e4f610a0689 165 }