iks01a1 accelerator only on mDot

Dependencies:   X_NUCLEO_IKS01A1 mbed

Fork of Sensors_Reader by ST

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers main.cpp Source File

main.cpp

Go to the documentation of this file.
00001 /**
00002  ******************************************************************************
00003  * @file    main.cpp
00004  * @author  AST / EST
00005  * @version V0.0.1
00006  * @date    14-April-2015
00007  * @brief   Example application for using the X_NUCLEO_IKS01A1 
00008  *          MEMS Inertial & Environmental Sensor Nucleo expansion board.
00009  ******************************************************************************
00010  * @attention
00011  *
00012  * <h2><center>&copy; COPYRIGHT(c) 2015 STMicroelectronics</center></h2>
00013  *
00014  * Redistribution and use in source and binary forms, with or without modification,
00015  * are permitted provided that the following conditions are met:
00016  *   1. Redistributions of source code must retain the above copyright notice,
00017  *      this list of conditions and the following disclaimer.
00018  *   2. Redistributions in binary form must reproduce the above copyright notice,
00019  *      this list of conditions and the following disclaimer in the documentation
00020  *      and/or other materials provided with the distribution.
00021  *   3. Neither the name of STMicroelectronics nor the names of its contributors
00022  *      may be used to endorse or promote products derived from this software
00023  *      without specific prior written permission.
00024  *
00025  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
00026  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
00027  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
00028  * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
00029  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
00030  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
00031  *  SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
00032  * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
00033  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
00034  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
00035  *
00036  ******************************************************************************
00037 */ 
00038 
00039 /**
00040  * @mainpage X_NUCLEO_IKS01A1 MEMS Inertial & Environmental Sensor Nucleo Expansion Board Firmware Package
00041  *
00042  * <b>Introduction</b>
00043  *
00044  * This firmware package includes Components Device Drivers, Board Support Package
00045  * and example application for STMicroelectronics X_NUCLEO_IKS01A1 MEMS Inertial & Environmental Nucleo
00046  * Expansion Board
00047  * 
00048  * <b>Example Application</b>
00049  *
00050  */
00051 
00052 
00053 /*** Includes ----------------------------------------------------------------- ***/
00054 #include "mbed.h"
00055 #include "assert.h"
00056 #include "x_nucleo_iks01a1.h"
00057 
00058 #include <Ticker.h>
00059 
00060 
00061 /*** Constants ---------------------------------------------------------------- ***/
00062 
00063 /*** Macros ------------------------------------------------------------------- ***/
00064 #define APP_LOOP_PERIOD 3000 // in ms
00065 
00066 /*** Typedefs ----------------------------------------------------------------- ***/
00067 typedef struct {
00068     int32_t AXIS_X;
00069     int32_t AXIS_Y;
00070     int32_t AXIS_Z;
00071 } AxesRaw_TypeDef;
00072 
00073 
00074 /*** Static variables --------------------------------------------------------- ***/
00075 #ifdef DBG_MCU
00076 /* betzw: enable debugging while using sleep modes */
00077 #include "DbgMCU.h"
00078 static DbgMCU enable_dbg;
00079 #endif // DBG_MCU
00080 
00081 static X_NUCLEO_IKS01A1 *mems_expansion_board = X_NUCLEO_IKS01A1::Instance();
00082 static MotionSensor *accelerometer = mems_expansion_board->GetAccelerometer();
00083 
00084 static Ticker ticker;
00085 //static DigitalOut myled(LED1, LED_OFF);
00086 
00087 static volatile bool timer_irq_triggered = false;
00088 
00089 /*** Interrupt Handler Top-Halves ------------------------------------------------------ ***/
00090 /* Called in interrupt context, therefore just set a trigger variable */
00091 static void timer_irq(void) {
00092     timer_irq_triggered = true;
00093 }
00094 
00095 /* Main cycle function */
00096 static void main_cycle(void) {
00097     AxesRaw_TypeDef ACC_Value;
00098     unsigned int ret = 0;
00099 
00100     /* Determine Environmental Values */
00101     ret |= (!CALL_METH(accelerometer, Get_X_Axes, (int32_t *)&ACC_Value, 0) ? 0x0 : 0x20);;
00102 
00103     /* Print Values Out */
00104     printf("ACC [mg]:     X:%9ld Y:%9ld Z:%9ld\n", ACC_Value.AXIS_X, ACC_Value.AXIS_Y, ACC_Value.AXIS_Z);
00105 }
00106 
00107 
00108 /*** Main function ------------------------------------------------------------- ***/
00109 /* Generic main function/loop for enabling WFE in case of 
00110    interrupt based cyclic execution
00111 */
00112 int main()
00113 {
00114     uint8_t id2;
00115     /* Start & initialize */
00116     printf("\n--- Starting new run ---\n");
00117     CALL_METH(accelerometer, ReadID, &id2, 0x0);    
00118 
00119     /* Start timer irq */
00120     //ticker.attach_us(timer_irq, MS_INTERVALS * APP_LOOP_PERIOD);
00121     ticker.attach_us(timer_irq, 200000);
00122 
00123     while (true) {
00124         if(timer_irq_triggered) {
00125             timer_irq_triggered = false;
00126             main_cycle();
00127         } else {
00128             __WFE(); /* it is recommended that SEVONPEND in the 
00129                     System Control Register is NOT set */
00130         }
00131     }
00132 }