Contains the BSP driver for the DISCO_F413ZH board.

Dependents:   DISCO_F413ZH-LCD-demo DISCO_F413ZH-touch-screen-demo DISCO_F413ZH-SD-demo DISCO_F413ZH-PSRAM-demo ... more

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers stm32f413h_discovery.c Source File

stm32f413h_discovery.c

Go to the documentation of this file.
00001 /**
00002   ******************************************************************************
00003   * @file    stm32f413h_discovery.c
00004   * @author  MCD Application Team
00005   * @brief   This file provides a set of firmware functions to manage LEDs, 
00006   *          push-buttons and COM ports available on STM32F413H-DISCOVERY board 
00007   *          (MB1209) from STMicroelectronics.
00008   ******************************************************************************
00009   * @attention
00010   *
00011   * <h2><center>&copy; COPYRIGHT(c) 2017 STMicroelectronics</center></h2>
00012   *
00013   * Redistribution and use in source and binary forms, with or without modification,
00014   * are permitted provided that the following conditions are met:
00015   *   1. Redistributions of source code must retain the above copyright notice,
00016   *      this list of conditions and the following disclaimer.
00017   *   2. Redistributions in binary form must reproduce the above copyright notice,
00018   *      this list of conditions and the following disclaimer in the documentation
00019   *      and/or other materials provided with the distribution.
00020   *   3. Neither the name of STMicroelectronics nor the names of its contributors
00021   *      may be used to endorse or promote products derived from this software
00022   *      without specific prior written permission.
00023   *
00024   * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
00025   * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
00026   * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
00027   * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
00028   * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
00029   * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
00030   * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
00031   * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
00032   * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
00033   * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
00034   *
00035   ******************************************************************************
00036   */ 
00037   
00038 /* Includes ------------------------------------------------------------------*/
00039 #include "stm32f413h_discovery.h"
00040 
00041 
00042 /** @addtogroup BSP
00043   * @{
00044   */ 
00045 
00046 /** @defgroup STM32F413H_DISCOVERY STM32F413H_DISCOVERY
00047   * @{
00048   */
00049 
00050 /** @defgroup STM32F413H_DISCOVERY_LOW_LEVEL STM32F413H-DISCOVERY LOW LEVEL
00051   * @{
00052   */
00053 
00054 /** @defgroup STM32F413H_DISCOVERY_LOW_LEVEL_Private_TypesDefinitions STM32F413H Discovery Low Level Private Typedef
00055   * @{
00056   */
00057 typedef struct
00058 {
00059   __IO uint16_t REG;
00060   __IO uint16_t RAM;
00061 }LCD_CONTROLLER_TypeDef;
00062 /**
00063   * @}
00064   */
00065 
00066 /** @defgroup STM32F413H_DISCOVERY_LOW_LEVEL_Private_Defines STM32F413H Discovery Low Level Private Def
00067   * @{
00068   */
00069 /**
00070  * @brief STM32F413H DISCOVERY BSP Driver version number V1.0.1
00071    */
00072 #define __STM32F413H_DISCOVERY_BSP_VERSION_MAIN   (0x01) /*!< [31:24] main version */
00073 #define __STM32F413H_DISCOVERY_BSP_VERSION_SUB1   (0x00) /*!< [23:16] sub1 version */
00074 #define __STM32F413H_DISCOVERY_BSP_VERSION_SUB2   (0x01) /*!< [15:8]  sub2 version */
00075 #define __STM32F413H_DISCOVERY_BSP_VERSION_RC     (0x00) /*!< [7:0]  release candidate */
00076 #define __STM32F413H_DISCOVERY_BSP_VERSION        ((__STM32F413H_DISCOVERY_BSP_VERSION_MAIN << 24)\
00077                                                  |(__STM32F413H_DISCOVERY_BSP_VERSION_SUB1 << 16)\
00078                                                  |(__STM32F413H_DISCOVERY_BSP_VERSION_SUB2 << 8 )\
00079                                                  |(__STM32F413H_DISCOVERY_BSP_VERSION_RC))
00080                                                                                 
00081 /* We use BANK3 as we use FMC_NE3 signal */
00082 #define FMC_BANK3_BASE  ((uint32_t)(0x60000000 | 0x08000000))  
00083 #define FMC_BANK3       ((LCD_CONTROLLER_TypeDef *) FMC_BANK3_BASE)
00084 
00085 /**
00086   * @}
00087   */
00088 
00089 /** @defgroup STM32F413H_DISCOVERY_LOW_LEVEL_Private_Variables STM32F413H Discovery Low Level Variables 
00090   * @{
00091   */
00092 
00093 const uint32_t GPIO_PIN[LEDn] = {LED3_PIN,
00094                                  LED4_PIN};
00095 
00096 
00097 GPIO_TypeDef* GPIO_PORT[LEDn] = {LED3_GPIO_PORT,
00098                                  LED4_GPIO_PORT};
00099 
00100 
00101 GPIO_TypeDef* BUTTON_PORT[BUTTONn] = {WAKEUP_BUTTON_GPIO_PORT};
00102 
00103 const uint16_t BUTTON_PIN[BUTTONn] = {WAKEUP_BUTTON_PIN};
00104 
00105 const uint16_t BUTTON_IRQn[BUTTONn] = {WAKEUP_BUTTON_EXTI_IRQn};
00106 
00107 USART_TypeDef* COM_USART[COMn] = {DISCOVERY_COM1};
00108 
00109 GPIO_TypeDef* COM_TX_PORT[COMn] = {DISCOVERY_COM1_TX_GPIO_PORT};
00110 
00111 GPIO_TypeDef* COM_RX_PORT[COMn] = {DISCOVERY_COM1_RX_GPIO_PORT};
00112 
00113 const uint16_t COM_TX_PIN[COMn] = {DISCOVERY_COM1_TX_PIN};
00114 
00115 const uint16_t COM_RX_PIN[COMn] = {DISCOVERY_COM1_RX_PIN};
00116 
00117 const uint16_t COM_TX_AF[COMn] = {DISCOVERY_COM1_TX_AF};
00118 
00119 const uint16_t COM_RX_AF[COMn] = {DISCOVERY_COM1_RX_AF};
00120 
00121 static FMPI2C_HandleTypeDef hI2cAudioHandler;
00122 
00123 /**
00124   * @}
00125   */
00126 
00127 /** @defgroup STM32F413H_DISCOVERY_LOW_LEVEL_Private_FunctionPrototypes STM32F413H Discovery Low Level Private Prototypes
00128   * @{
00129   */
00130 static void FMPI2Cx_Init(FMPI2C_HandleTypeDef *i2c_handler);
00131 static void FMPI2Cx_DeInit(FMPI2C_HandleTypeDef *i2c_handler);
00132 
00133 static HAL_StatusTypeDef FMPI2Cx_ReadMultiple(FMPI2C_HandleTypeDef *fmpi2c_handler, uint8_t Addr, uint16_t Reg, uint16_t MemAddSize, uint8_t *Buffer, uint16_t Length);
00134 static HAL_StatusTypeDef FMPI2Cx_WriteMultiple(FMPI2C_HandleTypeDef *fmpi2c_handler, uint8_t Addr, uint16_t Reg, uint16_t MemAddSize, uint8_t *Buffer, uint16_t Length);
00135 static void              FMPI2Cx_Error(FMPI2C_HandleTypeDef *fmpi2c_handler, uint8_t Addr);
00136 
00137 static void     FMC_BANK3_WriteData(uint16_t Data);
00138 static void     FMC_BANK3_WriteReg(uint8_t Reg);
00139 static uint16_t FMC_BANK3_ReadData(void);
00140 static void     FMC_BANK3_Init(void);
00141 static void     FMC_BANK3_MspInit(void);
00142 
00143 /* LCD IO functions */
00144 void            LCD_IO_Init(void);
00145 void            LCD_IO_WriteData(uint16_t RegValue);
00146 void            LCD_IO_WriteReg(uint8_t Reg);
00147 void            LCD_IO_WriteMultipleData(uint16_t *pData, uint32_t Size);
00148 uint16_t        LCD_IO_ReadData(void);
00149 void            LCD_IO_Delay(uint32_t Delay);
00150 
00151 /* AUDIO IO functions */
00152 void            AUDIO_IO_Init(void);
00153 void            AUDIO_IO_DeInit(void);
00154 void            AUDIO_IO_Write(uint8_t Addr, uint16_t Reg, uint16_t Value);
00155 uint16_t        AUDIO_IO_Read(uint8_t Addr, uint16_t Reg);
00156 void            AUDIO_IO_Delay(uint32_t Delay);
00157 
00158 /* TouchScreen (TS) IO functions */
00159 void            TS_IO_Init(void);
00160 void            TS_IO_DeInit(void);
00161 void            TS_IO_Write(uint8_t Addr, uint8_t Reg, uint8_t Value);
00162 uint8_t         TS_IO_Read(uint8_t Addr, uint8_t Reg);
00163 uint16_t        TS_IO_ReadMultiple(uint8_t Addr, uint8_t Reg, uint8_t *Buffer, uint16_t Length);
00164 void            TS_IO_WriteMultiple(uint8_t Addr, uint8_t Reg, uint8_t *Buffer, uint16_t Length);
00165 void            TS_IO_Delay(uint32_t Delay);
00166 
00167 /**
00168   * @}
00169   */
00170 
00171 /** @defgroup STM32F413H_DISCOVERY_LOW_LEVEL_Private_Functions STM32F413H Discovery Low Level Private Functions
00172   * @{
00173   */ 
00174 
00175 /**
00176   * @brief  This method returns the STM32F413H DISCOVERY BSP Driver revision
00177   * @retval version: 0xXYZR (8bits for each decimal, R for RC)
00178   */
00179 uint32_t BSP_GetVersion(void)
00180 {
00181   return __STM32F413H_DISCOVERY_BSP_VERSION;
00182 }
00183 
00184 /**
00185   * @brief  Configures LEDs.
00186   * @param  Led: LED to be configured. 
00187   *          This parameter can be one of the following values:
00188   *            @arg  LED3
00189   *            @arg  LED4
00190   */
00191 void BSP_LED_Init(Led_TypeDef Led)
00192 {
00193   GPIO_InitTypeDef  gpio_init_structure;
00194   
00195   LEDx_GPIO_CLK_ENABLE(Led);
00196   /* Configure the GPIO_LED pin */
00197   gpio_init_structure.Pin   = GPIO_PIN[Led];
00198   gpio_init_structure.Mode  = GPIO_MODE_OUTPUT_PP;
00199   gpio_init_structure.Pull  = GPIO_PULLUP;
00200   gpio_init_structure.Speed = GPIO_SPEED_HIGH;
00201   
00202   HAL_GPIO_Init(GPIO_PORT[Led], &gpio_init_structure);
00203 }
00204 
00205 /**
00206   * @brief  DeInit LEDs.
00207   * @param  Led: LED to be configured. 
00208   *          This parameter can be one of the following values:
00209   *            @arg  LED3
00210   *            @arg  LED4
00211   */
00212 void BSP_LED_DeInit(Led_TypeDef Led)
00213 {
00214   GPIO_InitTypeDef  gpio_init_structure;
00215   
00216   /* DeInit the GPIO_LED pin */
00217   gpio_init_structure.Pin = GPIO_PIN[Led];
00218   
00219   /* Turn off LED */
00220   HAL_GPIO_WritePin(GPIO_PORT[Led], GPIO_PIN[Led], GPIO_PIN_RESET);
00221   HAL_GPIO_DeInit(GPIO_PORT[Led], gpio_init_structure.Pin);
00222 }
00223 
00224 /**
00225   * @brief  Turns selected LED On.
00226   * @param  Led: LED to be set on 
00227   *          This parameter can be one of the following values:
00228   *            @arg  LED3
00229   *            @arg  LED4
00230   */
00231 void BSP_LED_On(Led_TypeDef Led)
00232 {
00233   HAL_GPIO_WritePin(GPIO_PORT[Led], GPIO_PIN[Led], GPIO_PIN_SET);
00234 }
00235 
00236 /**
00237   * @brief  Turns selected LED Off. 
00238   * @param  Led: LED to be set off
00239   *          This parameter can be one of the following values:
00240   *            @arg  LED3
00241   *            @arg  LED4
00242   */
00243 void BSP_LED_Off(Led_TypeDef Led)
00244 {
00245   HAL_GPIO_WritePin(GPIO_PORT[Led], GPIO_PIN[Led], GPIO_PIN_RESET);
00246 }
00247 
00248 /**
00249   * @brief  Toggles the selected LED.
00250   * @param  Led: LED to be toggled
00251   *          This parameter can be one of the following values:
00252   *            @arg  LED3
00253   *            @arg  LED4
00254   */
00255 void BSP_LED_Toggle(Led_TypeDef Led)
00256 {
00257   HAL_GPIO_TogglePin(GPIO_PORT[Led], GPIO_PIN[Led]);
00258 }
00259 
00260 /**
00261   * @brief  Configures button GPIO and EXTI Line.
00262   * @param  Button: Button to be configured
00263   *          This parameter can be one of the following values:
00264   *            @arg  BUTTON_WAKEUP: Wakeup Push Button
00265   * @param  ButtonMode: Button mode
00266   *          This parameter can be one of the following values:
00267   *            @arg  BUTTON_MODE_GPIO: Button will be used as simple IO
00268   *            @arg  BUTTON_MODE_EXTI: Button will be connected to EXTI line 
00269   *                                    with interrupt generation capability  
00270   */
00271 void BSP_PB_Init(Button_TypeDef Button, ButtonMode_TypeDef ButtonMode)
00272 {
00273   GPIO_InitTypeDef gpio_init_structure;
00274   
00275   /* Enable the BUTTON clock */
00276   WAKEUP_BUTTON_GPIO_CLK_ENABLE();
00277   
00278   if(ButtonMode == BUTTON_MODE_GPIO)
00279   {
00280     /* Configure Button pin as input */
00281     gpio_init_structure.Pin = BUTTON_PIN[Button];
00282     gpio_init_structure.Mode = GPIO_MODE_INPUT;
00283     gpio_init_structure.Pull = GPIO_PULLDOWN;
00284     gpio_init_structure.Speed = GPIO_SPEED_FAST;
00285     HAL_GPIO_Init(BUTTON_PORT[Button], &gpio_init_structure);
00286   }
00287   
00288   if(ButtonMode == BUTTON_MODE_EXTI)
00289   {
00290     /* Configure Button pin as input with External interrupt */
00291     gpio_init_structure.Pin = BUTTON_PIN[Button];
00292     gpio_init_structure.Pull = GPIO_PULLDOWN;
00293     gpio_init_structure.Speed = GPIO_SPEED_FREQ_VERY_HIGH;
00294     
00295     gpio_init_structure.Mode = GPIO_MODE_IT_RISING;
00296     
00297     HAL_GPIO_Init(BUTTON_PORT[Button], &gpio_init_structure);
00298     
00299     /* Enable and set Button EXTI Interrupt to the lowest priority */
00300     HAL_NVIC_SetPriority((IRQn_Type)(BUTTON_IRQn[Button]), 0x0F, 0x00);
00301     HAL_NVIC_EnableIRQ((IRQn_Type)(BUTTON_IRQn[Button]));
00302   }
00303 }
00304 
00305 /**
00306   * @brief  Push Button DeInit.
00307   * @param  Button: Button to be configured
00308   *          This parameter can be one of the following values:
00309   *            @arg  BUTTON_WAKEUP: Wakeup Push Button 
00310   * @note PB DeInit does not disable the GPIO clock
00311   */
00312 void BSP_PB_DeInit(Button_TypeDef Button)
00313 {
00314   GPIO_InitTypeDef gpio_init_structure;
00315 
00316   gpio_init_structure.Pin = BUTTON_PIN[Button];
00317   HAL_NVIC_DisableIRQ((IRQn_Type)(BUTTON_IRQn[Button]));
00318   HAL_GPIO_DeInit(BUTTON_PORT[Button], gpio_init_structure.Pin);
00319 }
00320 
00321 
00322 /**
00323   * @brief  Returns the selected button state.
00324   * @param  Button: Button to be checked
00325   *          This parameter can be one of the following values:
00326   *            @arg  BUTTON_WAKEUP: Wakeup Push Button 
00327   * @retval The Button GPIO pin value (GPIO_PIN_RESET = button pressed)
00328   */
00329 uint32_t BSP_PB_GetState(Button_TypeDef Button)
00330 {
00331   return HAL_GPIO_ReadPin(BUTTON_PORT[Button], BUTTON_PIN[Button]);
00332 }
00333 
00334 /**
00335   * @brief  Configures COM port.
00336   * @param  COM: COM port to be configured.
00337   *          This parameter can be one of the following values:
00338   *            @arg  COM1 
00339   * @param  huart: Pointer to a UART_HandleTypeDef structure that contains the
00340   *                configuration information for the specified USART peripheral.
00341   */
00342 void BSP_COM_Init(COM_TypeDef COM, UART_HandleTypeDef *huart)
00343 {
00344   GPIO_InitTypeDef gpio_init_structure;
00345 
00346   /* Enable GPIO clock */
00347   DISCOVERY_COMx_TX_GPIO_CLK_ENABLE(COM);
00348   DISCOVERY_COMx_RX_GPIO_CLK_ENABLE(COM);
00349 
00350   /* Enable USART clock */
00351   DISCOVERY_COMx_CLK_ENABLE(COM);
00352 
00353   /* Configure USART Tx as alternate function */
00354   gpio_init_structure.Pin = COM_TX_PIN[COM];
00355   gpio_init_structure.Mode = GPIO_MODE_AF_PP;
00356   gpio_init_structure.Speed = GPIO_SPEED_FREQ_HIGH;
00357   gpio_init_structure.Pull = GPIO_PULLUP;
00358   gpio_init_structure.Alternate = COM_TX_AF[COM];
00359   HAL_GPIO_Init(COM_TX_PORT[COM], &gpio_init_structure);
00360 
00361   /* Configure USART Rx as alternate function */
00362   gpio_init_structure.Pin = COM_RX_PIN[COM];
00363   gpio_init_structure.Mode = GPIO_MODE_AF_PP;
00364   gpio_init_structure.Alternate = COM_RX_AF[COM];
00365   HAL_GPIO_Init(COM_RX_PORT[COM], &gpio_init_structure);
00366 
00367   /* USART configuration */
00368   huart->Instance = COM_USART[COM];
00369   HAL_UART_Init(huart);
00370 }
00371 
00372 /**
00373   * @brief  DeInit COM port.
00374   * @param  COM: COM port to be configured.
00375   *          This parameter can be one of the following values:
00376   *            @arg  COM1 
00377   * @param  huart: Pointer to a UART_HandleTypeDef structure that contains the
00378   *                configuration information for the specified USART peripheral.
00379   */
00380 void BSP_COM_DeInit(COM_TypeDef COM, UART_HandleTypeDef *huart)
00381 {
00382   /* USART configuration */
00383   huart->Instance = COM_USART[COM];
00384   HAL_UART_DeInit(huart);
00385 
00386   /* Enable USART clock */
00387   DISCOVERY_COMx_CLK_DISABLE(COM);
00388 
00389   /* DeInit GPIO pins can be done in the application 
00390      (by surcharging this __weak function) */
00391 
00392   /* GPIO pins clock, FMC clock and DMA clock can be shut down in the application 
00393      by surcharging this __weak function */ 
00394 }
00395 
00396 
00397 /*******************************************************************************
00398                             BUS OPERATIONS
00399 *******************************************************************************/
00400 
00401 /******************************* I2C Routines *********************************/
00402 /**
00403   * @brief  Initializes FMPI2C MSP.
00404   * @param  fmpi2c_handler : FMPI2C handler
00405   */
00406 static void FMPI2Cx_MspInit(FMPI2C_HandleTypeDef *fmpi2c_handler)
00407 {
00408   GPIO_InitTypeDef  gpio_init_structure;  
00409   
00410   /* AUDIO FMPI2C MSP init */
00411   
00412   /*** Configure the GPIOs ***/
00413   /* Enable GPIO clock */
00414   DISCOVERY_AUDIO_I2Cx_SCL_SDA_GPIO_CLK_ENABLE();
00415   
00416   /* Configure I2C Tx as alternate function */
00417   gpio_init_structure.Pin = DISCOVERY_AUDIO_I2Cx_SCL_PIN;
00418   gpio_init_structure.Mode = GPIO_MODE_AF_OD;
00419   gpio_init_structure.Pull = GPIO_NOPULL;
00420   gpio_init_structure.Speed = GPIO_SPEED_FAST;
00421   gpio_init_structure.Alternate = DISCOVERY_AUDIO_I2Cx_SCL_SDA_AF;
00422   HAL_GPIO_Init(DISCOVERY_AUDIO_I2Cx_SCL_SDA_GPIO_PORT, &gpio_init_structure);
00423   
00424   /* Configure I2C Rx as alternate function */
00425   gpio_init_structure.Pin = DISCOVERY_AUDIO_I2Cx_SDA_PIN;
00426   gpio_init_structure.Alternate = DISCOVERY_AUDIO_I2Cx_SCL_SDA_AF;
00427   HAL_GPIO_Init(DISCOVERY_AUDIO_I2Cx_SCL_SDA_GPIO_PORT, &gpio_init_structure);
00428   
00429   /*** Configure the I2C peripheral ***/
00430   /* Enable I2C clock */
00431   DISCOVERY_AUDIO_I2Cx_CLK_ENABLE();
00432   
00433   /* Force the I2C peripheral clock reset */
00434   DISCOVERY_AUDIO_I2Cx_FORCE_RESET();
00435   
00436   /* Release the I2C peripheral clock reset */
00437   DISCOVERY_AUDIO_I2Cx_RELEASE_RESET();
00438   
00439   /* Enable and set I2Cx Interrupt to a lower priority */
00440   HAL_NVIC_SetPriority(DISCOVERY_AUDIO_I2Cx_EV_IRQn, 0x0F, 0x00);
00441   HAL_NVIC_EnableIRQ(DISCOVERY_AUDIO_I2Cx_EV_IRQn);
00442   
00443   /* Enable and set I2Cx Interrupt to a lower priority */
00444   HAL_NVIC_SetPriority(DISCOVERY_AUDIO_I2Cx_ER_IRQn, 0x0F, 0x00);
00445   HAL_NVIC_EnableIRQ(DISCOVERY_AUDIO_I2Cx_ER_IRQn);
00446 }
00447 
00448 /**
00449   * @brief  Initializes FMPI2C HAL.
00450   * @param  fmpi2c_handler : FMPI2C handler
00451   */
00452 static void FMPI2Cx_Init(FMPI2C_HandleTypeDef *fmpi2c_handler)
00453 {
00454   if(HAL_FMPI2C_GetState(fmpi2c_handler) == HAL_FMPI2C_STATE_RESET)
00455   {
00456     /* Audio FMPI2C configuration */
00457     fmpi2c_handler->Instance              = DISCOVERY_AUDIO_I2Cx;
00458     fmpi2c_handler->Init.Timing           = DISCOVERY_I2Cx_TIMING;
00459     fmpi2c_handler->Init.OwnAddress1      = 0;
00460     fmpi2c_handler->Init.AddressingMode   = FMPI2C_ADDRESSINGMODE_7BIT;
00461     fmpi2c_handler->Init.DualAddressMode  = FMPI2C_DUALADDRESS_DISABLE;
00462     fmpi2c_handler->Init.OwnAddress2      = 0;
00463     fmpi2c_handler->Init.OwnAddress2Masks = FMPI2C_OA2_NOMASK;
00464     fmpi2c_handler->Init.GeneralCallMode  = FMPI2C_GENERALCALL_DISABLE;
00465     fmpi2c_handler->Init.NoStretchMode    = FMPI2C_NOSTRETCH_DISABLE;
00466     /* Init the FMPI2C */
00467     FMPI2Cx_MspInit(fmpi2c_handler);
00468     HAL_FMPI2C_Init(fmpi2c_handler);
00469   }
00470 }
00471 
00472 /**
00473   * @brief  Reads multiple data.
00474   * @param  fmpi2c_handler : FMPI2C handler
00475   * @param  Addr: I2C address
00476   * @param  Reg: Reg address 
00477   * @param  MemAddress: Memory address
00478   * @param  Buffer: Pointer to data buffer
00479   * @param  Length: Length of the data
00480   * @retval Number of read data
00481   */
00482 static HAL_StatusTypeDef FMPI2Cx_ReadMultiple(FMPI2C_HandleTypeDef *fmpi2c_handler,
00483                                            uint8_t Addr,
00484                                            uint16_t Reg,
00485                                            uint16_t MemAddress,
00486                                            uint8_t *Buffer,
00487                                            uint16_t Length)
00488 {
00489   HAL_StatusTypeDef status = HAL_OK;
00490 
00491   status = HAL_FMPI2C_Mem_Read(fmpi2c_handler, Addr, (uint16_t)Reg, MemAddress, Buffer, Length, 1000);
00492 
00493   /* Check the communication status */
00494   if(status != HAL_OK)
00495   {
00496     /* FMPI2C error occurred */
00497     FMPI2Cx_Error(fmpi2c_handler, Addr);
00498   }
00499   return status;    
00500 }
00501 
00502 /**
00503   * @brief  Writes a value in a register of the device through BUS in using DMA mode.
00504   * @param  fmpi2c_handler : FMPI2C handler
00505   * @param  Addr: Device address on BUS Bus.  
00506   * @param  Reg: The target register address to write
00507   * @param  MemAddress: Memory address
00508   * @param  Buffer: The target register value to be written
00509   * @param  Length: buffer size to be written
00510   * @retval HAL status
00511   */
00512 static HAL_StatusTypeDef FMPI2Cx_WriteMultiple(FMPI2C_HandleTypeDef *fmpi2c_handler,
00513                                             uint8_t Addr,
00514                                             uint16_t Reg,
00515                                             uint16_t MemAddress,
00516                                             uint8_t *Buffer,
00517                                             uint16_t Length)
00518 {
00519   HAL_StatusTypeDef status = HAL_OK;
00520   
00521   status = HAL_FMPI2C_Mem_Write(fmpi2c_handler, Addr, (uint16_t)Reg, MemAddress, Buffer, Length, 1000);
00522   
00523   /* Check the communication status */
00524   if(status != HAL_OK)
00525   {
00526     /* Re-Initialize the FMPI2C Bus */
00527     FMPI2Cx_Error(fmpi2c_handler, Addr);
00528   }
00529   return status;
00530 }
00531 
00532 /**
00533   * @brief  Manages error callback by re-initializing I2C.
00534   * @param  fmpi2c_handler : FMPI2C handler
00535   * @param  Addr: I2C Address
00536   * @retval None
00537   */
00538 static void FMPI2Cx_Error(FMPI2C_HandleTypeDef *fmpi2c_handler, uint8_t Addr)
00539 {
00540   /* De-initialize the FMPI2C communication bus */
00541   HAL_FMPI2C_DeInit(fmpi2c_handler);
00542   
00543   /* Re-Initialize the FMPI2C communication bus */
00544   FMPI2Cx_Init(fmpi2c_handler);
00545 }
00546 
00547 /**
00548   * @brief  Deinitializes FMPI2C interface
00549   * @param  fmpi2c_handler : FMPI2C handler
00550   */
00551  static void FMPI2Cx_DeInit(FMPI2C_HandleTypeDef *fmpi2c_handler)
00552 {
00553   /* Audio and LCD I2C configuration */
00554   fmpi2c_handler->Instance = DISCOVERY_AUDIO_I2Cx;
00555   /* Disable FMPI2C block */
00556   __HAL_FMPI2C_DISABLE(fmpi2c_handler);
00557   
00558   /* DeInit the FMPI2C */
00559   HAL_FMPI2C_DeInit(fmpi2c_handler);
00560 }
00561 /*************************** FMC Routines ************************************/
00562 /**
00563   * @brief  Initializes FMC_BANK3 MSP.
00564   */
00565 static void FMC_BANK3_MspInit(void)
00566 {
00567   GPIO_InitTypeDef gpio_init_structure;
00568     
00569   /* Enable FSMC clock */
00570   __HAL_RCC_FSMC_CLK_ENABLE();
00571   
00572   /* Enable GPIOs clock */
00573   __HAL_RCC_GPIOD_CLK_ENABLE();
00574   __HAL_RCC_GPIOE_CLK_ENABLE();
00575   __HAL_RCC_GPIOF_CLK_ENABLE();
00576   __HAL_RCC_GPIOG_CLK_ENABLE();
00577   
00578   /* Common GPIO configuration */
00579   gpio_init_structure.Mode      = GPIO_MODE_AF_PP;
00580   gpio_init_structure.Pull      = GPIO_PULLUP;
00581   gpio_init_structure.Speed     = GPIO_SPEED_FREQ_VERY_HIGH;
00582   gpio_init_structure.Alternate = GPIO_AF12_FSMC;
00583   
00584   /* GPIOD configuration: GPIO_PIN_7 is  FMC_NE1 , GPIO_PIN_11 ans GPIO_PIN_12 are PSRAM_A16 and PSRAM_A17 */
00585   gpio_init_structure.Pin   = GPIO_PIN_0 | GPIO_PIN_1 | GPIO_PIN_4 | GPIO_PIN_5 | GPIO_PIN_8 |\
00586                               GPIO_PIN_9 | GPIO_PIN_10 | GPIO_PIN_14 | GPIO_PIN_15 | GPIO_PIN_7|\
00587                               GPIO_PIN_11 | GPIO_PIN_12;
00588    
00589   HAL_GPIO_Init(GPIOD, &gpio_init_structure);
00590 
00591   /* GPIOE configuration */  
00592   gpio_init_structure.Pin   = GPIO_PIN_7 | GPIO_PIN_8 | GPIO_PIN_9 | GPIO_PIN_10 | GPIO_PIN_11 |\
00593                               GPIO_PIN_12 |GPIO_PIN_13 | GPIO_PIN_14 | GPIO_PIN_15;
00594   HAL_GPIO_Init(GPIOE, &gpio_init_structure);
00595   
00596   /* GPIOF configuration */  
00597   gpio_init_structure.Pin   = GPIO_PIN_0 | GPIO_PIN_1 | GPIO_PIN_2 | GPIO_PIN_3 | GPIO_PIN_4 |\
00598                               GPIO_PIN_5 | GPIO_PIN_12 | GPIO_PIN_13 | GPIO_PIN_14 | GPIO_PIN_15; 
00599   HAL_GPIO_Init(GPIOF, &gpio_init_structure);
00600 
00601   /* GPIOG configuration */  
00602   gpio_init_structure.Pin   = GPIO_PIN_0 | GPIO_PIN_1 | GPIO_PIN_2 | GPIO_PIN_3 | GPIO_PIN_4 |\
00603                               GPIO_PIN_5 | GPIO_PIN_10 ; 
00604   HAL_GPIO_Init(GPIOG, &gpio_init_structure);
00605 }
00606 
00607 /**
00608   * @brief  Initializes LCD IOs.
00609   */ 
00610 static void FMC_BANK3_Init(void) 
00611 {  
00612   SRAM_HandleTypeDef hsram;
00613   FSMC_NORSRAM_TimingTypeDef sram_timing;
00614 
00615   /* Initialize the SRAM controller */
00616   FMC_BANK3_MspInit();
00617   
00618   /*** Configure the SRAM Bank 1 ***/
00619   /* Configure IPs */
00620   hsram.Instance  = FSMC_NORSRAM_DEVICE;
00621   hsram.Extended  = FSMC_NORSRAM_EXTENDED_DEVICE;
00622   
00623   /* Timing config */
00624   sram_timing.AddressSetupTime      = 3;
00625   sram_timing.AddressHoldTime       = 1;
00626   sram_timing.DataSetupTime         = 4;
00627   sram_timing.BusTurnAroundDuration = 1;
00628   sram_timing.CLKDivision           = 2;
00629   sram_timing.DataLatency           = 2;
00630   sram_timing.AccessMode            = FSMC_ACCESS_MODE_A;
00631       
00632   hsram.Init.NSBank             = FSMC_NORSRAM_BANK3;
00633   hsram.Init.DataAddressMux     = FSMC_DATA_ADDRESS_MUX_DISABLE;
00634   hsram.Init.MemoryType         = FSMC_MEMORY_TYPE_SRAM;
00635   hsram.Init.MemoryDataWidth    = FSMC_NORSRAM_MEM_BUS_WIDTH_16;
00636   hsram.Init.BurstAccessMode    = FSMC_BURST_ACCESS_MODE_DISABLE;
00637   hsram.Init.WaitSignalPolarity = FSMC_WAIT_SIGNAL_POLARITY_LOW;
00638   hsram.Init.WrapMode           = FSMC_WRAP_MODE_DISABLE;
00639   hsram.Init.WaitSignalActive   = FSMC_WAIT_TIMING_BEFORE_WS;
00640   hsram.Init.WriteOperation     = FSMC_WRITE_OPERATION_ENABLE;
00641   hsram.Init.WaitSignal         = FSMC_WAIT_SIGNAL_DISABLE;
00642   hsram.Init.ExtendedMode       = FSMC_EXTENDED_MODE_ENABLE;
00643   hsram.Init.AsynchronousWait   = FSMC_ASYNCHRONOUS_WAIT_DISABLE;
00644   hsram.Init.WriteBurst         = FSMC_WRITE_BURST_DISABLE;
00645   hsram.Init.WriteFifo          = FSMC_WRITE_FIFO_DISABLE;
00646   hsram.Init.PageSize           = FSMC_PAGE_SIZE_NONE;
00647   hsram.Init.ContinuousClock    = FSMC_CONTINUOUS_CLOCK_SYNC_ONLY;
00648 
00649   HAL_SRAM_Init(&hsram, &sram_timing, &sram_timing);
00650 }
00651 
00652 /**
00653   * @brief  Writes register value.
00654   * @param  Data: Data to be written 
00655   */
00656 static void FMC_BANK3_WriteData(uint16_t Data) 
00657 {
00658   /* Write 16-bit Reg */
00659   FMC_BANK3->RAM = Data;
00660   __DSB();
00661 }
00662 
00663 /**
00664   * @brief  Writes register address.
00665   * @param  Reg: Register to be written
00666   */
00667 static void FMC_BANK3_WriteReg(uint8_t Reg) 
00668 {
00669   /* Write 16-bit Index, then write register */
00670   FMC_BANK3->REG = Reg;
00671   __DSB();
00672 }
00673 
00674 /**
00675   * @brief  Reads register value.
00676   * @retval Read value
00677   */
00678 static uint16_t FMC_BANK3_ReadData(void) 
00679 {
00680   return FMC_BANK3->RAM;
00681 }
00682 
00683 /*******************************************************************************
00684                             LINK OPERATIONS
00685 *******************************************************************************/
00686 
00687 /********************************* LINK LCD ***********************************/
00688 /**
00689   * @brief  Initializes LCD low level.
00690   */
00691 void LCD_IO_Init(void) 
00692 {
00693   FMC_BANK3_Init();
00694 }
00695 
00696 /**
00697   * @brief  Writes data on LCD data register.
00698   * @param  RegValue: Data to be written
00699   */
00700 void LCD_IO_WriteData(uint16_t RegValue) 
00701 {
00702   /* Write 16-bit Reg */
00703   FMC_BANK3_WriteData(RegValue);
00704 }
00705 
00706 /**
00707   * @brief  Writes several data on LCD data register.
00708   * @param  pData: pointer on data to be written
00709   * @param  Size: data amount in 16bits short unit
00710   */
00711 void LCD_IO_WriteMultipleData(uint16_t *pData, uint32_t Size)
00712 {
00713   uint32_t  i;
00714 
00715   for (i = 0; i < Size; i++)
00716   {
00717     FMC_BANK3_WriteData(pData[i]);
00718   }
00719 }
00720 
00721 /**
00722   * @brief  Writes register on LCD register.
00723   * @param  Reg: Register to be written
00724   */
00725 void LCD_IO_WriteReg(uint8_t Reg) 
00726 {
00727   /* Write 16-bit Index, then Write Reg */
00728   FMC_BANK3_WriteReg(Reg);
00729 }
00730 
00731 /**
00732   * @brief  Reads data from LCD data register.
00733   * @retval Read data.
00734   */
00735 uint16_t LCD_IO_ReadData(void) 
00736 {
00737   return FMC_BANK3_ReadData();
00738 }
00739 
00740 /**
00741   * @brief  LCD delay
00742   * @param  Delay: Delay in ms
00743   */
00744 void LCD_IO_Delay(uint32_t Delay)
00745 {
00746   HAL_Delay(Delay);
00747 }
00748 
00749 /********************************* LINK AUDIO *********************************/
00750 
00751 /**
00752   * @brief  Initializes Audio low level.
00753   */
00754 void AUDIO_IO_Init(void) 
00755 {
00756   FMPI2Cx_Init(&hI2cAudioHandler);
00757 }
00758 
00759 /**
00760   * @brief  Deinitializes Audio low level.
00761   */
00762 void AUDIO_IO_DeInit(void)
00763 {
00764   FMPI2Cx_DeInit(&hI2cAudioHandler);
00765 }
00766 
00767 /**
00768   * @brief  Writes a single data.
00769   * @param  Addr: I2C address
00770   * @param  Reg: Reg address 
00771   * @param  Value: Data to be written
00772   */
00773 void AUDIO_IO_Write(uint8_t Addr, uint16_t Reg, uint16_t Value)
00774 {
00775   uint16_t tmp = Value;
00776   
00777   Value = ((uint16_t)(tmp >> 8) & 0x00FF);
00778   
00779   Value |= ((uint16_t)(tmp << 8)& 0xFF00);
00780   
00781   FMPI2Cx_WriteMultiple(&hI2cAudioHandler, Addr, Reg, FMPI2C_MEMADD_SIZE_16BIT,(uint8_t*)&Value, 2);
00782 }
00783 
00784 /**
00785   * @brief  Reads a single data.
00786   * @param  Addr: I2C address
00787   * @param  Reg: Reg address 
00788   * @retval Data to be read
00789   */
00790 uint16_t AUDIO_IO_Read(uint8_t Addr, uint16_t Reg)
00791 {
00792   uint16_t read_value = 0, tmp = 0;
00793   
00794   FMPI2Cx_ReadMultiple(&hI2cAudioHandler, Addr, Reg, FMPI2C_MEMADD_SIZE_16BIT, (uint8_t*)&read_value, 2);
00795   
00796   tmp = ((uint16_t)(read_value >> 8) & 0x00FF);
00797   
00798   tmp |= ((uint16_t)(read_value << 8)& 0xFF00);
00799   
00800   read_value = tmp;
00801   
00802   return read_value;
00803 }
00804 
00805 /**
00806   * @brief  Reads multiple data with I2C communication
00807   *         channel from TouchScreen.
00808   * @param  Addr: I2C address
00809   * @param  Reg: Register address
00810   * @param  Buffer: Pointer to data buffer
00811   * @param  Length: Length of the data
00812   * @retval Number of read data
00813   */
00814 uint16_t TS_IO_ReadMultiple(uint8_t Addr, uint8_t Reg, uint8_t *Buffer, uint16_t Length)
00815 {
00816  return FMPI2Cx_ReadMultiple(&hI2cAudioHandler, Addr, (uint16_t)Reg, I2C_MEMADD_SIZE_8BIT, Buffer, Length);
00817 }
00818 
00819 /**
00820   * @brief  Writes multiple data with I2C communication
00821   *         channel from MCU to TouchScreen.
00822   * @param  Addr: I2C address
00823   * @param  Reg: Register address
00824   * @param  Buffer: Pointer to data buffer
00825   * @param  Length: Length of the data
00826   */
00827 void TS_IO_WriteMultiple(uint8_t Addr, uint8_t Reg, uint8_t *Buffer, uint16_t Length)
00828 {
00829   FMPI2Cx_WriteMultiple(&hI2cAudioHandler, Addr, (uint16_t)Reg, I2C_MEMADD_SIZE_8BIT, Buffer, Length);
00830 }
00831 
00832 /**
00833   * @brief  AUDIO Codec delay 
00834   * @param  Delay: Delay in ms
00835   */
00836 void AUDIO_IO_Delay(uint32_t Delay)
00837 {
00838   HAL_Delay(Delay);
00839 }
00840 
00841 /******************************** LINK TS (TouchScreen) *****************************/
00842 
00843 /**
00844   * @brief  Initializes TS low level.
00845   */
00846 void TS_IO_Init(void) 
00847 {
00848   GPIO_InitTypeDef  gpio_init_structure;
00849       
00850   TS_RESET_GPIO_CLK_ENABLE();
00851 
00852   /* Configure Button pin as input */
00853   gpio_init_structure.Pin = TS_RESET_PIN;
00854   gpio_init_structure.Mode = GPIO_MODE_OUTPUT_PP;
00855   gpio_init_structure.Pull = GPIO_NOPULL;
00856   gpio_init_structure.Speed = GPIO_SPEED_FREQ_LOW;
00857   HAL_GPIO_Init(TS_RESET_GPIO_PORT, &gpio_init_structure);
00858   
00859   FMPI2Cx_Init(&hI2cAudioHandler);
00860 }
00861 
00862 /**
00863   * @brief  Deinitializes TS low level.
00864   */
00865 void TS_IO_DeInit(void)
00866 {
00867   FMPI2Cx_DeInit(&hI2cAudioHandler);
00868 }
00869 
00870 /**
00871   * @brief  Reads a single data.
00872   * @param  Addr: I2C address
00873   * @param  Reg: Register address 
00874   * @retval Data to be read
00875   */
00876 uint8_t TS_IO_Read(uint8_t Addr, uint8_t Reg)
00877 {
00878   uint8_t read_value = 0;
00879 
00880   FMPI2Cx_ReadMultiple(&hI2cAudioHandler, Addr, Reg, I2C_MEMADD_SIZE_8BIT, (uint8_t*)&read_value, 1);
00881 
00882   return read_value;
00883 }
00884 
00885 /**
00886   * @brief  Writes a single data.
00887   * @param  Addr: I2C address
00888   * @param  Reg: Reg address
00889   * @param  Value: Data to be written
00890   */
00891 void TS_IO_Write(uint8_t Addr, uint8_t Reg, uint8_t Value)
00892 {
00893   FMPI2Cx_WriteMultiple(&hI2cAudioHandler, Addr, (uint16_t)Reg, I2C_MEMADD_SIZE_8BIT,(uint8_t*)&Value, 1);
00894 }
00895 
00896 /**
00897   * @brief  Delay function used in TouchScreen low level driver.
00898   * @param  Delay: Delay in ms
00899   */
00900 void TS_IO_Delay(uint32_t Delay)
00901 {
00902   HAL_Delay(Delay);
00903 }
00904 
00905 /**
00906   * @}
00907   */
00908 
00909 /**
00910   * @}
00911   */ 
00912 
00913 /**
00914   * @}
00915   */
00916 
00917 /**
00918   * @}
00919   */    
00920     
00921 /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/