ISSDK  1.7
IoT Sensing Software Development Kit
mag3110_normal_interrupt.c
Go to the documentation of this file.
1 /*
2  * The Clear BSD License
3  * Copyright (c) 2015-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_normal_interrupt.c
37  * @brief The mag3110_normal_interrupt.c file implements the ISSDK MAG3110 sensor driver
38  * 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 #include "fsl_debug_console.h"
48 
49 //-----------------------------------------------------------------------
50 // CMSIS Includes
51 //-----------------------------------------------------------------------
52 #include "Driver_I2C.h"
53 
54 //-----------------------------------------------------------------------
55 // ISSDK Includes
56 //-----------------------------------------------------------------------
57 #include "issdk_hal.h"
58 #include "gpio_driver.h"
59 #include "mag3110_drv.h"
60 
61 //-----------------------------------------------------------------------
62 // Macros
63 //-----------------------------------------------------------------------
64 #define MAG3110_DATA_SIZE (6) /* 2 byte X,Y,Z Axis Data each. */
65 
66 //-----------------------------------------------------------------------
67 // Constants
68 //-----------------------------------------------------------------------
69 /*! @brief Register settings for Normal (non buffered) mode since Interrupt is enabled implicitly. */
71  /* Set Ouput Rate @10HZ (ODR = 2 and OSR = 32). */
74  /* Set Auto Magnetic Sensor Reset. */
78 
79 /*! @brief Address and size of Raw Pressure+Temperature Data in Normal Mode. */
82 
83 //-----------------------------------------------------------------------
84 // Global Variables
85 //-----------------------------------------------------------------------
86 volatile bool gMag3110DataReady;
87 
88 //-----------------------------------------------------------------------
89 // Functions
90 //-----------------------------------------------------------------------
91 /*! -----------------------------------------------------------------------
92  * @brief This is the Sensor Data Ready ISR implementation.
93  * @details This function sets the flag which indicates if a new sample(s) is available for reading.
94  * @param[in] pUserData This is a void pointer to the instance of the user specific data structure for the ISR.
95  * @return void There is no return value.
96  * @constraints None
97  * @reeentrant Yes
98  * -----------------------------------------------------------------------*/
99 void mag3110_int_data_ready_callback(void *pUserData)
100 { /*! @brief Set flag to indicate Sensor has signalled data ready. */
101  gMag3110DataReady = true;
102 }
103 
104 /*! -----------------------------------------------------------------------
105  * @brief This is the The main function implementation.
106  * @details This function invokes board initializes routines, then then brings up the sensor and
107  * finally enters an endless loop to continuously read available samples.
108  * @param[in] void This is no input parameter.
109  * @return void There is no return value.
110  * @constraints None
111  * @reeentrant No
112  * -----------------------------------------------------------------------*/
113 int main(void)
114 {
115  int32_t status;
116  uint8_t data[MAG3110_DATA_SIZE];
118 
119  ARM_DRIVER_I2C *I2Cdrv = &I2C_S_DRIVER; // Now using the shield.h value!!!
120  mag3110_i2c_sensorhandle_t mag3110Driver;
122 
123  /*! Initialize the MCU hardware. */
124  BOARD_InitPins();
127 
128  PRINTF("\r\n ISSDK MAG3110 sensor driver example demonstration with interrupt mode.\r\n");
129 
130  /*! Initialize MAG3110 pin used by FRDM board */
132 
133  /*! Initialize RGB LED pin used by FRDM board */
134  pGpioDriver->pin_init(&GREEN_LED, GPIO_DIRECTION_OUT, NULL, NULL, NULL);
135 
136  /*! Initialize the I2C driver. */
137  status = I2Cdrv->Initialize(I2C_S_SIGNAL_EVENT);
138  if (ARM_DRIVER_OK != status)
139  {
140  PRINTF("\r\n I2C Initialization Failed\r\n");
141  return -1;
142  }
143 
144  /*! Set the I2C Power mode. */
145  status = I2Cdrv->PowerControl(ARM_POWER_FULL);
146  if (ARM_DRIVER_OK != status)
147  {
148  PRINTF("\r\n I2C Power Mode setting Failed\r\n");
149  return -1;
150  }
151 
152  /*! Set the I2C bus speed. */
153  status = I2Cdrv->Control(ARM_I2C_BUS_SPEED, ARM_I2C_BUS_SPEED_FAST);
154  if (ARM_DRIVER_OK != status)
155  {
156  PRINTF("\r\n I2C Control Mode setting Failed\r\n");
157  return -1;
158  }
159 
160  /*! Initialize MAG3110 sensor driver. */
163  if (SENSOR_ERROR_NONE != status)
164  {
165  PRINTF("\r\n Sensor Initialization Failed\r\n");
166  return -1;
167  }
168  PRINTF("\r\n Successfully Initiliazed Sensor\r\n");
169 
170  /*! Set the task to be executed while waiting for I2C transactions to complete. */
172 
173  gMag3110DataReady = true; /* Since, INT for MAG3110 is by default High after Power ON, we have to directly read the
174  first sample for MAG3110 to clear INT. */
175 
176  /*! Configure the MAG3110 sensor driver. */
177  status = MAG3110_I2C_Configure(&mag3110Driver, cMag3110ConfigNormal);
178  if (SENSOR_ERROR_NONE != status)
179  {
180  PRINTF("\r\n nMAG3110 Sensor Configuration Failed, Err = %d\r\n", status);
181  return -1;
182  }
183  PRINTF("\r\n Successfully Applied MAG3110 Sensor Configuration\r\n");
184 
185  for (;;) /* Forever loop */
186  { /* In ISR Mode we do not need to check Data Ready Register.
187  * The receipt of interrupt will indicate data is ready. */
188  if (false == gMag3110DataReady)
189  { /* Loop, if new sample is not available. */
191  continue;
192  }
193  else
194  { /*! Clear the data ready flag, it will be set again by the ISR. */
195  gMag3110DataReady = false;
196  pGpioDriver->toggle_pin(&GREEN_LED);
197  }
198 
199  /*! Read the raw sensor data from the MAG3110. */
200  status = MAG3110_I2C_ReadData(&mag3110Driver, cMag3110OutputNormal, data);
201  if (ARM_DRIVER_OK != status)
202  {
203  PRINTF("\r\n Read Failed. \r\n");
204  return -1;
205  }
206 
207  /*! Process the sample and convert the raw sensor data to signed 16-bit container. */
208  rawData.mag[0] = ((int16_t)data[0] << 8) | data[1];
209  rawData.mag[1] = ((int16_t)data[2] << 8) | data[3];
210  rawData.mag[2] = ((int16_t)data[4] << 8) | data[5];
211 
212  MAG3110_CalibrateHardIronOffset(&rawData.mag[0], &rawData.mag[1], &rawData.mag[2]);
213 
214  /* NOTE: PRINTF is relatively expensive in terms of CPU time, specially when used with-in execution loop. */
215  PRINTF("\r\n Mag X = %d Y = %d Z = %d\r\n", rawData.mag[0], rawData.mag[1], rawData.mag[2]);
216  ASK_USER_TO_RESUME(100); /* Ask for user input after processing 100 samples. */
217  }
218 }
const registerreadlist_t cMag3110OutputNormal[]
Address and size of Raw Pressure+Temperature Data in Normal Mode.
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 MAG3110_CTRL_REG1_DR_ODR_2
Definition: mag3110.h:423
#define MAG3110_CTRL_REG2_MAG_RST_EN
Definition: mag3110.h:471
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
#define MAG3110_DATA_SIZE
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]
int32_t status
void(* toggle_pin)(pinID_t aPinId)
Definition: Driver_GPIO.h:74
void BOARD_InitDebugConsole(void)
Definition: board.c:41
#define MAG3110_CTRL_REG1_OS_OSR_32
Definition: mag3110.h:418
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 MAG3110_CTRL_REG2_RAW_RAW
Definition: mag3110.h:474
#define __END_READ_DATA__
Definition: sensor_drv.h:77
GENERIC_DRIVER_GPIO * pGpioDriver
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
This structure defines the mag3110 data buffer.
Definition: mag3110_drv.h:64
gpioHandleKSDK_t GREEN_LED
Definition: frdm_k64f.c:214
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
#define MAG3110_CTRL_REG1_DR_MASK
Definition: mag3110.h:404
#define MAG3110_CTRL_REG2_MAG_RST_MASK
Definition: mag3110.h:458
#define ASK_USER_TO_RESUME(x)
Definition: frdm_k64f.h:132
#define MAG3110_CTRL_REG2_RAW_MASK
Definition: mag3110.h:461
#define MAG3110_CTRL_REG2_AUTO_MSRT_EN_EN
Definition: mag3110.h:477
#define I2C_S_DEVICE_INDEX
Definition: issdk_hal.h:61
const registerwritelist_t cMag3110ConfigNormal[]
Register settings for Normal (non buffered) mode since Interrupt is enabled implicitly.
#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.
volatile bool gMag3110DataReady
void mag3110_int_data_ready_callback(void *pUserData)
This is the Sensor Data Ready ISR implementation.
int main(void)
This is the The main function implementation.
Access structure of the GPIO Driver.
Definition: Driver_GPIO.h:64
fxls8962_acceldataUser_t rawData
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
int16_t mag[3]
Definition: mag3110_drv.h:67
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
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
void BOARD_InitPins(void)
Configures pin routing and optionally pin electrical features.
Definition: pin_mux.c:73
#define MAG3110_CTRL_REG2_AUTO_MSRT_EN_MASK
Definition: mag3110.h:464
#define MAG3110_I2C_ADDR
#define I2C_S_SIGNAL_EVENT
Definition: issdk_hal.h:60
#define MAG3110_INT1