ISSDK  1.7
IoT Sensing Software Development Kit
mag3110_oneshot.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_oneshot.c
37  * @brief The mag3110_oneshot.c file implements the ISSDK MAG3110 sensor driver
38  * example demonstration with polling 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 /* ISSDK Includes */
55 #include "issdk_hal.h"
56 #include "mag3110_drv.h"
57 
58 //-----------------------------------------------------------------------
59 // Macros
60 //-----------------------------------------------------------------------
61 #define MAG3110_DATA_SIZE (6) /* 2 byte X,Y,Z Axis Data each. */
62 
63 //-----------------------------------------------------------------------
64 // Constants
65 //-----------------------------------------------------------------------
66 /*! @brief Register settings for Magnetic Strength readings in One-Shot mode. */
68  /* Set Auto Magnetic Sensor Reset. */
72 
73 /*! @brief Register settings for Triggring One-Shot Sampling. */
75  /* Set the One ShoT Bit. */
78 
79 /*! @brief Address of Register containing OST Bit. */
81 
82 /*! @brief Address and size of Raw Magnetic Strength Data. */
85 
86 //-----------------------------------------------------------------------
87 // Functions
88 //-----------------------------------------------------------------------
89 /*!
90  * @brief Main function
91  */
92 int main(void)
93 {
95  uint8_t dataReady;
96  uint8_t data[MAG3110_DATA_SIZE];
98 
99  ARM_DRIVER_I2C *I2Cdrv = &I2C_S_DRIVER; // Now using the shield.h value!!!
100  mag3110_i2c_sensorhandle_t mag3110Driver;
102 
103  BOARD_InitPins();
106 
107  PRINTF("\r\n ISSDK MAG3110 sensor driver example demonstration with oneshot mode\r\n");
108 
109  /*! Initialize the I2C driver. */
110  status = I2Cdrv->Initialize(I2C_S_SIGNAL_EVENT);
111  if (ARM_DRIVER_OK != status)
112  {
113  PRINTF("\r\n I2C Initialization Failed\r\n");
114  return -1;
115  }
116 
117  /*! Set the I2C Power mode. */
118  status = I2Cdrv->PowerControl(ARM_POWER_FULL);
119  if (ARM_DRIVER_OK != status)
120  {
121  PRINTF("\r\n I2C Power Mode setting Failed\r\n");
122  return -1;
123  }
124 
125  /*! Set the I2C bus speed. */
126  status = I2Cdrv->Control(ARM_I2C_BUS_SPEED, ARM_I2C_BUS_SPEED_FAST);
127  if (ARM_DRIVER_OK != status)
128  {
129  PRINTF("\r\n I2C Control Mode setting Failed\r\n");
130  return -1;
131  }
132 
133  /*! Initialize MAG3110 sensor driver. */
136  if (SENSOR_ERROR_NONE != status)
137  {
138  PRINTF("\r\n Sensor Initialization Failed\r\n");
139  return -1;
140  }
141  PRINTF("\r\n Successfully Initiliazed Sensor\r\n");
142 
143  /*! We do not need to call MAG3110_I2C_Configure() in this case as we are going to read samples on demand.
144  * Instead we directly write register settings for One-Shot Mode... */
145  deviceInfo.deviceInstance = I2C_S_DEVICE_INDEX;
146  deviceInfo.functionParam = SMC;
148  status = Sensor_I2C_Write(mag3110Driver.pCommDrv, &deviceInfo, mag3110Driver.slaveAddress, cMag3110ConfigOneShot);
149  if (ARM_DRIVER_OK != status)
150  {
151  PRINTF("\r\nWrite Failed.\r\n");
152  return -1;
153  }
154  PRINTF("\r\nMAG3110 will now trigger acquisitioin of one sample and then read the data...\r\n");
155 
156  /*! The code contained withing the braces illustrate steps to read one-sample.
157  * These can be repated at desired intervals to acquire more samples. */
158  { /* Trigger acquisition of One Sample. */
159  status = Sensor_I2C_Write(mag3110Driver.pCommDrv, &deviceInfo, mag3110Driver.slaveAddress, cMag3110SetOST);
160  if (ARM_DRIVER_OK != status)
161  {
162  PRINTF("\r\nWrite Failed.\r\n");
163  return SENSOR_ERROR_WRITE;
164  }
165 
166  do /*! Keep checking the OST FLAG for completion. */
167  {
168  status = MAG3110_I2C_ReadData(&mag3110Driver, cMag3110GetOST, &dataReady);
169  if (ARM_DRIVER_OK != status)
170  {
171  PRINTF("\r\nRead Failed.\r\n");
172  return -1;
173  }
174  } /* Loop, untill sample acquisition is not completed. */
175  while (0 != (dataReady & MAG3110_CTRL_REG1_TM_MASK));
176 
177  /*! Read raw sensor data from the MAG3110. */
178  status = MAG3110_I2C_ReadData(&mag3110Driver, cMag3110OutputNormal, data);
179  if (ARM_DRIVER_OK != status)
180  {
181  PRINTF("\r\n Read Failed. \r\n");
182  return -1;
183  }
184 
185  /*! Process the sample and convert the raw sensor data to signed 16-bit container. */
186  rawData.mag[0] = ((int16_t)data[0] << 8) | data[1];
187  rawData.mag[1] = ((int16_t)data[2] << 8) | data[3];
188  rawData.mag[2] = ((int16_t)data[4] << 8) | data[5];
189 
190  PRINTF("\r\n Mag X = %d Y = %d Z = %d\r\n", rawData.mag[0], rawData.mag[1], rawData.mag[2]);
191  ASK_USER_TO_RESUME(1); /* Ask for user input after processing 1 samples. */
192  }
193 
194  return 0;
195 }
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
registeridlefunction_t idleFunction
Definition: sensor_drv.h:130
#define MAG3110_CTRL_REG2_MAG_RST_EN
Definition: mag3110.h:471
#define BOARD_BootClockRUN
Definition: clock_config.h:45
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
const registerreadlist_t cMag3110OutputNormal[]
Address and size of Raw Magnetic Strength Data.
ARM_DRIVER_I2C * pCommDrv
Definition: mag3110_drv.h:58
void BOARD_InitDebugConsole(void)
Definition: board.c:41
void(* registeridlefunction_t)(void *userParam)
This is the register idle function type.
Definition: sensor_drv.h:123
#define MAG3110_CTRL_REG2_RAW_RAW
Definition: mag3110.h:474
#define __END_READ_DATA__
Definition: sensor_drv.h:77
This structure defines the mag3110 data buffer.
Definition: mag3110_drv.h:64
#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
registerDeviceInfo_t deviceInfo
I2C device context.
const registerwritelist_t cMag3110ConfigOneShot[]
Register settings for Magnetic Strength readings in One-Shot mode.
#define MAG3110_CTRL_REG2_AUTO_MSRT_EN_EN
Definition: mag3110.h:477
#define I2C_S_DEVICE_INDEX
Definition: issdk_hal.h:61
#define I2C_S_DRIVER
Definition: issdk_hal.h:59
const registerreadlist_t cMag3110GetOST[]
Address of Register containing OST Bit.
#define MAG3110_CTRL_REG1_TM_MASK
Definition: mag3110.h:395
typedef int32_t(DATA_FORMAT_Append_t))(void *pData
The interface function to append the data on the formated stream.
#define MAG3110_DATA_SIZE
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
This structure defines the device specific info required by register I/O.
Definition: sensor_drv.h:128
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_REG1_TM_TRIGGER
Definition: mag3110.h:414
#define MAG3110_CTRL_REG2_AUTO_MSRT_EN_MASK
Definition: mag3110.h:464
const registerwritelist_t cMag3110SetOST[]
Register settings for Triggring One-Shot Sampling.
#define MAG3110_I2C_ADDR
#define I2C_S_SIGNAL_EVENT
Definition: issdk_hal.h:60
int32_t Sensor_I2C_Write(ARM_DRIVER_I2C *pCommDrv, registerDeviceInfo_t *devInfo, uint16_t slaveAddress, const registerwritelist_t *pRegWriteList)
Write register data to a sensor.
Definition: sensor_io_i2c.c:97
int main(void)
Main function.