ISSDK  1.8
IoT Sensing Software Development Kit
fxlc95000_accel_spi.c
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2015 - 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_spi.c
11  * @brief The fxlc95000_accel_spi.c file implements the ISSDK FXLC95000 sensor driver
12  * example demonstration as for SPI Mode.
13  */
14 
15 //-----------------------------------------------------------------------
16 // SDK Includes
17 //-----------------------------------------------------------------------
18 #include "pin_mux.h"
19 #include "clock_config.h"
20 #include "board.h"
21 #include "fsl_lptmr.h"
22 #include "fsl_debug_console.h"
23 
24 //-----------------------------------------------------------------------
25 // ISSDK Includes
26 //-----------------------------------------------------------------------
27 #include "issdk_hal.h"
28 #include "gpio_driver.h"
29 #include "fxlc95000_drv.h"
30 #include "systick_utils.h"
31 
32 //-----------------------------------------------------------------------
33 // CMSIS Includes
34 //-----------------------------------------------------------------------
35 #include "Driver_SPI.h"
36 
37 //-----------------------------------------------------------------------
38 // Macros
39 //-----------------------------------------------------------------------
40 #define SAMPLING_RATE_us (100000) /* Timeout for the ODR Timer. */
41 #define FXLC95000_SAMPLE_SIZE (10) /* 4-Byte timestamp and 2-Byte X,Y,Z Data each. */
42 #define fxlc95000_odr_callback LPTMR0_IRQHandler /* Timer timeout Callback. */
43 
44 //-----------------------------------------------------------------------
45 // Constants
46 //-----------------------------------------------------------------------
47 /*! Create commands for setting FXLC95000L desired configuration. */
48 const uint8_t cFxlc95000_SetODR_Cmd[] = {FXLC95000_SET_ODR_CMD_HDR, /* ODR equal to Sampling Rate. */
50 const uint8_t cFxlc95000_SetResolution_Cmd[] = {FXLC95000_SET_RESOLUTION_CMD_HDR, /* Resolution 14-bits. */
52 const uint8_t cFxlc95000_SetRange_Cmd[] = {FXLC95000_SET_RANGE_CMD_HDR, /* FS Range 2G. */
54 
55 /*! Prepare the register write list to initialize FXLC95000L with desired MBox Settings. */
57  {QuickReadInterruptDisable, 0, sizeof(QuickReadInterruptDisable)}, /* Disable QR INT. */
58  {ConfigureMBoxCmd, 0, sizeof(ConfigureMBoxCmd)}, /* Configure MBox 16 to 25 with 10 byte Sample. */
59  __END_WRITE_CMD__ /* Ref. Table 3-7 of ISF1P195K_SW_REFERENCE_RM. */
60 };
61 
62 /*! Prepare the register write list to configure FXLC95000L with desired Sampling Settings. */
64  {StopDataCmd, 0, sizeof(StopDataCmd)}, /* Stop Data before (re)configuration. */
65  {cFxlc95000_SetODR_Cmd, 0, sizeof(cFxlc95000_SetODR_Cmd)}, /* Set Sensor Sampling Rate. */
66  {cFxlc95000_SetRange_Cmd, 0, sizeof(cFxlc95000_SetRange_Cmd)}, /* Set FS Range. */
67  {cFxlc95000_SetResolution_Cmd, 0, sizeof(cFxlc95000_SetResolution_Cmd)}, /* Set Resolution */
68  {StartDataCmd, 0, sizeof(StartDataCmd)}, /* Start Data after (re)configuration. */
70 
71 /*! Prepare the register read list to read the Timestamp and Accel data from FXLC95000. */
74 
75 /*******************************************************************************
76  * Globals
77  ******************************************************************************/
78 volatile bool gFxlc95000DataRead;
79 
80 /*******************************************************************************
81  * Code
82  ******************************************************************************/
83 /* LPTMR based ODR Callback function. */
85 {
86  LPTMR_ClearStatusFlags(LPTMR0, kLPTMR_TimerCompareFlag);
87  gFxlc95000DataRead = true;
88 }
89 
90 /*!
91  * @brief Main function
92  */
93 int main(void)
94 {
96  lptmr_config_t lptmrConfig;
98  uint8_t data[FXLC95000_SAMPLE_SIZE];
99 
100  ARM_DRIVER_SPI *pSPIdriver = &SPI_S_DRIVER;
102 
103  /*! Initialize the MCU hardware. */
104  BOARD_InitPins();
108 
109  /* Initialize ODR Timer. */
110  LPTMR_GetDefaultConfig(&lptmrConfig);
111  LPTMR_Init(LPTMR0, &lptmrConfig);
112  LPTMR_EnableInterrupts(LPTMR0, kLPTMR_TimerInterruptEnable);
113  LPTMR_SetTimerPeriod(LPTMR0, USEC_TO_COUNT(SAMPLING_RATE_us, CLOCK_GetFreq(kCLOCK_LpoClk)));
114  EnableIRQ(LPTMR0_IRQn);
115 
116  PRINTF("\r\n ISSDK FXLC95000 sensor driver example for SPI Mode. \r\n");
117 
118  /*! Initialize the SPI driver. */
119  status = pSPIdriver->Initialize(SPI_S_SIGNAL_EVENT);
120  if (ARM_DRIVER_OK != status)
121  {
122  PRINTF("\r\n SPI Initialization Failed\r\n");
123  return -1;
124  }
125 
126  /*! Set the SPI Power mode. */
127  status = pSPIdriver->PowerControl(ARM_POWER_FULL);
128  if (ARM_DRIVER_OK != status)
129  {
130  PRINTF("\r\n SPI Power Mode setting Failed\r\n");
131  return -1;
132  }
133 
134  /*! Set the SPI Slave speed. */
135  status = pSPIdriver->Control(ARM_SPI_MODE_MASTER | ARM_SPI_CPOL0_CPHA0, SPI_S_BAUDRATE);
136  if (ARM_DRIVER_OK != status)
137  {
138  PRINTF("\r\n SPI Control Mode setting Failed\r\n");
139  return -1;
140  }
141 
142  /*! Initialize the FXLC95000 sensor driver. */
145  if (SENSOR_ERROR_NONE != status)
146  {
147  PRINTF("\r\n Sensor Initialization Failed\r\n");
148  return -1;
149  }
150  PRINTF("\r\n Successfully Initiliazed Sensor\r\n");
151 
152  /*! Set the task to be executed while waiting for I2C transactions to complete. */
154 
155  gFxlc95000DataRead = false; /* Do not read data, data will be read after ODR Timer expires. */
156 
157  /*! Configure the FXLC95000 with MBox settings. */
158  status = FXLC95000_SPI_CommandResponse(&fxlc95000Driver, cFxlc95000ConfigMBox, NULL, NULL);
159  if (SENSOR_ERROR_NONE != status)
160  {
161  PRINTF("\r\n FXLC95000 MBox Configuration Failed, Err = %d \r\n", status);
162  return -1;
163  }
164 
165  /*! Configure the FXLC95000 with Sampling settings. */
166  status = FXLC95000_SPI_CommandResponse(&fxlc95000Driver, cFxlc95000ConfigSensor, NULL, NULL);
167  if (SENSOR_ERROR_NONE != status)
168  {
169  PRINTF("\r\n FXLC95000 Sensor Configuration Failed, Err = %d \r\n", status);
170  return -1;
171  }
172  PRINTF("\r\n Successfully Applied FXLC95000 Sensor Configuration\r\n");
173 
174  LPTMR_StartTimer(LPTMR0);
175  for (;;) /* Forever loop */
176  {
177  if (gFxlc95000DataRead == false)
178  {
179  __NOP();
180  continue;
181  }
182  else
183  {
184  gFxlc95000DataRead = false;
185  }
186 
187  /*! Read the raw sensor data from the FXLC95000. */
188  status = FXLC95000_SPI_CommandResponse(&fxlc95000Driver, NULL, cFxlc95000ReadSample, data);
189  if (ARM_DRIVER_OK != status)
190  {
191  PRINTF("\r\n Read Failed. \r\n");
192  return -1;
193  }
194 
195  /*! Convert the raw bytes to sensor data with correct endianness. */
196  rawData.timestamp = ((uint32_t)data[3] << 24) | ((uint32_t)data[2] << 16) | ((uint16_t)data[1] << 8) | data[0];
197  rawData.accel[0] = ((int16_t)data[5] << 8) | data[4];
198  rawData.accel[1] = ((int16_t)data[7] << 8) | data[6];
199  rawData.accel[2] = ((int16_t)data[9] << 8) | data[8];
200 
201  PRINTF("\r\n Timestamp = 0x%X \r\n Accel X = %d Y = %d Z = %d \r\n", rawData.timestamp, rawData.accel[0],
202  rawData.accel[1], rawData.accel[2]);
203  ASK_USER_TO_RESUME(100); /* Ask for user input after processing 100 samples. */
204  }
205 }
const uint8_t cFxlc95000_SetRange_Cmd[]
void FXLC95000_SPI_SetIdleTask(fxlc95000_spi_sensorhandle_t *pSensorHandle, registeridlefunction_t idleTask, void *userParam)
: The interface function to set the SPI Idle Task.
#define ASK_USER_TO_RESUME(x)
Definition: frdm_k64f.h:106
int32_t status
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:68
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
#define FXLC95000_SET_RESOLUTION_CMD_HDR
The FXLC95000 Set Resolution Command Header Bytes.
Definition: fxlc95000.h:45
#define FXLC95000_RST_GPIO
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
volatile bool gFxlc95000DataRead
#define SMC
Definition: lpc54114.h:118
#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.
#define SPI_S_BAUDRATE
Transfer baudrate - 500k.
Definition: frdm_k64f.h:88
const registercommandlist_t cFxlc95000ConfigMBox[]
The fxlc95000_drv.h file describes the FXLC95000L driver interface and structures.
#define SPI_S_DEVICE_INDEX
Definition: frdm_k64f.h:89
const uint8_t cFxlc95000_SetResolution_Cmd[]
#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
const uint8_t cFxlc95000_SetODR_Cmd[]
uint8_t data[FXLS8962_DATA_SIZE]
#define __END_READ_DATA__
Definition: sensor_drv.h:51
#define FXLC95000_SSB_IO3
#define __END_WRITE_CMD__
Definition: sensor_drv.h:57
#define SAMPLING_RATE_us
#define FXLC95000_SST_ODR_PAYLOAD(x)
The FXLC95000 Set Report Rate Payload Bytes.
Definition: fxlc95000.h:51
fxos8700_accelmagdata_t rawData
#define FXLC95000_SAMPLE_OFFSET
Time stamp and XYZ Data Register Offset.
Definition: fxlc95000.h:15
#define FXLC95000_SAMPLE_SIZE
uint16_t readFrom
Definition: sensor_drv.h:80
#define FXLC95000_PDB_B
const registercommandlist_t cFxlc95000ConfigSensor[]
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
#define fxlc95000_odr_callback
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 SPI_S_SIGNAL_EVENT
Definition: frdm_k64f.h:90
#define FXLC95000_SET_ODR_CMD_HDR
The FXLC95000 Set Report Rate Command Header Bytes.
Definition: fxlc95000.h:42
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
This defines the sensor specific information for SPI.
Definition: fxlc95000_drv.h:33
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.
int main(void)
Main function.
const registerreadlist_t cFxlc95000ReadSample[]
fxlc95000_i2c_sensorhandle_t fxlc95000Driver
#define FXLC95000_ACCEL_RANGE_2G
The FXLC95000 FS Range 2G.
Definition: fxlc95000.h:54