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 #include "hmc5883l.h"
33 
34 static HMC5883L* mag_ptr;
35 static void read_cb(uint8_t result);
36 
37 bool HMC5883L::init(I2C* i2c_drv)
38 {
39  mag_ptr = this;
40  mag_present_ = false;
41  i2c_ = i2c_drv;
42 
43  // Wait for the chip to power up
44 
47 
48  // Detect Magnetometer
49  uint8_t byte = 0;
50  if (i2c_->read(HMC58X3_ADDR, 0xFF, &byte) != I2C::RESULT_SUCCESS)
51  {
52  mag_present_ = false;
53  return false;
54  }
55  else
56  {
57  bool result = true;
58  // Configure HMC5833L
59  result &= i2c_->write(HMC58X3_ADDR, HMC58X3_CRA,
61  | 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:34
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:297
Definition: i2c.h:39
int16_t mag_data[3]
Definition: hmc5883l_read.c:28
bool init(I2C *i2c_drv)
Definition: hmc5883l.cpp:37
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:177
#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 Apr 15 2021 05:07:46