amiro-lld / source / L3G4200D / v1 / alld_L3G4200D_v1.c @ ef5bc242
History | View | Annotate | Download (11.324 KB)
1 |
/*
|
---|---|
2 |
AMiRo-LLD is a compilation of low-level hardware drivers for the Autonomous Mini Robot (AMiRo) platform.
|
3 |
Copyright (C) 2016..2019 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 alld_L3G4200D_v1.c
|
21 |
* @brief Gyroscope function implementations.
|
22 |
*
|
23 |
* @addtogroup lld_gyroscope
|
24 |
* @{
|
25 |
*/
|
26 |
|
27 |
#include<alld_L3G4200D.h> |
28 |
|
29 |
#if (defined(AMIROLLD_CFG_L3G4200D) && (AMIROLLD_CFG_L3G4200D == 1)) || defined(__DOXYGEN__) |
30 |
|
31 |
#include <string.h> |
32 |
|
33 |
/******************************************************************************/
|
34 |
/* LOCAL DEFINITIONS */
|
35 |
/******************************************************************************/
|
36 |
|
37 |
/******************************************************************************/
|
38 |
/* EXPORTED VARIABLES */
|
39 |
/******************************************************************************/
|
40 |
|
41 |
/******************************************************************************/
|
42 |
/* LOCAL TYPES */
|
43 |
/******************************************************************************/
|
44 |
|
45 |
/******************************************************************************/
|
46 |
/* LOCAL VARIABLES */
|
47 |
/******************************************************************************/
|
48 |
|
49 |
/******************************************************************************/
|
50 |
/* LOCAL FUNCTIONS */
|
51 |
/******************************************************************************/
|
52 |
|
53 |
/******************************************************************************/
|
54 |
/* EXPORTED FUNCTIONS */
|
55 |
/******************************************************************************/
|
56 |
|
57 |
/**
|
58 |
* @brief Read the content of one or more registers.
|
59 |
* @param[in] l3gd The L3G4200D driver to use.
|
60 |
* @param[in] regaddr The address of the first register.
|
61 |
* @param[out] data The data read from the registers.
|
62 |
* @param[in] length Number of registers to read.
|
63 |
*
|
64 |
* @return The return status indicates whether the function call was succesfull or a timeout occurred.
|
65 |
*/
|
66 |
apalExitStatus_t l3g4200d_lld_read_register(const L3G4200DDriver* const l3gd, const l3g4200d_lld_register_t regaddr, uint8_t* const data, const uint8_t length) |
67 |
{ |
68 |
apalDbgAssert(l3gd != NULL && l3gd->spid != NULL); |
69 |
apalDbgAssert(data != NULL);
|
70 |
|
71 |
uint8_t buffer[length+1];
|
72 |
buffer[0] = regaddr | L3G4200D_LLD_SPI_READ | ((length > 1) ? L3G4200D_LLD_SPI_MULT : 0); |
73 |
apalExitStatus_t status = apalSPIExchange(l3gd->spid, buffer, buffer, length+1);
|
74 |
memcpy(data, buffer+1, length);
|
75 |
return status;
|
76 |
} |
77 |
|
78 |
/**
|
79 |
* @brief Write to one or more registers.
|
80 |
* @param[in] l3gd The L3G4200D driver to use.
|
81 |
* @param[in] regaddr The address of the first register.
|
82 |
* @param[in] data The data to be written to the registers.
|
83 |
* @param[in] length Number of registers to write.
|
84 |
*
|
85 |
* @return The return status indicates whether the function call was succesfull or a timeout occurred.
|
86 |
*/
|
87 |
apalExitStatus_t l3g4200d_lld_write_register(const L3G4200DDriver* const l3gd, const l3g4200d_lld_register_t regaddr, const uint8_t* const data, const uint8_t length) |
88 |
{ |
89 |
apalDbgAssert(l3gd != NULL && l3gd->spid != NULL); |
90 |
apalDbgAssert(data != NULL);
|
91 |
|
92 |
uint8_t buffer[length+1];
|
93 |
buffer[0] = regaddr | L3G4200D_LLD_SPI_WRITE | ((length > 1) ? L3G4200D_LLD_SPI_MULT : 0); |
94 |
memcpy(buffer+1, data, length);
|
95 |
return apalSPITransmit(l3gd->spid, buffer, length+1); |
96 |
} |
97 |
|
98 |
/**
|
99 |
* @brief Read the sensor data of all 3 axes.
|
100 |
* @param[in] l3gd The L3G4200D driver to use.
|
101 |
* @param[out] data The sensor data.
|
102 |
* @param[in] cfg The current configuration. Needed to find out if the data is saved as little or big endian.
|
103 |
*
|
104 |
* @return The return status indicates whether the function call was succesfull or a timeout occurred.
|
105 |
*/
|
106 |
apalExitStatus_t l3g4200d_lld_read_all_data(const L3G4200DDriver* const l3gd, int16_t* const data, const l3g4200d_lld_cfg_t* const cfg) |
107 |
{ |
108 |
apalDbgAssert(data != NULL);
|
109 |
apalDbgAssert(cfg != NULL);
|
110 |
|
111 |
uint8_t buffer[6];
|
112 |
apalExitStatus_t status = l3g4200d_lld_read_register(l3gd, L3G4200D_LLD_REGISTER_OUT_X_L, buffer, 6);
|
113 |
if (!(cfg->registers.ctrl_reg4 & L3G4200D_LLD_BLE_MSB)) {
|
114 |
data[0] = (int16_t) (buffer[0] | (buffer[1] << 8)); |
115 |
data[1] = (int16_t) (buffer[2] | (buffer[3] << 8)); |
116 |
data[2] = (int16_t) (buffer[4] | (buffer[5] << 8)); |
117 |
} else {
|
118 |
data[0] = (int16_t) (buffer[1] | (buffer[0] << 8)); |
119 |
data[1] = (int16_t) (buffer[3] | (buffer[2] << 8)); |
120 |
data[2] = (int16_t) (buffer[5] | (buffer[4] << 8)); |
121 |
} |
122 |
return status;
|
123 |
} |
124 |
|
125 |
/**
|
126 |
* @brief Read the sensor data of one axis.
|
127 |
* @param[in] l3gd The L3G4200D driver to use.
|
128 |
* @param[out] data The sensor data.
|
129 |
* @param[in] axis The axis for which the data should be read.
|
130 |
* @param[in] cfg The current configuration. Needed to find out if the data is saved as little or big endian.
|
131 |
*
|
132 |
* @return The return status indicates whether the function call was succesfull or a timeout occurred.
|
133 |
*/
|
134 |
apalExitStatus_t l3g4200d_lld_read_data(const L3G4200DDriver* const l3gd, int16_t* const data, const l3g4200d_lld_axis_t axis, const l3g4200d_lld_cfg_t* const cfg) |
135 |
{ |
136 |
apalDbgAssert(data != NULL);
|
137 |
apalDbgAssert(cfg != NULL);
|
138 |
|
139 |
apalExitStatus_t status = APAL_STATUS_SUCCESS; |
140 |
uint8_t buffer[2];
|
141 |
switch (axis) {
|
142 |
case L3G4200D_LLD_X_AXIS:
|
143 |
status = l3g4200d_lld_read_register(l3gd, L3G4200D_LLD_REGISTER_OUT_X_L, buffer, 2);
|
144 |
break;
|
145 |
case L3G4200D_LLD_Y_AXIS:
|
146 |
status = l3g4200d_lld_read_register(l3gd, L3G4200D_LLD_REGISTER_OUT_Y_L, buffer, 2);
|
147 |
break;
|
148 |
case L3G4200D_LLD_Z_AXIS:
|
149 |
status = l3g4200d_lld_read_register(l3gd, L3G4200D_LLD_REGISTER_OUT_Z_L, buffer, 2);
|
150 |
break;
|
151 |
default:
|
152 |
return APAL_STATUS_INVALIDARGUMENTS;
|
153 |
} |
154 |
// entweder jedes mal endian abfragen oder config mit übergeben
|
155 |
if (!(cfg->registers.ctrl_reg4 & L3G4200D_LLD_BLE_MSB)) {
|
156 |
*data = (int16_t) (buffer[0] | (buffer[1] << 8)); |
157 |
} else {
|
158 |
*data = (int16_t) (buffer[1] | (buffer[0] << 8)); |
159 |
} |
160 |
return status;
|
161 |
} |
162 |
|
163 |
/**
|
164 |
* @brief Read the current configuration
|
165 |
* @param[in] l3gd The L3G4200D driver to use.
|
166 |
* @param[out] cfg The current configuration.
|
167 |
*
|
168 |
* @return The return status indicates whether the function call was succesfull or a timeout occurred.
|
169 |
*/
|
170 |
apalExitStatus_t l3g4200d_lld_read_config(const L3G4200DDriver* const l3gd, l3g4200d_lld_cfg_t* const cfg) |
171 |
{ |
172 |
apalDbgAssert(cfg != NULL);
|
173 |
|
174 |
return l3g4200d_lld_read_register(l3gd, L3G4200D_LLD_REGISTER_CTRL_REG1, cfg->data, 5); |
175 |
} |
176 |
|
177 |
/**
|
178 |
* @brief Write the a configuration
|
179 |
* @param[in] l3gd The L3G4200D driver to use.
|
180 |
* @param[in] cfg The new configuration.
|
181 |
*
|
182 |
* @return The return status indicates whether the function call was succesfull or a timeout occurred.
|
183 |
*/
|
184 |
apalExitStatus_t l3g4200d_lld_write_config(const L3G4200DDriver* const l3gd, const l3g4200d_lld_cfg_t cfg) |
185 |
{ |
186 |
return l3g4200d_lld_write_register(l3gd, L3G4200D_LLD_REGISTER_CTRL_REG1, cfg.data, 5); |
187 |
} |
188 |
|
189 |
/**
|
190 |
* @brief Read the interrupt configuration of one of the interrupts.
|
191 |
* @param[in] spid The SPI driver to use.
|
192 |
* @param[out] cfg The current interrupt configuration.
|
193 |
*
|
194 |
* @return The return status indicates whether the function call was succesfull or a timeout occurred.
|
195 |
*/
|
196 |
apalExitStatus_t l3g4200d_lld_read_int_config(const L3G4200DDriver* const l3gd, l3g4200d_lld_int_cfg_t* const cfg) |
197 |
{ |
198 |
apalDbgAssert(cfg != NULL);
|
199 |
|
200 |
return l3g4200d_lld_read_register(l3gd, L3G4200D_LLD_REGISTER_INT1_CFG, cfg->data, 9); |
201 |
} |
202 |
|
203 |
/**
|
204 |
* @brief Write the interrupt configuration.
|
205 |
* @param[in] spid The SPI driver to use.
|
206 |
* @param[in] cfg The current new configuration.
|
207 |
*
|
208 |
* @return The return status indicates whether the function call was succesfull or a timeout occurred.
|
209 |
*/
|
210 |
apalExitStatus_t l3g4200d_lld_write_int_config(const L3G4200DDriver* const l3gd, const l3g4200d_lld_int_cfg_t cfg) |
211 |
{ |
212 |
apalExitStatus_t status = l3g4200d_lld_write_register(l3gd, L3G4200D_LLD_REGISTER_INT1_CFG, cfg.data, 1);
|
213 |
if (status != APAL_STATUS_OK) {
|
214 |
return status;
|
215 |
} |
216 |
return l3g4200d_lld_write_register(l3gd, L3G4200D_LLD_REGISTER_INT1_TSH_XH, &cfg.data[2], 7); |
217 |
} |
218 |
|
219 |
/**
|
220 |
* @brief Read the status register.
|
221 |
* @param[in] spid The SPI driver to use.
|
222 |
* @param[out] status The current status register.
|
223 |
*
|
224 |
* @return The return status indicates whether the function call was succesfull or a timeout occurred.
|
225 |
*/
|
226 |
apalExitStatus_t l3g4200d_lld_read_status_register(const L3G4200DDriver* const l3gd, uint8_t* const status) |
227 |
{ |
228 |
apalDbgAssert(status != NULL);
|
229 |
|
230 |
return l3g4200d_lld_read_register(l3gd, L3G4200D_LLD_REGISTER_STATUS_REG, status, 1); |
231 |
} |
232 |
|
233 |
/**
|
234 |
* @brief Read the fifo ctrl register.
|
235 |
* @param[in] spid The SPI driver to use.
|
236 |
* @param[out] fifo The current fifo ctrl register.
|
237 |
*
|
238 |
* @return The return status indicates whether the function call was succesfull or a timeout occurred.
|
239 |
*/
|
240 |
inline apalExitStatus_t
|
241 |
l3g4200d_lld_read_fifo_ctrl_register(const L3G4200DDriver* const l3gd, uint8_t* const fifo) |
242 |
{ |
243 |
apalDbgAssert(fifo != NULL);
|
244 |
|
245 |
return l3g4200d_lld_read_register(l3gd, L3G4200D_LLD_REGISTER_FIFO_CTRL_REG, fifo, 1); |
246 |
} |
247 |
|
248 |
/**
|
249 |
* @brief Write the fifo ctrl register.
|
250 |
* @param[in] spid The SPI driver to use.
|
251 |
* @param[in] fifo The new fifo ctrl register.
|
252 |
*
|
253 |
* @return The return status indicates whether the function call was succesfull or a timeout occurred.
|
254 |
*/
|
255 |
apalExitStatus_t l3g4200d_lld_write_fifo_ctrl_register(const L3G4200DDriver* const l3gd, const uint8_t fifo) |
256 |
{ |
257 |
return l3g4200d_lld_write_register(l3gd, L3G4200D_LLD_REGISTER_FIFO_CTRL_REG, &fifo, 1); |
258 |
} |
259 |
|
260 |
/**
|
261 |
* @brief Read the fifo src register.
|
262 |
* @param[in] spid The SPI driver to use.
|
263 |
* @param[out] fifo The current fifo src register.
|
264 |
*
|
265 |
* @return The return status indicates whether the function call was succesfull or a timeout occurred.
|
266 |
*/
|
267 |
apalExitStatus_t l3g4200d_lld_read_fifo_src_register(const L3G4200DDriver* const l3gd, uint8_t* const fifo) |
268 |
{ |
269 |
apalDbgAssert(fifo != NULL);
|
270 |
|
271 |
return l3g4200d_lld_read_register(l3gd, L3G4200D_LLD_REGISTER_FIFO_SRC_REG, fifo, 1); |
272 |
} |
273 |
|
274 |
/**
|
275 |
* @brief Read the interrupt src register.
|
276 |
* @param[in] spid The SPI driver to use.
|
277 |
* @param[out] cfg The current fifo src register.
|
278 |
*
|
279 |
* @return The return status indicates whether the function call was succesfull or a timeout occurred.
|
280 |
*/
|
281 |
apalExitStatus_t l3g4200d_lld_read_int_src(const L3G4200DDriver* const l3gd, uint8_t* const cfg) |
282 |
{ |
283 |
apalDbgAssert(cfg != NULL);
|
284 |
|
285 |
return l3g4200d_lld_read_register(l3gd, L3G4200D_LLD_REGISTER_INT1_SRC, cfg, 1); |
286 |
} |
287 |
|
288 |
#endif /* defined(AMIROLLD_CFG_L3G4200D) && (AMIROLLD_CFG_L3G4200D == 1) */ |
289 |
|
290 |
/** @} */
|