hmc5883l.h
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2017, James Jackson and Tyler Miller
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 #pragma once
33 
34 #include "system.h"
35 #include "i2c.h"
36 
37 #define HMC58X3_ADDR 0x1E
38 #define HMC58X3_CRA 0x00
39 #define HMC58X3_CRB 0x01
40 #define HMC58X3_MODE 0x02
41 #define HMC58X3_DATA 0x03
42 #define HMC58X3_STATUS 0x09
43 #define HMC58X3_ID1 0x0A
44 #define HMC58X3_ID2 0x0B
45 #define HMC58X3_ID3 0x0C
46 
47 #define HMC58X3_CRA_NO_AVG 0x00
48 #define HMC58X3_CRA_AVG_2_MEAS 0x20
49 #define HMC58X3_CRA_AVG_4_MEAS 0x40
50 #define HMC58X3_CRA_AVG_8_MEAS 0x60
51 
52 #define HMC58X3_CRA_DO_0_75 0x00
53 #define HMC58X3_CRA_DO_1_5 0x04
54 #define HMC58X3_CRA_DO_3 0x08
55 #define HMC58X3_CRA_DO_7_5 0x0C
56 #define HMC58X3_CRA_DO_15 0x10
57 #define HMC58X3_CRA_DO_30 0x14
58 #define HMC58X3_CRA_DO_75 0x18
59 
60 #define HMC58X3_CRA_MEAS_MODE_NORMAL 0x00
61 #define HMC58X3_CRA_MEAS_MODE_POS_BIAS 0x01
62 #define HMC58X3_CRA_MEAS_MODE_NEG_BIAS 0x02
63 
64 #define HMC58X3_CRB_GN_1370 0x00
65 #define HMC58X3_CRB_GN_1090 0x20
66 #define HMC58X3_CRB_GN_820 0x40
67 #define HMC58X3_CRB_GN_660 0x60
68 #define HMC58X3_CRB_GN_440 0x80
69 #define HMC58X3_CRB_GN_390 0xA0
70 #define HMC58X3_CRB_GN_330 0xC0
71 #define HMC58X3_CRB_GN_230 0xE0
72 
73 #define HMC58X3_MODE_HS 0x80
74 #define HMC58X3_MODE_CONTINUOUS 0x00
75 #define HMC58X3_MODE_SINGLE 0x01
76 #define HMC58X3_MODE_IDLE 0x02
77 
78 #define HMC58X3_SR_LOCK 0x02
79 #define HMC58X3_SR_RDY 0x01
80 
81 #define HMC58X3_TIMEOUT 30000
82 
83 
84 class HMC5883L
85 {
86 public:
87  bool init(I2C *i2c_drv);
88  void update();
89  bool read(float mag_data[]);
90  bool present();
91  void cb(uint8_t result);
92 
93 private:
95  uint8_t i2c_buf_[6];
96  volatile float data_[3];
97  uint32_t last_update_ms_;
98  uint32_t next_update_ms_;
100 };
bool present()
Definition: hmc5883l.cpp:69
bool mag_present_
Definition: hmc5883l.h:99
volatile float data_[3]
Definition: hmc5883l.h:96
I2C * i2c_
Definition: hmc5883l.h:94
uint8_t i2c_buf_[6]
Definition: hmc5883l.h:95
void update()
Definition: hmc5883l.cpp:76
void cb(uint8_t result)
Definition: hmc5883l.cpp:85
uint32_t next_update_ms_
Definition: hmc5883l.h:98
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
uint32_t last_update_ms_
Definition: hmc5883l.h:97


rosflight_firmware
Author(s): Daniel Koch , James Jackson
autogenerated on Wed Jul 3 2019 19:59:24