ISSDK  1.7
IoT Sensing Software Development Kit
main_freertos_agm02.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_freertos_agm02.c
36  \brief FreeRTOS (two task) implementation of sensor fusion on FRDM-K64F/FRDM-FXS-AGM02.
37 
38  This file shows one recommended way to incorporate sensor fusion capabilities
39  into a FreeRTOS 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 "register_io_i2c.h"
50 #include "fsl_i2c_cmsis.h"
51 
52 // Sensor Fusion Headers
53 #include "sensor_fusion.h" // top level magCal and sensor fusion interfaces
54 #include "control.h" // Command/Streaming interface - application specific
55 #include "status.h" // Status indicator interface - application specific
56 #include "drivers.h" // NXP sensor drivers OR customer-supplied drivers
57 #include "driver_pit.h" // Project-specific - PIT is used to control main() timing loop
58 
59 /* FreeRTOS kernel includes. */
60 #include "FreeRTOS.h"
61 #include "task.h"
62 #include "queue.h"
63 #include "timers.h"
64 #include "event_groups.h"
65 
66 // Global data structures
67 SensorFusionGlobals sfg; ///< This is the primary sensor fusion data structure
68 ControlSubsystem controlSubsystem; ///< used for serial communications
69 StatusSubsystem statusSubsystem; ///< provides visual (usually LED) status indicator
70 struct PhysicalSensor sensors[3]; ///< This implementation uses three physical sensors
71 EventGroupHandle_t event_group = NULL;
72 
75  .functionParam = NULL,
76  .idleFunction = NULL
77 };
78 
79 static void read_task(void *pvParameters);
80 static void fusion_task(void *pvParameters);
81 
82 /// This is a FreeRTOS (dual task) implementation of the NXP sensor fusion demo build.
83 int main(void)
84 {
85  ARM_DRIVER_I2C* I2Cdrv = &I2C_S_DRIVER; // defined in the <shield>.h file
86  BOARD_InitPins(); // defined in pin_mux.c, initializes pkg pins
87  BOARD_BootClockRUN(); // defined in clock_config.c, initializes clocks
88  BOARD_InitDebugConsole(); // defined in board.c, initializes the OpenSDA port
89 
90  I2Cdrv->Initialize(I2C_S_SIGNAL_EVENT); // Initialize the KSDK driver for the I2C port
91  I2Cdrv->PowerControl(ARM_POWER_FULL); // Set the I2C Power mode.
92  I2Cdrv->Control(ARM_I2C_BUS_SPEED, ARM_I2C_BUS_SPEED_FAST); // Configure the I2C bus speed
93 
94  initializeControlPort(&controlSubsystem); // configure pins and ports for the control sub-system
95  initializeStatusSubsystem(&statusSubsystem); // configure pins and ports for the status sub-system
96  initSensorFusionGlobals(&sfg, &statusSubsystem, &controlSubsystem); // Initialize sensor fusion structures
97  // "install" the sensors we will be using
98  sfg.installSensor(&sfg, &sensors[0], FXLS8952C_I2C_ADDR, 1, (void*) I2Cdrv, &i2cBusInfo, FXLS8952_Init, FXLS8952_Read);
99  sfg.installSensor(&sfg, &sensors[1], FXAS21002_I2C_ADDR, 1, (void*) I2Cdrv, &i2cBusInfo, FXAS21002_Init, FXAS21002_Read);
100  sfg.installSensor(&sfg, &sensors[2], MAG3110_I2C_ADDR, 1, (void*) I2Cdrv, &i2cBusInfo, MAG3110_Init, MAG3110_Read);
101  sfg.initializeFusionEngine(&sfg); // This will initialize sensors and magnetic calibration
102 
103  event_group = xEventGroupCreate();
104  xTaskCreate(read_task, "READ", configMINIMAL_STACK_SIZE, NULL, tskIDLE_PRIORITY + 2, NULL);
105  xTaskCreate(fusion_task, "FUSION", configMINIMAL_STACK_SIZE, NULL, tskIDLE_PRIORITY + 1, NULL);
106 
107  sfg.setStatus(&sfg, NORMAL); // If we got this far, let's set status state to NORMAL
108  vTaskStartScheduler(); // Start the RTOS scheduler
109  sfg.setStatus(&sfg, HARD_FAULT); // If we got this far, FreeRTOS does not have enough memory allocated
110  for (;;) ;
111 }
112 
113 static void read_task(void *pvParameters)
114 {
115  uint16_t i=0; // general counter variable
116  portTickType lastWakeTime;
117  const portTickType frequency = 1; // tick counter runs at the read rate
118  lastWakeTime = xTaskGetTickCount();
119  while (1)
120  {
121  for (i=1; i<=OVERSAMPLE_RATE; i++) {
122  vTaskDelayUntil(&lastWakeTime, frequency);
123  sfg.readSensors(&sfg, i); // Reads sensors, applies HAL and does averaging (if applicable)
124  }
125  xEventGroupSetBits(event_group, B0);
126  }
127 }
128 
129 static void fusion_task(void *pvParameters)
130 {
131  uint16_t i=0; // general counter variable
132  while (1)
133  {
134  xEventGroupWaitBits(event_group, /* The event group handle. */
135  B0, /* The bit pattern the event group is waiting for. */
136  pdTRUE, /* BIT_0 and BIT_4 will be cleared automatically. */
137  pdFALSE, /* Don't wait for both bits, either bit unblock task. */
138  portMAX_DELAY); /* Block indefinitely to wait for the condition to be met. */
139 
140  sfg.conditionSensorReadings(&sfg); // magCal is run as part of this
141  sfg.runFusion(&sfg); // Run the actual fusion algorithms
142  sfg.applyPerturbation(&sfg); // apply debug perturbation (testing only)
143 
144  sfg.loopcounter++; // The loop counter is used to "serialize" mag cal operations
145  i=i+1;
146  if (i>=4) { // Some status codes include a "blink" feature. This loop
147  i=0; // should cycle at least four times for that to operate correctly.
148  sfg.updateStatus(&sfg); // This is where pending status updates are made visible
149  }
150  sfg.queueStatus(&sfg, NORMAL); // assume NORMAL status for next pass through the loop
151  sfg.pControlSubsystem->stream(&sfg, sUARTOutputBuffer); // Send stream data to the Sensor Fusion Toolbox
152 
153  }
154 }
155 
156 /// \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...
int8_t FXAS21002_Read(struct PhysicalSensor *sensor, SensorFusionGlobals *sfg)
int8_t FXLS8952_Read(struct PhysicalSensor *sensor, SensorFusionGlobals *sfg)
#define BOARD_BootClockRUN
Definition: clock_config.h:45
#define FXLS8952C_I2C_ADDR
SensorFusionGlobals sfg
This is the primary sensor fusion data structure.
void initializeStatusSubsystem(StatusSubsystem *pStatus)
Definition: status.c:191
installSensor_t * installSensor
function for installing a new sensor into t
void BOARD_InitDebugConsole(void)
Definition: board.c:41
updateStatus_t * updateStatus
status=next status
he ControlSubsystem encapsulates command and data streaming functions.
Definition: control.h:68
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)
#define B0
Definition: sensor_fusion.h:98
applyPerturbation_t * applyPerturbation
apply step function for testing purposes
int8_t FXAS21002_Init(struct PhysicalSensor *sensor, SensorFusionGlobals *sfg)
initializeFusionEngine_t * initializeFusionEngine
set sensor fusion structures to initial values
int main(void)
This is a FreeRTOS (dual task) implementation of the NXP sensor fusion demo build.
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
EventGroupHandle_t event_group
#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
struct PhysicalSensor sensors[3]
This implementation uses three physical sensors.
streamData_t * stream
function to create packets for serial stream
Definition: control.h:77
Provides a simple abstraction for a periodic interval timer.
Non-recoverable FAULT = something went very wrong.
ControlSubsystem controlSubsystem
used for serial communications
Application-specific status subsystem.
registerDeviceInfo_t i2cBusInfo
readSensors_t * readSensors
read all physical sensors
conditionSensorReadings_t * conditionSensorReadings
preprocessing step for sensor fusion
Defines control sub-system.
struct ControlSubsystem * pControlSubsystem
This structure defines the device specific info required by register I/O.
Definition: sensor_drv.h:128
StatusSubsystem statusSubsystem
provides visual (usually LED) status indicator
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...
int8_t FXLS8952_Init(struct PhysicalSensor *sensor, SensorFusionGlobals *sfg)
#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