-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstm32f3_discovery.c
721 lines (619 loc) · 20.7 KB
/
stm32f3_discovery.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
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
/**
******************************************************************************
* @file stm32f3_discovery.c
* @author MCD Application Team
* @brief This file provides set of firmware functions to manage Leds and
* push-button available on STM32F3-DISCOVERY Kit from STMicroelectronics.
******************************************************************************
* @attention
*
* Copyright (c) 2016 STMicroelectronics.
* All rights reserved.
*
* This software is licensed under terms that can be found in the LICENSE file
* in the root directory of this software component.
* If no LICENSE file comes with this software, it is provided AS-IS.
*
******************************************************************************
*/
/* Includes ------------------------------------------------------------------*/
#include "stm32f3_discovery.h"
/** @addtogroup BSP
* @{
*/
/** @addtogroup STM32F3_DISCOVERY
* @brief This file provides set of firmware functions to manage Leds and push-button
* available on STM32F3-Discovery Kit from STMicroelectronics.
* @{
*/
/** @addtogroup STM32F3_DISCOVERY_Common
* @{
*/
/** @addtogroup STM32F3_DISCOVERY_Private_Constants
* @{
*/
/**
* @brief STM32F3 DISCOVERY BSP Driver version number V2.1.7
*/
#define __STM32F3_DISCO_BSP_VERSION_MAIN (0x02) /*!< [31:24] main version */
#define __STM32F3_DISCO_BSP_VERSION_SUB1 (0x01) /*!< [23:16] sub1 version */
#define __STM32F3_DISCO_BSP_VERSION_SUB2 (0x07) /*!< [15:8] sub2 version */
#define __STM32F3_DISCO_BSP_VERSION_RC (0x00) /*!< [7:0] release candidate */
#define __STM32F3_DISCO_BSP_VERSION ((__STM32F3_DISCO_BSP_VERSION_MAIN << 24)\
|(__STM32F3_DISCO_BSP_VERSION_SUB1 << 16)\
|(__STM32F3_DISCO_BSP_VERSION_SUB2 << 8 )\
|(__STM32F3_DISCO_BSP_VERSION_RC))
/**
* @}
*/
/** @addtogroup STM32F3_DISCOVERY_Private_Variables
* @{
*/
/**
* @brief LED variables
*/
GPIO_TypeDef* LED_PORT[LEDn] = {LED3_GPIO_PORT, LED4_GPIO_PORT, LED5_GPIO_PORT, LED6_GPIO_PORT,
LED7_GPIO_PORT, LED8_GPIO_PORT, LED9_GPIO_PORT, LED10_GPIO_PORT};
const uint16_t LED_PIN[LEDn] = {LED3_PIN, LED4_PIN, LED5_PIN, LED6_PIN,
LED7_PIN, LED8_PIN, LED9_PIN, LED10_PIN};
/**
* @brief BUTTON variables
*/
GPIO_TypeDef* BUTTON_PORT[BUTTONn] = {USER_BUTTON_GPIO_PORT};
const uint16_t BUTTON_PIN[BUTTONn] = {USER_BUTTON_PIN};
const uint8_t BUTTON_IRQn[BUTTONn] = {USER_BUTTON_EXTI_IRQn};
/**
* @brief BUS variables
*/
#ifdef HAL_SPI_MODULE_ENABLED
uint32_t SpixTimeout = SPIx_TIMEOUT_MAX; /*<! Value of Timeout when SPI communication fails */
static SPI_HandleTypeDef SpiHandle;
#endif
#ifdef HAL_I2C_MODULE_ENABLED
static I2C_HandleTypeDef I2cHandle;
uint32_t I2cxTimeout = I2Cx_TIMEOUT_MAX; /*<! Value of Timeout when I2C communication fails */
#endif
/**
* @}
*/
/** @defgroup STM32F3_DISCOVERY_BUS Bus Operation functions
* @{
*/
#ifdef HAL_I2C_MODULE_ENABLED
/* I2Cx bus function */
static void I2Cx_Init(void);
static void I2Cx_WriteData(uint16_t Addr, uint8_t Reg, uint8_t Value);
static uint8_t I2Cx_ReadData(uint16_t Addr, uint8_t Reg);
static void I2Cx_Error (void);
static void I2Cx_MspInit(I2C_HandleTypeDef *hi2c);
#endif
#ifdef HAL_SPI_MODULE_ENABLED
/* SPIx bus function */
static void SPIx_Init(void);
static uint8_t SPIx_WriteRead(uint8_t byte);
static void SPIx_Error (void);
static void SPIx_MspInit(SPI_HandleTypeDef *hspi);
#endif
#ifdef HAL_SPI_MODULE_ENABLED
/* Link function for GYRO peripheral */
void GYRO_IO_Init(void);
void GYRO_IO_Write(uint8_t* pBuffer, uint8_t WriteAddr, uint16_t NumByteToWrite);
void GYRO_IO_Read(uint8_t* pBuffer, uint8_t ReadAddr, uint16_t NumByteToRead);
#endif
#ifdef HAL_I2C_MODULE_ENABLED
/* Link function for COMPASS / ACCELEROMETER peripheral */
void COMPASSACCELERO_IO_Init(void);
void COMPASSACCELERO_IO_ITConfig(void);
void COMPASSACCELERO_IO_Write(uint16_t DeviceAddr, uint8_t RegisterAddr, uint8_t Value);
uint8_t COMPASSACCELERO_IO_Read(uint16_t DeviceAddr, uint8_t RegisterAddr);
#endif
/**
* @}
*/
/** @addtogroup STM32F3_DISCOVERY_Exported_Functions
* @{
*/
/**
* @brief This method returns the STM32F3-DISCOVERY BSP Driver revision
* @retval version : 0xXYZR (8bits for each decimal, R for RC)
*/
uint32_t BSP_GetVersion(void)
{
return __STM32F3_DISCO_BSP_VERSION;
}
/**
* @brief Configures LED GPIO.
* @param Led Specifies the Led to be configured.
* This parameter can be one of following parameters:
* @arg LED_RED
* @arg LED_BLUE
* @arg LED_ORANGE
* @arg LED_GREEN
* @arg LED_GREEN2
* @arg LED_ORANGE2
* @arg LED_BLUE2
* @arg LED_RED2
* @retval None
*/
void BSP_LED_Init(Led_TypeDef Led)
{
GPIO_InitTypeDef GPIO_InitStruct;
/* Enable the GPIO_LED Clock */
LEDx_GPIO_CLK_ENABLE(Led);
/* Configure the GPIO_LED pin */
GPIO_InitStruct.Pin = LED_PIN[Led];
GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP;
GPIO_InitStruct.Pull = GPIO_PULLUP;
GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_HIGH;
HAL_GPIO_Init(LED_PORT[Led], &GPIO_InitStruct);
HAL_GPIO_WritePin(LED_PORT[Led], LED_PIN[Led], GPIO_PIN_RESET);
}
/**
* @brief Turns selected LED On.
* @param Led Specifies the Led to be set on.
* This parameter can be one of following parameters:
* @arg LED_RED
* @arg LED4
* @arg LED5
* @arg LED6
* @arg LED7
* @arg LED8
* @arg LED9
* @arg LED10
* @retval None
*/
void BSP_LED_On(Led_TypeDef Led)
{
HAL_GPIO_WritePin(LED_PORT[Led], LED_PIN[Led], GPIO_PIN_SET);
}
/**
* @brief Turns selected LED Off.
* @param Led Specifies the Led to be set off.
* This parameter can be one of following parameters:
* @arg LED_RED
* @arg LED_BLUE
* @arg LED_ORANGE
* @arg LED_GREEN
* @arg LED_GREEN2
* @arg LED_ORANGE2
* @arg LED_BLUE2
* @arg LED_RED2
* @retval None
*/
void BSP_LED_Off(Led_TypeDef Led)
{
HAL_GPIO_WritePin(LED_PORT[Led], LED_PIN[Led], GPIO_PIN_RESET);
}
/**
* @brief Toggles the selected LED.
* @param Led Specifies the Led to be toggled.
* This parameter can be one of following parameters:
* @arg LED_RED
* @arg LED_BLUE
* @arg LED_ORANGE
* @arg LED_GREEN
* @arg LED_GREEN2
* @arg LED_ORANGE2
* @arg LED_BLUE2
* @arg LED_RED2
* @retval None
*/
void BSP_LED_Toggle(Led_TypeDef Led)
{
HAL_GPIO_TogglePin(LED_PORT[Led], LED_PIN[Led]);
}
/**
* @brief Configures Push Button GPIO and EXTI Line.
* @param Button Specifies the Button to be configured.
* This parameter should be: BUTTON_USER
* @param ButtonMode Specifies Button mode.
* This parameter can be one of following parameters:
* @arg BUTTON_MODE_GPIO: Button will be used as simple IO
* @arg BUTTON_MODE_EXTI: Button will be connected to EXTI line with interrupt
* generation capability
* @retval None
*/
void BSP_PB_Init(Button_TypeDef Button, ButtonMode_TypeDef ButtonMode)
{
GPIO_InitTypeDef GPIO_InitStruct;
/* Enable the BUTTON Clock */
BUTTONx_GPIO_CLK_ENABLE(Button);
__HAL_RCC_SYSCFG_CLK_ENABLE();
if (ButtonMode == BUTTON_MODE_GPIO)
{
/* Configure Button pin as input */
GPIO_InitStruct.Pin = BUTTON_PIN[Button];
GPIO_InitStruct.Mode = GPIO_MODE_INPUT;
GPIO_InitStruct.Pull = GPIO_PULLDOWN;
GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_HIGH;
HAL_GPIO_Init(BUTTON_PORT[Button], &GPIO_InitStruct);
}
if (ButtonMode == BUTTON_MODE_EXTI)
{
/* Configure Button pin as input with External interrupt */
GPIO_InitStruct.Pin = BUTTON_PIN[Button];
GPIO_InitStruct.Pull = GPIO_NOPULL;
GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_HIGH;
GPIO_InitStruct.Mode = GPIO_MODE_IT_RISING;
HAL_GPIO_Init(BUTTON_PORT[Button], &GPIO_InitStruct);
/* Enable and set Button EXTI Interrupt to the lowest priority */
HAL_NVIC_SetPriority((IRQn_Type)(BUTTON_IRQn[Button]), 0x0F, 0x00);
HAL_NVIC_EnableIRQ((IRQn_Type)(BUTTON_IRQn[Button]));
}
}
/**
* @brief Returns the selected Push Button state.
* @param Button Specifies the Button to be checked.
* This parameter should be: BUTTON_USER
* @retval The Button GPIO pin value.
*/
uint32_t BSP_PB_GetState(Button_TypeDef Button)
{
return HAL_GPIO_ReadPin(BUTTON_PORT[Button], BUTTON_PIN[Button]);
}
/**
* @}
*/
/** @addtogroup STM32F3_DISCOVERY_BUS
* @{
*/
/******************************************************************************
BUS OPERATIONS
*******************************************************************************/
#ifdef HAL_I2C_MODULE_ENABLED
/******************************* I2C Routines**********************************/
/**
* @brief Discovery I2Cx MSP Initialization
* @param hi2c I2C handle
* @retval None
*/
static void I2Cx_MspInit(I2C_HandleTypeDef *hi2c)
{
GPIO_InitTypeDef GPIO_InitStructure;
/* Enable SCK and SDA GPIO clocks */
DISCOVERY_I2Cx_GPIO_CLK_ENABLE();
/* I2Cx SD1 & SCK pin configuration */
GPIO_InitStructure.Pin = (DISCOVERY_I2Cx_SDA_PIN | DISCOVERY_I2Cx_SCL_PIN);
GPIO_InitStructure.Mode = GPIO_MODE_AF_PP;
GPIO_InitStructure.Pull = GPIO_PULLDOWN;
GPIO_InitStructure.Speed = GPIO_SPEED_FREQ_HIGH;
GPIO_InitStructure.Alternate = DISCOVERY_I2Cx_AF;
HAL_GPIO_Init(DISCOVERY_I2Cx_GPIO_PORT, &GPIO_InitStructure);
/* Enable the I2C clock */
DISCOVERY_I2Cx_CLK_ENABLE();
}
/**
* @brief Discovery I2Cx Bus initialization
* @retval None
*/
static void I2Cx_Init(void)
{
if(HAL_I2C_GetState(&I2cHandle) == HAL_I2C_STATE_RESET)
{
I2cHandle.Instance = DISCOVERY_I2Cx;
I2cHandle.Init.OwnAddress1 = ACCELERO_I2C_ADDRESS;
I2cHandle.Init.AddressingMode = I2C_ADDRESSINGMODE_7BIT;
I2cHandle.Init.DualAddressMode = I2C_DUALADDRESS_DISABLE;
I2cHandle.Init.OwnAddress2 = 0;
I2cHandle.Init.GeneralCallMode = I2C_GENERALCALL_DISABLE;
I2cHandle.Init.NoStretchMode = I2C_NOSTRETCH_DISABLE;
/* Init the I2C */
I2Cx_MspInit(&I2cHandle);
HAL_I2C_Init(&I2cHandle);
}
}
/**
* @brief Write a value in a register of the device through BUS.
* @param Addr Device address on BUS Bus.
* @param Reg The target register address to write
* @param Value The target register value to be written
* @retval None
*/
static void I2Cx_WriteData(uint16_t Addr, uint8_t Reg, uint8_t Value)
{
HAL_StatusTypeDef status = HAL_OK;
status = HAL_I2C_Mem_Write(&I2cHandle, Addr, (uint16_t)Reg, I2C_MEMADD_SIZE_8BIT, &Value, 1, I2cxTimeout);
/* Check the communication status */
if(status != HAL_OK)
{
/* Execute user timeout callback */
I2Cx_Error();
}
}
/**
* @brief Read a value in a register of the device through BUS.
* @param Addr Device address on BUS Bus.
* @param Reg The target register address to write
* @retval Data read at register @
*/
static uint8_t I2Cx_ReadData(uint16_t Addr, uint8_t Reg)
{
HAL_StatusTypeDef status = HAL_OK;
uint8_t value = 0;
status = HAL_I2C_Mem_Read(&I2cHandle, Addr, Reg, I2C_MEMADD_SIZE_8BIT, &value, 1, I2cxTimeout);
/* Check the communication status */
if(status != HAL_OK)
{
/* Execute user timeout callback */
I2Cx_Error();
}
return value;
}
/**
* @brief I2C3 error treatment function
* @retval None
*/
static void I2Cx_Error (void)
{
/* De-initialize the I2C communication BUS */
HAL_I2C_DeInit(&I2cHandle);
/* Re- Initiaize the I2C communication BUS */
I2Cx_Init();
}
#endif
#ifdef HAL_SPI_MODULE_ENABLED
/******************************* SPI Routines**********************************/
/**
* @brief SPIx Bus initialization
* @retval None
*/
static void SPIx_Init(void)
{
if(HAL_SPI_GetState(&SpiHandle) == HAL_SPI_STATE_RESET)
{
/* SPI Config */
SpiHandle.Instance = DISCOVERY_SPIx;
/* SPI baudrate is set to 5.6 MHz (PCLK2/SPI_BaudRatePrescaler = 90/16 = 5.625 MHz)
to verify these constraints:
ILI9341 LCD SPI interface max baudrate is 10MHz for write and 6.66MHz for read
l3gd20 SPI interface max baudrate is 10MHz for write/read
PCLK2 frequency is set to 90 MHz
*/
SpiHandle.Init.BaudRatePrescaler = SPI_BAUDRATEPRESCALER_16;
SpiHandle.Init.Direction = SPI_DIRECTION_2LINES;
SpiHandle.Init.CLKPhase = SPI_PHASE_1EDGE;
SpiHandle.Init.CLKPolarity = SPI_POLARITY_LOW;
SpiHandle.Init.CRCCalculation = SPI_CRCCALCULATION_DISABLE;
SpiHandle.Init.CRCPolynomial = 7;
SpiHandle.Init.DataSize = SPI_DATASIZE_8BIT;
SpiHandle.Init.FirstBit = SPI_FIRSTBIT_MSB;
SpiHandle.Init.NSS = SPI_NSS_SOFT;
SpiHandle.Init.TIMode = SPI_TIMODE_DISABLE;
SpiHandle.Init.Mode = SPI_MODE_MASTER;
SPIx_MspInit(&SpiHandle);
HAL_SPI_Init(&SpiHandle);
}
}
/**
* @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 SPIx_WriteRead(uint8_t Byte)
{
uint8_t receivedbyte = 0;
/* Send a Byte through the SPI peripheral */
/* Read byte from the SPI bus */
if(HAL_SPI_TransmitReceive(&SpiHandle, (uint8_t*) &Byte, (uint8_t*) &receivedbyte, 1, SpixTimeout) != HAL_OK)
{
SPIx_Error();
}
return receivedbyte;
}
/**
* @brief SPIx error treatment function
* @retval None
*/
static void SPIx_Error (void)
{
/* De-initialize the SPI communication BUS */
HAL_SPI_DeInit(&SpiHandle);
/* Re- Initiaize the SPI communication BUS */
SPIx_Init();
}
/**
* @brief SPI MSP Init
* @param hspi SPI handle
* @retval None
*/
static void SPIx_MspInit(SPI_HandleTypeDef *hspi)
{
GPIO_InitTypeDef GPIO_InitStructure;
/* Enable SPI1 clock */
DISCOVERY_SPIx_CLK_ENABLE();
/* enable SPI1 gpio clock */
DISCOVERY_SPIx_GPIO_CLK_ENABLE();
/* configure SPI1 SCK, MOSI and MISO */
GPIO_InitStructure.Pin = (DISCOVERY_SPIx_SCK_PIN | DISCOVERY_SPIx_MOSI_PIN | DISCOVERY_SPIx_MISO_PIN);
GPIO_InitStructure.Mode = GPIO_MODE_AF_PP;
GPIO_InitStructure.Pull = GPIO_NOPULL; /* or GPIO_PULLDOWN */
GPIO_InitStructure.Speed = GPIO_SPEED_FREQ_HIGH;
GPIO_InitStructure.Alternate = DISCOVERY_SPIx_AF;
HAL_GPIO_Init(DISCOVERY_SPIx_GPIO_PORT, &GPIO_InitStructure);
}
/**
* @}
*/
/** @defgroup STM32F3_DISCOVERY_LINK_OPERATIONS Link Operation functions
* @{
*/
/******************************************************************************
LINK OPERATIONS
*******************************************************************************/
/********************************* LINK GYROSCOPE *****************************/
/**
* @brief Configures the GYROSCOPE SPI interface.
* @retval None
*/
void GYRO_IO_Init(void)
{
GPIO_InitTypeDef GPIO_InitStructure;
/* Configure the Gyroscope Control pins ------------------------------------------*/
/* Enable CS GPIO clock and Configure GPIO PIN for Gyroscope Chip select */
GYRO_CS_GPIO_CLK_ENABLE();
GPIO_InitStructure.Pin = GYRO_CS_PIN;
GPIO_InitStructure.Mode = GPIO_MODE_OUTPUT_PP;
GPIO_InitStructure.Pull = GPIO_NOPULL;
GPIO_InitStructure.Speed = GPIO_SPEED_FREQ_HIGH;
HAL_GPIO_Init(GYRO_CS_GPIO_PORT, &GPIO_InitStructure);
/* Deselect : Chip Select high */
GYRO_CS_HIGH();
/* Enable INT1, INT2 GPIO clock and Configure GPIO PINs to detect Interrupts */
GYRO_INT_GPIO_CLK_ENABLE();
GPIO_InitStructure.Pin = GYRO_INT1_PIN | GYRO_INT2_PIN;
GPIO_InitStructure.Mode = GPIO_MODE_INPUT;
GPIO_InitStructure.Speed = GPIO_SPEED_FREQ_HIGH;
GPIO_InitStructure.Pull= GPIO_NOPULL;
HAL_GPIO_Init(GYRO_INT_GPIO_PORT, &GPIO_InitStructure);
SPIx_Init();
}
/**
* @brief Writes one byte to the GYROSCOPE.
* @param pBuffer pointer to the buffer containing the data to be written to the GYROSCOPE.
* @param WriteAddr GYROSCOPE's internal address to write to.
* @param NumByteToWrite Number of bytes to write.
* @retval None
*/
void GYRO_IO_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 */
GYRO_CS_LOW();
/* Send the Address of the indexed register */
SPIx_WriteRead(WriteAddr);
/* Send the data that will be written into the device (MSB First) */
while(NumByteToWrite >= 0x01)
{
SPIx_WriteRead(*pBuffer);
NumByteToWrite--;
pBuffer++;
}
/* Set chip select High at the end of the transmission */
GYRO_CS_HIGH();
}
/**
* @brief Reads a block of data from the GYROSCOPE.
* @param pBuffer pointer to the buffer that receives the data read from the GYROSCOPE.
* @param ReadAddr GYROSCOPE's internal address to read from.
* @param NumByteToRead number of bytes to read from the GYROSCOPE.
* @retval None
*/
void GYRO_IO_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 */
GYRO_CS_LOW();
/* Send the Address of the indexed register */
SPIx_WriteRead(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 GYROSCOPE (Slave device) */
*pBuffer = SPIx_WriteRead(DUMMY_BYTE);
NumByteToRead--;
pBuffer++;
}
/* Set chip select High at the end of the transmission */
GYRO_CS_HIGH();
}
#endif /* HAL_SPI_MODULE_ENABLED */
#ifdef HAL_I2C_MODULE_ENABLED
/********************************* LINK ACCELEROMETER *****************************/
/**
* @brief Configures COMPASS / ACCELEROMETER I2C interface.
* @retval None
*/
void COMPASSACCELERO_IO_Init(void)
{
GPIO_InitTypeDef GPIO_InitStructure;
/* Enable DRDY clock */
ACCELERO_DRDY_GPIO_CLK_ENABLE();
/* Enable INT1 & INT2 GPIO clock */
ACCELERO_INT_GPIO_CLK_ENABLE();
/* Mems DRDY pin configuration */
GPIO_InitStructure.Pin = ACCELERO_DRDY_PIN;
GPIO_InitStructure.Mode = GPIO_MODE_INPUT;
GPIO_InitStructure.Pull = GPIO_NOPULL;
GPIO_InitStructure.Speed = GPIO_SPEED_FREQ_HIGH;
HAL_GPIO_Init(ACCELERO_DRDY_GPIO_PORT, &GPIO_InitStructure);
/* Enable and set Button EXTI Interrupt to the lowest priority */
HAL_NVIC_SetPriority(ACCELERO_DRDY_EXTI_IRQn, 0x0F, 0x00);
HAL_NVIC_EnableIRQ(ACCELERO_DRDY_EXTI_IRQn);
/* Configure GPIO PINs to detect Interrupts */
GPIO_InitStructure.Pin = ACCELERO_INT1_PIN | ACCELERO_INT2_PIN;
GPIO_InitStructure.Mode = GPIO_MODE_INPUT;
GPIO_InitStructure.Speed = GPIO_SPEED_FREQ_HIGH;
GPIO_InitStructure.Pull = GPIO_NOPULL;
HAL_GPIO_Init(ACCELERO_INT_GPIO_PORT, &GPIO_InitStructure);
I2Cx_Init();
}
/**
* @brief Configures COMPASS / ACCELERO click IT
* @retval None
*/
void COMPASSACCELERO_IO_ITConfig(void)
{
GPIO_InitTypeDef GPIO_InitStructure;
/* Enable INT1 & INT2 GPIO clock */
ACCELERO_INT_GPIO_CLK_ENABLE();
/* Configure GPIO PINs to detect Interrupts */
GPIO_InitStructure.Pin = ACCELERO_INT1_PIN | ACCELERO_INT2_PIN;
GPIO_InitStructure.Mode = GPIO_MODE_IT_RISING;
GPIO_InitStructure.Speed = GPIO_SPEED_FREQ_HIGH;
GPIO_InitStructure.Pull = GPIO_NOPULL;
HAL_GPIO_Init(ACCELERO_INT_GPIO_PORT, &GPIO_InitStructure);
/* Enable and set Button EXTI Interrupt to the lowest priority */
HAL_NVIC_SetPriority(ACCELERO_INT1_EXTI_IRQn, 0x0F, 0x00);
HAL_NVIC_EnableIRQ(ACCELERO_INT1_EXTI_IRQn);
}
/**
* @brief Writes one byte to the COMPASS / ACCELEROMETER.
* @param DeviceAddr specifies the slave address to be programmed.
* @param RegisterAddr specifies the COMPASS / ACCELEROMETER register to be written.
* @param Value Data to be written
* @retval None
*/
void COMPASSACCELERO_IO_Write(uint16_t DeviceAddr, uint8_t RegisterAddr, uint8_t Value)
{
/* call I2Cx Read data bus function */
I2Cx_WriteData(DeviceAddr, RegisterAddr, Value);
}
/**
* @brief Reads a block of data from the COMPASS / ACCELEROMETER.
* @param DeviceAddr specifies the slave address to be programmed(ACC_I2C_ADDRESS or MAG_I2C_ADDRESS).
* @param RegisterAddr specifies the COMPASS / ACCELEROMETER internal address register to read from
* @retval ACCELEROMETER register value
*/
uint8_t COMPASSACCELERO_IO_Read(uint16_t DeviceAddr, uint8_t RegisterAddr)
{
/* call I2Cx Read data bus function */
return I2Cx_ReadData(DeviceAddr, RegisterAddr);
}
#endif /* HAL_I2C_MODULE_ENABLED */
/**
* @}
*/
/**
* @}
*/
/**
* @}
*/
/**
* @}
*/