forked from simondlevy/BreezySTM32
-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathdrv_hmc5883l.c
144 lines (115 loc) · 3.94 KB
/
drv_hmc5883l.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
/*
drv_hmc5883l.c : Support for HMC5883L Magnetometer
Adapted from https://github.com/rosflight/firmware/blob/master/src/hmc5883l.cpp
This file is part of BreezySTM32.
BreezySTM32 is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
BreezySTM32 is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with BreezySTM32. If not, see <http://www.gnu.org/licenses/>.
*/
#include <breezystm32.h>
#include <math.h>
#define HMC58X3_ADDR 0x1E
#define HMC58X3_CRA 0x00
#define HMC58X3_CRB 0x01
#define HMC58X3_MODE 0x02
#define HMC58X3_DATA 0x03
#define HMC58X3_STATUS 0x09
#define HMC58X3_ID1 0x0A
#define HMC58X3_ID2 0x0B
#define HMC58X3_ID3 0x0C
#define HMC58X3_CRA_NO_AVG 0x00
#define HMC58X3_CRA_AVG_2_MEAS 0x20
#define HMC58X3_CRA_AVG_4_MEAS 0x40
#define HMC58X3_CRA_AVG_8_MEAS 0x60
#define HMC58X3_CRA_DO_0_75 0x00
#define HMC58X3_CRA_DO_1_5 0x04
#define HMC58X3_CRA_DO_3 0x08
#define HMC58X3_CRA_DO_7_5 0x0C
#define HMC58X3_CRA_DO_15 0x10
#define HMC58X3_CRA_DO_30 0x14
#define HMC58X3_CRA_DO_75 0x18
#define HMC58X3_CRA_MEAS_MODE_NORMAL 0x00
#define HMC58X3_CRA_MEAS_MODE_POS_BIAS 0x01
#define HMC58X3_CRA_MEAS_MODE_NEG_BIAS 0x02
#define HMC58X3_CRB_GN_1370 0x00
#define HMC58X3_CRB_GN_1090 0x20
#define HMC58X3_CRB_GN_820 0x40
#define HMC58X3_CRB_GN_660 0x60
#define HMC58X3_CRB_GN_440 0x80
#define HMC58X3_CRB_GN_390 0xA0
#define HMC58X3_CRB_GN_330 0xC0
#define HMC58X3_CRB_GN_230 0xE0
#define HMC58X3_MODE_HS 0x80
#define HMC58X3_MODE_CONTINUOUS 0x00
#define HMC58X3_MODE_SINGLE 0x01
#define HMC58X3_MODE_IDLE 0x02
#define HMC58X3_SR_LOCK 0x02
#define HMC58X3_SR_RDY 0x01
#define HMC58X3_TIMEOUT 30000
static uint8_t i2c_buf_[6];
static volatile float data_[3];
static uint32_t last_update_ms_;
static uint32_t next_update_ms_;
static bool mag_present_;
static uint8_t cmd;
static void read_cb(uint8_t result);
bool hmc5883lInit()
{
mag_present_ = false;
// Wait for the chip to power up
last_update_ms_ = millis();
next_update_ms_ = millis();
// Detect Magnetometer
cmd = 0;
if (i2cWrite(HMC58X3_ADDR, 0xFF, cmd) != true)
{
mag_present_ = false;
return false;
}
else
{
bool result = true;
// Configure HMC5833L
result &= i2cWrite(HMC58X3_ADDR, HMC58X3_CRA, HMC58X3_CRA_DO_75 | HMC58X3_CRA_NO_AVG | HMC58X3_CRA_MEAS_MODE_NORMAL ); // 75 Hz Measurement, no bias, no averaging
result &= i2cWrite(HMC58X3_ADDR, HMC58X3_CRB, HMC58X3_CRB_GN_390); // 390 LSB/Gauss
result &= i2cWrite(HMC58X3_ADDR, HMC58X3_MODE, HMC58X3_MODE_CONTINUOUS); // Continuous Measurement Mode
mag_present_ = true;
return result;
}
}
bool hmc5883l_present()
{
if (mag_present_ && millis() > last_update_ms_ + 1000)
mag_present_ = false;
return mag_present_;
}
void hmc5883l_request_async_update()
{
if ( millis() > next_update_ms_)
{
i2c_queue_job(READ, HMC58X3_ADDR, HMC58X3_DATA, i2c_buf_, 6, NULL, &read_cb);
next_update_ms_ += 10;
}
}
void read_cb(uint8_t result)
{
if (result != I2C_JOB_ERROR)
mag_present_ = true;
last_update_ms_ = millis();
data_[0] = (float)((int16_t)((i2c_buf_[0] << 8) | i2c_buf_[1]));
data_[1] = (float)((int16_t)((i2c_buf_[2] << 8) | i2c_buf_[3]));
data_[2] = (float)((int16_t)((i2c_buf_[4] << 8) | i2c_buf_[5]));
}
void hmc5883l_async_read(float *mag_data)
{
mag_data[0] = data_[0];
mag_data[1] = data_[1];
mag_data[2] = data_[2];
}