-
Notifications
You must be signed in to change notification settings - Fork 1
/
stm32f3_discovery_l3gd20.c
489 lines (402 loc) · 14 KB
/
stm32f3_discovery_l3gd20.c
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
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
/**
******************************************************************************
* @file stm32f3_discovery_l3gd20.c
* @author MCD Application Team
* @version V1.1.0
* @date 20-September-2012
* @brief This file provides a set of functions needed to manage the l3gd20
* MEMS accelerometer available on STM32F3-Discovery Kit.
******************************************************************************
* @attention
*
* <h2><center>© COPYRIGHT 2012 STMicroelectronics</center></h2>
*
* Licensed under MCD-ST Liberty SW License Agreement V2, (the "License");
* You may not use this file except in compliance with the License.
* You may obtain a copy of the License at:
*
* http://www.st.com/software_license_agreement_liberty_v2
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
******************************************************************************
*/
/* Includes ------------------------------------------------------------------*/
#include "stm32f3_discovery_l3gd20.h"
/** @addtogroup Utilities
* @{
*/
/** @addtogroup STM32F3_DISCOVERY
* @{
*/
/** @addtogroup STM32F3_DISCOVERY_L3GD20
* @{
*/
/** @defgroup STM32F3_DISCOVERY_L3GD20_Private_TypesDefinitions
* @{
*/
/**
* @}
*/
/** @defgroup STM32F3_DISCOVERY_L3GD20_Private_Defines
* @{
*/
/**
* @}
*/
/** @defgroup STM32F3_DISCOVERY_L3GD20_Private_Macros
* @{
*/
/**
* @}
*/
/** @defgroup STM32F3_DISCOVERY_L3GD20_Private_Variables
* @{
*/
__IO uint32_t L3GD20Timeout = L3GD20_FLAG_TIMEOUT;
/**
* @}
*/
/** @defgroup STM32F3_DISCOVERY_L3GD20_Private_FunctionPrototypes
* @{
*/
static uint8_t L3GD20_SendByte(uint8_t byte);
static void L3GD20_LowLevel_Init(void);
/**
* @}
*/
/** @defgroup STM32F3_DISCOVERY_L3GD20_Private_Functions
* @{
*/
/**
* @brief Set L3GD20 Initialization.
* @param L3GD20_InitStruct: pointer to a L3GD20_InitTypeDef structure
* that contains the configuration setting for the L3GD20.
* @retval None
*/
void L3GD20_Init(L3GD20_InitTypeDef *L3GD20_InitStruct)
{
uint8_t ctrl1 = 0x00, ctrl4 = 0x00;
/* Configure the low level interface ---------------------------------------*/
L3GD20_LowLevel_Init();
/* Configure MEMS: data rate, power mode, full scale and axes */
ctrl1 |= (uint8_t) (L3GD20_InitStruct->Power_Mode | L3GD20_InitStruct->Output_DataRate | \
L3GD20_InitStruct->Axes_Enable | L3GD20_InitStruct->Band_Width);
ctrl4 |= (uint8_t) (L3GD20_InitStruct->BlockData_Update | L3GD20_InitStruct->Endianness | \
L3GD20_InitStruct->Full_Scale);
/* Write value to MEMS CTRL_REG1 regsister */
L3GD20_Write(&ctrl1, L3GD20_CTRL_REG1_ADDR, 1);
/* Write value to MEMS CTRL_REG4 regsister */
L3GD20_Write(&ctrl4, L3GD20_CTRL_REG4_ADDR, 1);
}
/**
* @brief Reboot memory content of L3GD20
* @param None
* @retval None
*/
void L3GD20_RebootCmd(void)
{
uint8_t tmpreg;
/* Read CTRL_REG5 register */
L3GD20_Read(&tmpreg, L3GD20_CTRL_REG5_ADDR, 1);
/* Enable or Disable the reboot memory */
tmpreg |= L3GD20_BOOT_REBOOTMEMORY;
/* Write value to MEMS CTRL_REG5 regsister */
L3GD20_Write(&tmpreg, L3GD20_CTRL_REG5_ADDR, 1);
}
/**
* @brief Set L3GD20 Interrupt configuration
* @param L3GD20_InterruptConfig_TypeDef: pointer to a L3GD20_InterruptConfig_TypeDef
* structure that contains the configuration setting for the L3GD20 Interrupt.
* @retval None
*/
void L3GD20_INT1InterruptConfig(L3GD20_InterruptConfigTypeDef *L3GD20_IntConfigStruct)
{
uint8_t ctrl_cfr = 0x00, ctrl3 = 0x00;
/* Read INT1_CFG register */
L3GD20_Read(&ctrl_cfr, L3GD20_INT1_CFG_ADDR, 1);
/* Read CTRL_REG3 register */
L3GD20_Read(&ctrl3, L3GD20_CTRL_REG3_ADDR, 1);
ctrl_cfr &= 0x80;
ctrl3 &= 0xDF;
/* Configure latch Interrupt request and axe interrupts */
ctrl_cfr |= (uint8_t)(L3GD20_IntConfigStruct->Latch_Request| \
L3GD20_IntConfigStruct->Interrupt_Axes);
ctrl3 |= (uint8_t)(L3GD20_IntConfigStruct->Interrupt_ActiveEdge);
/* Write value to MEMS INT1_CFG register */
L3GD20_Write(&ctrl_cfr, L3GD20_INT1_CFG_ADDR, 1);
/* Write value to MEMS CTRL_REG3 register */
L3GD20_Write(&ctrl3, L3GD20_CTRL_REG3_ADDR, 1);
}
/**
* @brief Enable or disable INT1 interrupt
* @param InterruptState: State of INT1 Interrupt
* This parameter can be:
* @arg L3GD20_INT1INTERRUPT_DISABLE
* @arg L3GD20_INT1INTERRUPT_ENABLE
* @retval None
*/
void L3GD20_INT1InterruptCmd(uint8_t InterruptState)
{
uint8_t tmpreg;
/* Read CTRL_REG3 register */
L3GD20_Read(&tmpreg, L3GD20_CTRL_REG3_ADDR, 1);
tmpreg &= 0x7F;
tmpreg |= InterruptState;
/* Write value to MEMS CTRL_REG3 regsister */
L3GD20_Write(&tmpreg, L3GD20_CTRL_REG3_ADDR, 1);
}
/**
* @brief Enable or disable INT2 interrupt
* @param InterruptState: State of INT1 Interrupt
* This parameter can be:
* @arg L3GD20_INT2INTERRUPT_DISABLE
* @arg L3GD20_INT2INTERRUPT_ENABLE
* @retval None
*/
void L3GD20_INT2InterruptCmd(uint8_t InterruptState)
{
uint8_t tmpreg;
/* Read CTRL_REG3 register */
L3GD20_Read(&tmpreg, L3GD20_CTRL_REG3_ADDR, 1);
tmpreg &= 0xF7;
tmpreg |= InterruptState;
/* Write value to MEMS CTRL_REG3 regsister */
L3GD20_Write(&tmpreg, L3GD20_CTRL_REG3_ADDR, 1);
}
/**
* @brief Set High Pass Filter Modality
* @param L3GD20_FilterStruct: pointer to a L3GD20_FilterConfigTypeDef structure
* that contains the configuration setting for the L3GD20.
* @retval None
*/
void L3GD20_FilterConfig(L3GD20_FilterConfigTypeDef *L3GD20_FilterStruct)
{
uint8_t tmpreg;
/* Read CTRL_REG2 register */
L3GD20_Read(&tmpreg, L3GD20_CTRL_REG2_ADDR, 1);
tmpreg &= 0xC0;
/* Configure MEMS: mode and cutoff frquency */
tmpreg |= (uint8_t) (L3GD20_FilterStruct->HighPassFilter_Mode_Selection |\
L3GD20_FilterStruct->HighPassFilter_CutOff_Frequency);
/* Write value to MEMS CTRL_REG2 regsister */
L3GD20_Write(&tmpreg, L3GD20_CTRL_REG2_ADDR, 1);
}
/**
* @brief Enable or Disable High Pass Filter
* @param HighPassFilterState: new state of the High Pass Filter feature.
* This parameter can be:
* @arg: L3GD20_HIGHPASSFILTER_DISABLE
* @arg: L3GD20_HIGHPASSFILTER_ENABLE
* @retval None
*/
void L3GD20_FilterCmd(uint8_t HighPassFilterState)
{
uint8_t tmpreg;
/* Read CTRL_REG5 register */
L3GD20_Read(&tmpreg, L3GD20_CTRL_REG5_ADDR, 1);
tmpreg &= 0xEF;
tmpreg |= HighPassFilterState;
/* Write value to MEMS CTRL_REG5 regsister */
L3GD20_Write(&tmpreg, L3GD20_CTRL_REG5_ADDR, 1);
}
/**
* @brief Get status for L3GD20 data
* @param None
* @retval Data status in a L3GD20 Data
*/
uint8_t L3GD20_GetDataStatus(void)
{
uint8_t tmpreg;
/* Read STATUS_REG register */
L3GD20_Read(&tmpreg, L3GD20_STATUS_REG_ADDR, 1);
return tmpreg;
}
/**
* @brief Writes one byte to the L3GD20.
* @param pBuffer : pointer to the buffer containing the data to be written to the L3GD20.
* @param WriteAddr : L3GD20's internal address to write to.
* @param NumByteToWrite: Number of bytes to write.
* @retval None
*/
void L3GD20_Write(uint8_t* pBuffer, uint8_t WriteAddr, uint16_t NumByteToWrite)
{
/* Configure the MS bit:
- When 0, the address will remain unchanged in multiple read/write commands.
- When 1, the address will be auto incremented in multiple read/write commands.
*/
if(NumByteToWrite > 0x01)
{
WriteAddr |= (uint8_t)MULTIPLEBYTE_CMD;
}
/* Set chip select Low at the start of the transmission */
L3GD20_CS_LOW();
/* Send the Address of the indexed register */
L3GD20_SendByte(WriteAddr);
/* Send the data that will be written into the device (MSB First) */
while(NumByteToWrite >= 0x01)
{
L3GD20_SendByte(*pBuffer);
NumByteToWrite--;
pBuffer++;
}
/* Set chip select High at the end of the transmission */
L3GD20_CS_HIGH();
}
/**
* @brief Reads a block of data from the L3GD20.
* @param pBuffer : pointer to the buffer that receives the data read from the L3GD20.
* @param ReadAddr : L3GD20's internal address to read from.
* @param NumByteToRead : number of bytes to read from the L3GD20.
* @retval None
*/
void L3GD20_Read(uint8_t* pBuffer, uint8_t ReadAddr, uint16_t NumByteToRead)
{
if(NumByteToRead > 0x01)
{
ReadAddr |= (uint8_t)(READWRITE_CMD | MULTIPLEBYTE_CMD);
}
else
{
ReadAddr |= (uint8_t)READWRITE_CMD;
}
/* Set chip select Low at the start of the transmission */
L3GD20_CS_LOW();
/* Send the Address of the indexed register */
L3GD20_SendByte(ReadAddr);
/* Receive the data that will be read from the device (MSB First) */
while(NumByteToRead > 0x00)
{
/* Send dummy byte (0x00) to generate the SPI clock to L3GD20 (Slave device) */
*pBuffer = L3GD20_SendByte(DUMMY_BYTE);
NumByteToRead--;
pBuffer++;
}
/* Set chip select High at the end of the transmission */
L3GD20_CS_HIGH();
}
/**
* @brief Initializes the low level interface used to drive the L3GD20
* @param None
* @retval None
*/
static void L3GD20_LowLevel_Init(void)
{
GPIO_InitTypeDef GPIO_InitStructure;
SPI_InitTypeDef SPI_InitStructure;
/* Enable the SPI periph */
RCC_APB2PeriphClockCmd(L3GD20_SPI_CLK, ENABLE);
/* Enable SCK, MOSI and MISO GPIO clocks */
RCC_AHBPeriphClockCmd(L3GD20_SPI_SCK_GPIO_CLK | L3GD20_SPI_MISO_GPIO_CLK | L3GD20_SPI_MOSI_GPIO_CLK, ENABLE);
/* Enable CS GPIO clock */
RCC_AHBPeriphClockCmd(L3GD20_SPI_CS_GPIO_CLK, ENABLE);
/* Enable INT1 GPIO clock */
RCC_AHBPeriphClockCmd(L3GD20_SPI_INT1_GPIO_CLK, ENABLE);
/* Enable INT2 GPIO clock */
RCC_AHBPeriphClockCmd(L3GD20_SPI_INT2_GPIO_CLK, ENABLE);
GPIO_PinAFConfig(L3GD20_SPI_SCK_GPIO_PORT, L3GD20_SPI_SCK_SOURCE, L3GD20_SPI_SCK_AF);
GPIO_PinAFConfig(L3GD20_SPI_MISO_GPIO_PORT, L3GD20_SPI_MISO_SOURCE, L3GD20_SPI_MISO_AF);
GPIO_PinAFConfig(L3GD20_SPI_MOSI_GPIO_PORT, L3GD20_SPI_MOSI_SOURCE, L3GD20_SPI_MOSI_AF);
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;
GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL;//GPIO_PuPd_DOWN;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
/* SPI SCK pin configuration */
GPIO_InitStructure.GPIO_Pin = L3GD20_SPI_SCK_PIN;
GPIO_Init(L3GD20_SPI_SCK_GPIO_PORT, &GPIO_InitStructure);
/* SPI MOSI pin configuration */
GPIO_InitStructure.GPIO_Pin = L3GD20_SPI_MOSI_PIN;
GPIO_Init(L3GD20_SPI_MOSI_GPIO_PORT, &GPIO_InitStructure);
/* SPI MISO pin configuration */
GPIO_InitStructure.GPIO_Pin = L3GD20_SPI_MISO_PIN;
GPIO_Init(L3GD20_SPI_MISO_GPIO_PORT, &GPIO_InitStructure);
/* SPI configuration -------------------------------------------------------*/
SPI_I2S_DeInit(L3GD20_SPI);
SPI_InitStructure.SPI_Direction = SPI_Direction_2Lines_FullDuplex;
SPI_InitStructure.SPI_DataSize = SPI_DataSize_8b;
SPI_InitStructure.SPI_CPOL = SPI_CPOL_Low;
SPI_InitStructure.SPI_CPHA = SPI_CPHA_1Edge;
SPI_InitStructure.SPI_NSS = SPI_NSS_Soft;
SPI_InitStructure.SPI_BaudRatePrescaler = SPI_BaudRatePrescaler_8;
SPI_InitStructure.SPI_FirstBit = SPI_FirstBit_MSB;
SPI_InitStructure.SPI_CRCPolynomial = 7;
SPI_InitStructure.SPI_Mode = SPI_Mode_Master;
SPI_Init(L3GD20_SPI, &SPI_InitStructure);
/* Configure the RX FIFO Threshold */
SPI_RxFIFOThresholdConfig(L3GD20_SPI, SPI_RxFIFOThreshold_QF);
/* Enable SPI1 */
SPI_Cmd(L3GD20_SPI, ENABLE);
/* Configure GPIO PIN for Lis Chip select */
GPIO_InitStructure.GPIO_Pin = L3GD20_SPI_CS_PIN;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_OUT;
GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(L3GD20_SPI_CS_GPIO_PORT, &GPIO_InitStructure);
/* Deselect : Chip Select high */
GPIO_SetBits(L3GD20_SPI_CS_GPIO_PORT, L3GD20_SPI_CS_PIN);
/* Configure GPIO PINs to detect Interrupts */
GPIO_InitStructure.GPIO_Pin = L3GD20_SPI_INT1_PIN;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN;
GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL;
GPIO_Init(L3GD20_SPI_INT1_GPIO_PORT, &GPIO_InitStructure);
GPIO_InitStructure.GPIO_Pin = L3GD20_SPI_INT2_PIN;
GPIO_Init(L3GD20_SPI_INT2_GPIO_PORT, &GPIO_InitStructure);
}
/**
* @brief Sends a Byte through the SPI interface and return the Byte received
* from the SPI bus.
* @param Byte : Byte send.
* @retval The received byte value
*/
static uint8_t L3GD20_SendByte(uint8_t byte)
{
/* Loop while DR register in not empty */
L3GD20Timeout = L3GD20_FLAG_TIMEOUT;
while (SPI_I2S_GetFlagStatus(L3GD20_SPI, SPI_I2S_FLAG_TXE) == RESET)
{
if((L3GD20Timeout--) == 0) return L3GD20_TIMEOUT_UserCallback();
}
/* Send a Byte through the SPI peripheral */
SPI_SendData8(L3GD20_SPI, byte);
/* Wait to receive a Byte */
L3GD20Timeout = L3GD20_FLAG_TIMEOUT;
while (SPI_I2S_GetFlagStatus(L3GD20_SPI, SPI_I2S_FLAG_RXNE) == RESET)
{
if((L3GD20Timeout--) == 0) return L3GD20_TIMEOUT_UserCallback();
}
/* Return the Byte read from the SPI bus */
return (uint8_t)SPI_ReceiveData8(L3GD20_SPI);
}
#ifdef USE_DEFAULT_TIMEOUT_CALLBACK
/**
* @brief Basic management of the timeout situation.
* @param None.
* @retval None.
*/
uint32_t L3GD20_TIMEOUT_UserCallback(void)
{
/* Block communication and all processes */
while (1)
{
}
}
#endif /* USE_DEFAULT_TIMEOUT_CALLBACK */
/**
* @}
*/
/**
* @}
*/
/**
* @}
*/
/**
* @}
*/
/************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/