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