ISSDK  1.7
IoT Sensing Software Development Kit
main_baremetal.c
Go to the documentation of this file.
1 /*
2  * The Clear BSD License
3  * Copyright (c) 2015 - 2016, Freescale Semiconductor, Inc.
4  * Copyright 2016-2017 NXP
5  * All rights reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without modification,
8  * are permitted (subject to the limitations in the disclaimer below) provided
9  * that the following conditions are met:
10  *
11  * o Redistributions of source code must retain the above copyright notice, this list
12  * of conditions and the following disclaimer.
13  *
14  * o Redistributions in binary form must reproduce the above copyright notice, this
15  * list of conditions and the following disclaimer in the documentation and/or
16  * other materials provided with the distribution.
17  *
18  * o Neither the name of the copyright holder nor the names of its
19  * contributors may be used to endorse or promote products derived from this
20  * software without specific prior written permission.
21  *
22  * NO EXPRESS OR IMPLIED LICENSES TO ANY PARTY'S PATENT RIGHTS ARE GRANTED BY THIS LICENSE.
23  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
24  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
25  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
26  * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR
27  * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
28  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
29  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
30  * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
31  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
32  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
33  */
34 
35 /*! \file main_baremetal.c
36  \brief Bare metal implementation of sensor fusion on FRDM-K64F.
37 
38  This file shows the recommended way to incorporate sensor fusion capabilities
39  into a bare metal (no RTOS) project.
40 */
41 
42 // KSDK and ISSDK Headers
43 #include "fsl_debug_console.h" // KSDK header file for the debug interface
44 #include "board.h" // KSDK header file to define board configuration
45 #include "pin_mux.h" // KSDK header file for pin mux initialization functions
46 #include "clock_config.h" // KSDK header file for clock configuration
47 #include "fsl_port.h" // KSDK header file for Port I/O control
48 #include "fsl_i2c.h" // KSDK header file for I2C interfaces
49 #include "fsl_smc.h"
50 #include "register_io_i2c.h"
51 #include "fsl_i2c_cmsis.h"
52 
53 #include "fsl_pit.h" // KSDK header feile for Periodic Interval Timer
54 #include "fxas21002.h" // Register and bit-field definitions
55 #include "mpl3115.h" // Register and bit-field definitions
56 #include "fxos8700.h" // Register and bit-field definitions
57 
58 // Sensor Fusion Headers
59 #include "sensor_fusion.h" // top level magCal and sensor fusion interfaces
60 #include "control.h" // Command/Streaming interface - application specific
61 #include "status.h" // Status indicator interface - application specific
62 #include "drivers.h" // NXP sensor drivers OR customer-supplied drivers
63 #include "driver_pit.h" // Project-specific - PIT is used to control main() timing loop
64 
67  .functionParam = SMC,
69 };
70 
71 // Global data structures
72 SensorFusionGlobals sfg; ///< This is the primary sensor fusion data structure
73 struct ControlSubsystem controlSubsystem; ///< used for serial communications
74 struct StatusSubsystem statusSubsystem; ///< provides visual (usually LED) status indicator
75 struct PhysicalSensor sensors[3]; ///< This implementation uses up to 3 sensors
76 
77 /// This is a bare-metal implementation of the NXP sensor fusion demo build.
78 int main(void)
79 {
80  ARM_DRIVER_I2C* I2Cdrv = &I2C_S_DRIVER; // defined in the <shield>.h file
81  uint16_t i=0; // general counter variable
82  BOARD_InitPins(); // defined in pin_mux.c, initializes pkg pins
83  BOARD_BootClockRUN(); // defined in clock_config.c, initializes clocks
84  BOARD_InitDebugConsole(); // defined in board.c, initializes the OpenSDA port
85 
86  I2Cdrv->Initialize(I2C_S_SIGNAL_EVENT); // Initialize the KSDK driver for the I2C port
87  I2Cdrv->PowerControl(ARM_POWER_FULL); // Set the I2C Power mode.
88  I2Cdrv->Control(ARM_I2C_BUS_SPEED, ARM_I2C_BUS_SPEED_FAST); // Configure the I2C bus speed
89 
90  initializeControlPort(&controlSubsystem); // configure pins and ports for the control sub-system
91  initializeStatusSubsystem(&statusSubsystem); // configure pins and ports for the status sub-system
92  initSensorFusionGlobals(&sfg, &statusSubsystem, &controlSubsystem); // Initialize sensor fusion structures
93  // "install" the sensors we will be using
94 #if F_USING_ACCEL || F_USING_MAG
95  sfg.installSensor(&sfg, &sensors[0], FXOS8700_I2C_ADDR, 1, (void*) I2Cdrv, &i2cBusInfo, FXOS8700_Init, FXOS8700_Read);
96 #endif
97 #if F_USING_GYRO
98  sfg.installSensor(&sfg, &sensors[1], FXAS21002_I2C_ADDR, 1, (void*) I2Cdrv, &i2cBusInfo, FXAS21002_Init, FXAS21002_Read);
99 #endif
100 #if F_USING_PRESSURE
101  sfg.installSensor(&sfg, &sensors[2], MPL3115_I2C_ADDR, 1, (void*) I2Cdrv, &i2cBusInfo, MPL3115_Init, MPL3115_Read);
102 #endif
103  sfg.initializeFusionEngine(&sfg); // This will initialize sensors and magnetic calibration
104 
105  pit_init(1000000/FUSION_HZ); // pitIsrFlag will be set true at FUSION_HZ periodic intervals
106 
107  sfg.setStatus(&sfg, NORMAL); // If we got this far, let's set status state to NORMAL
108  while (true)
109  {
110  if (true == pitIsrFlag) { // Check whether occur interupt and toggle LED
111  sfg.readSensors(&sfg, 1); // Reads sensors, applies HAL and does averaging (if applicable)
112  sfg.conditionSensorReadings(&sfg); // magCal is run as part of this
113  sfg.runFusion(&sfg); // Run the actual fusion algorithms
114  sfg.applyPerturbation(&sfg); // apply debug perturbation (testing only)
115  sfg.loopcounter++; // The loop counter is used to "serialize" mag cal operations
116  i=i+1;
117  if (i>=4) { // Some status codes include a "blink" feature. This loop
118  i=0; // should cycle at least four times for that to operate correctly.
119  sfg.updateStatus(&sfg); // This is where pending status updates are made visible
120  }
121 
122  sfg.queueStatus(&sfg, NORMAL); // assume NORMAL status for next pass through the loop
123  sfg.pControlSubsystem->stream(&sfg, sUARTOutputBuffer); // Send stream data to the Sensor Fusion Toolbox
124  pitIsrFlag = false; // Reset the flag for the next cycle
125  }
126  }
127 }
128 /// \endcode
void initSensorFusionGlobals(SensorFusionGlobals *sfg, StatusSubsystem *pStatusSubsystem, ControlSubsystem *pControlSubsystem)
utility function to insert default values in the top level structure
Definition: sensor_fusion.c:77
The register_io_i2c.h file declares low-level interface functions for reading and writing sensor regi...
int8_t FXAS21002_Read(struct PhysicalSensor *sensor, SensorFusionGlobals *sfg)
#define BOARD_BootClockRUN
Definition: clock_config.h:45
void initializeStatusSubsystem(StatusSubsystem *pStatus)
Definition: status.c:191
installSensor_t * installSensor
function for installing a new sensor into t
int8_t MPL3115_Read(struct PhysicalSensor *sensor, SensorFusionGlobals *sfg)
struct StatusSubsystem statusSubsystem
provides visual (usually LED) status indicator
void BOARD_InitDebugConsole(void)
Definition: board.c:41
void(* registeridlefunction_t)(void *userParam)
This is the register idle function type.
Definition: sensor_drv.h:123
The fxos8700.h file contains the register definitions for FXOS8700 sensor driver. ...
updateStatus_t * updateStatus
status=next status
he ControlSubsystem encapsulates command and data streaming functions.
Definition: control.h:68
The fxas21002.h contains the fxas21002 sensor register definitions and its bit mask.
int8_t FXOS8700_Read(struct PhysicalSensor *sensor, SensorFusionGlobals *sfg)
void pit_init(uint32_t microseconds)
Definition: driver_pit.c:70
StatusSubsystem() provides an object-like interface for communicating status to the user...
Definition: status.h:48
The top level fusion structure.
applyPerturbation_t * applyPerturbation
apply step function for testing purposes
int main(void)
This is a bare-metal implementation of the NXP sensor fusion demo build.
int8_t FXAS21002_Init(struct PhysicalSensor *sensor, SensorFusionGlobals *sfg)
initializeFusionEngine_t * initializeFusionEngine
set sensor fusion structures to initial values
int8_t MPL3115_Init(struct PhysicalSensor *sensor, SensorFusionGlobals *sfg)
volatile bool pitIsrFlag
Definition: driver_pit.c:61
int8_t FXOS8700_Init(struct PhysicalSensor *sensor, SensorFusionGlobals *sfg)
setStatus_t * setStatus
change status indicator immediately
The sensor_fusion.h file implements the top level programming interface.
Provides function prototypes for driver level interfaces.
The mpl3115.h contains the MPL3115 Pressure sensor register definitions, access macros, and device access functions.
uint8_t sUARTOutputBuffer[256]
main output buffer defined in control.c
Definition: control.c:63
#define I2C_S_DEVICE_INDEX
Definition: issdk_hal.h:61
registerDeviceInfo_t i2cBusInfo
#define FXAS21002_I2C_ADDR
runFusion_t * runFusion
run the fusion routines
#define I2C_S_DRIVER
Definition: issdk_hal.h:59
int8_t initializeControlPort(ControlSubsystem *pComm)
Initialize the control subsystem and all related hardware.
Definition: control.c:186
streamData_t * stream
function to create packets for serial stream
Definition: control.h:77
Provides a simple abstraction for a periodic interval timer.
Application-specific status subsystem.
SensorFusionGlobals sfg
This is the primary sensor fusion data structure.
#define MPL3115_I2C_ADDR
readSensors_t * readSensors
read all physical sensors
#define FUSION_HZ
(int) actual rate of fusion algorithm execution and sensor FIFO reads
conditionSensorReadings_t * conditionSensorReadings
preprocessing step for sensor fusion
Defines control sub-system.
struct ControlSubsystem * pControlSubsystem
struct ControlSubsystem controlSubsystem
used for serial communications
#define SMC
Definition: lpc54114.h:144
struct PhysicalSensor sensors[3]
This implementation uses up to 3 sensors.
status_t SMC_SetPowerModeWait(void *arg)
Configures the system to WAIT power mode. API name used from Kinetis family to maintain compatibility...
Definition: lpc54114.c:181
This structure defines the device specific info required by register I/O.
Definition: sensor_drv.h:128
Operation is Nominal.
void BOARD_InitPins(void)
Configures pin routing and optionally pin electrical features.
Definition: pin_mux.c:73
setStatus_t * queueStatus
queue status change for next regular interval
An instance of PhysicalSensor structure type should be allocated for each physical sensors (combo dev...
#define FXOS8700_I2C_ADDR
int32_t loopcounter
counter incrementing each iteration of sensor fusion (typically 25Hz)
#define I2C_S_SIGNAL_EVENT
Definition: issdk_hal.h:60