ISSDK  1.8
IoT Sensing Software Development Kit
mag3110_oneshot.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_oneshot.c
11  * @brief The mag3110_oneshot.c file implements the ISSDK MAG3110 sensor driver
12  * example demonstration with polling 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 /* ISSDK Includes */
29 #include "issdk_hal.h"
30 #include "mag3110_drv.h"
31 
32 //-----------------------------------------------------------------------
33 // Macros
34 //-----------------------------------------------------------------------
35 #define MAG3110_DATA_SIZE (6) /* 2 byte X,Y,Z Axis Data each. */
36 
37 //-----------------------------------------------------------------------
38 // Constants
39 //-----------------------------------------------------------------------
40 /*! @brief Register settings for Magnetic Strength readings in One-Shot mode. */
42  /* Set Auto Magnetic Sensor Reset. */
46 
47 /*! @brief Register settings for Triggring One-Shot Sampling. */
49  /* Set the One ShoT Bit. */
52 
53 /*! @brief Address of Register containing OST Bit. */
55 
56 /*! @brief Address and size of Raw Magnetic Strength Data. */
59 
60 //-----------------------------------------------------------------------
61 // Functions
62 //-----------------------------------------------------------------------
63 /*!
64  * @brief Main function
65  */
66 int main(void)
67 {
69  uint8_t dataReady;
70  uint8_t data[MAG3110_DATA_SIZE];
72 
73  ARM_DRIVER_I2C *I2Cdrv = &I2C_S_DRIVER; // Now using the shield.h value!!!
74  mag3110_i2c_sensorhandle_t mag3110Driver;
75  registerDeviceInfo_t deviceInfo;
76 
80 
81  PRINTF("\r\n ISSDK MAG3110 sensor driver example demonstration with oneshot mode\r\n");
82 
83  /*! Initialize the I2C driver. */
84  status = I2Cdrv->Initialize(I2C_S_SIGNAL_EVENT);
85  if (ARM_DRIVER_OK != status)
86  {
87  PRINTF("\r\n I2C Initialization Failed\r\n");
88  return -1;
89  }
90 
91  /*! Set the I2C Power mode. */
92  status = I2Cdrv->PowerControl(ARM_POWER_FULL);
93  if (ARM_DRIVER_OK != status)
94  {
95  PRINTF("\r\n I2C Power Mode setting Failed\r\n");
96  return -1;
97  }
98 
99  /*! Set the I2C bus speed. */
100  status = I2Cdrv->Control(ARM_I2C_BUS_SPEED, ARM_I2C_BUS_SPEED_FAST);
101  if (ARM_DRIVER_OK != status)
102  {
103  PRINTF("\r\n I2C Control Mode setting Failed\r\n");
104  return -1;
105  }
106 
107  /*! Initialize MAG3110 sensor driver. */
110  if (SENSOR_ERROR_NONE != status)
111  {
112  PRINTF("\r\n Sensor Initialization Failed\r\n");
113  return -1;
114  }
115  PRINTF("\r\n Successfully Initiliazed Sensor\r\n");
116 
117  /*! We do not need to call MAG3110_I2C_Configure() in this case as we are going to read samples on demand.
118  * Instead we directly write register settings for One-Shot Mode... */
119  deviceInfo.deviceInstance = I2C_S_DEVICE_INDEX;
120  deviceInfo.functionParam = SMC;
122  status = Sensor_I2C_Write(mag3110Driver.pCommDrv, &deviceInfo, mag3110Driver.slaveAddress, cMag3110ConfigOneShot);
123  if (ARM_DRIVER_OK != status)
124  {
125  PRINTF("\r\nWrite Failed.\r\n");
126  return -1;
127  }
128  PRINTF("\r\nMAG3110 will now trigger acquisitioin of one sample and then read the data...\r\n");
129 
130  /*! The code contained withing the braces illustrate steps to read one-sample.
131  * These can be repated at desired intervals to acquire more samples. */
132  { /* Trigger acquisition of One Sample. */
133  status = Sensor_I2C_Write(mag3110Driver.pCommDrv, &deviceInfo, mag3110Driver.slaveAddress, cMag3110SetOST);
134  if (ARM_DRIVER_OK != status)
135  {
136  PRINTF("\r\nWrite Failed.\r\n");
137  return SENSOR_ERROR_WRITE;
138  }
139 
140  do /*! Keep checking the OST FLAG for completion. */
141  {
142  status = MAG3110_I2C_ReadData(&mag3110Driver, cMag3110GetOST, &dataReady);
143  if (ARM_DRIVER_OK != status)
144  {
145  PRINTF("\r\nRead Failed.\r\n");
146  return -1;
147  }
148  } /* Loop, untill sample acquisition is not completed. */
149  while (0 != (dataReady & MAG3110_CTRL_REG1_TM_MASK));
150 
151  /*! Read raw sensor data from the MAG3110. */
152  status = MAG3110_I2C_ReadData(&mag3110Driver, cMag3110OutputNormal, data);
153  if (ARM_DRIVER_OK != status)
154  {
155  PRINTF("\r\n Read Failed. \r\n");
156  return -1;
157  }
158 
159  /*! Process the sample and convert the raw sensor data to signed 16-bit container. */
160  rawData.mag[0] = ((int16_t)data[0] << 8) | data[1];
161  rawData.mag[1] = ((int16_t)data[2] << 8) | data[3];
162  rawData.mag[2] = ((int16_t)data[4] << 8) | data[5];
163 
164  PRINTF("\r\n Mag X = %d Y = %d Z = %d\r\n", rawData.mag[0], rawData.mag[1], rawData.mag[2]);
165  ASK_USER_TO_RESUME(1); /* Ask for user input after processing 1 samples. */
166  }
167 
168  return 0;
169 }
This structure defines the device specific info required by register I/O.
Definition: sensor_drv.h:102
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_REG2_MAG_RST_EN
Definition: mag3110.h:471
#define MAG3110_I2C_ADDR
This structure defines the mag3110 data buffer.
Definition: mag3110_drv.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 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:71
#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
int main(void)
Main function.
uint8_t data[FXLS8962_DATA_SIZE]
#define __END_READ_DATA__
Definition: sensor_drv.h:51
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
#define MAG3110_CTRL_REG1_TM_MASK
Definition: mag3110.h:395
ARM_DRIVER_I2C * I2Cdrv
fxos8700_accelmagdata_t rawData
#define MAG3110_CTRL_REG1_TM_TRIGGER
Definition: mag3110.h:414
uint16_t readFrom
Definition: sensor_drv.h:80
const registerreadlist_t cMag3110OutputNormal[]
Address and size of Raw Magnetic Strength Data.
#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
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
const registerreadlist_t cMag3110GetOST[]
Address of Register containing OST Bit.
int16_t mag[3]
Definition: mag3110_drv.h:41
#define I2C_S_DEVICE_INDEX
Definition: issdk_hal.h:35
#define MAG3110_DATA_SIZE
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
#define MAG3110_CTRL_REG2_RAW_MASK
Definition: mag3110.h:461
registeridlefunction_t idleFunction
Definition: sensor_drv.h:104
The mag3110_drv.h file describes the MAG3110 driver interface and structures.
const registerwritelist_t cMag3110ConfigOneShot[]
Register settings for Magnetic Strength readings in One-Shot mode.
const registerwritelist_t cMag3110SetOST[]
Register settings for Triggring One-Shot Sampling.