1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
|
/*
ChibiOS - Copyright (C) 2016 Rocco Marco Guglielmi
This file is part of ChibiOS.
ChibiOS is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 3 of the License, or
(at your option) any later version.
ChibiOS is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
/**
* @file lps25h.c
* @brief LPS25H MEMS interface module code.
*
* @addtogroup LPS25H
* @ingroup EX_ST
* @{
*/
#include "hal.h"
#include "lps25h.h"
/*===========================================================================*/
/* Driver local definitions. */
/*===========================================================================*/
/*===========================================================================*/
/* Driver exported variables. */
/*===========================================================================*/
/*===========================================================================*/
/* Driver local variables and types. */
/*===========================================================================*/
/*===========================================================================*/
/* Driver local functions. */
/*===========================================================================*/
#if (LPS25H_USE_I2C) || defined(__DOXYGEN__)
/**
* @brief Reads registers value using I2C.
* @pre The I2C interface must be initialized and the driver started.
*
* @param[in] i2cp pointer to the I2C interface
* @param[in] sad slave address without R bit
* @param[in] reg first sub-register address
* @param[out] rxbuf pointer to an output buffer
* @param[in] n number of consecutive register to read
* @return the operation status.
* @notapi
*/
msg_t lps25hI2CReadRegister(I2CDriver *i2cp, lps25h_sad_t sad, uint8_t reg,
uint8_t* rxbuf, size_t n) {
uint8_t txbuf = reg;
if(n > 1)
txbuf |= LPS25H_SUB_MS;
return i2cMasterTransmitTimeout(i2cp, sad, &txbuf, 1, rxbuf, n,
TIME_INFINITE);
}
/**
* @brief Writes a value into a register using I2C.
* @pre The I2C interface must be initialized and the driver started.
*
* @param[in] i2cp pointer to the I2C interface
* @param[in] sad slave address without R bit
* @param[in] txbuf buffer containing sub-address value in first position
* and values to write
* @param[in] n size of txbuf less one (not considering the first
* element)
* @return the operation status.
* @notapi
* @return the operation status.
*/
msg_t lps25hI2CWriteRegister(I2CDriver *i2cp, lps25h_sad_t sad, uint8_t* txbuf, size_t n) {
if (n > 1)
(*txbuf) |= LPS25H_SUB_MS;
return i2cMasterTransmitTimeout(i2cp, sad, txbuf, n + 1, NULL, 0,
TIME_INFINITE);
}
#endif /* LPS25H_USE_I2C */
/*
* Interface implementation.
*/
static size_t get_axes_number(void *ip) {
osalDbgCheck(ip != NULL);
return LPS25H_NUMBER_OF_AXES;
}
static msg_t read_raw(void *ip, int32_t* axis) {
uint8_t buff[3];
msg_t msg = MSG_OK;
*axis = 0;
osalDbgCheck((ip != NULL) && (axis != NULL));
osalDbgAssert((((LPS25HDriver *)ip)->state == LPS25H_READY),
"read_raw(), invalid state");
#if LPS25H_USE_I2C
osalDbgAssert((((LPS25HDriver *)ip)->config->i2cp->state == I2C_READY),
"read_raw(), channel not ready");
#if LPS25H_SHARED_I2C
i2cAcquireBus(((LPS25HDriver *)ip)->config->i2cp);
i2cStart(((LPS25HDriver *)ip)->config->i2cp,
((LPS25HDriver *)ip)->config->i2ccfg);
#endif /* LPS25H_SHARED_I2C */
msg = lps25hI2CReadRegister(((LPS25HDriver *)ip)->config->i2cp,
((LPS25HDriver *)ip)->config->slaveaddress,
LPS25H_AD_PRESS_OUT_XL, buff, 3);
#if LPS25H_SHARED_I2C
i2cReleaseBus(((LPS25HDriver *)ip)->config->i2cp);
#endif /* LPS25H_SHARED_I2C */
#endif /* LPS25H_USE_I2C */
if(msg == MSG_OK) {
*axis = buff[0] + (buff[1] << 8) + (buff[2] << 16);
}
return msg;
}
static msg_t read_cooked(void *ip, float* axis) {
int32_t raw;
msg_t msg;
osalDbgCheck((ip != NULL) && (axis != NULL));
osalDbgAssert((((LPS25HDriver *)ip)->state == LPS25H_READY),
"read_cooked(), invalid state");
msg = read_raw(ip, &raw);
*axis = raw * ((LPS25HDriver *)ip)->sensitivity;
*axis -= ((LPS25HDriver *)ip)->bias;
return msg;
}
static msg_t set_bias(void *ip, float *bp) {
osalDbgCheck((ip != NULL) && (bp != NULL));
osalDbgAssert((((LPS25HDriver *)ip)->state == LPS25H_READY) ||
(((LPS25HDriver *)ip)->state == LPS25H_STOP),
"set_bias(), invalid state");
((LPS25HDriver *)ip)->bias = *bp;
return MSG_OK;
}
static msg_t reset_bias(void *ip) {
osalDbgCheck(ip != NULL);
osalDbgAssert((((LPS25HDriver *)ip)->state == LPS25H_READY) ||
(((LPS25HDriver *)ip)->state == LPS25H_STOP),
"reset_bias(), invalid state");
((LPS25HDriver *)ip)->bias = 0.0f;
return MSG_OK;
}
static msg_t set_sensivity(void *ip, float *sp) {
osalDbgCheck((ip != NULL) && (sp !=NULL));
osalDbgAssert((((LPS25HDriver *)ip)->state == LPS25H_READY),
"set_sensivity(), invalid state");
((LPS25HDriver *)ip)->sensitivity = *sp;
return MSG_OK;
}
static msg_t reset_sensivity(void *ip) {
osalDbgCheck(ip != NULL);
osalDbgAssert((((LPS25HDriver *)ip)->state == LPS25H_READY),
"reset_sensivity(), invalid state");
((LPS25HDriver *)ip)->sensitivity = LPS25H_SENS;
return MSG_OK;
}
static const struct BaseSensorVMT vmt_basesensor = {
get_axes_number, read_raw, read_cooked
};
static const struct BaseBarometerVMT vmt_basebarometer = {
get_axes_number, read_raw, read_cooked,
set_bias, reset_bias, set_sensivity, reset_sensivity
};
/*===========================================================================*/
/* Driver exported functions. */
/*===========================================================================*/
/**
* @brief Initializes an instance.
*
* @param[out] devp pointer to the @p LPS25HDriver object
*
* @init
*/
void lps25hObjectInit(LPS25HDriver *devp) {
devp->vmt_basesensor = &vmt_basesensor;
devp->vmt_basebarometer = &vmt_basebarometer;
devp->config = NULL;
devp->bias = 0;
devp->state = LPS25H_STOP;
}
/**
* @brief Configures and activates LPS25H Complex Driver peripheral.
*
* @param[in] devp pointer to the @p LPS25HDriver object
* @param[in] config pointer to the @p LPS25HConfig object
*
* @api
*/
void lps25hStart(LPS25HDriver *devp, const LPS25HConfig *config) {
uint8_t cr[2];
osalDbgCheck((devp != NULL) && (config != NULL));
osalDbgAssert((devp->state == LPS25H_STOP) || (devp->state == LPS25H_READY),
"lps25hStart(), invalid state");
devp->config = config;
#if LPS25H_USE_I2C
/* Control register 1 configuration block.*/
{
cr[0] = LPS25H_AD_CTRL_REG1;
cr[1] = devp->config->outputdatarate | LPS25H_CTRL_REG1_PD;
#if LPS25H_USE_ADVANCED || defined(__DOXYGEN__)
cr[1] |= devp->config->blockdataupdate;
#endif
}
#if LPS25H_SHARED_I2C
i2cAcquireBus((devp)->config->i2cp);
#endif /* LPS25H_SHARED_I2C */
i2cStart((devp)->config->i2cp,
(devp)->config->i2ccfg);
lps25hI2CWriteRegister(devp->config->i2cp, devp->config->slaveaddress, cr, 1);
#if LPS25H_SHARED_I2C
i2cReleaseBus((devp)->config->i2cp);
#endif /* LPS25H_SHARED_I2C */
/* Resolution configuration block.*/
{
cr[0] = LPS25H_AD_RES_CONF;
cr[1] = 0x05;
#if LPS25H_USE_ADVANCED || defined(__DOXYGEN__)
cr[1] = devp->config->respressure | devp->config->restemperature;
#endif
}
#if LPS25H_SHARED_I2C
i2cAcquireBus((devp)->config->i2cp);
i2cStart((devp)->config->i2cp,
(devp)->config->i2ccfg);
#endif /* LPS25H_SHARED_I2C */
lps25hI2CWriteRegister(devp->config->i2cp, devp->config->slaveaddress,
cr, 1);
#if LPS25H_SHARED_I2C
i2cReleaseBus((devp)->config->i2cp);
#endif /* LPS25H_SHARED_I2C */
#endif /* LPS25H_USE_I2C */
if(devp->config->sensitivity == NULL) {
devp->sensitivity = LPS25H_SENS;
}
else{
/* Taking Sensitivity from user configurations */
devp->sensitivity = *devp->config->sensitivity;
}
if(devp->config->bias == NULL) {
devp->bias = 0.0f;
}
else{
/* Taking Bias from user configurations */
devp->bias = *devp->config->bias;
}
/* This is the Barometer transient recovery time */
osalThreadSleepMilliseconds(5);
devp->state = LPS25H_READY;
}
/**
* @brief Deactivates the LPS25H Complex Driver peripheral.
*
* @param[in] devp pointer to the @p LPS25HDriver object
*
* @api
*/
void lps25hStop(LPS25HDriver *devp) {
uint8_t cr[2];
osalDbgCheck(devp != NULL);
osalDbgAssert((devp->state == LPS25H_STOP) || (devp->state == LPS25H_READY),
"lps25hStop(), invalid state");
if (devp->state == LPS25H_READY) {
#if (LPS25H_USE_I2C)
#if LPS25H_SHARED_I2C
i2cAcquireBus((devp)->config->i2cp);
i2cStart((devp)->config->i2cp,
(devp)->config->i2ccfg);
#endif /* LPS25H_SHARED_I2C */
cr[0] = LPS25H_AD_CTRL_REG1;
cr[1] = 0;
lps25hI2CWriteRegister(devp->config->i2cp, devp->config->slaveaddress,
cr, 1);
i2cStop((devp)->config->i2cp);
#if LPS25H_SHARED_I2C
i2cReleaseBus((devp)->config->i2cp);
#endif /* LPS25H_SHARED_I2C */
#endif /* LPS25H_USE_I2C */
}
devp->state = LPS25H_STOP;
}
/** @} */
|