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