ISSDK  1.8
IoT Sensing Software Development Kit
fxas21002_poll_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_poll_demo.c
11  * @brief The fxas21002_poll_demo.c file implements the ISSDK FXAS21002 sensor
12  * demo example demonstration with Poll 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. */
69  /*! Clear CTRL_REG3 */
70  {FXAS21002_CTRL_REG3, 0x00, 0x00},
72 
73 /*! Prepare the register read list to read the status register from the FXAS21002. */
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, bFxas21002Ready = false;
92 volatile uint8_t bSkipPacket = 0;
93 uint8_t gStreamID; /* The auto assigned Stream ID. */
96 
97 //-----------------------------------------------------------------------
98 // Functions
99 //-----------------------------------------------------------------------
100 /* Handler for Device Info and Streaming Control Commands. */
102  uint8_t tag, uint8_t *hostCommand, uint8_t *hostResponse, size_t *hostMsgSize, size_t respBufferSize)
103 {
104  bool success = false;
105 
106  /* If it is Host requesting Device Info, send Board Name and Shield Name. */
107  if (tag == HOST_PRO_INT_DEV_TAG)
108  { /* Byte 1 : Payload - Length of APPLICATION_NAME (b)
109  * Bytes=b : Payload Application Name
110  * Byte b+1 : Payload - Length of BOARD_NAME (s)
111  * Bytes=s : Payload Board Name
112  * Byte b+s+2 : Payload - Length of SHIELD_NAME (v)
113  * Bytes=v : Payload Shield Name */
114 
115  size_t appNameLen = strlen(embAppName);
116  size_t boardNameLen = strlen(boardString);
117  size_t shieldNameLen = strlen(shieldString);
118 
119  if (respBufferSize >= boardNameLen + shieldNameLen + appNameLen + 3)
120  { /* We have sufficient buffer. */
121  *hostMsgSize = 0;
122  }
123  else
124  {
125  return false;
126  }
127 
128  hostResponse[*hostMsgSize] = appNameLen;
129  *hostMsgSize += 1;
130 
131  memcpy(hostResponse + *hostMsgSize, embAppName, appNameLen);
132  *hostMsgSize += appNameLen;
133 
134  hostResponse[*hostMsgSize] = boardNameLen;
135  *hostMsgSize += 1;
136 
137  memcpy(hostResponse + *hostMsgSize, boardString, boardNameLen);
138  *hostMsgSize += boardNameLen;
139 
140  hostResponse[*hostMsgSize] = shieldNameLen;
141  *hostMsgSize += 1;
142 
143  memcpy(hostResponse + *hostMsgSize, shieldString, shieldNameLen);
144  *hostMsgSize += shieldNameLen;
145 
146  return true;
147  }
148 
149  /* If it is Host sending Streaming Commands, take necessary actions. */
150  if ((tag == (HOST_PRO_INT_CMD_TAG | HOST_PRO_CMD_W_CFG_TAG)) &&
152  { /* Byte 1 : Payload - Operation Code (Start/Stop Operation Code)
153  * Byte 2 : Payload - Stream ID (Target Stream for carrying out operation) */
154  switch (hostCommand[0]) /* Execute desired operation (Start/Stop) on the requested Stream. */
155  {
156  case HOST_CMD_START:
157  if (hostCommand[1] == gStreamID && bFxas21002Ready && bStreamingEnabled == false)
158  {
160  bStreamingEnabled = true;
162  success = true;
163  }
164  break;
165  case HOST_CMD_STOP:
166  if (hostCommand[1] == gStreamID && bFxas21002Ready && bStreamingEnabled == true)
167  {
168  pGpioDriver->clr_pin(&GREEN_LED);
169  bStreamingEnabled = false;
170  success = true;
171  }
172  break;
173  default:
174  break;
175  }
176  *hostMsgSize = 0; /* Zero payload in response. */
177  }
178 
179  return success;
180 }
181 
182 /*!
183  * @brief Main function
184  */
185 int main(void)
186 {
187  int32_t status;
189 
190  fxas21002_i2c_sensorhandle_t fxas21002Driver;
192 
193  ARM_DRIVER_I2C *pI2Cdriver = &I2C_S_DRIVER;
194  ARM_DRIVER_USART *pUartDriver = &HOST_S_DRIVER;
195 
196  /*! Initialize the MCU hardware. */
199 
200  /* Create the Short Application Name String for ADS. */
201  sprintf(embAppName, "%s:%s", APPLICATION_NAME, APPLICATION_VERSION);
202 
203  /* Run ADS. */
205 
206  /* Create the Full Application Name String for Device Info Response. */
208 
209  /*! Initialize RGB LED pin used by FRDM board */
210  pGpioDriver->pin_init(&GREEN_LED, GPIO_DIRECTION_OUT, NULL, NULL, NULL);
211 
212  /*! Initialize the I2C driver. */
213  status = pI2Cdriver->Initialize(I2C_S_SIGNAL_EVENT);
214  if (ARM_DRIVER_OK != status)
215  {
216  return -1;
217  }
218 
219  /*! Set the I2C Power mode. */
220  status = pI2Cdriver->PowerControl(ARM_POWER_FULL);
221  if (ARM_DRIVER_OK != status)
222  {
223  return -1;
224  }
225 
226  /*! Set the I2C bus speed. */
227  status = pI2Cdriver->Control(ARM_I2C_BUS_SPEED, ARM_I2C_BUS_SPEED_FAST);
228  if (ARM_DRIVER_OK != status)
229  {
230  return -1;
231  }
232 
233  /*! Initialize the UART driver. */
234  status = pUartDriver->Initialize(HOST_S_SIGNAL_EVENT);
235  if (ARM_DRIVER_OK != status)
236  {
237  return -1;
238  }
239 
240  /*! Set the UART Power mode. */
241  status = pUartDriver->PowerControl(ARM_POWER_FULL);
242  if (ARM_DRIVER_OK != status)
243  {
244  return -1;
245  }
246 
247  /*! Set UART Baud Rate. */
248  status = pUartDriver->Control(ARM_USART_MODE_ASYNCHRONOUS, BOARD_DEBUG_UART_BAUDRATE);
249  if (ARM_DRIVER_OK != status)
250  {
251  return -1;
252  }
253 
254  /*! Initialize FXAS21002 sensor driver. */
257  if (SENSOR_ERROR_NONE == status)
258  {
259  /*! Set the task to be executed while waiting for SPI transactions to complete. */
261 
262  /*! Configure the FXAS21002 sensor. */
263  status = FXAS21002_I2C_Configure(&fxas21002Driver, fxas21002_Config_Isr);
264  if (SENSOR_ERROR_NONE == status)
265  {
266  bFxas21002Ready = true;
267  }
268  }
269 
270  /*! Initialize streaming and assign a Stream ID. */
271  gStreamID = Host_IO_Init(pUartDriver, (void *)fxas21002Driver.pCommDrv, &fxas21002Driver.deviceInfo, NULL,
273  /* Confirm if a valid Stream ID has been allocated for this stream. */
274  if (0 == gStreamID)
275  {
276  bFxas21002Ready = false;
277  }
278  else
279  {
280  /*! Populate streaming header. */
282  pGpioDriver->clr_pin(&GREEN_LED);
283  }
284 
285  for (;;) /* Forever loop */
286  { /* Call UART Non-Blocking Receive. */
288 
289  /* Process packets only if streaming has been enabled by Host and ISR is available.
290  * In ISR Mode we do not need to check Data Ready Register.
291  * The receipt of interrupt will indicate data is ready. */
292  if (false == bStreamingEnabled)
293  {
294  SMC_SetPowerModeWait(SMC); /* Power save, wait if nothing to do. */
295  continue;
296  }
297 
298  do /*! Keep checking the Status FLAG for completion. */
299  {
300  status = FXAS21002_I2C_ReadData(&fxas21002Driver, fxas21002_Status, &dataReady);
301  if (ARM_DRIVER_OK != status)
302  {
303  return -1;
304  }
305  /* Call UART Non-Blocking Receive while waiting for OST. */
307  } /* Loop, untill sample acquisition is not completed. */
308  while (0 == (dataReady & FXAS21002_DR_STATUS_ZYXDR_MASK));
309  pGpioDriver->toggle_pin(&GREEN_LED);
310 
311  /*! Read new raw sensor data from the FXAS21002. */
312  status = FXAS21002_I2C_ReadData(&fxas21002Driver, fxas21002_Output_Values, data);
313  if (ARM_DRIVER_OK != status)
314  { /* Loop, if sample read failed. */
315  continue;
316  }
317 
318  // Read intSrc Register of FXAS21002
319  status = FXAS21002_I2C_ReadData(&fxas21002Driver, fxas21002_Reg, &intSrc);
320  if (ARM_DRIVER_OK != status)
321  {
322  return -1;
323  }
324 
325  status = FXAS21002_I2C_ReadData(&fxas21002Driver, fxas21002_Temp, &tempOut);
326  if (ARM_DRIVER_OK != status)
327  {
328  return -1;
329  }
330 
331  /* Update timestamp from Systick framework. */
333 
334  /*! Convert the raw sensor data to signed 16-bit container for display to the debug port. */
335  rawData.gyro[0] = ((int16_t)data[0] << 8) | data[1];
336  rawData.gyro[1] = ((int16_t)data[2] << 8) | data[3];
337  rawData.gyro[2] = ((int16_t)data[4] << 8) | data[5];
338  rawData.temp = tempOut;
339  rawData.intsrc = intSrc;
340 
341  /* Copy Raw samples to Streaming Buffer. */
342  memcpy(streamingPacket + STREAMING_HEADER_LEN, &rawData, FXAS21002_STREAM_DATA_SIZE);
343  /* Send streaming packet to Host. */
344  if(bSkipPacket)
345  {
346  bSkipPacket--;
347  }
348  else
349  {
350  Host_IO_Send(streamingPacket, sizeof(streamingPacket), HOST_FORMAT_HDLC);
351  }
352  }
353 }
#define SKIP_PACKET_COUNT
#define FXAS21002_WHO_AM_I_WHOAMI_PROD_VALUE
Definition: fxas21002.h:402
uint8_t Host_IO_Init(ARM_DRIVER_USART *pDrv, void *pBus, void *pDevInfo, void *spiSlaveParams, uint16_t slaveAddress)
Definition: host_io_uart.c:100
const registerreadlist_t fxas21002_Status[]
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
int32_t status
int main(void)
Main function.
bool process_host_command(uint8_t tag, uint8_t *hostCommand, uint8_t *hostResponse, size_t *hostMsgSize, size_t respBufferSize)
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.
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
const registerreadlist_t fxas21002_Output_Values[]
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.
const registerwritelist_t fxas21002_Config_Isr[]
#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
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
int32_t gSystick
char shieldString[ADS_MAX_STRING_LENGTH]
#define I2C_S_SIGNAL_EVENT
Definition: issdk_hal.h:34
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 BOARD_DEBUG_UART_BAUDRATE
Definition: board.h:31
#define BOARD_BootClockRUN
Definition: clock_config.h:19
char boardString[ADS_MAX_STRING_LENGTH]
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]
char embAppName[ADS_MAX_STRING_LENGTH]
#define FXAS21002_DR_STATUS_ZYXDR_MASK
Definition: fxas21002.h:160
#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.
const registerreadlist_t fxas21002_Temp[]
#define FXAS21002_STREAM_DATA_SIZE
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
#define APPLICATION_NAME
Unique Name for this application which should match the target GUI pkg name.
uint16_t readFrom
Definition: sensor_drv.h:80
void(* toggle_pin)(pinID_t aPinId)
Definition: Driver_GPIO.h:48
#define ADS_MAX_STRING_LENGTH
#define APPLICATION_VERSION
Version to distinguish between instances the same application based on target Shield and updates...
const registerreadlist_t fxas21002_Reg[]
volatile bool bFxas21002Ready
#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
void BOARD_SystickEnable(void)
Function to enable systicks framework.
Definition: systick_utils.c:35
volatile bool bStreamingEnabled
#define STREAMING_HEADER_LEN
Definition: host_io_uart.h:25
GENERIC_DRIVER_GPIO * pGpioDriver
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.
gpioHandleKSDK_t GREEN_LED
Definition: frdm_k64f.c:188
#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
uint8_t gStreamID
volatile uint8_t bSkipPacket
#define HOST_S_DRIVER
Definition: frdm_k64f.h:93