amiro-lld / source / alld_hmc5883l.c @ 11500ad8
History | View | Annotate | Download (8.813 KB)
1 |
/*
|
---|---|
2 |
AMiRo-LLD is a compilation of low-level hardware drivers for the Autonomous Mini Robot (AMiRo) platform.
|
3 |
Copyright (C) 2016..2018 Thomas Schöpping et al.
|
4 |
|
5 |
This program is free software: you can redistribute it and/or modify
|
6 |
it under the terms of the GNU Lesser General Public License as published by
|
7 |
the Free Software Foundation, either version 3 of the License, or
|
8 |
(at your option) any later version.
|
9 |
|
10 |
This program is distributed in the hope that it will be useful,
|
11 |
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
12 |
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
13 |
GNU Lesser General Public License for more details.
|
14 |
|
15 |
You should have received a copy of the GNU Lesser General Public License
|
16 |
along with this program. If not, see <http://www.gnu.org/licenses/>.
|
17 |
*/
|
18 |
|
19 |
/**
|
20 |
* @file
|
21 |
* @brief Compass function implementations.
|
22 |
*
|
23 |
* @addtogroup lld_compass
|
24 |
* @{
|
25 |
*/
|
26 |
|
27 |
#include <alld_hmc5883l.h> |
28 |
|
29 |
#if defined(AMIROLLD_CFG_USE_HMC5883L) || defined(__DOXYGEN__)
|
30 |
|
31 |
#include <string.h> |
32 |
|
33 |
/**
|
34 |
* @brief Reads the registers starting from idetification register A.
|
35 |
* @param[in] hmcd The HMC5883L driver to use.
|
36 |
* @param[out] rxbuffer The data read from the identification registers.
|
37 |
* @param[in] num Number of registers to read.
|
38 |
* @param[in] timeout Timeout for the function to return (in microseconds).
|
39 |
*
|
40 |
* @return The return status indicates whether the function call was successfull or a timeout occured.
|
41 |
*/
|
42 |
inline apalExitStatus_t
|
43 |
hmc5883l_lld_check(const HMC5883LDriver* const hmcd, uint8_t* const rxbuffer, const uint8_t num, const apalTime_t timeout) |
44 |
{ |
45 |
apalDbgAssert(hmcd != NULL && hmcd->i2cd != NULL); |
46 |
apalDbgAssert(rxbuffer != NULL);
|
47 |
|
48 |
uint8_t txbuffer = HMC5883L_LLD_REGISTER_IDENTIFICATION_A; |
49 |
return apalI2CMasterTransmit(hmcd->i2cd,HMC5883L_LLD_I2C_ADDR,&txbuffer,1,rxbuffer,num,timeout); |
50 |
} |
51 |
|
52 |
/**
|
53 |
* @brief Write data to consecutive registers.
|
54 |
* @param[in] hmcd The HMC5883L driver to use.
|
55 |
* @param[in] regaddr Address of first register.
|
56 |
* @param[in] data Bytes to write to the registers.
|
57 |
* @param[in] num Number of registers to write.
|
58 |
* @param[in] timeout Timeout for the function to return (in microseconds).
|
59 |
*
|
60 |
* @return The return status indicates whether the function call was successfull or a timeout occured.
|
61 |
*/
|
62 |
inline apalExitStatus_t
|
63 |
hmc5883l_lld_write_register(const HMC5883LDriver* const hmcd, const hmc5883l_lld_register_t regaddr, const uint8_t* const data, const uint8_t num, const apalTime_t timeout) |
64 |
{ |
65 |
apalDbgAssert(hmcd != NULL && hmcd->i2cd != NULL); |
66 |
apalDbgAssert(data != NULL);
|
67 |
|
68 |
uint8_t buffer[num+1];
|
69 |
buffer[0] = regaddr;
|
70 |
memcpy(buffer+1, data, num);
|
71 |
return apalI2CMasterTransmit(hmcd->i2cd,HMC5883L_LLD_I2C_ADDR,buffer,num+1,NULL,0,timeout); |
72 |
} |
73 |
|
74 |
/**
|
75 |
* @brief Write data to one registers.
|
76 |
* @param[in] hmcd The HMC5883L driver to use.
|
77 |
* @param[in] regaddr Address of the register.
|
78 |
* @param[in] data Data to write ti the register.
|
79 |
* @param[in] timeout Timeout for the function to return (in microseconds).
|
80 |
*
|
81 |
* @return The return status indicates whether the function call was successfull or a timeout occured.
|
82 |
*/
|
83 |
inline apalExitStatus_t
|
84 |
hmc5883l_lld_set_register(const HMC5883LDriver* const hmcd, const hmc5883l_lld_register_t regaddr, const uint8_t data, const apalTime_t timeout) |
85 |
{ |
86 |
apalDbgAssert(hmcd != NULL && hmcd->i2cd != NULL); |
87 |
|
88 |
uint8_t buffer[2];
|
89 |
buffer[0] = regaddr;
|
90 |
buffer[1] = data;
|
91 |
return apalI2CMasterTransmit(hmcd->i2cd,HMC5883L_LLD_I2C_ADDR,buffer,2,NULL,0,timeout); |
92 |
} |
93 |
|
94 |
/**
|
95 |
* @brief Read data from consecutive registers.
|
96 |
* @param[in] hmcd The HMC5883L driver to use.
|
97 |
* @param[in] regaddr Address of first register.
|
98 |
* @param[out] data Bytes to read from the registers.
|
99 |
* @param[in] num Number of registers to read.
|
100 |
* @param[in] timeout Timeout for the function to return (in microseconds).
|
101 |
*
|
102 |
* @return The return status indicates whether the function call was successfull or a timeout occured.
|
103 |
*/
|
104 |
inline apalExitStatus_t
|
105 |
hmc5883l_lld_read_register(const HMC5883LDriver* const hmcd, const hmc5883l_lld_register_t regaddr, uint8_t* const data, const uint8_t num, const apalTime_t timeout) |
106 |
{ |
107 |
apalDbgAssert(hmcd != NULL && hmcd->i2cd != NULL); |
108 |
apalDbgAssert(data != NULL);
|
109 |
|
110 |
return apalI2CMasterTransmit(hmcd->i2cd,HMC5883L_LLD_I2C_ADDR,(uint8_t*)®addr,1,data,num,timeout); |
111 |
} |
112 |
|
113 |
/**
|
114 |
* @brief Read sensor data.
|
115 |
* @param[in] hmcd The HMC5883L driver to use.
|
116 |
* @param[out] data Sensor data.
|
117 |
* @param[in] timeout Timeout for the function to return (in microseconds).
|
118 |
*
|
119 |
* @return The return status indicates whether the function call was successfull or a timeout occured.
|
120 |
*/
|
121 |
inline apalExitStatus_t
|
122 |
hmc5883l_lld_read_data(const HMC5883LDriver* const hmcd, uint16_t* const data, const apalTime_t timeout) |
123 |
{ |
124 |
apalDbgAssert(data != NULL);
|
125 |
|
126 |
uint8_t buffer[6];
|
127 |
apalExitStatus_t status = hmc5883l_lld_read_register(hmcd, HMC5883L_LLD_REGISTER_DATA_OUT_X_MSB, buffer, 6, timeout);
|
128 |
data[0] = ((uint16_t)buffer[0] << 8) | buffer[1]; |
129 |
data[1] = ((uint16_t)buffer[2] << 8) | buffer[3]; |
130 |
data[2] = ((uint16_t)buffer[4] << 8) | buffer[5]; |
131 |
for (uint8_t dataIdx = 0; dataIdx < 3; dataIdx++) { |
132 |
if (data[dataIdx] >> 15) { |
133 |
data[dataIdx] = -1 * ~(data[dataIdx] - 1); |
134 |
} |
135 |
} |
136 |
return status;
|
137 |
} |
138 |
|
139 |
/**
|
140 |
* @brief Read the status register.
|
141 |
* @param[in] hmcd The HMC5883L driver to use.
|
142 |
* @param[out] status Content of status register,
|
143 |
* @param[in] timeout Timeout for the function to return (in microseconds).
|
144 |
*
|
145 |
* @return The return status indicates whether the function call was successfull or a timeout occured.
|
146 |
*/
|
147 |
inline apalExitStatus_t
|
148 |
hmc5883l_lld_read_status(const HMC5883LDriver* const hmcd, uint8_t* const status, const apalTime_t timeout) |
149 |
{ |
150 |
apalDbgAssert(status != NULL);
|
151 |
|
152 |
return hmc5883l_lld_read_register(hmcd, HMC5883L_LLD_REGISTER_STATUS, status, 1, timeout); |
153 |
} |
154 |
|
155 |
/**
|
156 |
* @brief Read the configuration of the hmc5883l.
|
157 |
* @param[in] hmcd The HMC5883L driver to use.
|
158 |
* @param[out] cfg Structure representing the current configuration.
|
159 |
* @param[in] timeout Timeout for the function to return (in microseconds).
|
160 |
*
|
161 |
* @return The return status indicates whether the function call was successfull or a timeout occured.
|
162 |
*/
|
163 |
inline apalExitStatus_t
|
164 |
hmc5883l_lld_read_config(const HMC5883LDriver* const hmcd, hmc5883l_lld_config_t* const cfg, const apalTime_t timeout) |
165 |
{ |
166 |
apalDbgAssert(cfg != NULL);
|
167 |
|
168 |
uint8_t conf[3];
|
169 |
apalExitStatus_t status = hmc5883l_lld_read_register(hmcd, HMC5883L_LLD_REGISTER_CONFIG_A, conf, 3, timeout);
|
170 |
cfg->avg = conf[0] & (3 << 5); |
171 |
cfg->outrate = conf[0] & (7 << 2); |
172 |
cfg->mbias = conf[0] & 3; |
173 |
cfg->gain = conf[1] & (7 << 5); |
174 |
cfg->highspeed = conf[2] >> 7; |
175 |
cfg->mode = conf[2] & 3; |
176 |
return status;
|
177 |
} |
178 |
|
179 |
/**
|
180 |
* @brief Write a configuration to the hmc5883l.
|
181 |
* @param[in] hmcd The HMC5883L driver to use.
|
182 |
* @param[in] cfg Structure representing the new configuration.
|
183 |
* @param[in] timeout Timeout for the function to return (in microseconds).
|
184 |
*
|
185 |
* @return The return status indicates whether the function call was successfull or a timeout occured.
|
186 |
*/
|
187 |
inline apalExitStatus_t
|
188 |
hmc5883l_lld_write_config(const HMC5883LDriver* const hmcd, const hmc5883l_lld_config_t cfg, const apalTime_t timeout) |
189 |
{ |
190 |
uint8_t conf[3];
|
191 |
conf[0] = cfg.avg | cfg.outrate | cfg.mbias;
|
192 |
conf[1] = cfg.gain;
|
193 |
conf[2] = (cfg.highspeed << 7) | cfg.mode; |
194 |
return hmc5883l_lld_write_register(hmcd, HMC5883L_LLD_REGISTER_CONFIG_A, conf, 3, timeout); |
195 |
} |
196 |
|
197 |
/**
|
198 |
* @brief Read the lock bit of the status register.
|
199 |
* @param[in] hmcd The HMC5883L driver to use.
|
200 |
* @param[in] lock Status of the lock bit.
|
201 |
* @param[in] timeout Timeout for the function to return (in microseconds).
|
202 |
*
|
203 |
* @return The return status indicates whether the function call was successfull or a timeout occured.
|
204 |
*/
|
205 |
inline apalExitStatus_t
|
206 |
hmc5883l_lld_read_lock(const HMC5883LDriver* const hmcd, uint8_t* const lock, const apalTime_t timeout) |
207 |
{ |
208 |
apalDbgAssert(lock != NULL);
|
209 |
|
210 |
apalExitStatus_t status = hmc5883l_lld_read_register(hmcd, HMC5883L_LLD_REGISTER_STATUS, lock, 1, timeout);
|
211 |
*lock >>= 1;
|
212 |
return status;
|
213 |
} |
214 |
|
215 |
/**
|
216 |
* @brief Read the rdy bit of the status register.
|
217 |
* @param[in] hmcd The HMC5883L driver to use.
|
218 |
* @param[in] rdy Status of the rdy bit.
|
219 |
* @param[in] timeout Timeout for the function to return (in microseconds).
|
220 |
*
|
221 |
* @return The return status indicates whether the function call was successfull or a timeout occured.
|
222 |
*/
|
223 |
inline apalExitStatus_t
|
224 |
hmc5883l_lld_read_rdy(const HMC5883LDriver* const hmcd, uint8_t* const rdy, const apalTime_t timeout) |
225 |
{ |
226 |
apalDbgAssert(rdy != NULL);
|
227 |
|
228 |
apalExitStatus_t status = hmc5883l_lld_read_register(hmcd, HMC5883L_LLD_REGISTER_STATUS, rdy, 1, timeout);
|
229 |
*rdy &= 1;
|
230 |
return status;
|
231 |
} |
232 |
|
233 |
#endif /* defined(AMIROLLD_CFG_USE_HMC5883L) */ |
234 |
|
235 |
/** @} */
|