Statistics
| Branch: | Tag: | Revision:

amiro-lld / source / alld_l3g4200d.c @ ef078306

History | View | Annotate | Download (11.3 KB)

1
/*
2
AMiRo-LLD is a compilation of low-level hardware drivers for the Autonomous Mini Robot (AMiRo) platform.
3
Copyright (C) 2016..2019  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_l3g4200d.c
21
 * @brief   Gyroscope function implementations.
22
 *
23
 * @addtogroup lld_gyroscope
24
 * @{
25
 */
26

    
27
#include<alld_l3g4200d.h>
28

    
29
#if defined(AMIROLLD_CFG_USE_L3G4200D) || defined(__DOXYGEN__)
30

    
31
#include <string.h>
32

    
33
/******************************************************************************/
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
/**
58
 * @brief Read the content of one or more registers.
59
 * @param[in]   l3gd      The L3G4200D 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
l3g4200d_lld_read_register(const L3G4200DDriver* const l3gd, const l3g4200d_lld_register_t regaddr, uint8_t* const data, const uint8_t length)
68
{
69
  apalDbgAssert(l3gd != NULL && l3gd->spid != NULL);
70
  apalDbgAssert(data != NULL);
71

    
72
  uint8_t buffer[length+1];
73
  buffer[0] = regaddr | L3G4200D_LLD_SPI_READ | ((length > 1) ? L3G4200D_LLD_SPI_MULT : 0);
74
  apalExitStatus_t status = apalSPIExchange(l3gd->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]   l3gd      The L3G4200D 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
l3g4200d_lld_write_register(const L3G4200DDriver* const l3gd, const l3g4200d_lld_register_t regaddr, const uint8_t* const data, const uint8_t length)
90
{
91
  apalDbgAssert(l3gd != NULL && l3gd->spid != NULL);
92
  apalDbgAssert(data != NULL);
93

    
94
  uint8_t buffer[length+1];
95
  buffer[0] = regaddr | L3G4200D_LLD_SPI_WRITE | ((length > 1) ? L3G4200D_LLD_SPI_MULT : 0);
96
  memcpy(buffer+1, data, length);
97
  return apalSPITransmit(l3gd->spid, buffer, length+1);
98
}
99

    
100
/**
101
 * @brief Read the sensor data of all 3 axes.
102
 * @param[in]   l3gd      The L3G4200D driver to use.
103
 * @param[out]  data      The sensor data.
104
 * @param[in]   cfg       The current configuration. Needed to find out if the data is saved as little or big endian.
105
 *
106
 * @return  The return status indicates whether the function call was succesfull or a timeout occurred.
107
 */
108
inline apalExitStatus_t
109
l3g4200d_lld_read_all_data(const L3G4200DDriver* const l3gd, int16_t* const data, const l3g4200d_lld_cfg_t* const cfg)
110
{
111
  apalDbgAssert(data != NULL);
112
  apalDbgAssert(cfg != NULL);
113

    
114
  uint8_t buffer[6];
115
  apalExitStatus_t status = l3g4200d_lld_read_register(l3gd, L3G4200D_LLD_REGISTER_OUT_X_L, buffer, 6);
116
  if (!(cfg->registers.ctrl_reg4 & L3G4200D_LLD_BLE_MSB)) {
117
    data[0] = (int16_t) (buffer[0] | (buffer[1] << 8));
118
    data[1] = (int16_t) (buffer[2] | (buffer[3] << 8));
119
    data[2] = (int16_t) (buffer[4] | (buffer[5] << 8));
120
  } else {
121
    data[0] = (int16_t) (buffer[1] | (buffer[0] << 8));
122
    data[1] = (int16_t) (buffer[3] | (buffer[2] << 8));
123
    data[2] = (int16_t) (buffer[5] | (buffer[4] << 8));
124
  }
125
  return status;
126
}
127

    
128
/**
129
 * @brief Read the sensor data of one axis.
130
 * @param[in]   l3gd      The L3G4200D driver to use.
131
 * @param[out]  data      The sensor data.
132
 * @param[in]   axis      The axis for which the data should be read.
133
 * @param[in]   cfg       The current configuration. Needed to find out if the data is saved as little or big endian.
134
 *
135
 * @return  The return status indicates whether the function call was succesfull or a timeout occurred.
136
 */
137
inline apalExitStatus_t
138
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)
139
{
140
  apalDbgAssert(data != NULL);
141
  apalDbgAssert(cfg != NULL);
142

    
143
  apalExitStatus_t status = APAL_STATUS_SUCCESS;
144
  uint8_t buffer[2];
145
  switch (axis) {
146
    case L3G4200D_LLD_X_AXIS:
147
      status = l3g4200d_lld_read_register(l3gd, L3G4200D_LLD_REGISTER_OUT_X_L, buffer, 2);
148
      break;
149
    case L3G4200D_LLD_Y_AXIS:
150
      status = l3g4200d_lld_read_register(l3gd, L3G4200D_LLD_REGISTER_OUT_Y_L, buffer, 2);
151
      break;
152
    case L3G4200D_LLD_Z_AXIS:
153
      status = l3g4200d_lld_read_register(l3gd, L3G4200D_LLD_REGISTER_OUT_Z_L, buffer, 2);
154
      break;
155
    default:
156
      return APAL_STATUS_INVALIDARGUMENTS;
157
  }
158
  // entweder jedes mal endian abfragen oder config mit übergeben
159
  if (!(cfg->registers.ctrl_reg4 & L3G4200D_LLD_BLE_MSB)) {
160
    *data = (int16_t) (buffer[0] | (buffer[1] << 8));
161
  } else {
162
    *data = (int16_t) (buffer[1] | (buffer[0] << 8));
163
  }
164
  return status;
165
}
166

    
167
/**
168
 * @brief Read the current configuration
169
 * @param[in]   l3gd      The L3G4200D driver to use.
170
 * @param[out]  cfg       The current configuration.
171
 *
172
 * @return  The return status indicates whether the function call was succesfull or a timeout occurred.
173
 */
174
inline apalExitStatus_t
175
l3g4200d_lld_read_config(const L3G4200DDriver* const l3gd, l3g4200d_lld_cfg_t* const cfg)
176
{
177
  apalDbgAssert(cfg != NULL);
178

    
179
  return l3g4200d_lld_read_register(l3gd, L3G4200D_LLD_REGISTER_CTRL_REG1, cfg->data, 5);
180
}
181

    
182
/**
183
 * @brief Write the a configuration
184
 * @param[in]   l3gd      The L3G4200D driver to use.
185
 * @param[in]   cfg       The new configuration.
186
 *
187
 * @return  The return status indicates whether the function call was succesfull or a timeout occurred.
188
 */
189
inline apalExitStatus_t
190
l3g4200d_lld_write_config(const L3G4200DDriver* const l3gd, const l3g4200d_lld_cfg_t cfg)
191
{
192
  return l3g4200d_lld_write_register(l3gd, L3G4200D_LLD_REGISTER_CTRL_REG1, cfg.data, 5);
193
}
194

    
195
/**
196
 * @brief Read the interrupt configuration of one of the interrupts.
197
 * @param[in]   spid        The SPI driver to use.
198
 * @param[out]  cfg         The current interrupt configuration.
199
 *
200
 * @return  The return status indicates whether the function call was succesfull or a timeout occurred.
201
 */
202
inline apalExitStatus_t
203
l3g4200d_lld_read_int_config(const L3G4200DDriver* const l3gd, l3g4200d_lld_int_cfg_t* const cfg)
204
{
205
  apalDbgAssert(cfg != NULL);
206

    
207
  return l3g4200d_lld_read_register(l3gd, L3G4200D_LLD_REGISTER_INT1_CFG, cfg->data, 9);
208
}
209

    
210
/**
211
 * @brief Write the interrupt configuration.
212
 * @param[in]   spid        The SPI driver to use.
213
 * @param[in]   cfg         The current new configuration.
214
 *
215
 * @return  The return status indicates whether the function call was succesfull or a timeout occurred.
216
 */
217
inline apalExitStatus_t
218
l3g4200d_lld_write_int_config(const L3G4200DDriver* const l3gd, const l3g4200d_lld_int_cfg_t cfg)
219
{
220
  apalExitStatus_t status = l3g4200d_lld_write_register(l3gd, L3G4200D_LLD_REGISTER_INT1_CFG, cfg.data, 1);
221
  if (status != APAL_STATUS_OK) {
222
    return status;
223
  }
224
  return l3g4200d_lld_write_register(l3gd, L3G4200D_LLD_REGISTER_INT1_TSH_XH, &cfg.data[2], 7);
225
}
226

    
227
/**
228
 * @brief Read the status register.
229
 * @param[in]   spid        The SPI driver to use.
230
 * @param[out]  status      The current status register.
231
 *
232
 * @return  The return status indicates whether the function call was succesfull or a timeout occurred.
233
 */
234
inline apalExitStatus_t
235
l3g4200d_lld_read_status_register(const L3G4200DDriver* const l3gd, uint8_t* const status)
236
{
237
  apalDbgAssert(status != NULL);
238

    
239
  return l3g4200d_lld_read_register(l3gd, L3G4200D_LLD_REGISTER_STATUS_REG, status, 1);
240
}
241

    
242
/**
243
 * @brief Read the fifo ctrl register.
244
 * @param[in]   spid        The SPI driver to use.
245
 * @param[out]  fifo        The current fifo ctrl register.
246
 *
247
 * @return  The return status indicates whether the function call was succesfull or a timeout occurred.
248
 */
249
inline apalExitStatus_t
250
l3g4200d_lld_read_fifo_ctrl_register(const L3G4200DDriver* const l3gd, uint8_t* const fifo)
251
{
252
  apalDbgAssert(fifo != NULL);
253

    
254
  return l3g4200d_lld_read_register(l3gd, L3G4200D_LLD_REGISTER_FIFO_CTRL_REG, fifo, 1);
255
}
256

    
257
/**
258
 * @brief Write the fifo ctrl register.
259
 * @param[in]   spid        The SPI driver to use.
260
 * @param[in]   fifo        The new fifo ctrl register.
261
 *
262
 * @return  The return status indicates whether the function call was succesfull or a timeout occurred.
263
 */
264
inline apalExitStatus_t
265
l3g4200d_lld_write_fifo_ctrl_register(const L3G4200DDriver* const l3gd, const uint8_t fifo)
266
{
267
  return l3g4200d_lld_write_register(l3gd, L3G4200D_LLD_REGISTER_FIFO_CTRL_REG, &fifo, 1);
268
}
269

    
270
/**
271
 * @brief Read the fifo src register.
272
 * @param[in]   spid        The SPI driver to use.
273
 * @param[out]  fifo        The current fifo src register.
274
 *
275
 * @return  The return status indicates whether the function call was succesfull or a timeout occurred.
276
 */
277
inline apalExitStatus_t
278
l3g4200d_lld_read_fifo_src_register(const L3G4200DDriver* const l3gd, uint8_t* const fifo)
279
{
280
  apalDbgAssert(fifo != NULL);
281

    
282
  return l3g4200d_lld_read_register(l3gd, L3G4200D_LLD_REGISTER_FIFO_SRC_REG, fifo, 1);
283
}
284

    
285
/**
286
 * @brief Read the interrupt src register.
287
 * @param[in]   spid        The SPI driver to use.
288
 * @param[out]  cfg         The current fifo src register.
289
 *
290
 * @return  The return status indicates whether the function call was succesfull or a timeout occurred.
291
 */
292
inline apalExitStatus_t
293
l3g4200d_lld_read_int_src(const L3G4200DDriver* const l3gd, uint8_t* const cfg)
294
{
295
  apalDbgAssert(cfg != NULL);
296

    
297
  return l3g4200d_lld_read_register(l3gd, L3G4200D_LLD_REGISTER_INT1_SRC, cfg, 1);
298
}
299

    
300
#endif /* defined(AMIROLLD_CFG_USE_L3G4200D) */
301

    
302
/** @} */