ISSDK  1.8
IoT Sensing Software Development Kit
mma9553_pedometer_spi.c
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2016, Freescale Semiconductor, Inc.
3  * Copyright 2016-2017 NXP
4  * All rights reserved.
5  *
6  * SPDX-License-Identifier: BSD-3-Clause
7  */
8 
9 /**
10  * @file mma9553_pedometer_spi.c
11  * @brief The mma9553_pedometer_spi.c file implements the ISSDK MMA9553L sensor driver
12  * example demonstration as a Pedometer in SPI Mode.
13  */
14 
15 /* SDK Includes */
16 #include "pin_mux.h"
17 #include "clock_config.h"
18 #include "board.h"
19 #include "fsl_lptmr.h"
20 #include "fsl_debug_console.h"
21 
22 /* CMSIS Includes */
23 #include "Driver_SPI.h"
24 
25 /* ISSDK Includes */
26 #include "issdk_hal.h"
27 #include "gpio_driver.h"
28 #include "mma9553_drv.h"
29 #include "systick_utils.h"
30 
31 /*******************************************************************************
32  * Macros
33  ******************************************************************************/
34 #define SAMPLING_RATE_ms (100) /* Timeout for the ODR Timer. */
35 #define MMA9553_ACCEL_DATA_SIZE (6) /* 2 byte X,Y,Z Axis Data each. */
36 #define mma9553_en_callback LPTMR0_IRQHandler /* Timer timeout Callback. */
37 #define MMA9553_SSB_IO3 D10 /* The SSB_IO3 pin of MMA9553 on the FRDM-STBC-SA955x Board. */
38 #define RESET_GPIO A3 /* The RESET_GPIO pin of MMA9553 on the FRDM-STBC-SA955x Board. */
39 
40 /*******************************************************************************
41  * Constants
42  ******************************************************************************/
43 /*! Prepare the register write list to configure MMA9553L in 30Hz Mode. */
45  {SetFSRange_2g, 0, sizeof(SetFSRange_2g)}, /* Set FS Range 2G */
46  {SetSampleRate_30Hz, 0, sizeof(SetSampleRate_30Hz)}, /* Set Sensor Sampling Rate 30Hz */
47  {SetAFEPriority_for30Hz, 0, sizeof(SetAFEPriority_for30Hz)}, /* Set AFE Priority for 30Hz Sampling Rate */
48  {SetMBoxPriority_for30Hz, 0, sizeof(SetMBoxPriority_for30Hz)}, /* Set MBox Priority for 30Hz Sampling Rate */
50 
51 /*! Prepare the register read list to read the raw Accel data from MMA9553. */
54 
55 /*! Prepare the command list to read the Pedometer data from MMA9553. */
58 
59 /*! Prepare the read list to read the Pedometer data from MMA9553. */
62 
63 /*******************************************************************************
64  * Globals
65  ******************************************************************************/
66 volatile bool gMma9553DataReady;
67 
68 /*******************************************************************************
69  * Functions
70  ******************************************************************************/
71 /* LPTMR based ODR Callback function. */
73 {
74  LPTMR_ClearStatusFlags(LPTMR0, kLPTMR_TimerCompareFlag);
75  gMma9553DataReady = true;
76 }
77 
78 /*!
79  * @brief Main function
80  */
81 int main(void)
82 {
84  lptmr_config_t lptmrConfig;
86  mma9553_pedometerdata_t pedometerData;
87  mma9553_spi_sensorhandle_t mma9553Driver;
88 
89  ARM_DRIVER_SPI *pSPIdriver = &SPI_S_DRIVER;
90 
91  /*! Initialize the MCU hardware */
96 
97  /* Initialize ODR Timer. */
98  LPTMR_GetDefaultConfig(&lptmrConfig);
99  LPTMR_Init(LPTMR0, &lptmrConfig);
100  LPTMR_EnableInterrupts(LPTMR0, kLPTMR_TimerInterruptEnable);
101  LPTMR_SetTimerPeriod(LPTMR0, MSEC_TO_COUNT(SAMPLING_RATE_ms, CLOCK_GetFreq(kCLOCK_LpoClk)));
102  EnableIRQ(LPTMR0_IRQn);
103 
104  PRINTF("\r\n ISSDK MMA9553 sensor driver example for Pedometer Mode. \r\n");
105 
106  /*! Initialize the SPI driver. */
107  status = pSPIdriver->Initialize(SPI_S_SIGNAL_EVENT);
108  if (ARM_DRIVER_OK != status)
109  {
110  PRINTF("\r\n SPI Initialization Failed\r\n");
111  return -1;
112  }
113 
114  /*! Set the SPI Power mode. */
115  status = pSPIdriver->PowerControl(ARM_POWER_FULL);
116  if (ARM_DRIVER_OK != status)
117  {
118  PRINTF("\r\n SPI Power Mode setting Failed\r\n");
119  return -1;
120  }
121 
122  /*! Set the SPI Slave speed. */
123  status = pSPIdriver->Control(ARM_SPI_MODE_MASTER | ARM_SPI_CPOL0_CPHA0, SPI_S_BAUDRATE);
124  if (ARM_DRIVER_OK != status)
125  {
126  PRINTF("\r\n SPI Control Mode setting Failed\r\n");
127  return -1;
128  }
129 
130  /*! Initialize the MMA9553 sensor driver. */
132  if (SENSOR_ERROR_NONE != status)
133  {
134  PRINTF("\r\n Sensor Initialization Failed\r\n");
135  return -1;
136  }
137  PRINTF("\r\n Successfully Initialized Sensor. \r\n");
138 
139  /*! Set the task to be executed while waiting for I2C transactions to complete. */
141 
142  gMma9553DataReady = false; /* Do not read data, data will be read after ODR Timer expires. */
143 
144  /*! Configure the MMA9553 sensor driver with 30Hz Mode settings. */
145  status = MMA9553_SPI_Configure(&mma9553Driver, cMma9553Config30Hz);
146  if (SENSOR_ERROR_NONE != status)
147  {
148  PRINTF("\r\n MMA9553 Sensor Configuration Failed, Err = %d \r\n", status);
149  return -1;
150  }
151 
152  LPTMR_StartTimer(LPTMR0);
153  PRINTF("\r\n Successfully Applied MMA9553 Sensor Configuration\r\n");
154 
155  for (;;) /* Forever loop */
156  {
157  if (gMma9553DataReady == false)
158  {
159  continue;
160  }
161  else
162  {
163  gMma9553DataReady = false;
164  }
165 
166  /*! Read the Pedometer data from the MMA9553. */
167  status = MMA9553_SPI_CommandResponse(&mma9553Driver, cMma9553ReadPedometerCommand, cMma9553ReadPedometerOutput,
168  (uint8_t *)&pedometerData);
169  if (ARM_DRIVER_OK != status)
170  {
171  PRINTF("\r\n Read Failed. \r\n");
172  return -1;
173  }
174 
175  /* Swap bytes for 2 byte fields for Little Endian to Big Endian conversion. */
176  pedometerData.statusRegister = (pedometerData.statusRegister >> 8) | (pedometerData.statusRegister << 8);
177  pedometerData.stepCount = (pedometerData.stepCount >> 8) | (pedometerData.stepCount << 8);
178  pedometerData.distance = (pedometerData.distance >> 8) | (pedometerData.distance << 8);
179  pedometerData.speed = (pedometerData.speed >> 8) | (pedometerData.speed << 8);
180  pedometerData.calories = (pedometerData.calories >> 8) | (pedometerData.calories << 8);
181  pedometerData.sleepCount = (pedometerData.sleepCount >> 8) | (pedometerData.sleepCount << 8);
182 
183  PRINTF("\r\n Steps = %d Distance = %dm Speed = %dm/h Calories = %d \r\n", pedometerData.stepCount,
184  pedometerData.distance, pedometerData.speed, pedometerData.calories);
185 
186  /*! Read the raw sensor data from the MMA9553. */
187  status = MMA9553_SPI_CommandResponse(&mma9553Driver, NULL, cMma9553ReadRawOutput, (uint8_t *)&rawData.accel);
188  if (ARM_DRIVER_OK != status)
189  {
190  PRINTF("\r\n Read Failed. \r\n");
191  return -1;
192  }
193 
194  PRINTF("\r\n Accel X = %d Y = %d Z = %d \r\n", rawData.accel[0], rawData.accel[1], rawData.accel[2]);
195  ASK_USER_TO_RESUME(100); /* Ask for user input after processing 100 samples. */
196  }
197 }
#define ASK_USER_TO_RESUME(x)
Definition: frdm_k64f.h:106
int32_t status
const uint8_t SetSampleRate_30Hz[5]
Definition: mma9553_drv.c:72
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:230
#define RESET_GPIO
const registerreadlist_t cMma9553ReadRawOutput[]
status_t SMC_SetPowerModeVlpr(void *arg)
Configures the system to VLPR power mode. API name used from Kinetis family to maintain compatibility...
Definition: lpc54114.c:169
const uint8_t ReadPedometerData[4]
Command to Read Pedometer Data.
Definition: mma9553_drv.c:98
This structure defines the Size of response for pedometer data read command.
Definition: mma9553_drv.h:60
This structure defines the Block command List.
Definition: sensor_drv.h:87
This defines the sensor specific information for SPI.
Definition: mma9553_drv.h:33
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:157
#define MMA9553_SSB_IO3
int main(void)
Main function.
#define SMC
Definition: lpc54114.h:118
int16_t accel[3]
Definition: mma9553_drv.h:56
#define MMA9553_ACCEL_DATA_SIZE
typedef int32_t(DATA_FORMAT_Append_t))(void *pData
The interface function to append the data on the formated stream.
#define SPI_S_BAUDRATE
Transfer baudrate - 500k.
Definition: frdm_k64f.h:88
The mma9553_drv.h file describes the MMA9553L driver interface and structures.
const registercommandlist_t cMma9553Config30Hz[]
This structure defines the mma9553 pedometer data buffer.
Definition: mma9553_drv.h:53
const registerreadlist_t cMma9553ReadPedometerOutput[]
const registercommandlist_t cMma9553ReadPedometerCommand[]
#define SPI_S_DEVICE_INDEX
Definition: frdm_k64f.h:89
const uint8_t SetFSRange_2g[5]
Full-Scale Range Selections.
Definition: mma9553_drv.c:63
#define MMA9553_XYZ_DATA_OFFSET
XYZ Data Register Offset.
Definition: mma9553.h:23
#define BOARD_BootClockRUN
Definition: clock_config.h:19
const uint8_t SetAFEPriority_for30Hz[5]
Definition: mma9553_drv.c:82
void(* registeridlefunction_t)(void *userParam)
This is the register idle function type.
Definition: sensor_drv.h:97
#define __END_READ_DATA__
Definition: sensor_drv.h:51
#define __END_WRITE_CMD__
Definition: sensor_drv.h:57
volatile bool gMma9553DataReady
fxos8700_accelmagdata_t rawData
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:222
uint16_t readFrom
Definition: sensor_drv.h:80
const uint8_t SetMBoxPriority_for30Hz[5]
Definition: mma9553_drv.c:92
void BOARD_SystickEnable(void)
Function to enable systicks framework.
Definition: systick_utils.c:35
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:295
This structure defines the Read command List.
Definition: sensor_drv.h:78
ARM Systick Utilities.
#define SPI_S_DRIVER
Definition: frdm_k64f.h:87
#define SAMPLING_RATE_ms
#define mma9553_en_callback
#define SPI_S_SIGNAL_EVENT
Definition: frdm_k64f.h:90
void BOARD_InitDebugConsole(void)
Definition: board.c:15
void BOARD_InitPins(void)
Configures pin routing and optionally pin electrical features.
Definition: pin_mux.c:47