BSP files for STM32H747I-Discovery Copy from ST Cube delivery

Dependents:   DISCO_H747I_LCD_demo DISCO_H747I_AUDIO_demo

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers stm32h747i_discovery.c Source File

stm32h747i_discovery.c

Go to the documentation of this file.
00001 /**
00002   ******************************************************************************
00003   * @file    stm32h747i_discovery.c
00004   * @author  MCD Application Team
00005   * @brief   This file provides a set of firmware functions to manage LEDs,
00006   *          push-buttons, external SDRAM, external QSPI Flash,
00007   *          available on STM32H747I-Discovery board (MB1248) from 
00008   *          STMicroelectronics.
00009   ******************************************************************************
00010   * @attention
00011   *
00012   * <h2><center>&copy; Copyright (c) 2019 STMicroelectronics.
00013   * All rights reserved.</center></h2>
00014   *
00015   * This software component is licensed by ST under BSD 3-Clause license,
00016   * the "License"; You may not use this file except in compliance with the
00017   * License. You may obtain a copy of the License at:
00018   *                        opensource.org/licenses/BSD-3-Clause
00019   *
00020   ******************************************************************************
00021   */
00022 
00023 /* Includes ------------------------------------------------------------------*/
00024 #include "stm32h747i_discovery.h"
00025 
00026 /** @defgroup BSP BSP
00027   * @{
00028   */
00029 
00030 /** @defgroup STM32H747I_DISCOVERY STM32H747I_DISCOVERY
00031   * @{
00032   */
00033 
00034 /** @defgroup STM32H747I_DISCOVERY_LOW_LEVEL STM32H747I_DISCOVERY_LOW_LEVEL
00035   * @{
00036   */
00037 
00038 /** @defgroup STM32H747I_DISCOVERY_LOW_LEVEL_Private_Defines Private Defines
00039   * @{
00040   */
00041 /**
00042  * @brief STM32H747I Discovery BSP Driver version number V1.0.0
00043    */
00044 #define __STM32H747I_DISCOVERY_BSP_VERSION_MAIN   (0x01) /*!< [31:24] main version */
00045 #define __STM32H747I_DISCOVERY_BSP_VERSION_SUB1   (0x00) /*!< [23:16] sub1 version */
00046 #define __STM32H747I_DISCOVERY_BSP_VERSION_SUB2   (0x00) /*!< [15:8]  sub2 version */
00047 #define __STM32H747I_DISCOVERY_BSP_VERSION_RC     (0x00) /*!< [7:0]  release candidate */
00048 #define __STM32H747I_DISCOVERY_BSP_VERSION        ((__STM32H747I_DISCOVERY_BSP_VERSION_MAIN << 24)\
00049                                                   |(__STM32H747I_DISCOVERY_BSP_VERSION_SUB1 << 16)\
00050                                                   |(__STM32H747I_DISCOVERY_BSP_VERSION_SUB2 << 8 )\
00051                                                   |(__STM32H747I_DISCOVERY_BSP_VERSION_RC))
00052 /**
00053   * @}
00054   */
00055 
00056 
00057 /** @defgroup STM32H747I_DISCOVERY_LOW_LEVEL_Private_Variables Private Variables
00058   * @{
00059   */
00060 
00061 GPIO_TypeDef* GPIO_PORT[LEDn] = {LED1_GPIO_PORT,
00062                                  LED2_GPIO_PORT,
00063                                  LED3_GPIO_PORT,
00064                                  LED4_GPIO_PORT};
00065 
00066 const uint32_t GPIO_PIN[LEDn] = {LED1_PIN,
00067                                  LED2_PIN,
00068                                  LED3_PIN,
00069                                  LED4_PIN};
00070 
00071 GPIO_TypeDef* BUTTON_PORT[BUTTONn] = {WAKEUP_BUTTON_GPIO_PORT };
00072 
00073 const uint16_t BUTTON_PIN[BUTTONn] = {WAKEUP_BUTTON_PIN };
00074 
00075 const uint16_t BUTTON_IRQn[BUTTONn] = {WAKEUP_BUTTON_EXTI_IRQn };
00076 
00077 GPIO_TypeDef* JOY_PORT[JOYn] =   {SEL_JOY_GPIO_PORT,
00078                                   DOWN_JOY_GPIO_PORT,
00079                                   LEFT_JOY_GPIO_PORT,
00080                                   RIGHT_JOY_GPIO_PORT,
00081                                   UP_JOY_GPIO_PORT};
00082 
00083 const uint16_t JOY_PIN[JOYn] =   {SEL_JOY_PIN,
00084                                   DOWN_JOY_PIN,
00085                                   LEFT_JOY_PIN,
00086                                   RIGHT_JOY_PIN,
00087                                   UP_JOY_PIN};
00088 
00089 const uint8_t JOY_IRQn[JOYn] =   {SEL_JOY_EXTI_IRQn,
00090                                   DOWN_JOY_EXTI_IRQn,
00091                                   LEFT_JOY_EXTI_IRQn,
00092                                   RIGHT_JOY_EXTI_IRQn,
00093                                   UP_JOY_EXTI_IRQn};
00094 
00095 static I2C_HandleTypeDef heval_I2c = {0};
00096 
00097 #if defined(BSP_USE_CMSIS_OS)
00098 static osSemaphoreId BspI2cSemaphore = 0;
00099 #endif
00100 
00101 /**
00102   * @}
00103   */
00104 
00105 /** @defgroup STM32H747I_DISCOVERY_LOW_LEVEL_Private_FunctionPrototypes Private FunctionPrototypes
00106   * @{
00107   */
00108 static void     I2Cx_MspInit(void);
00109 static void     I2Cx_Init(void);
00110 static void     I2Cx_Write(uint8_t Addr, uint8_t Reg, uint8_t Value);
00111 static uint8_t  I2Cx_Read(uint8_t Addr, uint8_t Reg);
00112 static HAL_StatusTypeDef I2Cx_ReadMultiple(uint8_t Addr, uint16_t Reg, uint16_t MemAddSize, uint8_t *Buffer, uint16_t Length);
00113 static HAL_StatusTypeDef I2Cx_WriteMultiple(uint8_t Addr, uint16_t Reg, uint16_t MemAddSize, uint8_t *Buffer, uint16_t Length);
00114 static HAL_StatusTypeDef I2Cx_IsDeviceReady(uint16_t DevAddress, uint32_t Trials);
00115 static void     I2Cx_Error(uint8_t Addr);
00116 
00117 /* HDMI IO functions */
00118 void            HDMI_IO_Init(void);
00119 void            HDMI_IO_Delay(uint32_t Delay);
00120 void            HDMI_IO_Write(uint8_t Addr, uint8_t Reg, uint8_t Value);
00121 uint8_t         HDMI_IO_Read(uint8_t Addr, uint8_t Reg);
00122 
00123 /* AUDIO IO functions */
00124 void            AUDIO_IO_Init(void);
00125 void            AUDIO_IO_DeInit(void);
00126 void            AUDIO_IO_Write(uint8_t Addr, uint16_t Reg, uint16_t Value);
00127 uint16_t        AUDIO_IO_Read(uint8_t Addr, uint16_t Reg);
00128 void            AUDIO_IO_Delay(uint32_t Delay);
00129 
00130 /* CAMERA IO functions */
00131 void            CAMERA_IO_Init(void);
00132 void            CAMERA_Delay(uint32_t Delay);
00133 void            CAMERA_IO_Write(uint8_t Addr, uint16_t Reg, uint16_t Value);
00134 uint16_t        CAMERA_IO_Read(uint8_t Addr, uint16_t Reg);
00135 
00136 /* I2C EEPROM IO function */
00137 void                EEPROM_IO_Init(void);
00138 HAL_StatusTypeDef   EEPROM_IO_WriteData(uint16_t DevAddress, uint16_t MemAddress, uint8_t* pBuffer, uint32_t BufferSize);
00139 HAL_StatusTypeDef   EEPROM_IO_ReadData(uint16_t DevAddress, uint16_t MemAddress, uint8_t* pBuffer, uint32_t BufferSize);
00140 HAL_StatusTypeDef   EEPROM_IO_IsDeviceReady(uint16_t DevAddress, uint32_t Trials);
00141 
00142 /* TouchScreen (TS) IO functions */
00143 void     TS_IO_Init(void);
00144 void     TS_IO_Write(uint8_t Addr, uint8_t Reg, uint8_t Value);
00145 uint8_t  TS_IO_Read(uint8_t Addr, uint8_t Reg);
00146 uint16_t TS_IO_ReadMultiple(uint8_t Addr, uint8_t Reg, uint8_t *Buffer, uint16_t Length);
00147 void     TS_IO_WriteMultiple(uint8_t Addr, uint8_t Reg, uint8_t *Buffer, uint16_t Length);
00148 void     TS_IO_Delay(uint32_t Delay);
00149 
00150 /* LCD Display IO functions */
00151 void OTM8009A_IO_Delay(uint32_t Delay);
00152 /**
00153   * @}
00154   */
00155 
00156 /** @defgroup STM32H747I_DISCOVERY_BSP_Exported_Functions BSP Exported Functions
00157   * @{
00158   */
00159 
00160 /**
00161   * @brief  BSP Error Notification
00162   * @note   Defined as a weak function to be overwritten by the application.
00163   * @retval None
00164   */
00165 __weak void BSP_ErrorNotify(void)
00166 {
00167 }
00168 
00169   /**
00170   * @brief  This method returns the STM32H747I Discovery BSP Driver revision
00171   * @retval version: 0xXYZR (8bits for each decimal, R for RC)
00172   */
00173 uint32_t BSP_GetVersion(void)
00174 {
00175   return __STM32H747I_DISCOVERY_BSP_VERSION;
00176 }
00177 
00178 /**
00179   * @brief  Configures LED GPIO.
00180   * @param  Led: LED to be configured.
00181   *          This parameter can be one of the following values:
00182   *            @arg  DISCO_LED1
00183   *            @arg  DISCO_LED2
00184   *            @arg  DISCO_LED3
00185   *            @arg  DISCO_LED4
00186   * @retval None
00187   */
00188 void BSP_LED_Init(Led_TypeDef Led)
00189 {
00190   GPIO_InitTypeDef  GPIO_InitStruct;
00191   
00192   /* Enable the GPIO_LED clock */
00193   LEDx_GPIO_CLK_ENABLE();
00194   
00195   /* Configure the GPIO_LED pin */
00196   GPIO_InitStruct.Pin = GPIO_PIN[Led];
00197   GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP;
00198   GPIO_InitStruct.Pull = GPIO_PULLUP;
00199   GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_VERY_HIGH;
00200   
00201   HAL_GPIO_Init(GPIO_PORT[Led], &GPIO_InitStruct);
00202   
00203   /* By default, turn off LED */
00204   HAL_GPIO_WritePin(GPIO_PORT[Led], GPIO_PIN[Led], GPIO_PIN_SET);
00205 }
00206 
00207 
00208 /**
00209   * @brief  DeInit LEDs.
00210   * @param  Led: LED to be configured.
00211   *          This parameter can be one of the following values:
00212   *            @arg  DISCO_LED1
00213   *            @arg  DISCO_LED2
00214   *            @arg  DISCO_LED3
00215   *            @arg  DISCO_LED4
00216   * @note Led DeInit does not disable the GPIO clock
00217   * @retval None
00218   */
00219 void BSP_LED_DeInit(Led_TypeDef Led)
00220 {
00221     /* Turn off LED */
00222     HAL_GPIO_WritePin(GPIO_PORT[Led], GPIO_PIN[Led], GPIO_PIN_RESET);
00223     /* Configure the GPIO_LED pin */
00224     HAL_GPIO_DeInit(GPIO_PORT[Led], GPIO_PIN[Led]);
00225 }
00226 
00227 /**
00228   * @brief  Turns selected LED On.
00229   * @param  Led: LED to be set on
00230   *          This parameter can be one of the following values:
00231   *            @arg  DISCO_LED1
00232   *            @arg  DISCO_LED2
00233   *            @arg  DISCO_LED3
00234   *            @arg  DISCO_LED4
00235   * @retval None
00236   */
00237 void BSP_LED_On(Led_TypeDef Led)
00238 {
00239   HAL_GPIO_WritePin(GPIO_PORT[Led], GPIO_PIN[Led], GPIO_PIN_RESET);
00240 }
00241 
00242 /**
00243   * @brief  Turns selected LED Off.
00244   * @param  Led: LED to be set off
00245   *          This parameter can be one of the following values:
00246   *            @arg  DISCO_LED1
00247   *            @arg  DISCO_LED2
00248   *            @arg  DISCO_LED3
00249   *            @arg  DISCO_LED4
00250   * @retval None
00251   */
00252 void BSP_LED_Off(Led_TypeDef Led)
00253 {
00254   HAL_GPIO_WritePin(GPIO_PORT[Led], GPIO_PIN[Led], GPIO_PIN_SET);
00255 }
00256 
00257 /**
00258   * @brief  Toggles the selected LED.
00259   * @param  Led: LED to be toggled
00260   *          This parameter can be one of the following values:
00261   *            @arg  DISCO_LED1
00262   *            @arg  DISCO_LED2
00263   *            @arg  DISCO_LED3
00264   *            @arg  DISCO_LED4
00265   * @retval None
00266   */
00267 void BSP_LED_Toggle(Led_TypeDef Led)
00268 {
00269   HAL_GPIO_TogglePin(GPIO_PORT[Led], GPIO_PIN[Led]);
00270 }
00271 
00272 /**
00273   * @brief  Configures button GPIO and EXTI Line.
00274   * @param  Button: Button to be configured
00275   *          This parameter can be one of the following values:
00276   *            @arg  BUTTON_WAKEUP: Wakeup Push Button
00277   *            @arg  BUTTON_USER: User Push Button
00278   * @param  Button_Mode: Button mode
00279   *          This parameter can be one of the following values:
00280   *            @arg  BUTTON_MODE_GPIO: Button will be used as simple IO
00281   *            @arg  BUTTON_MODE_EXTI: Button will be connected to EXTI line
00282   *                                    with interrupt generation capability
00283   * @retval None
00284   */
00285 void BSP_PB_Init(Button_TypeDef Button, ButtonMode_TypeDef Button_Mode)
00286 {
00287   GPIO_InitTypeDef GPIO_InitStruct;
00288   
00289   /* Enable the BUTTON clock */
00290   BUTTON_GPIO_CLK_ENABLE();
00291   
00292   if(Button_Mode == BUTTON_MODE_GPIO)
00293   {
00294     /* Configure Button pin as input */
00295     GPIO_InitStruct.Pin = BUTTON_PIN[Button];
00296     GPIO_InitStruct.Mode = GPIO_MODE_INPUT;
00297     GPIO_InitStruct.Pull = GPIO_NOPULL;
00298     GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_VERY_HIGH;
00299     HAL_GPIO_Init(BUTTON_PORT[Button], &GPIO_InitStruct);
00300   }
00301   
00302   if(Button_Mode == BUTTON_MODE_EXTI)
00303   {
00304     /* Configure Button pin as input with External interrupt */
00305     GPIO_InitStruct.Pin = BUTTON_PIN[Button];
00306     GPIO_InitStruct.Pull = GPIO_NOPULL;
00307     GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_VERY_HIGH;
00308     
00309     GPIO_InitStruct.Mode = GPIO_MODE_IT_RISING;
00310     
00311     HAL_GPIO_Init(BUTTON_PORT[Button], &GPIO_InitStruct);
00312     
00313     /* Enable and set Button EXTI Interrupt to the lowest priority */
00314     HAL_NVIC_SetPriority((IRQn_Type)(BUTTON_IRQn[Button]), 0x0F, 0x00);
00315     HAL_NVIC_EnableIRQ((IRQn_Type)(BUTTON_IRQn[Button]));
00316   }
00317 }
00318 
00319 /**
00320   * @brief  Push Button DeInit.
00321   * @param  Button: Button to be configured
00322   *          This parameter can be one of the following values:
00323   *            @arg  BUTTON_WAKEUP: Wakeup Push Button
00324   *            @arg  BUTTON_USER: User Push Button
00325   * @note PB DeInit does not disable the GPIO clock
00326   * @retval None
00327   */
00328 void BSP_PB_DeInit(Button_TypeDef Button)
00329 {
00330     GPIO_InitTypeDef gpio_init_structure;
00331 
00332     gpio_init_structure.Pin = BUTTON_PIN[Button];
00333     HAL_NVIC_DisableIRQ((IRQn_Type)(BUTTON_IRQn[Button]));
00334     HAL_GPIO_DeInit(BUTTON_PORT[Button], gpio_init_structure.Pin);
00335 }
00336 
00337 
00338 /**
00339   * @brief  Returns the selected button state.
00340   * @param  Button: Button to be checked
00341   *          This parameter can be one of the following values:
00342   *            @arg  BUTTON_WAKEUP: Wakeup Push Button
00343   *            @arg  BUTTON_USER: User Push Button
00344   * @retval The Button GPIO pin value
00345   */
00346 uint32_t BSP_PB_GetState(Button_TypeDef Button)
00347 {
00348   return HAL_GPIO_ReadPin(BUTTON_PORT[Button], BUTTON_PIN[Button]);
00349 }
00350 
00351 /**
00352   * @brief  Configures all joystick's buttons in GPIO or EXTI modes.
00353   * @param  Joy_Mode: Joystick mode.
00354   *    This parameter can be one of the following values:
00355   *     @arg  JOY_MODE_GPIO: Joystick pins will be used as simple IOs
00356   *     @arg  JOY_MODE_EXTI: Joystick pins will be connected to EXTI line
00357   *                                 with interrupt generation capability
00358   * @retval HAL_OK: if all initializations are OK. Other value if error.
00359   */
00360 uint8_t BSP_JOY_Init(JOYMode_TypeDef Joy_Mode)
00361 {
00362   JOYState_TypeDef joykey;
00363   GPIO_InitTypeDef GPIO_InitStruct;
00364 
00365   /* Initialized the Joystick. */
00366   for(joykey = JOY_SEL; joykey < (JOY_SEL + JOYn) ; joykey++)
00367   {
00368     /* Enable the JOY clock */
00369     JOYx_GPIO_CLK_ENABLE(joykey);
00370 
00371     GPIO_InitStruct.Pin = JOY_PIN[joykey];
00372     GPIO_InitStruct.Pull = GPIO_PULLUP;
00373     GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_VERY_HIGH;
00374 
00375     if (Joy_Mode == JOY_MODE_GPIO)
00376     {
00377       /* Configure Joy pin as input */
00378       GPIO_InitStruct.Mode = GPIO_MODE_INPUT;
00379       HAL_GPIO_Init(JOY_PORT[joykey], &GPIO_InitStruct);
00380     }
00381     else if (Joy_Mode == JOY_MODE_EXTI)
00382     {
00383       /* Configure Joy pin as input with External interrupt */
00384       GPIO_InitStruct.Mode = GPIO_MODE_IT_RISING_FALLING;
00385       HAL_GPIO_Init(JOY_PORT[joykey], &GPIO_InitStruct);
00386 
00387       /* Enable and set Joy EXTI Interrupt to the lowest priority */
00388       HAL_NVIC_SetPriority((IRQn_Type)(JOY_IRQn[joykey]), 0x0F, 0x00);
00389       HAL_NVIC_EnableIRQ((IRQn_Type)(JOY_IRQn[joykey]));
00390     }
00391   }
00392 
00393   return HAL_OK;
00394 }
00395 
00396 /**
00397   * @brief  Un-configures all GPIOs used as joystick's buttons.
00398   * @retval None.
00399   */
00400 void BSP_JOY_DeInit(void)
00401 {
00402   JOYState_TypeDef joykey;
00403 
00404   /* Initialized the Joystick. */
00405   for(joykey = JOY_SEL; joykey < (JOY_SEL + JOYn) ; joykey++)
00406   {
00407     /* Enable the JOY clock */
00408     JOYx_GPIO_CLK_ENABLE(joykey);
00409 
00410     HAL_GPIO_DeInit(JOY_PORT[joykey], JOY_PIN[joykey]);
00411   }
00412 }
00413 
00414 /**
00415 * @brief  Returns the current joystick status.
00416 * @retval Code of the joystick key pressed
00417 *          This code can be one of the following values:
00418 *            @arg  JOY_NONE
00419 *            @arg  JOY_SEL
00420 *            @arg  JOY_DOWN
00421 *            @arg  JOY_LEFT
00422 *            @arg  JOY_RIGHT
00423 *            @arg  JOY_UP
00424 */
00425 JOYState_TypeDef BSP_JOY_GetState(void)
00426 {
00427   JOYState_TypeDef joykey;
00428 
00429   for (joykey = JOY_SEL; joykey < (JOY_SEL + JOYn) ; joykey++)
00430   {
00431     if (HAL_GPIO_ReadPin(JOY_PORT[joykey], JOY_PIN[joykey]) == GPIO_PIN_RESET)
00432     {
00433       /* Return Code Joystick key pressed */
00434       return joykey;
00435     }
00436   }
00437 
00438   /* No Joystick key pressed */
00439   return JOY_NONE;
00440 }
00441 
00442 /**
00443   * @}
00444   */
00445 
00446 /** @defgroup STM32H747I_DISCOVERY_LOW_LEVEL_Private_Functions Private Functions
00447   * @{
00448   */
00449 /*******************************************************************************
00450                             BUS OPERATIONS
00451 *******************************************************************************/
00452 
00453 /******************************* I2C Routines *********************************/
00454 /**
00455   * @brief  Initializes I2C MSP.
00456   * @retval None
00457   */
00458 static void I2Cx_MspInit(void)
00459 {
00460   GPIO_InitTypeDef  gpio_init_structure;
00461   RCC_PeriphCLKInitTypeDef  RCC_PeriphClkInit;
00462   
00463   /* Configure the I2C clock source */
00464   RCC_PeriphClkInit.PeriphClockSelection = RCC_PERIPHCLK_I2C123;
00465   RCC_PeriphClkInit.I2c123ClockSelection = RCC_I2C123CLKSOURCE_D2PCLK1;
00466   HAL_RCCEx_PeriphCLKConfig(&RCC_PeriphClkInit);
00467   
00468   /* set STOPWUCK in RCC_CFGR */
00469   __HAL_RCC_WAKEUPSTOP_CLK_CONFIG(RCC_STOP_WAKEUPCLOCK_HSI);
00470   
00471   /*** Configure the GPIOs ***/  
00472   /* Enable GPIO clock */
00473   DISCOVERY_I2Cx_SCL_SDA_GPIO_CLK_ENABLE();
00474   
00475   /* Configure I2C Tx as alternate function */
00476   gpio_init_structure.Pin = DISCOVERY_I2Cx_SCL_PIN;
00477   gpio_init_structure.Mode = GPIO_MODE_AF_OD;
00478   gpio_init_structure.Pull = GPIO_NOPULL;
00479   gpio_init_structure.Speed = GPIO_SPEED_FREQ_VERY_HIGH;
00480   gpio_init_structure.Alternate = DISCOVERY_I2Cx_SCL_SDA_AF;
00481   HAL_GPIO_Init(DISCOVERY_I2Cx_SCL_SDA_GPIO_PORT, &gpio_init_structure);
00482   
00483   /* Configure I2C Rx as alternate function */
00484   gpio_init_structure.Pin = DISCOVERY_I2Cx_SDA_PIN;
00485   HAL_GPIO_Init(DISCOVERY_I2Cx_SCL_SDA_GPIO_PORT, &gpio_init_structure);
00486   
00487   /*** Configure the I2C peripheral ***/ 
00488   /* Enable I2C clock */
00489   DISCOVERY_I2Cx_CLK_ENABLE();
00490   
00491   /* Force the I2C peripheral clock reset */  
00492   DISCOVERY_I2Cx_FORCE_RESET(); 
00493   
00494   /* Release the I2C peripheral clock reset */  
00495   DISCOVERY_I2Cx_RELEASE_RESET(); 
00496   
00497   /* Enable and set I2Cx Interrupt to a lower priority */
00498   HAL_NVIC_SetPriority(DISCOVERY_I2Cx_EV_IRQn, 0x0F, 0);
00499   HAL_NVIC_EnableIRQ(DISCOVERY_I2Cx_EV_IRQn);
00500   
00501   /* Enable and set I2Cx Interrupt to a lower priority */
00502   HAL_NVIC_SetPriority(DISCOVERY_I2Cx_ER_IRQn, 0x0F, 0);
00503   HAL_NVIC_EnableIRQ(DISCOVERY_I2Cx_ER_IRQn);
00504 }
00505 
00506 /**
00507   * @brief  Initializes I2C HAL.
00508   * @retval None
00509   */
00510 static void I2Cx_Init(void)
00511 {
00512   if(HAL_I2C_GetState(&heval_I2c) == HAL_I2C_STATE_RESET)
00513   {
00514 #if defined(BSP_USE_CMSIS_OS)
00515     if(BspI2cSemaphore == NULL)
00516     {
00517       /* Create semaphore to prevent multiple I2C access */
00518       osSemaphoreDef(BSP_I2C_SEM);
00519       BspI2cSemaphore = osSemaphoreCreate(osSemaphore(BSP_I2C_SEM), 1);
00520     }
00521 #endif
00522 
00523     heval_I2c.Instance              = DISCOVERY_I2Cx;
00524     heval_I2c.Init.Timing           = DISCOVERY_I2Cx_TIMING;
00525     heval_I2c.Init.OwnAddress1      = 0x72;
00526     heval_I2c.Init.AddressingMode   = I2C_ADDRESSINGMODE_7BIT;
00527     heval_I2c.Init.DualAddressMode  = I2C_DUALADDRESS_ENABLE;
00528     heval_I2c.Init.OwnAddress2      = 0;
00529     heval_I2c.Init.OwnAddress2Masks = I2C_OA2_NOMASK;
00530     heval_I2c.Init.GeneralCallMode  = I2C_GENERALCALL_ENABLE;
00531     heval_I2c.Init.NoStretchMode    = I2C_NOSTRETCH_DISABLE;
00532     
00533     /* Init the I2C */
00534     I2Cx_MspInit();
00535     HAL_I2C_Init(&heval_I2c);    
00536   }
00537 }
00538 
00539 /**
00540   * @brief  Writes a single data.
00541   * @param  Addr: I2C address
00542   * @param  Reg: Register address 
00543   * @param  Value: Data to be written
00544   * @retval None
00545   */
00546 static void I2Cx_Write(uint8_t Addr, uint8_t Reg, uint8_t Value)
00547 {
00548   HAL_StatusTypeDef status = HAL_OK;
00549 
00550 #if defined(BSP_USE_CMSIS_OS)
00551   /* Get semaphore to prevent multiple I2C access */
00552   osSemaphoreWait(BspI2cSemaphore, osWaitForever);
00553 #endif
00554 
00555   status = HAL_I2C_Mem_Write(&heval_I2c, Addr, (uint16_t)Reg, I2C_MEMADD_SIZE_8BIT, &Value, 1, 100); 
00556 
00557 #if defined(BSP_USE_CMSIS_OS)
00558   /* Release semaphore to prevent multiple I2C access */
00559   osSemaphoreRelease(BspI2cSemaphore);
00560 #endif
00561 
00562   /* Check the communication status */
00563   if(status != HAL_OK)
00564   {
00565     /* Execute user timeout callback */
00566     I2Cx_Error(Addr);
00567   }
00568 }
00569 
00570 /**
00571   * @brief  Reads a single data.
00572   * @param  Addr: I2C address
00573   * @param  Reg: Register address
00574   * @retval Read data
00575   */
00576 static uint8_t I2Cx_Read(uint8_t Addr, uint8_t Reg)
00577 {
00578   HAL_StatusTypeDef status = HAL_OK;
00579   uint8_t Value = 0;
00580 
00581 #if defined(BSP_USE_CMSIS_OS)
00582   /* Get semaphore to prevent multiple I2C access */
00583   osSemaphoreWait(BspI2cSemaphore, osWaitForever);
00584 #endif
00585 
00586   status = HAL_I2C_Mem_Read(&heval_I2c, Addr, Reg, I2C_MEMADD_SIZE_8BIT, &Value, 1, 1000);
00587 
00588 #if defined(BSP_USE_CMSIS_OS)
00589   /* Release semaphore to prevent multiple I2C access */
00590   osSemaphoreRelease(BspI2cSemaphore);
00591 #endif
00592 
00593   /* Check the communication status */
00594   if(status != HAL_OK)
00595   {
00596     /* Execute user timeout callback */
00597     I2Cx_Error(Addr);
00598   }
00599   return Value;
00600 }
00601 
00602 /**
00603   * @brief  Reads multiple data.
00604   * @param  Addr: I2C address
00605   * @param  Reg: Reg address
00606   * @param  MemAddress: memory address
00607   * @param  Buffer: Pointer to data buffer
00608   * @param  Length: Length of the data
00609   * @retval HAL status
00610   */
00611 static HAL_StatusTypeDef I2Cx_ReadMultiple(uint8_t Addr, uint16_t Reg, uint16_t MemAddress, uint8_t *Buffer, uint16_t Length)
00612 {
00613   HAL_StatusTypeDef status = HAL_OK;
00614 
00615 #if defined(BSP_USE_CMSIS_OS)
00616   /* Get semaphore to prevent multiple I2C access */
00617   osSemaphoreWait(BspI2cSemaphore, osWaitForever);
00618 #endif
00619 
00620   status = HAL_I2C_Mem_Read(&heval_I2c, Addr, (uint16_t)Reg, MemAddress, Buffer, Length, 1000);
00621 
00622 #if defined(BSP_USE_CMSIS_OS)
00623   /* Release semaphore to prevent multiple I2C access */
00624   osSemaphoreRelease(BspI2cSemaphore);
00625 #endif
00626 
00627   /* Check the communication status */
00628   if(status != HAL_OK)
00629   {
00630     /* I2C error occurred */
00631     I2Cx_Error(Addr);
00632   }
00633   return status;
00634 }
00635 
00636 /**
00637   * @brief  Writes a value in a register of the device through BUS in using DMA mode.
00638   * @param  Addr: Device address on BUS Bus.  
00639   * @param  Reg: The target register address to write
00640   * @param  MemAddress: memory address  
00641   * @param  Buffer: The target register value to be written 
00642   * @param  Length: buffer size to be written
00643   * @retval HAL status
00644   */
00645 static HAL_StatusTypeDef I2Cx_WriteMultiple(uint8_t Addr, uint16_t Reg, uint16_t MemAddress, uint8_t *Buffer, uint16_t Length)
00646 {
00647   HAL_StatusTypeDef status = HAL_OK;
00648 
00649 #if defined(BSP_USE_CMSIS_OS)
00650   /* Get semaphore to prevent multiple I2C access */
00651   osSemaphoreWait(BspI2cSemaphore, osWaitForever);
00652 #endif
00653 
00654   status = HAL_I2C_Mem_Write(&heval_I2c, Addr, (uint16_t)Reg, MemAddress, Buffer, Length, 1000);
00655 
00656 #if defined(BSP_USE_CMSIS_OS)
00657   /* Release semaphore to prevent multiple I2C access */
00658   osSemaphoreRelease(BspI2cSemaphore);
00659 #endif
00660 
00661   /* Check the communication status */
00662   if(status != HAL_OK)
00663   {
00664     /* Re-Initiaize the I2C Bus */
00665     I2Cx_Error(Addr);
00666   }
00667   return status;
00668 }
00669 
00670 /**
00671   * @brief  Checks if target device is ready for communication. 
00672   * @note   This function is used with Memory devices
00673   * @param  DevAddress: Target device address
00674   * @param  Trials: Number of trials
00675   * @retval HAL status
00676   */
00677 static HAL_StatusTypeDef I2Cx_IsDeviceReady(uint16_t DevAddress, uint32_t Trials)
00678 {
00679   HAL_StatusTypeDef status = HAL_OK;
00680 
00681 #if defined(BSP_USE_CMSIS_OS)
00682   /* Get semaphore to prevent multiple I2C access */
00683   osSemaphoreWait(BspI2cSemaphore, osWaitForever);
00684 #endif
00685 
00686   status = HAL_I2C_IsDeviceReady(&heval_I2c, DevAddress, Trials, 1000);
00687 
00688 #if defined(BSP_USE_CMSIS_OS)
00689   /* Release semaphore to prevent multiple I2C access */
00690   osSemaphoreRelease(BspI2cSemaphore);
00691 #endif
00692 
00693   return status;
00694 }
00695 
00696 /**
00697   * @brief  Manages error callback by re-initializing I2C.
00698   * @param  Addr: I2C Address
00699   * @retval None
00700   */
00701 static void I2Cx_Error(uint8_t Addr)
00702 {
00703   BSP_ErrorNotify();
00704 
00705   /* De-initialize the I2C comunication bus */
00706   HAL_I2C_DeInit(&heval_I2c);
00707 
00708   /* Re-Initialize the I2C communication bus */
00709   I2Cx_Init();
00710 
00711 #if defined(BSP_USE_CMSIS_OS)
00712   /* Release semaphore to prevent multiple I2C access */
00713   osSemaphoreRelease(BspI2cSemaphore);
00714 #endif
00715 }
00716 
00717 /*******************************************************************************
00718                             LINK OPERATIONS
00719 *******************************************************************************/
00720 
00721 /********************************* LINK AUDIO *********************************/
00722 
00723 /**
00724   * @brief  Initializes Audio low level.
00725   * @retval None
00726   */
00727 void AUDIO_IO_Init(void) 
00728 {
00729   I2Cx_Init();
00730 }
00731 
00732 /**
00733   * @brief  De-Initializes Audio low level.
00734   * @retval None
00735   */
00736 void AUDIO_IO_DeInit(void)
00737 {
00738 }
00739 
00740 /**
00741   * @brief  Writes a single data.
00742   * @param  Addr: I2C address
00743   * @param  Reg: Reg address 
00744   * @param  Value: Data to be written
00745   * @retval None
00746   */
00747 void AUDIO_IO_Write(uint8_t Addr, uint16_t Reg, uint16_t Value)
00748 {
00749   uint16_t tmp = Value;
00750   
00751   Value = ((uint16_t)(tmp >> 8) & 0x00FF);
00752   
00753   Value |= ((uint16_t)(tmp << 8)& 0xFF00);
00754   
00755   I2Cx_WriteMultiple(Addr, Reg, I2C_MEMADD_SIZE_16BIT,(uint8_t*)&Value, 2);
00756 }
00757 
00758 /**
00759   * @brief  Reads a single data.
00760   * @param  Addr: I2C address
00761   * @param  Reg: Reg address 
00762   * @retval Data to be read
00763   */
00764 uint16_t AUDIO_IO_Read(uint8_t Addr, uint16_t Reg)
00765 {
00766   uint16_t read_value = 0, tmp = 0;
00767   
00768   I2Cx_ReadMultiple(Addr, Reg, I2C_MEMADD_SIZE_16BIT, (uint8_t*)&read_value, 2); 
00769   
00770   tmp = ((uint16_t)(read_value >> 8) & 0x00FF);
00771   
00772   tmp |= ((uint16_t)(read_value << 8)& 0xFF00);
00773   
00774   read_value = tmp;
00775   
00776   return read_value;
00777 }
00778 
00779 /**
00780   * @brief  AUDIO Codec delay
00781   * @param  Delay: Delay in ms
00782   * @retval None
00783   */
00784 void AUDIO_IO_Delay(uint32_t Delay)
00785 {
00786   HAL_Delay(Delay);
00787 }
00788 
00789 /********************************* LINK CAMERA ********************************/
00790 
00791 /**
00792   * @brief  Initializes Camera low level.
00793   * @retval None
00794   */
00795 void CAMERA_IO_Init(void) 
00796 {
00797   I2Cx_Init();
00798 }
00799 
00800 /**
00801   * @brief  Camera writes single data.
00802   * @param  Addr: I2C address
00803   * @param  Reg: Register address 
00804   * @param  Value: Data to be written
00805   * @retval None
00806   */
00807 void CAMERA_IO_Write(uint8_t Addr, uint16_t Reg, uint16_t Value)
00808 {
00809   
00810   I2Cx_WriteMultiple(Addr, (uint16_t)Reg, I2C_MEMADD_SIZE_8BIT,(uint8_t*)&Value, 1);
00811   
00812 }
00813 
00814 /**
00815   * @brief  Camera reads single data.
00816   * @param  Addr: I2C address
00817   * @param  Reg: Register address 
00818   * @retval Read data
00819   */
00820 uint16_t CAMERA_IO_Read(uint8_t Addr, uint16_t Reg)
00821 {
00822   
00823   uint16_t Read_Value = 0;
00824   
00825   I2Cx_ReadMultiple(Addr, Reg, I2C_MEMADD_SIZE_8BIT, (uint8_t*)&Read_Value, 1);
00826   
00827   return Read_Value;
00828   
00829 }
00830 
00831 /**
00832   * @brief  Camera delay 
00833   * @param  Delay: Delay in ms
00834   * @retval None
00835   */
00836 void CAMERA_Delay(uint32_t Delay)
00837 {
00838   HAL_Delay(Delay);
00839 }
00840 
00841 /******************************** LINK I2C EEPROM *****************************/
00842 
00843 /**
00844   * @brief  Initializes peripherals used by the I2C EEPROM driver.
00845   * @retval None
00846   */
00847 void EEPROM_IO_Init(void)
00848 {
00849   I2Cx_Init();
00850 }
00851 
00852 /**
00853   * @brief  Write data to I2C EEPROM driver in using DMA channel.
00854   * @param  DevAddress: Target device address
00855   * @param  MemAddress: Internal memory address
00856   * @param  pBuffer: Pointer to data buffer
00857   * @param  BufferSize: Amount of data to be sent
00858   * @retval HAL status
00859   */
00860 HAL_StatusTypeDef EEPROM_IO_WriteData(uint16_t DevAddress, uint16_t MemAddress, uint8_t* pBuffer, uint32_t BufferSize)
00861 {
00862   return (I2Cx_WriteMultiple(DevAddress, MemAddress, I2C_MEMADD_SIZE_16BIT, pBuffer, BufferSize));
00863 }
00864 
00865 /**
00866   * @brief  Read data from I2C EEPROM driver in using DMA channel.
00867   * @param  DevAddress: Target device address
00868   * @param  MemAddress: Internal memory address
00869   * @param  pBuffer: Pointer to data buffer
00870   * @param  BufferSize: Amount of data to be read
00871   * @retval HAL status
00872   */
00873 HAL_StatusTypeDef EEPROM_IO_ReadData(uint16_t DevAddress, uint16_t MemAddress, uint8_t* pBuffer, uint32_t BufferSize)
00874 {
00875   return (I2Cx_ReadMultiple(DevAddress, MemAddress, I2C_MEMADD_SIZE_16BIT, pBuffer, BufferSize));
00876 }
00877 
00878 /**
00879   * @brief  Checks if target device is ready for communication. 
00880   * @note   This function is used with Memory devices
00881   * @param  DevAddress: Target device address
00882   * @param  Trials: Number of trials
00883   * @retval HAL status
00884   */
00885 HAL_StatusTypeDef EEPROM_IO_IsDeviceReady(uint16_t DevAddress, uint32_t Trials)
00886 { 
00887   return (I2Cx_IsDeviceReady(DevAddress, Trials));
00888 }
00889 
00890 /******************************** LINK TS (TouchScreen) *****************************/
00891 
00892 /**
00893   * @brief  Initialize I2C communication
00894   *         channel from MCU to TouchScreen (TS).
00895   */
00896 void TS_IO_Init(void)
00897 {
00898   I2Cx_Init();
00899 }
00900 
00901 /**
00902   * @brief  Writes single data with I2C communication
00903   *         channel from MCU to TouchScreen.
00904   * @param  Addr: I2C address
00905   * @param  Reg: Register address
00906   * @param  Value: Data to be written
00907   */
00908 void TS_IO_Write(uint8_t Addr, uint8_t Reg, uint8_t Value)
00909 {
00910   I2Cx_Write(Addr, Reg, Value);
00911 }
00912 
00913 /**
00914   * @brief  Reads single data with I2C communication
00915   *         channel from TouchScreen.
00916   * @param  Addr: I2C address
00917   * @param  Reg: Register address
00918   * @retval Read data
00919   */
00920 uint8_t TS_IO_Read(uint8_t Addr, uint8_t Reg)
00921 {
00922   return I2Cx_Read(Addr, Reg);
00923 }
00924 
00925 /**
00926   * @brief  Reads multiple data with I2C communication
00927   *         channel from TouchScreen.
00928   * @param  Addr: I2C address
00929   * @param  Reg: Register address
00930   * @param  Buffer: Pointer to data buffer
00931   * @param  Length: Length of the data
00932   * @retval Number of read data
00933   */
00934 uint16_t TS_IO_ReadMultiple(uint8_t Addr, uint8_t Reg, uint8_t *Buffer, uint16_t Length)
00935 {
00936  return I2Cx_ReadMultiple(Addr, (uint16_t)Reg, I2C_MEMADD_SIZE_8BIT, Buffer, Length);
00937 }
00938 
00939 /**
00940   * @brief  Writes multiple data with I2C communication
00941   *         channel from MCU to TouchScreen.
00942   * @param  Addr: I2C address
00943   * @param  Reg: Register address
00944   * @param  Buffer: Pointer to data buffer
00945   * @param  Length: Length of the data
00946   * @retval None
00947   */
00948 void TS_IO_WriteMultiple(uint8_t Addr, uint8_t Reg, uint8_t *Buffer, uint16_t Length)
00949 {
00950   I2Cx_WriteMultiple(Addr, (uint16_t)Reg, I2C_MEMADD_SIZE_8BIT, Buffer, Length);
00951 }
00952 
00953 /**
00954   * @brief  Delay function used in TouchScreen low level driver.
00955   * @param  Delay: Delay in ms
00956   * @retval None
00957   */
00958 void TS_IO_Delay(uint32_t Delay)
00959 {
00960   HAL_Delay(Delay);
00961 }
00962 
00963 /**************************** LINK OTM8009A (Display driver) ******************/
00964 /**
00965   * @brief  OTM8009A delay
00966   * @param  Delay: Delay in ms
00967   */
00968 void OTM8009A_IO_Delay(uint32_t Delay)
00969 {
00970   HAL_Delay(Delay);
00971 }
00972 
00973 #if defined(USE_LCD_HDMI) 
00974 /**************************** LINK ADV7533 DSI-HDMI (Display driver) **********/
00975 /**
00976   * @brief  Initializes HDMI IO low level.
00977   * @retval None
00978   */
00979 void HDMI_IO_Init(void)
00980 {
00981   I2Cx_Init();
00982 }
00983 
00984 /**
00985   * @brief  HDMI writes single data.
00986   * @param  Addr: I2C address
00987   * @param  Reg: Register address 
00988   * @param  Value: Data to be written
00989   * @retval None
00990   */
00991 void HDMI_IO_Write(uint8_t Addr, uint8_t Reg, uint8_t Value)
00992 {
00993   I2Cx_Write(Addr, Reg, Value);
00994 }
00995 
00996 /**
00997   * @brief  Reads single data with I2C communication
00998   *         channel from HDMI bridge.
00999   * @param  Addr: I2C address
01000   * @param  Reg: Register address
01001   * @retval Read data
01002   */
01003 uint8_t HDMI_IO_Read(uint8_t Addr, uint8_t Reg)
01004 {
01005   return I2Cx_Read(Addr, Reg);
01006 }
01007 
01008 /**
01009   * @brief  HDMI delay 
01010   * @param  Delay: Delay in ms
01011   * @retval None
01012   */
01013 void HDMI_IO_Delay(uint32_t Delay)
01014 {
01015   HAL_Delay(Delay);
01016 }
01017 #endif /* USE_LCD_HDMI */
01018 
01019 /**
01020   * @}
01021   */
01022 
01023 /**
01024   * @}
01025   */
01026 /**
01027   * @}
01028   */
01029 
01030 /**
01031   * @}
01032   */
01033   
01034 /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/