ISSDK  1.7
IoT Sensing Software Development Kit
main_agmp03_freertos_two_tasks.c
Go to the documentation of this file.
1 /*
2  * The Clear BSD License
3  * Copyright (c) 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_agmp03_freertos_two_tasks.c
36  \brief FreeRTOS (two task) implementation of sensor fusion.
37 
38  This file shows one recommended way to incorporate sensor fusion capabilities
39  into a FreeRTOS project.
40 */
41 
42 /* FreeRTOS kernel includes. */
43 #include "FreeRTOS.h"
44 #include "task.h"
45 #include "queue.h"
46 #include "timers.h"
47 #include "event_groups.h"
48 
49 // KSDK & ISSDK Headers
50 #include "fsl_debug_console.h" // KSDK header file for the debug interface
51 #include "board.h" // KSDK header file to define board configuration
52 #include "pin_mux.h" // KSDK header file for pin mux initialization functions
53 #include "clock_config.h" // KSDK header file for clock configuration
54 #ifndef CPU_LPC54114J256BD64_cm4
55 #include "fsl_port.h" // KSDK header file for Port I/O control
56 #endif
57 #include "fsl_i2c.h" // KSDK header file for I2C interfaces
58 #include "fxas21002.h" // register address and bit field definitions
59 #include "fxls8962.h" // register address and bit field definitions
60 #include "mag3110.h" // register address and bit field definitions
61 #include "register_io_i2c.h"
62 #include "fsl_i2c_cmsis.h"
63 //#include "fsl_dspi_cmsis.h"
64 
65 // Sensor Fusion Headers
66 #include "sensor_fusion.h" // top level magCal and sensor fusion interfaces
67 #include "control.h" // Command/Streaming interface - application specific
68 #include "status.h" // Sta:tus indicator interface - application specific
69 #include "drivers.h" // NXP sensor drivers OR customer-supplied drivers
70 
71 // Global data structures
72 SensorFusionGlobals sfg; ///< This is the primary sensor fusion data structure
73 ControlSubsystem controlSubsystem; ///< used for serial communications
74 StatusSubsystem statusSubsystem; ///< provides visual (usually LED) status indicator
75 struct PhysicalSensor sensors[4]; ///< This implementation uses four physical sensors
76 EventGroupHandle_t event_group = NULL;
77 
80  .functionParam = NULL,
81  .idleFunction = NULL
82 };
83 
84 static void read_task(void *pvParameters); // FreeRTOS Task definition
85 static void fusion_task(void *pvParameters); // FreeRTOS Task definition
86 
87 /// This is a FreeRTOS (dual task) implementation of the NXP sensor fusion demo build.
88 int main(void)
89 {
90  ARM_DRIVER_I2C* I2Cdrv = &I2C_S_DRIVER; // defined in the <shield>.h file
91  BOARD_InitPins(); // defined in pin_mux.c, initializes pkg pins
92  BOARD_BootClockRUN(); // defined in clock_config.c, initializes clocks
93  BOARD_InitDebugConsole(); // defined in board.c, initializes the OpenSDA port
94 
95  I2Cdrv->Initialize(I2C_S_SIGNAL_EVENT); // Initialize the KSDK driver for the I2C port
96  I2Cdrv->PowerControl(ARM_POWER_FULL); // Set the I2C Power mode.
97  I2Cdrv->Control(ARM_I2C_BUS_SPEED, ARM_I2C_BUS_SPEED_FAST); // Configure the I2C bus speed
98 
99  initializeControlPort(&controlSubsystem); // configure pins and ports for the control sub-system
100  initializeStatusSubsystem(&statusSubsystem); // configure pins and ports for the status sub-system
101  initSensorFusionGlobals(&sfg, &statusSubsystem, &controlSubsystem); // Initialize sensor fusion structures
102  // "install" the sensors we will be using
103  sfg.installSensor(&sfg, &sensors[0], FXAS21002_I2C_ADDR, 1, (void*) I2Cdrv, &i2cBusInfo, FXAS21002_Init, FXAS21002_Read);
104  sfg.installSensor(&sfg, &sensors[1], MAG3110_I2C_ADDR, 1, (void*) I2Cdrv, &i2cBusInfo, MAG3110_Init, MAG3110_Read);
105  sfg.installSensor(&sfg, &sensors[2], MPL3115_I2C_ADDR, 1, (void*) I2Cdrv, &i2cBusInfo, MPL3115_Init, MPL3115_Read);
106  sfg.installSensor(&sfg, &sensors[3], FXLS8962_I2C_ADDR, 1, (void*) I2Cdrv, &i2cBusInfo, FXLS8962_Init, FXLS8962_Read);
107  sfg.initializeFusionEngine(&sfg); // This will initialize sensors and magnetic calibration
108 
109  event_group = xEventGroupCreate();
110  xTaskCreate(read_task, "READ", configMINIMAL_STACK_SIZE, NULL, tskIDLE_PRIORITY + 2, NULL);
111  xTaskCreate(fusion_task, "FUSION", configMINIMAL_STACK_SIZE, NULL, tskIDLE_PRIORITY + 1, NULL);
112 
113  sfg.setStatus(&sfg, NORMAL); // If we got this far, let's set status state to NORMAL
114  vTaskStartScheduler(); // Start the RTOS scheduler
115  sfg.setStatus(&sfg, HARD_FAULT); // If we got this far, FreeRTOS does not have enough memory allocated
116  for (;;) ;
117 }
118 
119 static void read_task(void *pvParameters)
120 {
121  uint16_t i=0; // general counter variable
122  portTickType lastWakeTime;
123  const portTickType frequency = 1; // tick counter runs at the read rate
124  lastWakeTime = xTaskGetTickCount();
125  while (1)
126  {
127  for (i=1; i<=OVERSAMPLE_RATE; i++) {
128  vTaskDelayUntil(&lastWakeTime, frequency);
129  sfg.readSensors(&sfg, i); // Reads sensors, applies HAL and does averaging (if applicable)
130  }
131  xEventGroupSetBits(event_group, B0);
132  }
133 }
134 
135 static void fusion_task(void *pvParameters)
136 {
137  uint16_t i=0; // general counter variable
138  while (1)
139  {
140  xEventGroupWaitBits(event_group, /* The event group handle. */
141  B0, /* The bit pattern the event group is waiting for. */
142  pdTRUE, /* BIT_0 and BIT_4 will be cleared automatically. */
143  pdFALSE, /* Don't wait for both bits, either bit unblock task. */
144  portMAX_DELAY); /* Block indefinitely to wait for the condition to be met. */
145 
146  sfg.conditionSensorReadings(&sfg); // magCal is run as part of this
147  sfg.runFusion(&sfg); // Run the actual fusion algorithms
148  sfg.applyPerturbation(&sfg); // apply debug perturbation (testing only)
149 
150  sfg.loopcounter++; // The loop counter is used to "serialize" mag cal operations
151  i=i+1;
152  if (i>=4) { // Some status codes include a "blink" feature. This loop
153  i=0; // should cycle at least four times for that to operate correctly.
154  sfg.updateStatus(&sfg); // This is where pending status updates are made visible
155  }
156  sfg.queueStatus(&sfg, NORMAL); // assume NORMAL status for next pass through the loop
157  sfg.pControlSubsystem->stream(&sfg, sUARTOutputBuffer); // Send stream data to the Sensor Fusion Toolbox
158  }
159 }
160 
161 /// \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
#define OVERSAMPLE_RATE
The register_io_i2c.h file declares low-level interface functions for reading and writing sensor regi...
int main(void)
This is a FreeRTOS (dual task) implementation of the NXP sensor fusion demo build.
int8_t FXAS21002_Read(struct PhysicalSensor *sensor, SensorFusionGlobals *sfg)
ControlSubsystem controlSubsystem
used for serial communications
#define BOARD_BootClockRUN
Definition: clock_config.h:45
registerDeviceInfo_t i2cBusInfo
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)
void BOARD_InitDebugConsole(void)
Definition: board.c:41
StatusSubsystem statusSubsystem
provides visual (usually LED) status indicator
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.
SensorFusionGlobals sfg
This is the primary sensor fusion data structure.
StatusSubsystem() provides an object-like interface for communicating status to the user...
Definition: status.h:48
The top level fusion structure.
int8_t MAG3110_Read(struct PhysicalSensor *sensor, SensorFusionGlobals *sfg)
EventGroupHandle_t event_group
#define B0
Definition: sensor_fusion.h:98
applyPerturbation_t * applyPerturbation
apply step function for testing purposes
The mag3110.h contains the MAG3110 Magnetic sensor register definitions, access macros, and device access functions.
#define FXLS8962_I2C_ADDR
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)
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.
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
#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
Non-recoverable FAULT = something went very wrong.
Application-specific status subsystem.
This file contains the FXLS8962 Accelerometer register definitions, access macros, and device access functions.
#define MPL3115_I2C_ADDR
readSensors_t * readSensors
read all physical sensors
conditionSensorReadings_t * conditionSensorReadings
preprocessing step for sensor fusion
Defines control sub-system.
struct ControlSubsystem * pControlSubsystem
int8_t FXLS8962_Read(struct PhysicalSensor *sensor, SensorFusionGlobals *sfg)
struct PhysicalSensor sensors[4]
This implementation uses four physical sensors.
This structure defines the device specific info required by register I/O.
Definition: sensor_drv.h:128
int8_t FXLS8962_Init(struct PhysicalSensor *sensor, SensorFusionGlobals *sfg)
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
int8_t MAG3110_Init(struct PhysicalSensor *sensor, SensorFusionGlobals *sfg)
An instance of PhysicalSensor structure type should be allocated for each physical sensors (combo dev...
#define MAG3110_I2C_ADDR
int32_t loopcounter
counter incrementing each iteration of sensor fusion (typically 25Hz)
#define I2C_S_SIGNAL_EVENT
Definition: issdk_hal.h:60