Statistics
| Branch: | Tag: | Revision:

amiro-lld / source / alld_lis331dlh.c @ ef078306

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