ISSDK  1.8
IoT Sensing Software Development Kit
mag3110_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 mag3110_demo.c
11  * @brief The mag3110_demo.c file implements the ISSDK MAG3110 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 "mag3110_drv.h"
34 #include "host_io_uart.h"
35 #include "systick_utils.h"
36 #include "auto_detection_service.h"
37 
38 //-----------------------------------------------------------------------
39 // Macros
40 //-----------------------------------------------------------------------
41 #define MAG3110_DATA_SIZE 6 /* 2 byte X,Y,Z Axis Data each. */
42 #define MAG3110_STREAM_DATA_SIZE 12 /* 6 byte Data */
43 
44 /*! @brief Unique Name for this application which should match the target GUI pkg name. */
45 #define APPLICATION_NAME "MAG3110 Magnetometer Demo"
46 /*! @brief Version to distinguish between instances the same application based on target Shield and updates. */
47 #define APPLICATION_VERSION "2.5"
48 
49 /*! @brief This structure defines the mag3110 data buffer.*/
50 typedef struct
51 {
52  uint32_t timestamp; /*!< Time stamp value in micro-seconds. */
53  int16_t mag[3]; /*!< Sensor Magnetic Strength output: signed 16-bits. */
54  int16_t temp; /*!< The INTSRC data */
56 
57 //-----------------------------------------------------------------------
58 // Constants
59 //-----------------------------------------------------------------------
60 /*! @brief Register settings for Normal (non buffered) mode since Interrupt is enabled implicitly. */
62  /* Set Ouput Rate @10HZ (ODR = 2 and OSR = 32). */
65  /* Set Auto Magnetic Sensor Reset. */
69 
70 /*! @brief Address and size of Raw Pressure+Temperature Data in Normal Mode. */
73 /*! @brief Address and size of Temperature Data in Normal Mode. */
76 
77 //-----------------------------------------------------------------------
78 // Global Variables
79 //-----------------------------------------------------------------------
82 volatile bool bStreamingEnabled = false, bMag3110DataReady = false, bMag3110Ready = false;
83 uint8_t gStreamID; /* The auto assigned Stream ID. */
86 
87 //-----------------------------------------------------------------------
88 // Functions
89 //-----------------------------------------------------------------------
90 /* This is the Sensor Data Ready ISR implementation.*/
91 void mag3110_int_data_ready_callback(void *pUserData)
92 { /*! @brief Set flag to indicate Sensor has signalled data ready. */
93  bMag3110DataReady = true;
94 }
95 
96 /* Handler for Device Info and Streaming Control Commands. */
98  uint8_t tag, uint8_t *hostCommand, uint8_t *hostResponse, size_t *hostMsgSize, size_t respBufferSize)
99 {
100  bool success = false;
101 
102  /* If it is Host requesting Device Info, send Board Name and Shield Name. */
103  if (tag == HOST_PRO_INT_DEV_TAG)
104  { /* Byte 1 : Payload - Length of APPLICATION_NAME (b)
105  * Bytes=b : Payload Application Name
106  * Byte b+1 : Payload - Length of BOARD_NAME (s)
107  * Bytes=s : Payload Board Name
108  * Byte b+s+2 : Payload - Length of SHIELD_NAME (v)
109  * Bytes=v : Payload Shield Name */
110 
111  size_t appNameLen = strlen(embAppName);
112  size_t boardNameLen = strlen(boardString);
113  size_t shieldNameLen = strlen(shieldString);
114 
115  if (respBufferSize >= boardNameLen + shieldNameLen + appNameLen + 3)
116  { /* We have sufficient buffer. */
117  *hostMsgSize = 0;
118  }
119  else
120  {
121  return false;
122  }
123 
124  hostResponse[*hostMsgSize] = appNameLen;
125  *hostMsgSize += 1;
126 
127  memcpy(hostResponse + *hostMsgSize, embAppName, appNameLen);
128  *hostMsgSize += appNameLen;
129 
130  hostResponse[*hostMsgSize] = boardNameLen;
131  *hostMsgSize += 1;
132 
133  memcpy(hostResponse + *hostMsgSize, boardString, boardNameLen);
134  *hostMsgSize += boardNameLen;
135 
136  hostResponse[*hostMsgSize] = shieldNameLen;
137  *hostMsgSize += 1;
138 
139  memcpy(hostResponse + *hostMsgSize, shieldString, shieldNameLen);
140  *hostMsgSize += shieldNameLen;
141 
142  return true;
143  }
144 
145  /* If it is Host sending Streaming Commands, take necessary actions. */
146  if ((tag == (HOST_PRO_INT_CMD_TAG | HOST_PRO_CMD_W_CFG_TAG)) &&
148  { /* Byte 1 : Payload - Operation Code (Start/Stop Operation Code)
149  * Byte 2 : Payload - Stream ID (Target Stream for carrying out operation) */
150  switch (hostCommand[0]) /* Execute desired operation (Start/Stop) on the requested Stream. */
151  {
152  case HOST_CMD_START:
153  if (hostCommand[1] == gStreamID && bMag3110Ready && bStreamingEnabled == false)
154  {
156  bStreamingEnabled = true;
157  success = true;
158  }
159  break;
160  case HOST_CMD_STOP:
161  if (hostCommand[1] == gStreamID && bMag3110Ready && bStreamingEnabled == true)
162  {
163  pGpioDriver->clr_pin(&GREEN_LED);
164  bStreamingEnabled = false;
165  success = true;
166  }
167  break;
168  default:
169  break;
170  }
171  *hostMsgSize = 0; /* Zero payload in response. */
172  }
173 
174  return success;
175 }
176 
177 /*!
178  * @brief Main function
179  */
180 int main(void)
181 {
182  int32_t status;
184 
185  mag3110_i2c_sensorhandle_t mag3110Driver;
187 
188  ARM_DRIVER_I2C *pI2cDriver = &I2C_S_DRIVER;
189  ARM_DRIVER_USART *pUartDriver = &HOST_S_DRIVER;
190 
191  /*! Initialize the MCU hardware. */
194 
195  /* Create the Short Application Name String for ADS. */
196  sprintf(embAppName, "%s:%s", APPLICATION_NAME, APPLICATION_VERSION);
197 
198  /* Run ADS. */
200 
201  /* Create the Full Application Name String for Device Info Response. */
203 
204  /*! Initialize INT1 MAG3110 pin used by FRDM board */
206 
207  /*! Initialize GREEN LED pin used by FRDM board */
208  pGpioDriver->pin_init(&GREEN_LED, GPIO_DIRECTION_OUT, NULL, NULL, NULL);
209 
210  /*! Initialize the I2C driver. */
211  status = pI2cDriver->Initialize(I2C_S_SIGNAL_EVENT);
212  if (ARM_DRIVER_OK != status)
213  {
214  return -1;
215  }
216 
217  /*! Set the I2C Power mode. */
218  status = pI2cDriver->PowerControl(ARM_POWER_FULL);
219  if (ARM_DRIVER_OK != status)
220  {
221  return -1;
222  }
223 
224  /*! Set the I2C bus speed. */
225  status = pI2cDriver->Control(ARM_I2C_BUS_SPEED, ARM_I2C_BUS_SPEED_FAST);
226  if (ARM_DRIVER_OK != status)
227  {
228  return -1;
229  }
230 
231  /*! Initialize the UART driver. */
232  status = pUartDriver->Initialize(HOST_S_SIGNAL_EVENT);
233  if (ARM_DRIVER_OK != status)
234  {
235  return -1;
236  }
237 
238  /*! Set the UART Power mode. */
239  status = pUartDriver->PowerControl(ARM_POWER_FULL);
240  if (ARM_DRIVER_OK != status)
241  {
242  return -1;
243  }
244 
245  /*! Set UART Baud Rate. */
246  status = pUartDriver->Control(ARM_USART_MODE_ASYNCHRONOUS, BOARD_DEBUG_UART_BAUDRATE);
247  if (ARM_DRIVER_OK != status)
248  {
249  return -1;
250  }
251 
252  /*! Initialize the MAG3110 sensor driver. */
255  if (SENSOR_ERROR_NONE == status)
256  {
257  /*! Set the task to be executed while waiting for I2C transactions to complete. */
259 
260  /*! Configure the MAG3110 sensor driver. */
261  status = MAG3110_I2C_Configure(&mag3110Driver, cMag3110ConfigNormal);
262  if (SENSOR_ERROR_NONE == status)
263  {
264  bMag3110Ready = true;
265  }
266  }
267 
268  /*! Initialize streaming and assign a Stream ID. */
269  gStreamID =
270  Host_IO_Init(pUartDriver, (void *)mag3110Driver.pCommDrv, &mag3110Driver.deviceInfo, NULL, MAG3110_I2C_ADDR);
271  /* Confirm if a valid Stream ID has been allocated for this stream. */
272  if (0 == gStreamID)
273  {
274  bMag3110Ready = false;
275  }
276  else
277  {
278  /*! Populate streaming header. */
280  pGpioDriver->clr_pin(&GREEN_LED); /* Set LED to indicate application is ready. */
281  }
282 
283  for (;;) /* Forever loop */
284  { /* Call UART Non-Blocking Receive. */
286 
287  /* Process packets only if streaming has been enabled by Host and ISR is available.
288  * In ISR Mode we do not need to check Data Ready Register.
289  * The receipt of interrupt will indicate data is ready. */
290  if (false == bStreamingEnabled || false == bMag3110DataReady)
291  {
292  SMC_SetPowerModeWait(SMC); /* Power save, wait if nothing to do. */
293  continue;
294  }
295  else
296  { /*! Clear the data ready flag, it will be set again by the ISR. */
297  bMag3110DataReady = false;
298  pGpioDriver->toggle_pin(&GREEN_LED);
299  }
300 
301  /*! Read raw sensor data from the MAG3110. */
302  status = MAG3110_I2C_ReadData(&mag3110Driver, cMag3110OutputNormal, data);
303  if (ARM_DRIVER_OK != status)
304  { /* Loop, if sample read failed. */
305  continue;
306  }
307 
308  /* Update timestamp from Systick framework. */
310 
311  /*! Convert the raw sensor data to signed 32-bit and 16-bit containers for display to the debug port. */
312  rawData.mag[0] = ((int16_t)data[0] << 8) | data[1];
313  rawData.mag[1] = ((int16_t)data[2] << 8) | data[3];
314  rawData.mag[2] = ((int16_t)data[4] << 8) | data[5];
315 
316  MAG3110_CalibrateHardIronOffset(&rawData.mag[0], &rawData.mag[1], &rawData.mag[2]);
317  /*! Read TEMP_SRC 0x0F from MAG3110. */
318  status = MAG3110_I2C_ReadData(&mag3110Driver, cMag3110TempOut, &tempdata);
319  rawData.temp = tempdata;
320 
321  /* Copy Raw samples to Streaming Buffer. */
322  memcpy(streamingPacket + STREAMING_HEADER_LEN, &rawData, MAG3110_STREAM_DATA_SIZE);
323  /* Send streaming packet to Host. */
324  Host_IO_Send(streamingPacket, sizeof(streamingPacket), HOST_FORMAT_HDLC);
325  }
326 }
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
uint8_t gStreamID
Definition: mag3110_demo.c:83
int32_t MAG3110_I2C_Initialize(mag3110_i2c_sensorhandle_t *pSensorHandle, ARM_DRIVER_I2C *pBus, uint8_t index, uint16_t sAddress, uint8_t whoAmi)
The interface function to initialize the sensor.
Definition: mag3110_drv.c:22
int32_t status
#define MAG3110_CTRL_REG1_DR_ODR_2
Definition: mag3110.h:423
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
#define MAG3110_CTRL_REG2_MAG_RST_EN
Definition: mag3110.h:471
The host_io_uart.h file contains the Host Protocol interface definitions and configuration.
volatile bool bMag3110Ready
Definition: mag3110_demo.c:82
#define MAG3110_I2C_ADDR
This structure defines the mag3110 data buffer.
Definition: mag3110_demo.c:50
#define MAG3110_INT1
#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
int main(void)
Main function.
Definition: mag3110_demo.c:180
bool process_host_command(uint8_t tag, uint8_t *hostCommand, uint8_t *hostResponse, size_t *hostMsgSize, size_t respBufferSize)
Definition: mag3110_demo.c:97
#define MAG3110_DATA_SIZE
Definition: mag3110_demo.c:41
#define MAG3110_CTRL_REG2_MAG_RST_MASK
Definition: mag3110.h:458
#define __END_WRITE_DATA__
Definition: sensor_drv.h:45
char shieldString[ADS_MAX_STRING_LENGTH]
Definition: mag3110_demo.c:80
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
const registerreadlist_t cMag3110OutputNormal[]
Address and size of Raw Pressure+Temperature Data in Normal Mode.
Definition: mag3110_demo.c:71
#define I2C_S_SIGNAL_EVENT
Definition: issdk_hal.h:34
#define I2C_S_DRIVER
Definition: issdk_hal.h:33
int32_t MAG3110_I2C_Configure(mag3110_i2c_sensorhandle_t *pSensorHandle, const registerwritelist_t *pRegWriteList)
The interface function to configure he sensor.
Definition: mag3110_drv.c:61
int32_t gSystick
Definition: mag3110_demo.c:84
void Host_IO_Add_ISO_Header(uint8_t streamID, uint8_t *pStreamingPacket, size_t sizePayload)
Definition: host_io_uart.c:86
char boardString[ADS_MAX_STRING_LENGTH]
Definition: mag3110_demo.c:80
#define BOARD_DEBUG_UART_BAUDRATE
Definition: board.h:31
const registerwritelist_t cMag3110ConfigNormal[]
Register settings for Normal (non buffered) mode since Interrupt is enabled implicitly.
Definition: mag3110_demo.c:61
#define BOARD_BootClockRUN
Definition: clock_config.h:19
ARM_DRIVER_I2C * pCommDrv
Definition: mag3110_drv.h:32
void(* registeridlefunction_t)(void *userParam)
This is the register idle function type.
Definition: sensor_drv.h:97
registerDeviceInfo_t deviceInfo
Definition: mag3110_drv.h:31
uint8_t streamingPacket[STREAMING_HEADER_LEN+FXLS8962_STREAM_DATA_SIZE]
GENERIC_DRIVER_GPIO Driver_GPIO_KSDK
Definition: gpio_driver.c:177
void Host_IO_Send(uint8_t *pMsg, size_t size, uint8_t encoding)
Definition: host_io_uart.c:136
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
uint8_t data[FXLS8962_DATA_SIZE]
#define MAG3110_STREAM_DATA_SIZE
Definition: mag3110_demo.c:42
const registerreadlist_t cMag3110TempOut[]
Address and size of Temperature Data in Normal Mode.
Definition: mag3110_demo.c:74
#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.
int32_t MAG3110_I2C_ReadData(mag3110_i2c_sensorhandle_t *pSensorHandle, const registerreadlist_t *pReadList, uint8_t *pBuffer)
The interface function to read the sensor data.
Definition: mag3110_drv.c:104
fxos8700_accelmagdata_t rawData
char embAppName[ADS_MAX_STRING_LENGTH]
Definition: mag3110_demo.c:81
#define MAG3110_CTRL_REG1_DR_MASK
Definition: mag3110.h:404
volatile bool bMag3110DataReady
Definition: mag3110_demo.c:82
uint16_t readFrom
Definition: sensor_drv.h:80
void mag3110_int_data_ready_callback(void *pUserData)
Definition: mag3110_demo.c:91
void(* toggle_pin)(pinID_t aPinId)
Definition: Driver_GPIO.h:48
#define ADS_MAX_STRING_LENGTH
#define MAG3110_CTRL_REG1_OS_MASK
Definition: mag3110.h:401
#define APPLICATION_NAME
Unique Name for this application which should match the target GUI pkg name.
Definition: mag3110_demo.c:45
#define MAG3110_CTRL_REG2_AUTO_MSRT_EN_MASK
Definition: mag3110.h:464
void BOARD_SystickEnable(void)
Function to enable systicks framework.
Definition: systick_utils.c:35
#define MAG3110_CTRL_REG2_RAW_RAW
Definition: mag3110.h:474
#define APPLICATION_VERSION
Version to distinguish between instances the same application based on target Shield and updates...
Definition: mag3110_demo.c:47
#define STREAMING_HEADER_LEN
Definition: host_io_uart.h:25
#define MAG3110_CTRL_REG2_AUTO_MSRT_EN_EN
Definition: mag3110.h:477
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 defines the sensor specific information.
Definition: mag3110_drv.h:29
This structure defines the Read command List.
Definition: sensor_drv.h:78
volatile bool bStreamingEnabled
Definition: mag3110_demo.c:82
ARM Systick Utilities.
#define MAG3110_WHOAMI_VALUE
Definition: mag3110.h:37
void MAG3110_CalibrateHardIronOffset(int16_t *xValue, int16_t *yValue, int16_t *zValue)
Calibrates the magnetometer reading by determining the current hard iron offset.
Definition: mag3110_drv.c:164
gpioHandleKSDK_t GREEN_LED
Definition: frdm_k64f.c:188
#define I2C_S_DEVICE_INDEX
Definition: issdk_hal.h:35
GENERIC_DRIVER_GPIO * pGpioDriver
Definition: mag3110_demo.c:85
#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
void MAG3110_I2C_SetIdleTask(mag3110_i2c_sensorhandle_t *pSensorHandle, registeridlefunction_t idleTask, void *userParam)
: The interface function to set the I2C Idle Task.
Definition: mag3110_drv.c:53
#define MAG3110_CTRL_REG1_OS_OSR_32
Definition: mag3110.h:418
#define MAG3110_CTRL_REG2_RAW_MASK
Definition: mag3110.h:461
The mag3110_drv.h file describes the MAG3110 driver interface and structures.
#define HOST_S_DRIVER
Definition: frdm_k64f.h:93