/
tm_stm32f4_lis302dl_lis3dsh.h
308 lines (284 loc) · 10.4 KB
/
tm_stm32f4_lis302dl_lis3dsh.h
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
/**
* LIS302DL and LIS3DSH library for STM32F4-Discovery board
*
* @author Tilen Majerle
* @email tilen@majerle.eu
* @website http://stm32f4-discovery.com
* @link http://stm32f4-discovery.com/2014/09/library-35-lis302dl-or-lis3dsh-accelerometer
* @version v1.0
* @ide Keil uVision
* @license GNU GPL v3
*
* |----------------------------------------------------------------------
* | Copyright (C) Tilen Majerle, 2014
* |
* | This program 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
* | any later version.
* |
* | This program 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/>.
* |----------------------------------------------------------------------
*
* This library supports both devices. I connect both devices, because
* old F4-Discovery board has LIS302DL accelerometer,
* new F4-Discovery board has LIS3DSH acelerometer. New accelerometer
* has some more features for sensitivity.
*
* Default pinout on F4-Discovery board
*
* LIS STM32F4 Description
*
* MOSI PA7 Master out, Slave in for SPI1
* MISO PA6 Master in, Slave out for SPI1
* SCK PA5 Serial clock for SPI1
* CS PE3 Chip select for LIS
*
* Change default pinout. Open defines.h file and copy lines below and edit them
*
* //Select custom SPI
* //#define LIS302DL_LIS3DSH_SPI SPI1
* //#define LIS302DL_LIS3DSH_SPI_PINSPACK TM_SPI_PinsPack_1
*
* //Select custom CS pin for SPI
* //#define LIS302DL_LIS3DSH_CS_RCC RCC_AHB1Periph_GPIOE
* //#define LIS302DL_LIS3DSH_CS_PORT GPIOE
* //#define LIS302DL_LIS3DSH_CS_PIN GPIO_Pin_3
*
*/
#ifndef TM_LIS302DL_LIS3DSH_H
#define TM_LIS302DL_LIS3DSH_H 100
/**
* Library dependencies
* - STM32F4xx
* - STM32F4xx RCC
* - STM32F4xx GPIO
* - defines.h
* - TM SPI
*/
/**
* Includes
*/
#include "stm32f4xx.h"
#include "stm32f4xx_rcc.h"
#include "stm32f4xx_gpio.h"
#include "defines.h"
#include "tm_stm32f4_spi.h"
/* SPI on STM32F4-Discovery board */
#ifndef LIS302DL_LIS3DSH_SPI
#define LIS302DL_LIS3DSH_SPI SPI1
#define LIS302DL_LIS3DSH_SPI_PINSPACK TM_SPI_PinsPack_1
#endif
/* CS pin on STM32F4-Discovery board */
#ifndef LIS302DL_LIS3DSH_CS_PIN
#define LIS302DL_LIS3DSH_CS_RCC RCC_AHB1Periph_GPIOE
#define LIS302DL_LIS3DSH_CS_PORT GPIOE
#define LIS302DL_LIS3DSH_CS_PIN GPIO_Pin_3
#endif
/* CS pin settings */
#define LIS302DL_LIS3DSH_CS_LOW LIS302DL_LIS3DSH_CS_PORT->BSRRH = LIS302DL_LIS3DSH_CS_PIN
#define LIS302DL_LIS3DSH_CS_HIGH LIS302DL_LIS3DSH_CS_PORT->BSRRL = LIS302DL_LIS3DSH_CS_PIN
/* Who I am values */
#define LIS302DL_ID 0x3B
#define LIS3DSH_ID 0x3F
/* Common registers */
#define LIS302DL_LIS3DSH_REG_WHO_I_AM 0x0F
/* ----------------------------------------- */
/* LIS3DSH registers */
/* ----------------------------------------- */
#define LIS3DSH_WHO_AM_I_ADDR 0x0F
#define LIS3DSH_CTRL_REG4_ADDR 0x20
#define LIS3DSH_CTRL_REG1_ADDR 0x21
#define LIS3DSH_CTRL_REG2_ADDR 0x22
#define LIS3DSH_CTRL_REG3_ADDR 0x23
#define LIS3DSH_CTRL_REG5_ADDR 0x24
#define LIS3DSH_CTRL_REG6_ADDR 0x25
#define LIS3DSH_OUT_X_L_ADDR 0x28
#define LIS3DSH_OUT_X_H_ADDR 0x29
#define LIS3DSH_OUT_Y_L_ADDR 0x2A
#define LIS3DSH_OUT_Y_H_ADDR 0x2B
#define LIS3DSH_OUT_Z_L_ADDR 0x2C
#define LIS3DSH_OUT_Z_H_ADDR 0x2D
#define LIS3DSH_SENSITIVITY_0_06G 0.06 /* 0.06 mg/digit*/
#define LIS3DSH_SENSITIVITY_0_12G 0.12 /* 0.12 mg/digit*/
#define LIS3DSH_SENSITIVITY_0_18G 0.18 /* 0.18 mg/digit*/
#define LIS3DSH_SENSITIVITY_0_24G 0.24 /* 0.24 mg/digit*/
#define LIS3DSH_SENSITIVITY_0_73G 0.73 /* 0.73 mg/digit*/
#define LIS3DSH_DATARATE_100 ((uint8_t)0x60)
#define LIS3DSH_FULLSCALE_2 ((uint8_t)0x00) /* 2 g */
#define LIS3DSH_FULLSCALE_4 ((uint8_t)0x08) /* 4 g */
#define LIS3DSH_FULLSCALE_6 ((uint8_t)0x10) /* 6 g */
#define LIS3DSH_FULLSCALE_8 ((uint8_t)0x18) /* 8 g */
#define LIS3DSH_FULLSCALE_16 ((uint8_t)0x20) /* 16 g */
#define LIS3DSH__FULLSCALE_SELECTION ((uint8_t)0x38)
#define LIS3DSH_FILTER_BW_800 ((uint8_t)0x00) /* 800 Hz */
#define LIS3DSH_FILTER_BW_400 ((uint8_t)0x40)//((uint8_t)0x08) /* 400 Hz */
#define LIS3DSH_FILTER_BW_200 ((uint8_t)0x80)//((uint8_t)0x10) /* 200 Hz */
#define LIS3DSH_FILTER_BW_50 ((uint8_t)(0x80 | 0x40))//((uint8_t)0x18) /* 50 Hz */
#define LIS3DSH_SELFTEST_NORMAL ((uint8_t)0x00)
#define LIS3DSH_XYZ_ENABLE ((uint8_t)0x07)
#define LIS3DSH_SERIALINTERFACE_4WIRE ((uint8_t)0x00)
#define LIS3DSH_SM_ENABLE ((uint8_t)0x01)
#define LIS3DSH_SM_DISABLE ((uint8_t)0x00)
/* ----------------------------------------- */
/* LIS302DL registers */
/* ----------------------------------------- */
#define LIS302DL_CTRL_REG1_ADDR 0x20
#define LIS302DL_CTRL_REG2_ADDR 0x21
#define LIS302DL_CTRL_REG3_ADDR 0x22
#define LIS302DL_OUT_X_ADDR 0x29
#define LIS302DL_OUT_Y_ADDR 0x2B
#define LIS302DL_OUT_Z_ADDR 0x2D
#define LIS302DL_SENSITIVITY_2_3G 18 /* 18 mg/digit*/
#define LIS302DL_SENSITIVITY_9_2G 72 /* 72 mg/digit*/
#define LIS302DL_DATARATE_100 ((uint8_t)0x00)
#define LIS302DL_DATARATE_400 ((uint8_t)0x80)
#define LIS302DL_LOWPOWERMODE_ACTIVE ((uint8_t)0x40)
#define LIS302DL_FULLSCALE_2_3 ((uint8_t)0x00)
#define LIS302DL_FULLSCALE_9_2 ((uint8_t)0x20)
#define LIS302DL_SELFTEST_NORMAL ((uint8_t)0x00)
#define LIS302DL_XYZ_ENABLE ((uint8_t)0x07)
#define LIS302DL_SERIALINTERFACE_4WIRE ((uint8_t)0x00)
#define LIS302DL_BOOT_NORMALMODE ((uint8_t)0x00)
#define LIS302DL_BOOT_REBOOTMEMORY ((uint8_t)0x40)
#define LIS302DL_FILTEREDDATASELECTION_OUTPUTREGISTER ((uint8_t)0x20)
#define LIS302DL_HIGHPASSFILTERINTERRUPT_OFF ((uint8_t)0x00)
#define LIS302DL_HIGHPASSFILTERINTERRUPT_1 ((uint8_t)0x04)
#define LIS302DL_HIGHPASSFILTERINTERRUPT_2 ((uint8_t)0x08)
#define LIS302DL_HIGHPASSFILTERINTERRUPT_1_2 ((uint8_t)0x0C)
#define LIS302DL_HIGHPASSFILTER_LEVEL_0 ((uint8_t)0x00)
#define LIS302DL_HIGHPASSFILTER_LEVEL_1 ((uint8_t)0x01)
#define LIS302DL_HIGHPASSFILTER_LEVEL_2 ((uint8_t)0x02)
#define LIS302DL_HIGHPASSFILTER_LEVEL_3 ((uint8_t)0x03)
/**
* Typedef enumeration for returning from functions
*
* Parameters:
* - TM_LIS302DL_LIS3DSH_Device_Error:
* Returned from function when device is not recognized
* - TM_LIS302DL_LIS3DSH_Device_LIS302DL:
* Returned from functions when device is LIS302DL on old boards
* - TM_LIS302DL_LIS3DSH_Device_LIS3DSH:
* Returned from functions when device is LIS3DSH on new boards
*/
typedef enum {
TM_LIS302DL_LIS3DSH_Device_Error,
TM_LIS302DL_LIS3DSH_Device_LIS302DL,
TM_LIS302DL_LIS3DSH_Device_LIS3DSH
} TM_LIS302DL_LIS3DSH_Device_t;
/**
* Possible sensitivities for both devices
*
* Parameters:
* - TM_LIS3DSH_Sensitivity_2G:
* 2G sensitivity on LIS3DSH device
* - TM_LIS3DSH_Sensitivity_4G:
* 4G sensitivity on LIS3DSH device
* - TM_LIS3DSH_Sensitivity_6G:
* 6G sensitivity on LIS3DSH device
* - TM_LIS3DSH_Sensitivity_8G:
* 8G sensitivity on LIS3DSH device
* - TM_LIS3DSH_Sensitivity_16G:
* 16G sensitivity on LIS3DSH device
*
* - TM_LIS302DL_Sensitivity_2_3G:
* 2.3G sensitivity on LIS3DSH device
* - TM_LIS302DL_Sensitivity_9_2G:
* 9.2G sensitivity on LIS3DSH device
*/
typedef enum {
/* LIS3DSH */
TM_LIS3DSH_Sensitivity_2G,
TM_LIS3DSH_Sensitivity_4G,
TM_LIS3DSH_Sensitivity_6G,
TM_LIS3DSH_Sensitivity_8G,
TM_LIS3DSH_Sensitivity_16G,
/* LIS302DL */
TM_LIS302DL_Sensitivity_2_3G,
TM_LIS302DL_Sensitivity_9_2G
} TM_LIS302DL_LIS3DSH_Sensitivity_t;
/**
* Filter values for both accelerometers
*
* Parameters:
* - TM_LIS3DSH_Filter_800Hz:
* 800 Hz high pass filter on LIS3DSH
* - TM_LIS3DSH_Filter_400Hz:
* 400 Hz high pass filter on LIS3DSH
* - TM_LIS3DSH_Filter_200Hz:
* 200 Hz high pass filter on LIS3DSH
* - TM_LIS3DSH_Filter_50Hz:
* 50 Hz high pass filter on LIS3DSH
*
* - TM_LIS302DL_Filter_2Hz:
* 2 Hz filter on LIS302DL
* - TM_LIS302DL_Filter_1Hz:
* 1 Hz filter on LIS302DL
* - TM_LIS302DL_Filter_500mHz:
* 500 mHz filter on LIS302DL
* - TM_LIS302DL_Filter_250mHz
* 250 mHz filter on LIS302DL
*/
typedef enum {
/* LIS3DSH */
TM_LIS3DSH_Filter_800Hz,
TM_LIS3DSH_Filter_400Hz,
TM_LIS3DSH_Filter_200Hz,
TM_LIS3DSH_Filter_50Hz,
/* LIS302DL */
TM_LIS302DL_Filter_2Hz,
TM_LIS302DL_Filter_1Hz,
TM_LIS302DL_Filter_500mHz,
TM_LIS302DL_Filter_250mHz
} TM_LIS302DL_LIS3DSH_Filter_t;
/**
* Data structure
*
* Parameters:
* - int16_t X:
* X axis value
* - int16_t Y:
* Y axis value
* - int16_t Z:
* Z axis value
*/
typedef struct {
int16_t X;
int16_t Y;
int16_t Z;
} TM_LIS302DL_LIS3DSH_t;
/**
* Detect LIS302DL or LIS3DSH device connected on F4-Discovery board
*
* Member of TM_LIS302DL_LIS3DSH_Device_t is returned
*/
extern TM_LIS302DL_LIS3DSH_Device_t TM_LIS302DL_LIS3DSH_Detect(void);
/**
* Initialize proper device
*
* Parameters:
* - TM_LIS302DL_LIS3DSH_Sensitivity_t Sensitivity:
* Select sensitivity for device. There are separate possible sensitivities for proper device
* - TM_LIS302DL_LIS3DSH_Filter_t Filter:
* Select filter for device. There are separate possible sensitivities for proper device
*
* Member of TM_LIS302DL_LIS3DSH_Device_t is returned
*/
extern TM_LIS302DL_LIS3DSH_Device_t TM_LIS302DL_LIS3DSH_Init(TM_LIS302DL_LIS3DSH_Sensitivity_t Sensitivity, TM_LIS302DL_LIS3DSH_Filter_t Filter);
/**
* Read axes from device
*
* Parameters:
* - TM_LIS302DL_LIS3DSH_t* Axes_Data
* Pointer to TM_LIS302DL_LIS3DSH_t struct
*
* Member of TM_LIS302DL_LIS3DSH_Device_t is returned
*/
extern TM_LIS302DL_LIS3DSH_Device_t TM_LIS302DL_LIS3DSH_ReadAxes(TM_LIS302DL_LIS3DSH_t* Axes_Data);
#endif