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