amiro-lld / source / alld_lis331dlh.c @ 1d4fc180
History | View | Annotate | Download (9.567 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 alld_lis331dlh.c
|
21 |
* @brief Accelerometer function implementations.
|
22 |
*
|
23 |
* @addtogroup lld_accel
|
24 |
* @{
|
25 |
*/
|
26 |
|
27 |
#include <alld_lis331dlh.h> |
28 |
|
29 |
#if defined(AMIROLLD_CFG_USE_LIS331DLH) || defined(__DOXYGEN__)
|
30 |
|
31 |
#include <string.h> |
32 |
|
33 |
/**
|
34 |
* @brief Read the content of one or more registers.
|
35 |
* @param[in] lisd The LIS331DLH driver to use.
|
36 |
* @param[in] regaddr The address of the first register.
|
37 |
* @param[out] data The data read from the registers.
|
38 |
* @param[in] length Number of registers to read.
|
39 |
*
|
40 |
* @return The return status indicates whether the function call was succesfull or a timeout occurred.
|
41 |
*/
|
42 |
inline apalExitStatus_t
|
43 |
lis331dlh_lld_read_register(const LIS331DLHDriver* const lisd, const lis331dlh_lld_register_t regaddr, uint8_t *data, const uint8_t length) |
44 |
{ |
45 |
apalDbgAssert(lisd != NULL && lisd->spid != NULL); |
46 |
apalDbgAssert(data != NULL);
|
47 |
|
48 |
uint8_t buffer[length+1];
|
49 |
buffer[0] = regaddr | LIS331DLH_LLD_SPI_READ | ((length > 1) ? LIS331DLH_LLD_SPI_MULT : 0); |
50 |
apalExitStatus_t status = apalSPIExchange(lisd->spid, buffer, buffer, length+1);
|
51 |
memcpy(data, buffer+1, length);
|
52 |
return status;
|
53 |
} |
54 |
|
55 |
/**
|
56 |
* @brief Write to one or more registers.
|
57 |
* @param[in] lisd The LIS331DLH driver to use.
|
58 |
* @param[in] regaddr The address of the first register.
|
59 |
* @param[in] data The data to be written to the registers.
|
60 |
* @param[in] length Number of registers to write.
|
61 |
*
|
62 |
* @return The return status indicates whether the function call was succesfull or a timeout occurred.
|
63 |
*/
|
64 |
inline apalExitStatus_t
|
65 |
lis331dlh_lld_write_register(const LIS331DLHDriver* const lisd, const lis331dlh_lld_register_t regaddr, const uint8_t *data, const uint8_t length) |
66 |
{ |
67 |
apalDbgAssert(lisd != NULL && lisd->spid != NULL); |
68 |
apalDbgAssert(data != NULL);
|
69 |
|
70 |
uint8_t buffer[length+1];
|
71 |
buffer[0] = regaddr | LIS331DLH_LLD_SPI_WRITE | ((length > 1) ? LIS331DLH_LLD_SPI_MULT : 0); |
72 |
memcpy(buffer+1, data, length);
|
73 |
return apalSPITransmit(lisd->spid, buffer, length+1); |
74 |
} |
75 |
|
76 |
/**
|
77 |
* @brief Reset the high pass filter
|
78 |
* @param[in] lisd The LIS331DLH driver to use.
|
79 |
*
|
80 |
* @return The return status indicates whether the function call was succesfull or a timeout occurred.
|
81 |
*/
|
82 |
inline apalExitStatus_t
|
83 |
lis331dlh_lld_reset_hp_filter(const LIS331DLHDriver* const lisd) |
84 |
{ |
85 |
return lis331dlh_lld_read_register(lisd, LIS331DLH_LLD_REGISTER_HP_FILTER_RESET, NULL, 0); |
86 |
} |
87 |
|
88 |
/**
|
89 |
* @brief Read the sensor data of all 3 axes.
|
90 |
* @param[in] lisd The LIS331DLH driver to use.
|
91 |
* @param[out] data The sensor data.
|
92 |
* @param[in] cfg The current configuration. Needed to find out if the data is saved as little or big endian.
|
93 |
*
|
94 |
* @return The return status indicates whether the function call was succesfull or a timeout occurred.
|
95 |
*/
|
96 |
inline apalExitStatus_t
|
97 |
lis331dlh_lld_read_all_data(const LIS331DLHDriver* const lisd, int16_t *data, const lis331dlh_lld_cfg_t *cfg) |
98 |
{ |
99 |
apalDbgAssert(data != NULL);
|
100 |
apalDbgAssert(cfg != NULL);
|
101 |
|
102 |
uint8_t buffer[6];
|
103 |
apalExitStatus_t status = lis331dlh_lld_read_register(lisd, LIS331DLH_LLD_REGISTER_OUT_X_L, buffer, 6);
|
104 |
if (!(cfg->registers.ctrl_reg4 & LIS331DLH_LLD_BLE_BE)) {
|
105 |
data[0] = (int16_t) (buffer[0] | (buffer[1] << 8)); |
106 |
data[1] = (int16_t) (buffer[2] | (buffer[3] << 8)); |
107 |
data[2] = (int16_t) (buffer[4] | (buffer[5] << 8)); |
108 |
} else {
|
109 |
data[0] = (int16_t) (buffer[1] | (buffer[0] << 8)); |
110 |
data[1] = (int16_t) (buffer[3] | (buffer[2] << 8)); |
111 |
data[2] = (int16_t) (buffer[5] | (buffer[4] << 8)); |
112 |
} |
113 |
return status;
|
114 |
} |
115 |
|
116 |
/**
|
117 |
* @brief Read the sensor data of one axis.
|
118 |
* @param[in] lisd The LIS331DLH driver to use.
|
119 |
* @param[out] data The sensor data.
|
120 |
* @param[in] axis The axis for which the data should be read.
|
121 |
* @param[in] cfg The current configuration. Needed to find out if the data is saved as little or big endian.
|
122 |
*
|
123 |
* @return The return status indicates whether the function call was succesfull or a timeout occurred.
|
124 |
*/
|
125 |
inline apalExitStatus_t
|
126 |
lis331dlh_lld_read_data(const LIS331DLHDriver* const lisd, int16_t *data, const lis331dlh_lld_axis_t axis, const lis331dlh_lld_cfg_t *cfg) |
127 |
{ |
128 |
apalDbgAssert(data != NULL);
|
129 |
apalDbgAssert(cfg != NULL);
|
130 |
|
131 |
apalExitStatus_t status = APAL_STATUS_SUCCESS; |
132 |
uint8_t buffer[2];
|
133 |
switch (axis) {
|
134 |
case LIS331DLH_LLD_X_AXIS:
|
135 |
status = lis331dlh_lld_read_register(lisd, LIS331DLH_LLD_REGISTER_OUT_X_L, buffer, 2);
|
136 |
break;
|
137 |
case LIS331DLH_LLD_Y_AXIS:
|
138 |
status = lis331dlh_lld_read_register(lisd, LIS331DLH_LLD_REGISTER_OUT_Y_L, buffer, 2);
|
139 |
break;
|
140 |
case LIS331DLH_LLD_Z_AXIS:
|
141 |
status = lis331dlh_lld_read_register(lisd, LIS331DLH_LLD_REGISTER_OUT_Z_L, buffer, 2);
|
142 |
break;
|
143 |
default:
|
144 |
return APAL_STATUS_INVALIDARGUMENTS;
|
145 |
} |
146 |
// entweder jedes mal endian abfragen oder config mit übergeben
|
147 |
if (!(cfg->registers.ctrl_reg4 & LIS331DLH_LLD_BLE_BE)) {
|
148 |
*data = (int16_t) (buffer[0] | (buffer[1] << 8)); |
149 |
} else {
|
150 |
*data = (int16_t) (buffer[1] | (buffer[0] << 8)); |
151 |
} |
152 |
return status;
|
153 |
} |
154 |
|
155 |
/**
|
156 |
* @brief Read the current configuration
|
157 |
* @param[in] lisd The LIS331DLH driver to use.
|
158 |
* @param[out] cfg The current configuration.
|
159 |
*
|
160 |
* @return The return status indicates whether the function call was succesfull or a timeout occurred.
|
161 |
*/
|
162 |
inline apalExitStatus_t
|
163 |
lis331dlh_lld_read_config(const LIS331DLHDriver* const lisd, lis331dlh_lld_cfg_t *cfg) |
164 |
{ |
165 |
apalDbgAssert(cfg != NULL);
|
166 |
|
167 |
return lis331dlh_lld_read_register(lisd, LIS331DLH_LLD_REGISTER_CTRL_REG1, cfg->data, 5); |
168 |
} |
169 |
|
170 |
/**
|
171 |
* @brief Write the a configuration
|
172 |
* @param[in] lisd The LIS331DLH driver to use.
|
173 |
* @param[in] cfg The new configuration.
|
174 |
*
|
175 |
* @return The return status indicates whether the function call was succesfull or a timeout occurred.
|
176 |
*/
|
177 |
inline apalExitStatus_t
|
178 |
lis331dlh_lld_write_config(const LIS331DLHDriver* const lisd, const lis331dlh_lld_cfg_t *cfg) |
179 |
{ |
180 |
apalDbgAssert(cfg != NULL);
|
181 |
|
182 |
return lis331dlh_lld_write_register(lisd, LIS331DLH_LLD_REGISTER_CTRL_REG1, cfg->data, 5); |
183 |
} |
184 |
|
185 |
/**
|
186 |
* @brief Read the interrupt configuration of one of the interrupts.
|
187 |
* @param[in] spid The SPI driver to use.
|
188 |
* @param[out] cfg The current interrupt configuration.
|
189 |
* @param[in] interrupt The interrupt for which the configuration should be read.
|
190 |
*
|
191 |
* @return The return status indicates whether the function call was succesfull or a timeout occurred.
|
192 |
*/
|
193 |
inline apalExitStatus_t
|
194 |
lis331dlh_lld_read_int_config(const LIS331DLHDriver* const lisd, lis331dlh_lld_int_cfg_t *cfg, const lis331dlh_lld_int_t interrupt) |
195 |
{ |
196 |
apalDbgAssert(cfg != NULL);
|
197 |
|
198 |
uint8_t buffer[2];
|
199 |
apalExitStatus_t status = APAL_STATUS_OK; |
200 |
if (interrupt == LIS331DLH_LLD_INT1) {
|
201 |
status |= lis331dlh_lld_read_register(lisd, LIS331DLH_LLD_REGISTER_INT1_CFG, &cfg->cfg_reg, 1);
|
202 |
status |= lis331dlh_lld_read_register(lisd, LIS331DLH_LLD_REGISTER_INT1_THS, buffer, 2);
|
203 |
} else {
|
204 |
status |= lis331dlh_lld_read_register(lisd, LIS331DLH_LLD_REGISTER_INT2_CFG, &cfg->cfg_reg, 1);
|
205 |
status |= lis331dlh_lld_read_register(lisd, LIS331DLH_LLD_REGISTER_INT2_THS, buffer, 2);
|
206 |
} |
207 |
if (status != APAL_STATUS_OK) {
|
208 |
return status;
|
209 |
} |
210 |
cfg->threshold = buffer[0];
|
211 |
cfg->duration = buffer[1];
|
212 |
return status;
|
213 |
} |
214 |
|
215 |
/**
|
216 |
* @brief Write the interrupt configuration of one of the interrupts.
|
217 |
* @param[in] spid The SPI driver to use.
|
218 |
* @param[in] cfg The current new configuration.
|
219 |
* @param[in] interrupt The interrupt for which the configuration should be written.
|
220 |
*
|
221 |
* @return The return status indicates whether the function call was succesfull or a timeout occurred.
|
222 |
*/
|
223 |
inline apalExitStatus_t
|
224 |
lis331dlh_lld_write_int_config(const LIS331DLHDriver* const lisd, const lis331dlh_lld_int_cfg_t *cfg, const lis331dlh_lld_int_t interrupt) |
225 |
{ |
226 |
apalDbgAssert(cfg != NULL);
|
227 |
|
228 |
uint8_t buffer[2];
|
229 |
buffer[0] = cfg->threshold;
|
230 |
buffer[1] = cfg->duration;
|
231 |
apalExitStatus_t status = APAL_STATUS_OK; |
232 |
if (interrupt == LIS331DLH_LLD_INT1) {
|
233 |
status |= lis331dlh_lld_write_register(lisd, LIS331DLH_LLD_REGISTER_INT1_CFG, &(cfg->cfg_reg), 1);
|
234 |
status |= lis331dlh_lld_write_register(lisd, LIS331DLH_LLD_REGISTER_INT1_THS, buffer, 2);
|
235 |
} else {
|
236 |
status |= lis331dlh_lld_write_register(lisd, LIS331DLH_LLD_REGISTER_INT2_CFG, &(cfg->cfg_reg), 1);
|
237 |
status |= lis331dlh_lld_write_register(lisd, LIS331DLH_LLD_REGISTER_INT2_THS, buffer, 2);
|
238 |
} |
239 |
return status;
|
240 |
} |
241 |
|
242 |
/**
|
243 |
* @brief Read the status register.
|
244 |
* @param[in] spid The SPI driver to use.
|
245 |
* @param[out] status The status register value.
|
246 |
*
|
247 |
* @return The return status indicates whether the function call was succesfull or a timeout occurred.
|
248 |
*/
|
249 |
inline apalExitStatus_t
|
250 |
lis331dlh_lld_read_status_register(const LIS331DLHDriver* const lisd, uint8_t *status) |
251 |
{ |
252 |
apalDbgAssert(status != NULL);
|
253 |
|
254 |
return lis331dlh_lld_read_register(lisd, LIS331DLH_LLD_REGISTER_STATUS_REG, status, 1); |
255 |
} |
256 |
|
257 |
#endif /* defined(AMIROLLD_CFG_USE_LIS331DLH) */ |
258 |
|
259 |
/** @} */
|