ST / BSP_DISCO_L4R9I

Dependents:   DISCO_L4R9I-LCD-demo

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers stm32l4r9i_discovery.c Source File

stm32l4r9i_discovery.c

Go to the documentation of this file.
00001 /**
00002   ******************************************************************************
00003   * @file    stm32l4r9i_discovery.c
00004   * @author  MCD Application Team
00005   * @brief   This file provides a set of firmware functions to manage Leds,
00006   *          push-button and joystick of STM32L4R9I_DISCOVERY board (MB1311)
00007   ******************************************************************************
00008   * @attention
00009   *
00010   * <h2><center>&copy; Copyright (c) 2017 STMicroelectronics.
00011   * All rights reserved.</center></h2>
00012   *
00013   * This software component is licensed by ST under BSD 3-Clause license,
00014   * the "License"; You may not use this file except in compliance with the
00015   * License. You may obtain a copy of the License at:
00016   *                        opensource.org/licenses/BSD-3-Clause
00017   *
00018   ******************************************************************************
00019   */
00020 
00021 /* Includes ------------------------------------------------------------------*/
00022 #include "stm32l4r9i_discovery.h"
00023 #include "stm32l4r9i_discovery_io.h"
00024 #include "stm32l4r9i_discovery_lcd.h"
00025 
00026 /** @addtogroup BSP
00027   * @{
00028   */
00029 
00030 /** @defgroup STM32L4R9I_DISCOVERY STM32L4R9I_DISCOVERY
00031   * @{
00032   */
00033 
00034 /** @defgroup STM32L4R9I_DISCOVERY_Common STM32L4R9I_DISCOVERY Common
00035   * @{
00036   */
00037 
00038 /** @defgroup STM32L4R9I_DISCOVERY_Private_Defines Private Defines
00039   * @{
00040   */
00041 
00042 /**
00043  * @brief STM32L4R9I_DISCOVERY BSP Driver version number
00044    */
00045 #define __STM32L4R9I_DISCOVERY_BSP_VERSION_MAIN   (0x01) /*!< [31:24] main version */
00046 #define __STM32L4R9I_DISCOVERY_BSP_VERSION_SUB1   (0x00) /*!< [23:16] sub1 version */
00047 #define __STM32L4R9I_DISCOVERY_BSP_VERSION_SUB2   (0x02) /*!< [15:8]  sub2 version */
00048 #define __STM32L4R9I_DISCOVERY_BSP_VERSION_RC     (0x00) /*!< [7:0]  release candidate */
00049 #define __STM32L4R9I_DISCOVERY_BSP_VERSION            ((__STM32L4R9I_DISCOVERY_BSP_VERSION_MAIN << 24)\
00050                                                       |(__STM32L4R9I_DISCOVERY_BSP_VERSION_SUB1 << 16)\
00051                                                       |(__STM32L4R9I_DISCOVERY_BSP_VERSION_SUB2 << 8 )\
00052                                                       |(__STM32L4R9I_DISCOVERY_BSP_VERSION_RC))
00053 
00054 #ifdef HAL_I2C_MODULE_ENABLED
00055 /**
00056   * @brief BSP I2C users
00057   */
00058 #define BSP_I2C_NO_USER     0x00000000U
00059 #define BSP_I2C_AUDIO_USER  0x00000001U
00060 #define BSP_I2C_TS_USER     0x00000002U
00061 #define BSP_I2C_MFX_USER    0x00000004U
00062 #define BSP_I2C_CAMERA_USER 0x00000008U
00063 #define BSP_I2C_ALL_USERS   0x0000000FU
00064 #endif
00065 
00066 /**
00067   * @}
00068   */
00069 
00070 
00071 /** @defgroup STM32L4R9I_DISCOVERY_Exported_Variables Exported Variables
00072   * @{
00073   */
00074 
00075 /**
00076  * @brief LED variables
00077  */
00078 const uint16_t  LED_PIN[LEDn] =                 {LED1_PIN,
00079                                                  LED2_PIN};
00080 
00081 /**
00082  * @brief COM port variables
00083  */
00084 #if defined(HAL_UART_MODULE_ENABLED)
00085 USART_TypeDef* COM_USART[COMn] = {DISCOVERY_COM1};
00086 GPIO_TypeDef* COM_TX_PORT[COMn] = {DISCOVERY_COM1_TX_GPIO_PORT};
00087 GPIO_TypeDef* COM_RX_PORT[COMn] = {DISCOVERY_COM1_RX_GPIO_PORT};
00088 const uint16_t COM_TX_PIN[COMn] = {DISCOVERY_COM1_TX_PIN};
00089 const uint16_t COM_RX_PIN[COMn] = {DISCOVERY_COM1_RX_PIN};
00090 const uint16_t COM_TX_AF[COMn] = {DISCOVERY_COM1_TX_AF};
00091 const uint16_t COM_RX_AF[COMn] = {DISCOVERY_COM1_RX_AF};
00092 #endif /* HAL_UART_MODULE_ENABLED */
00093 
00094 /**
00095  * @brief BUS variables
00096  */
00097 #if defined(HAL_I2C_MODULE_ENABLED)
00098 uint32_t I2c1Timeout = DISCOVERY_I2C1_TIMEOUT_MAX;  /*<! Value of Timeout when I2C1 communication fails */
00099 static   I2C_HandleTypeDef I2c1Handle = {0};
00100 static uint32_t v_bspI2cUsers = BSP_I2C_NO_USER;
00101 #if defined(BSP_USE_CMSIS_OS)
00102 static osSemaphoreId BspI2cSemaphore;
00103 #endif
00104 #endif /* HAL_I2C_MODULE_ENABLED */
00105 
00106 /* LCD/PSRAM initialization status sharing the same power source */
00107 uint32_t bsp_lcd_initialized = 0;
00108 uint32_t bsp_psram_initialized = 0;
00109 
00110 /**
00111   * @}
00112   */
00113 
00114 /** @defgroup STM32L4R9I_DISCOVERY_Private_FunctionPrototypes Private Functions Prototypes
00115   * @{
00116   */
00117 /**************************** Bus functions ************************************/
00118 /* I2C1 bus function */
00119 #if defined(HAL_I2C_MODULE_ENABLED)
00120 static void               I2C1_Init(uint32_t user);
00121 static void               I2C1_MspInit(I2C_HandleTypeDef *hi2c);
00122 void                      I2C1_DeInit(uint32_t user);
00123 static void               I2C1_MspDeInit(I2C_HandleTypeDef *hi2c);
00124 static void               I2C1_WriteData(uint16_t Addr, uint16_t Reg, uint16_t RegSize, uint8_t Value);
00125 static HAL_StatusTypeDef  I2C1_WriteBuffer(uint16_t Addr, uint16_t Reg, uint16_t RegSize, uint8_t *pBuffer, uint16_t Length);
00126 static uint8_t            I2C1_ReadData(uint16_t Addr, uint16_t Reg, uint16_t RegSize);
00127 static HAL_StatusTypeDef  I2C1_ReadBuffer(uint16_t Addr, uint16_t Reg, uint16_t RegSize, uint8_t *pBuffer, uint16_t Length);
00128 static void               I2C1_Error(void);
00129 static HAL_StatusTypeDef  I2C1_ReadMultiple(uint8_t Addr, uint16_t Reg, uint16_t MemAddSize, uint8_t *Buffer, uint16_t Length);
00130 static HAL_StatusTypeDef  I2C1_WriteMultiple(uint8_t Addr, uint16_t Reg, uint16_t MemAddSize, uint8_t *Buffer, uint16_t Length);
00131 static uint8_t            I2C1_isDeviceReady(uint16_t Addr, uint32_t Trials);
00132 #endif/* HAL_I2C_MODULE_ENABLED */
00133 
00134 /**************************** Link functions ***********************************/
00135 #if defined(HAL_I2C_MODULE_ENABLED)
00136 
00137 /* Link functions for Audio Codec peripheral */
00138 void            AUDIO_IO_Init(void);
00139 void            AUDIO_IO_DeInit(void);
00140 void            AUDIO_IO_Write(uint8_t Addr, uint8_t Reg, uint8_t Value);
00141 uint8_t         AUDIO_IO_Read(uint8_t Addr, uint8_t Reg);
00142 void            AUDIO_IO_Delay(uint32_t delay);
00143 
00144 /* TouchScreen (TS) IO functions */
00145 void            TS_IO_Init(void);
00146 void            TS_IO_Write(uint8_t Addr, uint8_t Reg, uint8_t Value);
00147 uint8_t         TS_IO_Read(uint8_t Addr, uint8_t Reg);
00148 uint16_t        TS_IO_ReadMultiple(uint8_t Addr, uint8_t Reg, uint8_t *Buffer, uint16_t Length);
00149 void            TS_IO_WriteMultiple(uint8_t Addr, uint8_t Reg, uint8_t *Buffer, uint16_t Length);
00150 void            TS_IO_Delay(uint32_t Delay);
00151 
00152 /* CAMERA IO functions */
00153 void            CAMERA_IO_Init(void);
00154 void            CAMERA_Delay(uint32_t Delay);
00155 void            CAMERA_IO_Write(uint8_t Addr, uint8_t Reg, uint8_t Value);
00156 uint8_t         CAMERA_IO_Read(uint8_t Addr, uint8_t Reg);
00157 
00158 /* Link functions for IDD measurment */
00159 void            MFX_IO_Init(void);
00160 void            MFX_IO_DeInit(void);
00161 void            MFX_IO_ITConfig (void);
00162 void            MFX_IO_EnableWakeupPin(void);
00163 void            MFX_IO_Wakeup(void);
00164 void            MFX_IO_Delay(uint32_t delay);
00165 void            MFX_IO_Write(uint16_t addr, uint8_t reg, uint8_t value);
00166 uint8_t         MFX_IO_Read(uint16_t addr, uint8_t reg);
00167 void            MFX_IO_WriteMultiple(uint16_t Addr, uint8_t Reg, uint8_t *Buffer, uint16_t Length);
00168 uint16_t        MFX_IO_ReadMultiple(uint16_t addr, uint8_t reg, uint8_t *buffer, uint16_t length);
00169 
00170 #endif/* HAL_I2C_MODULE_ENABLED */
00171 /**
00172   * @}
00173   */
00174 
00175 /** @defgroup STM32L4R9I_DISCOVERY_Exported_Functions Exported Functions
00176   * @{
00177   */
00178 
00179 /**
00180   * @brief  Error Handler
00181   * @note   Defined as a weak function to be overwritten by the application.
00182   * @retval None
00183   */
00184 __weak void BSP_ErrorHandler(void)
00185 {
00186   while(1);
00187 }
00188 
00189 /**
00190   * @brief  This method returns the STM32L4R9I DISCOVERY BSP Driver revision
00191   * @retval version : 0xXYZR (8bits for each decimal, R for RC)
00192   */
00193 uint32_t BSP_GetVersion(void)
00194 {
00195   return __STM32L4R9I_DISCOVERY_BSP_VERSION;
00196 }
00197 
00198 /**
00199   * @brief  Configures LED GPIOs.
00200   * @param  Led: Specifies the Led to be configured.
00201   *   This parameter can be one of following parameters:
00202   *     @arg DISCO_LED1
00203   *     @arg DISCO_LED2
00204   * @retval None
00205   */
00206 void BSP_LED_Init(Led_TypeDef Led)
00207 {
00208   GPIO_InitTypeDef  GPIO_InitStructure;
00209 
00210   if (Led == DISCO_LED2)
00211   {
00212     /* Enable the GPIO_LED clock */
00213     LED2_GPIO_CLK_ENABLE();
00214 
00215     /* Configure the GPIO_LED pin */
00216     GPIO_InitStructure.Pin   = LED_PIN[Led];
00217     GPIO_InitStructure.Mode  = GPIO_MODE_OUTPUT_PP;
00218     GPIO_InitStructure.Pull  = GPIO_NOPULL;
00219     GPIO_InitStructure.Speed = GPIO_SPEED_FREQ_VERY_HIGH;
00220 
00221     HAL_GPIO_Init(LED2_GPIO_PORT, &GPIO_InitStructure);
00222     /* By default, turn off LED */
00223     HAL_GPIO_WritePin(LED2_GPIO_PORT, GPIO_InitStructure.Pin, GPIO_PIN_SET);
00224   }
00225   else
00226   {
00227     /* Initialize the IO functionalities */
00228     if (BSP_IO_Init() == IO_ERROR)
00229     {
00230       BSP_ErrorHandler();
00231     }
00232 
00233     /* By default, turn off LED */
00234     BSP_IO_WritePin(LED_PIN[Led], GPIO_PIN_SET);
00235 
00236     BSP_IO_ConfigPin(LED_PIN[Led],IO_MODE_OUTPUT);
00237   }
00238 
00239 }
00240 
00241 
00242 /**
00243   * @brief  Unconfigures LED GPIOs.
00244   * @param  Led: Specifies the Led to be unconfigured.
00245   *   This parameter can be one of following parameters:
00246   *     @arg DISCO_LED1
00247   *     @arg DISCO_LED2
00248   * @retval None
00249   */
00250 void BSP_LED_DeInit(Led_TypeDef Led)
00251 {
00252   if (Led == DISCO_LED2)
00253   {
00254     /* Enable the GPIO_LED clock */
00255     LED2_GPIO_CLK_ENABLE();
00256 
00257     HAL_GPIO_DeInit(LED2_GPIO_PORT, LED_PIN[Led]);
00258   }
00259 }
00260 
00261 
00262 /**
00263   * @brief  Turns selected LED On.
00264   * @param  Led: Specifies the Led to be set on.
00265   *   This parameter can be one of following parameters:
00266   *     @arg DISCO_LED1
00267   *     @arg DISCO_LED2
00268   * @retval None
00269   */
00270 void BSP_LED_On(Led_TypeDef Led)
00271 {
00272   if (Led == DISCO_LED2)
00273   {
00274     HAL_GPIO_WritePin(LED2_GPIO_PORT, LED_PIN[Led], GPIO_PIN_RESET);
00275   }
00276   else
00277   {
00278     BSP_IO_WritePin(LED_PIN[Led], GPIO_PIN_RESET);
00279   }
00280 }
00281 
00282 /**
00283   * @brief  Turns selected LED Off.
00284   * @param  Led: Specifies the Led to be set off.
00285   *   This parameter can be one of following parameters:
00286   *     @arg DISCO_LED1
00287   *     @arg DISCO_LED2
00288   * @retval None
00289   */
00290 void BSP_LED_Off(Led_TypeDef Led)
00291 {
00292   if (Led == DISCO_LED2)
00293   {
00294     HAL_GPIO_WritePin(LED2_GPIO_PORT, LED_PIN[Led], GPIO_PIN_SET);
00295   }
00296   else
00297   {
00298     BSP_IO_WritePin(LED_PIN[Led], GPIO_PIN_SET);
00299   }
00300 }
00301 
00302 /**
00303   * @brief  Toggles the selected LED.
00304   * @param  Led: Specifies the Led to be toggled.
00305   *   This parameter can be one of following parameters:
00306   *     @arg DISCO_LED1
00307   *     @arg DISCO_LED2
00308   * @retval None
00309   */
00310 void BSP_LED_Toggle(Led_TypeDef Led)
00311 {
00312   if (Led == DISCO_LED2)
00313   {
00314     HAL_GPIO_TogglePin(LED2_GPIO_PORT, LED_PIN[Led]);
00315   }
00316   else
00317   {
00318     BSP_IO_TogglePin(LED_PIN[Led]);
00319   }
00320 }
00321 
00322 /**
00323   * @brief  Configures all buttons of the joystick in GPIO or EXTI modes.
00324   * @param  Joy_Mode: Joystick mode.
00325   *    This parameter can be one of the following values:
00326   *     @arg  JOY_MODE_GPIO: Joystick pins will be used as simple IOs
00327   *     @arg  JOY_MODE_EXTI: Joystick pins will be connected to EXTI line
00328   *                                 with interrupt generation capability
00329   * @retval HAL_OK: if all initializations are OK. Other value if error.
00330   */
00331 uint8_t BSP_JOY_Init(JOYMode_TypeDef Joy_Mode)
00332 {
00333   uint8_t Status = HAL_OK;
00334   GPIO_InitTypeDef GPIO_InitStruct;
00335 
00336   /* Initialized BSP IO */
00337   BSP_IO_Init();
00338   /* Common configuration for GPIO used for joystick SEL */
00339   SEL_JOY_GPIO_CLK_ENABLE();
00340   GPIO_InitStruct.Pin   = SEL_JOY_PIN;
00341   GPIO_InitStruct.Pull  = GPIO_PULLDOWN;
00342   GPIO_InitStruct.Speed = GPIO_SPEED_LOW;
00343 
00344   if(Joy_Mode == JOY_MODE_GPIO)
00345   {
00346     /* Configure GPIO used for joystick SEL as input */
00347     GPIO_InitStruct.Mode = GPIO_MODE_INPUT;
00348     HAL_GPIO_Init(SEL_JOY_GPIO_PORT, &GPIO_InitStruct);
00349 
00350     /* Configure other joystick pins */
00351     BSP_IO_ConfigPin((RIGHT_JOY_PIN | LEFT_JOY_PIN | UP_JOY_PIN | DOWN_JOY_PIN), IO_MODE_INPUT_PU);
00352   }
00353   else if(Joy_Mode == JOY_MODE_EXTI)
00354   {
00355     /* Configure GPIO used for joystick SEL as input with External interrupt */
00356     GPIO_InitStruct.Mode = GPIO_MODE_IT_RISING;
00357     HAL_GPIO_Init(SEL_JOY_GPIO_PORT, &GPIO_InitStruct);
00358     /* Enable and set corresponding EXTI Interrupt to the lowest priority */
00359     HAL_NVIC_SetPriority((IRQn_Type)(SEL_JOY_EXTI_IRQn), 0x0F, 0x00);
00360     HAL_NVIC_EnableIRQ((IRQn_Type)(SEL_JOY_EXTI_IRQn));
00361 
00362     /* Configure other joystick pins */
00363     BSP_IO_ConfigPin((RIGHT_JOY_PIN | LEFT_JOY_PIN | UP_JOY_PIN | DOWN_JOY_PIN), IO_MODE_IT_HIGH_LEVEL_PD);
00364     /* Configure IO Expander interrupt */
00365     MFX_IO_ITConfig();
00366   }
00367   else
00368   {
00369     Status = HAL_ERROR;
00370   }
00371   return Status;
00372 }
00373 
00374 /**
00375   * @brief  Unonfigures all GPIOs used as buttons of the joystick.
00376   * @retval None.
00377   */
00378 void BSP_JOY_DeInit(void)
00379 {
00380   /* Enable the JOY clock */
00381   SEL_JOY_GPIO_CLK_ENABLE();
00382   HAL_GPIO_DeInit(SEL_JOY_GPIO_PORT, SEL_JOY_PIN);
00383 
00384   BSP_IO_ConfigPin((RIGHT_JOY_PIN | LEFT_JOY_PIN | UP_JOY_PIN | DOWN_JOY_PIN), IO_MODE_OFF);
00385 }
00386 
00387 /**
00388 * @brief  Returns the current joystick status.
00389 * @retval Code of the joystick key pressed
00390 *          This code can be one of the following values:
00391 *            @arg  JOY_NONE
00392 *            @arg  JOY_SEL
00393 *            @arg  JOY_DOWN
00394 *            @arg  JOY_LEFT
00395 *            @arg  JOY_RIGHT
00396 *            @arg  JOY_UP
00397 */
00398 JOYState_TypeDef BSP_JOY_GetState(void)
00399 {
00400   uint32_t tmp = 0;
00401 
00402   if(HAL_GPIO_ReadPin(SEL_JOY_GPIO_PORT, SEL_JOY_PIN) != GPIO_PIN_RESET)
00403   {
00404     return JOY_SEL;
00405   }
00406 
00407   tmp = BSP_IO_ReadPin((RIGHT_JOY_PIN | LEFT_JOY_PIN | UP_JOY_PIN | DOWN_JOY_PIN));
00408   /* Check the pressed joystick button */
00409   if(tmp & DOWN_JOY_PIN)
00410   {
00411     return JOY_DOWN;
00412   }
00413   else if(tmp & LEFT_JOY_PIN)
00414   {
00415     return JOY_LEFT;
00416   }
00417   else if(tmp & RIGHT_JOY_PIN)
00418   {
00419     return JOY_RIGHT;
00420   }
00421   else if(tmp & UP_JOY_PIN)
00422   {
00423     return JOY_UP;
00424   }
00425   else
00426   {
00427     return JOY_NONE;
00428   }
00429 }
00430 
00431 #if defined(HAL_UART_MODULE_ENABLED)
00432 /**
00433   * @brief  Configures COM port.
00434   * @param  COM: COM port to be configured.
00435   *          This parameter can be one of the following values:
00436   *            @arg  COM1
00437   * @param  huart: Pointer to a UART_HandleTypeDef structure that contains the
00438   *                configuration information for the specified USART peripheral.
00439   */
00440 void BSP_COM_Init(COM_TypeDef COM, UART_HandleTypeDef *huart)
00441 {
00442   GPIO_InitTypeDef gpio_init_structure;
00443 
00444   /* Enable GPIO clock */
00445   DISCOVERY_COMx_TX_GPIO_CLK_ENABLE(COM);
00446   DISCOVERY_COMx_RX_GPIO_CLK_ENABLE(COM);
00447 
00448   /* Enable USART clock */
00449   DISCOVERY_COMx_CLK_ENABLE(COM);
00450 
00451   /* Configure USART Tx as alternate function */
00452   gpio_init_structure.Pin = COM_TX_PIN[COM];
00453   gpio_init_structure.Mode = GPIO_MODE_AF_PP;
00454   gpio_init_structure.Speed = GPIO_SPEED_FREQ_HIGH;
00455   gpio_init_structure.Pull = GPIO_NOPULL;
00456   gpio_init_structure.Alternate = COM_TX_AF[COM];
00457   HAL_GPIO_Init(COM_TX_PORT[COM], &gpio_init_structure);
00458 
00459   /* Configure USART Rx as alternate function */
00460   gpio_init_structure.Pin = COM_RX_PIN[COM];
00461   gpio_init_structure.Mode = GPIO_MODE_AF_PP;
00462   gpio_init_structure.Alternate = COM_RX_AF[COM];
00463   HAL_GPIO_Init(COM_RX_PORT[COM], &gpio_init_structure);
00464 
00465   /* USART configuration */
00466   huart->Instance = COM_USART[COM];
00467   HAL_UART_Init(huart);
00468 }
00469 
00470 /**
00471   * @brief  DeInit COM port.
00472   * @param  COM: COM port to be configured.
00473   *          This parameter can be one of the following values:
00474   *            @arg  COM1
00475   * @param  huart: Pointer to a UART_HandleTypeDef structure that contains the
00476   *                configuration information for the specified USART peripheral.
00477   */
00478 void BSP_COM_DeInit(COM_TypeDef COM, UART_HandleTypeDef *huart)
00479 {
00480   /* USART deinitialization */
00481   huart->Instance = COM_USART[COM];
00482   HAL_UART_DeInit(huart);
00483 
00484   /* Disable USART clock */
00485   DISCOVERY_COMx_CLK_DISABLE(COM);
00486 
00487   /* USART TX/RX pins deinitializations */
00488   HAL_GPIO_DeInit(COM_TX_PORT[COM], COM_TX_PIN[COM]);
00489   HAL_GPIO_DeInit(COM_RX_PORT[COM], COM_RX_PIN[COM]);
00490 
00491   /* Disable GPIOs clock is left for application */
00492 }
00493 #endif /* HAL_UART_MODULE_ENABLED */
00494 
00495 /**
00496   * @}
00497   */
00498 
00499 /** @defgroup STM32L4R9I_DISCOVERY_Private_Functions Private Functions
00500   * @{
00501   */
00502 
00503 /*******************************************************************************
00504                             BUS OPERATIONS
00505 *******************************************************************************/
00506 
00507 #if defined(HAL_I2C_MODULE_ENABLED)
00508 /******************************* I2C Routines**********************************/
00509 /**
00510   * @brief Discovery I2C1 Bus initialization
00511   * @retval None
00512   */
00513 static void I2C1_Init(uint32_t user)
00514 {
00515   if(HAL_I2C_GetState(&I2c1Handle) == HAL_I2C_STATE_RESET)
00516   {
00517 #if defined(BSP_USE_CMSIS_OS)
00518     /* Create semaphore to prevent multiple I2C access */
00519     osSemaphoreDef(BSP_I2C_SEM);
00520     BspI2cSemaphore = osSemaphoreCreate(osSemaphore(BSP_I2C_SEM), 1);
00521 #endif
00522 
00523     I2c1Handle.Instance              = I2C1;
00524     I2c1Handle.Init.Timing           = DISCOVERY_I2C1_TIMING;
00525     I2c1Handle.Init.OwnAddress1      = 0x70;
00526     I2c1Handle.Init.AddressingMode   = I2C_ADDRESSINGMODE_7BIT;
00527     I2c1Handle.Init.DualAddressMode  = I2C_DUALADDRESS_DISABLE;
00528     I2c1Handle.Init.OwnAddress2      = 0xFF;
00529     I2c1Handle.Init.GeneralCallMode  = I2C_GENERALCALL_DISABLE;
00530     I2c1Handle.Init.NoStretchMode    = I2C_NOSTRETCH_DISABLE;
00531 
00532     /* Init the I2C */
00533     I2C1_MspInit(&I2c1Handle);
00534     HAL_I2C_Init(&I2c1Handle);
00535   }
00536   /* Update BSP I2C users list */
00537   v_bspI2cUsers |= user;
00538 }
00539 
00540 /**
00541   * @brief Discovery I2C1 MSP Initialization
00542   * @param hi2c: I2C1 handle
00543   * @retval None
00544   */
00545 static void I2C1_MspInit(I2C_HandleTypeDef *hi2c)
00546 {
00547   GPIO_InitTypeDef          GPIO_InitStructure;
00548   RCC_PeriphCLKInitTypeDef  RCC_PeriphCLKInitStruct;
00549 
00550   if (hi2c->Instance == I2C1)
00551   {
00552     /*##-1- Configure the Discovery I2C1 clock source. The clock is derived from the SYSCLK #*/
00553     RCC_PeriphCLKInitStruct.PeriphClockSelection = RCC_PERIPHCLK_I2C1;
00554     RCC_PeriphCLKInitStruct.I2c1ClockSelection   = RCC_I2C1CLKSOURCE_SYSCLK;
00555     HAL_RCCEx_PeriphCLKConfig(&RCC_PeriphCLKInitStruct);
00556 
00557     /*##-2- Configure the GPIOs ################################################*/
00558     /* Enable GPIO clock */
00559     __HAL_RCC_GPIOB_CLK_ENABLE();
00560     /* VddIO2 must be enabled to access GPIO port G[2:15] */
00561     __HAL_RCC_PWR_CLK_ENABLE();
00562     HAL_PWREx_EnableVddIO2();
00563     __HAL_RCC_GPIOG_CLK_ENABLE();
00564 
00565     /* Configure I2C pins as alternate function  */
00566     GPIO_InitStructure.Pin       = GPIO_PIN_6;
00567     GPIO_InitStructure.Mode      = GPIO_MODE_AF_OD;
00568     GPIO_InitStructure.Pull      = GPIO_PULLUP;
00569     GPIO_InitStructure.Speed     = GPIO_SPEED_FREQ_VERY_HIGH;
00570     GPIO_InitStructure.Alternate = GPIO_AF4_I2C1;
00571     HAL_GPIO_Init(GPIOB, &GPIO_InitStructure); /* I2C1_SCL on PB6 */
00572     GPIO_InitStructure.Pin       = GPIO_PIN_13;
00573     HAL_GPIO_Init(GPIOG, &GPIO_InitStructure); /* I2C1_SDA on PG13 */
00574 
00575     /*##-3- Configure the Discovery I2C1 peripheral #############################*/
00576     /* Enable I2C1 clock */
00577     __HAL_RCC_I2C1_CLK_ENABLE();
00578 
00579     /* Force and release the I2C1 Peripheral */
00580     __HAL_RCC_I2C1_FORCE_RESET();
00581     __HAL_RCC_I2C1_RELEASE_RESET();
00582 
00583     /* Enable and set I2C1 event interrupt to the highest priority */
00584     HAL_NVIC_SetPriority(I2C1_EV_IRQn, 0x00, 0);
00585     HAL_NVIC_EnableIRQ(I2C1_EV_IRQn);
00586 
00587     /* Enable and set Discovery I2C1 error interrupt to the highest priority */
00588     HAL_NVIC_SetPriority(I2C1_ER_IRQn, 0x00, 0);
00589     HAL_NVIC_EnableIRQ(I2C1_ER_IRQn);
00590   }
00591 }
00592 
00593 /**
00594   * @brief Discovery I2C1 Bus Deinitialization
00595   * @retval None
00596   */
00597 void I2C1_DeInit(uint32_t user)
00598 {
00599   /* Update BSP I2C users list */
00600   v_bspI2cUsers &= ~(user);
00601 
00602   if((HAL_I2C_GetState(&I2c1Handle) != HAL_I2C_STATE_RESET) && (v_bspI2cUsers == BSP_I2C_NO_USER))
00603   {
00604     /* DeInit the I2C */
00605     HAL_I2C_DeInit(&I2c1Handle);
00606     I2C1_MspDeInit(&I2c1Handle);
00607 
00608 #if defined(BSP_USE_CMSIS_OS)
00609     /* Delete semaphore to prevent multiple I2C access */
00610     osSemaphoreDelete(BspI2cSemaphore);
00611 #endif
00612   }
00613 }
00614 
00615 /**
00616   * @brief Discovery I2C1 MSP DeInitialization
00617   * @param hi2c: I2C1 handle
00618   * @retval None
00619   */
00620 static void I2C1_MspDeInit(I2C_HandleTypeDef *hi2c)
00621 {
00622   if (hi2c->Instance == I2C1)
00623   {
00624     /*##-1- Unconfigure the GPIOs ################################################*/
00625     /* Enable GPIO clock */
00626     __HAL_RCC_GPIOB_CLK_ENABLE();
00627     /* VddIO2 must be enabled to access GPIO port G[2:15] */
00628     __HAL_RCC_PWR_CLK_ENABLE();
00629     HAL_PWREx_EnableVddIO2();
00630     __HAL_RCC_GPIOG_CLK_ENABLE();
00631 
00632     /* Reset configuration of I2C GPIOs as alternate function  */
00633     HAL_GPIO_DeInit(GPIOB, GPIO_PIN_6);  /* I2C1_SCL */
00634     HAL_GPIO_DeInit(GPIOG, GPIO_PIN_13); /* I2C1_SDA */
00635 
00636     /*##-2- Unconfigure the Discovery I2C1 peripheral ############################*/
00637     /* Force and release I2C1 Peripheral */
00638     __HAL_RCC_I2C1_FORCE_RESET();
00639     __HAL_RCC_I2C1_RELEASE_RESET();
00640 
00641     /* Disable I2C1 clock */
00642     __HAL_RCC_I2C1_CLK_DISABLE();
00643 
00644     /* Disable I2C1 interrupts */
00645     HAL_NVIC_DisableIRQ(I2C1_EV_IRQn);
00646     HAL_NVIC_DisableIRQ(I2C1_ER_IRQn);
00647   }
00648 }
00649 
00650 /**
00651   * @brief  Write a value in a register of the device through BUS.
00652   * @param  Addr: Device address on BUS Bus.
00653   * @param  Reg: The target register address to write
00654   * @param  RegSize: The target register size (can be 8BIT or 16BIT)
00655   * @param  Value: The target register value to be written
00656   * @retval None
00657   */
00658 static void I2C1_WriteData(uint16_t Addr, uint16_t Reg, uint16_t RegSize, uint8_t Value)
00659 {
00660   HAL_StatusTypeDef status = HAL_OK;
00661 
00662 #if defined(BSP_USE_CMSIS_OS)
00663   /* Get semaphore to prevent multiple I2C access */
00664   osSemaphoreWait(BspI2cSemaphore, osWaitForever);
00665 #endif
00666 
00667   status = HAL_I2C_Mem_Write(&I2c1Handle, Addr, (uint16_t)Reg, RegSize, &Value, 1, I2c1Timeout);
00668 
00669 #if defined(BSP_USE_CMSIS_OS)
00670   /* Release semaphore to prevent multiple I2C access */
00671   osSemaphoreRelease(BspI2cSemaphore);
00672 #endif
00673 
00674   /* Check the communication status */
00675   if(status != HAL_OK)
00676   {
00677     /* Re-Initiaize the BUS */
00678     I2C1_Error();
00679   }
00680 }
00681 
00682 /**
00683   * @brief  Write a value in a register of the device through BUS.
00684   * @param  Addr: Device address on BUS Bus.
00685   * @param  Reg: The target register address to write
00686   * @param  RegSize: The target register size (can be 8BIT or 16BIT)
00687   * @param  pBuffer: The target register value to be written
00688   * @param  Length: buffer size to be written
00689   * @retval None
00690   */
00691 static HAL_StatusTypeDef I2C1_WriteBuffer(uint16_t Addr, uint16_t Reg, uint16_t RegSize, uint8_t *pBuffer, uint16_t Length)
00692 {
00693   HAL_StatusTypeDef status = HAL_OK;
00694 
00695 #if defined(BSP_USE_CMSIS_OS)
00696   /* Get semaphore to prevent multiple I2C access */
00697   osSemaphoreWait(BspI2cSemaphore, osWaitForever);
00698 #endif
00699 
00700   status = HAL_I2C_Mem_Write(&I2c1Handle, Addr, (uint16_t)Reg, RegSize, pBuffer, Length, I2c1Timeout);
00701 
00702 #if defined(BSP_USE_CMSIS_OS)
00703   /* Release semaphore to prevent multiple I2C access */
00704   osSemaphoreRelease(BspI2cSemaphore);
00705 #endif
00706 
00707   /* Check the communication status */
00708   if(status != HAL_OK)
00709   {
00710     /* Re-Initiaize the BUS */
00711     I2C1_Error();
00712   }
00713 
00714   return status;
00715 }
00716 
00717 /**
00718   * @brief  Read a register of the device through BUS
00719   * @param  Addr: Device address on BUS
00720   * @param  Reg: The target register address to read
00721   * @param  RegSize: The target register size (can be 8BIT or 16BIT)
00722   * @retval read register value
00723   */
00724 static uint8_t I2C1_ReadData(uint16_t Addr, uint16_t Reg, uint16_t RegSize)
00725 {
00726   HAL_StatusTypeDef status = HAL_OK;
00727   uint8_t value = 0x0;
00728 
00729 #if defined(BSP_USE_CMSIS_OS)
00730   /* Get semaphore to prevent multiple I2C access */
00731   osSemaphoreWait(BspI2cSemaphore, osWaitForever);
00732 #endif
00733 
00734   status = HAL_I2C_Mem_Read(&I2c1Handle, Addr, Reg, RegSize, &value, 1, I2c1Timeout);
00735 
00736 #if defined(BSP_USE_CMSIS_OS)
00737   /* Release semaphore to prevent multiple I2C access */
00738   osSemaphoreRelease(BspI2cSemaphore);
00739 #endif
00740 
00741   /* Check the communication status */
00742   if(status != HAL_OK)
00743   {
00744     /* Re-Initiaize the BUS */
00745     I2C1_Error();
00746   }
00747 
00748   return value;
00749 }
00750 
00751 /**
00752 * @brief  Checks if target device is ready for communication.
00753 * @note   This function is used with Memory devices
00754 * @param  Addr: Target device address
00755 * @param  Trials: Number of trials
00756 * @retval HAL status
00757 */
00758 static uint8_t I2C1_isDeviceReady(uint16_t Addr, uint32_t Trials)
00759 {
00760   HAL_StatusTypeDef status = HAL_OK;
00761 
00762 #if defined(BSP_USE_CMSIS_OS)
00763   /* Get semaphore to prevent multiple I2C access */
00764   osSemaphoreWait(BspI2cSemaphore, osWaitForever);
00765 #endif
00766 
00767   status = HAL_I2C_IsDeviceReady(&I2c1Handle, Addr, Trials, 50);
00768 
00769 #if defined(BSP_USE_CMSIS_OS)
00770   /* Release semaphore to prevent multiple I2C access */
00771   osSemaphoreRelease(BspI2cSemaphore);
00772 #endif
00773 
00774   /* Check the communication status */
00775   if(status != HAL_OK)
00776   {
00777     /* Re-Initiaize the BUS */
00778     I2C1_Error();
00779   }
00780 
00781   return status;
00782 }
00783 
00784 /**
00785   * @brief  Reads multiple data on the BUS.
00786   * @param  Addr: I2C Address
00787   * @param  Reg: Reg Address
00788   * @param  RegSize : The target register size (can be 8BIT or 16BIT)
00789   * @param  pBuffer: pointer to read data buffer
00790   * @param  Length: length of the data
00791   * @retval 0 if no problems to read multiple data
00792   */
00793 static HAL_StatusTypeDef I2C1_ReadBuffer(uint16_t Addr, uint16_t Reg, uint16_t RegSize, uint8_t *pBuffer, uint16_t Length)
00794 {
00795   HAL_StatusTypeDef status = HAL_OK;
00796 
00797 #if defined(BSP_USE_CMSIS_OS)
00798   /* Get semaphore to prevent multiple I2C access */
00799   osSemaphoreWait(BspI2cSemaphore, osWaitForever);
00800 #endif
00801 
00802   status = HAL_I2C_Mem_Read(&I2c1Handle, Addr, (uint16_t)Reg, RegSize, pBuffer, Length, I2c1Timeout);
00803 
00804 #if defined(BSP_USE_CMSIS_OS)
00805   /* Release semaphore to prevent multiple I2C access */
00806   osSemaphoreRelease(BspI2cSemaphore);
00807 #endif
00808 
00809   /* Check the communication status */
00810   if(status != HAL_OK)
00811   {
00812     /* Re-Initiaize the BUS */
00813     I2C1_Error();
00814   }
00815 
00816   return status;
00817 }
00818 
00819 /**
00820   * @brief Discovery I2C1 error treatment function
00821   * @retval None
00822   */
00823 static void I2C1_Error(void)
00824 {
00825   uint32_t tmpI2cUsers;
00826 
00827   BSP_ErrorHandler();
00828 
00829   /* De-initialize the I2C communication BUS */
00830   tmpI2cUsers = v_bspI2cUsers;
00831   I2C1_DeInit(BSP_I2C_ALL_USERS);
00832 
00833   /* Re- Initiaize the I2C communication BUS */
00834   I2C1_Init(tmpI2cUsers);
00835 }
00836 
00837 /**
00838   * @brief  Reads multiple data.
00839   * @param  Addr: I2C address
00840   * @param  Reg: Reg address
00841   * @param  MemAddress: Memory address
00842   * @param  Buffer: Pointer to data buffer
00843   * @param  Length: Length of the data
00844   * @retval HAL status
00845   */
00846 static HAL_StatusTypeDef I2C1_ReadMultiple(uint8_t  Addr,
00847                                            uint16_t Reg,
00848                                            uint16_t MemAddress,
00849                                            uint8_t *Buffer,
00850                                            uint16_t Length)
00851 {
00852   HAL_StatusTypeDef status = HAL_OK;
00853 
00854 #if defined(BSP_USE_CMSIS_OS)
00855   /* Get semaphore to prevent multiple I2C access */
00856   osSemaphoreWait(BspI2cSemaphore, osWaitForever);
00857 #endif
00858 
00859   status = HAL_I2C_Mem_Read(&I2c1Handle, Addr, (uint16_t)Reg, MemAddress, Buffer, Length, I2c1Timeout);
00860 
00861 #if defined(BSP_USE_CMSIS_OS)
00862   /* Release semaphore to prevent multiple I2C access */
00863   osSemaphoreRelease(BspI2cSemaphore);
00864 #endif
00865 
00866   /* Check the communication status */
00867   if(status != HAL_OK)
00868   {
00869     /* I2C error occurred */
00870     I2C1_Error();
00871   }
00872   return status;
00873 }
00874 
00875 /**
00876   * @brief  Writes a value in a register of the device through BUS.
00877   * @param  Addr: Device address on BUS Bus.
00878   * @param  Reg: The target register address to write
00879   * @param  MemAddress: Memory address
00880   * @param  Buffer: The target register value to be written
00881   * @param  Length: buffer size to be written
00882   * @retval HAL status
00883   */
00884 static HAL_StatusTypeDef I2C1_WriteMultiple(uint8_t  Addr,
00885                                             uint16_t Reg,
00886                                             uint16_t MemAddress,
00887                                             uint8_t *Buffer,
00888                                             uint16_t Length)
00889 {
00890   HAL_StatusTypeDef status = HAL_OK;
00891 
00892 #if defined(BSP_USE_CMSIS_OS)
00893   /* Get semaphore to prevent multiple I2C access */
00894   osSemaphoreWait(BspI2cSemaphore, osWaitForever);
00895 #endif
00896 
00897   status = HAL_I2C_Mem_Write(&I2c1Handle, Addr, (uint16_t)Reg, MemAddress, Buffer, Length, I2c1Timeout);
00898 
00899 #if defined(BSP_USE_CMSIS_OS)
00900   /* Release semaphore to prevent multiple I2C access */
00901   osSemaphoreRelease(BspI2cSemaphore);
00902 #endif
00903 
00904   /* Check the communication status */
00905   if(status != HAL_OK)
00906   {
00907     /* Re-Initialize the I2C Bus */
00908     I2C1_Error();
00909   }
00910   return status;
00911 }
00912 #endif /*HAL_I2C_MODULE_ENABLED*/
00913 
00914 /*******************************************************************************
00915                             LINK OPERATIONS
00916 *******************************************************************************/
00917 
00918 #if defined(HAL_I2C_MODULE_ENABLED)
00919 /********************************* LINK MFX ***********************************/
00920 /**
00921   * @brief  Initializes MFX low level.
00922   * @retval None
00923   */
00924 void MFX_IO_Init(void)
00925 {
00926   /* I2C1 init */
00927   I2C1_Init(BSP_I2C_MFX_USER);
00928 
00929   /* Wait for device ready */
00930   if(I2C1_isDeviceReady(IO_I2C_ADDRESS, 4) != HAL_OK)
00931   {
00932      BSP_ErrorHandler();
00933   }
00934 }
00935 /**
00936   * @brief  Deinitializes MFX low level.
00937   * @retval None
00938   */
00939 void MFX_IO_DeInit(void)
00940 {
00941   GPIO_InitTypeDef  GPIO_InitStruct;
00942 
00943   /* Enable wakeup gpio clock */
00944   MFX_WAKEUP_GPIO_CLK_ENABLE();
00945 
00946   /* MFX wakeup pin configuration */
00947   GPIO_InitStruct.Pin   = MFX_WAKEUP_PIN;
00948   GPIO_InitStruct.Mode  = GPIO_MODE_OUTPUT_PP;
00949   GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW;
00950   GPIO_InitStruct.Pull  = GPIO_PULLDOWN;
00951   HAL_GPIO_Init(MFX_WAKEUP_GPIO_PORT, &GPIO_InitStruct);
00952 
00953   /* DeInit interrupt pin : disable IRQ before to avoid spurious interrupt */
00954   HAL_NVIC_DisableIRQ((IRQn_Type)(MFX_INT_EXTI_IRQn));
00955   MFX_INT_GPIO_CLK_ENABLE();
00956   HAL_GPIO_DeInit(MFX_INT_GPIO_PORT, MFX_INT_PIN);
00957 
00958   /* I2C1 Deinit */
00959   I2C1_DeInit(BSP_I2C_MFX_USER);
00960 }
00961 
00962 /**
00963   * @brief  Configures MFX low level interrupt.
00964   * @retval None
00965   */
00966 void MFX_IO_ITConfig(void)
00967 {
00968   GPIO_InitTypeDef  GPIO_InitStruct;
00969 
00970   /* Enable the GPIO clock */
00971   MFX_INT_GPIO_CLK_ENABLE();
00972 
00973   /* MFX_OUT_IRQ (normally used for EXTI_WKUP) */
00974   GPIO_InitStruct.Pin   = MFX_INT_PIN;
00975   GPIO_InitStruct.Pull  = GPIO_NOPULL;
00976   GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW;
00977   GPIO_InitStruct.Mode  = GPIO_MODE_IT_RISING;
00978   HAL_GPIO_Init(MFX_INT_GPIO_PORT, &GPIO_InitStruct);
00979 
00980   /* Enable and set GPIO EXTI Interrupt to the lowest priority */
00981   HAL_NVIC_SetPriority((IRQn_Type)(MFX_INT_EXTI_IRQn), 0x0F, 0x0F);
00982   HAL_NVIC_EnableIRQ((IRQn_Type)(MFX_INT_EXTI_IRQn));
00983 }
00984 
00985 /**
00986   * @brief  Configures MFX wke up  pin.
00987   * @retval None
00988   */
00989 void MFX_IO_EnableWakeupPin(void)
00990 {
00991   GPIO_InitTypeDef  GPIO_InitStruct;
00992 
00993   /* Enable wakeup gpio clock */
00994   MFX_WAKEUP_GPIO_CLK_ENABLE();
00995 
00996   /* MFX wakeup pin configuration */
00997   GPIO_InitStruct.Pin   = MFX_WAKEUP_PIN;
00998   GPIO_InitStruct.Mode  = GPIO_MODE_OUTPUT_PP;
00999   GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_HIGH;
01000   GPIO_InitStruct.Pull  = GPIO_NOPULL;
01001   HAL_GPIO_Init(MFX_WAKEUP_GPIO_PORT, &GPIO_InitStruct);
01002 }
01003 
01004 /**
01005   * @brief  Wakeup MFX.
01006   * @retval None
01007   */
01008 void MFX_IO_Wakeup(void)
01009 {
01010   /* Set Wakeup pin to high to wakeup Idd measurement component from standby mode */
01011   HAL_GPIO_WritePin(MFX_WAKEUP_GPIO_PORT, MFX_WAKEUP_PIN, GPIO_PIN_SET);
01012 
01013   /* Wait */
01014   HAL_Delay(1);
01015 
01016   /* Set gpio pin basck to low */
01017   HAL_GPIO_WritePin(MFX_WAKEUP_GPIO_PORT, MFX_WAKEUP_PIN, GPIO_PIN_RESET);
01018 }
01019 
01020 /**
01021   * @brief  MFX writes single data.
01022   * @param  Addr: I2C address
01023   * @param  Reg: Register address
01024   * @param  Value: Data to be written
01025   * @retval None
01026   */
01027 void MFX_IO_Write(uint16_t Addr, uint8_t Reg, uint8_t Value)
01028 {
01029   I2C1_WriteData(Addr, Reg, I2C_MEMADD_SIZE_8BIT, Value);
01030 }
01031 
01032 /**
01033   * @brief  MFX reads single data.
01034   * @param  Addr: I2C address
01035   * @param  Reg: Register address
01036   * @retval Read data
01037   */
01038 uint8_t MFX_IO_Read(uint16_t Addr, uint8_t Reg)
01039 {
01040   return I2C1_ReadData(Addr, Reg, I2C_MEMADD_SIZE_8BIT);
01041 }
01042 
01043 /**
01044   * @brief  MFX reads multiple data.
01045   * @param  Addr: I2C address
01046   * @param  Reg: Register address
01047   * @param  Buffer: Pointer to data buffer
01048   * @param  Length: Length of the data
01049   * @retval Number of read data
01050   */
01051 uint16_t MFX_IO_ReadMultiple(uint16_t Addr, uint8_t Reg, uint8_t *Buffer, uint16_t Length)
01052 {
01053  return I2C1_ReadBuffer(Addr, (uint16_t)Reg, I2C_MEMADD_SIZE_8BIT, Buffer, Length);
01054 }
01055 
01056 /**
01057   * @brief  MFX writes multiple data.
01058   * @param  Addr: I2C address
01059   * @param  Reg: Register address
01060   * @param  Buffer: Pointer to data buffer
01061   * @param  Length: Length of the data
01062   * @retval None
01063   */
01064 void MFX_IO_WriteMultiple(uint16_t Addr, uint8_t Reg, uint8_t *Buffer, uint16_t Length)
01065 {
01066   I2C1_WriteBuffer(Addr, (uint16_t)Reg, I2C_MEMADD_SIZE_8BIT, Buffer, Length);
01067 }
01068 
01069 /**
01070   * @brief  MFX delay
01071   * @param  Delay: Delay in ms
01072   * @retval None
01073   */
01074 void MFX_IO_Delay(uint32_t Delay)
01075 {
01076   HAL_Delay(Delay);
01077 }
01078 
01079 
01080 /********************************* LINK AUDIO *********************************/
01081 /**
01082   * @brief  Initializes Audio low level.
01083   * @retval None
01084   */
01085 void AUDIO_IO_Init(void)
01086 {
01087   uint8_t Value;
01088 
01089   /* Initialized BSP IO */
01090   BSP_IO_Init();
01091 
01092   /* Audio reset pin configuration */
01093   BSP_IO_ConfigPin(AUDIO_RESET_PIN, IO_MODE_OUTPUT_PP_PD);
01094 
01095   /* I2C bus init */
01096   I2C1_Init(BSP_I2C_AUDIO_USER);
01097 
01098   /* Power off the codec */
01099   CODEC_AUDIO_POWER_OFF();
01100 
01101   /* wait until power supplies are stable */
01102   HAL_Delay(10);
01103 
01104   /* Power on the codec */
01105   CODEC_AUDIO_POWER_ON();
01106 
01107   /* Wait at least 500ns after reset */
01108   HAL_Delay(1);
01109 
01110   /* Set the device in standby mode */
01111   Value = AUDIO_IO_Read(AUDIO_I2C_ADDRESS, 0x02);
01112   AUDIO_IO_Write(AUDIO_I2C_ADDRESS, 0x02, (Value | 0x01));
01113 
01114   /* Set all power down bits to 1 */
01115   AUDIO_IO_Write(AUDIO_I2C_ADDRESS, 0x02, 0x7F);
01116   Value = AUDIO_IO_Read(AUDIO_I2C_ADDRESS, 0x03);
01117   AUDIO_IO_Write(AUDIO_I2C_ADDRESS, 0x03, (Value | 0x0E));
01118 }
01119 
01120 /**
01121   * @brief  Deinitializes Audio low level.
01122   * @retval None
01123   */
01124 void AUDIO_IO_DeInit(void)
01125 {
01126   uint8_t Value;
01127 
01128   /* Mute DAC and ADC */
01129   Value = AUDIO_IO_Read(AUDIO_I2C_ADDRESS, 0x08);
01130   AUDIO_IO_Write(AUDIO_I2C_ADDRESS, 0x08, (Value | 0x03));
01131   Value = AUDIO_IO_Read(AUDIO_I2C_ADDRESS, 0x07);
01132   AUDIO_IO_Write(AUDIO_I2C_ADDRESS, 0x07, (Value | 0x03));
01133 
01134   /* Disable soft ramp and zero cross */
01135   Value = AUDIO_IO_Read(AUDIO_I2C_ADDRESS, 0x06);
01136   AUDIO_IO_Write(AUDIO_I2C_ADDRESS, 0x06, (Value & 0xF0));
01137 
01138   /* Set PDN to 1 */
01139   Value = AUDIO_IO_Read(AUDIO_I2C_ADDRESS, 0x02);
01140   AUDIO_IO_Write(AUDIO_I2C_ADDRESS, 0x02, (Value | 0x01));
01141 
01142   /* Set all power down bits to 1 */
01143   AUDIO_IO_Write(AUDIO_I2C_ADDRESS, 0x02, 0x7F);
01144   Value = AUDIO_IO_Read(AUDIO_I2C_ADDRESS, 0x03);
01145   AUDIO_IO_Write(AUDIO_I2C_ADDRESS, 0x03, (Value | 0x0E));
01146 
01147   /* Power off the codec */
01148   CODEC_AUDIO_POWER_OFF();
01149 
01150   /* I2C bus Deinit */
01151   I2C1_DeInit(BSP_I2C_AUDIO_USER);
01152 }
01153 
01154 /**
01155   * @brief  Writes a single data.
01156   * @param  Addr: I2C address
01157   * @param  Reg: Reg address
01158   * @param  Value: Data to be written
01159   * @retval None
01160   */
01161 void AUDIO_IO_Write(uint8_t Addr, uint8_t Reg, uint8_t Value)
01162 {
01163   I2C1_WriteBuffer(Addr, (uint16_t)Reg, I2C_MEMADD_SIZE_8BIT, &Value, 1);
01164 }
01165 
01166 /**
01167   * @brief  Reads a single data.
01168   * @param  Addr: I2C address
01169   * @param  Reg: Reg address
01170   * @retval Data to be read
01171   */
01172 uint8_t AUDIO_IO_Read(uint8_t Addr, uint8_t Reg)
01173 {
01174   uint8_t Read_Value = 0;
01175 
01176   I2C1_ReadBuffer((uint16_t) Addr, (uint16_t) Reg, I2C_MEMADD_SIZE_8BIT, &Read_Value, 1);
01177 
01178   return Read_Value;
01179 }
01180 
01181 /**
01182   * @brief  AUDIO Codec delay
01183   * @param  Delay: Delay in ms
01184   * @retval None
01185   */
01186 void AUDIO_IO_Delay(uint32_t Delay)
01187 {
01188   HAL_Delay(Delay);
01189 }
01190 
01191 /*******************************************************************************
01192                             LINK OPERATIONS
01193 *******************************************************************************/
01194 
01195 /************************** LINK TS (TouchScreen) *****************************/
01196 /**
01197   * @brief  Initializes Touchscreen low level.
01198   * @retval None
01199   */
01200 void TS_IO_Init(void)
01201 {
01202   I2C1_Init(BSP_I2C_TS_USER);
01203 }
01204 
01205 /**
01206   * @brief  Writes a single data.
01207   * @param  Addr: I2C address
01208   * @param  Reg: Reg address
01209   * @param  Value: Data to be written
01210   * @retval None
01211   */
01212 void TS_IO_Write(uint8_t Addr, uint8_t Reg, uint8_t Value)
01213 {
01214   I2C1_WriteMultiple(Addr, (uint16_t)Reg, I2C_MEMADD_SIZE_8BIT,(uint8_t*)&Value, 1);
01215 }
01216 
01217 /**
01218   * @brief  Reads a single data.
01219   * @param  Addr: I2C address
01220   * @param  Reg: Reg address
01221   * @retval Data to be read
01222   */
01223 uint8_t TS_IO_Read(uint8_t Addr, uint8_t Reg)
01224 {
01225   return I2C1_ReadData(Addr, Reg, I2C_MEMADD_SIZE_8BIT);
01226 }
01227 
01228 /**
01229   * @brief  Reads multiple data with I2C communication
01230   *         channel from TouchScreen.
01231   * @param  Addr: I2C address
01232   * @param  Reg: Register address
01233   * @param  Buffer: Pointer to data buffer
01234   * @param  Length: Length of the data
01235   * @retval Number of read data
01236   */
01237 uint16_t TS_IO_ReadMultiple(uint8_t Addr, uint8_t Reg, uint8_t *Buffer, uint16_t Length)
01238 {
01239  return I2C1_ReadMultiple(Addr, (uint16_t)Reg, I2C_MEMADD_SIZE_8BIT, Buffer, Length);
01240 }
01241 
01242 /**
01243   * @brief  Writes multiple data with I2C communication
01244   *         channel from MCU to TouchScreen.
01245   * @param  Addr: I2C address
01246   * @param  Reg: Register address
01247   * @param  Buffer: Pointer to data buffer
01248   * @param  Length: Length of the data
01249   * @retval None
01250   */
01251 void TS_IO_WriteMultiple(uint8_t Addr, uint8_t Reg, uint8_t *Buffer, uint16_t Length)
01252 {
01253   I2C1_WriteMultiple(Addr, (uint16_t)Reg, I2C_MEMADD_SIZE_8BIT, Buffer, Length);
01254 }
01255 
01256 /**
01257   * @brief  Delay function used in TouchScreen low level driver.
01258   * @param  Delay: Delay in ms
01259   * @retval None
01260   */
01261 void TS_IO_Delay(uint32_t Delay)
01262 {
01263   HAL_Delay(Delay);
01264 }
01265 
01266 
01267 /************************** Camera  *****************************/
01268 /**
01269   * @brief  Initializes Camera low level.
01270   * @retval None
01271   */
01272 void CAMERA_IO_Init(void)
01273 {
01274   I2C1_Init(BSP_I2C_CAMERA_USER);
01275 }
01276 
01277 /**
01278   * @brief  Camera writes single data.
01279   * @param  Addr: I2C address
01280   * @param  Reg: Register address
01281   * @param  Value: Data to be written
01282   * @retval None
01283   */
01284 void CAMERA_IO_Write(uint8_t Addr, uint8_t Reg, uint8_t Value)
01285 {
01286   I2C1_WriteMultiple(Addr, (uint16_t)Reg, I2C_MEMADD_SIZE_8BIT,(uint8_t*)&Value, 1);
01287 }
01288 
01289 /**
01290   * @brief  Camera reads single data.
01291   * @param  Addr: I2C address
01292   * @param  Reg: Register address
01293   * @retval Read data
01294   */
01295 uint8_t CAMERA_IO_Read(uint8_t Addr, uint8_t Reg)
01296 {
01297   uint8_t read_value = 0;
01298 
01299   I2C1_ReadMultiple(Addr, Reg, I2C_MEMADD_SIZE_8BIT, (uint8_t*)&read_value, 1);
01300 
01301   return read_value;
01302 }
01303 
01304 /**
01305   * @brief  Camera delay
01306   * @param  Delay: Delay in ms
01307   * @retval None
01308   */
01309 void CAMERA_Delay(uint32_t Delay)
01310 {
01311   HAL_Delay(Delay);
01312 }
01313 
01314 #endif /* HAL_I2C_MODULE_ENABLED */
01315 
01316 /**
01317   * @}
01318   */
01319 
01320 /**
01321   * @}
01322   */
01323 
01324 /**
01325   * @}
01326   */
01327 
01328 /**
01329   * @}
01330   */
01331 
01332 /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/