Statistics
| Branch: | Tag: | Revision:

amiro-lld / source / alld_l3g4200d.c @ cf1f756b

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