hmc5883l.cpp
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2017, James Jackson
3  *
4  * All rights reserved.
5  *
6  * Redistribution and use in source and binary forms, with or without
7  * modification, are permitted provided that the following conditions are met:
8  *
9  * * Redistributions of source code must retain the above copyright notice, this
10  * list of conditions and the following disclaimer.
11  *
12  * * Redistributions in binary form must reproduce the above copyright notice,
13  * this list of conditions and the following disclaimer in the documentation
14  * and/or other materials provided with the distribution.
15  *
16  * * Neither the name of the copyright holder nor the names of its
17  * contributors may be used to endorse or promote products derived from
18  * this software without specific prior written permission.
19  *
20  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
21  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
23  * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
24  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
26  * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
27  * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
28  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
29  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
30  */
31 
32 
33 #include "hmc5883l.h"
34 
35 static HMC5883L* mag_ptr;
36 static void read_cb(uint8_t result);
37 
38 bool HMC5883L::init(I2C *i2c_drv)
39 {
40  mag_ptr = this;
41  mag_present_ = false;
42  i2c_ = i2c_drv;
43 
44  // Wait for the chip to power up
45 
48 
49  // Detect Magnetometer
50  uint8_t byte = 0;
51  if (i2c_->read(HMC58X3_ADDR, 0xFF, &byte) != I2C::RESULT_SUCCESS)
52  {
53  mag_present_ = false;
54  return false;
55  }
56  else
57  {
58  bool result = true;
59  // Configure HMC5833L
60  result &= i2c_->write(HMC58X3_ADDR, HMC58X3_CRA, HMC58X3_CRA_DO_75 | HMC58X3_CRA_NO_AVG | HMC58X3_CRA_MEAS_MODE_NORMAL ); // 75 Hz Measurement, no bias, no averaging
61  result &= i2c_->write(HMC58X3_ADDR, HMC58X3_CRB, HMC58X3_CRB_GN_390); // 390 LSB/Gauss
62  result &= i2c_->write(HMC58X3_ADDR, HMC58X3_MODE, HMC58X3_MODE_CONTINUOUS); // Continuous Measurement Mode
63  mag_present_ = true;
64  return result;
65  }
66 }
67 
69 {
70  if (mag_present_ && millis() > last_update_ms_ + 200)
71  mag_present_ = false;
72  return mag_present_;
73 }
74 
76 {
77  if ( millis() > next_update_ms_)
78  {
80  next_update_ms_ += 10;
81  }
82 }
83 
84 void HMC5883L::cb(uint8_t result)
85 {
86  if (result == I2C::RESULT_SUCCESS)
87  mag_present_ = true;
89  data_[0] = static_cast<float>(static_cast<int16_t>((i2c_buf_[0] << 8) | i2c_buf_[1]));
90  data_[1] = static_cast<float>(static_cast<int16_t>((i2c_buf_[2] << 8) | i2c_buf_[3]));
91  data_[2] = static_cast<float>(static_cast<int16_t>((i2c_buf_[4] << 8) | i2c_buf_[5]));
92 }
93 
94 void read_cb(uint8_t result)
95 {
96  mag_ptr->cb(result);
97 }
98 
99 bool HMC5883L::read(float mag_data[3])
100 {
101  mag_data[0] = data_[0];
102  mag_data[1] = data_[1];
103  mag_data[2] = data_[2];
104 
105  //if the mag's ADC over or underflows, then the data register is given the value of -4096
106  //the data register can also be assigned -4096 if there's a math overflow during bias calculation
107  return mag_data[0] != -4096 && mag_data[1] != -4096 && mag_data[2] != -4096;
108 }
#define HMC58X3_CRA_MEAS_MODE_NORMAL
Definition: hmc5883l.h:60
volatile uint32_t millis(void)
Definition: system.c:50
#define HMC58X3_MODE
Definition: hmc5883l.h:40
static void read_cb(uint8_t result)
Definition: hmc5883l.cpp:94
#define HMC58X3_MODE_CONTINUOUS
Definition: hmc5883l.h:74
#define HMC58X3_ADDR
Definition: hmc5883l.h:37
#define HMC58X3_CRA
Definition: hmc5883l.h:38
bool present()
Definition: hmc5883l.cpp:68
bool mag_present_
Definition: hmc5883l.h:99
#define HMC58X3_CRA_DO_75
Definition: hmc5883l.h:58
#define HMC58X3_CRB_GN_390
Definition: hmc5883l.h:69
static HMC5883L * mag_ptr
Definition: hmc5883l.cpp:35
volatile float data_[3]
Definition: hmc5883l.h:96
I2C * i2c_
Definition: hmc5883l.h:94
uint8_t i2c_buf_[6]
Definition: hmc5883l.h:95
#define HMC58X3_CRA_NO_AVG
Definition: hmc5883l.h:47
void update()
Definition: hmc5883l.cpp:75
void cb(uint8_t result)
Definition: hmc5883l.cpp:84
uint32_t next_update_ms_
Definition: hmc5883l.h:98
int8_t write(uint8_t addr, uint8_t reg, uint8_t data, void(*callback)(uint8_t), bool blocking=false)
Definition: i2c.cpp:298
Definition: i2c.h:40
int16_t mag_data[3]
Definition: hmc5883l_read.c:28
bool init(I2C *i2c_drv)
Definition: hmc5883l.cpp:38
bool read(float mag_data[])
Definition: hmc5883l.cpp:99
int8_t read(uint8_t addr, uint8_t reg, uint8_t num_bytes, uint8_t *data, void(*callback)(uint8_t)=nullptr, bool blocking=false)
Definition: i2c.cpp:179
#define HMC58X3_DATA
Definition: hmc5883l.h:41
uint32_t last_update_ms_
Definition: hmc5883l.h:97
#define HMC58X3_CRB
Definition: hmc5883l.h:39


rosflight_firmware
Author(s): Daniel Koch , James Jackson
autogenerated on Thu Oct 24 2019 03:17:18