Dependents: DiscoLogger DISCO_L476VG_GlassLCD DISCO_L476VG_MicrophoneRecorder DISCO_L476VG_UART ... more
Drivers/BSP/Components/lsm303c/lsm303c.c@5:4943b15cce9f, 2019-09-24 (annotated)
- Committer:
- jeromecoutant
- Date:
- Tue Sep 24 18:00:58 2019 +0200
- Revision:
- 5:4943b15cce9f
- Parent:
- 1:917af0ca86df
Update BSP files with CubeL4 V1.14.0
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
Jerome Coutant
1:917af0ca86df
|
1
|
/**
|
|
Jerome Coutant
1:917af0ca86df
|
2
|
******************************************************************************
|
|
Jerome Coutant
1:917af0ca86df
|
3
|
* @file lsm303c.c
|
|
Jerome Coutant
1:917af0ca86df
|
4
|
* @author MCD Application Team
|
|
Jerome Coutant
1:917af0ca86df
|
5
|
* @brief This file provides a set of functions needed to manage the LSM303C
|
|
Jerome Coutant
1:917af0ca86df
|
6
|
* MEMS accelerometer.
|
|
Jerome Coutant
1:917af0ca86df
|
7
|
******************************************************************************
|
|
Jerome Coutant
1:917af0ca86df
|
8
|
* @attention
|
|
Jerome Coutant
1:917af0ca86df
|
9
|
*
|
|
jeromecoutant | 5:4943b15cce9f | 10 | * <h2><center>© Copyright (c) 2015 STMicroelectronics. |
jeromecoutant | 5:4943b15cce9f | 11 | * All rights reserved.</center></h2> |
Jerome Coutant
1:917af0ca86df
|
12
|
*
|
|
jeromecoutant | 5:4943b15cce9f | 13 | * This software component is licensed by ST under BSD 3-Clause license, |
jeromecoutant | 5:4943b15cce9f | 14 | * the "License"; You may not use this file except in compliance with the |
jeromecoutant | 5:4943b15cce9f | 15 | * License. You may obtain a copy of the License at: |
jeromecoutant | 5:4943b15cce9f | 16 | * opensource.org/licenses/BSD-3-Clause |
Jerome Coutant
1:917af0ca86df
|
17
|
*
|
|
Jerome Coutant
1:917af0ca86df
|
18
|
******************************************************************************
|
|
Jerome Coutant
1:917af0ca86df
|
19
|
*/
|
|
Jerome Coutant
1:917af0ca86df
|
20
|
/* Includes ------------------------------------------------------------------*/
|
|
Jerome Coutant
1:917af0ca86df
|
21
|
#include "lsm303c.h"
|
|
Jerome Coutant
1:917af0ca86df
|
22
|
|
|
Jerome Coutant
1:917af0ca86df
|
23
|
/** @addtogroup BSP
|
|
Jerome Coutant
1:917af0ca86df
|
24
|
* @{
|
|
Jerome Coutant
1:917af0ca86df
|
25
|
*/
|
|
Jerome Coutant
1:917af0ca86df
|
26
|
|
|
Jerome Coutant
1:917af0ca86df
|
27
|
/** @addtogroup Components
|
|
Jerome Coutant
1:917af0ca86df
|
28
|
* @{
|
|
Jerome Coutant
1:917af0ca86df
|
29
|
*/
|
|
Jerome Coutant
1:917af0ca86df
|
30
|
|
|
Jerome Coutant
1:917af0ca86df
|
31
|
/** @addtogroup LSM303C
|
|
Jerome Coutant
1:917af0ca86df
|
32
|
* @{
|
|
Jerome Coutant
1:917af0ca86df
|
33
|
*/
|
|
Jerome Coutant
1:917af0ca86df
|
34
|
|
|
Jerome Coutant
1:917af0ca86df
|
35
|
/** @defgroup LSM303C_Private_TypesDefinitions
|
|
Jerome Coutant
1:917af0ca86df
|
36
|
* @{
|
|
Jerome Coutant
1:917af0ca86df
|
37
|
*/
|
|
Jerome Coutant
1:917af0ca86df
|
38
|
|
|
Jerome Coutant
1:917af0ca86df
|
39
|
/**
|
|
Jerome Coutant
1:917af0ca86df
|
40
|
* @}
|
|
Jerome Coutant
1:917af0ca86df
|
41
|
*/
|
|
Jerome Coutant
1:917af0ca86df
|
42
|
|
|
Jerome Coutant
1:917af0ca86df
|
43
|
/** @defgroup LSM303C_Private_Defines
|
|
Jerome Coutant
1:917af0ca86df
|
44
|
* @{
|
|
Jerome Coutant
1:917af0ca86df
|
45
|
*/
|
|
Jerome Coutant
1:917af0ca86df
|
46
|
|
|
Jerome Coutant
1:917af0ca86df
|
47
|
/**
|
|
Jerome Coutant
1:917af0ca86df
|
48
|
* @}
|
|
Jerome Coutant
1:917af0ca86df
|
49
|
*/
|
|
Jerome Coutant
1:917af0ca86df
|
50
|
|
|
Jerome Coutant
1:917af0ca86df
|
51
|
/** @defgroup LSM303C_Private_Macros
|
|
Jerome Coutant
1:917af0ca86df
|
52
|
* @{
|
|
Jerome Coutant
1:917af0ca86df
|
53
|
*/
|
|
Jerome Coutant
1:917af0ca86df
|
54
|
|
|
Jerome Coutant
1:917af0ca86df
|
55
|
/**
|
|
Jerome Coutant
1:917af0ca86df
|
56
|
* @}
|
|
Jerome Coutant
1:917af0ca86df
|
57
|
*/
|
|
Jerome Coutant
1:917af0ca86df
|
58
|
|
|
Jerome Coutant
1:917af0ca86df
|
59
|
/** @defgroup LSM303C_Private_Functions
|
|
Jerome Coutant
1:917af0ca86df
|
60
|
* @{
|
|
Jerome Coutant
1:917af0ca86df
|
61
|
*/
|
|
Jerome Coutant
1:917af0ca86df
|
62
|
|
|
Jerome Coutant
1:917af0ca86df
|
63
|
/**
|
|
Jerome Coutant
1:917af0ca86df
|
64
|
* @}
|
|
Jerome Coutant
1:917af0ca86df
|
65
|
*/
|
|
Jerome Coutant
1:917af0ca86df
|
66
|
|
|
Jerome Coutant
1:917af0ca86df
|
67
|
|
|
Jerome Coutant
1:917af0ca86df
|
68
|
/** @defgroup LSM303C_Private_Variables
|
|
Jerome Coutant
1:917af0ca86df
|
69
|
* @{
|
|
Jerome Coutant
1:917af0ca86df
|
70
|
*/
|
|
Jerome Coutant
1:917af0ca86df
|
71
|
ACCELERO_DrvTypeDef Lsm303cDrv_accelero =
|
|
Jerome Coutant
1:917af0ca86df
|
72
|
{
|
|
Jerome Coutant
1:917af0ca86df
|
73
|
LSM303C_AccInit,
|
|
Jerome Coutant
1:917af0ca86df
|
74
|
LSM303C_AccDeInit,
|
|
Jerome Coutant
1:917af0ca86df
|
75
|
LSM303C_AccReadID,
|
|
Jerome Coutant
1:917af0ca86df
|
76
|
0,
|
|
Jerome Coutant
1:917af0ca86df
|
77
|
LSM303C_AccLowPower,
|
|
Jerome Coutant
1:917af0ca86df
|
78
|
0,
|
|
Jerome Coutant
1:917af0ca86df
|
79
|
0,
|
|
Jerome Coutant
1:917af0ca86df
|
80
|
0,
|
|
Jerome Coutant
1:917af0ca86df
|
81
|
0,
|
|
Jerome Coutant
1:917af0ca86df
|
82
|
0,
|
|
Jerome Coutant
1:917af0ca86df
|
83
|
LSM303C_AccFilterConfig,
|
|
Jerome Coutant
1:917af0ca86df
|
84
|
0,
|
|
Jerome Coutant
1:917af0ca86df
|
85
|
LSM303C_AccReadXYZ
|
|
Jerome Coutant
1:917af0ca86df
|
86
|
};
|
|
Jerome Coutant
1:917af0ca86df
|
87
|
|
|
Jerome Coutant
1:917af0ca86df
|
88
|
MAGNETO_DrvTypeDef Lsm303cDrv_magneto =
|
|
Jerome Coutant
1:917af0ca86df
|
89
|
{
|
|
Jerome Coutant
1:917af0ca86df
|
90
|
LSM303C_MagInit,
|
|
Jerome Coutant
1:917af0ca86df
|
91
|
LSM303C_MagDeInit,
|
|
Jerome Coutant
1:917af0ca86df
|
92
|
LSM303C_MagReadID,
|
|
Jerome Coutant
1:917af0ca86df
|
93
|
0,
|
|
Jerome Coutant
1:917af0ca86df
|
94
|
LSM303C_MagLowPower,
|
|
Jerome Coutant
1:917af0ca86df
|
95
|
0,
|
|
Jerome Coutant
1:917af0ca86df
|
96
|
0,
|
|
Jerome Coutant
1:917af0ca86df
|
97
|
0,
|
|
Jerome Coutant
1:917af0ca86df
|
98
|
0,
|
|
Jerome Coutant
1:917af0ca86df
|
99
|
0,
|
|
Jerome Coutant
1:917af0ca86df
|
100
|
0,
|
|
Jerome Coutant
1:917af0ca86df
|
101
|
0,
|
|
Jerome Coutant
1:917af0ca86df
|
102
|
LSM303C_MagReadXYZ
|
|
Jerome Coutant
1:917af0ca86df
|
103
|
|
|
Jerome Coutant
1:917af0ca86df
|
104
|
};
|
|
Jerome Coutant
1:917af0ca86df
|
105
|
|
|
Jerome Coutant
1:917af0ca86df
|
106
|
/**
|
|
Jerome Coutant
1:917af0ca86df
|
107
|
* @}
|
|
Jerome Coutant
1:917af0ca86df
|
108
|
*/
|
|
Jerome Coutant
1:917af0ca86df
|
109
|
|
|
Jerome Coutant
1:917af0ca86df
|
110
|
|
|
Jerome Coutant
1:917af0ca86df
|
111
|
/**
|
|
Jerome Coutant
1:917af0ca86df
|
112
|
* @brief Set LSM303C Accelerometer Initialization.
|
|
Jerome Coutant
1:917af0ca86df
|
113
|
* @param InitStruct: Init parameters
|
|
Jerome Coutant
1:917af0ca86df
|
114
|
* @retval None
|
|
Jerome Coutant
1:917af0ca86df
|
115
|
*/
|
|
Jerome Coutant
1:917af0ca86df
|
116
|
void LSM303C_AccInit(uint16_t InitStruct)
|
|
Jerome Coutant
1:917af0ca86df
|
117
|
{
|
|
Jerome Coutant
1:917af0ca86df
|
118
|
uint8_t ctrl = 0x00;
|
|
Jerome Coutant
1:917af0ca86df
|
119
|
|
|
Jerome Coutant
1:917af0ca86df
|
120
|
/* Low level init */
|
|
Jerome Coutant
1:917af0ca86df
|
121
|
ACCELERO_IO_Init();
|
|
Jerome Coutant
1:917af0ca86df
|
122
|
|
|
Jerome Coutant
1:917af0ca86df
|
123
|
/* Write value to ACC MEMS CTRL_REG1 register */
|
|
Jerome Coutant
1:917af0ca86df
|
124
|
ctrl = (uint8_t) InitStruct;
|
|
Jerome Coutant
1:917af0ca86df
|
125
|
ACCELERO_IO_Write(LSM303C_CTRL_REG1_A, ctrl);
|
|
Jerome Coutant
1:917af0ca86df
|
126
|
|
|
Jerome Coutant
1:917af0ca86df
|
127
|
/* Write value to ACC MEMS CTRL_REG4 register */
|
|
Jerome Coutant
1:917af0ca86df
|
128
|
ctrl = ((uint8_t) (InitStruct >> 8));
|
|
Jerome Coutant
1:917af0ca86df
|
129
|
ACCELERO_IO_Write(LSM303C_CTRL_REG4_A, ctrl);
|
|
Jerome Coutant
1:917af0ca86df
|
130
|
}
|
|
Jerome Coutant
1:917af0ca86df
|
131
|
|
|
Jerome Coutant
1:917af0ca86df
|
132
|
/**
|
|
Jerome Coutant
1:917af0ca86df
|
133
|
* @brief LSM303C Accelerometer De-initialization.
|
|
Jerome Coutant
1:917af0ca86df
|
134
|
* @param None
|
|
Jerome Coutant
1:917af0ca86df
|
135
|
* @retval None
|
|
Jerome Coutant
1:917af0ca86df
|
136
|
*/
|
|
Jerome Coutant
1:917af0ca86df
|
137
|
void LSM303C_AccDeInit(void)
|
|
Jerome Coutant
1:917af0ca86df
|
138
|
{
|
|
Jerome Coutant
1:917af0ca86df
|
139
|
}
|
|
Jerome Coutant
1:917af0ca86df
|
140
|
|
|
Jerome Coutant
1:917af0ca86df
|
141
|
/**
|
|
Jerome Coutant
1:917af0ca86df
|
142
|
* @brief Read LSM303C ID.
|
|
Jerome Coutant
1:917af0ca86df
|
143
|
* @param None
|
|
Jerome Coutant
1:917af0ca86df
|
144
|
* @retval ID
|
|
Jerome Coutant
1:917af0ca86df
|
145
|
*/
|
|
Jerome Coutant
1:917af0ca86df
|
146
|
uint8_t LSM303C_AccReadID(void)
|
|
Jerome Coutant
1:917af0ca86df
|
147
|
{
|
|
Jerome Coutant
1:917af0ca86df
|
148
|
uint8_t ctrl = 0x00;
|
|
Jerome Coutant
1:917af0ca86df
|
149
|
|
|
Jerome Coutant
1:917af0ca86df
|
150
|
/* Low level init */
|
|
Jerome Coutant
1:917af0ca86df
|
151
|
ACCELERO_IO_Init();
|
|
Jerome Coutant
1:917af0ca86df
|
152
|
|
|
Jerome Coutant
1:917af0ca86df
|
153
|
/* Enabled SPI/I2C read communication */
|
|
Jerome Coutant
1:917af0ca86df
|
154
|
ACCELERO_IO_Write(LSM303C_CTRL_REG4_A, 0x5);
|
|
Jerome Coutant
1:917af0ca86df
|
155
|
|
|
Jerome Coutant
1:917af0ca86df
|
156
|
/* Read value at Who am I register address */
|
|
Jerome Coutant
1:917af0ca86df
|
157
|
ctrl = ACCELERO_IO_Read(LSM303C_WHO_AM_I_ADDR);
|
|
Jerome Coutant
1:917af0ca86df
|
158
|
|
|
Jerome Coutant
1:917af0ca86df
|
159
|
return ctrl;
|
|
Jerome Coutant
1:917af0ca86df
|
160
|
}
|
|
Jerome Coutant
1:917af0ca86df
|
161
|
|
|
Jerome Coutant
1:917af0ca86df
|
162
|
/**
|
|
Jerome Coutant
1:917af0ca86df
|
163
|
* @brief Put Accelerometer in power down mode or not.
|
|
Jerome Coutant
1:917af0ca86df
|
164
|
* @param Mode equal to LSM303C_ACC_ODR_OFF means enable Low Power Mode, otherwise Output data rate is set.
|
|
Jerome Coutant
1:917af0ca86df
|
165
|
* This parameter can be a value of @ref Acc_OutPut_DataRate_Selection
|
|
Jerome Coutant
1:917af0ca86df
|
166
|
* @retval None
|
|
Jerome Coutant
1:917af0ca86df
|
167
|
*/
|
|
Jerome Coutant
1:917af0ca86df
|
168
|
void LSM303C_AccLowPower(uint16_t Mode)
|
|
Jerome Coutant
1:917af0ca86df
|
169
|
{
|
|
Jerome Coutant
1:917af0ca86df
|
170
|
uint8_t ctrl = 0x00;
|
|
Jerome Coutant
1:917af0ca86df
|
171
|
|
|
Jerome Coutant
1:917af0ca86df
|
172
|
/* Read control register 1 value */
|
|
Jerome Coutant
1:917af0ca86df
|
173
|
ctrl = ACCELERO_IO_Read(LSM303C_CTRL_REG1_A);
|
|
Jerome Coutant
1:917af0ca86df
|
174
|
|
|
Jerome Coutant
1:917af0ca86df
|
175
|
/* Clear ODR bits */
|
|
Jerome Coutant
1:917af0ca86df
|
176
|
ctrl &= ~(LSM303C_ACC_ODR_BITPOSITION);
|
|
Jerome Coutant
1:917af0ca86df
|
177
|
|
|
Jerome Coutant
1:917af0ca86df
|
178
|
/* Set Power down */
|
|
Jerome Coutant
1:917af0ca86df
|
179
|
ctrl |= (uint8_t)Mode;
|
|
Jerome Coutant
1:917af0ca86df
|
180
|
|
|
Jerome Coutant
1:917af0ca86df
|
181
|
/* write back control register */
|
|
Jerome Coutant
1:917af0ca86df
|
182
|
ACCELERO_IO_Write(LSM303C_CTRL_REG1_A, ctrl);
|
|
Jerome Coutant
1:917af0ca86df
|
183
|
}
|
|
Jerome Coutant
1:917af0ca86df
|
184
|
|
|
Jerome Coutant
1:917af0ca86df
|
185
|
/**
|
|
Jerome Coutant
1:917af0ca86df
|
186
|
* @brief Set High Pass Filter Modality
|
|
Jerome Coutant
1:917af0ca86df
|
187
|
* @param FilterStruct: contains data for filter config
|
|
Jerome Coutant
1:917af0ca86df
|
188
|
* @retval None
|
|
Jerome Coutant
1:917af0ca86df
|
189
|
*/
|
|
Jerome Coutant
1:917af0ca86df
|
190
|
void LSM303C_AccFilterConfig(uint8_t FilterStruct)
|
|
Jerome Coutant
1:917af0ca86df
|
191
|
{
|
|
Jerome Coutant
1:917af0ca86df
|
192
|
uint8_t tmpreg;
|
|
Jerome Coutant
1:917af0ca86df
|
193
|
|
|
Jerome Coutant
1:917af0ca86df
|
194
|
// /* Read CTRL_REG2 register */
|
|
Jerome Coutant
1:917af0ca86df
|
195
|
// tmpreg = ACCELERO_IO_Read(LSM303C_CTRL_REG2_A);
|
|
Jerome Coutant
1:917af0ca86df
|
196
|
//
|
|
Jerome Coutant
1:917af0ca86df
|
197
|
// tmpreg &= 0x0C;
|
|
Jerome Coutant
1:917af0ca86df
|
198
|
tmpreg = FilterStruct;
|
|
Jerome Coutant
1:917af0ca86df
|
199
|
|
|
Jerome Coutant
1:917af0ca86df
|
200
|
/* Write value to ACC MEMS CTRL_REG2 register */
|
|
Jerome Coutant
1:917af0ca86df
|
201
|
ACCELERO_IO_Write(LSM303C_CTRL_REG2_A, tmpreg);
|
|
Jerome Coutant
1:917af0ca86df
|
202
|
}
|
|
Jerome Coutant
1:917af0ca86df
|
203
|
|
|
Jerome Coutant
1:917af0ca86df
|
204
|
/**
|
|
Jerome Coutant
1:917af0ca86df
|
205
|
* @brief Read X, Y & Z Acceleration values
|
|
Jerome Coutant
1:917af0ca86df
|
206
|
* @param pData: Data out pointer
|
|
Jerome Coutant
1:917af0ca86df
|
207
|
* @retval None
|
|
Jerome Coutant
1:917af0ca86df
|
208
|
*/
|
|
Jerome Coutant
1:917af0ca86df
|
209
|
void LSM303C_AccReadXYZ(int16_t* pData)
|
|
Jerome Coutant
1:917af0ca86df
|
210
|
{
|
|
Jerome Coutant
1:917af0ca86df
|
211
|
int16_t pnRawData[3];
|
|
Jerome Coutant
1:917af0ca86df
|
212
|
uint8_t ctrlx[2]={0,0};
|
|
Jerome Coutant
1:917af0ca86df
|
213
|
uint8_t buffer[6];
|
|
Jerome Coutant
1:917af0ca86df
|
214
|
uint8_t i = 0;
|
|
Jerome Coutant
1:917af0ca86df
|
215
|
uint8_t sensitivity = LSM303C_ACC_SENSITIVITY_2G;
|
|
Jerome Coutant
1:917af0ca86df
|
216
|
|
|
Jerome Coutant
1:917af0ca86df
|
217
|
/* Read the acceleration control register content */
|
|
Jerome Coutant
1:917af0ca86df
|
218
|
ctrlx[0] = ACCELERO_IO_Read(LSM303C_CTRL_REG4_A);
|
|
Jerome Coutant
1:917af0ca86df
|
219
|
ctrlx[1] = ACCELERO_IO_Read(LSM303C_CTRL_REG5_A);
|
|
Jerome Coutant
1:917af0ca86df
|
220
|
|
|
Jerome Coutant
1:917af0ca86df
|
221
|
/* Read output register X, Y & Z acceleration */
|
|
Jerome Coutant
1:917af0ca86df
|
222
|
buffer[0] = ACCELERO_IO_Read(LSM303C_OUT_X_L_A);
|
|
Jerome Coutant
1:917af0ca86df
|
223
|
buffer[1] = ACCELERO_IO_Read(LSM303C_OUT_X_H_A);
|
|
Jerome Coutant
1:917af0ca86df
|
224
|
buffer[2] = ACCELERO_IO_Read(LSM303C_OUT_Y_L_A);
|
|
Jerome Coutant
1:917af0ca86df
|
225
|
buffer[3] = ACCELERO_IO_Read(LSM303C_OUT_Y_H_A);
|
|
Jerome Coutant
1:917af0ca86df
|
226
|
buffer[4] = ACCELERO_IO_Read(LSM303C_OUT_Z_L_A);
|
|
Jerome Coutant
1:917af0ca86df
|
227
|
buffer[5] = ACCELERO_IO_Read(LSM303C_OUT_Z_H_A);
|
|
Jerome Coutant
1:917af0ca86df
|
228
|
|
|
Jerome Coutant
1:917af0ca86df
|
229
|
for(i=0; i<3; i++)
|
|
Jerome Coutant
1:917af0ca86df
|
230
|
{
|
|
Jerome Coutant
1:917af0ca86df
|
231
|
pnRawData[i]=((int16_t)((uint16_t)buffer[2*i+1] << 8) + buffer[2*i]);
|
|
Jerome Coutant
1:917af0ca86df
|
232
|
}
|
|
Jerome Coutant
1:917af0ca86df
|
233
|
|
|
Jerome Coutant
1:917af0ca86df
|
234
|
/* Normal mode */
|
|
Jerome Coutant
1:917af0ca86df
|
235
|
/* Switch the sensitivity value set in the CRTL4 */
|
|
Jerome Coutant
1:917af0ca86df
|
236
|
switch(ctrlx[0] & LSM303C_ACC_FULLSCALE_8G)
|
|
Jerome Coutant
1:917af0ca86df
|
237
|
{
|
|
Jerome Coutant
1:917af0ca86df
|
238
|
case LSM303C_ACC_FULLSCALE_2G:
|
|
Jerome Coutant
1:917af0ca86df
|
239
|
sensitivity = LSM303C_ACC_SENSITIVITY_2G;
|
|
Jerome Coutant
1:917af0ca86df
|
240
|
break;
|
|
Jerome Coutant
1:917af0ca86df
|
241
|
case LSM303C_ACC_FULLSCALE_4G:
|
|
Jerome Coutant
1:917af0ca86df
|
242
|
sensitivity = LSM303C_ACC_SENSITIVITY_4G;
|
|
Jerome Coutant
1:917af0ca86df
|
243
|
break;
|
|
Jerome Coutant
1:917af0ca86df
|
244
|
case LSM303C_ACC_FULLSCALE_8G:
|
|
Jerome Coutant
1:917af0ca86df
|
245
|
sensitivity = LSM303C_ACC_SENSITIVITY_8G;
|
|
Jerome Coutant
1:917af0ca86df
|
246
|
break;
|
|
Jerome Coutant
1:917af0ca86df
|
247
|
}
|
|
Jerome Coutant
1:917af0ca86df
|
248
|
|
|
Jerome Coutant
1:917af0ca86df
|
249
|
/* Obtain the mg value for the three axis */
|
|
Jerome Coutant
1:917af0ca86df
|
250
|
for(i=0; i<3; i++)
|
|
Jerome Coutant
1:917af0ca86df
|
251
|
{
|
|
Jerome Coutant
1:917af0ca86df
|
252
|
pData[i]=(pnRawData[i] * sensitivity);
|
|
Jerome Coutant
1:917af0ca86df
|
253
|
}
|
|
Jerome Coutant
1:917af0ca86df
|
254
|
}
|
|
Jerome Coutant
1:917af0ca86df
|
255
|
|
|
Jerome Coutant
1:917af0ca86df
|
256
|
/***********************************************************************************************
|
|
Jerome Coutant
1:917af0ca86df
|
257
|
Magnetometer driver
|
|
Jerome Coutant
1:917af0ca86df
|
258
|
***********************************************************************************************/
|
|
Jerome Coutant
1:917af0ca86df
|
259
|
|
|
Jerome Coutant
1:917af0ca86df
|
260
|
/**
|
|
Jerome Coutant
1:917af0ca86df
|
261
|
* @brief Set LSM303C Magnetometer Initialization.
|
|
Jerome Coutant
1:917af0ca86df
|
262
|
* @param LSM303C_InitStruct: pointer to a LSM303C_MagInitTypeDef structure
|
|
Jerome Coutant
1:917af0ca86df
|
263
|
* that contains the configuration setting for the LSM303C.
|
|
Jerome Coutant
1:917af0ca86df
|
264
|
* @retval None
|
|
Jerome Coutant
1:917af0ca86df
|
265
|
*/
|
|
Jerome Coutant
1:917af0ca86df
|
266
|
void LSM303C_MagInit(MAGNETO_InitTypeDef LSM303C_InitStruct)
|
|
Jerome Coutant
1:917af0ca86df
|
267
|
{
|
|
Jerome Coutant
1:917af0ca86df
|
268
|
MAGNETO_IO_Write(LSM303C_CTRL_REG1_M, LSM303C_InitStruct.Register1);
|
|
Jerome Coutant
1:917af0ca86df
|
269
|
MAGNETO_IO_Write(LSM303C_CTRL_REG2_M, LSM303C_InitStruct.Register2);
|
|
Jerome Coutant
1:917af0ca86df
|
270
|
MAGNETO_IO_Write(LSM303C_CTRL_REG3_M, LSM303C_InitStruct.Register3);
|
|
Jerome Coutant
1:917af0ca86df
|
271
|
MAGNETO_IO_Write(LSM303C_CTRL_REG4_M, LSM303C_InitStruct.Register4);
|
|
Jerome Coutant
1:917af0ca86df
|
272
|
MAGNETO_IO_Write(LSM303C_CTRL_REG5_M, LSM303C_InitStruct.Register5);
|
|
Jerome Coutant
1:917af0ca86df
|
273
|
}
|
|
Jerome Coutant
1:917af0ca86df
|
274
|
|
|
Jerome Coutant
1:917af0ca86df
|
275
|
/**
|
|
Jerome Coutant
1:917af0ca86df
|
276
|
* @brief LSM303C Magnetometer De-initialization.
|
|
Jerome Coutant
1:917af0ca86df
|
277
|
* @param None
|
|
Jerome Coutant
1:917af0ca86df
|
278
|
* @retval None
|
|
Jerome Coutant
1:917af0ca86df
|
279
|
*/
|
|
Jerome Coutant
1:917af0ca86df
|
280
|
void LSM303C_MagDeInit(void)
|
|
Jerome Coutant
1:917af0ca86df
|
281
|
{
|
|
Jerome Coutant
1:917af0ca86df
|
282
|
}
|
|
Jerome Coutant
1:917af0ca86df
|
283
|
|
|
Jerome Coutant
1:917af0ca86df
|
284
|
/**
|
|
Jerome Coutant
1:917af0ca86df
|
285
|
* @brief Read LSM303C ID.
|
|
Jerome Coutant
1:917af0ca86df
|
286
|
* @param None
|
|
Jerome Coutant
1:917af0ca86df
|
287
|
* @retval ID
|
|
Jerome Coutant
1:917af0ca86df
|
288
|
*/
|
|
Jerome Coutant
1:917af0ca86df
|
289
|
uint8_t LSM303C_MagReadID(void)
|
|
Jerome Coutant
1:917af0ca86df
|
290
|
{
|
|
Jerome Coutant
1:917af0ca86df
|
291
|
/* Low level init */
|
|
Jerome Coutant
1:917af0ca86df
|
292
|
MAGNETO_IO_Init();
|
|
Jerome Coutant
1:917af0ca86df
|
293
|
|
|
Jerome Coutant
1:917af0ca86df
|
294
|
/* Enabled the SPI/I2C read operation */
|
|
Jerome Coutant
1:917af0ca86df
|
295
|
MAGNETO_IO_Write(LSM303C_CTRL_REG3_M, 0x84);
|
|
Jerome Coutant
1:917af0ca86df
|
296
|
|
|
Jerome Coutant
1:917af0ca86df
|
297
|
/* Read value at Who am I register address */
|
|
Jerome Coutant
1:917af0ca86df
|
298
|
return MAGNETO_IO_Read(LSM303C_WHO_AM_I_ADDR);
|
|
Jerome Coutant
1:917af0ca86df
|
299
|
}
|
|
Jerome Coutant
1:917af0ca86df
|
300
|
|
|
Jerome Coutant
1:917af0ca86df
|
301
|
/**
|
|
Jerome Coutant
1:917af0ca86df
|
302
|
* @brief Put Magnetometer in power down mode or not.
|
|
Jerome Coutant
1:917af0ca86df
|
303
|
* @param Mode equal to LSM303C_MAG_POWERDOWN2_MODE means enable deepest Low Power Mode, otherwise other mode is set.
|
|
Jerome Coutant
1:917af0ca86df
|
304
|
* This parameter can be a value of @ref Mag_Operation_Mode
|
|
Jerome Coutant
1:917af0ca86df
|
305
|
* @retval None
|
|
Jerome Coutant
1:917af0ca86df
|
306
|
*/
|
|
Jerome Coutant
1:917af0ca86df
|
307
|
void LSM303C_MagLowPower(uint16_t Mode)
|
|
Jerome Coutant
1:917af0ca86df
|
308
|
{
|
|
Jerome Coutant
1:917af0ca86df
|
309
|
uint8_t ctrl = 0x00;
|
|
Jerome Coutant
1:917af0ca86df
|
310
|
|
|
Jerome Coutant
1:917af0ca86df
|
311
|
/* Read control register 1 value */
|
|
Jerome Coutant
1:917af0ca86df
|
312
|
ctrl = MAGNETO_IO_Read(LSM303C_CTRL_REG3_M);
|
|
Jerome Coutant
1:917af0ca86df
|
313
|
|
|
Jerome Coutant
1:917af0ca86df
|
314
|
/* Clear ODR bits */
|
|
Jerome Coutant
1:917af0ca86df
|
315
|
ctrl &= ~(LSM303C_MAG_SELECTION_MODE);
|
|
Jerome Coutant
1:917af0ca86df
|
316
|
|
|
Jerome Coutant
1:917af0ca86df
|
317
|
/* Set mode */
|
|
Jerome Coutant
1:917af0ca86df
|
318
|
ctrl |= (uint8_t)Mode;
|
|
Jerome Coutant
1:917af0ca86df
|
319
|
|
|
Jerome Coutant
1:917af0ca86df
|
320
|
/* write back control register */
|
|
Jerome Coutant
1:917af0ca86df
|
321
|
MAGNETO_IO_Write(LSM303C_CTRL_REG3_M, ctrl);
|
|
Jerome Coutant
1:917af0ca86df
|
322
|
}
|
|
Jerome Coutant
1:917af0ca86df
|
323
|
|
|
Jerome Coutant
1:917af0ca86df
|
324
|
/**
|
|
Jerome Coutant
1:917af0ca86df
|
325
|
* @brief Get status for Mag LSM303C data
|
|
Jerome Coutant
1:917af0ca86df
|
326
|
* @param None
|
|
Jerome Coutant
1:917af0ca86df
|
327
|
* @retval Data status in a LSM303C Data register
|
|
Jerome Coutant
1:917af0ca86df
|
328
|
*/
|
|
Jerome Coutant
1:917af0ca86df
|
329
|
uint8_t LSM303C_MagGetDataStatus(void)
|
|
Jerome Coutant
1:917af0ca86df
|
330
|
{
|
|
Jerome Coutant
1:917af0ca86df
|
331
|
/* Read Mag STATUS register */
|
|
Jerome Coutant
1:917af0ca86df
|
332
|
return MAGNETO_IO_Read(LSM303C_STATUS_REG_M);
|
|
Jerome Coutant
1:917af0ca86df
|
333
|
}
|
|
Jerome Coutant
1:917af0ca86df
|
334
|
|
|
Jerome Coutant
1:917af0ca86df
|
335
|
/**
|
|
Jerome Coutant
1:917af0ca86df
|
336
|
* @brief Read X, Y & Z Magnetometer values
|
|
Jerome Coutant
1:917af0ca86df
|
337
|
* @param pData: Data out pointer
|
|
Jerome Coutant
1:917af0ca86df
|
338
|
* @retval None
|
|
Jerome Coutant
1:917af0ca86df
|
339
|
*/
|
|
Jerome Coutant
1:917af0ca86df
|
340
|
void LSM303C_MagReadXYZ(int16_t* pData)
|
|
Jerome Coutant
1:917af0ca86df
|
341
|
{
|
|
Jerome Coutant
1:917af0ca86df
|
342
|
uint8_t ctrlx;
|
|
Jerome Coutant
1:917af0ca86df
|
343
|
uint8_t buffer[6];
|
|
Jerome Coutant
1:917af0ca86df
|
344
|
uint8_t i=0;
|
|
Jerome Coutant
1:917af0ca86df
|
345
|
|
|
Jerome Coutant
1:917af0ca86df
|
346
|
/* Read the magnetometer control register content */
|
|
Jerome Coutant
1:917af0ca86df
|
347
|
ctrlx = MAGNETO_IO_Read(LSM303C_CTRL_REG4_M);
|
|
Jerome Coutant
1:917af0ca86df
|
348
|
|
|
Jerome Coutant
1:917af0ca86df
|
349
|
/* Read output register X, Y & Z magnetometer */
|
|
Jerome Coutant
1:917af0ca86df
|
350
|
buffer[0] = MAGNETO_IO_Read(LSM303C_OUT_X_L_M);
|
|
Jerome Coutant
1:917af0ca86df
|
351
|
buffer[1] = MAGNETO_IO_Read(LSM303C_OUT_X_H_M);
|
|
Jerome Coutant
1:917af0ca86df
|
352
|
buffer[2] = MAGNETO_IO_Read(LSM303C_OUT_Y_L_M);
|
|
Jerome Coutant
1:917af0ca86df
|
353
|
buffer[3] = MAGNETO_IO_Read(LSM303C_OUT_Y_H_M);
|
|
Jerome Coutant
1:917af0ca86df
|
354
|
buffer[4] = MAGNETO_IO_Read(LSM303C_OUT_Z_L_M);
|
|
Jerome Coutant
1:917af0ca86df
|
355
|
buffer[5] = MAGNETO_IO_Read(LSM303C_OUT_Z_H_M);
|
|
Jerome Coutant
1:917af0ca86df
|
356
|
|
|
Jerome Coutant
1:917af0ca86df
|
357
|
/* Check in the control register4 the data alignment*/
|
|
Jerome Coutant
1:917af0ca86df
|
358
|
if((ctrlx & LSM303C_MAG_BLE_MSB))
|
|
Jerome Coutant
1:917af0ca86df
|
359
|
{
|
|
Jerome Coutant
1:917af0ca86df
|
360
|
for(i=0; i<3; i++)
|
|
Jerome Coutant
1:917af0ca86df
|
361
|
{
|
|
Jerome Coutant
1:917af0ca86df
|
362
|
pData[i]=((int16_t)((uint16_t)buffer[2*i] << 8) + buffer[2*i+1]);
|
|
Jerome Coutant
1:917af0ca86df
|
363
|
}
|
|
Jerome Coutant
1:917af0ca86df
|
364
|
}
|
|
Jerome Coutant
1:917af0ca86df
|
365
|
else
|
|
Jerome Coutant
1:917af0ca86df
|
366
|
{
|
|
Jerome Coutant
1:917af0ca86df
|
367
|
for(i=0; i<3; i++)
|
|
Jerome Coutant
1:917af0ca86df
|
368
|
{
|
|
Jerome Coutant
1:917af0ca86df
|
369
|
pData[i]=((int16_t)((uint16_t)buffer[2*i+1] << 8) + buffer[2*i]);
|
|
Jerome Coutant
1:917af0ca86df
|
370
|
}
|
|
Jerome Coutant
1:917af0ca86df
|
371
|
}
|
|
Jerome Coutant
1:917af0ca86df
|
372
|
}
|
|
Jerome Coutant
1:917af0ca86df
|
373
|
|
|
Jerome Coutant
1:917af0ca86df
|
374
|
/**
|
|
Jerome Coutant
1:917af0ca86df
|
375
|
* @}
|
|
Jerome Coutant
1:917af0ca86df
|
376
|
*/
|
|
Jerome Coutant
1:917af0ca86df
|
377
|
|
|
Jerome Coutant
1:917af0ca86df
|
378
|
/**
|
|
Jerome Coutant
1:917af0ca86df
|
379
|
* @}
|
|
Jerome Coutant
1:917af0ca86df
|
380
|
*/
|
|
Jerome Coutant
1:917af0ca86df
|
381
|
|
|
Jerome Coutant
1:917af0ca86df
|
382
|
/**
|
|
Jerome Coutant
1:917af0ca86df
|
383
|
* @}
|
|
Jerome Coutant
1:917af0ca86df
|
384
|
*/
|
|
Jerome Coutant
1:917af0ca86df
|
385
|
|
|
Jerome Coutant
1:917af0ca86df
|
386
|
/**
|
|
Jerome Coutant
1:917af0ca86df
|
387
|
* @}
|
|
Jerome Coutant
1:917af0ca86df
|
388
|
*/
|
|
Jerome Coutant
1:917af0ca86df
|
389
|
|
|
Jerome Coutant
1:917af0ca86df
|
390
|
/************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/
|
|