Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Dependents: DISCO_L496AG-LCD-prova_1 DISCO_L496AG-LCD-prova_2 DISCO_L496AG-LCD-demo DISCO_L496AG-SRAM-demo
stm32l496g_discovery.c
00001 /** 00002 ****************************************************************************** 00003 * @file stm32l496g_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 STM32L496G-Discovery board (MB1261) 00007 ****************************************************************************** 00008 * @attention 00009 * 00010 * <h2><center>© 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 "stm32l496g_discovery.h" 00023 #include "stm32l496g_discovery_io.h" 00024 #include "stm32l496g_discovery_lcd.h" 00025 00026 /** @addtogroup BSP 00027 * @{ 00028 */ 00029 00030 /** @defgroup STM32L496G_DISCOVERY STM32L496G-DISCOVERY 00031 * @{ 00032 */ 00033 00034 /** @defgroup STM32L496G_DISCOVERY_Common STM32L496G-DISCOVERY Common 00035 * @{ 00036 */ 00037 00038 /** @defgroup STM32L496G_DISCOVERY_Private_TypesDefinitions Private Types Definitions 00039 * @brief This file provides firmware functions to manage Leds, push-buttons, 00040 * COM ports, SD card on SPI and temperature sensor (TS751) available on 00041 * STM32L496G-DISCOVERY discoveryuation board from STMicroelectronics. 00042 * @{ 00043 */ 00044 00045 /** 00046 * @} 00047 */ 00048 00049 /** @defgroup STM32L496G_DISCOVERY_Private_Defines Private Defines 00050 * @{ 00051 */ 00052 00053 /** 00054 * @brief STM32L496G DISCOVERY BSP Driver version number 00055 */ 00056 #define __STM32L496G_DISCOVERY_BSP_VERSION_MAIN (0x01) /*!< [31:24] main version */ 00057 #define __STM32L496G_DISCOVERY_BSP_VERSION_SUB1 (0x01) /*!< [23:16] sub1 version */ 00058 #define __STM32L496G_DISCOVERY_BSP_VERSION_SUB2 (0x02) /*!< [15:8] sub2 version */ 00059 #define __STM32L496G_DISCOVERY_BSP_VERSION_RC (0x00) /*!< [7:0] release candidate */ 00060 #define __STM32L496G_DISCOVERY_BSP_VERSION ((__STM32L496G_DISCOVERY_BSP_VERSION_MAIN << 24)\ 00061 |(__STM32L496G_DISCOVERY_BSP_VERSION_SUB1 << 16)\ 00062 |(__STM32L496G_DISCOVERY_BSP_VERSION_SUB2 << 8 )\ 00063 |(__STM32L496G_DISCOVERY_BSP_VERSION_RC)) 00064 /** 00065 * @} 00066 */ 00067 00068 00069 /** @defgroup STM32L496G_DISCOVERY_Private_Macros Private Macros 00070 * @{ 00071 */ 00072 00073 /** 00074 * @} 00075 */ 00076 00077 00078 /** @defgroup STM32L496G_DISCOVERY_Exported_Variables Exported Variables 00079 * @{ 00080 */ 00081 00082 /** 00083 * @brief LED variables 00084 */ 00085 const uint16_t LED_PIN[LEDn] = {LED1_PIN, 00086 LED2_PIN 00087 }; 00088 00089 /** 00090 * @brief JOYSTICK variables 00091 */ 00092 GPIO_TypeDef *JOY_PORT[JOYn] = {SEL_JOY_GPIO_PORT, 00093 LEFT_JOY_GPIO_PORT, 00094 RIGHT_JOY_GPIO_PORT, 00095 DOWN_JOY_GPIO_PORT, 00096 UP_JOY_GPIO_PORT 00097 }; 00098 00099 const uint16_t JOY_PIN[JOYn] = {SEL_JOY_PIN, 00100 LEFT_JOY_PIN, 00101 RIGHT_JOY_PIN, 00102 DOWN_JOY_PIN, 00103 UP_JOY_PIN 00104 }; 00105 00106 const uint8_t JOY_IRQn[JOYn] = {SEL_JOY_EXTI_IRQn, 00107 LEFT_JOY_EXTI_IRQn, 00108 RIGHT_JOY_EXTI_IRQn, 00109 DOWN_JOY_EXTI_IRQn, 00110 UP_JOY_EXTI_IRQn 00111 }; 00112 00113 00114 /** 00115 * @brief COM port variables 00116 */ 00117 #if defined(HAL_UART_MODULE_ENABLED) 00118 USART_TypeDef* COM_USART[COMn] = {DISCOVERY_COM1}; 00119 GPIO_TypeDef* COM_TX_PORT[COMn] = {DISCOVERY_COM1_TX_GPIO_PORT}; 00120 GPIO_TypeDef* COM_RX_PORT[COMn] = {DISCOVERY_COM1_RX_GPIO_PORT}; 00121 const uint16_t COM_TX_PIN[COMn] = {DISCOVERY_COM1_TX_PIN}; 00122 const uint16_t COM_RX_PIN[COMn] = {DISCOVERY_COM1_RX_PIN}; 00123 const uint16_t COM_TX_AF[COMn] = {DISCOVERY_COM1_TX_AF}; 00124 const uint16_t COM_RX_AF[COMn] = {DISCOVERY_COM1_RX_AF}; 00125 #endif /* HAL_UART_MODULE_ENABLED */ 00126 00127 00128 /** 00129 * @brief BUS variables 00130 */ 00131 #if defined(HAL_I2C_MODULE_ENABLED) 00132 uint32_t I2c1Timeout = DISCOVERY_I2C2_TIMEOUT_MAX; /*<! Value of Timeout when I2C1 communication fails */ 00133 uint32_t I2c2Timeout = DISCOVERY_I2C2_TIMEOUT_MAX; /*<! Value of Timeout when I2C2 communication fails */ 00134 uint32_t ts_io_init = 0; 00135 static I2C_HandleTypeDef I2c2Handle = {0}; 00136 static I2C_HandleTypeDef hI2cTSHandler = {0}; /* Touch Screen (Capacitive Touch Panel) Handler */ 00137 static I2C_HandleTypeDef hI2cAudioHandler = {0}; 00138 static I2C_HandleTypeDef hI2cCameraHandler = {0} ; 00139 #endif /* HAL_I2C_MODULE_ENABLED */ 00140 00141 /** 00142 * @} 00143 */ 00144 00145 /** @defgroup STM32L496G_DISCOVERY_Private_FunctionPrototypes Private Functions 00146 * @{ 00147 */ 00148 /**************************** Bus functions ************************************/ 00149 /* I2C2 bus function */ 00150 #if defined(HAL_I2C_MODULE_ENABLED) 00151 static void I2C2_MspInit(I2C_HandleTypeDef *hi2c); 00152 static void I2C2_MspDeInit(I2C_HandleTypeDef *hi2c); 00153 static void I2C2_WriteData(uint16_t Addr, uint16_t Reg, uint16_t RegSize, uint8_t Value); 00154 static HAL_StatusTypeDef I2C2_WriteBuffer(uint16_t Addr, uint16_t Reg, uint16_t RegSize, uint8_t *pBuffer, uint16_t Length); 00155 static uint8_t I2C2_ReadData(uint16_t Addr, uint16_t Reg, uint16_t RegSize); 00156 static HAL_StatusTypeDef I2C2_ReadBuffer(uint16_t Addr, uint16_t Reg, uint16_t RegSize, uint8_t *pBuffer, uint16_t Length); 00157 static void I2C2_Error(void); 00158 00159 #endif/* HAL_I2C_MODULE_ENABLED */ 00160 00161 00162 /**************************** Link functions ***********************************/ 00163 #if defined(HAL_I2C_MODULE_ENABLED) 00164 00165 static void FMC_BANK1_WriteData(uint16_t Data); 00166 static void FMC_BANK1_WriteReg(uint8_t Reg); 00167 static uint16_t FMC_BANK1_ReadData(void); 00168 static void FMC_BANK1_Init(void); 00169 00170 00171 /* LCD IO functions */ 00172 void LCD_IO_Init(void); 00173 void LCD_IO_WriteData(uint16_t RegValue); 00174 void LCD_IO_WriteReg(uint8_t Reg); 00175 void LCD_IO_WriteMultipleData(uint16_t *pData, uint32_t Size); 00176 uint16_t LCD_IO_ReadData(void); 00177 void LCD_IO_Delay(uint32_t Delay); 00178 00179 /* Link functions for Audio Codec peripheral */ 00180 void AUDIO_IO_Init(void); 00181 void AUDIO_IO_DeInit(void); 00182 void AUDIO_IO_Write(uint8_t Addr, uint8_t Reg, uint8_t Value); 00183 uint8_t AUDIO_IO_Read(uint8_t Addr, uint8_t Reg); 00184 void AUDIO_IO_Delay(uint32_t delay); 00185 00186 /* TouchScreen (TS) IO functions */ 00187 void TS_IO_Init(void); 00188 void TS_IO_Write(uint8_t Addr, uint8_t Reg, uint8_t Value); 00189 uint8_t TS_IO_Read(uint8_t Addr, uint8_t Reg); 00190 uint16_t TS_IO_ReadMultiple(uint8_t Addr, uint8_t Reg, uint8_t *Buffer, uint16_t Length); 00191 void TS_IO_WriteMultiple(uint8_t Addr, uint8_t Reg, uint8_t *Buffer, uint16_t Length); 00192 void TS_IO_Delay(uint32_t Delay); 00193 00194 /* CAMERA IO functions */ 00195 void CAMERA_IO_Init(void); 00196 void CAMERA_Delay(uint32_t Delay); 00197 void CAMERA_IO_Write(uint8_t Addr, uint8_t Reg, uint8_t Value); 00198 uint8_t CAMERA_IO_Read(uint8_t Addr, uint8_t Reg); 00199 00200 #endif/* HAL_I2C_MODULE_ENABLED */ 00201 00202 00203 #if defined(HAL_I2C_MODULE_ENABLED) 00204 /* Link functions IOExpander */ 00205 void IOE_Init(void); 00206 void IOE_ITConfig(void); 00207 void IOE_Delay(uint32_t Delay); 00208 void IOE_Write(uint8_t Addr, uint8_t Reg, uint8_t Value); 00209 uint8_t IOE_Read(uint8_t Addr, uint8_t Reg); 00210 uint16_t IOE_ReadMultiple(uint8_t Addr, uint8_t Reg, uint8_t *Buffer, uint16_t Length); 00211 00212 /* Link functions for IDD measurment */ 00213 void MFX_IO_Init(void); 00214 void MFX_IO_DeInit(void); 00215 void MFX_IO_ITConfig(void); 00216 void MFX_IO_EnableWakeupPin(void); 00217 void MFX_IO_Wakeup(void); 00218 void MFX_IO_Delay(uint32_t delay); 00219 void MFX_IO_Write(uint16_t addr, uint8_t reg, uint8_t value); 00220 uint8_t MFX_IO_Read(uint16_t addr, uint8_t reg); 00221 void MFX_IO_WriteMultiple(uint16_t Addr, uint8_t Reg, uint8_t *Buffer, uint16_t Length); 00222 uint16_t MFX_IO_ReadMultiple(uint16_t addr, uint8_t reg, uint8_t *buffer, uint16_t length); 00223 00224 static void I2Cx_Init(I2C_HandleTypeDef *i2c_handler); 00225 00226 static HAL_StatusTypeDef I2Cx_ReadMultiple(I2C_HandleTypeDef *i2c_handler, uint8_t Addr, uint16_t Reg, uint16_t MemAddSize, uint8_t *Buffer, uint16_t Length); 00227 static HAL_StatusTypeDef I2Cx_WriteMultiple(I2C_HandleTypeDef *i2c_handler, uint8_t Addr, uint16_t Reg, uint16_t MemAddSize, uint8_t *Buffer, uint16_t Length); 00228 static void I2Cx_Error(I2C_HandleTypeDef *i2c_handler, uint8_t Addr); 00229 00230 static uint8_t I2C2_isDeviceReady(uint16_t Addr, uint32_t trial); 00231 00232 #endif/* HAL_I2C_MODULE_ENABLED */ 00233 /** 00234 * @} 00235 */ 00236 00237 __weak void BSP_ErrorHandler(void) 00238 { 00239 while (1); 00240 } 00241 00242 /** @defgroup STM32L496G_DISCOVERY_Exported_Functions Exported Functions 00243 * @{ 00244 */ 00245 00246 /** 00247 * @brief This method returns the STM32L496 DISCOVERY BSP Driver revision 00248 * @retval version : 0xXYZR (8bits for each decimal, R for RC) 00249 */ 00250 uint32_t BSP_GetVersion(void) 00251 { 00252 return __STM32L496G_DISCOVERY_BSP_VERSION; 00253 } 00254 00255 00256 00257 /** 00258 * @brief Configures LED GPIOs. 00259 * @param Led: Specifies the Led to be configured. 00260 * This parameter can be one of following parameters: 00261 * @arg DISCO_LED1 00262 * @arg DISCO_LED2 00263 * @retval None 00264 */ 00265 void BSP_LED_Init(Led_TypeDef Led) 00266 { 00267 GPIO_InitTypeDef GPIO_InitStructure; 00268 00269 if (Led == DISCO_LED2) 00270 { 00271 /* Enable the GPIO_LED clock */ 00272 LED2_GPIO_CLK_ENABLE(); 00273 00274 /* Configure the GPIO_LED pin */ 00275 GPIO_InitStructure.Pin = LED_PIN[Led]; 00276 GPIO_InitStructure.Mode = GPIO_MODE_OUTPUT_PP; 00277 GPIO_InitStructure.Pull = GPIO_NOPULL; 00278 GPIO_InitStructure.Speed = GPIO_SPEED_FREQ_VERY_HIGH; 00279 00280 HAL_GPIO_Init(LED2_GPIO_PORT, &GPIO_InitStructure); 00281 /* By default, turn off LED */ 00282 HAL_GPIO_WritePin(LED2_GPIO_PORT, GPIO_InitStructure.Pin, GPIO_PIN_SET); 00283 } 00284 else 00285 { 00286 /* Initialize the IO functionalities */ 00287 if (BSP_IO_Init() == IO_ERROR) 00288 { 00289 BSP_ErrorHandler(); 00290 } 00291 00292 BSP_IO_ConfigPin(LED_PIN[Led], IO_MODE_OUTPUT); 00293 00294 /* By default, turn off LED */ 00295 BSP_IO_WritePin(LED_PIN[Led], GPIO_PIN_SET); 00296 } 00297 00298 } 00299 00300 00301 /** 00302 * @brief Unconfigures LED GPIOs. 00303 * @param Led: Specifies the Led to be unconfigured. 00304 * This parameter can be one of following parameters: 00305 * @arg DISCO_LED1 00306 * @arg DISCO_LED2 00307 * @retval None 00308 */ 00309 void BSP_LED_DeInit(Led_TypeDef Led) 00310 { 00311 if (Led == DISCO_LED2) 00312 { 00313 /* Enable the GPIO_LED clock */ 00314 LED2_GPIO_CLK_ENABLE(); 00315 00316 HAL_GPIO_DeInit(LED2_GPIO_PORT, LED_PIN[Led]); 00317 } 00318 } 00319 00320 00321 /** 00322 * @brief Turns selected LED On. 00323 * @param Led: Specifies the Led to be set on. 00324 * This parameter can be one of following parameters: 00325 * @arg DISCO_LED1 00326 * @arg DISCO_LED2 00327 * @retval None 00328 */ 00329 void BSP_LED_On(Led_TypeDef Led) 00330 { 00331 if (Led == DISCO_LED2) 00332 { 00333 HAL_GPIO_WritePin(LED2_GPIO_PORT, LED_PIN[Led], GPIO_PIN_RESET); 00334 } 00335 else 00336 { 00337 BSP_IO_WritePin(LED_PIN[Led], GPIO_PIN_RESET); 00338 } 00339 } 00340 00341 00342 /** 00343 * @brief Turns selected LED Off. 00344 * @param Led: Specifies the Led to be set off. 00345 * This parameter can be one of following parameters: 00346 * @arg DISCO_LED1 00347 * @arg DISCO_LED2 00348 * @retval None 00349 */ 00350 void BSP_LED_Off(Led_TypeDef Led) 00351 { 00352 if (Led == DISCO_LED2) 00353 { 00354 HAL_GPIO_WritePin(LED2_GPIO_PORT, LED_PIN[Led], GPIO_PIN_SET); 00355 } 00356 else 00357 { 00358 BSP_IO_WritePin(LED_PIN[Led], GPIO_PIN_SET); 00359 } 00360 } 00361 00362 00363 /** 00364 * @brief Toggles the selected LED. 00365 * @param Led: Specifies the Led to be toggled. 00366 * This parameter can be one of following parameters: 00367 * @arg DISCO_LED1 00368 * @arg DISCO_LED2 00369 * @retval None 00370 */ 00371 void BSP_LED_Toggle(Led_TypeDef Led) 00372 { 00373 if (Led == DISCO_LED2) 00374 { 00375 HAL_GPIO_TogglePin(LED2_GPIO_PORT, LED_PIN[Led]); 00376 } 00377 else 00378 { 00379 BSP_IO_TogglePin(LED_PIN[Led]); 00380 } 00381 } 00382 00383 /** 00384 * @brief Configures all buttons of the joystick in GPIO or EXTI modes. 00385 * @param Joy_Mode: Joystick mode. 00386 * This parameter can be one of the following values: 00387 * @arg JOY_MODE_GPIO: Joystick pins will be used as simple IOs 00388 * @arg JOY_MODE_EXTI: Joystick pins will be connected to EXTI line 00389 * with interrupt generation capability 00390 * @retval HAL_OK: if all initializations are OK. Other value if error. 00391 */ 00392 uint8_t BSP_JOY_Init(JOYMode_TypeDef Joy_Mode) 00393 { 00394 JOYState_TypeDef joykey; 00395 GPIO_InitTypeDef GPIO_InitStruct; 00396 00397 /* Initialized the Joystick. */ 00398 for (joykey = JOY_SEL; joykey < (JOY_SEL + JOYn) ; joykey++) 00399 { 00400 /* Enable the JOY clock */ 00401 JOYx_GPIO_CLK_ENABLE(joykey); 00402 00403 GPIO_InitStruct.Pin = JOY_PIN[joykey]; 00404 GPIO_InitStruct.Pull = GPIO_PULLDOWN; 00405 GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW; 00406 00407 if (Joy_Mode == JOY_MODE_GPIO) 00408 { 00409 /* Configure Joy pin as input */ 00410 GPIO_InitStruct.Mode = GPIO_MODE_INPUT; 00411 HAL_GPIO_Init(JOY_PORT[joykey], &GPIO_InitStruct); 00412 } 00413 else if (Joy_Mode == JOY_MODE_EXTI) 00414 { 00415 /* Configure Joy pin as input with External interrupt */ 00416 GPIO_InitStruct.Mode = GPIO_MODE_IT_FALLING; 00417 HAL_GPIO_Init(JOY_PORT[joykey], &GPIO_InitStruct); 00418 /* Enable and set Joy EXTI Interrupt to the lowest priority */ 00419 HAL_NVIC_SetPriority((IRQn_Type)(JOY_IRQn[joykey]), 0x0F, 0x00); 00420 HAL_NVIC_EnableIRQ((IRQn_Type)(JOY_IRQn[joykey])); 00421 } 00422 } 00423 00424 return HAL_OK; 00425 } 00426 00427 /** 00428 * @brief Unconfigures all GPIOs used as buttons of the joystick. 00429 * @retval None. 00430 */ 00431 void BSP_JOY_DeInit(void) 00432 { 00433 JOYState_TypeDef joykey; 00434 00435 /* Initialized the Joystick. */ 00436 for (joykey = JOY_SEL; joykey < (JOY_SEL + JOYn) ; joykey++) 00437 { 00438 /* Enable the JOY clock */ 00439 JOYx_GPIO_CLK_ENABLE(joykey); 00440 00441 HAL_GPIO_DeInit(JOY_PORT[joykey], JOY_PIN[joykey]); 00442 } 00443 } 00444 00445 /** 00446 * @brief Returns the current joystick status. 00447 * @retval Code of the joystick key pressed 00448 * This code can be one of the following values: 00449 * @arg JOY_NONE 00450 * @arg JOY_SEL 00451 * @arg JOY_DOWN 00452 * @arg JOY_LEFT 00453 * @arg JOY_RIGHT 00454 * @arg JOY_UP 00455 */ 00456 JOYState_TypeDef BSP_JOY_GetState(void) 00457 { 00458 JOYState_TypeDef joykey; 00459 00460 for (joykey = JOY_SEL; joykey < (JOY_SEL + JOYn) ; joykey++) 00461 { 00462 if (HAL_GPIO_ReadPin(JOY_PORT[joykey], JOY_PIN[joykey]) == GPIO_PIN_SET) 00463 { 00464 /* Return Code Joystick key pressed */ 00465 return joykey; 00466 } 00467 } 00468 00469 /* No Joystick key pressed */ 00470 return JOY_NONE; 00471 } 00472 00473 #if defined(HAL_UART_MODULE_ENABLED) 00474 /** 00475 * @brief Configures COM port. 00476 * @param COM: COM port to be configured. 00477 * This parameter can be one of the following values: 00478 * @arg COM1 00479 * @param huart: Pointer to a UART_HandleTypeDef structure that contains the 00480 * configuration information for the specified USART peripheral. 00481 */ 00482 void BSP_COM_Init(COM_TypeDef COM, UART_HandleTypeDef *huart) 00483 { 00484 GPIO_InitTypeDef gpio_init_structure; 00485 00486 /* Enable GPIO clock */ 00487 DISCOVERY_COMx_TX_GPIO_CLK_ENABLE(COM); 00488 DISCOVERY_COMx_RX_GPIO_CLK_ENABLE(COM); 00489 00490 /* Enable USART clock */ 00491 DISCOVERY_COMx_CLK_ENABLE(COM); 00492 00493 /* Configure USART Tx as alternate function */ 00494 gpio_init_structure.Pin = COM_TX_PIN[COM]; 00495 gpio_init_structure.Mode = GPIO_MODE_AF_PP; 00496 gpio_init_structure.Speed = GPIO_SPEED_FREQ_HIGH; 00497 gpio_init_structure.Pull = GPIO_NOPULL; 00498 gpio_init_structure.Alternate = COM_TX_AF[COM]; 00499 HAL_GPIO_Init(COM_TX_PORT[COM], &gpio_init_structure); 00500 00501 /* Configure USART Rx as alternate function */ 00502 gpio_init_structure.Pin = COM_RX_PIN[COM]; 00503 gpio_init_structure.Mode = GPIO_MODE_AF_PP; 00504 gpio_init_structure.Alternate = COM_RX_AF[COM]; 00505 HAL_GPIO_Init(COM_RX_PORT[COM], &gpio_init_structure); 00506 00507 /* USART configuration */ 00508 huart->Instance = COM_USART[COM]; 00509 HAL_UART_Init(huart); 00510 } 00511 00512 /** 00513 * @brief DeInit COM port. 00514 * @param COM: COM port to be configured. 00515 * This parameter can be one of the following values: 00516 * @arg COM1 00517 * @param huart: Pointer to a UART_HandleTypeDef structure that contains the 00518 * configuration information for the specified USART peripheral. 00519 */ 00520 void BSP_COM_DeInit(COM_TypeDef COM, UART_HandleTypeDef *huart) 00521 { 00522 /* USART deinitialization */ 00523 huart->Instance = COM_USART[COM]; 00524 HAL_UART_DeInit(huart); 00525 00526 /* Disable USART clock */ 00527 DISCOVERY_COMx_CLK_DISABLE(COM); 00528 00529 /* USART TX/RX pins deinitializations */ 00530 HAL_GPIO_DeInit(COM_TX_PORT[COM], COM_TX_PIN[COM]); 00531 HAL_GPIO_DeInit(COM_RX_PORT[COM], COM_RX_PIN[COM]); 00532 00533 /* Disable GPIOs clock is left for application */ 00534 } 00535 #endif /* HAL_UART_MODULE_ENABLED */ 00536 00537 /** 00538 * @} 00539 */ 00540 00541 /** @defgroup STM32L496G_DISCOVERY_BusOperations_Functions Bus Operations Functions 00542 * @{ 00543 */ 00544 00545 /******************************************************************************* 00546 BUS OPERATIONS 00547 *******************************************************************************/ 00548 00549 #if defined(HAL_I2C_MODULE_ENABLED) 00550 /******************************* I2C Routines**********************************/ 00551 /** 00552 * @brief Discovery I2C2 Bus initialization 00553 * @retval None 00554 */ 00555 void I2C2_Init(void) 00556 { 00557 if (HAL_I2C_GetState(&I2c2Handle) == HAL_I2C_STATE_RESET) 00558 { 00559 I2c2Handle.Instance = DISCOVERY_I2C2; 00560 I2c2Handle.Init.Timing = DISCOVERY_I2C2_TIMING; 00561 I2c2Handle.Init.OwnAddress1 = 0x70; 00562 I2c2Handle.Init.AddressingMode = I2C_ADDRESSINGMODE_7BIT; 00563 I2c2Handle.Init.DualAddressMode = I2C_DUALADDRESS_DISABLE; 00564 I2c2Handle.Init.OwnAddress2 = 0xFF; 00565 I2c2Handle.Init.GeneralCallMode = I2C_GENERALCALL_DISABLE; 00566 I2c2Handle.Init.NoStretchMode = I2C_NOSTRETCH_DISABLE; 00567 00568 /* Init the I2C */ 00569 I2C2_MspInit(&I2c2Handle); 00570 HAL_I2C_Init(&I2c2Handle); 00571 } 00572 } 00573 00574 /** 00575 * @brief Discovery I2C2 MSP Initialization 00576 * @param hi2c: I2C2 handle 00577 * @retval None 00578 */ 00579 static void I2C2_MspInit(I2C_HandleTypeDef *hi2c) 00580 { 00581 GPIO_InitTypeDef GPIO_InitStructure; 00582 RCC_PeriphCLKInitTypeDef RCC_PeriphCLKInitStruct; 00583 00584 if (hi2c->Instance == DISCOVERY_I2C2) 00585 { 00586 /*##-1- Configure the Discovery I2C2 clock source. The clock is derived from the SYSCLK #*/ 00587 RCC_PeriphCLKInitStruct.PeriphClockSelection = RCC_PERIPHCLK_I2C2; 00588 RCC_PeriphCLKInitStruct.I2c2ClockSelection = RCC_I2C2CLKSOURCE_SYSCLK; 00589 HAL_RCCEx_PeriphCLKConfig(&RCC_PeriphCLKInitStruct); 00590 00591 /*##-2- Configure the GPIOs ################################################*/ 00592 /* Enable GPIO clock */ 00593 DISCOVERY_I2C2_SDA_GPIO_CLK_ENABLE(); 00594 DISCOVERY_I2C2_SCL_GPIO_CLK_ENABLE(); 00595 00596 /* Configure I2C Rx/Tx as alternate function */ 00597 GPIO_InitStructure.Pin = DISCOVERY_I2C2_SCL_PIN; 00598 GPIO_InitStructure.Mode = GPIO_MODE_AF_OD; 00599 GPIO_InitStructure.Pull = /*GPIO_NOPULL*/ GPIO_PULLUP; 00600 GPIO_InitStructure.Speed = /*GPIO_SPEED_MEDIUM*/ GPIO_SPEED_FREQ_VERY_HIGH; 00601 GPIO_InitStructure.Alternate = DISCOVERY_I2C2_SCL_SDA_AF; 00602 HAL_GPIO_Init(DISCOVERY_I2C2_SCL_GPIO_PORT, &GPIO_InitStructure); 00603 GPIO_InitStructure.Pin = DISCOVERY_I2C2_SDA_PIN; 00604 HAL_GPIO_Init(DISCOVERY_I2C2_SDA_GPIO_PORT, &GPIO_InitStructure); 00605 00606 /*##-3- Configure the Discovery I2C2 peripheral #############################*/ 00607 /* Enable Discovery_I2C2 clock */ 00608 DISCOVERY_I2C2_CLK_ENABLE(); 00609 00610 /* Force and release the I2C Peripheral Clock Reset */ 00611 DISCOVERY_I2C2_FORCE_RESET(); 00612 DISCOVERY_I2C2_RELEASE_RESET(); 00613 00614 /* Enable and set Discovery I2C2 Interrupt to the highest priority */ 00615 HAL_NVIC_SetPriority(DISCOVERY_I2C2_EV_IRQn, 0x00, 0); 00616 HAL_NVIC_EnableIRQ(DISCOVERY_I2C2_EV_IRQn); 00617 00618 /* Enable and set Discovery I2C2 Interrupt to the highest priority */ 00619 HAL_NVIC_SetPriority(DISCOVERY_I2C2_ER_IRQn, 0x00, 0); 00620 HAL_NVIC_EnableIRQ(DISCOVERY_I2C2_ER_IRQn); 00621 } 00622 } 00623 00624 /** 00625 * @brief Discovery I2C2 Bus Deinitialization 00626 * @retval None 00627 */ 00628 void I2C2_DeInit(void) 00629 { 00630 if (HAL_I2C_GetState(&I2c2Handle) != HAL_I2C_STATE_RESET) 00631 { 00632 /* DeInit the I2C */ 00633 HAL_I2C_DeInit(&I2c2Handle); 00634 I2C2_MspDeInit(&I2c2Handle); 00635 } 00636 } 00637 00638 /** 00639 * @brief Discovery I2C2 MSP DeInitialization 00640 * @param hi2c: I2C2 handle 00641 * @retval None 00642 */ 00643 static void I2C2_MspDeInit(I2C_HandleTypeDef *hi2c) 00644 { 00645 if (hi2c->Instance == DISCOVERY_I2C2) 00646 { 00647 /*##-1- Unconfigure the GPIOs ################################################*/ 00648 /* Enable GPIO clock */ 00649 DISCOVERY_I2C2_SDA_GPIO_CLK_ENABLE(); 00650 DISCOVERY_I2C2_SCL_GPIO_CLK_ENABLE(); 00651 00652 /* Configure I2C Rx/Tx as alternate function */ 00653 HAL_GPIO_DeInit(DISCOVERY_I2C2_SCL_GPIO_PORT, DISCOVERY_I2C2_SCL_PIN); 00654 HAL_GPIO_DeInit(DISCOVERY_I2C2_SDA_GPIO_PORT, DISCOVERY_I2C2_SDA_PIN); 00655 00656 /*##-2- Unconfigure the Discovery I2C2 peripheral ############################*/ 00657 /* Force and release I2C Peripheral */ 00658 DISCOVERY_I2C2_FORCE_RESET(); 00659 DISCOVERY_I2C2_RELEASE_RESET(); 00660 00661 /* Disable Discovery I2C2 clock */ 00662 DISCOVERY_I2C2_CLK_DISABLE(); 00663 00664 /* Disable Discovery I2C2 interrupts */ 00665 HAL_NVIC_DisableIRQ(DISCOVERY_I2C2_EV_IRQn); 00666 HAL_NVIC_DisableIRQ(DISCOVERY_I2C2_ER_IRQn); 00667 } 00668 } 00669 00670 /** 00671 * @brief Write a value in a register of the device through BUS. 00672 * @param Addr: Device address on BUS Bus. 00673 * @param Reg: The target register address to write 00674 * @param RegSize: The target register size (can be 8BIT or 16BIT) 00675 * @param Value: The target register value to be written 00676 * @retval None 00677 */ 00678 static void I2C2_WriteData(uint16_t Addr, uint16_t Reg, uint16_t RegSize, uint8_t Value) 00679 { 00680 HAL_StatusTypeDef status = HAL_OK; 00681 00682 __disable_irq(); 00683 00684 status = HAL_I2C_Mem_Write(&I2c2Handle, Addr, (uint16_t)Reg, RegSize, &Value, 1, I2c2Timeout); 00685 00686 __enable_irq(); 00687 00688 00689 /* Check the communication status */ 00690 if (status != HAL_OK) 00691 { 00692 /* Re-Initiaize the BUS */ 00693 I2C2_Error(); 00694 } 00695 } 00696 00697 /** 00698 * @brief Write a value in a register of the device through BUS. 00699 * @param Addr: Device address on BUS Bus. 00700 * @param Reg: The target register address to write 00701 * @param RegSize: The target register size (can be 8BIT or 16BIT) 00702 * @param pBuffer: The target register value to be written 00703 * @param Length: buffer size to be written 00704 * @retval None 00705 */ 00706 static HAL_StatusTypeDef I2C2_WriteBuffer(uint16_t Addr, uint16_t Reg, uint16_t RegSize, uint8_t *pBuffer, uint16_t Length) 00707 { 00708 HAL_StatusTypeDef status = HAL_OK; 00709 00710 __disable_irq(); 00711 00712 status = HAL_I2C_Mem_Write(&I2c2Handle, Addr, (uint16_t)Reg, RegSize, pBuffer, Length, I2c2Timeout); 00713 00714 __enable_irq(); 00715 00716 00717 /* Check the communication status */ 00718 if (status != HAL_OK) 00719 { 00720 /* Re-Initiaize the BUS */ 00721 I2C2_Error(); 00722 } 00723 00724 return status; 00725 } 00726 00727 /** 00728 * @brief Read a register of the device through BUS 00729 * @param Addr: Device address on BUS 00730 * @param Reg: The target register address to read 00731 * @param RegSize: The target register size (can be 8BIT or 16BIT) 00732 * @retval read register value 00733 */ 00734 static uint8_t I2C2_ReadData(uint16_t Addr, uint16_t Reg, uint16_t RegSize) 00735 { 00736 HAL_StatusTypeDef status = HAL_OK; 00737 uint8_t value = 0x0; 00738 00739 __disable_irq(); 00740 00741 status = HAL_I2C_Mem_Read(&I2c2Handle, Addr, Reg, RegSize, &value, 1, I2c2Timeout); 00742 00743 __enable_irq(); 00744 00745 /* Check the communication status */ 00746 if (status != HAL_OK) 00747 { 00748 /* Re-Initiaize the BUS */ 00749 I2C2_Error(); 00750 HAL_Delay(200); 00751 } 00752 00753 return value; 00754 } 00755 00756 static uint8_t I2C2_isDeviceReady(uint16_t Addr, uint32_t trial) 00757 { 00758 HAL_StatusTypeDef status = HAL_OK; 00759 uint8_t value = 0x0; 00760 00761 __disable_irq(); 00762 00763 status = HAL_I2C_IsDeviceReady(&I2c2Handle, Addr, trial, 50); 00764 00765 __enable_irq(); 00766 00767 /* Check the communication status */ 00768 if (status != HAL_OK) 00769 { 00770 /* Re-Initiaize the BUS */ 00771 I2C2_Error(); 00772 HAL_Delay(200); 00773 } 00774 00775 return value; 00776 } 00777 00778 /** 00779 * @brief Reads multiple data on the BUS. 00780 * @param Addr: I2C Address 00781 * @param Reg: Reg Address 00782 * @param RegSize : The target register size (can be 8BIT or 16BIT) 00783 * @param pBuffer: pointer to read data buffer 00784 * @param Length: length of the data 00785 * @retval 0 if no problems to read multiple data 00786 */ 00787 static HAL_StatusTypeDef I2C2_ReadBuffer(uint16_t Addr, uint16_t Reg, uint16_t RegSize, uint8_t *pBuffer, uint16_t Length) 00788 { 00789 HAL_StatusTypeDef status = HAL_OK; 00790 00791 __disable_irq(); 00792 00793 status = HAL_I2C_Mem_Read(&I2c2Handle, Addr, (uint16_t)Reg, RegSize, pBuffer, Length, I2c2Timeout); 00794 00795 __enable_irq(); 00796 00797 /* Check the communication status */ 00798 if (status != HAL_OK) 00799 { 00800 /* Re-Initiaize the BUS */ 00801 I2C2_Error(); 00802 } 00803 00804 return status; 00805 } 00806 00807 /** 00808 * @brief Discovery I2C2 error treatment function 00809 * @retval None 00810 */ 00811 static void I2C2_Error(void) 00812 { 00813 BSP_ErrorHandler(); 00814 00815 /* De-initialize the I2C communication BUS */ 00816 HAL_I2C_DeInit(&I2c2Handle); 00817 00818 /* Re- Initiaize the I2C communication BUS */ 00819 I2C2_Init(); 00820 } 00821 00822 00823 00824 /******************************* I2C Routines *********************************/ 00825 /** 00826 * @brief Initializes I2C HAL. 00827 * @param i2c_handler : I2C handler 00828 * @retval None 00829 */ 00830 static void I2Cx_Init(I2C_HandleTypeDef *i2c_handler) 00831 { 00832 00833 if (HAL_I2C_GetState(i2c_handler) == HAL_I2C_STATE_RESET) 00834 { 00835 if (i2c_handler == (I2C_HandleTypeDef *)(&hI2cTSHandler)) 00836 { 00837 /* TS (Capacitive Touch Panel) and LCD I2C configuration */ 00838 i2c_handler->Instance = DISCOVERY_TS_I2Cx; 00839 00840 /* Need to enable MFX, and in doing so, initialize I2C at the same time */ 00841 /* MFX_IO_Init();*/ 00842 00843 } 00844 else if (i2c_handler == (I2C_HandleTypeDef *)(&hI2cAudioHandler)) 00845 { 00846 /* Audio and LCD I2C configuration */ 00847 i2c_handler->Instance = DISCOVERY_AUDIO_I2Cx; 00848 } 00849 else if (i2c_handler == (I2C_HandleTypeDef *)(&hI2cCameraHandler)) 00850 { 00851 i2c_handler->Instance = DISCOVERY_CAMERA_I2Cx; 00852 } 00853 else 00854 { 00855 /* External, EEPROM and Arduino connector I2C configuration */ 00856 i2c_handler->Instance = DISCOVERY_EXT_I2Cx; 00857 } 00858 00859 i2c_handler->Init.Timing = DISCOVERY_I2C_TIMING; 00860 i2c_handler->Init.OwnAddress1 = 0x70; 00861 i2c_handler->Init.AddressingMode = I2C_ADDRESSINGMODE_7BIT; 00862 i2c_handler->Init.DualAddressMode = I2C_DUALADDRESS_DISABLE; 00863 i2c_handler->Init.OwnAddress2 = 0xFF; 00864 i2c_handler->Init.GeneralCallMode = I2C_GENERALCALL_DISABLE; 00865 i2c_handler->Init.NoStretchMode = I2C_NOSTRETCH_DISABLE; 00866 00867 00868 /* Init the I2C */ 00869 HAL_I2C_Init(i2c_handler); 00870 00871 } 00872 00873 } 00874 00875 /** 00876 * @brief Reads multiple data. 00877 * @param i2c_handler : I2C handler 00878 * @param Addr: I2C address 00879 * @param Reg: Reg address 00880 * @param MemAddress: Memory address 00881 * @param Buffer: Pointer to data buffer 00882 * @param Length: Length of the data 00883 * @retval Number of read data 00884 */ 00885 static HAL_StatusTypeDef I2Cx_ReadMultiple(I2C_HandleTypeDef *i2c_handler, 00886 uint8_t Addr, 00887 uint16_t Reg, 00888 uint16_t MemAddress, 00889 uint8_t *Buffer, 00890 uint16_t Length) 00891 { 00892 HAL_StatusTypeDef status = HAL_OK; 00893 00894 status = HAL_I2C_Mem_Read(i2c_handler, Addr, (uint16_t)Reg, MemAddress, Buffer, Length, 1000); 00895 00896 /* Check the communication status */ 00897 if (status != HAL_OK) 00898 { 00899 /* I2C error occurred */ 00900 I2Cx_Error(i2c_handler, Addr); 00901 } 00902 return status; 00903 } 00904 00905 /** 00906 * @brief Writes a value in a register of the device through BUS in using DMA mode. 00907 * @param i2c_handler : I2C handler 00908 * @param Addr: Device address on BUS Bus. 00909 * @param Reg: The target register address to write 00910 * @param MemAddress: Memory address 00911 * @param Buffer: The target register value to be written 00912 * @param Length: buffer size to be written 00913 * @retval HAL status 00914 */ 00915 static HAL_StatusTypeDef I2Cx_WriteMultiple(I2C_HandleTypeDef *i2c_handler, 00916 uint8_t Addr, 00917 uint16_t Reg, 00918 uint16_t MemAddress, 00919 uint8_t *Buffer, 00920 uint16_t Length) 00921 { 00922 HAL_StatusTypeDef status = HAL_OK; 00923 00924 status = HAL_I2C_Mem_Write(i2c_handler, Addr, (uint16_t)Reg, MemAddress, Buffer, Length, 1000); 00925 00926 /* Check the communication status */ 00927 if (status != HAL_OK) 00928 { 00929 /* Re-Initialize the I2C Bus */ 00930 I2Cx_Error(i2c_handler, Addr); 00931 } 00932 return status; 00933 } 00934 00935 /** 00936 * @brief Manages error callback by re-initializing I2C. 00937 * @param i2c_handler : I2C handler 00938 * @param Addr: I2C Address 00939 * @retval None 00940 */ 00941 static void I2Cx_Error(I2C_HandleTypeDef *i2c_handler, uint8_t Addr) 00942 { 00943 BSP_ErrorHandler(); 00944 00945 /* De-initialize the I2C communication bus */ 00946 if (i2c_handler == (I2C_HandleTypeDef *)(&hI2cTSHandler)) 00947 { 00948 I2C2_DeInit(); 00949 } 00950 else 00951 { 00952 HAL_I2C_DeInit(i2c_handler); 00953 } 00954 00955 /* Re-Initialize the I2C communication bus */ 00956 I2Cx_Init(i2c_handler); 00957 } 00958 #endif /*HAL_I2C_MODULE_ENABLED*/ 00959 00960 00961 /******************************************************************************* 00962 LINK OPERATIONS 00963 *******************************************************************************/ 00964 00965 #if defined(HAL_I2C_MODULE_ENABLED) 00966 /********************************* LINK MFX ***********************************/ 00967 /** 00968 * @brief Initializes MFX low level. 00969 * @retval None 00970 */ 00971 void MFX_IO_Init(void) 00972 { 00973 /* I2C2 init */ 00974 I2C2_Init(); 00975 00976 /* Wait for device ready */ 00977 if (I2C2_isDeviceReady(IO1_I2C_ADDRESS, 4) != HAL_OK) 00978 { 00979 BSP_ErrorHandler(); 00980 } 00981 } 00982 /** 00983 * @brief Deinitializes MFX low level. 00984 * @retval None 00985 */ 00986 void MFX_IO_DeInit(void) 00987 { 00988 GPIO_InitTypeDef GPIO_InitStruct; 00989 00990 /* Enable wakeup gpio clock */ 00991 MFX_WAKEUP_GPIO_CLK_ENABLE(); 00992 00993 /* MFX wakeup pin configuration */ 00994 GPIO_InitStruct.Pin = MFX_WAKEUP_PIN; 00995 GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP; 00996 GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW; 00997 GPIO_InitStruct.Pull = GPIO_PULLDOWN; 00998 HAL_GPIO_Init(MFX_WAKEUP_GPIO_PORT, &GPIO_InitStruct); 00999 01000 /* DeInit interrupt pin : disable IRQ before to avoid spurious interrupt */ 01001 HAL_NVIC_DisableIRQ((IRQn_Type)(MFX_INT_EXTI_IRQn)); 01002 MFX_INT_GPIO_CLK_ENABLE(); 01003 HAL_GPIO_DeInit(MFX_INT_GPIO_PORT, MFX_INT_PIN); 01004 01005 /* I2C2 Deinit */ 01006 I2C2_DeInit(); 01007 } 01008 01009 /** 01010 * @brief Configures MFX low level interrupt. 01011 * @retval None 01012 */ 01013 void MFX_IO_ITConfig(void) 01014 { 01015 GPIO_InitTypeDef GPIO_InitStruct; 01016 01017 /* Enable the GPIO clock */ 01018 MFX_INT_GPIO_CLK_ENABLE(); 01019 01020 /* MFX_OUT_IRQ (normally used for EXTI_WKUP) */ 01021 GPIO_InitStruct.Pin = MFX_INT_PIN; 01022 GPIO_InitStruct.Pull = GPIO_NOPULL; 01023 GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW; 01024 GPIO_InitStruct.Mode = GPIO_MODE_IT_RISING; 01025 HAL_GPIO_Init(MFX_INT_GPIO_PORT, &GPIO_InitStruct); 01026 01027 /* Enable and set GPIO EXTI Interrupt to the lowest priority */ 01028 HAL_NVIC_SetPriority((IRQn_Type)(MFX_INT_EXTI_IRQn), 0x0F, 0x0F); 01029 HAL_NVIC_EnableIRQ((IRQn_Type)(MFX_INT_EXTI_IRQn)); 01030 } 01031 01032 /** 01033 * @brief Configures MFX wke up pin. 01034 * @retval None 01035 */ 01036 void MFX_IO_EnableWakeupPin(void) 01037 { 01038 GPIO_InitTypeDef GPIO_InitStruct; 01039 01040 /* Enable wakeup gpio clock */ 01041 MFX_WAKEUP_GPIO_CLK_ENABLE(); 01042 01043 /* MFX wakeup pin configuration */ 01044 GPIO_InitStruct.Pin = MFX_WAKEUP_PIN; 01045 GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP; 01046 GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_HIGH; 01047 GPIO_InitStruct.Pull = GPIO_NOPULL; 01048 HAL_GPIO_Init(MFX_WAKEUP_GPIO_PORT, &GPIO_InitStruct); 01049 } 01050 01051 /** 01052 * @brief Wakeup MFX. 01053 * @retval None 01054 */ 01055 void MFX_IO_Wakeup(void) 01056 { 01057 /* Set Wakeup pin to high to wakeup Idd measurement component from standby mode */ 01058 HAL_GPIO_WritePin(MFX_WAKEUP_GPIO_PORT, MFX_WAKEUP_PIN, GPIO_PIN_SET); 01059 01060 /* Wait */ 01061 HAL_Delay(1); 01062 01063 /* Set gpio pin basck to low */ 01064 HAL_GPIO_WritePin(MFX_WAKEUP_GPIO_PORT, MFX_WAKEUP_PIN, GPIO_PIN_RESET); 01065 } 01066 01067 /** 01068 * @brief MFX writes single data. 01069 * @param Addr: I2C address 01070 * @param Reg: Register address 01071 * @param Value: Data to be written 01072 * @retval None 01073 */ 01074 void MFX_IO_Write(uint16_t Addr, uint8_t Reg, uint8_t Value) 01075 { 01076 I2C2_WriteData(Addr, Reg, I2C_MEMADD_SIZE_8BIT, Value); 01077 } 01078 01079 /** 01080 * @brief MFX reads single data. 01081 * @param Addr: I2C address 01082 * @param Reg: Register address 01083 * @retval Read data 01084 */ 01085 uint8_t MFX_IO_Read(uint16_t Addr, uint8_t Reg) 01086 { 01087 return I2C2_ReadData(Addr, Reg, I2C_MEMADD_SIZE_8BIT); 01088 } 01089 01090 /** 01091 * @brief MFX reads multiple data. 01092 * @param Addr: I2C address 01093 * @param Reg: Register address 01094 * @param Buffer: Pointer to data buffer 01095 * @param Length: Length of the data 01096 * @retval Number of read data 01097 */ 01098 uint16_t MFX_IO_ReadMultiple(uint16_t Addr, uint8_t Reg, uint8_t *Buffer, uint16_t Length) 01099 { 01100 return I2C2_ReadBuffer(Addr, (uint16_t)Reg, I2C_MEMADD_SIZE_8BIT, Buffer, Length); 01101 } 01102 01103 /** 01104 * @brief MFX writes multiple data. 01105 * @param Addr: I2C address 01106 * @param Reg: Register address 01107 * @param Buffer: Pointer to data buffer 01108 * @param Length: Length of the data 01109 * @retval None 01110 */ 01111 void MFX_IO_WriteMultiple(uint16_t Addr, uint8_t Reg, uint8_t *Buffer, uint16_t Length) 01112 { 01113 I2C2_WriteBuffer(Addr, (uint16_t)Reg, I2C_MEMADD_SIZE_8BIT, Buffer, Length); 01114 } 01115 01116 /** 01117 * @brief MFX delay 01118 * @param Delay: Delay in ms 01119 * @retval None 01120 */ 01121 void MFX_IO_Delay(uint32_t Delay) 01122 { 01123 HAL_Delay(Delay); 01124 } 01125 01126 01127 /********************************* LINK AUDIO *********************************/ 01128 /** 01129 * @brief Initializes Audio low level. 01130 * @retval None 01131 */ 01132 void AUDIO_IO_Init(void) 01133 { 01134 GPIO_InitTypeDef GPIO_InitStruct; 01135 uint8_t Value; 01136 01137 /* Enable Reset GPIO Clock */ 01138 AUDIO_RESET_GPIO_CLK_ENABLE(); 01139 01140 /* Audio reset pin configuration */ 01141 GPIO_InitStruct.Pin = AUDIO_RESET_PIN; 01142 GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP; 01143 GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_HIGH; 01144 GPIO_InitStruct.Pull = GPIO_NOPULL; 01145 HAL_GPIO_Init(AUDIO_RESET_GPIO, &GPIO_InitStruct); 01146 01147 /* I2C bus init */ 01148 I2C2_Init(); 01149 01150 /* Power off the codec */ 01151 CODEC_AUDIO_POWER_OFF(); 01152 01153 /* wait until power supplies are stable */ 01154 HAL_Delay(10); 01155 01156 /* Power on the codec */ 01157 CODEC_AUDIO_POWER_ON(); 01158 01159 /* Set the device in standby mode */ 01160 Value = AUDIO_IO_Read(AUDIO_I2C_ADDRESS, 0x02); 01161 AUDIO_IO_Write(AUDIO_I2C_ADDRESS, 0x02, (Value | 0x01)); 01162 01163 /* Set all power down bits to 1 */ 01164 AUDIO_IO_Write(AUDIO_I2C_ADDRESS, 0x02, 0x7F); 01165 Value = AUDIO_IO_Read(AUDIO_I2C_ADDRESS, 0x03); 01166 AUDIO_IO_Write(AUDIO_I2C_ADDRESS, 0x03, (Value | 0x0E)); 01167 } 01168 01169 /** 01170 * @brief Deinitializes Audio low level. 01171 * @retval None 01172 */ 01173 void AUDIO_IO_DeInit(void) 01174 { 01175 uint8_t Value; 01176 01177 /* Mute DAC and ADC */ 01178 Value = AUDIO_IO_Read(AUDIO_I2C_ADDRESS, 0x08); 01179 AUDIO_IO_Write(AUDIO_I2C_ADDRESS, 0x08, (Value | 0x03)); 01180 Value = AUDIO_IO_Read(AUDIO_I2C_ADDRESS, 0x07); 01181 AUDIO_IO_Write(AUDIO_I2C_ADDRESS, 0x07, (Value | 0x03)); 01182 01183 /* Disable soft ramp and zero cross */ 01184 Value = AUDIO_IO_Read(AUDIO_I2C_ADDRESS, 0x06); 01185 AUDIO_IO_Write(AUDIO_I2C_ADDRESS, 0x06, (Value & 0xF0)); 01186 01187 /* Set PDN to 1 */ 01188 Value = AUDIO_IO_Read(AUDIO_I2C_ADDRESS, 0x02); 01189 AUDIO_IO_Write(AUDIO_I2C_ADDRESS, 0x02, (Value | 0x01)); 01190 01191 /* Set all power down bits to 1 */ 01192 AUDIO_IO_Write(AUDIO_I2C_ADDRESS, 0x02, 0x7F); 01193 Value = AUDIO_IO_Read(AUDIO_I2C_ADDRESS, 0x03); 01194 AUDIO_IO_Write(AUDIO_I2C_ADDRESS, 0x03, (Value | 0x0E)); 01195 01196 /* Power off the codec */ 01197 CODEC_AUDIO_POWER_OFF(); 01198 01199 } 01200 01201 /** 01202 * @brief Writes a single data. 01203 * @param Addr: I2C address 01204 * @param Reg: Reg address 01205 * @param Value: Data to be written 01206 * @retval None 01207 */ 01208 void AUDIO_IO_Write(uint8_t Addr, uint8_t Reg, uint8_t Value) 01209 { 01210 I2C2_WriteBuffer(Addr, (uint16_t)Reg, I2C_MEMADD_SIZE_8BIT, &Value, 1); 01211 } 01212 01213 /** 01214 * @brief Reads a single data. 01215 * @param Addr: I2C address 01216 * @param Reg: Reg address 01217 * @retval Data to be read 01218 */ 01219 uint8_t AUDIO_IO_Read(uint8_t Addr, uint8_t Reg) 01220 { 01221 uint8_t Read_Value = 0; 01222 01223 I2C2_ReadBuffer((uint16_t) Addr, (uint16_t) Reg, I2C_MEMADD_SIZE_8BIT, &Read_Value, 1); 01224 01225 return Read_Value; 01226 } 01227 01228 /** 01229 * @brief AUDIO Codec delay 01230 * @param Delay: Delay in ms 01231 * @retval None 01232 */ 01233 void AUDIO_IO_Delay(uint32_t Delay) 01234 { 01235 HAL_Delay(Delay); 01236 } 01237 01238 01239 01240 01241 /*************************** FMC Routines ************************************/ 01242 /** 01243 * @brief Initializes FMC_BANK1_LCD_IO MSP. 01244 * @param None 01245 * @retval None 01246 */ 01247 void FMC_BANK1_MspInit(void) 01248 { 01249 01250 GPIO_InitTypeDef GPIO_Init_Structure; 01251 01252 /* Enable FMC clock */ 01253 __HAL_RCC_FMC_CLK_ENABLE(); 01254 01255 /* Enable GPIOs clock */ 01256 __HAL_RCC_GPIOD_CLK_ENABLE(); 01257 __HAL_RCC_GPIOE_CLK_ENABLE(); 01258 __HAL_RCC_GPIOF_CLK_ENABLE(); 01259 __HAL_RCC_GPIOG_CLK_ENABLE(); 01260 __HAL_RCC_PWR_CLK_ENABLE(); 01261 HAL_PWREx_EnableVddIO2(); 01262 01263 /* Set PD11 */ 01264 GPIO_Init_Structure.Pin = GPIO_PIN_11; 01265 GPIO_Init_Structure.Mode = GPIO_MODE_OUTPUT_PP; 01266 GPIO_Init_Structure.Pull = GPIO_NOPULL; 01267 GPIO_Init_Structure.Speed = GPIO_SPEED_FREQ_LOW; 01268 GPIO_Init_Structure.Alternate = 0; 01269 01270 HAL_GPIO_Init(GPIOD, &GPIO_Init_Structure); 01271 01272 01273 GPIO_Init_Structure.Mode = GPIO_MODE_AF_PP; 01274 GPIO_Init_Structure.Pull = GPIO_PULLUP; 01275 GPIO_Init_Structure.Speed = GPIO_SPEED_FREQ_VERY_HIGH; 01276 GPIO_Init_Structure.Alternate = GPIO_AF12_FMC; 01277 /* GPIOD configuration */ /* GPIO_PIN_7 is FMC_NE1 */ 01278 GPIO_Init_Structure.Pin = GPIO_PIN_0 | GPIO_PIN_1 | GPIO_PIN_4 | GPIO_PIN_5 | GPIO_PIN_8 | \ 01279 GPIO_PIN_9 | GPIO_PIN_10 | GPIO_PIN_14 | GPIO_PIN_15 | GPIO_PIN_7; 01280 01281 HAL_GPIO_Init(GPIOD, &GPIO_Init_Structure); 01282 01283 01284 01285 01286 /* GPIOE configuration */ 01287 GPIO_Init_Structure.Pin = GPIO_PIN_7 | \ 01288 GPIO_PIN_8 | GPIO_PIN_9 | GPIO_PIN_10 | GPIO_PIN_11 | GPIO_PIN_12 | \ 01289 GPIO_PIN_13 | GPIO_PIN_14 | GPIO_PIN_15; 01290 HAL_GPIO_Init(GPIOE, &GPIO_Init_Structure); 01291 01292 /* GPIOF configuration */ 01293 GPIO_Init_Structure.Pin = GPIO_PIN_13 ; 01294 HAL_GPIO_Init(GPIOD, &GPIO_Init_Structure); 01295 01296 GPIO_Init_Structure.Pin = GPIO_PIN_0; 01297 HAL_GPIO_Init(GPIOF, &GPIO_Init_Structure); 01298 } 01299 01300 01301 /** 01302 * @brief Initializes LCD IO. 01303 * @param None 01304 * @retval None 01305 */ 01306 void FMC_BANK1_Init(void) 01307 { 01308 SRAM_HandleTypeDef hsram; 01309 FMC_NORSRAM_TimingTypeDef sram_timing; 01310 FMC_NORSRAM_TimingTypeDef sram_timing_write; 01311 01312 /*** Configure the SRAM Bank 1 ***/ 01313 /* Configure IPs */ 01314 hsram.Instance = FMC_NORSRAM_DEVICE; 01315 hsram.Extended = FMC_NORSRAM_EXTENDED_DEVICE; 01316 01317 01318 /* Timing for READING */ 01319 01320 sram_timing.AddressSetupTime = 1; 01321 sram_timing.AddressHoldTime = 1; 01322 sram_timing.DataSetupTime = 1; 01323 sram_timing.BusTurnAroundDuration = 0; 01324 sram_timing.CLKDivision = 2; 01325 sram_timing.DataLatency = 2; 01326 sram_timing.AccessMode = FMC_ACCESS_MODE_A; 01327 /* Timing for WRITING */ 01328 sram_timing_write.AddressSetupTime = 5; 01329 sram_timing_write.AddressHoldTime = 1; 01330 sram_timing_write.DataSetupTime = 3; 01331 sram_timing_write.BusTurnAroundDuration = 2; 01332 sram_timing_write.CLKDivision = 2; 01333 sram_timing_write.DataLatency = 2; 01334 sram_timing_write.AccessMode = FMC_ACCESS_MODE_A; 01335 01336 01337 hsram.Init.NSBank = FMC_NORSRAM_BANK1; 01338 hsram.Init.DataAddressMux = FMC_DATA_ADDRESS_MUX_DISABLE; 01339 hsram.Init.MemoryType = FMC_MEMORY_TYPE_SRAM; 01340 hsram.Init.MemoryDataWidth = FMC_NORSRAM_MEM_BUS_WIDTH_16; 01341 hsram.Init.BurstAccessMode = FMC_BURST_ACCESS_MODE_DISABLE; 01342 hsram.Init.WaitSignalPolarity = FMC_WAIT_SIGNAL_POLARITY_LOW; 01343 hsram.Init.WaitSignalActive = FMC_WAIT_TIMING_BEFORE_WS; 01344 hsram.Init.WriteOperation = FMC_WRITE_OPERATION_ENABLE; 01345 hsram.Init.WaitSignal = FMC_WAIT_SIGNAL_DISABLE; 01346 hsram.Init.ExtendedMode = FMC_EXTENDED_MODE_DISABLE; 01347 hsram.Init.AsynchronousWait = FMC_ASYNCHRONOUS_WAIT_DISABLE; 01348 hsram.Init.WriteBurst = FMC_WRITE_BURST_DISABLE; 01349 hsram.Init.PageSize = FMC_PAGE_SIZE_NONE; 01350 hsram.Init.WriteFifo = FMC_WRITE_FIFO_DISABLE; 01351 hsram.Init.ContinuousClock = FMC_CONTINUOUS_CLOCK_SYNC_ONLY; 01352 /* Initialize the SRAM controller */ 01353 FMC_BANK1_MspInit(); 01354 HAL_SRAM_Init(&hsram, &sram_timing, &sram_timing_write); 01355 01356 } 01357 01358 01359 /** 01360 * @brief DeInitializes FMC_BANK1_LCD_IO MSP. 01361 * @param None 01362 * @retval None 01363 */ 01364 void FMC_BANK1_MspDeInit(void) 01365 { 01366 01367 /* Enable FMC clock */ 01368 __HAL_RCC_FMC_CLK_ENABLE(); 01369 01370 /* Enable GPIOs clock */ 01371 __HAL_RCC_GPIOD_CLK_ENABLE(); 01372 __HAL_RCC_GPIOE_CLK_ENABLE(); 01373 __HAL_RCC_GPIOF_CLK_ENABLE(); 01374 __HAL_RCC_GPIOG_CLK_ENABLE(); 01375 __HAL_RCC_PWR_CLK_ENABLE(); 01376 01377 01378 HAL_GPIO_DeInit(GPIOD, GPIO_PIN_0 | GPIO_PIN_1 | GPIO_PIN_4 | GPIO_PIN_5 | GPIO_PIN_8 | \ 01379 GPIO_PIN_9 | GPIO_PIN_10 | GPIO_PIN_14 | GPIO_PIN_15 | GPIO_PIN_7 | GPIO_PIN_11 | GPIO_PIN_13); 01380 01381 01382 01383 HAL_GPIO_DeInit(GPIOE, GPIO_PIN_7 | \ 01384 GPIO_PIN_8 | GPIO_PIN_9 | GPIO_PIN_10 | GPIO_PIN_11 | GPIO_PIN_12 | \ 01385 GPIO_PIN_13 | GPIO_PIN_14 | GPIO_PIN_15); 01386 01387 HAL_GPIO_DeInit(GPIOF, GPIO_PIN_0); 01388 01389 01390 } 01391 01392 01393 /** 01394 * @brief Writes register value. 01395 * @param Data: Data to be written 01396 * @retval None 01397 */ 01398 static void FMC_BANK1_WriteData(uint16_t Data) 01399 { 01400 /* Write 16-bit Reg */ 01401 LCD_ADDR->REG = Data; 01402 } 01403 01404 /** 01405 * @brief Writes register address. 01406 * @param Reg: Register to be written 01407 * @retval None 01408 */ 01409 static void FMC_BANK1_WriteReg(uint8_t Reg) 01410 { 01411 /* Write 16-bit Index, then write register */ 01412 FMC_BANK1_ADDR->REG = Reg; 01413 } 01414 01415 /** 01416 * @brief Reads register value. 01417 * @param None 01418 * @retval Read value 01419 */ 01420 static uint16_t FMC_BANK1_ReadData(void) 01421 { 01422 return LCD_ADDR->REG; 01423 } 01424 01425 /******************************************************************************* 01426 LINK OPERATIONS 01427 *******************************************************************************/ 01428 01429 /********************************* LINK LCD ***********************************/ 01430 01431 /** 01432 * @brief Initializes LCD low level. 01433 * @param None 01434 * @retval None 01435 */ 01436 void LCD_IO_Init(void) 01437 { 01438 FMC_BANK1_Init(); 01439 } 01440 01441 /** 01442 * @brief Writes data on LCD data register. 01443 * @param Data: Data to be written 01444 * @retval None 01445 */ 01446 void LCD_IO_WriteData(uint16_t RegValue) 01447 { 01448 /* Write 16-bit Reg */ 01449 FMC_BANK1_WriteData(RegValue); 01450 } 01451 01452 /** 01453 * @brief Writes several data on LCD data register. 01454 * @param Data: pointer on data to be written 01455 * @param Size: data amount in 16bits short unit 01456 * @retval None 01457 */ 01458 void LCD_IO_WriteMultipleData(uint16_t *pData, uint32_t Size) 01459 { 01460 uint32_t i; 01461 01462 for (i = 0; i < Size; i++) 01463 { 01464 FMC_BANK1_WriteData(pData[i]); 01465 } 01466 } 01467 01468 /** 01469 * @brief Writes register on LCD register. 01470 * @param Reg: Register to be written 01471 * @retval None 01472 */ 01473 void LCD_IO_WriteReg(uint8_t Reg) 01474 { 01475 /* Write 16-bit Index, then Write Reg */ 01476 FMC_BANK1_WriteReg(Reg); 01477 } 01478 01479 /** 01480 * @brief Reads data from LCD data register. 01481 * @param None 01482 * @retval Read data. 01483 */ 01484 uint16_t LCD_IO_ReadData(void) 01485 { 01486 return FMC_BANK1_ReadData(); 01487 } 01488 01489 /** 01490 * @brief LCD delay 01491 * @param Delay: Delay in ms 01492 * @retval None 01493 */ 01494 void LCD_IO_Delay(uint32_t Delay) 01495 { 01496 HAL_Delay(Delay); 01497 } 01498 01499 01500 /************************** LINK TS (TouchScreen) *****************************/ 01501 /** 01502 * @brief Initializes Touchscreen low level. 01503 * @retval None 01504 */ 01505 void TS_IO_Init(void) 01506 { 01507 I2Cx_Init(&hI2cTSHandler); 01508 01509 if (ts_io_init == 0) 01510 { 01511 if (BSP_LCD_Init() == LCD_ERROR) 01512 { 01513 BSP_ErrorHandler(); 01514 } 01515 01516 BSP_IO_ConfigPin(TS_RST_PIN, IO_MODE_OUTPUT); 01517 01518 BSP_IO_WritePin(TS_RST_PIN, GPIO_PIN_RESET); 01519 HAL_Delay(10); 01520 BSP_IO_WritePin(TS_RST_PIN, GPIO_PIN_SET); 01521 HAL_Delay(200); 01522 01523 ts_io_init = 1; 01524 } 01525 } 01526 01527 /** 01528 * @brief Writes a single data. 01529 * @param Addr: I2C address 01530 * @param Reg: Reg address 01531 * @param Value: Data to be written 01532 * @retval None 01533 */ 01534 void TS_IO_Write(uint8_t Addr, uint8_t Reg, uint8_t Value) 01535 { 01536 I2Cx_WriteMultiple(&hI2cTSHandler, Addr, (uint16_t)Reg, I2C_MEMADD_SIZE_8BIT, (uint8_t *)&Value, 1); 01537 } 01538 01539 /** 01540 * @brief Reads a single data. 01541 * @param Addr: I2C address 01542 * @param Reg: Reg address 01543 * @retval Data to be read 01544 */ 01545 uint8_t TS_IO_Read(uint8_t Addr, uint8_t Reg) 01546 { 01547 return I2C2_ReadData(Addr, Reg, I2C_MEMADD_SIZE_8BIT); 01548 } 01549 01550 /** 01551 * @brief Reads multiple data with I2C communication 01552 * channel from TouchScreen. 01553 * @param Addr: I2C address 01554 * @param Reg: Register address 01555 * @param Buffer: Pointer to data buffer 01556 * @param Length: Length of the data 01557 * @retval Number of read data 01558 */ 01559 uint16_t TS_IO_ReadMultiple(uint8_t Addr, uint8_t Reg, uint8_t *Buffer, uint16_t Length) 01560 { 01561 return I2Cx_ReadMultiple(&hI2cTSHandler, Addr, (uint16_t)Reg, I2C_MEMADD_SIZE_8BIT, Buffer, Length); 01562 } 01563 01564 /** 01565 * @brief Writes multiple data with I2C communication 01566 * channel from MCU to TouchScreen. 01567 * @param Addr: I2C address 01568 * @param Reg: Register address 01569 * @param Buffer: Pointer to data buffer 01570 * @param Length: Length of the data 01571 * @retval None 01572 */ 01573 void TS_IO_WriteMultiple(uint8_t Addr, uint8_t Reg, uint8_t *Buffer, uint16_t Length) 01574 { 01575 I2Cx_WriteMultiple(&hI2cTSHandler, Addr, (uint16_t)Reg, I2C_MEMADD_SIZE_8BIT, Buffer, Length); 01576 } 01577 01578 /** 01579 * @brief Delay function used in TouchScreen low level driver. 01580 * @param Delay: Delay in ms 01581 * @retval None 01582 */ 01583 void TS_IO_Delay(uint32_t Delay) 01584 { 01585 HAL_Delay(Delay); 01586 } 01587 01588 01589 /************************** Camera *****************************/ 01590 /** 01591 * @brief Initializes Camera low level. 01592 * @retval None 01593 */ 01594 void CAMERA_IO_Init(void) 01595 { 01596 I2Cx_Init(&hI2cCameraHandler); 01597 } 01598 01599 /** 01600 * @brief Camera writes single data. 01601 * @param Addr: I2C address 01602 * @param Reg: Register address 01603 * @param Value: Data to be written 01604 * @retval None 01605 */ 01606 void CAMERA_IO_Write(uint8_t Addr, uint8_t Reg, uint8_t Value) 01607 { 01608 I2Cx_WriteMultiple(&hI2cCameraHandler, Addr, (uint16_t)Reg, I2C_MEMADD_SIZE_8BIT, (uint8_t *)&Value, 1); 01609 } 01610 01611 /** 01612 * @brief Camera reads single data. 01613 * @param Addr: I2C address 01614 * @param Reg: Register address 01615 * @retval Read data 01616 */ 01617 uint8_t CAMERA_IO_Read(uint8_t Addr, uint8_t Reg) 01618 { 01619 uint8_t read_value = 0; 01620 01621 I2Cx_ReadMultiple(&hI2cCameraHandler, Addr, Reg, I2C_MEMADD_SIZE_8BIT, (uint8_t *)&read_value, 1); 01622 01623 return read_value; 01624 } 01625 01626 /** 01627 * @brief Camera delay 01628 * @param Delay: Delay in ms 01629 * @retval None 01630 */ 01631 void CAMERA_Delay(uint32_t Delay) 01632 { 01633 HAL_Delay(Delay); 01634 } 01635 01636 #endif /* HAL_I2C_MODULE_ENABLED */ 01637 01638 01639 __weak uint8_t BSP_LCD_Init(void) 01640 { 01641 uint8_t ret = 0; 01642 return ret; 01643 } 01644 01645 /** 01646 * @} 01647 */ 01648 01649 /** 01650 * @} 01651 */ 01652 01653 /** 01654 * @} 01655 */ 01656 01657 /** 01658 * @} 01659 */ 01660 01661 /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/
Generated on Tue Jul 12 2022 15:07:44 by
1.7.2