Skip to content

Commit 8901f7e

Browse files
authored
Merge pull request #774 from mzltn/tp_QMC5883L
Add driver for QMC5883L magnetometer
2 parents 19c420b + 6956393 commit 8901f7e

2 files changed

Lines changed: 160 additions & 0 deletions

File tree

STM32/QMC5883L/QMC5883L.c

Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
// I2Cdev library collection - QMC5883L I2C device class header file
2+
// Based on QST QMC5883L datasheet 1.0, 02/2016
3+
/* ============================================
4+
I2Cdev device library code is placed under the MIT license
5+
Copyright (c) 2011 Jeff Rowberg
6+
7+
Permission is hereby granted, free of charge, to any person obtaining a copy
8+
of this software and associated documentation files (the "Software"), to deal
9+
in the Software without restriction, including without limitation the rights
10+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
11+
copies of the Software, and to permit persons to whom the Software is
12+
furnished to do so, subject to the following conditions:
13+
14+
The above copyright notice and this permission notice shall be included in
15+
all copies or substantial portions of the Software.
16+
17+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
20+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
21+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
22+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
23+
THE SOFTWARE.
24+
===============================================
25+
*/
26+
#include <QMC5883L.h>
27+
#include <I2Cdev.h>
28+
29+
static const uint8_t chip_addr = QMC5883L_DEFAULT_ADDR;
30+
31+
bool QMC5883L_soft_reset() {
32+
if (I2Cdev_writeByte(chip_addr, QMC5883L_REG_CTRL_2, 0x80) == 0) return false;
33+
return true;
34+
}
35+
36+
bool QMC5883L_fbr_set(uint8_t fbr) {
37+
if (I2Cdev_writeByte(chip_addr, QMC5883L_REG_SR_PERIOD, fbr) == 0) return false;
38+
return true;
39+
}
40+
41+
bool QMC5883L_control_1_set(uint8_t value) {
42+
if (I2Cdev_writeByte(chip_addr, QMC5883L_REG_CTRL_1, value) == 0) return false;
43+
return true;
44+
}
45+
46+
bool QMC5883L_statusGet(uint8_t *data) {
47+
if (I2Cdev_readByte(chip_addr, QMC5883L_REG_STATUS, data) == 0) return false;
48+
return true;
49+
}
50+
51+
bool QMC5883L_magGet(int16_t *v_mag) {
52+
uint8_t bytes[6];
53+
if (I2Cdev_readBytes(chip_addr, QMC5883L_REG_DATA_X_LSB, 6, bytes) == 0) return false;
54+
55+
v_mag[0] = (((uint16_t)bytes[1]) << 8) | bytes[0];
56+
v_mag[1] = (((uint16_t)bytes[3]) << 8) | bytes[2];
57+
v_mag[2] = (((uint16_t)bytes[5]) << 8) | bytes[4];
58+
59+
return true;
60+
}
61+
62+
bool QMC5883L_tempGet(int16_t *temp) {
63+
uint8_t bytes[2];
64+
if (I2Cdev_readBytes(chip_addr, QMC5883L_REG_TEMP_LSB, 2, bytes) == 0) return false;
65+
66+
*temp = (((uint16_t)bytes[1]) << 8) | bytes[0];
67+
68+
return true;
69+
}

STM32/QMC5883L/QMC5883L.h

Lines changed: 91 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,91 @@
1+
// I2Cdev library collection - QMC5883L I2C device class header file
2+
// Based on QST QMC5883L datasheet 1.0, 02/2016
3+
/* ============================================
4+
I2Cdev device library code is placed under the MIT license
5+
Copyright (c) 2011 Jeff Rowberg
6+
7+
Permission is hereby granted, free of charge, to any person obtaining a copy
8+
of this software and associated documentation files (the "Software"), to deal
9+
in the Software without restriction, including without limitation the rights
10+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
11+
copies of the Software, and to permit persons to whom the Software is
12+
furnished to do so, subject to the following conditions:
13+
14+
The above copyright notice and this permission notice shall be included in
15+
all copies or substantial portions of the Software.
16+
17+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
20+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
21+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
22+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
23+
THE SOFTWARE.
24+
===============================================
25+
*/
26+
27+
#ifndef _QMC5883L_H_
28+
#define _QMC5883L_H_
29+
30+
#include <stdint.h>
31+
#include <stdbool.h>
32+
33+
#define QMC5883L_DEFAULT_ADDR 0x0D
34+
35+
/* register addresses */
36+
#define QMC5883L_REG_DATA_X_LSB 0x00
37+
#define QMC5883L_REG_DATA_X_MSB 0x01
38+
#define QMC5883L_REG_DATA_Y_LSB 0x02
39+
#define QMC5883L_REG_DATA_Y_MSB 0x03
40+
#define QMC5883L_REG_DATA_Z_LSB 0x04
41+
#define QMC5883L_REG_DATA_Z_MSB 0x05
42+
#define QMC5883L_REG_STATUS 0x06
43+
#define QMC5883L_REG_TEMP_LSB 0x07
44+
#define QMC5883L_REG_TEMP_MSB 0x08
45+
#define QMC5883L_REG_CTRL_1 0x09
46+
#define QMC5883L_REG_CTRL_2 0x0A
47+
#define QMC5883L_REG_SR_PERIOD 0x0B
48+
49+
/* values for control_1 register */
50+
#define QMC5883L_OVERSAMPLE_512 0b00
51+
#define QMC5883L_OVERSAMPLE_256 0b01
52+
#define QMC5883L_OVERSAMPLE_128 0b10
53+
#define QMC5883L_OVERSAMPLE_64 0b11
54+
55+
#define QMC5883L_SCALE_2G 0b00
56+
#define QMC5883L_SCALE_8G 0b01
57+
58+
#define QMC5883L_OUTPUT_RATE_10HZ 0b00
59+
#define QMC5883L_OUTPUT_RATE_50HZ 0b01
60+
#define QMC5883L_OUTPUT_RATE_100HZ 0b10
61+
#define QMC5883L_OUTPUT_RATE_200HZ 0b11
62+
63+
#define QMC5883L_MODE_STBY 0b00
64+
#define QMC5883L_MODE_CONT 0b01
65+
66+
#define QMC5883L_CTRL1_VALUE(_mode_, _output_rate_, _scale_, _oversample_) \
67+
((_mode_) | ((_output_rate_) << 2) | ((_scale_) << 4) | ((_oversample_) << 6))
68+
69+
/* QMC5883L_soft_reset: does a soft reset */
70+
bool QMC5883L_soft_reset();
71+
72+
/* QMC5883L_fbr_set: SET/RESET Period FBR; recommended value: 1 */
73+
bool QMC5883L_fbr_set(uint8_t fbr);
74+
75+
/* QMC5883L_control_1_set: set value for control register (mode, output rate, scale, oversampling) */
76+
bool QMC5883L_control_1_set(uint8_t value);
77+
78+
bool QMC5883L_statusGet(uint8_t *data);
79+
80+
/* QMC5883L_magGet:
81+
* parameters:
82+
* v_mag: int16_t array of length 3
83+
* returns:
84+
* success: true
85+
* failure: false
86+
*/
87+
bool QMC5883L_magGet(int16_t *v_mag);
88+
89+
bool QMC5883L_tempGet(int16_t *temp);
90+
91+
#endif

0 commit comments

Comments
 (0)