ISSDK  1.7
IoT Sensing Software Development Kit
fxlc95000_accel_i2c.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 fxlc95000_accel_i2c.c
37  * @brief The fxlc95000_accel_i2c.c file implements the ISSDK FXLC95000 sensor driver
38  * example demonstration as for I2C 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_I2C.h"
50 
51 /* ISSDK Includes */
52 #include "issdk_hal.h"
53 #include "fxlc95000_drv.h"
54 #include "systick_utils.h"
55 
56 /*******************************************************************************
57  * Macros
58  ******************************************************************************/
59 #define SAMPLING_RATE_us (100000) /* Timeout for the ODR Timer. */
60 #define FXLC95000_SAMPLE_SIZE (10) /* 4-Byte timestamp and 2-Byte X,Y,Z Data each. */
61 #define fxlc95000_odr_callback LPTMR0_IRQHandler /* Timer timeout Callback. */
62 
63 /*******************************************************************************
64  * Constants
65  ******************************************************************************/
66 /*! Create commands for setting FXLC95000L desired configuration. */
67 const uint8_t cFxlc95000_SetODR_Cmd[] = {FXLC95000_SET_ODR_CMD_HDR, /* ODR equal to Sampling Rate. */
69 const uint8_t cFxlc95000_SetResolution_Cmd[] = {FXLC95000_SET_RESOLUTION_CMD_HDR, /* Resolution 14-bits. */
71 const uint8_t cFxlc95000_SetRange_Cmd[] = {FXLC95000_SET_RANGE_CMD_HDR, /* FS Range 2G. */
73 
74 /*! Prepare the register write list to initialize FXLC95000L with desired MBox Settings. */
76  {QuickReadInterruptDisable, 0, sizeof(QuickReadInterruptDisable)}, /* Disable QR INT. */
77  {ConfigureMBoxCmd, 0, sizeof(ConfigureMBoxCmd)}, /* Configure MBox 16 to 25 with 10 byte Sample. */
78  __END_WRITE_CMD__ /* Ref. Table 3-7 of ISF1P195K_SW_REFERENCE_RM. */
79 };
80 
81 /*! Prepare the register write list to configure FXLC95000L with desired Sampling Settings. */
83  {StopDataCmd, 0, sizeof(StopDataCmd)}, /* Stop Data before (re)configuration. */
84  {cFxlc95000_SetODR_Cmd, 0, sizeof(cFxlc95000_SetODR_Cmd)}, /* Set Sensor Sampling Rate. */
85  {cFxlc95000_SetRange_Cmd, 0, sizeof(cFxlc95000_SetRange_Cmd)}, /* Set FS Range. */
86  {cFxlc95000_SetResolution_Cmd, 0, sizeof(cFxlc95000_SetResolution_Cmd)}, /* Set Resolution */
87  {StartDataCmd, 0, sizeof(StartDataCmd)}, /* Start Data after (re)configuration. */
89 
90 /*! Prepare the register read list to read the Timestamp and Accel data from FXLC95000. */
93 
94 /*******************************************************************************
95  * Globals
96  ******************************************************************************/
97 volatile bool gFxlc95000DataRead;
98 
99 /*******************************************************************************
100  * Code
101  ******************************************************************************/
102 /* LPTMR based ODR Callback function. */
104 {
105  LPTMR_ClearStatusFlags(LPTMR0, kLPTMR_TimerCompareFlag);
106  gFxlc95000DataRead = true;
107 }
108 
109 /*!
110  * @brief Main function
111  */
112 int main(void)
113 {
114  int32_t status;
115  lptmr_config_t lptmrConfig;
117  uint8_t data[FXLC95000_SAMPLE_SIZE];
118 
119  ARM_DRIVER_I2C *I2Cdrv = &I2C_S_DRIVER; // Now using the shield.h value!!!
121 
122  /*! Initialize the MCU hardware */
123  BOARD_InitPins();
127 
128  /* Initialize ODR Timer. */
129  LPTMR_GetDefaultConfig(&lptmrConfig);
130  LPTMR_Init(LPTMR0, &lptmrConfig);
131  LPTMR_EnableInterrupts(LPTMR0, kLPTMR_TimerInterruptEnable);
132  LPTMR_SetTimerPeriod(LPTMR0, USEC_TO_COUNT(SAMPLING_RATE_us, CLOCK_GetFreq(kCLOCK_LpoClk)));
133  EnableIRQ(LPTMR0_IRQn);
134 
135  PRINTF("\r\n ISSDK FXLC95000 sensor driver example for Normal Mode. \r\n");
136 
137  /*! Initialize the I2C driver. */
138  status = I2Cdrv->Initialize(I2C_S_SIGNAL_EVENT);
139  if (ARM_DRIVER_OK != status)
140  {
141  PRINTF("\r\n I2C Initialization Failed\r\n");
142  return -1;
143  }
144 
145  /*! Set the I2C Power mode. */
146  status = I2Cdrv->PowerControl(ARM_POWER_FULL);
147  if (ARM_DRIVER_OK != status)
148  {
149  PRINTF("\r\n I2C Power Mode setting Failed\r\n");
150  return -1;
151  }
152 
153  /*! Set the I2C bus speed. */
154  status = I2Cdrv->Control(ARM_I2C_BUS_SPEED, ARM_I2C_BUS_SPEED_FAST);
155  if (ARM_DRIVER_OK != status)
156  {
157  PRINTF("\r\n I2C Control Mode setting Failed\r\n");
158  return -1;
159  }
160 
161  /*! Initialize the FXLC95000 sensor driver. */
164  if (SENSOR_ERROR_NONE != status)
165  {
166  PRINTF("\r\n Sensor Initialization Failed\r\n");
167  return -1;
168  }
169  PRINTF("\r\n Successfully Initiliazed Sensor\r\n");
170 
171  /*! Set the task to be executed while waiting for I2C transactions to complete. */
173 
174  gFxlc95000DataRead = false; /* Do not read data, data will be read after ODR Timer expires. */
175 
176  /*! Configure the FXLC95000 with MBox settings. */
177  status = FXLC95000_I2C_CommandResponse(&fxlc95000Driver, cFxlc95000ConfigMBox, NULL, NULL);
178  if (SENSOR_ERROR_NONE != status)
179  {
180  PRINTF("\r\n FXLC95000 MBox Configuration Failed, Err = %d \r\n", status);
181  return -1;
182  }
183  /*! Configure the FXLC95000 with Sampling settings. */
184  status = FXLC95000_I2C_CommandResponse(&fxlc95000Driver, cFxlc95000ConfigSensor, NULL, NULL);
185  if (SENSOR_ERROR_NONE != status)
186  {
187  PRINTF("\r\n FXLC95000 Sensor Configuration Failed, Err = %d \r\n", status);
188  return -1;
189  }
190  PRINTF("\r\n Successfully Applied FXLC95000 Sensor Configuration\r\n");
191 
192  LPTMR_StartTimer(LPTMR0);
193  for (;;) /* Forever loop */
194  {
195  if (gFxlc95000DataRead == false)
196  {
197  __NOP();
198  continue;
199  }
200  else
201  {
202  gFxlc95000DataRead = false;
203  }
204 
205  /*! Read the raw sensor data from the FXLC95000. */
206  status = FXLC95000_I2C_CommandResponse(&fxlc95000Driver, NULL, cFxlc95000ReadSample, data);
207  if (ARM_DRIVER_OK != status)
208  {
209  PRINTF("\r\n Read Failed. \r\n");
210  return -1;
211  }
212 
213  /*! Convert the raw bytes to sensor data with correct endianness. */
214  rawData.timestamp = ((uint32_t)data[3] << 24) | ((uint32_t)data[2] << 16) | ((uint16_t)data[1] << 8) | data[0];
215  rawData.accel[0] = ((int16_t)data[5] << 8) | data[4];
216  rawData.accel[1] = ((int16_t)data[7] << 8) | data[6];
217  rawData.accel[2] = ((int16_t)data[9] << 8) | data[8];
218 
219  PRINTF("\r\n Timestamp = 0x%X \r\n Accel X = %d Y = %d Z = %d \r\n", rawData.timestamp, rawData.accel[0],
220  rawData.accel[1], rawData.accel[2]);
221  ASK_USER_TO_RESUME(100); /* Ask for user input after processing 100 samples. */
222  }
223 }
#define FXLC95000_SET_RESOLUTION_CMD_HDR
The FXLC95000 Set Resolution Command Header Bytes.
Definition: fxlc95000.h:46
This structure defines the fxlc95000 pedometer data buffer.
Definition: fxlc95000_drv.h:79
const registercommandlist_t cFxlc95000ConfigSensor[]
#define FXLC95000_I2C_ADDR
#define FXLC95000_ACCEL_RANGE_2G
The FXLC95000 FS Range 2G.
Definition: fxlc95000.h:55
#define BOARD_BootClockRUN
Definition: clock_config.h:45
int main(void)
Main function.
uint8_t data[FXLS8962_DATA_SIZE]
int32_t status
void BOARD_InitDebugConsole(void)
Definition: board.c:41
const registerreadlist_t cFxlc95000ReadSample[]
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
#define FXLC95000_SET_RANGE_CMD_HDR
The FXLC95000 Set Range Command Header Bytes.
Definition: fxlc95000.h:49
#define FXLC95000_SAMPLE_SIZE
The fxlc95000_drv.h file describes the FXLC95000L driver interface and structures.
#define ASK_USER_TO_RESUME(x)
Definition: frdm_k64f.h:132
#define FXLC95000_SAMPLE_OFFSET
Time stamp and XYZ Data Register Offset.
Definition: fxlc95000.h:16
#define __END_WRITE_CMD__
Definition: sensor_drv.h:83
const uint8_t cFxlc95000_SetODR_Cmd[]
void FXLC95000_I2C_SetIdleTask(fxlc95000_i2c_sensorhandle_t *pSensorHandle, registeridlefunction_t idleTask, void *userParam)
: The interface function to set the I2C Idle Task.
void BOARD_SystickEnable(void)
Function to enable systicks framework.
Definition: systick_utils.c:70
int32_t FXLC95000_I2C_CommandResponse(fxlc95000_i2c_sensorhandle_t *pSensorHandle, const registercommandlist_t *pCommandList, const registerreadlist_t *pResponseList, uint8_t *pBuffer)
The interface function to read the sensor data.
#define I2C_S_DEVICE_INDEX
Definition: issdk_hal.h:61
#define FXLC95000_SET_ODR_CMD_HDR
The FXLC95000 Set Report Rate Command Header Bytes.
Definition: fxlc95000.h:43
fxlc95000_i2c_sensorhandle_t fxlc95000Driver
#define I2C_S_DRIVER
Definition: issdk_hal.h:59
typedef int32_t(DATA_FORMAT_Append_t))(void *pData
The interface function to append the data on the formated stream.
#define SAMPLING_RATE_us
int32_t FXLC95000_I2C_Initialize(fxlc95000_i2c_sensorhandle_t *pSensorHandle, ARM_DRIVER_I2C *pBus, uint8_t index, uint16_t sAddress, uint16_t buildId)
The interface function to initialize the sensor.
ARM Systick Utilities.
fxls8962_acceldataUser_t rawData
const registercommandlist_t cFxlc95000ConfigMBox[]
#define FXLC95000_BUILD_ID
The FXLC95000 BCD encoded ISF1.1_95k_Build_ID.
Definition: fxlc95000.h:25
This structure defines the Read command List.
Definition: sensor_drv.h:104
#define SMC
Definition: lpc54114.h:144
#define fxlc95000_odr_callback
volatile bool gFxlc95000DataRead
This defines the sensor specific information for I2C.
Definition: fxlc95000_drv.h:70
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 FXLC95000_SST_ODR_PAYLOAD(x)
The FXLC95000 Set Report Rate Payload Bytes.
Definition: fxlc95000.h:52
const uint8_t cFxlc95000_SetResolution_Cmd[]
#define I2C_S_SIGNAL_EVENT
Definition: issdk_hal.h:60
const uint8_t cFxlc95000_SetRange_Cmd[]
#define FXLC95000_ACCEL_RESOLUTION_14_BIT
The FXLC95000 Resoultion 14-Bit.
Definition: fxlc95000.h:70