Paparazzi UAS v7.1_unstable
Paparazzi is a free software Unmanned Aircraft System.
Loading...
Searching...
No Matches
qmc5883l.c
Go to the documentation of this file.
1/*
2 * Copyright (C) 2022 Paparazzi Team
3 *
4 * This file is part of paparazzi.
5 *
6 * paparazzi is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2, or (at your option)
9 * any later version.
10 *
11 * paparazzi is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with paparazzi; see the file COPYING. If not, see
18 * <http://www.gnu.org/licenses/>.
19 */
20
28
29/* Registers Axis X,Y,Z */
30#define QMC5883L_REG_DATXL 0x00
31#define QMC5883L_REG_DATXM 0x01
32#define QMC5883L_REG_DATYL 0x02
33#define QMC5883L_REG_DATYM 0x03
34#define QMC5883L_REG_DATZL 0x04
35#define QMC5883L_REG_DATZM 0x05
36
37/* Register I2C bus transaction Status */
38#define QMC5883L_REG_STATUS 0x06
39#define QMC5883L_STATUS_OVL 0x02
40
41/* Registers Temperature, relative thus not so useful ATM, therefore not implemented in reading */
42#define QMC5883L_REG_TEMPM 0x07
43#define QMC5883L_REG_TEMPL 0x08
44
45/* Registers Config */
46#define QMC5883L_REG_CONTROL_1 0x09 /* settings for MODE */
47#define QMC5883L_REG_CONTROL_2 0x0A /* settings for INT_ENB */
48#define QMC5883L_REG_RESET_PERIOD 0x0B
49
50#define QMC5883L_REG_IDC 0x0C /* OEM reserved */
51#define QMC5883L_REG_IDD 0x0D /* OEM reserved */
52
53/* Options for CONTROL_1 */
54#define QMC5883L_MODE_STBY 0x00
55#define QMC5883L_MODE_CONT 0x01
56
57/* Options for scale RaNGe(RNG) Gauss */
58#define QMC5883L_RNG_2G 0x00
59#define QMC5883L_RNG_8G 0x10
60
61/* options for Over-Sample Ratio (OSR) */
62#define QMC5883L_OSR_512 0x00 /* Use 512 if powerusage of chip is not an issue */
63#define QMC5883L_OSR_256 0x40
64#define QMC5883L_OSR_128 0x80
65#define QMC5883L_OSR_64 0xC0
66
67void qmc5883l_init(struct Qmc5883l *mag, struct i2c_periph *i2c_p, uint8_t addr, uint8_t data_rate)
68{
69 /* set i2c_peripheral */
70 mag->i2c_p = i2c_p;
71 /* set i2c address */
72 mag->i2c_trans.slave_addr = addr;
74 /* store data rate */
75 mag->data_rate = data_rate;
76 mag->initialized = false;
78 mag->data_available = false;
79}
80
82{
83 // Only configure when not busy
85 && mag->i2c_trans.status != I2CTransDone) {
86 return;
87 }
88
89 // Only when successful continue with next
90 if (mag->i2c_trans.status == I2CTransSuccess) {
91 mag->status++; //Here the Enum Counter goes to the next one
92 }
93
95 switch (mag->status) {
96
98 /* prepare config request */
100 mag->i2c_trans.buf[1] = 0x01;
101 /* send config request, ask for i2c frame for set/reset period */
102 i2c_transmit(mag->i2c_p, &(mag->i2c_trans), mag->i2c_trans.slave_addr, 2);
103 break;
104
108 i2c_transmit(mag->i2c_p, &(mag->i2c_trans), mag->i2c_trans.slave_addr, 2);
109 break;
110
114 i2c_transmit(mag->i2c_p, &(mag->i2c_trans), mag->i2c_trans.slave_addr, 2);
115 break;
116
119 mag->initialized = true;
120 break;
121
122 default:
123 break;
124 }
125}
126
127void qmc5883l_read(struct Qmc5883l *mag)
128{
129 if (mag->status != QMC5883L_STATUS_IDLE) {
130 return;
131 }
132
133 /* get 3 x 2 bytes data = 6 Bytes and one status byte = 7 */
140
141 /* Chip transaction status, not used ATM in case of driver mishap once can considder using it */
143 // Add some code if you experience reading issue
144 // DRDY = ((mag->i2c_trans.buf[6]) >> 0) & 1;
145 // OVL = ((mag->i2c_trans.buf[6]) >> 1) & 1;
146 // DOR = ((uint8_t)(mag->i2c_trans.buf[6]) >> 2) & 1;
147
148 i2c_transceive(mag->i2c_p, &(mag->i2c_trans), mag->i2c_trans.slave_addr, 1, 7);
150}
151/* Convert and align raw values */
152#define Int16FromBuf(_buf,_idx) ((int16_t)(_buf[_idx] | (_buf[_idx+1] << 8)))
153
154
155void qmc5883l_event(struct Qmc5883l *mag)
156{
157 if (!mag->initialized) {
158 return;
159 }
160
161 switch (mag->status) {
162
164 if (mag->i2c_trans.status == I2CTransSuccess) {
165 mag->data.vect.x = Int16FromBuf(mag->i2c_trans.buf, 0);
166 mag->data.vect.y = Int16FromBuf(mag->i2c_trans.buf, 2);
167 mag->data.vect.z = Int16FromBuf(mag->i2c_trans.buf, 4);
168
169 const uint8_t sample_status = mag->i2c_trans.buf[6];
171 /* End of measure reading, go back to idle */
173 }
174 else if (mag->i2c_trans.status == I2CTransFailed) {
176 }
177 break;
178
179 default:
181 /* Per default set to idle */
184 }
185 break;
186 }
187}
188
volatile uint8_t buf[I2C_BUF_LEN]
Transaction buffer With I2C_BUF_LEN number of bytes.
Definition i2c.h:123
enum I2CTransactionStatus status
Transaction status.
Definition i2c.h:127
uint8_t slave_addr
Slave address.
Definition i2c.h:105
bool i2c_transmit(struct i2c_periph *p, struct i2c_transaction *t, uint8_t s_addr, uint8_t len)
Submit a write only transaction.
Definition i2c.c:202
bool i2c_transceive(struct i2c_periph *p, struct i2c_transaction *t, uint8_t s_addr, uint8_t len_w, uint16_t len_r)
Submit a write/read transaction.
Definition i2c.c:222
@ I2CTransSuccess
transaction successfully finished by I2C driver
Definition i2c.h:58
@ I2CTransFailed
transaction failed
Definition i2c.h:59
@ I2CTransDone
transaction set to done by user level
Definition i2c.h:60
uint16_t foo
Definition main_demo5.c:58
void qmc5883l_event(struct Qmc5883l *mag)
Definition qmc5883l.c:155
#define QMC5883L_REG_DATZM
Definition qmc5883l.c:35
#define QMC5883L_STATUS_OVL
Definition qmc5883l.c:39
void qmc5883l_read(struct Qmc5883l *mag)
Definition qmc5883l.c:127
#define QMC5883L_RNG_8G
Definition qmc5883l.c:59
#define QMC5883L_MODE_CONT
Definition qmc5883l.c:55
#define QMC5883L_REG_CONTROL_1
Definition qmc5883l.c:46
#define QMC5883L_REG_STATUS
Definition qmc5883l.c:38
#define QMC5883L_REG_DATZL
Definition qmc5883l.c:34
#define QMC5883L_REG_DATXM
Definition qmc5883l.c:31
#define QMC5883L_REG_DATYM
Definition qmc5883l.c:33
#define QMC5883L_OSR_512
Definition qmc5883l.c:62
#define QMC5883L_REG_DATXL
Definition qmc5883l.c:30
#define QMC5883L_REG_DATYL
Definition qmc5883l.c:32
void qmc5883l_configure(struct Qmc5883l *mag)
Definition qmc5883l.c:81
#define QMC5883L_REG_RESET_PERIOD
Definition qmc5883l.c:48
void qmc5883l_init(struct Qmc5883l *mag, struct i2c_periph *i2c_p, uint8_t addr, uint8_t data_rate)
Definition qmc5883l.c:67
#define Int16FromBuf(_buf, _idx)
Definition qmc5883l.c:152
QST QMC5883L 3-axis magnetometer driver interface (I2C).
volatile bool data_available
data ready flag
Definition qmc5883l.h:66
@ QMC5883L_CONF_CCR_DONE
Definition qmc5883l.h:53
@ QMC5883L_CONF_TMRC_DONE
Definition qmc5883l.h:54
@ QMC5883L_STATUS_MEAS
Definition qmc5883l.h:57
@ QMC5883L_STATUS_IDLE
Definition qmc5883l.h:56
@ QMC5883L_CONF_UNINIT
Definition qmc5883l.h:52
@ QMC5883L_CONF_CCM_DONE
Definition qmc5883l.h:55
union Qmc5883l::@355 data
struct i2c_periph * i2c_p
peripheral used for communcation
Definition qmc5883l.h:61
enum Qmc5883lStatus status
init status
Definition qmc5883l.h:65
bool initialized
config done flag
Definition qmc5883l.h:64
struct i2c_transaction i2c_trans
i2c transaction
Definition qmc5883l.h:62
uint8_t data_rate
sensor data rate
Definition qmc5883l.h:63
unsigned char uint8_t
Typedef defining 8 bit unsigned char type.