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