ISSDK  1.7
IoT Sensing Software Development Kit
mma9553_pedometer_spi.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 /**
36  * @file mma9553_pedometer_spi.c
37  * @brief The mma9553_pedometer_spi.c file implements the ISSDK MMA9553L sensor driver
38  * example demonstration as a Pedometer in SPI Mode.
39  */
40 
41 /* SDK Includes */
42 #include "board.h"
43 #include "pin_mux.h"
44 #include "fsl_lptmr.h"
45 #include "clock_config.h"
46 #include "fsl_debug_console.h"
47 
48 /* CMSIS Includes */
49 #include "Driver_SPI.h"
50 
51 /* ISSDK Includes */
52 #include "issdk_hal.h"
53 #include "gpio_driver.h"
54 #include "mma9553_drv.h"
55 #include "systick_utils.h"
56 
57 /*******************************************************************************
58  * Macros
59  ******************************************************************************/
60 #define SAMPLING_RATE_ms (100) /* Timeout for the ODR Timer. */
61 #define MMA9553_ACCEL_DATA_SIZE (6) /* 2 byte X,Y,Z Axis Data each. */
62 #define mma9553_en_callback LPTMR0_IRQHandler /* Timer timeout Callback. */
63 #define MMA9553_SSB_IO3 D10 /* The SSB_IO3 pin of MMA9553 on the FRDM-STBC-SA955x Board. */
64 #define RESET_GPIO A3 /* The RESET_GPIO pin of MMA9553 on the FRDM-STBC-SA955x Board. */
65 
66 /*******************************************************************************
67  * Constants
68  ******************************************************************************/
69 /*! Prepare the register write list to configure MMA9553L in 30Hz Mode. */
71  {SetFSRange_2g, 0, sizeof(SetFSRange_2g)}, /* Set FS Range 2G */
72  {SetSampleRate_30Hz, 0, sizeof(SetSampleRate_30Hz)}, /* Set Sensor Sampling Rate 30Hz */
73  {SetAFEPriority_for30Hz, 0, sizeof(SetAFEPriority_for30Hz)}, /* Set AFE Priority for 30Hz Sampling Rate */
74  {SetMBoxPriority_for30Hz, 0, sizeof(SetMBoxPriority_for30Hz)}, /* Set MBox Priority for 30Hz Sampling Rate */
76 
77 /*! Prepare the register read list to read the raw Accel data from MMA9553. */
80 
81 /*! Prepare the command list to read the Pedometer data from MMA9553. */
84 
85 /*! Prepare the read list to read the Pedometer data from MMA9553. */
88 
89 /*******************************************************************************
90  * Globals
91  ******************************************************************************/
92 volatile bool gMma9553DataReady;
93 
94 /*******************************************************************************
95  * Functions
96  ******************************************************************************/
97 /* LPTMR based ODR Callback function. */
99 {
100  LPTMR_ClearStatusFlags(LPTMR0, kLPTMR_TimerCompareFlag);
101  gMma9553DataReady = true;
102 }
103 
104 /*!
105  * @brief Main function
106  */
107 int main(void)
108 {
109  int32_t status;
110  lptmr_config_t lptmrConfig;
112  mma9553_pedometerdata_t pedometerData;
113  mma9553_spi_sensorhandle_t mma9553Driver;
114 
115  ARM_DRIVER_SPI *pSPIdriver = &SPI_S_DRIVER;
116 
117  /*! Initialize the MCU hardware */
118  BOARD_InitPins();
122 
123  /* Initialize ODR Timer. */
124  LPTMR_GetDefaultConfig(&lptmrConfig);
125  LPTMR_Init(LPTMR0, &lptmrConfig);
126  LPTMR_EnableInterrupts(LPTMR0, kLPTMR_TimerInterruptEnable);
127  LPTMR_SetTimerPeriod(LPTMR0, MSEC_TO_COUNT(SAMPLING_RATE_ms, CLOCK_GetFreq(kCLOCK_LpoClk)));
128  EnableIRQ(LPTMR0_IRQn);
129 
130  PRINTF("\r\n ISSDK MMA9553 sensor driver example for Pedometer Mode. \r\n");
131 
132  /*! Initialize the SPI driver. */
133  status = pSPIdriver->Initialize(SPI_S_SIGNAL_EVENT);
134  if (ARM_DRIVER_OK != status)
135  {
136  PRINTF("\r\n SPI Initialization Failed\r\n");
137  return -1;
138  }
139 
140  /*! Set the SPI Power mode. */
141  status = pSPIdriver->PowerControl(ARM_POWER_FULL);
142  if (ARM_DRIVER_OK != status)
143  {
144  PRINTF("\r\n SPI Power Mode setting Failed\r\n");
145  return -1;
146  }
147 
148  /*! Set the SPI Slave speed. */
149  status = pSPIdriver->Control(ARM_SPI_MODE_MASTER | ARM_SPI_CPOL0_CPHA0, SPI_S_BAUDRATE);
150  if (ARM_DRIVER_OK != status)
151  {
152  PRINTF("\r\n SPI Control Mode setting Failed\r\n");
153  return -1;
154  }
155 
156  /*! Initialize the MMA9553 sensor driver. */
158  if (SENSOR_ERROR_NONE != status)
159  {
160  PRINTF("\r\n Sensor Initialization Failed\r\n");
161  return -1;
162  }
163  PRINTF("\r\n Successfully Initialized Sensor. \r\n");
164 
165  /*! Set the task to be executed while waiting for I2C transactions to complete. */
167 
168  gMma9553DataReady = false; /* Do not read data, data will be read after ODR Timer expires. */
169 
170  /*! Configure the MMA9553 sensor driver with 30Hz Mode settings. */
171  status = MMA9553_SPI_Configure(&mma9553Driver, cMma9553Config30Hz);
172  if (SENSOR_ERROR_NONE != status)
173  {
174  PRINTF("\r\n MMA9553 Sensor Configuration Failed, Err = %d \r\n", status);
175  return -1;
176  }
177 
178  LPTMR_StartTimer(LPTMR0);
179  PRINTF("\r\n Successfully Applied MMA9553 Sensor Configuration\r\n");
180 
181  for (;;) /* Forever loop */
182  {
183  if (gMma9553DataReady == false)
184  {
185  continue;
186  }
187  else
188  {
189  gMma9553DataReady = false;
190  }
191 
192  /*! Read the Pedometer data from the MMA9553. */
193  status = MMA9553_SPI_CommandResponse(&mma9553Driver, cMma9553ReadPedometerCommand, cMma9553ReadPedometerOutput,
194  (uint8_t *)&pedometerData);
195  if (ARM_DRIVER_OK != status)
196  {
197  PRINTF("\r\n Read Failed. \r\n");
198  return -1;
199  }
200 
201  /* Swap bytes for 2 byte fields for Little Endian to Big Endian conversion. */
202  pedometerData.statusRegister = (pedometerData.statusRegister >> 8) | (pedometerData.statusRegister << 8);
203  pedometerData.stepCount = (pedometerData.stepCount >> 8) | (pedometerData.stepCount << 8);
204  pedometerData.distance = (pedometerData.distance >> 8) | (pedometerData.distance << 8);
205  pedometerData.speed = (pedometerData.speed >> 8) | (pedometerData.speed << 8);
206  pedometerData.calories = (pedometerData.calories >> 8) | (pedometerData.calories << 8);
207  pedometerData.sleepCount = (pedometerData.sleepCount >> 8) | (pedometerData.sleepCount << 8);
208 
209  PRINTF("\r\n Steps = %d Distance = %dm Speed = %dm/h Calories = %d \r\n", pedometerData.stepCount,
210  pedometerData.distance, pedometerData.speed, pedometerData.calories);
211 
212  /*! Read the raw sensor data from the MMA9553. */
213  status = MMA9553_SPI_CommandResponse(&mma9553Driver, NULL, cMma9553ReadRawOutput, (uint8_t *)&rawData.accel);
214  if (ARM_DRIVER_OK != status)
215  {
216  PRINTF("\r\n Read Failed. \r\n");
217  return -1;
218  }
219 
220  PRINTF("\r\n Accel X = %d Y = %d Z = %d \r\n", rawData.accel[0], rawData.accel[1], rawData.accel[2]);
221  ASK_USER_TO_RESUME(100); /* Ask for user input after processing 100 samples. */
222  }
223 }
int32_t MMA9553_SPI_Initialize(mma9553_spi_sensorhandle_t *pSensorHandle, ARM_DRIVER_SPI *pBus, uint8_t index, void *pSlaveSelect, void *pReset)
The interface function to initialize the sensor.
Definition: mma9553_drv.c:183
const uint8_t SetFSRange_2g[5]
Full-Scale Range Selections.
Definition: mma9553_drv.c:89
const registercommandlist_t cMma9553Config30Hz[]
#define MMA9553_ACCEL_DATA_SIZE
#define BOARD_BootClockRUN
Definition: clock_config.h:45
This structure defines the Size of response for pedometer data read command.
Definition: mma9553_drv.h:86
const uint8_t SetMBoxPriority_for30Hz[5]
Definition: mma9553_drv.c:118
const uint8_t ReadPedometerData[4]
Command to Read Pedometer Data.
Definition: mma9553_drv.c:124
#define MMA9553_XYZ_DATA_OFFSET
XYZ Data Register Offset.
Definition: mma9553.h:22
int32_t status
const uint8_t SetAFEPriority_for30Hz[5]
Definition: mma9553_drv.c:108
This defines the sensor specific information for SPI.
Definition: mma9553_drv.h:59
void BOARD_InitDebugConsole(void)
Definition: board.c:41
This structure defines the mma9553 pedometer data buffer.
Definition: mma9553_drv.h:79
const uint8_t SetSampleRate_30Hz[5]
Definition: mma9553_drv.c:98
void(* registeridlefunction_t)(void *userParam)
This is the register idle function type.
Definition: sensor_drv.h:123
#define __END_READ_DATA__
Definition: sensor_drv.h:77
volatile bool gMma9553DataReady
#define MMA9553_SSB_IO3
const registerreadlist_t cMma9553ReadPedometerOutput[]
#define ASK_USER_TO_RESUME(x)
Definition: frdm_k64f.h:132
#define __END_WRITE_CMD__
Definition: sensor_drv.h:83
int32_t MMA9553_SPI_Configure(mma9553_spi_sensorhandle_t *pSensorHandle, const registercommandlist_t *pCommandList)
The interface function to configure he sensor.
Definition: mma9553_drv.c:256
const registercommandlist_t cMma9553ReadPedometerCommand[]
#define SPI_S_DEVICE_INDEX
Definition: frdm_k64f.h:115
void BOARD_SystickEnable(void)
Function to enable systicks framework.
Definition: systick_utils.c:70
int16_t accel[3]
Definition: mma9553_drv.h:82
const registerreadlist_t cMma9553ReadRawOutput[]
void MMA9553_SPI_SetIdleTask(mma9553_spi_sensorhandle_t *pSensorHandle, registeridlefunction_t idleTask, void *userParam)
: The interface function to set the SPI Idle Task.
Definition: mma9553_drv.c:248
typedef int32_t(DATA_FORMAT_Append_t))(void *pData
The interface function to append the data on the formated stream.
The mma9553_drv.h file describes the MMA9553L driver interface and structures.
int main(void)
Main function.
#define SAMPLING_RATE_ms
ARM Systick Utilities.
#define SPI_S_BAUDRATE
Transfer baudrate - 500k.
Definition: frdm_k64f.h:114
fxls8962_acceldataUser_t rawData
#define SPI_S_SIGNAL_EVENT
Definition: frdm_k64f.h:116
#define mma9553_en_callback
This structure defines the Read command List.
Definition: sensor_drv.h:104
#define SMC
Definition: lpc54114.h:144
int32_t MMA9553_SPI_CommandResponse(mma9553_spi_sensorhandle_t *pSensorHandle, const registercommandlist_t *pCommandList, const registerreadlist_t *pResponseList, uint8_t *pBuffer)
The interface function to read the sensor data.
Definition: mma9553_drv.c:321
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 Block command List.
Definition: sensor_drv.h:113
void BOARD_InitPins(void)
Configures pin routing and optionally pin electrical features.
Definition: pin_mux.c:73
#define RESET_GPIO
#define SPI_S_DRIVER
Definition: frdm_k64f.h:113