ISSDK  1.8
IoT Sensing Software Development Kit
main_freertos_agm02.c
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2015, Freescale Semiconductor, Inc.
3  * Copyright 2016-2017 NXP
4  * All rights reserved.
5  *
6  * SPDX-License-Identifier: BSD-3-Clause
7  */
8 
9 /*! \file main_freertos_agm02.c
10  \brief FreeRTOS (two task) implementation of sensor fusion on FRDM-K64F/FRDM-FXS-AGM02.
11 
12  This file shows one recommended way to incorporate sensor fusion capabilities
13  into a FreeRTOS project.
14 */
15 
16 // KSDK and ISSDK Headers
17 #include "fsl_debug_console.h" // KSDK header file for the debug interface
18 #include "board.h" // KSDK header file to define board configuration
19 #include "pin_mux.h" // KSDK header file for pin mux initialization functions
20 #include "clock_config.h" // KSDK header file for clock configuration
21 #include "fsl_port.h" // KSDK header file for Port I/O control
22 #include "fsl_i2c.h" // KSDK header file for I2C interfaces
23 #include "register_io_i2c.h"
24 #include "fsl_i2c_cmsis.h"
25 
26 // Sensor Fusion Headers
27 #include "sensor_fusion.h" // top level magCal and sensor fusion interfaces
28 #include "control.h" // Command/Streaming interface - application specific
29 #include "status.h" // Status indicator interface - application specific
30 #include "drivers.h" // NXP sensor drivers OR customer-supplied drivers
31 #include "driver_pit.h" // Project-specific - PIT is used to control main() timing loop
32 
33 /* FreeRTOS kernel includes. */
34 #include "FreeRTOS.h"
35 #include "task.h"
36 #include "queue.h"
37 #include "timers.h"
38 #include "event_groups.h"
39 
40 // Global data structures
41 SensorFusionGlobals sfg; ///< This is the primary sensor fusion data structure
42 ControlSubsystem controlSubsystem; ///< used for serial communications
43 StatusSubsystem statusSubsystem; ///< provides visual (usually LED) status indicator
44 struct PhysicalSensor sensors[3]; ///< This implementation uses three physical sensors
45 EventGroupHandle_t event_group = NULL;
46 
49  .functionParam = NULL,
50  .idleFunction = NULL
51 };
52 
53 static void read_task(void *pvParameters);
54 static void fusion_task(void *pvParameters);
55 
56 /// This is a FreeRTOS (dual task) implementation of the NXP sensor fusion demo build.
57 int main(void)
58 {
59  ARM_DRIVER_I2C* I2Cdrv = &I2C_S_DRIVER; // defined in the <shield>.h file
60  BOARD_InitPins(); // defined in pin_mux.c, initializes pkg pins
61  BOARD_BootClockRUN(); // defined in clock_config.c, initializes clocks
62  BOARD_InitDebugConsole(); // defined in board.c, initializes the OpenSDA port
63 
64  I2Cdrv->Initialize(I2C_S_SIGNAL_EVENT); // Initialize the KSDK driver for the I2C port
65  I2Cdrv->PowerControl(ARM_POWER_FULL); // Set the I2C Power mode.
66  I2Cdrv->Control(ARM_I2C_BUS_SPEED, ARM_I2C_BUS_SPEED_FAST); // Configure the I2C bus speed
67 
68  initializeControlPort(&controlSubsystem); // configure pins and ports for the control sub-system
69  initializeStatusSubsystem(&statusSubsystem); // configure pins and ports for the status sub-system
70  initSensorFusionGlobals(&sfg, &statusSubsystem, &controlSubsystem); // Initialize sensor fusion structures
71  // "install" the sensors we will be using
72  sfg.installSensor(&sfg, &sensors[0], FXLS8952C_I2C_ADDR, 1, (void*) I2Cdrv, &i2cBusInfo, FXLS8952_Init, FXLS8952_Read);
73  sfg.installSensor(&sfg, &sensors[1], FXAS21002_I2C_ADDR, 1, (void*) I2Cdrv, &i2cBusInfo, FXAS21002_Init, FXAS21002_Read);
74  sfg.installSensor(&sfg, &sensors[2], MAG3110_I2C_ADDR, 1, (void*) I2Cdrv, &i2cBusInfo, MAG3110_Init, MAG3110_Read);
75  sfg.initializeFusionEngine(&sfg); // This will initialize sensors and magnetic calibration
76 
77  event_group = xEventGroupCreate();
78  xTaskCreate(read_task, "READ", configMINIMAL_STACK_SIZE, NULL, tskIDLE_PRIORITY + 2, NULL);
79  xTaskCreate(fusion_task, "FUSION", configMINIMAL_STACK_SIZE, NULL, tskIDLE_PRIORITY + 1, NULL);
80 
81  sfg.setStatus(&sfg, NORMAL); // If we got this far, let's set status state to NORMAL
82  vTaskStartScheduler(); // Start the RTOS scheduler
83  sfg.setStatus(&sfg, HARD_FAULT); // If we got this far, FreeRTOS does not have enough memory allocated
84  for (;;) ;
85 }
86 
87 static void read_task(void *pvParameters)
88 {
89  uint16_t i=0; // general counter variable
90  portTickType lastWakeTime;
91  const portTickType frequency = 1; // tick counter runs at the read rate
92  lastWakeTime = xTaskGetTickCount();
93  while (1)
94  {
95  for (i=1; i<=OVERSAMPLE_RATE; i++) {
96  vTaskDelayUntil(&lastWakeTime, frequency);
97  sfg.readSensors(&sfg, i); // Reads sensors, applies HAL and does averaging (if applicable)
98  }
99  xEventGroupSetBits(event_group, B0);
100  }
101 }
102 
103 static void fusion_task(void *pvParameters)
104 {
105  uint16_t i=0; // general counter variable
106  while (1)
107  {
108  xEventGroupWaitBits(event_group, /* The event group handle. */
109  B0, /* The bit pattern the event group is waiting for. */
110  pdTRUE, /* BIT_0 and BIT_4 will be cleared automatically. */
111  pdFALSE, /* Don't wait for both bits, either bit unblock task. */
112  portMAX_DELAY); /* Block indefinitely to wait for the condition to be met. */
113 
114  sfg.conditionSensorReadings(&sfg); // magCal is run as part of this
115  sfg.runFusion(&sfg); // Run the actual fusion algorithms
116  sfg.applyPerturbation(&sfg); // apply debug perturbation (testing only)
117 
118  sfg.loopcounter++; // The loop counter is used to "serialize" mag cal operations
119  i=i+1;
120  if (i>=4) { // Some status codes include a "blink" feature. This loop
121  i=0; // should cycle at least four times for that to operate correctly.
122  sfg.updateStatus(&sfg); // This is where pending status updates are made visible
123  }
124  sfg.queueStatus(&sfg, NORMAL); // assume NORMAL status for next pass through the loop
125  sfg.pControlSubsystem->stream(&sfg, sUARTOutputBuffer); // Send stream data to the Sensor Fusion Toolbox
126 
127  }
128 }
129 
130 /// \endcode
EventGroupHandle_t event_group
#define B0
Definition: sensor_fusion.h:72
readSensors_t * readSensors
read all physical sensors
This structure defines the device specific info required by register I/O.
Definition: sensor_drv.h:102
initializeFusionEngine_t * initializeFusionEngine
set sensor fusion structures to initial values
#define OVERSAMPLE_RATE
Provides function prototypes for driver level interfaces.
StatusSubsystem statusSubsystem
provides visual (usually LED) status indicator
#define MAG3110_I2C_ADDR
Operation is Nominal.
An instance of PhysicalSensor structure type should be allocated for each physical sensors (combo dev...
setStatus_t * queueStatus
queue status change for next regular interval
int8_t MAG3110_Read(struct PhysicalSensor *sensor, SensorFusionGlobals *sfg)
int8_t FXLS8952_Read(struct PhysicalSensor *sensor, SensorFusionGlobals *sfg)
int8_t MAG3110_Init(struct PhysicalSensor *sensor, SensorFusionGlobals *sfg)
registerDeviceInfo_t i2cBusInfo
Application-specific status subsystem.
conditionSensorReadings_t * conditionSensorReadings
preprocessing step for sensor fusion
Defines control sub-system.
The top level fusion structure.
int main(void)
This is a FreeRTOS (dual task) implementation of the NXP sensor fusion demo build.
void initSensorFusionGlobals(SensorFusionGlobals *sfg, StatusSubsystem *pStatusSubsystem, ControlSubsystem *pControlSubsystem)
utility function to insert default values in the top level structure
Definition: sensor_fusion.c:51
int8_t FXAS21002_Init(struct PhysicalSensor *sensor, SensorFusionGlobals *sfg)
#define I2C_S_SIGNAL_EVENT
Definition: issdk_hal.h:34
The register_io_i2c.h file declares low-level interface functions for reading and writing sensor regi...
ControlSubsystem controlSubsystem
used for serial communications
#define I2C_S_DRIVER
Definition: issdk_hal.h:33
he ControlSubsystem encapsulates command and data streaming functions.
Definition: control.h:42
#define BOARD_BootClockRUN
Definition: clock_config.h:19
runFusion_t * runFusion
run the fusion routines
#define FXAS21002_I2C_ADDR
Non-recoverable FAULT = something went very wrong.
The sensor_fusion.h file implements the top level programming interface.
setStatus_t * setStatus
change status indicator immediately
ARM_DRIVER_I2C * I2Cdrv
uint8_t sUARTOutputBuffer[256]
main output buffer defined in control.c
Definition: control.c:37
updateStatus_t * updateStatus
status=next status
installSensor_t * installSensor
function for installing a new sensor into t
struct ControlSubsystem * pControlSubsystem
int8_t FXLS8952_Init(struct PhysicalSensor *sensor, SensorFusionGlobals *sfg)
int8_t initializeControlPort(ControlSubsystem *pComm)
Initialize the control subsystem and all related hardware.
Definition: control.c:160
SensorFusionGlobals sfg
This is the primary sensor fusion data structure.
struct PhysicalSensor sensors[3]
This implementation uses three physical sensors.
#define I2C_S_DEVICE_INDEX
Definition: issdk_hal.h:35
int8_t FXAS21002_Read(struct PhysicalSensor *sensor, SensorFusionGlobals *sfg)
#define FXLS8952C_I2C_ADDR
void BOARD_InitDebugConsole(void)
Definition: board.c:15
void initializeStatusSubsystem(StatusSubsystem *pStatus)
Definition: status.c:165
void BOARD_InitPins(void)
Configures pin routing and optionally pin electrical features.
Definition: pin_mux.c:47
streamData_t * stream
function to create packets for serial stream
Definition: control.h:51
applyPerturbation_t * applyPerturbation
apply step function for testing purposes
StatusSubsystem() provides an object-like interface for communicating status to the user...
Definition: status.h:22
int32_t loopcounter
counter incrementing each iteration of sensor fusion (typically 25Hz)
Provides a simple abstraction for a periodic interval timer.