ISSDK  1.7
IoT Sensing Software Development Kit
driver_MAG3110.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 /*! \file driver_MAG3110.c
36  \brief Provides init() and read() functions for the MAG3110 magnetometer.
37 */
38 
39 #include "board.h" // generated by Kinetis Expert. Long term - merge sensor_board.h into this file
40 #include "sensor_fusion.h" // Sensor fusion structures and types
41 #include "sensor_drv.h"
42 #include "sensor_io_i2c.h" // Required for registerreadlist_t / registerwritelist_t declarations
43 #include "drivers.h" // Device specific drivers supplied by NXP (can be replaced with user drivers)
44 #include "mag3110.h"
45 #define MAG3110_COUNTSPERUT 10 // fixed range for MAG3110 magnetometer
46 
47 #if F_USING_MAG
48 
49 // Command definition to read the WHO_AM_I value.
51 {
52  { .readFrom = MAG3110_WHO_AM_I, .numBytes = 1 }, __END_READ_DATA__
53 };
54 
55 // Command definition to read the number of entries in the accel FIFO.
57 {
58  { .readFrom = MAG3110_DR_STATUS, .numBytes = 1 }, __END_READ_DATA__
59 };
60 
61 // Command definition to read the converted result
63 {
64  { .readFrom = MAG3110_OUT_X_MSB, .numBytes = 6 }, __END_READ_DATA__
65 };
66 
67 // Each entry in a RegisterWriteList is composed of: register address, value to write, bit-mask to apply to write (0 enables)
69 {
70  // write 0000 0000 = 0x00 to CTRL_REG1 to place MAG3110 into standby
71  // [7-1] = 0000 000
72  // [0]: AC=0 for standby
73  { MAG3110_CTRL_REG1, 0x00, 0x00 },
74 
75  // write 1001 0000 = 0x90 to CTRL_REG2
76  // [7]: AUTO_MRST_EN=1: enable degaussing
77  // [6]: unused=0
78  // [5]: RAW=0: normal mode
79  // [4]: Mag_RST=1: enable a single degauss
80  // [3-0]: unused=0
81  { MAG3110_CTRL_REG2, 0x90, 0x00 },
82 
83  // write 000X X001 to CTRL_REG1 to set ODR and take MAG3110 out of standby
84  // [7-5]: DR=000 for 1280Hz ADC (to give best noise performance)
85  // [4-3]: OS=11 for 10Hz ODR giving 0x19
86  // [4-3]: OS=10 for 20Hz ODR giving 0x11
87  // [4-3]: OS=01 for 40Hz ODR giving 0x09
88  // [4-3]: OS=00 for 80Hz ODR giving 0x01
89  // [2]: FT=0 for normal reads
90  // [1]: TM=0 to not trigger immediate measurement
91  // [0]: AC=1 for active mode
92 #if (MAG_ODR_HZ <= 10) // select 10Hz ODR
93  { MAG3110_CTRL_REG1, 0x19, 0x00 },
94 #elif (MAG_ODR_HZ <= 30) // select 20Hz ODR (to give lower noise with standard 25Hz build)
95  { MAG3110_CTRL_REG1, 0x11, 0x00 },
96 #elif (MAG_ODR_HZ <= 40) // select 40Hz ODR
97  { MAG3110_CTRL_REG1, 0x09, 0x00 },
98 #else // select 80Hz ODR
99  { MAG3110_CTRL_REG1, 0x01, 0x00 },
100 #endif
102 };
103 
104 // All sensor drivers and initialization functions have the same prototype.
105 // sfg is a pointer to the master "global" sensor fusion structure.
106 // sensor = pointer to linked list element used by the sensor fusion subsystem to specify required sensors
107 //#pragma optimize=no_scheduling
109 {
110  int32_t status;
111  uint8_t reg;
112  status = Register_I2C_Read(sensor->bus_driver, &sensor->deviceInfo, sensor->addr, MAG3110_WHO_AM_I, 1, &reg);
113  if (status==SENSOR_ERROR_NONE) {
114  sfg->Mag.iWhoAmI = reg;
115  if (reg!=MAG3110_WHOAMI_VALUE) {
116  return(SENSOR_ERROR_INIT);
117  }
118  } else {
119  // iWhoAmI will return default value of zero
120  // return with error
121  return(SENSOR_ERROR_INIT);
122  }
123 
124  // Configure and start the MAG3110 sensor. This does multiple register writes
125  // (see MAG3110_Initialization definition above)
126  status = Sensor_I2C_Write(sensor->bus_driver, &sensor->deviceInfo, sensor->addr, MAG3110_Initialization );
127 
128  // Stash some needed constants in the SF data structure for this sensor
130  sfg->Mag.fCountsPeruT = (float)MAG3110_COUNTSPERUT; // IAR optimized this out without the #pragma before the function
131  sfg->Mag.fuTPerCount = 1.0F / MAG3110_COUNTSPERUT; // IAR optimized this out without the #pragma before the function
132 
133  sensor->isInitialized = F_USING_MAG; // IAR optimized this out without the #pragma before the function
134  sfg->Mag.isEnabled = true; // IAR optimized this out without the #pragma before the function
135 
136  return (status);
137 }
138 
140 {
141  uint8_t I2C_Buffer[6]; // I2C read buffer
142  int8_t status; // I2C transaction status
143  int16_t sample[3]; // Reconstructed sample
144 
145  if(sensor->isInitialized != F_USING_MAG)
146  {
147  return SENSOR_ERROR_INIT;
148  }
149 
150  status = Sensor_I2C_Read(sensor->bus_driver, &sensor->deviceInfo, sensor->addr, MAG3110_DATA_READ, I2C_Buffer );
151  sample[CHX] = (I2C_Buffer[0] << 8) | I2C_Buffer[1];
152  sample[CHY] = (I2C_Buffer[2] << 8) | I2C_Buffer[3];
153  sample[CHZ] = (I2C_Buffer[4] << 8) | I2C_Buffer[5];
154  if (status==SENSOR_ERROR_NONE) {
155  conditionSample(sample); // truncate negative values to -32767
156  sample[CHZ] = -sample[CHZ]; // +Z should point up (MAG3110 Z positive is down)
157  addToFifo((union FifoSensor*) &(sfg->Mag), MAG_FIFO_SIZE, sample);
158  }
159 
160  return (status);
161 }
162 
163 
164 // Each entry in a RegisterWriteList is composed of: register address, value to write, bit-mask to apply to write (0 enables)
166 {
167  // Set ACTIVE = 0
168  { MAG3110_CTRL_REG1, 0x00, 0x00 },
170 };
171 
172 // MAG3110_Idle places the sensor into STANDBY mode (wakeup time = 25ms at ODR = 80Hz)
174 {
175  int32_t status;
176  if(sensor->isInitialized == F_USING_MAG) {
177  status = Sensor_I2C_Write(sensor->bus_driver, &sensor->deviceInfo, sensor->addr, MAG3110_IDLE );
178  sensor->isInitialized = 0;
179  sfg->Mag.isEnabled = false;
180  } else {
181  return SENSOR_ERROR_INIT;
182  }
183  return status;
184 }
185 
186 #endif
The sensor_drv.h file contains sensor state and error definitions.
#define MAG3110_COUNTSPERUT
registerreadlist_t MAG3110_DATA_READ[]
#define __END_WRITE_DATA__
Definition: sensor_drv.h:71
void * bus_driver
should be of type (ARM_DRIVER_I2C* for I2C-based sensors, ARM_DRIVER_SPI* for SPI) ...
#define CHY
Used to access Y-channel entries in various data data structures.
Definition: sensor_fusion.h:87
int8_t MAG3110_Idle(struct PhysicalSensor *sensor, SensorFusionGlobals *sfg)
SensorFusionGlobals sfg
int32_t status
const registerwritelist_t MAG3110_IDLE[]
float fuTPerCount
uT per count
#define MAG_FIFO_SIZE
FXOS8700 (mag), MAG3110 have no FIFO so equivalent to 1 element FIFO.
int32_t Sensor_I2C_Read(ARM_DRIVER_I2C *pCommDrv, registerDeviceInfo_t *devInfo, uint16_t slaveAddress, const registerreadlist_t *pReadList, uint8_t *pOutBuffer)
Read register data from a sensor.
uint8_t iWhoAmI
sensor whoami
#define __END_READ_DATA__
Definition: sensor_drv.h:77
bool isEnabled
true if the device is sampling
The top level fusion structure.
int8_t MAG3110_Read(struct PhysicalSensor *sensor, SensorFusionGlobals *sfg)
The mag3110.h contains the MAG3110 Magnetic sensor register definitions, access macros, and device access functions.
struct MagSensor Mag
magnetometer storage
uint16_t addr
I2C address if applicable.
void addToFifo(union FifoSensor *sensor, uint16_t maxFifoSize, int16_t sample[3])
addToFifo is called from within sensor driver read functions
The sensor_fusion.h file implements the top level programming interface.
The FifoSensor union allows us to use common pointers for Accel, Mag & Gyro logical sensor structures...
#define CHZ
Used to access Z-channel entries in various data data structures.
Definition: sensor_fusion.h:88
uint16_t isInitialized
Bitfields to indicate sensor is active (use SensorBitFields from build.h)
registerDeviceInfo_t deviceInfo
I2C device context.
Provides function prototypes for driver level interfaces.
const registerwritelist_t MAG3110_Initialization[]
const registerreadlist_t MAG3110_DR_STATUS_READ[]
#define CHX
Used to access X-channel entries in various data data structures.
Definition: sensor_fusion.h:86
int16_t iCountsPeruT
counts per uT
void conditionSample(int16_t sample[3])
conditionSample ensures that we never encounter the maximum negative two&#39;s complement value for a 16-...
typedef int32_t(DATA_FORMAT_Append_t))(void *pData
The interface function to append the data on the formated stream.
const registerreadlist_t MAG3110_WHO_AM_I_READ[]
int32_t Register_I2C_Read(ARM_DRIVER_I2C *pCommDrv, registerDeviceInfo_t *devInfo, uint16_t slaveAddress, uint8_t offset, uint8_t length, uint8_t *pOutBuffer)
The interface function to read a sensor register.
This structure defines the Write command List.
Definition: sensor_drv.h:94
This structure defines the Read command List.
Definition: sensor_drv.h:104
float fCountsPeruT
counts per uT
#define MAG3110_WHOAMI_VALUE
Definition: mag3110.h:37
int8_t MAG3110_Init(struct PhysicalSensor *sensor, SensorFusionGlobals *sfg)
An instance of PhysicalSensor structure type should be allocated for each physical sensors (combo dev...
#define F_USING_MAG
Definition: magnetic.h:47
The sensor_io_i2c.h file declares low-level interface functions for reading and writing sensor regist...
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