ISSDK  1.8
IoT Sensing Software Development Kit
fxls8974cf_spi.c
Go to the documentation of this file.
1 /*
2  * Copyright 2022 NXP
3  * All rights reserved.
4  *
5  * SPDX-License-Identifier: BSD-3-Clause
6  */
7 
8 /**
9  * @file fxls8974cf_spi.c
10  * @brief The fxls8974cf_spi.c file implements the ISSDK FXLS8974CF SPI sensor driver
11  * example demonstration for SPI Mode with polling.
12  */
13 
14 //-----------------------------------------------------------------------
15 // SDK Includes
16 //-----------------------------------------------------------------------
17 #include "pin_mux.h"
18 #include "clock_config.h"
19 #include "board.h"
20 #include "fsl_debug_console.h"
21 
22 //-----------------------------------------------------------------------
23 // ISSDK Includes
24 //-----------------------------------------------------------------------
25 #include "issdk_hal.h"
26 #include "gpio_driver.h"
27 #include "fxls8974_drv.h"
28 #include "systick_utils.h"
29 
30 //-----------------------------------------------------------------------
31 // CMSIS Includes
32 //-----------------------------------------------------------------------
33 #include "Driver_SPI.h"
34 
35 //-----------------------------------------------------------------------
36 // Macros
37 //-----------------------------------------------------------------------
38 #define FXLS8974_DATA_SIZE (6)
39 
40 //-----------------------------------------------------------------------
41 // Constants
42 //-----------------------------------------------------------------------
43 /*! @brief Register settings for Normal (non buffered) mode. */
45  /* Set Full-scale range as 2G. */
47  /* Set Wake Mode ODR Rate as 6.25Hz. */
50 
51 /*! @brief Address of DATA Ready Status Register. */
53 
54 /*! @brief Address of Raw Accel Data in Normal Mode. */
57 
58 //-----------------------------------------------------------------------
59 // Functions
60 //-----------------------------------------------------------------------
61 /*! -----------------------------------------------------------------------
62  * @brief This is the The main function implementation.
63  * @details This function invokes board initializes routines, then then brings up the sensor and
64  * finally enters an endless loop to continuously read available samples.
65  * @param[in] void This is no input parameter.
66  * @return void There is no return value.
67  * @constraints None
68  * @reeentrant No
69  * -----------------------------------------------------------------------*/
70 int main(void)
71 {
73  uint8_t whoami;
74  uint8_t gfxls8974DataReady;
75  uint8_t data[FXLS8974_DATA_SIZE];
77 
78  ARM_DRIVER_SPI *pSPIdriver = &SPI_S_DRIVER;
80 
81  /*! Initialize the MCU hardware. */
86 
87  PRINTF("\r\n ISSDK FXLS8974 sensor driver example demonstration for SPI with Poll Mode.\r\n");
88 
89  /*! Initialize the SPI driver. */
90  status = pSPIdriver->Initialize(SPI_S_SIGNAL_EVENT);
91  if (ARM_DRIVER_OK != status)
92  {
93  PRINTF("\r\n SPI Initialization Failed\r\n");
94  return -1;
95  }
96 
97  /*! Set the SPI Power mode. */
98  status = pSPIdriver->PowerControl(ARM_POWER_FULL);
99  if (ARM_DRIVER_OK != status)
100  {
101  PRINTF("\r\n SPI Power Mode setting Failed\r\n");
102  return -1;
103  }
104 
105  /*! Set the SPI Slave speed. */
106  status = pSPIdriver->Control(ARM_SPI_MODE_MASTER | ARM_SPI_CPOL0_CPHA0, SPI_S_BAUDRATE);
107  if (ARM_DRIVER_OK != status)
108  {
109  PRINTF("\r\n SPI Control Mode setting Failed\r\n");
110  return -1;
111  }
112 
113  /*! Initialize the fxls8974 sensor driver. */
115  &whoami);
116  if (SENSOR_ERROR_NONE != status)
117  {
118  PRINTF("\r\n FXLS8974 Sensor Initialization Failed\r\n");
119  return -1;
120  }
121  if ((FXLS8964_WHOAMI_VALUE == whoami) || (FXLS8967_WHOAMI_VALUE == whoami))
122  {
123  PRINTF("\r\n Successfully Initialized Gemini with WHO_AM_I = 0x%X\r\n", whoami);
124  }
125  else if ((FXLS8974_WHOAMI_VALUE == whoami) || (FXLS8968_WHOAMI_VALUE == whoami))
126  {
127  PRINTF("\r\n Successfully Initialized Timandra with WHO_AM_I = 0x%X\r\n", whoami);
128  }
129  else if (FXLS8962_WHOAMI_VALUE == whoami)
130  {
131  PRINTF("\r\n Successfully Initialized Newstein with WHO_AM_I = 0x%X\r\n", whoami);
132  }
133  else
134  {
135  PRINTF("\r\n Bad WHO_AM_I = 0x%X\r\n", whoami);
136  }
137 
138  /*! Set the task to be executed while waiting for SPI transactions to complete. */
140 
141  /*! Configure the FXLS8974 sensor driver. */
142  status = FXLS8974_SPI_Configure(&fxls8974Driver, cfxls8974ConfigNormal);
143  if (SENSOR_ERROR_NONE != status)
144  {
145  PRINTF("\r\n FXLS8974 Sensor Configuration Failed, Err = %d\r\n", status);
146  return -1;
147  }
148  PRINTF("\r\n Successfully Applied FXLS8974 Sensor Configuration\r\n");
149 
150  for (;;) /* Forever loop */
151  {
152  /*! Wait for data ready from the FXLS8974. */
153  status = FXLS8974_SPI_ReadData(&fxls8974Driver, cfxls8974DRDYEvent, &gfxls8974DataReady);
154  if (0 == (gfxls8974DataReady & FXLS8974_INT_STATUS_SRC_DRDY_MASK))
155  { /* Loop, if new sample is not available. */
156  continue;
157  }
158 
159  /*! Read the raw sensor data from the FXLS8974. */
160  status = FXLS8974_SPI_ReadData(&fxls8974Driver, cfxls8974OutputNormal, data);
161  if (ARM_DRIVER_OK != status)
162  {
163  PRINTF("\r\nRead Failed.\r\n");
164  return -1;
165  }
166 
167  /*! Convert the raw sensor data for display to the debug port. */
168  rawData.accel[0] = ((int16_t)data[1] << 8) | data[0];
169  rawData.accel[1] = ((int16_t)data[3] << 8) | data[2];
170  rawData.accel[2] = ((int16_t)data[5] << 8) | data[4];
171 
172  /* NOTE: PRINTF is relatively expensive in terms of CPU time, specially when used with-in execution loop. */
173  PRINTF("\r\n X=%5d Y=%5d Z=%5d\r\n", rawData.accel[0], rawData.accel[1], rawData.accel[2]);
174  }
175 }
const registerreadlist_t cfxls8974OutputNormal[]
Address of Raw Accel Data in Normal Mode.
This structure defines the Write command List.
Definition: sensor_drv.h:68
#define FXLS8974_SENS_CONFIG1_FSR_MASK
Definition: fxls8974.h:423
#define FXLS8974_INT_STATUS_SRC_DRDY_MASK
Definition: fxls8974.h:147
void FXLS8974_SPI_SetIdleTask(fxls8974_spi_sensorhandle_t *pSensorHandle, registeridlefunction_t idleTask, void *userParam)
: The interface function to set the SPI Idle Task.
Definition: fxls8974_drv.c:122
int32_t status
const registerreadlist_t cfxls8974DRDYEvent[]
Address of DATA Ready Status Register.
int32_t FXLS8974_SPI_Initialize(fxls8974_spi_sensorhandle_t *pSensorHandle, ARM_DRIVER_SPI *pBus, uint8_t index, void *pSlaveSelect, uint8_t *whoami)
The interface function to initialize the sensor.
Definition: fxls8974_drv.c:67
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
int32_t FXLS8974_SPI_ReadData(fxls8974_spi_sensorhandle_t *pSensorHandle, const registerreadlist_t *pReadList, uint8_t *pBuffer)
The interface function to read the sensor data.
Definition: fxls8974_drv.c:175
#define SMC
Definition: lpc54114.h:118
int main(void)
This is the The main function implementation.
#define __END_WRITE_DATA__
Definition: sensor_drv.h:45
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
#define FXLS8967_WHOAMI_VALUE
Definition: fxls8962.h:89
#define SPI_S_DEVICE_INDEX
Definition: frdm_k64f.h:89
This defines the sensor specific information for SPI.
Definition: fxls8974_drv.h:32
int32_t FXLS8974_SPI_Configure(fxls8974_spi_sensorhandle_t *pSensorHandle, const registerwritelist_t *pRegWriteList)
The interface function to configure he sensor.
Definition: fxls8974_drv.c:130
#define BOARD_BootClockRUN
Definition: clock_config.h:19
void(* registeridlefunction_t)(void *userParam)
This is the register idle function type.
Definition: sensor_drv.h:97
This structure defines the fxls8974 raw data buffer.
Definition: fxls8974_drv.h:52
uint8_t data[FXLS8962_DATA_SIZE]
#define __END_READ_DATA__
Definition: sensor_drv.h:51
#define FXLS8974_SENS_CONFIG3_WAKE_ODR_MASK
Definition: fxls8974.h:551
fxls8974_i2c_sensorhandle_t fxls8974Driver
fxos8700_accelmagdata_t rawData
uint16_t readFrom
Definition: sensor_drv.h:80
void BOARD_SystickEnable(void)
Function to enable systicks framework.
Definition: systick_utils.c:35
#define FXLS8974_SENS_CONFIG1_FSR_2G
Definition: fxls8974.h:453
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 FXLS8974_CS
#define FXLS8974_WHOAMI_VALUE
Definition: fxls8962.h:90
The fxls8974_drv.h file describes the FXLS8974CF driver interface and structures. ...
#define FXLS8968_WHOAMI_VALUE
Definition: fxls896x.h:89
#define SPI_S_SIGNAL_EVENT
Definition: frdm_k64f.h:90
const registerwritelist_t cfxls8974ConfigNormal[]
Register settings for Normal (non buffered) mode.
#define FXLS8974_DATA_SIZE
void BOARD_InitDebugConsole(void)
Definition: board.c:15
#define FXLS8962_WHOAMI_VALUE
Definition: fxls8962.h:87
#define FXLS8974_SENS_CONFIG3_WAKE_ODR_6_25HZ
Definition: fxls8974.h:566
void BOARD_InitPins(void)
Configures pin routing and optionally pin electrical features.
Definition: pin_mux.c:47
#define FXLS8964_WHOAMI_VALUE
Definition: fxls8962.h:88