Statistics
| Branch: | Tag: | Revision:

amiro-lld / source / alld_l3g4200d.c @ 11500ad8

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