ISSDK  1.7
IoT Sensing Software Development Kit
fxas21002_poll_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 /*! @file fxas21002_poll_spi.c
36  * @brief The fxas21002_poll_spi.c file implements the ISSDK FXAS21002 SPI sensor driver
37  * example demonstration for SPI Mode with polling.
38  */
39 
40 //-----------------------------------------------------------------------
41 // SDK Includes
42 //-----------------------------------------------------------------------
43 #include "board.h"
44 #include "pin_mux.h"
45 #include "clock_config.h"
46 #include "fsl_debug_console.h"
47 
48 //-----------------------------------------------------------------------
49 // ISSDK Includes
50 //-----------------------------------------------------------------------
51 #include "issdk_hal.h"
52 #include "gpio_driver.h"
53 #include "fxas21002_drv.h"
54 
55 //-----------------------------------------------------------------------
56 // CMSIS Includes
57 //-----------------------------------------------------------------------
58 #include "Driver_SPI.h"
59 
60 //-----------------------------------------------------------------------
61 // Constants
62 //-----------------------------------------------------------------------
63 /*! Prepare the register write list to configure FXAS21002 in non-FIFO mode. */
65  /*! Configure CTRL_REG1 register to put FXAS21002 to 12.5Hz sampling rate. */
68 
69 /*! Prepare the register read list to read FXAS21002 DataReady status. */
71 
72 /*! Prepare the register read list to read the raw gyro data from the FXAS21002. */
75 
76 //-----------------------------------------------------------------------
77 // Functions
78 //-----------------------------------------------------------------------
79 /*! -----------------------------------------------------------------------
80  * @brief This is the The main function implementation.
81  * @details This function invokes board initializes routines, then then brings up the sensor and
82  * finally enters an endless loop to continuously read available samples.
83  * @param[in] void This is no input parameter.
84  * @return void There is no return value.
85  * @constraints None
86  * @reeentrant No
87  * -----------------------------------------------------------------------*/
88 int main(void)
89 {
91  uint8_t gFxas21002DataReady;
94 
95  ARM_DRIVER_SPI *pSPIdriver = &SPI_S_DRIVER;
96  fxas21002_spi_sensorhandle_t fxas21002Driver;
97 
98  /*! Initialize the MCU hardware. */
102 
103  PRINTF("\r\n ISSDK FXAS21002 sensor driver example demonstration for SPI with Poll Mode.\r\n");
104 
105  /*! Initialize the SPI driver. */
106  status = pSPIdriver->Initialize(SPI_S_SIGNAL_EVENT);
107  if (ARM_DRIVER_OK != status)
108  {
109  PRINTF("\r\n SPI Initialization Failed\r\n");
110  return -1;
111  }
112 
113  /*! Set the SPI Power mode. */
114  status = pSPIdriver->PowerControl(ARM_POWER_FULL);
115  if (ARM_DRIVER_OK != status)
116  {
117  PRINTF("\r\n SPI Power Mode setting Failed\r\n");
118  return -1;
119  }
120 
121  /*! Set the SPI Slave speed. */
122  status = pSPIdriver->Control(ARM_SPI_MODE_MASTER | ARM_SPI_CPOL0_CPHA0, SPI_S_BAUDRATE);
123  if (ARM_DRIVER_OK != status)
124  {
125  PRINTF("\r\n SPI Control Mode setting Failed\r\n");
126  return -1;
127  }
128 
129  /*! Initialize the FXAS21002 sensor driver. */
132  if (SENSOR_ERROR_NONE != status)
133  {
134  PRINTF("\r\n FXAS21002 Sensor Initialization Failed\r\n");
135  return -1;
136  }
137  PRINTF("\r\n Successfully Initiliazed FXAS21002 Sensor\r\n");
138 
139  /*! Set the task to be executed while waiting for SPI transactions to complete. */
141 
142  /*! Configure the FXAS21002 sensor driver. */
143  status = FXAS21002_SPI_Configure(&fxas21002Driver, cFxas21002_Config_Normal);
144  if (SENSOR_ERROR_NONE != status)
145  {
146  PRINTF("\r\n FXAS21002 Sensor Configuration Failed, Err = %d\r\n", status);
147  return -1;
148  }
149  PRINTF("\r\n Successfully Applied FXAS21002 Sensor Configuration\r\n");
150 
151  for (;;) /* Forever loop */
152  {
153  /*! Wait for data ready from the FXAS21002. */
154  status = FXAS21002_SPI_ReadData(&fxas21002Driver, cFxas21002_DataReady, &gFxas21002DataReady);
155  if (0 == (gFxas21002DataReady & FXAS21002_DR_STATUS_ZYXDR_MASK))
156  { /* Loop, if new sample is not available. */
157  continue;
158  }
159 
160  /*! Read the raw sensor data from the FXAS21002. */
161  status = FXAS21002_SPI_ReadData(&fxas21002Driver, cFxas21002_Output_Values, data);
162  if (ARM_DRIVER_OK != status)
163  {
164  PRINTF("\r\nRead Failed.\r\n");
165  return -1;
166  }
167 
168  /*! Convert the raw sensor data for display to the debug port. */
169  rawData.gyro[0] = ((int16_t)data[0] << 8) | data[1];
170  rawData.gyro[1] = ((int16_t)data[2] << 8) | data[3];
171  rawData.gyro[2] = ((int16_t)data[4] << 8) | data[5];
172 
173  /* NOTE: PRINTF is relatively expensive in terms of CPU time, specially when used with-in execution loop. */
174  PRINTF("\r\n Gyro X = %d Y = %d Z = %d \r\n", rawData.gyro[0], rawData.gyro[1], rawData.gyro[2]);
175  }
176 }
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.
#define __END_WRITE_DATA__
Definition: sensor_drv.h:71
int main(void)
This is the The main function implementation.
#define BOARD_BootClockRUN
Definition: clock_config.h:45
#define FXAS21002_CTRL_REG1_DR_MASK
Definition: fxas21002.h:710
uint8_t data[FXLS8962_DATA_SIZE]
int32_t status
const registerreadlist_t cFxas21002_DataReady[]
const registerreadlist_t cFxas21002_Output_Values[]
void FXAS21002_SPI_SetIdleTask(fxas21002_spi_sensorhandle_t *pSensorHandle, registeridlefunction_t idleTask, void *userParam)
: The interface function to set the SPI Idle Task.
void BOARD_InitDebugConsole(void)
Definition: board.c:41
const registerwritelist_t cFxas21002_Config_Normal[]
void(* registeridlefunction_t)(void *userParam)
This is the register idle function type.
Definition: sensor_drv.h:123
The fxas21002_drv.h file describes the fxas21002 driver interface and structures. ...
#define __END_READ_DATA__
Definition: sensor_drv.h:77
int32_t FXAS21002_SPI_Configure(fxas21002_spi_sensorhandle_t *pSensorHandle, const registerwritelist_t *pRegWriteList)
The interface function to configure he sensor.
#define FXAS21002_DR_STATUS_ZYXDR_MASK
Definition: fxas21002.h:186
This structure defines the fxas21002 raw data buffer.
Definition: fxas21002_drv.h:79
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:91
#define SPI_S_DEVICE_INDEX
Definition: frdm_k64f.h:115
#define FXAS21002_CS
#define FXAS21002_WHO_AM_I_WHOAMI_PROD_VALUE
Definition: fxas21002.h:428
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:114
This defines the sensor specific information for SPI.
Definition: fxas21002_drv.h:59
fxls8962_acceldataUser_t rawData
#define SPI_S_SIGNAL_EVENT
Definition: frdm_k64f.h:116
This structure defines the Write command List.
Definition: sensor_drv.h:94
This structure defines the Read command List.
Definition: sensor_drv.h:104
#define SMC
Definition: lpc54114.h:144
#define FXAS21002_GYRO_DATA_SIZE
The size of the FXAS21002 gyro data.
Definition: fxas21002_drv.h:87
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
void BOARD_InitPins(void)
Configures pin routing and optionally pin electrical features.
Definition: pin_mux.c:73
#define FXAS21002_CTRL_REG1_DR_12_5HZ
Definition: fxas21002.h:736
#define SPI_S_DRIVER
Definition: frdm_k64f.h:113