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,
61  HMC58X3_CRA_DO_75 | HMC58X3_CRA_NO_AVG | HMC58X3_CRA_MEAS_MODE_NORMAL); // 75 Hz Measurement, no bias, no averaging
62  result &= i2c_->write(HMC58X3_ADDR, HMC58X3_CRB, HMC58X3_CRB_GN_390); // 390 LSB/Gauss
63  result &= i2c_->write(HMC58X3_ADDR, HMC58X3_MODE, HMC58X3_MODE_CONTINUOUS); // Continuous Measurement Mode
64  mag_present_ = true;
65  return result;
66  }
67 }
68 
70 {
71  if (mag_present_ && millis() > last_update_ms_ + 200)
72  mag_present_ = false;
73  return mag_present_;
74 }
75 
77 {
78  if (millis() > next_update_ms_)
79  {
81  next_update_ms_ += 10;
82  }
83 }
84 
85 void HMC5883L::cb(uint8_t result)
86 {
87  if (result == I2C::RESULT_SUCCESS)
88  mag_present_ = true;
90  data_[0] = static_cast<float>(static_cast<int16_t>((i2c_buf_[0] << 8) | i2c_buf_[1]));
91  data_[1] = static_cast<float>(static_cast<int16_t>((i2c_buf_[2] << 8) | i2c_buf_[3]));
92  data_[2] = static_cast<float>(static_cast<int16_t>((i2c_buf_[4] << 8) | i2c_buf_[5]));
93 }
94 
95 void read_cb(uint8_t result)
96 {
97  mag_ptr->cb(result);
98 }
99 
100 bool HMC5883L::read(float mag_data[3])
101 {
102  mag_data[0] = data_[0];
103  mag_data[1] = data_[1];
104  mag_data[2] = data_[2];
105 
106  //if the mag's ADC over or underflows, then the data register is given the value of -4096
107  //the data register can also be assigned -4096 if there's a math overflow during bias calculation
108  return mag_data[0] != -4096 && mag_data[1] != -4096 && mag_data[2] != -4096;
109 }
#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:95
#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:69
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:76
void cb(uint8_t result)
Definition: hmc5883l.cpp:85
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:100
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 Wed Jul 3 2019 19:59:24