1 /**
2 ******************************************************************************
3 * @file stm32f4_discovery.c
4 * @author MCD Application Team
5 * @version V2.1.2
6 * @date 27-January-2017
7 * @brief This file provides set of firmware functions to manage Leds and
8 * push-button available on STM32F4-Discovery Kit from STMicroelectronics.
9 ******************************************************************************
10 * @attention
11 *
12 * <h2><center>© COPYRIGHT(c) 2017 STMicroelectronics</center></h2>
13 *
14 * Redistribution and use in source and binary forms, with or without modification,
15 * are permitted provided that the following conditions are met:
16 * 1. Redistributions of source code must retain the above copyright notice,
17 * this list of conditions and the following disclaimer.
18 * 2. Redistributions in binary form must reproduce the above copyright notice,
19 * this list of conditions and the following disclaimer in the documentation
20 * and/or other materials provided with the distribution.
21 * 3. Neither the name of STMicroelectronics nor the names of its contributors
22 * may be used to endorse or promote products derived from this software
23 * without specific prior written permission.
24 *
25 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
26 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
27 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
28 * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
29 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
30 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
31 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
32 * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
33 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
34 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
35 *
36 ******************************************************************************
37 */
38 /* Includes ------------------------------------------------------------------*/
39 #include "stm32f4_discovery.h"
40
41 /** @defgroup BSP BSP
42 * @{
43 */
44
45 /** @defgroup STM32F4_DISCOVERY STM32F4 DISCOVERY
46 * @{
47 */
48
49 /** @defgroup STM32F4_DISCOVERY_LOW_LEVEL STM32F4 DISCOVERY LOW LEVEL
50 * @brief This file provides set of firmware functions to manage Leds and push-button
51 * available on STM32F4-Discovery Kit from STMicroelectronics.
52 * @{
53 */
54
55 /** @defgroup STM32F4_DISCOVERY_LOW_LEVEL_Private_TypesDefinitions STM32F4 DISCOVERY LOW LEVEL Private TypesDefinitions
56 * @{
57 */
58 /**
59 * @}
60 */
61
62 /** @defgroup STM32F4_DISCOVERY_LOW_LEVEL_Private_Defines STM32F4 DISCOVERY LOW LEVEL Private Defines
63 * @{
64 */
65
66 /**
67 * @brief STM32F4 DISCO BSP Driver version number V2.1.2
68 */
69 #define __STM32F4_DISCO_BSP_VERSION_MAIN (0x02) /*!< [31:24] main version */
70 #define __STM32F4_DISCO_BSP_VERSION_SUB1 (0x01) /*!< [23:16] sub1 version */
71 #define __STM32F4_DISCO_BSP_VERSION_SUB2 (0x02) /*!< [15:8] sub2 version */
72 #define __STM32F4_DISCO_BSP_VERSION_RC (0x00) /*!< [7:0] release candidate */
73 #define __STM32F4_DISCO_BSP_VERSION ((__STM32F4_DISCO_BSP_VERSION_MAIN << 24)\
74 |(__STM32F4_DISCO_BSP_VERSION_SUB1 << 16)\
75 |(__STM32F4_DISCO_BSP_VERSION_SUB2 << 8 )\
76 |(__STM32F4_DISCO_BSP_VERSION_RC))
77 /**
78 * @}
79 */
80
81
82 /** @defgroup STM32F4_DISCOVERY_LOW_LEVEL_Private_Macros STM32F4 DISCOVERY LOW LEVEL Private Macros
83 * @{
84 */
85 /**
86 * @}
87 */
88
89 /** @defgroup STM32F4_DISCOVERY_LOW_LEVEL_Private_Variables STM32F4 DISCOVERY LOW LEVEL Private Variables
90 * @{
91 */
92 GPIO_TypeDef* GPIO_PORT[LEDn] = {LED4_GPIO_PORT,
93 LED3_GPIO_PORT,
94 LED5_GPIO_PORT,
95 LED6_GPIO_PORT};
96 const uint16_t GPIO_PIN[LEDn] = {LED4_PIN,
97 LED3_PIN,
98 LED5_PIN,
99 LED6_PIN};
100
101 GPIO_TypeDef* BUTTON_PORT[BUTTONn] = {KEY_BUTTON_GPIO_PORT};
102 const uint16_t BUTTON_PIN[BUTTONn] = {KEY_BUTTON_PIN};
103 const uint8_t BUTTON_IRQn[BUTTONn] = {KEY_BUTTON_EXTI_IRQn};
104
105 uint32_t I2cxTimeout = I2Cx_TIMEOUT_MAX; /*<! Value of Timeout when I2C communication fails */
106 uint32_t SpixTimeout = SPIx_TIMEOUT_MAX; /*<! Value of Timeout when SPI communication fails */
107
108 static SPI_HandleTypeDef SpiHandle;
109 static I2C_HandleTypeDef I2cHandle;
110 /**
111 * @}
112 */
113
114 /** @defgroup STM32F4_DISCOVERY_LOW_LEVEL_Private_FunctionPrototypes STM32F4 DISCOVERY LOW LEVEL Private FunctionPrototypes
115 * @{
116 */
117 /**
118 * @}
119 */
120
121 /** @defgroup STM32F4_DISCOVERY_LOW_LEVEL_Private_Functions STM32F4 DISCOVERY LOW LEVEL Private Functions
122 * @{
123 */
124 static void I2Cx_Init(void);
125 static void I2Cx_WriteData(uint8_t Addr, uint8_t Reg, uint8_t Value);
126 static uint8_t I2Cx_ReadData(uint8_t Addr, uint8_t Reg);
127 static void I2Cx_MspInit(void);
128 static void I2Cx_Error(uint8_t Addr);
129
130 static void SPIx_Init(void);
131 static void SPIx_MspInit(void);
132 static uint8_t SPIx_WriteRead(uint8_t Byte);
133 static void SPIx_Error(void);
134
135 /* Link functions for Accelerometer peripheral */
136 void ACCELERO_IO_Init(void);
137 void ACCELERO_IO_ITConfig(void);
138 void ACCELERO_IO_Write(uint8_t *pBuffer, uint8_t WriteAddr, uint16_t NumByteToWrite);
139 void ACCELERO_IO_Read(uint8_t *pBuffer, uint8_t ReadAddr, uint16_t NumByteToRead);
140
141 /* Link functions for Audio peripheral */
142 void AUDIO_IO_Init(void);
143 void AUDIO_IO_DeInit(void);
144 void AUDIO_IO_Write(uint8_t Addr, uint8_t Reg, uint8_t Value);
145 uint8_t AUDIO_IO_Read(uint8_t Addr, uint8_t Reg);
146 /**
147 * @}
148 */
149
150 /** @defgroup STM32F4_DISCOVERY_LOW_LEVEL_LED_Functions STM32F4 DISCOVERY LOW LEVEL LED Functions
151 * @{
152 */
153
154 /**
155 * @brief This method returns the STM32F4 DISCO BSP Driver revision
156 * @retval version : 0xXYZR (8bits for each decimal, R for RC)
157 */
BSP_GetVersion(void)158 uint32_t BSP_GetVersion(void)
159 {
160 return __STM32F4_DISCO_BSP_VERSION;
161 }
162
163 /**
164 * @brief Configures LED GPIO.
165 * @param Led: Specifies the Led to be configured.
166 * This parameter can be one of following parameters:
167 * @arg LED4
168 * @arg LED3
169 * @arg LED5
170 * @arg LED6
171 */
BSP_LED_Init(Led_TypeDef Led)172 void BSP_LED_Init(Led_TypeDef Led)
173 {
174 GPIO_InitTypeDef GPIO_InitStruct;
175
176 /* Enable the GPIO_LED Clock */
177 LEDx_GPIO_CLK_ENABLE(Led);
178
179 /* Configure the GPIO_LED pin */
180 GPIO_InitStruct.Pin = GPIO_PIN[Led];
181 GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP;
182 GPIO_InitStruct.Pull = GPIO_PULLUP;
183 GPIO_InitStruct.Speed = GPIO_SPEED_FAST;
184
185 HAL_GPIO_Init(GPIO_PORT[Led], &GPIO_InitStruct);
186
187 HAL_GPIO_WritePin(GPIO_PORT[Led], GPIO_PIN[Led], GPIO_PIN_RESET);
188 }
189
190 /**
191 * @brief Turns selected LED On.
192 * @param Led: Specifies the Led to be set on.
193 * This parameter can be one of following parameters:
194 * @arg LED4
195 * @arg LED3
196 * @arg LED5
197 * @arg LED6
198 */
BSP_LED_On(Led_TypeDef Led)199 void BSP_LED_On(Led_TypeDef Led)
200 {
201 HAL_GPIO_WritePin(GPIO_PORT[Led], GPIO_PIN[Led], GPIO_PIN_SET);
202 }
203
204 /**
205 * @brief Turns selected LED Off.
206 * @param Led: Specifies the Led to be set off.
207 * This parameter can be one of following parameters:
208 * @arg LED4
209 * @arg LED3
210 * @arg LED5
211 * @arg LED6
212 */
BSP_LED_Off(Led_TypeDef Led)213 void BSP_LED_Off(Led_TypeDef Led)
214 {
215 HAL_GPIO_WritePin(GPIO_PORT[Led], GPIO_PIN[Led], GPIO_PIN_RESET);
216 }
217
218 /**
219 * @brief Toggles the selected LED.
220 * @param Led: Specifies the Led to be toggled.
221 * This parameter can be one of following parameters:
222 * @arg LED4
223 * @arg LED3
224 * @arg LED5
225 * @arg LED6
226 */
BSP_LED_Toggle(Led_TypeDef Led)227 void BSP_LED_Toggle(Led_TypeDef Led)
228 {
229 HAL_GPIO_TogglePin(GPIO_PORT[Led], GPIO_PIN[Led]);
230 }
231
232 /**
233 * @}
234 */
235
236 /** @defgroup STM32F4_DISCOVERY_LOW_LEVEL_BUTTON_Functions STM32F4 DISCOVERY LOW LEVEL BUTTON Functions
237 * @{
238 */
239
240 /**
241 * @brief Configures Button GPIO and EXTI Line.
242 * @param Button: Specifies the Button to be configured.
243 * This parameter should be: BUTTON_KEY
244 * @param Mode: Specifies Button mode.
245 * This parameter can be one of following parameters:
246 * @arg BUTTON_MODE_GPIO: Button will be used as simple IO
247 * @arg BUTTON_MODE_EXTI: Button will be connected to EXTI line with interrupt
248 * generation capability
249 */
BSP_PB_Init(Button_TypeDef Button,ButtonMode_TypeDef Mode)250 void BSP_PB_Init(Button_TypeDef Button, ButtonMode_TypeDef Mode)
251 {
252 GPIO_InitTypeDef GPIO_InitStruct;
253
254 /* Enable the BUTTON Clock */
255 BUTTONx_GPIO_CLK_ENABLE(Button);
256
257 if (Mode == BUTTON_MODE_GPIO)
258 {
259 /* Configure Button pin as input */
260 GPIO_InitStruct.Pin = BUTTON_PIN[Button];
261 GPIO_InitStruct.Mode = GPIO_MODE_INPUT;
262 GPIO_InitStruct.Pull = GPIO_NOPULL;
263 GPIO_InitStruct.Speed = GPIO_SPEED_FAST;
264
265 HAL_GPIO_Init(BUTTON_PORT[Button], &GPIO_InitStruct);
266 }
267
268 if (Mode == BUTTON_MODE_EXTI)
269 {
270 /* Configure Button pin as input with External interrupt */
271 GPIO_InitStruct.Pin = BUTTON_PIN[Button];
272 GPIO_InitStruct.Pull = GPIO_NOPULL;
273 GPIO_InitStruct.Speed = GPIO_SPEED_FAST;
274 GPIO_InitStruct.Mode = GPIO_MODE_IT_RISING;
275 HAL_GPIO_Init(BUTTON_PORT[Button], &GPIO_InitStruct);
276
277 /* Enable and set Button EXTI Interrupt to the lowest priority */
278 HAL_NVIC_SetPriority((IRQn_Type)(BUTTON_IRQn[Button]), 0x0F, 0);
279 HAL_NVIC_EnableIRQ((IRQn_Type)(BUTTON_IRQn[Button]));
280 }
281 }
282
283 /**
284 * @brief Returns the selected Button state.
285 * @param Button: Specifies the Button to be checked.
286 * This parameter should be: BUTTON_KEY
287 * @retval The Button GPIO pin value.
288 */
BSP_PB_GetState(Button_TypeDef Button)289 uint32_t BSP_PB_GetState(Button_TypeDef Button)
290 {
291 return HAL_GPIO_ReadPin(BUTTON_PORT[Button], BUTTON_PIN[Button]);
292 }
293
294 /**
295 * @}
296 */
297
298 /** @defgroup STM32F4_DISCOVERY_LOW_LEVEL_BUS_Functions STM32F4 DISCOVERY LOW LEVEL BUS Functions
299 * @{
300 */
301
302 /*******************************************************************************
303 BUS OPERATIONS
304 *******************************************************************************/
305
306 /******************************* SPI Routines *********************************/
307
308 /**
309 * @brief SPIx Bus initialization
310 */
SPIx_Init(void)311 static void SPIx_Init(void)
312 {
313 if(HAL_SPI_GetState(&SpiHandle) == HAL_SPI_STATE_RESET)
314 {
315 /* SPI configuration -----------------------------------------------------*/
316 SpiHandle.Instance = DISCOVERY_SPIx;
317 SpiHandle.Init.BaudRatePrescaler = SPI_BAUDRATEPRESCALER_16;
318 SpiHandle.Init.Direction = SPI_DIRECTION_2LINES;
319 SpiHandle.Init.CLKPhase = SPI_PHASE_1EDGE;
320 SpiHandle.Init.CLKPolarity = SPI_POLARITY_LOW;
321 SpiHandle.Init.CRCCalculation = SPI_CRCCALCULATION_DISABLED;
322 SpiHandle.Init.CRCPolynomial = 7;
323 SpiHandle.Init.DataSize = SPI_DATASIZE_8BIT;
324 SpiHandle.Init.FirstBit = SPI_FIRSTBIT_MSB;
325 SpiHandle.Init.NSS = SPI_NSS_SOFT;
326 SpiHandle.Init.TIMode = SPI_TIMODE_DISABLED;
327 SpiHandle.Init.Mode = SPI_MODE_MASTER;
328
329 SPIx_MspInit();
330 HAL_SPI_Init(&SpiHandle);
331 }
332 }
333
334 /**
335 * @brief Sends a Byte through the SPI interface and return the Byte received
336 * from the SPI bus.
337 * @param Byte: Byte send.
338 * @retval The received byte value
339 */
SPIx_WriteRead(uint8_t Byte)340 static uint8_t SPIx_WriteRead(uint8_t Byte)
341 {
342 uint8_t receivedbyte = 0;
343
344 /* Send a Byte through the SPI peripheral */
345 /* Read byte from the SPI bus */
346 if(HAL_SPI_TransmitReceive(&SpiHandle, (uint8_t*) &Byte, (uint8_t*) &receivedbyte, 1, SpixTimeout) != HAL_OK)
347 {
348 SPIx_Error();
349 }
350
351 return receivedbyte;
352 }
353
354 /**
355 * @brief SPIx error treatment function.
356 */
SPIx_Error(void)357 static void SPIx_Error(void)
358 {
359 /* De-initialize the SPI communication bus */
360 HAL_SPI_DeInit(&SpiHandle);
361
362 /* Re-Initialize the SPI communication bus */
363 SPIx_Init();
364 }
365
366 /**
367 * @brief SPI MSP Init.
368 */
SPIx_MspInit(void)369 static void SPIx_MspInit(void)
370 {
371 GPIO_InitTypeDef GPIO_InitStructure;
372
373 /* Enable the SPI peripheral */
374 DISCOVERY_SPIx_CLK_ENABLE();
375
376 /* Enable SCK, MOSI and MISO GPIO clocks */
377 DISCOVERY_SPIx_GPIO_CLK_ENABLE();
378
379 /* SPI SCK, MOSI, MISO pin configuration */
380 GPIO_InitStructure.Pin = (DISCOVERY_SPIx_SCK_PIN | DISCOVERY_SPIx_MISO_PIN | DISCOVERY_SPIx_MOSI_PIN);
381 GPIO_InitStructure.Mode = GPIO_MODE_AF_PP;
382 GPIO_InitStructure.Pull = GPIO_PULLDOWN;
383 GPIO_InitStructure.Speed = GPIO_SPEED_MEDIUM;
384 GPIO_InitStructure.Alternate = DISCOVERY_SPIx_AF;
385 HAL_GPIO_Init(DISCOVERY_SPIx_GPIO_PORT, &GPIO_InitStructure);
386 }
387
388 /******************************* I2C Routines**********************************/
389 /**
390 * @brief Configures I2C interface.
391 */
I2Cx_Init(void)392 static void I2Cx_Init(void)
393 {
394 if(HAL_I2C_GetState(&I2cHandle) == HAL_I2C_STATE_RESET)
395 {
396 /* DISCOVERY_I2Cx peripheral configuration */
397 I2cHandle.Init.ClockSpeed = BSP_I2C_SPEED;
398 I2cHandle.Init.DutyCycle = I2C_DUTYCYCLE_2;
399 I2cHandle.Init.OwnAddress1 = 0x33;
400 I2cHandle.Init.AddressingMode = I2C_ADDRESSINGMODE_7BIT;
401 I2cHandle.Instance = DISCOVERY_I2Cx;
402
403 /* Init the I2C */
404 I2Cx_MspInit();
405 HAL_I2C_Init(&I2cHandle);
406 }
407 }
408
409 /**
410 * @brief Write a value in a register of the device through BUS.
411 * @param Addr: Device address on BUS Bus.
412 * @param Reg: The target register address to write
413 * @param Value: The target register value to be written
414 * @retval HAL status
415 */
I2Cx_WriteData(uint8_t Addr,uint8_t Reg,uint8_t Value)416 static void I2Cx_WriteData(uint8_t Addr, uint8_t Reg, uint8_t Value)
417 {
418 HAL_StatusTypeDef status = HAL_OK;
419
420 status = HAL_I2C_Mem_Write(&I2cHandle, Addr, (uint16_t)Reg, I2C_MEMADD_SIZE_8BIT, &Value, 1, I2cxTimeout);
421
422 /* Check the communication status */
423 if(status != HAL_OK)
424 {
425 /* Execute user timeout callback */
426 I2Cx_Error(Addr);
427 }
428 }
429
430 /**
431 * @brief Read a register of the device through BUS
432 * @param Addr: Device address on BUS
433 * @param Reg: The target register address to read
434 * @retval HAL status
435 */
I2Cx_ReadData(uint8_t Addr,uint8_t Reg)436 static uint8_t I2Cx_ReadData(uint8_t Addr, uint8_t Reg)
437 {
438 HAL_StatusTypeDef status = HAL_OK;
439 uint8_t value = 0;
440
441 status = HAL_I2C_Mem_Read(&I2cHandle, Addr, (uint16_t)Reg, I2C_MEMADD_SIZE_8BIT, &value, 1,I2cxTimeout);
442
443 /* Check the communication status */
444 if(status != HAL_OK)
445 {
446 /* Execute user timeout callback */
447 I2Cx_Error(Addr);
448 }
449 return value;
450 }
451
452 /**
453 * @brief Manages error callback by re-initializing I2C.
454 * @param Addr: I2C Address
455 */
I2Cx_Error(uint8_t Addr)456 static void I2Cx_Error(uint8_t Addr)
457 {
458 /* De-initialize the I2C communication bus */
459 HAL_I2C_DeInit(&I2cHandle);
460
461 /* Re-Initialize the I2C communication bus */
462 I2Cx_Init();
463 }
464
465 /**
466 * @brief I2C MSP Initialization
467 */
I2Cx_MspInit(void)468 static void I2Cx_MspInit(void)
469 {
470 GPIO_InitTypeDef GPIO_InitStruct;
471
472 /* Enable I2C GPIO clocks */
473 DISCOVERY_I2Cx_SCL_SDA_GPIO_CLK_ENABLE();
474
475 /* DISCOVERY_I2Cx SCL and SDA pins configuration ---------------------------*/
476 GPIO_InitStruct.Pin = DISCOVERY_I2Cx_SCL_PIN | DISCOVERY_I2Cx_SDA_PIN;
477 GPIO_InitStruct.Mode = GPIO_MODE_AF_OD;
478 GPIO_InitStruct.Speed = GPIO_SPEED_FAST;
479 GPIO_InitStruct.Pull = GPIO_NOPULL;
480 GPIO_InitStruct.Alternate = DISCOVERY_I2Cx_SCL_SDA_AF;
481 HAL_GPIO_Init(DISCOVERY_I2Cx_SCL_SDA_GPIO_PORT, &GPIO_InitStruct);
482
483 /* Enable the DISCOVERY_I2Cx peripheral clock */
484 DISCOVERY_I2Cx_CLK_ENABLE();
485
486 /* Force the I2C peripheral clock reset */
487 DISCOVERY_I2Cx_FORCE_RESET();
488
489 /* Release the I2C peripheral clock reset */
490 DISCOVERY_I2Cx_RELEASE_RESET();
491
492 /* Enable and set I2Cx Interrupt to the highest priority */
493 HAL_NVIC_SetPriority(DISCOVERY_I2Cx_EV_IRQn, 0, 0);
494 HAL_NVIC_EnableIRQ(DISCOVERY_I2Cx_EV_IRQn);
495
496 /* Enable and set I2Cx Interrupt to the highest priority */
497 HAL_NVIC_SetPriority(DISCOVERY_I2Cx_ER_IRQn, 0, 0);
498 HAL_NVIC_EnableIRQ(DISCOVERY_I2Cx_ER_IRQn);
499 }
500
501 /*******************************************************************************
502 LINK OPERATIONS
503 *******************************************************************************/
504
505 /***************************** LINK ACCELEROMETER *****************************/
506
507 /**
508 * @brief Configures the Accelerometer SPI interface.
509 */
ACCELERO_IO_Init(void)510 void ACCELERO_IO_Init(void)
511 {
512 GPIO_InitTypeDef GPIO_InitStructure;
513
514 /* Configure the Accelerometer Control pins --------------------------------*/
515 /* Enable CS GPIO clock and configure GPIO pin for Accelerometer Chip select */
516 ACCELERO_CS_GPIO_CLK_ENABLE();
517
518 /* Configure GPIO PIN for LIS Chip select */
519 GPIO_InitStructure.Pin = ACCELERO_CS_PIN;
520 GPIO_InitStructure.Mode = GPIO_MODE_OUTPUT_PP;
521 GPIO_InitStructure.Pull = GPIO_NOPULL;
522 GPIO_InitStructure.Speed = GPIO_SPEED_MEDIUM;
523 HAL_GPIO_Init(ACCELERO_CS_GPIO_PORT, &GPIO_InitStructure);
524
525 /* Deselect: Chip Select high */
526 ACCELERO_CS_HIGH();
527
528 SPIx_Init();
529 }
530
531 /**
532 * @brief Configures the Accelerometer INT2.
533 * EXTI0 is already used by user button so INT1 is not configured here.
534 */
ACCELERO_IO_ITConfig(void)535 void ACCELERO_IO_ITConfig(void)
536 {
537 GPIO_InitTypeDef GPIO_InitStructure;
538
539 /* Enable INT2 GPIO clock and configure GPIO PINs to detect Interrupts */
540 ACCELERO_INT_GPIO_CLK_ENABLE();
541
542 /* Configure GPIO PINs to detect Interrupts */
543 GPIO_InitStructure.Pin = ACCELERO_INT2_PIN;
544 GPIO_InitStructure.Mode = GPIO_MODE_IT_RISING;
545 GPIO_InitStructure.Speed = GPIO_SPEED_FAST;
546 GPIO_InitStructure.Pull = GPIO_NOPULL;
547 HAL_GPIO_Init(ACCELERO_INT_GPIO_PORT, &GPIO_InitStructure);
548
549 /* Enable and set Accelerometer INT2 to the lowest priority */
550 HAL_NVIC_SetPriority((IRQn_Type)ACCELERO_INT2_EXTI_IRQn, 0x0F, 0);
551 HAL_NVIC_EnableIRQ((IRQn_Type)ACCELERO_INT2_EXTI_IRQn);
552 }
553
554 /**
555 * @brief Writes one byte to the Accelerometer.
556 * @param pBuffer: pointer to the buffer containing the data to be written to the Accelerometer.
557 * @param WriteAddr: Accelerometer's internal address to write to.
558 * @param NumByteToWrite: Number of bytes to write.
559 */
ACCELERO_IO_Write(uint8_t * pBuffer,uint8_t WriteAddr,uint16_t NumByteToWrite)560 void ACCELERO_IO_Write(uint8_t *pBuffer, uint8_t WriteAddr, uint16_t NumByteToWrite)
561 {
562 /* Configure the MS bit:
563 - When 0, the address will remain unchanged in multiple read/write commands.
564 - When 1, the address will be auto incremented in multiple read/write commands.
565 */
566 if(NumByteToWrite > 0x01)
567 {
568 WriteAddr |= (uint8_t)MULTIPLEBYTE_CMD;
569 }
570 /* Set chip select Low at the start of the transmission */
571 ACCELERO_CS_LOW();
572
573 /* Send the Address of the indexed register */
574 SPIx_WriteRead(WriteAddr);
575
576 /* Send the data that will be written into the device (MSB First) */
577 while(NumByteToWrite >= 0x01)
578 {
579 SPIx_WriteRead(*pBuffer);
580 NumByteToWrite--;
581 pBuffer++;
582 }
583
584 /* Set chip select High at the end of the transmission */
585 ACCELERO_CS_HIGH();
586 }
587
588 /**
589 * @brief Reads a block of data from the Accelerometer.
590 * @param pBuffer: pointer to the buffer that receives the data read from the Accelerometer.
591 * @param ReadAddr: Accelerometer's internal address to read from.
592 * @param NumByteToRead: number of bytes to read from the Accelerometer.
593 */
ACCELERO_IO_Read(uint8_t * pBuffer,uint8_t ReadAddr,uint16_t NumByteToRead)594 void ACCELERO_IO_Read(uint8_t *pBuffer, uint8_t ReadAddr, uint16_t NumByteToRead)
595 {
596 if(NumByteToRead > 0x01)
597 {
598 ReadAddr |= (uint8_t)(READWRITE_CMD | MULTIPLEBYTE_CMD);
599 }
600 else
601 {
602 ReadAddr |= (uint8_t)READWRITE_CMD;
603 }
604 /* Set chip select Low at the start of the transmission */
605 ACCELERO_CS_LOW();
606
607 /* Send the Address of the indexed register */
608 SPIx_WriteRead(ReadAddr);
609
610 /* Receive the data that will be read from the device (MSB First) */
611 while(NumByteToRead > 0x00)
612 {
613 /* Send dummy byte (0x00) to generate the SPI clock to ACCELEROMETER (Slave device) */
614 *pBuffer = SPIx_WriteRead(DUMMY_BYTE);
615 NumByteToRead--;
616 pBuffer++;
617 }
618
619 /* Set chip select High at the end of the transmission */
620 ACCELERO_CS_HIGH();
621 }
622
623 /********************************* LINK AUDIO *********************************/
624
625 /**
626 * @brief Initializes Audio low level.
627 */
AUDIO_IO_Init(void)628 void AUDIO_IO_Init(void)
629 {
630 GPIO_InitTypeDef GPIO_InitStruct;
631
632 /* Enable Reset GPIO Clock */
633 AUDIO_RESET_GPIO_CLK_ENABLE();
634
635 /* Audio reset pin configuration */
636 GPIO_InitStruct.Pin = AUDIO_RESET_PIN;
637 GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP;
638 GPIO_InitStruct.Speed = GPIO_SPEED_FAST;
639 GPIO_InitStruct.Pull = GPIO_NOPULL;
640 HAL_GPIO_Init(AUDIO_RESET_GPIO, &GPIO_InitStruct);
641
642 I2Cx_Init();
643
644 /* Power Down the codec */
645 HAL_GPIO_WritePin(AUDIO_RESET_GPIO, AUDIO_RESET_PIN, GPIO_PIN_RESET);
646
647 /* Wait for a delay to insure registers erasing */
648 HAL_Delay(5);
649
650 /* Power on the codec */
651 HAL_GPIO_WritePin(AUDIO_RESET_GPIO, AUDIO_RESET_PIN, GPIO_PIN_SET);
652
653 /* Wait for a delay to insure registers erasing */
654 HAL_Delay(5);
655 }
656
657 /**
658 * @brief DeInitializes Audio low level.
659 */
AUDIO_IO_DeInit(void)660 void AUDIO_IO_DeInit(void)
661 {
662
663 }
664
665 /**
666 * @brief Writes a single data.
667 * @param Addr: I2C address
668 * @param Reg: Reg address
669 * @param Value: Data to be written
670 */
AUDIO_IO_Write(uint8_t Addr,uint8_t Reg,uint8_t Value)671 void AUDIO_IO_Write (uint8_t Addr, uint8_t Reg, uint8_t Value)
672 {
673 I2Cx_WriteData(Addr, Reg, Value);
674 }
675
676 /**
677 * @brief Reads a single data.
678 * @param Addr: I2C address
679 * @param Reg: Reg address
680 * @retval Data to be read
681 */
AUDIO_IO_Read(uint8_t Addr,uint8_t Reg)682 uint8_t AUDIO_IO_Read(uint8_t Addr, uint8_t Reg)
683 {
684 return I2Cx_ReadData(Addr, Reg);
685 }
686
687 /**
688 * @}
689 */
690
691 /**
692 * @}
693 */
694
695 /**
696 * @}
697 */
698
699 /**
700 * @}
701 */
702
703 /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/
704