8000 Merge pull request #299 from nameofuser1/master · ArduinoWorks/i2cdevlib@470bc2c · GitHub
[go: up one dir, main page]

Skip to content

Commit 470bc2c

Browse files
authored
Merge pull request jrowberg#299 from nameofuser1/master
Port to STM32 using Keil MDK pack.
2 parents 9a1ef6e + df92b18 commit 470bc2c

File tree

7 files changed

+4898
-0
lines changed

7 files changed

+4898
-0
lines changed

STM32/HMC5883/HMC5883L.c

Lines changed: 358 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,358 @@
1+
// I2Cdev library collection - HMC5883L I2C device class header file
2+
// Based on Honeywell HMC5883L datasheet, 10/2010 (Form #900405 Rev B)
3+
// 6/12/2012 by Jeff Rowberg <jeff@rowberg.net>
4+
// 6/6/2015 by Andrey Voloshin <voloshin@think.in.ua>
5+
// 03/28/2017 by Kamnev Yuriy <kamnev.u1969@gmail.com>
6+
//
7+
// Changelog:
8+
// 2017-03-28 - ported to STM32 using Keil MDK Pack
9+
// 2015-06-06 - ported to STM32 HAL library from Arduino code
10+
// 2012-06-12 - fixed swapped Y/Z axes
11+
// 2011-08-22 - small Doxygen comment fixes
12+
// 2011-07-31 - initial release
13+
14+
/* ============================================
15+
I2Cdev device library code is placed under the MIT license
16+
Copyright (c) 2011 Jeff Rowberg
17+
18+
Permission is hereby granted, free of charge, to any person obtaining a copy
19+
of this software and associated documentation files (the "Software"), to deal
20+
in the Software without restriction, including without limitation the rights
21+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
22+
copies of the Software, and to permit persons to whom the Software is
23+
furnished to do so, subject to the following conditions:
24+
25+
The above copyright notice and this permission notice shall be included in
26+
all copies or substantial portions of the Software.
27+
28+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
29+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
30+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
31+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
32+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
33+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
34+
THE SOFTWARE.
35+
===============================================
36+
*/
37+
38+
#include "HMC5883L.h"
39+
40+
static uint8_t devAddr;
41+
static uint8_t buffer[6];
42+
static uint8_t mode;
43+
44+
45+
/** Power on and prepare for general usage.
46+
* This will prepare the magnetometer with default settings, ready for single-
47+
* use mode (very low power requirements). Default settings include 8-sample
48+
* averaging, 15 Hz data output rate, normal measurement bias, a,d 1090 gain (in
49+
* terms of LSB/Gauss). Be sure to adjust any settings you need specifically
50+
* after initialization, especially the gain settings if you happen to be seeing
51+
* a lot of -4096 values (see the datasheet for mor information).
52+
*/
53+
void HMC5883L_initialize() {
54+
devAddr = HMC5883L_DEFAULT_ADDRESS;
55+
// write CONFIG_A register
56+
I2Cdev_writeByte(devAddr, HMC5883L_RA_CONFIG_A,
57+
(HMC5883L_AVERAGING_8 << (HMC5883L_CRA_AVERAGE_BIT - HMC5883L_CRA_AVERAGE_LENGTH + 1)) |
58+
(HMC5883L_RATE_15 << (HMC5883L_CRA_RATE_BIT - HMC5883L_CRA_RATE_LENGTH + 1)) |
59+
(HMC5883L_BIAS_NORMAL << (HMC5883L_CRA_BIAS_BIT - HMC5883L_CRA_BIAS_LENGTH + 1)));
60+
61+
// write CONFIG_B register
62+
HMC5883L_setGain(HMC5883L_GAIN_1090);
63+
64+
// write MODE register
65+
HMC5883L_setMode(HMC5883L_MODE_SINGLE);
66+
}
67+
68+
/** Verify the I2C connection.
69+
* Make sure the device is connected and responds as expected.
70+
* @return True if connection is valid, false otherwise
71+
*/
72+
bool HMC5883L_testConnection() {
73+
if (I2Cdev_readBytes(devAddr, HMC5883L_RA_ID_A, 3, buffer) == 0) {
74+
return (buffer[0] == 'H' && buffer[1] == '4' && buffer[2] == '3');
75+
}
76+
return false;
77+
}
78+
79+
// CONFIG_A register
80+
81+
/** Get number of samples averaged per measurement.
82+
* @return Current samples averaged per measurement (0-3 for 1/2/4/8 respectively)
83+
* @see HMC5883L_AVERAGING_8
84+
* @see HMC5883L_RA_CONFIG_A
85+
* @see HMC5883L_CRA_AVERAGE_BIT
86+
* @see HMC5883L_CRA_AVERAGE_LENGTH
87+
*/
88+
uint8_t HMC5883L_getSampleAveraging() {
89+
I2Cdev_readBits(devAddr, HMC5883L_RA_CONFIG_A, HMC5883L_CRA_AVERAGE_BIT, HMC5883L_CRA_AVERAGE_LENGTH, buffer);
90+
return buffer[0];
91+
}
92+
/** Set number of samples averaged per measurement.
93+
* @param averaging New samples averaged per measurement setting(0-3 for 1/2/4/8 respectively)
94+
* @see HMC5883L_RA_CONFIG_A
95+
* @see HMC5883L_CRA_AVERAGE_BIT
96+
* @see HMC5883L_CRA_AVERAGE_LENGTH
97+
*/
98+
void HMC5883L_setSampleAveraging(uint8_t averaging) {
99+
I2Cdev_writeBits(devAddr, HMC5883L_RA_CONFIG_A, HMC5883L_CRA_AVERAGE_BIT, HMC5883L_CRA_AVERAGE_LENGTH, averaging);
100+
}
101+
/** Get data output rate value.
102+
* The Table below shows all selectable output rates in continuous measurement
103+
* mode. All three channels shall be measured within a given output rate. Other
104+
* output rates with maximum rate of 160 Hz can be achieved by monitoring DRDY
105+
* interrupt pin in single measurement mode.
106+
*
107+
* Value | Typical Data Output Rate (Hz)
108+
* ------+------------------------------
109+
* 0 | 0.75
110+
* 1 | 1.5
111+
* 2 | 3
112+
* 3 | 7.5
113+
* 4 | 15 (Default)
114+
* 5 | 30
115+
* 6 | 75
116+
* 7 | Not used
117+
*
118+
* @return Current rate of data output to registers
119+
* @see HMC5883L_RATE_15
120+
* @see HMC5883L_RA_CONFIG_A
121+
* @see HMC5883L_CRA_RATE_BIT
122+
* @see HMC5883L_CRA_RATE_LENGTH
123+
*/
124+
uint8_t HMC5883 10000 L_getDataRate() {
125+
I2Cdev_readBits(devAddr, HMC5883L_RA_CONFIG_A, HMC5883L_CRA_RATE_BIT, HMC5883L_CRA_RATE_LENGTH, buffer);
126+
return buffer[0];
127+
}
128+
/** Set data output rate value.
129+
* @param rate Rate of data output to registers
130+
* @see getDataRate()
131+
* @see HMC5883L_RATE_15
132+
* @see HMC5883L_RA_CONFIG_A
133+
* @see HMC5883L_CRA_RATE_BIT
134+
* @see HMC5883L_CRA_RATE_LENGTH
135+
*/
136+
void HMC5883L_setDataRate(uint8_t rate) {
137+
I2Cdev_writeBits(devAddr, HMC5883L_RA_CONFIG_A, HMC5883L_CRA_RATE_BIT, HMC5883L_CRA_RATE_LENGTH, rate);
138+
}
139+
/** Get measurement bias value.
140+
* @return Current bias value (0-2 for normal/positive/negative respectively)
141+
* @see HMC5883L_BIAS_NORMAL
142+
* @see HMC5883L_RA_CONFIG_A
143+
* @see HMC5883L_CRA_BIAS_BIT
144+
* @see HMC5883L_CRA_BIAS_LENGTH
145+
*/
146+
uint8_t HMC5883L_getMeasurementBias() {
147+
I2Cdev_readBits(devAddr, HMC5883L_RA_CONFIG_A, HMC5883L_CRA_BIAS_BIT, HMC5883L_CRA_BIAS_LENGTH, buffer);
148+
return buffer[0];
149+
}
150+
/** Set measurement bias value.
151+
* @param bias New bias value (0-2 for normal/positive/negative respectively)
152+
* @see HMC5883L_BIAS_NORMAL
153+
* @see HMC5883L_RA_CONFIG_A
154+
* @see HMC5883L_CRA_BIAS_BIT
155+
* @see HMC5883L_CRA_BIAS_LENGTH
156+
*/
157+
void HMC5883L_setMeasurementBias(uint8_t bias) {
158+
I2Cdev_writeBits(devAddr, HMC5883L_RA_CONFIG_A, HMC5883L_CRA_BIAS_BIT, HMC5883L_CRA_BIAS_LENGTH, bias);
159+
}
160+
161+
// CONFIG_B register
162+
163+
/** Get magnetic field gain value.
164+
* The table below shows nominal gain settings. Use the "Gain" column to convert
165+
* counts to Gauss. Choose a lower gain value (higher GN#) when total field
166+
* strength causes overflow in one of the data output registers (saturation).
167+
* The data output range for all settings is 0xF800-0x07FF (-2048 - 2047).
168+
*
169+
* Value | Field Range | Gain (LSB/Gauss)
170+
* ------+-------------+-----------------
171+
* 0 | +/- 0.88 Ga | 1370
172+
* 1 | +/- 1.3 Ga | 1090 (Default)
173+
* 2 | +/- 1.9 Ga | 820
174+
* 3 | +/- 2.5 Ga | 660
175+
* 4 | +/- 4.0 Ga | 440
176+
* 5 | +/- 4.7 Ga | 390
177+
* 6 | +/- 5.6 Ga | 330
178+
* 7 | +/- 8.1 Ga | 230
179+
*
180+
* @return Current magnetic field gain value
181+
* @see HMC5883L_GAIN_1090
182+
* @see HMC5883L_RA_CONFIG_B
183+
* @see HMC5883L_CRB_GAIN_BIT
184+
* @see HMC5883L_CRB_GAIN_LENGTH
185+
*/
186+
uint8_t HMC5883L_getGain() {
187+
I2Cdev_readBits(devAddr, HMC5883L_RA_CONFIG_B, HMC5883L_CRB_GAIN_BIT, HMC5883L_CRB_GAIN_LENGTH, buffer);
188+
return buffer[0];
189+
}
190+
/** Set magnetic field gain value.
191+
* @param gain New magnetic field gain value
192+
* @see getGain()
193+
* @see HMC5883L_RA_CONFIG_B
194+
* @see HMC5883L_CRB_GAIN_BIT
195+
* @see HMC5883L_CRB_GAIN_LENGTH
196+
*/
197+
void HMC5883L_setGain(uint8_t gain) {
198+
// use this method to guarantee that bits 4-0 are set to zero, which is a
199+
// requirement specified in the datasheet; it's actually more efficient than
200+
// using the I2Cdev.writeBits method
201+
I2Cdev_writeByte(devAddr, HMC5883L_RA_CONFIG_B, gain << (HMC5883L_CRB_GAIN_BIT - HMC5883L_CRB_GAIN_LENGTH + 1));
202+
}
203+
204+
// MODE register
205+
206+
/** Get measurement mode.
207+
* In continuous-measurement mode, the device continuously performs measurements
208+
* and places the result in the data register. RDY goes high when new data is
209+
* placed in all three registers. After a power-on or a write to the mode or
210+
* configuration register, the first measurement set is available from all three
211+
* data output registers after a period of 2/fDO and subsequent measurements are
212+
* available at a frequency of fDO, where fDO is the frequency of data output.
213+
*
214+
* When single-measurement mode (default) is selected, device performs a single
215+
* measurement, sets RDY high and returned to idle mode. Mode register returns
216+
* to idle mode bit values. The measurement remains in the data output register
217+
* and RDY remains high until the data output register is read or another
218+
* measurement is performed.
219+
*
220+
* @return Current measurement mode
221+
* @see HMC5883L_MODE_CONTINUOUS
222+
* @see HMC5883L_MODE_SINGLE
223+
* @see HMC5883L_MODE_IDLE
224+
* @see HMC5883L_RA_MODE
225+
* @see HMC5883L_MODEREG_BIT
226+
* @see HMC5883L_MODEREG_LENGTH
227+
*/
228+
uint8_t HMC5883L_getMode() {
229+
I2Cdev_readBits(devAddr, HMC5883L_RA_MODE, HMC5883L_MODEREG_BIT, HMC5883L_MODEREG_LENGTH, buffer);
230+
return buffer[0];
231+
}
232+
/** Set measurement mode.
233+
* @param newMode New measurement mode
234+
* @see getMode()
235+
* @see HMC5883L_MODE_CONTINUOUS
236+
* @see HMC5883L_MODE_SINGLE
237+
* @see HMC5883L_MODE_IDLE
238+
* @see HMC5883L_RA_MODE
239+
* @see HMC5883L_MODEREG_BIT
240+
* @see HMC5883L_MODEREG_LENGTH
241+
*/
242+
void HMC5883L_setMode(uint8_t newMode) {
243+
// use this method to guarantee that bits 7-2 are set to zero, which is a
244+
// requirement specified in the datasheet; it's actually more efficient than
245+
// using the I2Cdev.writeBits method
246+
I2Cdev_writeByte(devAddr, HMC5883L_RA_MODE, newMode << (HMC5883L_MODEREG_BIT - HMC5883L_MODEREG_LENGTH + 1));
247+
mode = newMode; // track to tell if we have to clear bit 7 after a read
248+
}
249+
250+
// DATA* registers
251+
252+
/** Get 3-axis heading measurements.
253+
* In the event the ADC reading overflows or underflows for the given channel,
254+
* or if there is a math overflow during the bias measurement, this data
255+
* register will contain the value -4096. This register value will clear when
256+
* after the next valid measurement is made. Note that this method automatically
257+
* clears the appropriate bit in the MODE register if Single mode is active.
258+
* @param x 16-bit signed integer container for X-axis heading
259+
* @param y 16-bit signed integer container for Y-axis heading
260+
* @param z 16-bit signed integer container for Z-axis heading
261+
* @see HMC5883L_RA_DATAX_H
262+
*/
263+
void HMC5883L_getHeading(int16_t *x, int16_t *y, int16_t *z) {
264+
I2Cdev_readBytes(devAddr, HMC5883L_RA_DATAX_H, 6, buffer);
265+
if (mode == HMC5883L_MODE_SINGLE) I2Cdev_writeByte(devAddr, HMC5883L_RA_MODE, HMC5883L_MODE_SINGLE << (HMC5883L_MODEREG_BIT - HMC5883L_MODEREG_LENGTH + 1));
266+
*x = (((int16_t)buffer[0]) << 8) | buffer[1];
267+
*y = (((int16_t)buffer[4]) << 8) | buffer[5];
268+
*z = (((int16_t)buffer[2]) << 8) | buffer[3];
269+
}
270+
/** Get X-axis heading measurement.
271+
* @return 16-bit signed integer with X-axis heading
272+
* @see HMC5883L_RA_DATAX_H
273+
*/
274+
int16_t HMC5883L_getHeadingX() {
275+
// each axis read requires that ALL axis registers be read, even if only
276+
// one is used; this was not done ineffiently in the code by accident
277+
I2Cdev_readBytes(devAddr, HMC5883L_RA_DATAX_H, 6, buffer);
278+
if (mode == HMC5883L_MODE_SINGLE) I2Cdev_writeByte(devAddr, HMC5883L_RA_MODE, HMC5883L_MODE_SINGLE << (HMC5883L_MODEREG_BIT - HMC5883L_MODEREG_LENGTH + 1));
279+
return (((int16_t)buffer[0]) << 8) | buffer[1];
280+
}
281+
/** Get Y-axis heading measurement.
282+
* @return 16-bit signed integer with Y-axis heading
283+
* @see HMC5883L_RA_DATAY_H
284+
*/
285+
int16_t HMC5883L_getHeadingY() {
286+
// each axis read requires that ALL axis registers be read, even if only
287+
// one is used; this was not done ineffiently in the code by accident
288+
I2Cdev_readBytes(devAddr, HMC5883L_RA_DATAX_H, 6, buffer);
289+
if (mode == HMC5883L_MODE_SINGLE) I2Cdev_writeByte(devAddr, HMC5883L_RA_MODE, HMC5883L_MODE_SINGLE << (HMC5883L_MODEREG_BIT - HMC5883L_MODEREG_LENGTH + 1));
290+
return (((int16_t)buffer[4]) << 8) | buffer[5];
291+
}
292+
/** Get Z-axis heading measurement.
293+
* @return 16-bit signed integer with Z-axis heading
294+
* @see HMC5883L_RA_DATAZ_H
295+
*/
296+
int16_t HMC5883L_getHeadingZ() {
297+
// each axis read requires that ALL axis registers be read, even if only
298+
// one is used; this was not done ineffiently in the code by accident
299+
I2Cdev_readBytes(devAddr, HMC5883L_RA_DATAX_H, 6, buffer);
300+
if (mode == HMC5883L_MODE_SINGLE) I2Cdev_writeByte(devAddr, HMC5883L_RA_MODE, HMC5883L_MODE_SINGLE << (HMC5883L_MODEREG_BIT - HMC5883L_MODEREG_LENGTH + 1));
301+
return (((int16_t)buffer[2]) << 8) | buffer[3];
302+
}
303+
304+
// STATUS register
305+
306+
/** Get data output register lock status.
307+
* This bit is set when this some but not all for of the six data output
308+
* registers have been read. When this bit is set, the six data output registers
309+
* are locked and any new data will not be placed in these register until one of
310+
* three conditions are met: one, all six bytes have been read or the mode
311+
* changed, two, the mode is changed, or three, the measurement configuration is
312+
* changed.
313+
* @return Data output register lock status
314+
* @see HMC5883L_RA_STATUS
315+
* @see HMC5883L_STATUS_LOCK_BIT
316+
*/
317+
bool HMC5883L_getLockStatus() {
318+
I2Cdev_readBit(devAddr, HMC5883L_RA_STATUS, HMC5883L_STATUS_LOCK_BIT, buffer);
319+
return buffer[0];
320+
}
321+
/** Get data ready status.
322+
* This bit is set when data is written to all six data registers, and cleared
323+
* when the device initiates a write to the data output registers and after one
324+
* or more of the data output registers are written to. When RDY bit is clear it
325+
* shall remain cleared for 250 us. DRDY pin can be used as an alternative to
326+
* the status register for monitoring the device for measurement data.
327+
* @return Data ready status
328+
* @see HMC5883L_RA_STATUS
329+
* @see HMC5883L_STATUS_READY_BIT
330+
*/
331+
bool HMC5883L_getReadyStatus() {
332+
I2Cdev_readBit(devAddr, HMC5883L_RA_STATUS, HMC5883L_STATUS_READY_BIT, buffer);
333+
return buffer[0];
334+
}
335+
336+
// ID_* registers
337+
338+
/** Get identification byte A
339+
* @return ID_A byte (should be 01001000, ASCII value 'H')
340+
*/
341+
uint8_t HMC5883L_getIDA() {
342+
I2Cdev_readByte(devAddr, HMC5883L_RA_ID_A, buffer);
343+
return buffer[0];
344+
}
345+
/** Get identification byte B
346+
* @return ID_A byte (should be 00110100, ASCII value '4')
347+
*/
348+
uint8_t HMC5883L_getIDB() {
349+
I2Cdev_readByte(devAddr, HMC5883L_RA_ID_B, buffer);
350+
return buffer[0];
351+
}
352+
/** Get identification byte C
353+
* @return ID_A byte (should be 00110011, ASCII value '3')
354+
*/
355+
uint8_t HMC5883L_getIDC() {
356+
I2Cdev_readByte(devAddr, HMC5883L_RA_ID_C, buffer);
357+
return buffer[0];
358+
}

0 commit comments

Comments
 (0)
0