ISSDK  1.7
IoT Sensing Software Development Kit
fxas21002_demo.c
Go to the documentation of this file.
1 /*
2  * The Clear BSD License
3  * Copyright (c) 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 fxas21002_demo.c
37  * @brief The fxas21002_demo.c file implements the ISSDK FXAS21002 sensor
38  * demo example demonstration with interrupt mode.
39  */
40 
41 //-----------------------------------------------------------------------
42 // SDK Includes
43 //-----------------------------------------------------------------------
44 #include "board.h"
45 #include "pin_mux.h"
46 #include "clock_config.h"
47 
48 //-----------------------------------------------------------------------
49 // CMSIS Includes
50 //-----------------------------------------------------------------------
51 #include "Driver_I2C.h"
52 #include "Driver_USART.h"
53 
54 //-----------------------------------------------------------------------
55 // ISSDK Includes
56 //-----------------------------------------------------------------------
57 #include "issdk_hal.h"
58 #include "gpio_driver.h"
59 #include "host_io_uart.h"
60 #include "systick_utils.h"
61 #include "fxas21002_drv.h"
62 #include "auto_detection_service.h"
63 
64 //-----------------------------------------------------------------------
65 // Macros
66 //-----------------------------------------------------------------------
67 #define FXAS21002_STREAM_DATA_SIZE 12
68 
69 /*! @brief Unique Name for this application which should match the target GUI pkg name. */
70 #define APPLICATION_NAME "FXAS21002 Gyroscope Demo"
71 /*! @brief Version to distinguish between instances the same application based on target Shield and updates. */
72 #define APPLICATION_VERSION "2.5"
73 /* Number of packets to skip after every restart. */
74 #define SKIP_PACKET_COUNT 5
75 
76 /*! @brief This structure defines the fxas21002 raw data buffer.*/
77 typedef struct
78 {
79  uint32_t timestamp; /*! The time, this sample was recorded. */
80  int16_t gyro[3]; /*!< The gyro data */
81  int8_t temp;
82  uint8_t intsrc;
84 
85 //-----------------------------------------------------------------------
86 // Constants
87 //-----------------------------------------------------------------------
88 /*! Prepare the register write list to configure FXAS21002 in non-FIFO mode. */
90  /*! Clear F_SETUP */
91  {FXAS21002_F_SETUP, 0x00, 0x00},
92  /*! Configure CTRL_REG1 register to put FXAS21002 to 12.5Hz sampling rate. */
94  /*! Configure CTRL_REG2 register to set interrupt configuration settings. */
98  /*! Clear CTRL_REG3 */
99  {FXAS21002_CTRL_REG3, 0x00, 0x00},
101 
102 /*! Prepare the register read list to read the raw gyro data from the FXAS21002. */
105 
107  {.readFrom = FXAS21002_INT_SRC_FLAG, .numBytes = 1}, __END_READ_DATA__};
108 
110  {.readFrom = FXAS21002_TEMP, .numBytes = 1}, __END_READ_DATA__};
111 
112 //-----------------------------------------------------------------------
113 // Global Variables
114 //-----------------------------------------------------------------------
117 volatile bool bStreamingEnabled = false, bFxas21002DataReady = false, bFxas21002Ready = false;
118 volatile uint8_t bSkipPacket = 0;
119 uint8_t gStreamID; /* The auto assigned Stream ID. */
122 
123 //-----------------------------------------------------------------------
124 // Functions
125 //-----------------------------------------------------------------------
126 /* This is the Sensor Data Ready ISR implementation.*/
128 { /*! @brief Set flag to indicate Sensor has signalled data ready. */
129  bFxas21002DataReady = true;
130 }
131 
132 /* Handler for Device Info and Streaming Control Commands. */
134  uint8_t tag, uint8_t *hostCommand, uint8_t *hostResponse, size_t *hostMsgSize, size_t respBufferSize)
135 {
136  bool success = false;
137 
138  /* If it is Host requesting Device Info, send Board Name and Shield Name. */
139  if (tag == HOST_PRO_INT_DEV_TAG)
140  { /* Byte 1 : Payload - Length of APPLICATION_NAME (b)
141  * Bytes=b : Payload Application Name
142  * Byte b+1 : Payload - Length of BOARD_NAME (s)
143  * Bytes=s : Payload Board Name
144  * Byte b+s+2 : Payload - Length of SHIELD_NAME (v)
145  * Bytes=v : Payload Shield Name */
146 
147  size_t appNameLen = strlen(embAppName);
148  size_t boardNameLen = strlen(boardString);
149  size_t shieldNameLen = strlen(shieldString);
150 
151  if (respBufferSize >= boardNameLen + shieldNameLen + appNameLen + 3)
152  { /* We have sufficient buffer. */
153  *hostMsgSize = 0;
154  }
155  else
156  {
157  return false;
158  }
159 
160  hostResponse[*hostMsgSize] = appNameLen;
161  *hostMsgSize += 1;
162 
163  memcpy(hostResponse + *hostMsgSize, embAppName, appNameLen);
164  *hostMsgSize += appNameLen;
165 
166  hostResponse[*hostMsgSize] = boardNameLen;
167  *hostMsgSize += 1;
168 
169  memcpy(hostResponse + *hostMsgSize, boardString, boardNameLen);
170  *hostMsgSize += boardNameLen;
171 
172  hostResponse[*hostMsgSize] = shieldNameLen;
173  *hostMsgSize += 1;
174 
175  memcpy(hostResponse + *hostMsgSize, shieldString, shieldNameLen);
176  *hostMsgSize += shieldNameLen;
177 
178  return true;
179  }
180 
181  /* If it is Host sending Streaming Commands, take necessary actions. */
182  if ((tag == (HOST_PRO_INT_CMD_TAG | HOST_PRO_CMD_W_CFG_TAG)) &&
184  { /* Byte 1 : Payload - Operation Code (Start/Stop Operation Code)
185  * Byte 2 : Payload - Stream ID (Target Stream for carrying out operation) */
186  switch (hostCommand[0]) /* Execute desired operation (Start/Stop) on the requested Stream. */
187  {
188  case HOST_CMD_START:
189  if (hostCommand[1] == gStreamID && bFxas21002Ready && bStreamingEnabled == false)
190  {
192  bStreamingEnabled = true;
194  success = true;
195  }
196  break;
197  case HOST_CMD_STOP:
198  if (hostCommand[1] == gStreamID && bFxas21002Ready && bStreamingEnabled == true)
199  {
200  pGpioDriver->clr_pin(&GREEN_LED);
201  bStreamingEnabled = false;
202  success = true;
203  }
204  break;
205  default:
206  break;
207  }
208  *hostMsgSize = 0; /* Zero payload in response. */
209  }
210 
211  return success;
212 }
213 
214 /*!
215  * @brief Main function
216  */
217 int main(void)
218 {
219  int32_t status;
221 
222  fxas21002_i2c_sensorhandle_t fxas21002Driver;
224 
225  ARM_DRIVER_I2C *pI2Cdriver = &I2C_S_DRIVER;
226  ARM_DRIVER_USART *pUartDriver = &HOST_S_DRIVER;
227 
228  /*! Initialize the MCU hardware. */
231 
232  /* Create the Short Application Name String for ADS. */
233  sprintf(embAppName, "%s:%s", APPLICATION_NAME, APPLICATION_VERSION);
234 
235  /* Run ADS. */
237 
238  /* Create the Full Application Name String for Device Info Response. */
240 
241  /*! Initialize FXAS21002 pin used by FRDM board */
243 
244  /*! Initialize RGB LED pin used by FRDM board */
245  pGpioDriver->pin_init(&GREEN_LED, GPIO_DIRECTION_OUT, NULL, NULL, NULL);
246 
247  /*! Initialize the I2C driver. */
248  status = pI2Cdriver->Initialize(I2C_S_SIGNAL_EVENT);
249  if (ARM_DRIVER_OK != status)
250  {
251  return -1;
252  }
253 
254  /*! Set the I2C Power mode. */
255  status = pI2Cdriver->PowerControl(ARM_POWER_FULL);
256  if (ARM_DRIVER_OK != status)
257  {
258  return -1;
259  }
260 
261  /*! Set the I2C bus speed. */
262  status = pI2Cdriver->Control(ARM_I2C_BUS_SPEED, ARM_I2C_BUS_SPEED_FAST);
263  if (ARM_DRIVER_OK != status)
264  {
265  return -1;
266  }
267 
268  /*! Initialize the UART driver. */
269  status = pUartDriver->Initialize(HOST_S_SIGNAL_EVENT);
270  if (ARM_DRIVER_OK != status)
271  {
272  return -1;
273  }
274 
275  /*! Set the UART Power mode. */
276  status = pUartDriver->PowerControl(ARM_POWER_FULL);
277  if (ARM_DRIVER_OK != status)
278  {
279  return -1;
280  }
281 
282  /*! Set UART Baud Rate. */
283  status = pUartDriver->Control(ARM_USART_MODE_ASYNCHRONOUS, BOARD_DEBUG_UART_BAUDRATE);
284  if (ARM_DRIVER_OK != status)
285  {
286  return -1;
287  }
288 
289  /*! Initialize FXAS21002 sensor driver. */
292  if (SENSOR_ERROR_NONE == status)
293  {
294  /*! Set the task to be executed while waiting for SPI transactions to complete. */
296 
297  /*! Configure the FXAS21002 sensor. */
298  status = FXAS21002_I2C_Configure(&fxas21002Driver, fxas21002_Config_Isr);
299  if (SENSOR_ERROR_NONE == status)
300  {
301  bFxas21002Ready = true;
302  }
303  }
304 
305  /*! Initialize streaming and assign a Stream ID. */
306  gStreamID = Host_IO_Init(pUartDriver, (void *)fxas21002Driver.pCommDrv, &fxas21002Driver.deviceInfo, NULL,
308  /* Confirm if a valid Stream ID has been allocated for this stream. */
309  if (0 == gStreamID)
310  {
311  bFxas21002Ready = false;
312  }
313  else
314  {
315  /*! Populate streaming header. */
317  pGpioDriver->clr_pin(&GREEN_LED);
318  }
319 
320  for (;;) /* Forever loop */
321  { /* Call UART Non-Blocking Receive. */
323 
324  /* Process packets only if streaming has been enabled by Host and ISR is available.
325  * In ISR Mode we do not need to check Data Ready Register.
326  * The receipt of interrupt will indicate data is ready. */
327  if (false == bStreamingEnabled || false == bFxas21002DataReady)
328  {
329  SMC_SetPowerModeWait(SMC); /* Power save, wait if nothing to do. */
330  continue;
331  }
332  else
333  { /*! Clear the data ready flag, it will be set again by the ISR. */
334  bFxas21002DataReady = false;
335  pGpioDriver->toggle_pin(&GREEN_LED);
336  }
337 
338  /*! Read new raw sensor data from the FXAS21002. */
339  status = FXAS21002_I2C_ReadData(&fxas21002Driver, fxas21002_Output_Values, data);
340  if (ARM_DRIVER_OK != status)
341  { /* Loop, if sample read failed. */
342  continue;
343  }
344 
345  // Read intSrc Register of FXAS21002
346  status = FXAS21002_I2C_ReadData(&fxas21002Driver, fxas21002_Reg, &intSrc);
347  if (ARM_DRIVER_OK != status)
348  {
349  return -1;
350  }
351 
352  status = FXAS21002_I2C_ReadData(&fxas21002Driver, fxas21002_Temp, &tempOut);
353  if (ARM_DRIVER_OK != status)
354  {
355  return -1;
356  }
357 
358  /* Update timestamp from Systick framework. */
360 
361  /*! Convert the raw sensor data to signed 16-bit container for display to the debug port. */
362  rawData.gyro[0] = ((int16_t)data[0] << 8) | data[1];
363  rawData.gyro[1] = ((int16_t)data[2] << 8) | data[3];
364  rawData.gyro[2] = ((int16_t)data[4] << 8) | data[5];
365  rawData.temp = tempOut;
366  rawData.intsrc = intSrc;
367 
368  /* Copy Raw samples to Streaming Buffer. */
369  memcpy(streamingPacket + STREAMING_HEADER_LEN, &rawData, FXAS21002_STREAM_DATA_SIZE);
370  /* Send streaming packet to Host. */
371  if(bSkipPacket)
372  {
373  bSkipPacket--;
374  }
375  else
376  {
377  Host_IO_Send(streamingPacket, sizeof(streamingPacket), HOST_FORMAT_HDLC);
378  }
379  }
380 }
uint32_t BOARD_SystickElapsedTime_us(int32_t *pStart)
Function to compute the Elapsed Time.
Definition: systick_utils.c:99
#define FXAS21002_CTRL_REG2_INT_EN_DRDY_MASK
Definition: fxas21002.h:783
#define FXAS21002_CTRL_REG2_INT_CFG_DRDY_INT1
Definition: fxas21002.h:812
#define FXAS21002_CTRL_REG2_INT_EN_RT_ENABLE
Definition: fxas21002.h:813
#define FXAS21002_CTRL_REG2_IPOL_MASK
Definition: fxas21002.h:780
#define __END_WRITE_DATA__
Definition: sensor_drv.h:71
#define HOST_PRO_CMD_W_CFG_TAG
Definition: host_io_uart.h:89
GENERIC_DRIVER_GPIO * pGpioDriver
#define HOST_S_SIGNAL_EVENT
Definition: frdm_k64f.h:120
void Host_IO_Receive(host_cmd_proc_fn_t process_host_command, uint8_t encoding)
Definition: host_io_uart.c:233
void(* pin_init)(pinID_t aPinId, gpio_direction_t dir, void *apPinConfig, gpio_isr_handler_t aIsrHandler, void *apUserData)
Definition: Driver_GPIO.h:67
#define BOARD_BootClockRUN
Definition: clock_config.h:45
#define FXAS21002_CTRL_REG1_DR_MASK
Definition: fxas21002.h:710
#define SKIP_PACKET_COUNT
volatile bool bFxas21002DataReady
uint8_t gStreamID
uint8_t data[FXLS8962_DATA_SIZE]
#define APPLICATION_VERSION
Version to distinguish between instances the same application based on target Shield and updates...
int32_t status
#define FXAS21002_CTRL_REG2_INT_EN_DRDY_ENABLE
Definition: fxas21002.h:809
void(* toggle_pin)(pinID_t aPinId)
Definition: Driver_GPIO.h:74
#define SHIELD_NAME
void fxas21002_int_data_ready_callback(void *pUserData)
char shieldString[ADS_MAX_STRING_LENGTH]
#define FXAS21002_STREAM_DATA_SIZE
#define ADS_MAX_STRING_LENGTH
#define HOST_PRO_INT_DEV_TAG
Definition: host_io_uart.h:85
uint8_t streamingPacket[STREAMING_HEADER_LEN+FXLS8962_STREAM_DATA_SIZE]
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 HOST_PRO_INT_CMD_TAG
Bit aligned values for Host Protocol Interface IDs (Bits 5-6).
Definition: host_io_uart.h:83
#define __END_READ_DATA__
Definition: sensor_drv.h:77
volatile bool bStreamingEnabled
const registerreadlist_t fxas21002_Reg[]
#define FXAS21002_CTRL_REG2_INT_CFG_DRDY_MASK
Definition: fxas21002.h:786
#define APPLICATION_NAME
Unique Name for this application which should match the target GUI pkg name.
gpioHandleKSDK_t GREEN_LED
Definition: frdm_k64f.c:214
volatile uint8_t bSkipPacket
This defines the sensor specific information for I2C.
Definition: fxas21002_drv.h:70
volatile bool bFxas21002Ready
char embAppName[ADS_MAX_STRING_LENGTH]
void Host_IO_Add_ISO_Header(uint8_t streamID, uint8_t *pStreamingPacket, size_t sizePayload)
Definition: host_io_uart.c:112
void Host_IO_Send(uint8_t *pMsg, size_t size, uint8_t encoding)
Definition: host_io_uart.c:162
void(* clr_pin)(pinID_t aPinId)
Definition: Driver_GPIO.h:73
int32_t FXAS21002_I2C_ReadData(fxas21002_i2c_sensorhandle_t *pSensorHandle, const registerreadlist_t *pReadList, uint8_t *pBuffer)
The interface function to read the sensor data.
void BOARD_SystickEnable(void)
Function to enable systicks framework.
Definition: systick_utils.c:70
#define FXAS21002_INT1
#define I2C_S_DEVICE_INDEX
Definition: issdk_hal.h:61
#define FXAS21002_WHO_AM_I_WHOAMI_PROD_VALUE
Definition: fxas21002.h:428
#define FXAS21002_I2C_ADDR
int32_t FXAS21002_I2C_Configure(fxas21002_i2c_sensorhandle_t *pSensorHandle, const registerwritelist_t *pRegWriteList)
The interface function to configure he sensor.
#define I2C_S_DRIVER
Definition: issdk_hal.h:59
GENERIC_DRIVER_GPIO Driver_GPIO_KSDK
Definition: gpio_driver.c:203
typedef int32_t(DATA_FORMAT_Append_t))(void *pData
The interface function to append the data on the formated stream.
void FXAS21002_I2C_SetIdleTask(fxas21002_i2c_sensorhandle_t *pSensorHandle, registeridlefunction_t idleTask, void *userParam)
: The interface function to set the I2C Idle Task.
#define FXAS21002_CTRL_REG2_INT_EN_RT_MASK
Definition: fxas21002.h:789
#define BOARD_DEBUG_UART_BAUDRATE
Definition: board.h:57
int32_t gSystick
const registerreadlist_t fxas21002_Temp[]
Access structure of the GPIO Driver.
Definition: Driver_GPIO.h:64
void BOARD_SystickStart(int32_t *pStart)
Function to Record the Start systick.
Definition: systick_utils.c:79
ARM Systick Utilities.
void BOARD_RunADS(const char *appName, char *boardString, char *shieldString, size_t bufferLength)
The function to register Application Name and initialte ADS.
fxls8962_acceldataUser_t rawData
int main(void)
Main function.
uint8_t Host_IO_Init(ARM_DRIVER_USART *pDrv, void *pBus, void *pDevInfo, void *spiSlaveParams, uint16_t slaveAddress)
Definition: host_io_uart.c:126
This structure defines the fxas21002 raw data buffer.
This structure defines the Write command List.
Definition: sensor_drv.h:94
const registerwritelist_t fxas21002_Config_Isr[]
This structure defines the Read command List.
Definition: sensor_drv.h:104
#define SMC
Definition: lpc54114.h:144
The host_io_uart.h file contains the Host Protocol interface definitions and configuration.
#define STREAMING_HEADER_LEN
Definition: host_io_uart.h:51
#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
#define FXAS21002_CTRL_REG2_IPOL_ACTIVE_HIGH
Definition: fxas21002.h:808
bool process_host_command(uint8_t tag, uint8_t *hostCommand, uint8_t *hostResponse, size_t *hostMsgSize, size_t respBufferSize)
#define FXAS21002_CTRL_REG1_DR_12_5HZ
Definition: fxas21002.h:736
#define I2C_S_SIGNAL_EVENT
Definition: issdk_hal.h:60
#define HOST_S_DRIVER
Definition: frdm_k64f.h:119
const registerreadlist_t fxas21002_Output_Values[]
int32_t FXAS21002_I2C_Initialize(fxas21002_i2c_sensorhandle_t *pSensorHandle, ARM_DRIVER_I2C *pBus, uint8_t index, uint16_t sAddress, uint8_t whoAmi)
The interface function to initialize the sensor.
char boardString[ADS_MAX_STRING_LENGTH]
registerDeviceInfo_t deviceInfo
Definition: fxas21002_drv.h:72