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