ISSDK  1.8
IoT Sensing Software Development Kit
mag3110_normal_interrupt.c
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2015-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_normal_interrupt.c
11  * @brief The mag3110_normal_interrupt.c file implements the ISSDK MAG3110 sensor driver
12  * example demonstration with interrupt mode.
13  */
14 
15 //-----------------------------------------------------------------------
16 // SDK Includes
17 //-----------------------------------------------------------------------
18 #include "pin_mux.h"
19 #include "clock_config.h"
20 #include "board.h"
21 #include "fsl_debug_console.h"
22 
23 //-----------------------------------------------------------------------
24 // CMSIS Includes
25 //-----------------------------------------------------------------------
26 #include "Driver_I2C.h"
27 
28 //-----------------------------------------------------------------------
29 // ISSDK Includes
30 //-----------------------------------------------------------------------
31 #include "issdk_hal.h"
32 #include "gpio_driver.h"
33 #include "mag3110_drv.h"
34 
35 //-----------------------------------------------------------------------
36 // Macros
37 //-----------------------------------------------------------------------
38 #define MAG3110_DATA_SIZE (6) /* 2 byte X,Y,Z Axis Data each. */
39 
40 //-----------------------------------------------------------------------
41 // Constants
42 //-----------------------------------------------------------------------
43 /*! @brief Register settings for Normal (non buffered) mode since Interrupt is enabled implicitly. */
45  /* Set Ouput Rate @10HZ (ODR = 2 and OSR = 32). */
48  /* Set Auto Magnetic Sensor Reset. */
52 
53 /*! @brief Address and size of Raw Pressure+Temperature Data in Normal Mode. */
56 
57 //-----------------------------------------------------------------------
58 // Global Variables
59 //-----------------------------------------------------------------------
60 volatile bool gMag3110DataReady;
61 
62 //-----------------------------------------------------------------------
63 // Functions
64 //-----------------------------------------------------------------------
65 /*! -----------------------------------------------------------------------
66  * @brief This is the Sensor Data Ready ISR implementation.
67  * @details This function sets the flag which indicates if a new sample(s) is available for reading.
68  * @param[in] pUserData This is a void pointer to the instance of the user specific data structure for the ISR.
69  * @return void There is no return value.
70  * @constraints None
71  * @reeentrant Yes
72  * -----------------------------------------------------------------------*/
73 void mag3110_int_data_ready_callback(void *pUserData)
74 { /*! @brief Set flag to indicate Sensor has signalled data ready. */
75  gMag3110DataReady = true;
76 }
77 
78 /*! -----------------------------------------------------------------------
79  * @brief This is the The main function implementation.
80  * @details This function invokes board initializes routines, then then brings up the sensor and
81  * finally enters an endless loop to continuously read available samples.
82  * @param[in] void This is no input parameter.
83  * @return void There is no return value.
84  * @constraints None
85  * @reeentrant No
86  * -----------------------------------------------------------------------*/
87 int main(void)
88 {
90  uint8_t data[MAG3110_DATA_SIZE];
92 
93  ARM_DRIVER_I2C *I2Cdrv = &I2C_S_DRIVER; // Now using the shield.h value!!!
94  mag3110_i2c_sensorhandle_t mag3110Driver;
96 
97  /*! Initialize the MCU hardware. */
101 
102  PRINTF("\r\n ISSDK MAG3110 sensor driver example demonstration with interrupt mode.\r\n");
103 
104  /*! Initialize MAG3110 pin used by FRDM board */
106 
107  /*! Initialize RGB LED pin used by FRDM board */
108  pGpioDriver->pin_init(&GREEN_LED, GPIO_DIRECTION_OUT, NULL, NULL, NULL);
109 
110  /*! Initialize the I2C driver. */
111  status = I2Cdrv->Initialize(I2C_S_SIGNAL_EVENT);
112  if (ARM_DRIVER_OK != status)
113  {
114  PRINTF("\r\n I2C Initialization Failed\r\n");
115  return -1;
116  }
117 
118  /*! Set the I2C Power mode. */
119  status = I2Cdrv->PowerControl(ARM_POWER_FULL);
120  if (ARM_DRIVER_OK != status)
121  {
122  PRINTF("\r\n I2C Power Mode setting Failed\r\n");
123  return -1;
124  }
125 
126  /*! Set the I2C bus speed. */
127  status = I2Cdrv->Control(ARM_I2C_BUS_SPEED, ARM_I2C_BUS_SPEED_FAST);
128  if (ARM_DRIVER_OK != status)
129  {
130  PRINTF("\r\n I2C Control Mode setting Failed\r\n");
131  return -1;
132  }
133 
134  /*! Initialize MAG3110 sensor driver. */
137  if (SENSOR_ERROR_NONE != status)
138  {
139  PRINTF("\r\n Sensor Initialization Failed\r\n");
140  return -1;
141  }
142  PRINTF("\r\n Successfully Initiliazed Sensor\r\n");
143 
144  /*! Set the task to be executed while waiting for I2C transactions to complete. */
146 
147  gMag3110DataReady = true; /* Since, INT for MAG3110 is by default High after Power ON, we have to directly read the
148  first sample for MAG3110 to clear INT. */
149 
150  /*! Configure the MAG3110 sensor driver. */
151  status = MAG3110_I2C_Configure(&mag3110Driver, cMag3110ConfigNormal);
152  if (SENSOR_ERROR_NONE != status)
153  {
154  PRINTF("\r\n nMAG3110 Sensor Configuration Failed, Err = %d\r\n", status);
155  return -1;
156  }
157  PRINTF("\r\n Successfully Applied MAG3110 Sensor Configuration\r\n");
158 
159  for (;;) /* Forever loop */
160  { /* In ISR Mode we do not need to check Data Ready Register.
161  * The receipt of interrupt will indicate data is ready. */
162  if (false == gMag3110DataReady)
163  { /* Loop, if new sample is not available. */
165  continue;
166  }
167  else
168  { /*! Clear the data ready flag, it will be set again by the ISR. */
169  gMag3110DataReady = false;
170  pGpioDriver->toggle_pin(&GREEN_LED);
171  }
172 
173  /*! Read the raw sensor data from the MAG3110. */
174  status = MAG3110_I2C_ReadData(&mag3110Driver, cMag3110OutputNormal, data);
175  if (ARM_DRIVER_OK != status)
176  {
177  PRINTF("\r\n Read Failed. \r\n");
178  return -1;
179  }
180 
181  /*! Process the sample and convert the raw sensor data to signed 16-bit container. */
182  rawData.mag[0] = ((int16_t)data[0] << 8) | data[1];
183  rawData.mag[1] = ((int16_t)data[2] << 8) | data[3];
184  rawData.mag[2] = ((int16_t)data[4] << 8) | data[5];
185 
186  MAG3110_CalibrateHardIronOffset(&rawData.mag[0], &rawData.mag[1], &rawData.mag[2]);
187 
188  /* NOTE: PRINTF is relatively expensive in terms of CPU time, specially when used with-in execution loop. */
189  PRINTF("\r\n Mag X = %d Y = %d Z = %d\r\n", rawData.mag[0], rawData.mag[1], rawData.mag[2]);
190  ASK_USER_TO_RESUME(100); /* Ask for user input after processing 100 samples. */
191  }
192 }
This structure defines the Write command List.
Definition: sensor_drv.h:68
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
#define ASK_USER_TO_RESUME(x)
Definition: frdm_k64f.h:106
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
const registerwritelist_t cMag3110ConfigNormal[]
Register settings for Normal (non buffered) mode since Interrupt is enabled implicitly.
#define MAG3110_I2C_ADDR
This structure defines the mag3110 data buffer.
Definition: mag3110_drv.h:38
#define MAG3110_INT1
Access structure of the GPIO Driver.
Definition: Driver_GPIO.h:38
#define SMC
Definition: lpc54114.h:118
#define MAG3110_CTRL_REG2_MAG_RST_MASK
Definition: mag3110.h:458
#define __END_WRITE_DATA__
Definition: sensor_drv.h:45
typedef int32_t(DATA_FORMAT_Append_t))(void *pData
The interface function to append the data on the formated stream.
#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
#define BOARD_BootClockRUN
Definition: clock_config.h:19
void(* registeridlefunction_t)(void *userParam)
This is the register idle function type.
Definition: sensor_drv.h:97
GENERIC_DRIVER_GPIO Driver_GPIO_KSDK
Definition: gpio_driver.c:177
GENERIC_DRIVER_GPIO * pGpioDriver
uint8_t data[FXLS8962_DATA_SIZE]
#define __END_READ_DATA__
Definition: sensor_drv.h:51
const registerreadlist_t cMag3110OutputNormal[]
Address and size of Raw Pressure+Temperature Data in Normal Mode.
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
void mag3110_int_data_ready_callback(void *pUserData)
This is the Sensor Data Ready ISR implementation.
ARM_DRIVER_I2C * I2Cdrv
fxos8700_accelmagdata_t rawData
#define MAG3110_CTRL_REG1_DR_MASK
Definition: mag3110.h:404
uint16_t readFrom
Definition: sensor_drv.h:80
void(* toggle_pin)(pinID_t aPinId)
Definition: Driver_GPIO.h:48
#define MAG3110_CTRL_REG1_OS_MASK
Definition: mag3110.h:401
#define MAG3110_DATA_SIZE
#define MAG3110_CTRL_REG2_AUTO_MSRT_EN_MASK
Definition: mag3110.h:464
#define MAG3110_CTRL_REG2_RAW_RAW
Definition: mag3110.h:474
#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
#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
int16_t mag[3]
Definition: mag3110_drv.h:41
#define I2C_S_DEVICE_INDEX
Definition: issdk_hal.h:35
volatile bool gMag3110DataReady
void BOARD_InitDebugConsole(void)
Definition: board.c:15
void BOARD_InitPins(void)
Configures pin routing and optionally pin electrical features.
Definition: pin_mux.c:47
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.
int main(void)
This is the The main function implementation.