amiro-lld / source / LIS331DLH / v1 / alld_LIS331DLH_v1.c @ 01828616
History | View | Annotate | Download (10.99 KB)
1 | d6728c5b | Thomas Schöpping | /*
|
---|---|---|---|
2 | AMiRo-LLD is a compilation of low-level hardware drivers for the Autonomous Mini Robot (AMiRo) platform.
|
||
3 | f125ae07 | Thomas Schöpping | Copyright (C) 2016..2019 Thomas Schöpping et al.
|
4 | d6728c5b | Thomas Schöpping | |
5 | This program is free software: you can redistribute it and/or modify
|
||
6 | f0ca400f | Thomas Schöpping | it under the terms of the GNU Lesser General Public License as published by
|
7 | d6728c5b | Thomas Schöpping | 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 | f0ca400f | Thomas Schöpping | GNU Lesser General Public License for more details.
|
14 | d6728c5b | Thomas Schöpping | |
15 | f0ca400f | Thomas Schöpping | You should have received a copy of the GNU Lesser General Public License
|
16 | d6728c5b | Thomas Schöpping | along with this program. If not, see <http://www.gnu.org/licenses/>.
|
17 | */
|
||
18 | |||
19 | 5e2f673b | Marc Rothmann | /**
|
20 | 1d5bcc82 | Thomas Schöpping | * @file alld_LIS331DLH_v1.c
|
21 | 5e2f673b | Marc Rothmann | * @brief Accelerometer function implementations.
|
22 | *
|
||
23 | * @addtogroup lld_accel
|
||
24 | * @{
|
||
25 | */
|
||
26 | |||
27 | 1d5bcc82 | Thomas Schöpping | #include <alld_LIS331DLH.h> |
28 | d6728c5b | Thomas Schöpping | |
29 | 1d5bcc82 | Thomas Schöpping | #if (defined(AMIROLLD_CFG_LIS331DLH) && (AMIROLLD_CFG_LIS331DLH == 1)) || defined(__DOXYGEN__) |
30 | d6728c5b | Thomas Schöpping | |
31 | #include <string.h> |
||
32 | |||
33 | ef078306 | Thomas Schöpping | /******************************************************************************/
|
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 | d6728c5b | Thomas Schöpping | /**
|
58 | * @brief Read the content of one or more registers.
|
||
59 | * @param[in] lisd The LIS331DLH 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 | 21076167 | Thomas Schöpping | apalExitStatus_t lis331dlh_lld_read_register(const LIS331DLHDriver* const lisd, const lis331dlh_lld_register_t regaddr, uint8_t *data, const uint8_t length) |
67 | d6728c5b | Thomas Schöpping | { |
68 | apalDbgAssert(lisd != NULL && lisd->spid != NULL); |
||
69 | apalDbgAssert(data != NULL);
|
||
70 | |||
71 | uint8_t buffer[length+1];
|
||
72 | buffer[0] = regaddr | LIS331DLH_LLD_SPI_READ | ((length > 1) ? LIS331DLH_LLD_SPI_MULT : 0); |
||
73 | apalExitStatus_t status = apalSPIExchange(lisd->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] lisd The LIS331DLH 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 | 21076167 | Thomas Schöpping | apalExitStatus_t lis331dlh_lld_write_register(const LIS331DLHDriver* const lisd, const lis331dlh_lld_register_t regaddr, const uint8_t *data, const uint8_t length) |
88 | d6728c5b | Thomas Schöpping | { |
89 | apalDbgAssert(lisd != NULL && lisd->spid != NULL); |
||
90 | apalDbgAssert(data != NULL);
|
||
91 | |||
92 | uint8_t buffer[length+1];
|
||
93 | buffer[0] = regaddr | LIS331DLH_LLD_SPI_WRITE | ((length > 1) ? LIS331DLH_LLD_SPI_MULT : 0); |
||
94 | memcpy(buffer+1, data, length);
|
||
95 | return apalSPITransmit(lisd->spid, buffer, length+1); |
||
96 | } |
||
97 | |||
98 | /**
|
||
99 | * @brief Reset the high pass filter
|
||
100 | * @param[in] lisd The LIS331DLH driver to use.
|
||
101 | *
|
||
102 | * @return The return status indicates whether the function call was succesfull or a timeout occurred.
|
||
103 | */
|
||
104 | 21076167 | Thomas Schöpping | apalExitStatus_t lis331dlh_lld_reset_hp_filter(const LIS331DLHDriver* const lisd) |
105 | d6728c5b | Thomas Schöpping | { |
106 | return lis331dlh_lld_read_register(lisd, LIS331DLH_LLD_REGISTER_HP_FILTER_RESET, NULL, 0); |
||
107 | } |
||
108 | |||
109 | /**
|
||
110 | * @brief Read the sensor data of all 3 axes.
|
||
111 | * @param[in] lisd The LIS331DLH driver to use.
|
||
112 | * @param[out] data The sensor data.
|
||
113 | * @param[in] cfg The current configuration. Needed to find out if the data is saved as little or big endian.
|
||
114 | *
|
||
115 | * @return The return status indicates whether the function call was succesfull or a timeout occurred.
|
||
116 | */
|
||
117 | 21076167 | Thomas Schöpping | apalExitStatus_t lis331dlh_lld_read_all_data(const LIS331DLHDriver* const lisd, int16_t *data, const lis331dlh_lld_cfg_t *cfg) |
118 | d6728c5b | Thomas Schöpping | { |
119 | apalDbgAssert(data != NULL);
|
||
120 | apalDbgAssert(cfg != NULL);
|
||
121 | |||
122 | uint8_t buffer[6];
|
||
123 | apalExitStatus_t status = lis331dlh_lld_read_register(lisd, LIS331DLH_LLD_REGISTER_OUT_X_L, buffer, 6);
|
||
124 | if (!(cfg->registers.ctrl_reg4 & LIS331DLH_LLD_BLE_BE)) {
|
||
125 | data[0] = (int16_t) (buffer[0] | (buffer[1] << 8)); |
||
126 | data[1] = (int16_t) (buffer[2] | (buffer[3] << 8)); |
||
127 | data[2] = (int16_t) (buffer[4] | (buffer[5] << 8)); |
||
128 | } else {
|
||
129 | data[0] = (int16_t) (buffer[1] | (buffer[0] << 8)); |
||
130 | data[1] = (int16_t) (buffer[3] | (buffer[2] << 8)); |
||
131 | data[2] = (int16_t) (buffer[5] | (buffer[4] << 8)); |
||
132 | } |
||
133 | return status;
|
||
134 | } |
||
135 | |||
136 | /**
|
||
137 | * @brief Read the sensor data of one axis.
|
||
138 | * @param[in] lisd The LIS331DLH driver to use.
|
||
139 | * @param[out] data The sensor data.
|
||
140 | * @param[in] axis The axis for which the data should be read.
|
||
141 | * @param[in] cfg The current configuration. Needed to find out if the data is saved as little or big endian.
|
||
142 | *
|
||
143 | * @return The return status indicates whether the function call was succesfull or a timeout occurred.
|
||
144 | */
|
||
145 | 21076167 | Thomas Schöpping | apalExitStatus_t lis331dlh_lld_read_data(const LIS331DLHDriver* const lisd, int16_t *data, const lis331dlh_lld_axis_t axis, const lis331dlh_lld_cfg_t *cfg) |
146 | d6728c5b | Thomas Schöpping | { |
147 | apalDbgAssert(data != NULL);
|
||
148 | apalDbgAssert(cfg != NULL);
|
||
149 | |||
150 | apalExitStatus_t status = APAL_STATUS_SUCCESS; |
||
151 | uint8_t buffer[2];
|
||
152 | switch (axis) {
|
||
153 | case LIS331DLH_LLD_X_AXIS:
|
||
154 | status = lis331dlh_lld_read_register(lisd, LIS331DLH_LLD_REGISTER_OUT_X_L, buffer, 2);
|
||
155 | break;
|
||
156 | case LIS331DLH_LLD_Y_AXIS:
|
||
157 | status = lis331dlh_lld_read_register(lisd, LIS331DLH_LLD_REGISTER_OUT_Y_L, buffer, 2);
|
||
158 | break;
|
||
159 | case LIS331DLH_LLD_Z_AXIS:
|
||
160 | status = lis331dlh_lld_read_register(lisd, LIS331DLH_LLD_REGISTER_OUT_Z_L, buffer, 2);
|
||
161 | break;
|
||
162 | default:
|
||
163 | return APAL_STATUS_INVALIDARGUMENTS;
|
||
164 | } |
||
165 | // entweder jedes mal endian abfragen oder config mit übergeben
|
||
166 | if (!(cfg->registers.ctrl_reg4 & LIS331DLH_LLD_BLE_BE)) {
|
||
167 | *data = (int16_t) (buffer[0] | (buffer[1] << 8)); |
||
168 | } else {
|
||
169 | *data = (int16_t) (buffer[1] | (buffer[0] << 8)); |
||
170 | } |
||
171 | return status;
|
||
172 | } |
||
173 | |||
174 | /**
|
||
175 | * @brief Read the current configuration
|
||
176 | * @param[in] lisd The LIS331DLH driver to use.
|
||
177 | * @param[out] cfg The current configuration.
|
||
178 | *
|
||
179 | * @return The return status indicates whether the function call was succesfull or a timeout occurred.
|
||
180 | */
|
||
181 | 21076167 | Thomas Schöpping | apalExitStatus_t lis331dlh_lld_read_config(const LIS331DLHDriver* const lisd, lis331dlh_lld_cfg_t *cfg) |
182 | d6728c5b | Thomas Schöpping | { |
183 | apalDbgAssert(cfg != NULL);
|
||
184 | |||
185 | return lis331dlh_lld_read_register(lisd, LIS331DLH_LLD_REGISTER_CTRL_REG1, cfg->data, 5); |
||
186 | } |
||
187 | |||
188 | /**
|
||
189 | * @brief Write the a configuration
|
||
190 | * @param[in] lisd The LIS331DLH driver to use.
|
||
191 | * @param[in] cfg The new configuration.
|
||
192 | *
|
||
193 | * @return The return status indicates whether the function call was succesfull or a timeout occurred.
|
||
194 | */
|
||
195 | 21076167 | Thomas Schöpping | apalExitStatus_t lis331dlh_lld_write_config(const LIS331DLHDriver* const lisd, const lis331dlh_lld_cfg_t *cfg) |
196 | d6728c5b | Thomas Schöpping | { |
197 | apalDbgAssert(cfg != NULL);
|
||
198 | |||
199 | return lis331dlh_lld_write_register(lisd, LIS331DLH_LLD_REGISTER_CTRL_REG1, cfg->data, 5); |
||
200 | } |
||
201 | |||
202 | /**
|
||
203 | * @brief Read the interrupt configuration of one of the interrupts.
|
||
204 | * @param[in] spid The SPI driver to use.
|
||
205 | * @param[out] cfg The current interrupt configuration.
|
||
206 | * @param[in] interrupt The interrupt for which the configuration should be read.
|
||
207 | *
|
||
208 | * @return The return status indicates whether the function call was succesfull or a timeout occurred.
|
||
209 | */
|
||
210 | 21076167 | Thomas Schöpping | apalExitStatus_t lis331dlh_lld_read_int_config(const LIS331DLHDriver* const lisd, lis331dlh_lld_int_cfg_t *cfg, const lis331dlh_lld_int_t interrupt) |
211 | d6728c5b | Thomas Schöpping | { |
212 | apalDbgAssert(cfg != NULL);
|
||
213 | |||
214 | uint8_t buffer[2];
|
||
215 | apalExitStatus_t status = APAL_STATUS_OK; |
||
216 | if (interrupt == LIS331DLH_LLD_INT1) {
|
||
217 | status |= lis331dlh_lld_read_register(lisd, LIS331DLH_LLD_REGISTER_INT1_CFG, &cfg->cfg_reg, 1);
|
||
218 | status |= lis331dlh_lld_read_register(lisd, LIS331DLH_LLD_REGISTER_INT1_THS, buffer, 2);
|
||
219 | } else {
|
||
220 | status |= lis331dlh_lld_read_register(lisd, LIS331DLH_LLD_REGISTER_INT2_CFG, &cfg->cfg_reg, 1);
|
||
221 | status |= lis331dlh_lld_read_register(lisd, LIS331DLH_LLD_REGISTER_INT2_THS, buffer, 2);
|
||
222 | } |
||
223 | if (status != APAL_STATUS_OK) {
|
||
224 | return status;
|
||
225 | } |
||
226 | cfg->threshold = buffer[0];
|
||
227 | cfg->duration = buffer[1];
|
||
228 | return status;
|
||
229 | } |
||
230 | |||
231 | /**
|
||
232 | * @brief Write the interrupt configuration of one of the interrupts.
|
||
233 | * @param[in] spid The SPI driver to use.
|
||
234 | * @param[in] cfg The current new configuration.
|
||
235 | * @param[in] interrupt The interrupt for which the configuration should be written.
|
||
236 | *
|
||
237 | * @return The return status indicates whether the function call was succesfull or a timeout occurred.
|
||
238 | */
|
||
239 | 21076167 | Thomas Schöpping | apalExitStatus_t lis331dlh_lld_write_int_config(const LIS331DLHDriver* const lisd, const lis331dlh_lld_int_cfg_t *cfg, const lis331dlh_lld_int_t interrupt) |
240 | d6728c5b | Thomas Schöpping | { |
241 | apalDbgAssert(cfg != NULL);
|
||
242 | |||
243 | uint8_t buffer[2];
|
||
244 | buffer[0] = cfg->threshold;
|
||
245 | buffer[1] = cfg->duration;
|
||
246 | apalExitStatus_t status = APAL_STATUS_OK; |
||
247 | if (interrupt == LIS331DLH_LLD_INT1) {
|
||
248 | status |= lis331dlh_lld_write_register(lisd, LIS331DLH_LLD_REGISTER_INT1_CFG, &(cfg->cfg_reg), 1);
|
||
249 | status |= lis331dlh_lld_write_register(lisd, LIS331DLH_LLD_REGISTER_INT1_THS, buffer, 2);
|
||
250 | } else {
|
||
251 | status |= lis331dlh_lld_write_register(lisd, LIS331DLH_LLD_REGISTER_INT2_CFG, &(cfg->cfg_reg), 1);
|
||
252 | status |= lis331dlh_lld_write_register(lisd, LIS331DLH_LLD_REGISTER_INT2_THS, buffer, 2);
|
||
253 | } |
||
254 | return status;
|
||
255 | } |
||
256 | |||
257 | /**
|
||
258 | * @brief Read the status register.
|
||
259 | * @param[in] spid The SPI driver to use.
|
||
260 | * @param[out] status The status register value.
|
||
261 | *
|
||
262 | * @return The return status indicates whether the function call was succesfull or a timeout occurred.
|
||
263 | */
|
||
264 | 21076167 | Thomas Schöpping | apalExitStatus_t lis331dlh_lld_read_status_register(const LIS331DLHDriver* const lisd, uint8_t *status) |
265 | d6728c5b | Thomas Schöpping | { |
266 | apalDbgAssert(status != NULL);
|
||
267 | |||
268 | return lis331dlh_lld_read_register(lisd, LIS331DLH_LLD_REGISTER_STATUS_REG, status, 1); |
||
269 | } |
||
270 | |||
271 | 1d5bcc82 | Thomas Schöpping | #endif /* defined(AMIROLLD_CFG_LIS331DLH) && (AMIROLLD_CFG_LIS331DLH == 1) */ |
272 | 5e2f673b | Marc Rothmann | |
273 | /** @} */ |