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