BSP library for DISCO-STM32F746NG board. Added support for on-board QSPI Flash memory MICRO N25Q128A. Ported from library BSP_DISCO_L476VG.

Dependents:   DISCO-F746NG_QSPI

Fork of BSP_DISCO_F746NG by ST

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers stm32746g_discovery.c Source File

stm32746g_discovery.c

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