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