ISSDK  1.8
IoT Sensing Software Development Kit
fxas21002_poll_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 /*! @file fxas21002_poll_spi.c
10  * @brief The fxas21002_poll_spi.c file implements the ISSDK FXAS21002 SPI sensor driver
11  * example demonstration for SPI Mode with polling.
12  */
13 
14 //-----------------------------------------------------------------------
15 // SDK Includes
16 //-----------------------------------------------------------------------
17 #include "board.h"
18 #include "pin_mux.h"
19 #include "clock_config.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 "fxas21002_drv.h"
28 
29 //-----------------------------------------------------------------------
30 // CMSIS Includes
31 //-----------------------------------------------------------------------
32 #include "Driver_SPI.h"
33 
34 //-----------------------------------------------------------------------
35 // Constants
36 //-----------------------------------------------------------------------
37 /*! Prepare the register write list to configure FXAS21002 in non-FIFO mode. */
39  /*! Configure CTRL_REG1 register to put FXAS21002 to 12.5Hz sampling rate. */
42 
43 /*! Prepare the register read list to read FXAS21002 DataReady status. */
45 
46 /*! Prepare the register read list to read the raw gyro data from the FXAS21002. */
49 
50 //-----------------------------------------------------------------------
51 // Functions
52 //-----------------------------------------------------------------------
53 /*! -----------------------------------------------------------------------
54  * @brief This is the The main function implementation.
55  * @details This function invokes board initializes routines, then then brings up the sensor and
56  * finally enters an endless loop to continuously read available samples.
57  * @param[in] void This is no input parameter.
58  * @return void There is no return value.
59  * @constraints None
60  * @reeentrant No
61  * -----------------------------------------------------------------------*/
62 int main(void)
63 {
65  uint8_t gFxas21002DataReady;
68 
69  ARM_DRIVER_SPI *pSPIdriver = &SPI_S_DRIVER;
70  fxas21002_spi_sensorhandle_t fxas21002Driver;
71 
72  /*! Initialize the MCU hardware. */
76 
77  PRINTF("\r\n ISSDK FXAS21002 sensor driver example demonstration for SPI with Poll Mode.\r\n");
78 
79  /*! Initialize the SPI driver. */
80  status = pSPIdriver->Initialize(SPI_S_SIGNAL_EVENT);
81  if (ARM_DRIVER_OK != status)
82  {
83  PRINTF("\r\n SPI Initialization Failed\r\n");
84  return -1;
85  }
86 
87  /*! Set the SPI Power mode. */
88  status = pSPIdriver->PowerControl(ARM_POWER_FULL);
89  if (ARM_DRIVER_OK != status)
90  {
91  PRINTF("\r\n SPI Power Mode setting Failed\r\n");
92  return -1;
93  }
94 
95  /*! Set the SPI Slave speed. */
96  status = pSPIdriver->Control(ARM_SPI_MODE_MASTER | ARM_SPI_CPOL0_CPHA0, SPI_S_BAUDRATE);
97  if (ARM_DRIVER_OK != status)
98  {
99  PRINTF("\r\n SPI Control Mode setting Failed\r\n");
100  return -1;
101  }
102 
103  /*! Initialize the FXAS21002 sensor driver. */
106  if (SENSOR_ERROR_NONE != status)
107  {
108  PRINTF("\r\n FXAS21002 Sensor Initialization Failed\r\n");
109  return -1;
110  }
111  PRINTF("\r\n Successfully Initiliazed FXAS21002 Sensor\r\n");
112 
113  /*! Set the task to be executed while waiting for SPI transactions to complete. */
115 
116  /*! Configure the FXAS21002 sensor driver. */
117  status = FXAS21002_SPI_Configure(&fxas21002Driver, cFxas21002_Config_Normal);
118  if (SENSOR_ERROR_NONE != status)
119  {
120  PRINTF("\r\n FXAS21002 Sensor Configuration Failed, Err = %d\r\n", status);
121  return -1;
122  }
123  PRINTF("\r\n Successfully Applied FXAS21002 Sensor Configuration\r\n");
124 
125  for (;;) /* Forever loop */
126  {
127  /*! Wait for data ready from the FXAS21002. */
128  status = FXAS21002_SPI_ReadData(&fxas21002Driver, cFxas21002_DataReady, &gFxas21002DataReady);
129  if (0 == (gFxas21002DataReady & FXAS21002_DR_STATUS_ZYXDR_MASK))
130  { /* Loop, if new sample is not available. */
131  continue;
132  }
133 
134  /*! Read the raw sensor data from the FXAS21002. */
135  status = FXAS21002_SPI_ReadData(&fxas21002Driver, cFxas21002_Output_Values, data);
136  if (ARM_DRIVER_OK != status)
137  {
138  PRINTF("\r\nRead Failed.\r\n");
139  return -1;
140  }
141 
142  /*! Convert the raw sensor data for display to the debug port. */
143  rawData.gyro[0] = ((int16_t)data[0] << 8) | data[1];
144  rawData.gyro[1] = ((int16_t)data[2] << 8) | data[3];
145  rawData.gyro[2] = ((int16_t)data[4] << 8) | data[5];
146 
147  /* NOTE: PRINTF is relatively expensive in terms of CPU time, specially when used with-in execution loop. */
148  PRINTF("\r\n Gyro X = %d Y = %d Z = %d \r\n", rawData.gyro[0], rawData.gyro[1], rawData.gyro[2]);
149  }
150 }
int32_t FXAS21002_SPI_Configure(fxas21002_spi_sensorhandle_t *pSensorHandle, const registerwritelist_t *pRegWriteList)
The interface function to configure he sensor.
#define FXAS21002_WHO_AM_I_WHOAMI_PROD_VALUE
Definition: fxas21002.h:402
This structure defines the Write command List.
Definition: sensor_drv.h:68
int32_t status
This defines the sensor specific information for SPI.
Definition: fxas21002_drv.h:33
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
This structure defines the fxas21002 raw data buffer.
Definition: fxas21002_drv.h:53
#define SMC
Definition: lpc54114.h:118
#define FXAS21002_CS
#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 SPI_S_DEVICE_INDEX
Definition: frdm_k64f.h:89
#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
int main(void)
This is the The main function implementation.
uint8_t data[FXLS8962_DATA_SIZE]
#define FXAS21002_DR_STATUS_ZYXDR_MASK
Definition: fxas21002.h:160
#define __END_READ_DATA__
Definition: sensor_drv.h:51
The fxas21002_drv.h file describes the fxas21002 driver interface and structures. ...
fxos8700_accelmagdata_t rawData
uint16_t readFrom
Definition: sensor_drv.h:80
const registerwritelist_t cFxas21002_Config_Normal[]
#define FXAS21002_GYRO_DATA_SIZE
The size of the FXAS21002 gyro data.
Definition: fxas21002_drv.h:61
#define FXAS21002_CTRL_REG1_DR_MASK
Definition: fxas21002.h:684
int32_t FXAS21002_SPI_ReadData(fxas21002_spi_sensorhandle_t *pSensorHandle, const registerreadlist_t *pReadList, uint8_t *pBuffer)
The interface function to read the sensor data.
This structure defines the Read command List.
Definition: sensor_drv.h:78
#define SPI_S_DRIVER
Definition: frdm_k64f.h:87
int32_t FXAS21002_SPI_Initialize(fxas21002_spi_sensorhandle_t *pSensorHandle, ARM_DRIVER_SPI *pBus, uint8_t index, void *pSlaveSelect, uint8_t whoAmi)
The interface function to initialize the sensor.
Definition: fxas21002_drv.c:65
#define SPI_S_SIGNAL_EVENT
Definition: frdm_k64f.h:90
void FXAS21002_SPI_SetIdleTask(fxas21002_spi_sensorhandle_t *pSensorHandle, registeridlefunction_t idleTask, void *userParam)
: The interface function to set the SPI Idle Task.
#define FXAS21002_CTRL_REG1_DR_12_5HZ
Definition: fxas21002.h:710
void BOARD_InitDebugConsole(void)
Definition: board.c:15
const registerreadlist_t cFxas21002_DataReady[]
void BOARD_InitPins(void)
Configures pin routing and optionally pin electrical features.
Definition: pin_mux.c:47
const registerreadlist_t cFxas21002_Output_Values[]