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