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.
stm32l476g_discovery.c
00001 /** 00002 ****************************************************************************** 00003 * @file stm32l476g_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 STM32L476G-Discovery board (MB1184) 00007 ****************************************************************************** 00008 * @attention 00009 * 00010 * <h2><center>© Copyright (c) 2016 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 "stm32l476g_discovery.h" 00023 00024 /** @addtogroup BSP 00025 * @{ 00026 */ 00027 00028 /** @defgroup STM32L476G_DISCOVERY STM32L476G-DISCOVERY 00029 * @{ 00030 */ 00031 00032 /** @defgroup STM32L476G_DISCOVERY_Common STM32L476G-DISCOVERY Common 00033 * @{ 00034 */ 00035 00036 /** @defgroup STM32L476G_DISCOVERY_Private_TypesDefinitions Private Types Definitions 00037 * @brief This file provides firmware functions to manage Leds, push-buttons, 00038 * COM ports, SD card on SPI and temperature sensor (TS751) available on 00039 * STM32L476G-DISCOVERY discoveryuation board from STMicroelectronics. 00040 * @{ 00041 */ 00042 00043 /** 00044 * @} 00045 */ 00046 00047 /** @defgroup STM32L476G_DISCOVERY_Private_Defines Private Defines 00048 * @{ 00049 */ 00050 00051 /** 00052 * @brief STM32L476G DISCOVERY BSP Driver version number 00053 */ 00054 #define __STM32L476G_DISCOVERY_BSP_VERSION_MAIN (0x02) /*!< [31:24] main version */ 00055 #define __STM32L476G_DISCOVERY_BSP_VERSION_SUB1 (0x00) /*!< [23:16] sub1 version */ 00056 #define __STM32L476G_DISCOVERY_BSP_VERSION_SUB2 (0x03) /*!< [15:8] sub2 version */ 00057 #define __STM32L476G_DISCOVERY_BSP_VERSION_RC (0x00) /*!< [7:0] release candidate */ 00058 #define __STM32L476G_DISCOVERY_BSP_VERSION ((__STM32L476G_DISCOVERY_BSP_VERSION_MAIN << 24)\ 00059 |(__STM32L476G_DISCOVERY_BSP_VERSION_SUB1 << 16)\ 00060 |(__STM32L476G_DISCOVERY_BSP_VERSION_SUB2 << 8 )\ 00061 |(__STM32L476G_DISCOVERY_BSP_VERSION_RC)) 00062 /** 00063 * @} 00064 */ 00065 00066 00067 /** @defgroup STM32L476G_DISCOVERY_Private_Macros Private Macros 00068 * @{ 00069 */ 00070 00071 /** 00072 * @} 00073 */ 00074 00075 00076 /** @defgroup STM32L476G_DISCOVERY_Exported_Variables Exported Variables 00077 * @{ 00078 */ 00079 00080 /** 00081 * @brief LED variables 00082 */ 00083 #if defined (USE_STM32L476G_DISCO_REVC) || defined (USE_STM32L476G_DISCO_REVB) 00084 GPIO_TypeDef *LED_PORT[LEDn] = {LED4_GPIO_PORT, 00085 LED5_GPIO_PORT 00086 }; 00087 00088 const uint16_t LED_PIN[LEDn] = {LED4_PIN, 00089 LED5_PIN 00090 }; 00091 #elif defined (USE_STM32L476G_DISCO_REVA) 00092 GPIO_TypeDef *LED_PORT[LEDn] = {LED3_GPIO_PORT, 00093 LED4_GPIO_PORT 00094 }; 00095 00096 const uint16_t LED_PIN[LEDn] = {LED3_PIN, 00097 LED4_PIN 00098 }; 00099 #endif 00100 00101 00102 /** 00103 * @brief JOYSTICK variables 00104 */ 00105 GPIO_TypeDef *JOY_PORT[JOYn] = {SEL_JOY_GPIO_PORT, 00106 DOWN_JOY_GPIO_PORT, 00107 LEFT_JOY_GPIO_PORT, 00108 RIGHT_JOY_GPIO_PORT, 00109 UP_JOY_GPIO_PORT 00110 }; 00111 00112 const uint16_t JOY_PIN[JOYn] = {SEL_JOY_PIN, 00113 LEFT_JOY_PIN, 00114 RIGHT_JOY_PIN, 00115 DOWN_JOY_PIN, 00116 UP_JOY_PIN 00117 }; 00118 00119 const uint8_t JOY_IRQn[JOYn] = {SEL_JOY_EXTI_IRQn, 00120 LEFT_JOY_EXTI_IRQn, 00121 RIGHT_JOY_EXTI_IRQn, 00122 DOWN_JOY_EXTI_IRQn, 00123 UP_JOY_EXTI_IRQn 00124 }; 00125 00126 /** 00127 * @brief BUS variables 00128 */ 00129 #if defined(HAL_I2C_MODULE_ENABLED) 00130 uint32_t I2c1Timeout = DISCOVERY_I2C2_TIMEOUT_MAX; /*<! Value of Timeout when I2C1 communication fails */ 00131 uint32_t I2c2Timeout = DISCOVERY_I2C2_TIMEOUT_MAX; /*<! Value of Timeout when I2C2 communication fails */ 00132 static I2C_HandleTypeDef I2c1Handle; 00133 static I2C_HandleTypeDef I2c2Handle; 00134 #endif /* HAL_I2C_MODULE_ENABLED */ 00135 00136 #if defined(HAL_SPI_MODULE_ENABLED) 00137 00138 /* LL definition */ 00139 #define __SPI_DIRECTION_2LINES(__HANDLE__) do{\ 00140 CLEAR_BIT((__HANDLE__)->Instance->CR1, SPI_CR1_RXONLY | SPI_CR1_BIDIMODE | SPI_CR1_BIDIOE);\ 00141 }while(0); 00142 00143 #define __SPI_DIRECTION_2LINES_RXONLY(__HANDLE__) do{\ 00144 CLEAR_BIT((__HANDLE__)->Instance->CR1, SPI_CR1_RXONLY | SPI_CR1_BIDIMODE | SPI_CR1_BIDIOE);\ 00145 SET_BIT((__HANDLE__)->Instance->CR1, SPI_CR1_RXONLY);\ 00146 }while(0); 00147 00148 #define __SPI_DIRECTION_1LINE_TX(__HANDLE__) do{\ 00149 CLEAR_BIT((__HANDLE__)->Instance->CR1, SPI_CR1_RXONLY | SPI_CR1_BIDIMODE | SPI_CR1_BIDIOE);\ 00150 SET_BIT((__HANDLE__)->Instance->CR1, SPI_CR1_BIDIMODE | SPI_CR1_BIDIOE);\ 00151 }while(0); 00152 00153 #define __SPI_DIRECTION_1LINE_RX(__HANDLE__) do {\ 00154 CLEAR_BIT((__HANDLE__)->Instance->CR1, SPI_CR1_RXONLY | SPI_CR1_BIDIMODE | SPI_CR1_BIDIOE);\ 00155 SET_BIT((__HANDLE__)->Instance->CR1, SPI_CR1_BIDIMODE);\ 00156 } while(0); 00157 00158 00159 uint32_t SpixTimeout = SPIx_TIMEOUT_MAX; /*<! Value of Timeout when SPI communication fails */ 00160 static SPI_HandleTypeDef SpiHandle; 00161 #endif /* HAL_SPI_MODULE_ENABLED */ 00162 00163 /** 00164 * @} 00165 */ 00166 00167 /** @defgroup STM32L476G_DISCOVERY_Private_FunctionPrototypes Private Functions 00168 * @{ 00169 */ 00170 /**************************** Bus functions ************************************/ 00171 /* I2C2 bus function */ 00172 #if defined(HAL_I2C_MODULE_ENABLED) 00173 static void I2C2_Init(void); 00174 static void I2C2_MspInit(I2C_HandleTypeDef *hi2c); 00175 static void I2C2_DeInit(void); 00176 static void I2C2_MspDeInit(I2C_HandleTypeDef *hi2c); 00177 static void I2C2_WriteData(uint16_t Addr, uint16_t Reg, uint16_t RegSize, uint8_t Value); 00178 static HAL_StatusTypeDef I2C2_WriteBuffer(uint16_t Addr, uint16_t Reg, uint16_t RegSize, uint8_t *pBuffer, uint16_t Length); 00179 static uint8_t I2C2_ReadData(uint16_t Addr, uint16_t Reg, uint16_t RegSize); 00180 static HAL_StatusTypeDef I2C2_ReadBuffer(uint16_t Addr, uint16_t Reg, uint16_t RegSize, uint8_t *pBuffer, uint16_t Length); 00181 static void I2C2_Error(void); 00182 00183 static void I2C1_Init(void); 00184 static void I2C1_MspInit(I2C_HandleTypeDef *hi2c); 00185 static void I2C1_DeInit(void); 00186 static void I2C1_MspDeInit(I2C_HandleTypeDef *hi2c); 00187 static HAL_StatusTypeDef I2C1_WriteBuffer(uint16_t Addr, uint16_t Reg, uint16_t RegSize, uint8_t *pBuffer, uint16_t Length); 00188 static HAL_StatusTypeDef I2C1_ReadBuffer(uint16_t Addr, uint16_t Reg, uint16_t RegSize, uint8_t *pBuffer, uint16_t Length); 00189 static void I2C1_Error(void); 00190 #endif/* HAL_I2C_MODULE_ENABLED */ 00191 00192 /* SPIx bus function */ 00193 #if defined(HAL_SPI_MODULE_ENABLED) 00194 static void SPIx_Init(void); 00195 static void SPIx_MspInit(SPI_HandleTypeDef *hspi); 00196 static void SPIx_DeInit(void); 00197 static void SPIx_MspDeInit(void); 00198 static uint8_t SPIx_WriteRead(uint8_t Byte); 00199 static void SPIx_Write(uint8_t byte); 00200 static uint8_t SPIx_Read(void); 00201 #endif 00202 00203 /**************************** Link functions ***********************************/ 00204 #if defined(HAL_I2C_MODULE_ENABLED) 00205 /* Link functions for EEPROM peripheral over I2C */ 00206 void EEPROM_I2C_IO_Init(void); 00207 HAL_StatusTypeDef EEPROM_I2C_IO_WriteData(uint16_t DevAddress, uint16_t MemAddress, uint8_t *pBuffer, uint32_t BufferSize); 00208 HAL_StatusTypeDef EEPROM_I2C_IO_ReadData(uint16_t DevAddress, uint16_t MemAddress, uint8_t *pBuffer, uint32_t BufferSize); 00209 HAL_StatusTypeDef EEPROM_I2C_IO_IsDeviceReady(uint16_t DevAddress, uint32_t Trials); 00210 00211 /* Link functions for Audio Codec peripheral */ 00212 void AUDIO_IO_Init(void); 00213 void AUDIO_IO_DeInit(void); 00214 void AUDIO_IO_Write(uint8_t Addr, uint8_t Reg, uint8_t Value); 00215 uint8_t AUDIO_IO_Read(uint8_t Addr, uint8_t Reg); 00216 void AUDIO_IO_Delay(uint32_t delay); 00217 #endif/* HAL_I2C_MODULE_ENABLED */ 00218 00219 #if defined(HAL_SPI_MODULE_ENABLED) 00220 /* Link function for COMPASS / ACCELERO peripheral */ 00221 void ACCELERO_IO_Init(void); 00222 void ACCELERO_IO_DeInit(void); 00223 void ACCELERO_IO_ITConfig(void); 00224 void ACCELERO_IO_Write(uint8_t RegisterAddr, uint8_t Value); 00225 uint8_t ACCELERO_IO_Read(uint8_t RegisterAddr); 00226 00227 void MAGNETO_IO_Init(void); 00228 void MAGNETO_IO_DeInit(void); 00229 void MAGNETO_IO_ITConfig(void); 00230 void MAGNETO_IO_Write(uint8_t RegisterAddr, uint8_t Value); 00231 uint8_t MAGNETO_IO_Read(uint8_t RegisterAddr); 00232 00233 00234 /* Link functions for GYRO peripheral */ 00235 void GYRO_IO_Init(void); 00236 void GYRO_IO_DeInit(void); 00237 void GYRO_IO_Write(uint8_t *pBuffer, uint8_t WriteAddr, uint16_t NumByteToWrite); 00238 void GYRO_IO_Read(uint8_t *pBuffer, uint8_t ReadAddr, uint16_t NumByteToRead); 00239 00240 #endif 00241 00242 #if defined(HAL_I2C_MODULE_ENABLED) 00243 /* Link functions IOExpander */ 00244 void IOE_Init(void); 00245 void IOE_ITConfig(void); 00246 void IOE_Delay(uint32_t Delay); 00247 void IOE_Write(uint8_t Addr, uint8_t Reg, uint8_t Value); 00248 uint8_t IOE_Read(uint8_t Addr, uint8_t Reg); 00249 uint16_t IOE_ReadMultiple(uint8_t Addr, uint8_t Reg, uint8_t *Buffer, uint16_t Length); 00250 00251 /* Link functions for IDD measurment */ 00252 void MFX_IO_Init(void); 00253 void MFX_IO_DeInit(void); 00254 void MFX_IO_ITConfig(void); 00255 void MFX_IO_EnableWakeupPin(void); 00256 void MFX_IO_Wakeup(void); 00257 void MFX_IO_Delay(uint32_t delay); 00258 void MFX_IO_Write(uint16_t addr, uint8_t reg, uint8_t value); 00259 uint8_t MFX_IO_Read(uint16_t addr, uint8_t reg); 00260 void MFX_IO_WriteMultiple(uint16_t Addr, uint8_t Reg, uint8_t *Buffer, uint16_t Length); 00261 uint16_t MFX_IO_ReadMultiple(uint16_t addr, uint8_t reg, uint8_t *buffer, uint16_t length); 00262 #endif/* HAL_I2C_MODULE_ENABLED */ 00263 /** 00264 * @} 00265 */ 00266 00267 /** @defgroup STM32L476G_DISCOVERY_Exported_Functions Exported Functions 00268 * @{ 00269 */ 00270 00271 /** 00272 * @brief This method returns the STM32L476 DISCOVERY BSP Driver revision 00273 * @retval version : 0xXYZR (8bits for each decimal, R for RC) 00274 */ 00275 uint32_t BSP_GetVersion(void) 00276 { 00277 return __STM32L476G_DISCOVERY_BSP_VERSION; 00278 } 00279 00280 /** 00281 * @brief This method returns the STM32L476 DISCOVERY supply mode 00282 * @retval Code of current supply mode 00283 * This code can be one of following: 00284 * @arg SUPPLY_MODE_EXTERNAL 00285 * @arg SUPPLY_MODE_BATTERY 00286 */ 00287 SupplyMode_TypeDef BSP_SupplyModeDetection(void) 00288 { 00289 SupplyMode_TypeDef supplymode = SUPPLY_MODE_ERROR; 00290 GPIO_InitTypeDef GPIO_InitStruct; 00291 00292 BATTERY_DETECTION_GPIO_CLK_ENABLE(); 00293 00294 /* COMP GPIO pin configuration */ 00295 GPIO_InitStruct.Pin = BATTERY_DETECTION_PIN; 00296 GPIO_InitStruct.Mode = GPIO_MODE_INPUT; 00297 GPIO_InitStruct.Pull = GPIO_NOPULL; 00298 GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_VERY_HIGH; 00299 HAL_GPIO_Init(BATTERY_DETECTION_GPIO_PORT, &GPIO_InitStruct); 00300 00301 HAL_Delay(400); 00302 if (HAL_GPIO_ReadPin(BATTERY_DETECTION_GPIO_PORT, GPIO_InitStruct.Pin) != GPIO_PIN_RESET) 00303 { 00304 supplymode = SUPPLY_MODE_EXTERNAL; 00305 } 00306 else 00307 { 00308 supplymode = SUPPLY_MODE_BATTERY; 00309 } 00310 00311 HAL_GPIO_DeInit(BATTERY_DETECTION_GPIO_PORT, GPIO_InitStruct.Pin); 00312 00313 return supplymode; 00314 } 00315 00316 #if defined (USE_STM32L476G_DISCO_REVC) || defined (USE_STM32L476G_DISCO_REVB) 00317 /** 00318 * @brief Configures LED GPIOs. 00319 * @param Led: Specifies the Led to be configured. 00320 * This parameter can be one of following parameters: 00321 * @arg DISCO_LED4 00322 * @arg DISCO_LED5 00323 * @retval None 00324 */ 00325 #elif defined (USE_STM32L476G_DISCO_REVA) 00326 /** 00327 * @brief Configures LED GPIOs. 00328 * @param Led: Specifies the Led to be configured. 00329 * This parameter can be one of following parameters: 00330 * @arg DISCO_LED3 00331 * @arg DISCO_LED4 00332 * @retval None 00333 */ 00334 #endif 00335 void BSP_LED_Init(Led_TypeDef Led) 00336 { 00337 GPIO_InitTypeDef GPIO_InitStructure; 00338 00339 /* Enable the GPIO_LED clock */ 00340 LEDx_GPIO_CLK_ENABLE(Led); 00341 00342 /* Configure the GPIO_LED pin */ 00343 GPIO_InitStructure.Pin = LED_PIN[Led]; 00344 GPIO_InitStructure.Mode = GPIO_MODE_OUTPUT_PP; 00345 GPIO_InitStructure.Pull = GPIO_NOPULL; 00346 GPIO_InitStructure.Speed = GPIO_SPEED_FREQ_VERY_HIGH; 00347 00348 HAL_GPIO_Init(LED_PORT[Led], &GPIO_InitStructure); 00349 00350 HAL_GPIO_WritePin(LED_PORT[Led], GPIO_InitStructure.Pin, GPIO_PIN_RESET); 00351 } 00352 00353 #if defined (USE_STM32L476G_DISCO_REVC) || defined (USE_STM32L476G_DISCO_REVB) 00354 /** 00355 * @brief Unconfigures LED GPIOs. 00356 * @param Led: Specifies the Led to be unconfigured. 00357 * This parameter can be one of following parameters: 00358 * @arg DISCO_LED4 00359 * @arg DISCO_LED5 00360 * @retval None 00361 */ 00362 #elif defined (USE_STM32L476G_DISCO_REVA) 00363 /** 00364 * @brief Unconfigures LED GPIOs. 00365 * @param Led: Specifies the Led to be unconfigured. 00366 * This parameter can be one of following parameters: 00367 * @arg DISCO_LED3 00368 * @arg DISCO_LED4 00369 * @retval None 00370 */ 00371 #endif 00372 void BSP_LED_DeInit(Led_TypeDef Led) 00373 { 00374 /* Enable the GPIO_LED clock */ 00375 LEDx_GPIO_CLK_ENABLE(Led); 00376 00377 HAL_GPIO_DeInit(LED_PORT[Led], LED_PIN[Led]); 00378 } 00379 00380 #if defined (USE_STM32L476G_DISCO_REVC) || defined (USE_STM32L476G_DISCO_REVB) 00381 /** 00382 * @brief Turns selected LED On. 00383 * @param Led: Specifies the Led to be set on. 00384 * This parameter can be one of following parameters: 00385 * @arg DISCO_LED4 00386 * @arg DISCO_LED5 00387 * @retval None 00388 */ 00389 #elif defined (USE_STM32L476G_DISCO_REVA) 00390 /** 00391 * @brief Turns selected LED On. 00392 * @param Led: Specifies the Led to be set on. 00393 * This parameter can be one of following parameters: 00394 * @arg DISCO_LED3 00395 * @arg DISCO_LED4 00396 * @retval None 00397 */ 00398 #endif 00399 void BSP_LED_On(Led_TypeDef Led) 00400 { 00401 HAL_GPIO_WritePin(LED_PORT[Led], LED_PIN[Led], GPIO_PIN_SET); 00402 } 00403 00404 #if defined (USE_STM32L476G_DISCO_REVC) || defined (USE_STM32L476G_DISCO_REVB) 00405 /** 00406 * @brief Turns selected LED Off. 00407 * @param Led: Specifies the Led to be set off. 00408 * This parameter can be one of following parameters: 00409 * @arg DISCO_LED4 00410 * @arg DISCO_LED5 00411 * @retval None 00412 */ 00413 #elif defined (USE_STM32L476G_DISCO_REVA) 00414 /** 00415 * @brief Turns selected LED Off. 00416 * @param Led: Specifies the Led to be set off. 00417 * This parameter can be one of following parameters: 00418 * @arg DISCO_LED3 00419 * @arg DISCO_LED4 00420 * @retval None 00421 */ 00422 #endif 00423 void BSP_LED_Off(Led_TypeDef Led) 00424 { 00425 HAL_GPIO_WritePin(LED_PORT[Led], LED_PIN[Led], GPIO_PIN_RESET); 00426 } 00427 00428 #if defined (USE_STM32L476G_DISCO_REVC) || defined (USE_STM32L476G_DISCO_REVB) 00429 /** 00430 * @brief Toggles the selected LED. 00431 * @param Led: Specifies the Led to be toggled. 00432 * This parameter can be one of following parameters: 00433 * @arg DISCO_LED4 00434 * @arg DISCO_LED5 00435 * @retval None 00436 */ 00437 #elif defined (USE_STM32L476G_DISCO_REVA) 00438 /** 00439 * @brief Toggles the selected LED. 00440 * @param Led: Specifies the Led to be toggled. 00441 * This parameter can be one of following parameters: 00442 * @arg DISCO_LED3 00443 * @arg DISCO_LED4 00444 * @retval None 00445 */ 00446 #endif 00447 void BSP_LED_Toggle(Led_TypeDef Led) 00448 { 00449 HAL_GPIO_TogglePin(LED_PORT[Led], LED_PIN[Led]); 00450 } 00451 00452 /** 00453 * @brief Configures all buttons of the joystick in GPIO or EXTI modes. 00454 * @param Joy_Mode: Joystick mode. 00455 * This parameter can be one of the following values: 00456 * @arg JOY_MODE_GPIO: Joystick pins will be used as simple IOs 00457 * @arg JOY_MODE_EXTI: Joystick pins will be connected to EXTI line 00458 * with interrupt generation capability 00459 * @retval HAL_OK: if all initializations are OK. Other value if error. 00460 */ 00461 uint8_t BSP_JOY_Init(JOYMode_TypeDef Joy_Mode) 00462 { 00463 JOYState_TypeDef joykey; 00464 GPIO_InitTypeDef GPIO_InitStruct; 00465 00466 /* Initialized the Joystick. */ 00467 for (joykey = JOY_SEL; joykey < (JOY_SEL + JOYn) ; joykey++) 00468 { 00469 /* Enable the JOY clock */ 00470 JOYx_GPIO_CLK_ENABLE(joykey); 00471 00472 GPIO_InitStruct.Pin = JOY_PIN[joykey]; 00473 GPIO_InitStruct.Pull = GPIO_PULLDOWN; 00474 GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_VERY_HIGH; 00475 00476 if (Joy_Mode == JOY_MODE_GPIO) 00477 { 00478 /* Configure Joy pin as input */ 00479 GPIO_InitStruct.Mode = GPIO_MODE_INPUT; 00480 HAL_GPIO_Init(JOY_PORT[joykey], &GPIO_InitStruct); 00481 } 00482 else if (Joy_Mode == JOY_MODE_EXTI) 00483 { 00484 /* Configure Joy pin as input with External interrupt */ 00485 GPIO_InitStruct.Mode = GPIO_MODE_IT_FALLING; 00486 HAL_GPIO_Init(JOY_PORT[joykey], &GPIO_InitStruct); 00487 00488 /* Enable and set Joy EXTI Interrupt to the lowest priority */ 00489 HAL_NVIC_SetPriority((IRQn_Type)(JOY_IRQn[joykey]), 0x0F, 0x00); 00490 HAL_NVIC_EnableIRQ((IRQn_Type)(JOY_IRQn[joykey])); 00491 } 00492 } 00493 00494 return HAL_OK; 00495 } 00496 00497 /** 00498 * @brief Unonfigures all GPIOs used as buttons of the joystick. 00499 * @retval None. 00500 */ 00501 void BSP_JOY_DeInit(void) 00502 { 00503 JOYState_TypeDef joykey; 00504 00505 /* Initialized the Joystick. */ 00506 for (joykey = JOY_SEL; joykey < (JOY_SEL + JOYn) ; joykey++) 00507 { 00508 /* Enable the JOY clock */ 00509 JOYx_GPIO_CLK_ENABLE(joykey); 00510 00511 HAL_GPIO_DeInit(JOY_PORT[joykey], JOY_PIN[joykey]); 00512 } 00513 } 00514 00515 /** 00516 * @brief Returns the current joystick status. 00517 * @retval Code of the joystick key pressed 00518 * This code can be one of the following values: 00519 * @arg JOY_NONE 00520 * @arg JOY_SEL 00521 * @arg JOY_DOWN 00522 * @arg JOY_LEFT 00523 * @arg JOY_RIGHT 00524 * @arg JOY_UP 00525 */ 00526 JOYState_TypeDef BSP_JOY_GetState(void) 00527 { 00528 JOYState_TypeDef joykey; 00529 00530 for (joykey = JOY_SEL; joykey < (JOY_SEL + JOYn) ; joykey++) 00531 { 00532 if (HAL_GPIO_ReadPin(JOY_PORT[joykey], JOY_PIN[joykey]) == GPIO_PIN_SET) 00533 { 00534 /* Return Code Joystick key pressed */ 00535 return joykey; 00536 } 00537 } 00538 00539 /* No Joystick key pressed */ 00540 return JOY_NONE; 00541 } 00542 00543 /** 00544 * @} 00545 */ 00546 00547 /** @defgroup STM32L476G_DISCOVERY_BusOperations_Functions Bus Operations Functions 00548 * @{ 00549 */ 00550 00551 /******************************************************************************* 00552 BUS OPERATIONS 00553 *******************************************************************************/ 00554 #if defined(HAL_SPI_MODULE_ENABLED) 00555 /******************************* SPI Routines**********************************/ 00556 /** 00557 * @brief SPIx Bus initialization 00558 * @retval None 00559 */ 00560 static void SPIx_Init(void) 00561 { 00562 if (HAL_SPI_GetState(&SpiHandle) == HAL_SPI_STATE_RESET) 00563 { 00564 /* SPI Config */ 00565 SpiHandle.Instance = DISCOVERY_SPIx; 00566 /* SPI baudrate is set to 10 MHz (PCLK1/SPI_BaudRatePrescaler = 80/8 = 10 MHz) 00567 to verify these constraints: 00568 lsm303c SPI interface max baudrate is 10MHz for write/read 00569 PCLK1 max frequency is set to 80 MHz 00570 */ 00571 SpiHandle.Init.BaudRatePrescaler = SPI_BAUDRATEPRESCALER_8; 00572 SpiHandle.Init.Direction = SPI_DIRECTION_2LINES; 00573 SpiHandle.Init.CLKPhase = SPI_PHASE_1EDGE; 00574 SpiHandle.Init.CLKPolarity = SPI_POLARITY_LOW; 00575 SpiHandle.Init.CRCCalculation = SPI_CRCCALCULATION_DISABLE; 00576 SpiHandle.Init.CRCPolynomial = 7; 00577 SpiHandle.Init.DataSize = SPI_DATASIZE_8BIT; 00578 SpiHandle.Init.FirstBit = SPI_FIRSTBIT_MSB; 00579 SpiHandle.Init.NSS = SPI_NSS_SOFT; 00580 SpiHandle.Init.TIMode = SPI_TIMODE_DISABLE; 00581 SpiHandle.Init.Mode = SPI_MODE_MASTER; 00582 00583 SPIx_MspInit(&SpiHandle); 00584 HAL_SPI_Init(&SpiHandle); 00585 } 00586 } 00587 00588 /** 00589 * @brief SPI MSP Init 00590 * @param hspi: SPI handle 00591 * @retval None 00592 */ 00593 static void SPIx_MspInit(SPI_HandleTypeDef *hspi) 00594 { 00595 GPIO_InitTypeDef GPIO_InitStructure; 00596 00597 /* Enable SPIx clock */ 00598 DISCOVERY_SPIx_CLOCK_ENABLE(); 00599 00600 /* enable SPIx gpio clock */ 00601 DISCOVERY_SPIx_GPIO_CLK_ENABLE(); 00602 00603 /* configure SPIx SCK, MOSI and MISO */ 00604 GPIO_InitStructure.Pin = (DISCOVERY_SPIx_SCK_PIN | DISCOVERY_SPIx_MOSI_PIN | DISCOVERY_SPIx_MISO_PIN); 00605 GPIO_InitStructure.Mode = GPIO_MODE_AF_PP; 00606 GPIO_InitStructure.Pull = GPIO_NOPULL; // GPIO_PULLDOWN; 00607 GPIO_InitStructure.Speed = GPIO_SPEED_FREQ_HIGH; 00608 GPIO_InitStructure.Alternate = DISCOVERY_SPIx_AF; 00609 HAL_GPIO_Init(DISCOVERY_SPIx_GPIO_PORT, &GPIO_InitStructure); 00610 } 00611 00612 /** 00613 * @brief SPIx Bus Deinitialization 00614 * @retval None 00615 */ 00616 void SPIx_DeInit(void) 00617 { 00618 if (HAL_SPI_GetState(&SpiHandle) != HAL_SPI_STATE_RESET) 00619 { 00620 /* SPI Deinit */ 00621 HAL_SPI_DeInit(&SpiHandle); 00622 SPIx_MspDeInit(); 00623 } 00624 } 00625 00626 /** 00627 * @brief SPI MSP DeInit 00628 * @retval None 00629 */ 00630 static void SPIx_MspDeInit(void) 00631 { 00632 /* enable SPIx gpio clock */ 00633 DISCOVERY_SPIx_GPIO_CLK_ENABLE(); 00634 00635 /* Unconfigure SPIx SCK, MOSI and MISO */ 00636 HAL_GPIO_DeInit(DISCOVERY_SPIx_GPIO_PORT, (DISCOVERY_SPIx_SCK_PIN | DISCOVERY_SPIx_MOSI_PIN | DISCOVERY_SPIx_MISO_PIN)); 00637 00638 DISCOVERY_SPIx_GPIO_FORCE_RESET(); 00639 DISCOVERY_SPIx_GPIO_RELEASE_RESET(); 00640 00641 /* Disable SPIx clock */ 00642 DISCOVERY_SPIx_CLOCK_DISABLE(); 00643 } 00644 00645 /** 00646 * @brief Sends a Byte through the SPI interface and return the Byte received 00647 * from the SPI bus. 00648 * @param Byte : Byte send. 00649 * @retval none. 00650 */ 00651 static uint8_t SPIx_WriteRead(uint8_t Byte) 00652 { 00653 uint8_t receivedbyte; 00654 00655 /* Enable the SPI */ 00656 __HAL_SPI_ENABLE(&SpiHandle); 00657 /* check TXE flag */ 00658 while ((SpiHandle.Instance->SR & SPI_FLAG_TXE) != SPI_FLAG_TXE); 00659 00660 /* Write the data */ 00661 *((__IO uint8_t *)&SpiHandle.Instance->DR) = Byte; 00662 00663 while ((SpiHandle.Instance->SR & SPI_FLAG_RXNE) != SPI_FLAG_RXNE); 00664 receivedbyte = *((__IO uint8_t *)&SpiHandle.Instance->DR); 00665 00666 /* Wait BSY flag */ 00667 while ((SpiHandle.Instance->SR & SPI_FLAG_FTLVL) != SPI_FTLVL_EMPTY); 00668 while ((SpiHandle.Instance->SR & SPI_FLAG_BSY) == SPI_FLAG_BSY); 00669 00670 /* disable the SPI */ 00671 __HAL_SPI_DISABLE(&SpiHandle); 00672 00673 return receivedbyte; 00674 } 00675 00676 /** 00677 * @brief Sends a Byte through the SPI interface. 00678 * @param Byte : Byte to send. 00679 * @retval none. 00680 */ 00681 static void SPIx_Write(uint8_t Byte) 00682 { 00683 /* Enable the SPI */ 00684 __HAL_SPI_ENABLE(&SpiHandle); 00685 /* check TXE flag */ 00686 while ((SpiHandle.Instance->SR & SPI_FLAG_TXE) != SPI_FLAG_TXE); 00687 00688 /* Write the data */ 00689 *((__IO uint8_t *)&SpiHandle.Instance->DR) = Byte; 00690 00691 /* Wait BSY flag */ 00692 while ((SpiHandle.Instance->SR & SPI_FLAG_BSY) == SPI_FLAG_BSY); 00693 00694 /* disable the SPI */ 00695 __HAL_SPI_DISABLE(&SpiHandle); 00696 } 00697 00698 #if defined(__ICCARM__) 00699 #pragma optimize=none 00700 #endif 00701 /** 00702 * @brief Receives a Byte from the SPI bus. 00703 * @retval The received byte value 00704 */ 00705 static uint8_t SPIx_Read(void) 00706 { 00707 uint8_t receivedbyte; 00708 00709 __HAL_SPI_ENABLE(&SpiHandle); 00710 __DSB(); 00711 __DSB(); 00712 __DSB(); 00713 __DSB(); 00714 __DSB(); 00715 __DSB(); 00716 __DSB(); 00717 __DSB(); 00718 __HAL_SPI_DISABLE(&SpiHandle); 00719 00720 while ((SpiHandle.Instance->SR & SPI_FLAG_RXNE) != SPI_FLAG_RXNE); 00721 /* read the received data */ 00722 receivedbyte = *(__IO uint8_t *)&SpiHandle.Instance->DR; 00723 00724 /* Wait for the BSY flag reset */ 00725 while ((SpiHandle.Instance->SR & SPI_FLAG_BSY) == SPI_FLAG_BSY); 00726 00727 00728 return receivedbyte; 00729 } 00730 #endif /* HAL_SPI_MODULE_ENABLED */ 00731 00732 00733 #if defined(HAL_I2C_MODULE_ENABLED) 00734 /******************************* I2C Routines**********************************/ 00735 /** 00736 * @brief Discovery I2C1 Bus initialization 00737 * @retval None 00738 */ 00739 static void I2C1_Init(void) 00740 { 00741 if (HAL_I2C_GetState(&I2c1Handle) == HAL_I2C_STATE_RESET) 00742 { 00743 I2c1Handle.Instance = DISCOVERY_I2C1; 00744 I2c1Handle.Init.Timing = DISCOVERY_I2C1_TIMING; 00745 I2c1Handle.Init.OwnAddress1 = 0; 00746 I2c1Handle.Init.AddressingMode = I2C_ADDRESSINGMODE_7BIT; 00747 I2c1Handle.Init.DualAddressMode = I2C_DUALADDRESS_DISABLE; 00748 I2c1Handle.Init.OwnAddress2 = 0; 00749 I2c1Handle.Init.GeneralCallMode = I2C_GENERALCALL_DISABLE; 00750 I2c1Handle.Init.NoStretchMode = I2C_NOSTRETCH_DISABLE; 00751 00752 /* Init the I2C */ 00753 I2C1_MspInit(&I2c1Handle); 00754 HAL_I2C_Init(&I2c1Handle); 00755 } 00756 } 00757 00758 /** 00759 * @brief Discovery I2C1 MSP Initialization 00760 * @param hi2c: I2C handle 00761 * @retval None 00762 */ 00763 static void I2C1_MspInit(I2C_HandleTypeDef *hi2c) 00764 { 00765 GPIO_InitTypeDef GPIO_InitStructure; 00766 RCC_PeriphCLKInitTypeDef RCC_PeriphCLKInitStruct; 00767 00768 /* IOSV bit MUST be set to access GPIO port G[2:15] */ 00769 __HAL_RCC_PWR_CLK_ENABLE(); 00770 HAL_PWREx_EnableVddIO2(); 00771 00772 if (hi2c->Instance == DISCOVERY_I2C1) 00773 { 00774 /*##-1- Configure the Discovery I2C clock source. The clock is derived from the SYSCLK #*/ 00775 RCC_PeriphCLKInitStruct.PeriphClockSelection = RCC_PERIPHCLK_I2C1; 00776 RCC_PeriphCLKInitStruct.I2c1ClockSelection = RCC_I2C1CLKSOURCE_SYSCLK; 00777 HAL_RCCEx_PeriphCLKConfig(&RCC_PeriphCLKInitStruct); 00778 00779 /*##-2- Configure the GPIOs ################################################*/ 00780 /* Enable GPIO clock */ 00781 DISCOVERY_I2C1_SDA_GPIO_CLK_ENABLE(); 00782 DISCOVERY_I2C1_SCL_GPIO_CLK_ENABLE(); 00783 00784 /* Configure I2C Rx/Tx as alternate function */ 00785 GPIO_InitStructure.Pin = DISCOVERY_I2C1_SCL_PIN | DISCOVERY_I2C1_SDA_PIN; 00786 GPIO_InitStructure.Mode = GPIO_MODE_AF_OD; 00787 GPIO_InitStructure.Pull = GPIO_PULLUP; 00788 GPIO_InitStructure.Speed = GPIO_SPEED_FREQ_VERY_HIGH; 00789 GPIO_InitStructure.Alternate = DISCOVERY_I2C1_SCL_SDA_AF; 00790 HAL_GPIO_Init(DISCOVERY_I2C1_SCL_GPIO_PORT, &GPIO_InitStructure); 00791 00792 /*##-3- Configure the Discovery I2C1 peripheral #######################################*/ 00793 /* Enable Discovery I2C1 clock */ 00794 DISCOVERY_I2C1_CLK_ENABLE(); 00795 00796 /* Force and release the I2C Peripheral Clock Reset */ 00797 DISCOVERY_I2C1_FORCE_RESET(); 00798 DISCOVERY_I2C1_RELEASE_RESET(); 00799 00800 /* Enable and set Discovery I2C1 Interrupt to the highest priority */ 00801 HAL_NVIC_SetPriority(DISCOVERY_I2C1_EV_IRQn, 0x00, 0); 00802 HAL_NVIC_EnableIRQ(DISCOVERY_I2C1_EV_IRQn); 00803 00804 /* Enable and set Discovery I2C1 Interrupt to the highest priority */ 00805 HAL_NVIC_SetPriority(DISCOVERY_I2C1_ER_IRQn, 0x00, 0); 00806 HAL_NVIC_EnableIRQ(DISCOVERY_I2C1_ER_IRQn); 00807 } 00808 } 00809 00810 /** 00811 * @brief Discovery I2C1 Bus Deitialization 00812 * @retval None 00813 */ 00814 static void I2C1_DeInit(void) 00815 { 00816 if (HAL_I2C_GetState(&I2c1Handle) != HAL_I2C_STATE_RESET) 00817 { 00818 /* Deinit the I2C */ 00819 HAL_I2C_DeInit(&I2c1Handle); 00820 I2C1_MspDeInit(&I2c1Handle); 00821 } 00822 } 00823 00824 /** 00825 * @brief Discovery I2C1 MSP Deinitialization 00826 * @param hi2c: I2C handle 00827 * @retval None 00828 */ 00829 static void I2C1_MspDeInit(I2C_HandleTypeDef *hi2c) 00830 { 00831 if (hi2c->Instance == DISCOVERY_I2C1) 00832 { 00833 /*##-1- Unconfigure the GPIOs ################################################*/ 00834 /* Enable GPIO clock */ 00835 DISCOVERY_I2C1_SDA_GPIO_CLK_ENABLE(); 00836 DISCOVERY_I2C1_SCL_GPIO_CLK_ENABLE(); 00837 00838 /* Deinit Rx/Tx pins */ 00839 HAL_GPIO_DeInit(DISCOVERY_I2C1_SCL_GPIO_PORT, (DISCOVERY_I2C1_SCL_PIN | DISCOVERY_I2C1_SDA_PIN)); 00840 00841 /*##-2- Unconfigure the Discovery I2C1 peripheral ############################*/ 00842 /* Force & Release the I2C Peripheral Clock Reset */ 00843 DISCOVERY_I2C1_FORCE_RESET(); 00844 DISCOVERY_I2C1_RELEASE_RESET(); 00845 00846 /* Disable Discovery I2C1 clock */ 00847 DISCOVERY_I2C1_CLK_DISABLE(); 00848 00849 /* Disable Discovery I2C1 interrupts */ 00850 HAL_NVIC_DisableIRQ(DISCOVERY_I2C1_EV_IRQn); 00851 HAL_NVIC_DisableIRQ(DISCOVERY_I2C1_ER_IRQn); 00852 00853 __HAL_RCC_PWR_CLK_ENABLE(); 00854 HAL_PWREx_DisableVddIO2(); 00855 } 00856 } 00857 00858 /** 00859 * @brief Write a value in a register of the device through BUS. 00860 * @param Addr: Device address on BUS Bus. 00861 * @param Reg: The target register address to write 00862 * @param RegSize: The target register size (can be 8BIT or 16BIT) 00863 * @param pBuffer: The target register value to be written 00864 * @param Length: buffer size to be written 00865 * @retval None 00866 */ 00867 static HAL_StatusTypeDef I2C1_WriteBuffer(uint16_t Addr, uint16_t Reg, uint16_t RegSize, uint8_t *pBuffer, uint16_t Length) 00868 { 00869 HAL_StatusTypeDef status = HAL_OK; 00870 00871 status = HAL_I2C_Mem_Write(&I2c1Handle, Addr, (uint16_t)Reg, RegSize, pBuffer, Length, I2c1Timeout); 00872 00873 /* Check the communication status */ 00874 if (status != HAL_OK) 00875 { 00876 /* Re-Initiaize the BUS */ 00877 I2C1_Error(); 00878 } 00879 return status; 00880 } 00881 00882 /** 00883 * @brief Reads multiple data on the BUS. 00884 * @param Addr: I2C Address 00885 * @param Reg: Reg Address 00886 * @param RegSize : The target register size (can be 8BIT or 16BIT) 00887 * @param pBuffer: pointer to read data buffer 00888 * @param Length: length of the data 00889 * @retval 0 if no problems to read multiple data 00890 */ 00891 static HAL_StatusTypeDef I2C1_ReadBuffer(uint16_t Addr, uint16_t Reg, uint16_t RegSize, uint8_t *pBuffer, uint16_t Length) 00892 { 00893 HAL_StatusTypeDef status = HAL_OK; 00894 00895 status = HAL_I2C_Mem_Read(&I2c1Handle, Addr, (uint16_t)Reg, RegSize, pBuffer, Length, I2c1Timeout); 00896 00897 /* Check the communication status */ 00898 if (status != HAL_OK) 00899 { 00900 /* Re-Initiaize the BUS */ 00901 I2C1_Error(); 00902 } 00903 return status; 00904 } 00905 00906 /** 00907 * @brief Discovery I2C1 error treatment function 00908 * @retval None 00909 */ 00910 static void I2C1_Error(void) 00911 { 00912 /* De-initialize the I2C communication BUS */ 00913 HAL_I2C_DeInit(&I2c1Handle); 00914 00915 /* Re- Initiaize the I2C communication BUS */ 00916 I2C1_Init(); 00917 } 00918 00919 /** 00920 * @brief Discovery I2C2 Bus initialization 00921 * @retval None 00922 */ 00923 static void I2C2_Init(void) 00924 { 00925 if (HAL_I2C_GetState(&I2c2Handle) == HAL_I2C_STATE_RESET) 00926 { 00927 I2c2Handle.Instance = DISCOVERY_I2C2; 00928 I2c2Handle.Init.Timing = DISCOVERY_I2C2_TIMING; 00929 I2c2Handle.Init.OwnAddress1 = 0; 00930 I2c2Handle.Init.AddressingMode = I2C_ADDRESSINGMODE_7BIT; 00931 I2c2Handle.Init.DualAddressMode = I2C_DUALADDRESS_DISABLE; 00932 I2c2Handle.Init.OwnAddress2 = 0; 00933 I2c2Handle.Init.GeneralCallMode = I2C_GENERALCALL_DISABLE; 00934 I2c2Handle.Init.NoStretchMode = I2C_NOSTRETCH_DISABLE; 00935 00936 /* Init the I2C */ 00937 I2C2_MspInit(&I2c2Handle); 00938 HAL_I2C_Init(&I2c2Handle); 00939 } 00940 } 00941 00942 /** 00943 * @brief Discovery I2C2 MSP Initialization 00944 * @param hi2c: I2C2 handle 00945 * @retval None 00946 */ 00947 static void I2C2_MspInit(I2C_HandleTypeDef *hi2c) 00948 { 00949 GPIO_InitTypeDef GPIO_InitStructure; 00950 RCC_PeriphCLKInitTypeDef RCC_PeriphCLKInitStruct; 00951 00952 if (hi2c->Instance == DISCOVERY_I2C2) 00953 { 00954 /*##-1- Configure the Discovery I2C2 clock source. The clock is derived from the SYSCLK #*/ 00955 RCC_PeriphCLKInitStruct.PeriphClockSelection = RCC_PERIPHCLK_I2C2; 00956 RCC_PeriphCLKInitStruct.I2c2ClockSelection = RCC_I2C2CLKSOURCE_SYSCLK; 00957 HAL_RCCEx_PeriphCLKConfig(&RCC_PeriphCLKInitStruct); 00958 00959 /*##-2- Configure the GPIOs ################################################*/ 00960 /* Enable GPIO clock */ 00961 DISCOVERY_I2C2_SDA_GPIO_CLK_ENABLE(); 00962 DISCOVERY_I2C2_SCL_GPIO_CLK_ENABLE(); 00963 00964 /* Configure I2C Rx/Tx as alternate function */ 00965 GPIO_InitStructure.Pin = DISCOVERY_I2C2_SCL_PIN | DISCOVERY_I2C2_SDA_PIN; 00966 GPIO_InitStructure.Mode = GPIO_MODE_AF_OD; 00967 GPIO_InitStructure.Pull = GPIO_PULLUP; 00968 GPIO_InitStructure.Speed = GPIO_SPEED_FREQ_VERY_HIGH; 00969 GPIO_InitStructure.Alternate = DISCOVERY_I2C2_SCL_SDA_AF; 00970 HAL_GPIO_Init(DISCOVERY_I2C2_SCL_GPIO_PORT, &GPIO_InitStructure); 00971 00972 /*##-3- Configure the Discovery I2C2 peripheral #############################*/ 00973 /* Enable Discovery_I2C2 clock */ 00974 DISCOVERY_I2C2_CLK_ENABLE(); 00975 00976 /* Force and release the I2C Peripheral Clock Reset */ 00977 DISCOVERY_I2C2_FORCE_RESET(); 00978 DISCOVERY_I2C2_RELEASE_RESET(); 00979 00980 /* Enable and set Discovery I2C2 Interrupt to the highest priority */ 00981 HAL_NVIC_SetPriority(DISCOVERY_I2C2_EV_IRQn, 0x00, 0); 00982 HAL_NVIC_EnableIRQ(DISCOVERY_I2C2_EV_IRQn); 00983 00984 /* Enable and set Discovery I2C2 Interrupt to the highest priority */ 00985 HAL_NVIC_SetPriority(DISCOVERY_I2C2_ER_IRQn, 0x00, 0); 00986 HAL_NVIC_EnableIRQ(DISCOVERY_I2C2_ER_IRQn); 00987 } 00988 } 00989 00990 /** 00991 * @brief Discovery I2C2 Bus Deinitialization 00992 * @retval None 00993 */ 00994 static void I2C2_DeInit(void) 00995 { 00996 if (HAL_I2C_GetState(&I2c2Handle) != HAL_I2C_STATE_RESET) 00997 { 00998 /* DeInit the I2C */ 00999 HAL_I2C_DeInit(&I2c2Handle); 01000 I2C2_MspDeInit(&I2c2Handle); 01001 } 01002 } 01003 01004 /** 01005 * @brief Discovery I2C2 MSP DeInitialization 01006 * @param hi2c: I2C2 handle 01007 * @retval None 01008 */ 01009 static void I2C2_MspDeInit(I2C_HandleTypeDef *hi2c) 01010 { 01011 if (hi2c->Instance == DISCOVERY_I2C2) 01012 { 01013 /*##-1- Unconfigure the GPIOs ################################################*/ 01014 /* Enable GPIO clock */ 01015 DISCOVERY_I2C2_SDA_GPIO_CLK_ENABLE(); 01016 DISCOVERY_I2C2_SCL_GPIO_CLK_ENABLE(); 01017 01018 /* Configure I2C Rx/Tx as alternate function */ 01019 HAL_GPIO_DeInit(DISCOVERY_I2C2_SCL_GPIO_PORT, (DISCOVERY_I2C2_SCL_PIN | DISCOVERY_I2C2_SDA_PIN)); 01020 01021 /*##-2- Unconfigure the Discovery I2C2 peripheral ############################*/ 01022 /* Force and release I2C Peripheral */ 01023 DISCOVERY_I2C2_FORCE_RESET(); 01024 DISCOVERY_I2C2_RELEASE_RESET(); 01025 01026 /* Disable Discovery I2C2 clock */ 01027 DISCOVERY_I2C2_CLK_DISABLE(); 01028 01029 /* Disable Discovery I2C2 interrupts */ 01030 HAL_NVIC_DisableIRQ(DISCOVERY_I2C2_EV_IRQn); 01031 HAL_NVIC_DisableIRQ(DISCOVERY_I2C2_ER_IRQn); 01032 } 01033 } 01034 01035 /** 01036 * @brief Write a value in a register of the device through BUS. 01037 * @param Addr: Device address on BUS Bus. 01038 * @param Reg: The target register address to write 01039 * @param RegSize: The target register size (can be 8BIT or 16BIT) 01040 * @param Value: The target register value to be written 01041 * @retval None 01042 */ 01043 static void I2C2_WriteData(uint16_t Addr, uint16_t Reg, uint16_t RegSize, uint8_t Value) 01044 { 01045 HAL_StatusTypeDef status = HAL_OK; 01046 01047 status = HAL_I2C_Mem_Write(&I2c2Handle, Addr, (uint16_t)Reg, RegSize, &Value, 1, I2c2Timeout); 01048 01049 /* Check the communication status */ 01050 if (status != HAL_OK) 01051 { 01052 /* Re-Initiaize the BUS */ 01053 I2C2_Error(); 01054 } 01055 } 01056 01057 /** 01058 * @brief Write a value in a register of the device through BUS. 01059 * @param Addr: Device address on BUS Bus. 01060 * @param Reg: The target register address to write 01061 * @param RegSize: The target register size (can be 8BIT or 16BIT) 01062 * @param pBuffer: The target register value to be written 01063 * @param Length: buffer size to be written 01064 * @retval None 01065 */ 01066 static HAL_StatusTypeDef I2C2_WriteBuffer(uint16_t Addr, uint16_t Reg, uint16_t RegSize, uint8_t *pBuffer, uint16_t Length) 01067 { 01068 HAL_StatusTypeDef status = HAL_OK; 01069 01070 status = HAL_I2C_Mem_Write(&I2c2Handle, Addr, (uint16_t)Reg, RegSize, pBuffer, Length, I2c2Timeout); 01071 01072 /* Check the communication status */ 01073 if (status != HAL_OK) 01074 { 01075 /* Re-Initiaize the BUS */ 01076 I2C2_Error(); 01077 } 01078 01079 return status; 01080 } 01081 01082 /** 01083 * @brief Read a register of the device through BUS 01084 * @param Addr: Device address on BUS 01085 * @param Reg: The target register address to read 01086 * @param RegSize: The target register size (can be 8BIT or 16BIT) 01087 * @retval read register value 01088 */ 01089 static uint8_t I2C2_ReadData(uint16_t Addr, uint16_t Reg, uint16_t RegSize) 01090 { 01091 HAL_StatusTypeDef status = HAL_OK; 01092 uint8_t value = 0x0; 01093 01094 status = HAL_I2C_Mem_Read(&I2c2Handle, Addr, Reg, RegSize, &value, 1, I2c2Timeout); 01095 01096 /* Check the communication status */ 01097 if (status != HAL_OK) 01098 { 01099 /* Re-Initiaize the BUS */ 01100 I2C2_Error(); 01101 } 01102 01103 return value; 01104 } 01105 01106 /** 01107 * @brief Reads multiple data on the BUS. 01108 * @param Addr: I2C Address 01109 * @param Reg: Reg Address 01110 * @param RegSize : The target register size (can be 8BIT or 16BIT) 01111 * @param pBuffer: pointer to read data buffer 01112 * @param Length: length of the data 01113 * @retval 0 if no problems to read multiple data 01114 */ 01115 static HAL_StatusTypeDef I2C2_ReadBuffer(uint16_t Addr, uint16_t Reg, uint16_t RegSize, uint8_t *pBuffer, uint16_t Length) 01116 { 01117 HAL_StatusTypeDef status = HAL_OK; 01118 01119 status = HAL_I2C_Mem_Read(&I2c2Handle, Addr, (uint16_t)Reg, RegSize, pBuffer, Length, I2c2Timeout); 01120 01121 /* Check the communication status */ 01122 if (status != HAL_OK) 01123 { 01124 /* Re-Initiaize the BUS */ 01125 I2C2_Error(); 01126 } 01127 01128 return status; 01129 } 01130 01131 /** 01132 * @brief Discovery I2C2 error treatment function 01133 * @retval None 01134 */ 01135 static void I2C2_Error(void) 01136 { 01137 /* De-initialize the I2C communication BUS */ 01138 HAL_I2C_DeInit(&I2c2Handle); 01139 01140 /* Re- Initiaize the I2C communication BUS */ 01141 I2C2_Init(); 01142 } 01143 #endif /*HAL_I2C_MODULE_ENABLED*/ 01144 01145 01146 /******************************************************************************* 01147 LINK OPERATIONS 01148 *******************************************************************************/ 01149 #if defined(HAL_SPI_MODULE_ENABLED) 01150 /*********************** LINK ACCELEROMETER ***********************************/ 01151 /** 01152 * @brief Configures COMPASS/ACCELEROMETER io interface. 01153 * @retval None 01154 */ 01155 void ACCELERO_IO_Init(void) 01156 { 01157 GPIO_InitTypeDef GPIO_InitStructure; 01158 01159 /* Enable CS GPIO clock and Configure GPIO PIN for Gyroscope Chip select */ 01160 ACCELERO_CS_GPIO_CLK_ENABLE(); 01161 GPIO_InitStructure.Pin = ACCELERO_CS_PIN; 01162 GPIO_InitStructure.Mode = GPIO_MODE_OUTPUT_PP; 01163 GPIO_InitStructure.Pull = GPIO_NOPULL; 01164 GPIO_InitStructure.Speed = GPIO_SPEED_FREQ_VERY_HIGH; 01165 HAL_GPIO_Init(ACCELERO_CS_GPIO_PORT, &GPIO_InitStructure); 01166 01167 /* Deselect : Chip Select high */ 01168 ACCELERO_CS_HIGH(); 01169 01170 SPIx_Init(); 01171 } 01172 01173 /** 01174 * @brief De-Configures COMPASS/ACCELEROMETER io interface. 01175 * @retval None 01176 */ 01177 void ACCELERO_IO_DeInit(void) 01178 { 01179 GPIO_InitTypeDef GPIO_InitStructure; 01180 01181 /* Enable CS GPIO clock and Configure GPIO PIN for Gyroscope Chip select */ 01182 ACCELERO_CS_GPIO_CLK_ENABLE(); 01183 GPIO_InitStructure.Pin = ACCELERO_CS_PIN; 01184 GPIO_InitStructure.Mode = GPIO_MODE_OUTPUT_PP; 01185 GPIO_InitStructure.Pull = GPIO_NOPULL; 01186 GPIO_InitStructure.Speed = GPIO_SPEED_FREQ_VERY_HIGH; 01187 HAL_GPIO_Init(ACCELERO_CS_GPIO_PORT, &GPIO_InitStructure); 01188 01189 /* Deselect : Chip Select high */ 01190 ACCELERO_CS_HIGH(); 01191 01192 /* Uninitialize SPI bus */ 01193 SPIx_DeInit(); 01194 } 01195 01196 /** 01197 * @brief Configures COMPASS / ACCELERO click IT 01198 * @retval None 01199 */ 01200 void ACCELERO_IO_ITConfig(void) 01201 { 01202 } 01203 01204 /** 01205 * @brief Writes one byte to the COMPASS / ACCELEROMETER. 01206 * @param RegisterAddr specifies the COMPASS / ACCELEROMETER register to be written. 01207 * @param Value : Data to be written 01208 * @retval None 01209 */ 01210 void ACCELERO_IO_Write(uint8_t RegisterAddr, uint8_t Value) 01211 { 01212 ACCELERO_CS_LOW(); 01213 __SPI_DIRECTION_1LINE_TX(&SpiHandle); 01214 /* call SPI Read data bus function */ 01215 SPIx_Write(RegisterAddr); 01216 SPIx_Write(Value); 01217 ACCELERO_CS_HIGH(); 01218 } 01219 01220 /** 01221 * @brief Reads a block of data from the COMPASS / ACCELEROMETER. 01222 * @param RegisterAddr : specifies the COMPASS / ACCELEROMETER internal address register to read from 01223 * @retval ACCELEROMETER register value 01224 */ 01225 uint8_t ACCELERO_IO_Read(uint8_t RegisterAddr) 01226 { 01227 RegisterAddr = RegisterAddr | ((uint8_t)0x80); 01228 ACCELERO_CS_LOW(); 01229 __SPI_DIRECTION_1LINE_TX(&SpiHandle); 01230 SPIx_Write(RegisterAddr); 01231 __SPI_DIRECTION_1LINE_RX(&SpiHandle); 01232 uint8_t val = SPIx_Read(); 01233 ACCELERO_CS_HIGH(); 01234 return val; 01235 } 01236 01237 /********************************* LINK MAGNETO *******************************/ 01238 /** 01239 * @brief Configures COMPASS/MAGNETO SPI interface. 01240 * @retval None 01241 */ 01242 void MAGNETO_IO_Init(void) 01243 { 01244 GPIO_InitTypeDef GPIO_InitStructure; 01245 01246 /* Enable CS GPIO clock and Configure GPIO PIN for Gyroscope Chip select */ 01247 MAGNETO_CS_GPIO_CLK_ENABLE(); 01248 GPIO_InitStructure.Pin = MAGNETO_CS_PIN; 01249 GPIO_InitStructure.Mode = GPIO_MODE_OUTPUT_PP; 01250 GPIO_InitStructure.Pull = GPIO_NOPULL; 01251 GPIO_InitStructure.Speed = GPIO_SPEED_FREQ_VERY_HIGH; 01252 HAL_GPIO_Init(MAGNETO_CS_GPIO_PORT, &GPIO_InitStructure); 01253 01254 /* Deselect : Chip Select high */ 01255 MAGNETO_CS_HIGH(); 01256 01257 SPIx_Init(); 01258 } 01259 01260 /** 01261 * @brief de-Configures COMPASS/MAGNETO SPI interface. 01262 * @retval None 01263 */ 01264 void MAGNETO_IO_DeInit(void) 01265 { 01266 GPIO_InitTypeDef GPIO_InitStructure; 01267 01268 /* Enable CS GPIO clock and Configure GPIO PIN for Gyroscope Chip select */ 01269 MAGNETO_CS_GPIO_CLK_ENABLE(); 01270 GPIO_InitStructure.Pin = MAGNETO_CS_PIN; 01271 GPIO_InitStructure.Mode = GPIO_MODE_OUTPUT_PP; 01272 GPIO_InitStructure.Pull = GPIO_NOPULL; 01273 GPIO_InitStructure.Speed = GPIO_SPEED_FREQ_VERY_HIGH; 01274 HAL_GPIO_Init(MAGNETO_CS_GPIO_PORT, &GPIO_InitStructure); 01275 01276 /* Deselect : Chip Select high */ 01277 MAGNETO_CS_HIGH(); 01278 01279 HAL_GPIO_DeInit(MAGNETO_CS_GPIO_PORT, MAGNETO_INT1_PIN | MAGNETO_DRDY_PIN); 01280 01281 01282 /* Uninitialize SPI bus */ 01283 SPIx_DeInit(); 01284 } 01285 01286 /** 01287 * @brief Writes one byte to the COMPASS/MAGNETO. 01288 * @param RegisterAddr specifies the COMPASS/MAGNETO register to be written. 01289 * @param Value : Data to be written 01290 * @retval None 01291 */ 01292 void MAGNETO_IO_Write(uint8_t RegisterAddr, uint8_t Value) 01293 { 01294 MAGNETO_CS_LOW(); 01295 __SPI_DIRECTION_1LINE_TX(&SpiHandle); 01296 /* call SPI Read data bus function */ 01297 SPIx_Write(RegisterAddr); 01298 SPIx_Write(Value); 01299 MAGNETO_CS_HIGH(); 01300 } 01301 01302 /** 01303 * @brief Reads a block of data from the COMPASS/MAGNETO. 01304 * @param RegisterAddr : specifies the COMPASS/MAGNETO internal address register to read from 01305 * @retval ACCELEROMETER register value 01306 */ 01307 uint8_t MAGNETO_IO_Read(uint8_t RegisterAddr) 01308 { 01309 MAGNETO_CS_LOW(); 01310 __SPI_DIRECTION_1LINE_TX(&SpiHandle); 01311 SPIx_Write(RegisterAddr | 0x80); 01312 __SPI_DIRECTION_1LINE_RX(&SpiHandle); 01313 uint8_t val = SPIx_Read(); 01314 MAGNETO_CS_HIGH(); 01315 return val; 01316 } 01317 01318 /********************************* LINK GYRO *****************************/ 01319 /** 01320 * @brief Configures the GYRO SPI interface. 01321 * @retval None 01322 */ 01323 void GYRO_IO_Init(void) 01324 { 01325 GPIO_InitTypeDef GPIO_InitStructure; 01326 01327 01328 /* Case GYRO not used in the demonstration software except being set in 01329 low power mode. 01330 To avoid access conflicts with accelerometer and magnetometer, 01331 initialize XL_CS and MAG_CS pins then deselect these I/O */ 01332 ACCELERO_CS_GPIO_CLK_ENABLE(); 01333 GPIO_InitStructure.Pin = ACCELERO_CS_PIN; 01334 GPIO_InitStructure.Mode = GPIO_MODE_OUTPUT_PP; 01335 GPIO_InitStructure.Pull = GPIO_NOPULL; 01336 GPIO_InitStructure.Speed = GPIO_SPEED_FREQ_VERY_HIGH; 01337 HAL_GPIO_Init(ACCELERO_CS_GPIO_PORT, &GPIO_InitStructure); 01338 01339 /* Deselect : Chip Select high */ 01340 ACCELERO_CS_HIGH(); 01341 01342 /* Enable CS GPIO clock and Configure GPIO PIN for Gyroscope Chip select */ 01343 MAGNETO_CS_GPIO_CLK_ENABLE(); 01344 GPIO_InitStructure.Pin = MAGNETO_CS_PIN; 01345 GPIO_InitStructure.Mode = GPIO_MODE_OUTPUT_PP; 01346 GPIO_InitStructure.Pull = GPIO_NOPULL; 01347 GPIO_InitStructure.Speed = GPIO_SPEED_FREQ_VERY_HIGH; 01348 HAL_GPIO_Init(MAGNETO_CS_GPIO_PORT, &GPIO_InitStructure); 01349 01350 /* Deselect : Chip Select high */ 01351 MAGNETO_CS_HIGH(); 01352 01353 01354 /* Configure the Gyroscope Control pins ---------------------------------*/ 01355 /* Enable CS GPIO clock and Configure GPIO PIN for Gyroscope Chip select */ 01356 GYRO_CS_GPIO_CLK_ENABLE(); 01357 GPIO_InitStructure.Pin = GYRO_CS_PIN; 01358 GPIO_InitStructure.Mode = GPIO_MODE_OUTPUT_PP; 01359 GPIO_InitStructure.Pull = GPIO_NOPULL; 01360 GPIO_InitStructure.Speed = GPIO_SPEED_FREQ_VERY_HIGH; 01361 HAL_GPIO_Init(GYRO_CS_GPIO_PORT, &GPIO_InitStructure); 01362 01363 /* Deselect : Chip Select high */ 01364 GYRO_CS_HIGH(); 01365 01366 /* Enable INT1, INT2 GPIO clock and Configure GPIO PINs to detect Interrupts */ 01367 GYRO_INT1_GPIO_CLK_ENABLE(); 01368 GPIO_InitStructure.Pin = GYRO_INT1_PIN; 01369 GPIO_InitStructure.Mode = GPIO_MODE_INPUT; 01370 GPIO_InitStructure.Speed = GPIO_SPEED_FREQ_VERY_HIGH; 01371 GPIO_InitStructure.Pull = GPIO_NOPULL; 01372 HAL_GPIO_Init(GYRO_INT1_GPIO_PORT, &GPIO_InitStructure); 01373 01374 GYRO_INT2_GPIO_CLK_ENABLE(); 01375 GPIO_InitStructure.Pin = GYRO_INT2_PIN; 01376 HAL_GPIO_Init(GYRO_INT2_GPIO_PORT, &GPIO_InitStructure); 01377 01378 SPIx_Init(); 01379 01380 } 01381 01382 01383 /** 01384 * @brief de-Configures GYRO SPI interface. 01385 * @retval None 01386 */ 01387 void GYRO_IO_DeInit(void) 01388 { 01389 GPIO_InitTypeDef GPIO_InitStructure; 01390 /* Enable CS GPIO clock */ 01391 GYRO_CS_GPIO_CLK_ENABLE(); 01392 01393 GPIO_InitStructure.Pin = GYRO_CS_PIN; 01394 GPIO_InitStructure.Mode = GPIO_MODE_OUTPUT_PP; 01395 GPIO_InitStructure.Pull = GPIO_NOPULL; 01396 GPIO_InitStructure.Speed = GPIO_SPEED_FREQ_VERY_HIGH; 01397 HAL_GPIO_Init(GYRO_CS_GPIO_PORT, &GPIO_InitStructure); 01398 01399 /* Deselect : Chip Select high */ 01400 GYRO_CS_HIGH(); 01401 01402 GYRO_INT1_GPIO_CLK_ENABLE(); 01403 GYRO_INT2_GPIO_CLK_ENABLE(); 01404 01405 /* Uninitialize the INT1/INT2 Pins */ 01406 HAL_GPIO_DeInit(GYRO_INT1_GPIO_PORT, GYRO_INT1_PIN); 01407 HAL_GPIO_DeInit(GYRO_INT2_GPIO_PORT, GYRO_INT2_PIN); 01408 01409 /* Uninitialize SPI bus */ 01410 SPIx_DeInit(); 01411 } 01412 01413 /** 01414 * @brief Writes one byte to the GYRO. 01415 * @param pBuffer : pointer to the buffer containing the data to be written to the GYRO. 01416 * @param WriteAddr : GYRO's internal address to write to. 01417 * @param NumByteToWrite: Number of bytes to write. 01418 * @retval None 01419 */ 01420 void GYRO_IO_Write(uint8_t *pBuffer, uint8_t WriteAddr, uint16_t NumByteToWrite) 01421 { 01422 /* Configure the MS bit: 01423 - When 0, the address will remain unchanged in multiple read/write commands. 01424 - When 1, the address will be auto incremented in multiple read/write commands. 01425 */ 01426 if (NumByteToWrite > 0x01) 01427 { 01428 WriteAddr |= (uint8_t)MULTIPLEBYTE_CMD; 01429 } 01430 /* Set chip select Low at the start of the transmission */ 01431 GYRO_CS_LOW(); 01432 __SPI_DIRECTION_2LINES(&SpiHandle); 01433 01434 /* Send the Address of the indexed register */ 01435 SPIx_WriteRead(WriteAddr); 01436 01437 /* Send the data that will be written into the device (MSB First) */ 01438 while (NumByteToWrite >= 0x01) 01439 { 01440 SPIx_WriteRead(*pBuffer); 01441 NumByteToWrite--; 01442 pBuffer++; 01443 } 01444 01445 /* Set chip select High at the end of the transmission */ 01446 GYRO_CS_HIGH(); 01447 } 01448 01449 /** 01450 * @brief Reads a block of data from the GYROSCOPE. 01451 * @param pBuffer : pointer to the buffer that receives the data read from the GYROSCOPE. 01452 * @param ReadAddr : GYROSCOPE's internal address to read from. 01453 * @param NumByteToRead : number of bytes to read from the GYROSCOPE. 01454 * @retval None 01455 */ 01456 void GYRO_IO_Read(uint8_t *pBuffer, uint8_t ReadAddr, uint16_t NumByteToRead) 01457 { 01458 if (NumByteToRead > 0x01) 01459 { 01460 ReadAddr |= (uint8_t)(READWRITE_CMD | MULTIPLEBYTE_CMD); 01461 } 01462 else 01463 { 01464 ReadAddr |= (uint8_t)READWRITE_CMD; 01465 } 01466 /* Set chip select Low at the start of the transmission */ 01467 GYRO_CS_LOW(); 01468 __SPI_DIRECTION_2LINES(&SpiHandle); 01469 /* Send the Address of the indexed register */ 01470 SPIx_WriteRead(ReadAddr); 01471 01472 /* Receive the data that will be read from the device (MSB First) */ 01473 while (NumByteToRead > 0x00) 01474 { 01475 /* Send dummy byte (0x00) to generate the SPI clock to GYROSCOPE (Slave device) */ 01476 *pBuffer = SPIx_WriteRead(0x00); 01477 NumByteToRead--; 01478 pBuffer++; 01479 } 01480 01481 /* Set chip select High at the end of the transmission */ 01482 GYRO_CS_HIGH(); 01483 } 01484 #endif /* HAL_SPI_MODULE_ENABLED */ 01485 01486 #if defined(HAL_I2C_MODULE_ENABLED) 01487 /********************************* LINK MFX ***********************************/ 01488 /** 01489 * @brief Initializes MFX low level. 01490 * @retval None 01491 */ 01492 void MFX_IO_Init(void) 01493 { 01494 /* I2C2 init */ 01495 I2C2_Init(); 01496 } 01497 /** 01498 * @brief Deinitializes MFX low level. 01499 * @retval None 01500 */ 01501 void MFX_IO_DeInit(void) 01502 { 01503 GPIO_InitTypeDef GPIO_InitStruct; 01504 01505 /* Enable wakeup gpio clock */ 01506 IDD_WAKEUP_GPIO_CLK_ENABLE(); 01507 01508 /* MFX wakeup pin configuration */ 01509 GPIO_InitStruct.Pin = IDD_WAKEUP_PIN; 01510 GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP; 01511 GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW; 01512 GPIO_InitStruct.Pull = GPIO_PULLDOWN; 01513 HAL_GPIO_Init(IDD_WAKEUP_GPIO_PORT, &GPIO_InitStruct); 01514 01515 /* DeInit interrupt pin : disable IRQ before to avoid spurious interrupt */ 01516 HAL_NVIC_DisableIRQ((IRQn_Type)(IDD_INT_EXTI_IRQn)); 01517 IDD_INT_GPIO_CLK_ENABLE(); 01518 HAL_GPIO_DeInit(IDD_INT_GPIO_PORT, IDD_INT_PIN); 01519 01520 /* I2C2 Deinit */ 01521 I2C2_DeInit(); 01522 } 01523 01524 /** 01525 * @brief Configures MFX low level interrupt. 01526 * @retval None 01527 */ 01528 void MFX_IO_ITConfig(void) 01529 { 01530 GPIO_InitTypeDef GPIO_InitStruct; 01531 01532 /* Enable the GPIO clock */ 01533 IDD_INT_GPIO_CLK_ENABLE(); 01534 01535 /* MFX_OUT_IRQ (normally used for EXTI_WKUP) */ 01536 GPIO_InitStruct.Pin = IDD_INT_PIN; 01537 GPIO_InitStruct.Pull = GPIO_PULLDOWN; 01538 GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_VERY_HIGH; 01539 GPIO_InitStruct.Mode = GPIO_MODE_IT_RISING; 01540 HAL_GPIO_Init(IDD_INT_GPIO_PORT, &GPIO_InitStruct); 01541 01542 /* Enable and set GPIO EXTI Interrupt to the lowest priority */ 01543 HAL_NVIC_SetPriority((IRQn_Type)(IDD_INT_EXTI_IRQn), 0x0F, 0x0F); 01544 HAL_NVIC_EnableIRQ((IRQn_Type)(IDD_INT_EXTI_IRQn)); 01545 } 01546 01547 /** 01548 * @brief Configures MFX wke up pin. 01549 * @retval None 01550 */ 01551 void MFX_IO_EnableWakeupPin(void) 01552 { 01553 GPIO_InitTypeDef GPIO_InitStruct; 01554 01555 /* Enable wakeup gpio clock */ 01556 IDD_WAKEUP_GPIO_CLK_ENABLE(); 01557 01558 /* MFX wakeup pin configuration */ 01559 GPIO_InitStruct.Pin = IDD_WAKEUP_PIN; 01560 GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP; 01561 GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_HIGH; 01562 GPIO_InitStruct.Pull = GPIO_NOPULL; 01563 HAL_GPIO_Init(IDD_WAKEUP_GPIO_PORT, &GPIO_InitStruct); 01564 } 01565 01566 /** 01567 * @brief Wakeup MFX. 01568 * @retval None 01569 */ 01570 void MFX_IO_Wakeup(void) 01571 { 01572 /* Set Wakeup pin to high to wakeup Idd measurement component from standby mode */ 01573 HAL_GPIO_WritePin(IDD_WAKEUP_GPIO_PORT, IDD_WAKEUP_PIN, GPIO_PIN_SET); 01574 01575 /* Wait */ 01576 HAL_Delay(1); 01577 01578 /* Set gpio pin basck to low */ 01579 HAL_GPIO_WritePin(IDD_WAKEUP_GPIO_PORT, IDD_WAKEUP_PIN, GPIO_PIN_RESET); 01580 } 01581 01582 /** 01583 * @brief MFX writes single data. 01584 * @param Addr: I2C address 01585 * @param Reg: Register address 01586 * @param Value: Data to be written 01587 * @retval None 01588 */ 01589 void MFX_IO_Write(uint16_t Addr, uint8_t Reg, uint8_t Value) 01590 { 01591 I2C2_WriteData(Addr, Reg, I2C_MEMADD_SIZE_8BIT, Value); 01592 } 01593 01594 /** 01595 * @brief MFX reads single data. 01596 * @param Addr: I2C address 01597 * @param Reg: Register address 01598 * @retval Read data 01599 */ 01600 uint8_t MFX_IO_Read(uint16_t Addr, uint8_t Reg) 01601 { 01602 return I2C2_ReadData(Addr, Reg, I2C_MEMADD_SIZE_8BIT); 01603 } 01604 01605 /** 01606 * @brief MFX reads multiple data. 01607 * @param Addr: I2C address 01608 * @param Reg: Register address 01609 * @param Buffer: Pointer to data buffer 01610 * @param Length: Length of the data 01611 * @retval Number of read data 01612 */ 01613 uint16_t MFX_IO_ReadMultiple(uint16_t Addr, uint8_t Reg, uint8_t *Buffer, uint16_t Length) 01614 { 01615 return I2C2_ReadBuffer(Addr, (uint16_t)Reg, I2C_MEMADD_SIZE_8BIT, Buffer, Length); 01616 } 01617 01618 /** 01619 * @brief MFX writes multiple data. 01620 * @param Addr: I2C address 01621 * @param Reg: Register address 01622 * @param Buffer: Pointer to data buffer 01623 * @param Length: Length of the data 01624 * @retval None 01625 */ 01626 void MFX_IO_WriteMultiple(uint16_t Addr, uint8_t Reg, uint8_t *Buffer, uint16_t Length) 01627 { 01628 I2C2_WriteBuffer(Addr, (uint16_t)Reg, I2C_MEMADD_SIZE_8BIT, Buffer, Length); 01629 } 01630 01631 /** 01632 * @brief MFX delay 01633 * @param Delay: Delay in ms 01634 * @retval None 01635 */ 01636 void MFX_IO_Delay(uint32_t Delay) 01637 { 01638 HAL_Delay(Delay); 01639 } 01640 01641 01642 /********************************* LINK AUDIO *********************************/ 01643 /** 01644 * @brief Initializes Audio low level. 01645 * @retval None 01646 */ 01647 void AUDIO_IO_Init(void) 01648 { 01649 GPIO_InitTypeDef GPIO_InitStruct; 01650 01651 /* Enable Reset GPIO Clock */ 01652 AUDIO_RESET_GPIO_CLK_ENABLE(); 01653 01654 /* Audio reset pin configuration */ 01655 GPIO_InitStruct.Pin = AUDIO_RESET_PIN; 01656 GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP; 01657 GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_HIGH; 01658 GPIO_InitStruct.Pull = GPIO_NOPULL; 01659 HAL_GPIO_Init(AUDIO_RESET_GPIO, &GPIO_InitStruct); 01660 01661 /* I2C bus init */ 01662 I2C1_Init(); 01663 01664 /* Power Down the codec */ 01665 CODEC_AUDIO_POWER_OFF(); 01666 01667 /* wait for a delay to insure registers erasing */ 01668 HAL_Delay(5); 01669 01670 /* Power on the codec */ 01671 CODEC_AUDIO_POWER_ON(); 01672 01673 /* wait for a delay to insure registers erasing */ 01674 HAL_Delay(5); 01675 } 01676 01677 /** 01678 * @brief Deinitializes Audio low level. 01679 * @retval None 01680 */ 01681 void AUDIO_IO_DeInit(void) /* TO DO */ 01682 { 01683 GPIO_InitTypeDef GPIO_InitStruct; 01684 01685 /***********************************************************************/ 01686 /* In case of battery-supplied powered, there is no audio codec-based 01687 features available. Set audio codec I/O default setting */ 01688 /***********************************************************************/ 01689 __HAL_RCC_GPIOE_CLK_ENABLE(); 01690 GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP ; 01691 GPIO_InitStruct.Pin = (GPIO_PIN_2 | GPIO_PIN_3 | GPIO_PIN_4 | GPIO_PIN_5 | GPIO_PIN_6); 01692 GPIO_InitStruct.Pull = GPIO_PULLDOWN; 01693 GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_VERY_HIGH; 01694 HAL_GPIO_Init(GPIOE, &GPIO_InitStruct); 01695 HAL_GPIO_WritePin(GPIOE, GPIO_PIN_2, GPIO_PIN_RESET); 01696 HAL_GPIO_WritePin(GPIOE, GPIO_PIN_3, GPIO_PIN_RESET); 01697 HAL_GPIO_WritePin(GPIOE, GPIO_PIN_4, GPIO_PIN_RESET); 01698 HAL_GPIO_WritePin(GPIOE, GPIO_PIN_5, GPIO_PIN_RESET); 01699 HAL_GPIO_WritePin(GPIOE, GPIO_PIN_6, GPIO_PIN_RESET); 01700 01701 /* I2C bus Deinit */ 01702 I2C1_DeInit(); 01703 } 01704 01705 /** 01706 * @brief Writes a single data. 01707 * @param Addr: I2C address 01708 * @param Reg: Reg address 01709 * @param Value: Data to be written 01710 * @retval None 01711 */ 01712 void AUDIO_IO_Write(uint8_t Addr, uint8_t Reg, uint8_t Value) 01713 { 01714 I2C1_WriteBuffer(Addr, (uint16_t)Reg, I2C_MEMADD_SIZE_8BIT, &Value, 1); 01715 } 01716 01717 /** 01718 * @brief Reads a single data. 01719 * @param Addr: I2C address 01720 * @param Reg: Reg address 01721 * @retval Data to be read 01722 */ 01723 uint8_t AUDIO_IO_Read(uint8_t Addr, uint8_t Reg) 01724 { 01725 uint8_t Read_Value = 0; 01726 01727 I2C1_ReadBuffer((uint16_t) Addr, (uint16_t) Reg, I2C_MEMADD_SIZE_8BIT, &Read_Value, 1); 01728 01729 return Read_Value; 01730 } 01731 01732 /** 01733 * @brief AUDIO Codec delay 01734 * @param Delay: Delay in ms 01735 * @retval None 01736 */ 01737 void AUDIO_IO_Delay(uint32_t Delay) 01738 { 01739 HAL_Delay(Delay); 01740 } 01741 #endif /* HAL_I2C_MODULE_ENABLED */ 01742 01743 /** 01744 * @} 01745 */ 01746 01747 /** 01748 * @} 01749 */ 01750 01751 /** 01752 * @} 01753 */ 01754 01755 /** 01756 * @} 01757 */ 01758 01759 /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/
Generated on Wed Aug 17 2022 15:47:40 by
