stm32f7xx_hal_i2c_ex.c 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270
  1. /**
  2. ******************************************************************************
  3. * @file stm32f7xx_hal_i2c_ex.c
  4. * @author MCD Application Team
  5. * @brief I2C Extended HAL module driver.
  6. * This file provides firmware functions to manage the following
  7. * functionalities of I2C Extended peripheral:
  8. * + Filter Mode Functions
  9. * + FastModePlus Functions
  10. *
  11. ******************************************************************************
  12. * @attention
  13. *
  14. * Copyright (c) 2017 STMicroelectronics.
  15. * All rights reserved.
  16. *
  17. * This software is licensed under terms that can be found in the LICENSE file
  18. * in the root directory of this software component.
  19. * If no LICENSE file comes with this software, it is provided AS-IS.
  20. *
  21. ******************************************************************************
  22. @verbatim
  23. ==============================================================================
  24. ##### I2C peripheral Extended features #####
  25. ==============================================================================
  26. [..] Comparing to other previous devices, the I2C interface for STM32F7xx
  27. devices contains the following additional features
  28. (+) Possibility to disable or enable Analog Noise Filter
  29. (+) Use of a configured Digital Noise Filter
  30. (+) Disable or enable Fast Mode Plus
  31. ##### How to use this driver #####
  32. ==============================================================================
  33. [..] This driver provides functions to:
  34. (#) Configure I2C Analog noise filter using the function HAL_I2CEx_ConfigAnalogFilter()
  35. (#) Configure I2C Digital noise filter using the function HAL_I2CEx_ConfigDigitalFilter()
  36. (#) Configure the enable or disable of fast mode plus driving capability using the functions :
  37. (++) HAL_I2CEx_EnableFastModePlus()
  38. (++) HAL_I2CEx_DisableFastModePlus()
  39. @endverbatim
  40. */
  41. /* Includes ------------------------------------------------------------------*/
  42. #include "stm32f7xx_hal.h"
  43. /** @addtogroup STM32F7xx_HAL_Driver
  44. * @{
  45. */
  46. /** @defgroup I2CEx I2CEx
  47. * @brief I2C Extended HAL module driver
  48. * @{
  49. */
  50. #ifdef HAL_I2C_MODULE_ENABLED
  51. /* Private typedef -----------------------------------------------------------*/
  52. /* Private define ------------------------------------------------------------*/
  53. /* Private macro -------------------------------------------------------------*/
  54. /* Private variables ---------------------------------------------------------*/
  55. /* Private function prototypes -----------------------------------------------*/
  56. /* Private functions ---------------------------------------------------------*/
  57. /** @defgroup I2CEx_Exported_Functions I2C Extended Exported Functions
  58. * @{
  59. */
  60. /** @defgroup I2CEx_Exported_Functions_Group1 Filter Mode Functions
  61. * @brief Filter Mode Functions
  62. *
  63. @verbatim
  64. ===============================================================================
  65. ##### Filter Mode Functions #####
  66. ===============================================================================
  67. [..] This section provides functions allowing to:
  68. (+) Configure Noise Filters
  69. @endverbatim
  70. * @{
  71. */
  72. /**
  73. * @brief Configure I2C Analog noise filter.
  74. * @param hi2c Pointer to a I2C_HandleTypeDef structure that contains
  75. * the configuration information for the specified I2Cx peripheral.
  76. * @param AnalogFilter New state of the Analog filter.
  77. * @retval HAL status
  78. */
  79. HAL_StatusTypeDef HAL_I2CEx_ConfigAnalogFilter(I2C_HandleTypeDef *hi2c, uint32_t AnalogFilter)
  80. {
  81. /* Check the parameters */
  82. assert_param(IS_I2C_ALL_INSTANCE(hi2c->Instance));
  83. assert_param(IS_I2C_ANALOG_FILTER(AnalogFilter));
  84. if (hi2c->State == HAL_I2C_STATE_READY)
  85. {
  86. /* Process Locked */
  87. __HAL_LOCK(hi2c);
  88. hi2c->State = HAL_I2C_STATE_BUSY;
  89. /* Disable the selected I2C peripheral */
  90. __HAL_I2C_DISABLE(hi2c);
  91. /* Reset I2Cx ANOFF bit */
  92. hi2c->Instance->CR1 &= ~(I2C_CR1_ANFOFF);
  93. /* Set analog filter bit*/
  94. hi2c->Instance->CR1 |= AnalogFilter;
  95. __HAL_I2C_ENABLE(hi2c);
  96. hi2c->State = HAL_I2C_STATE_READY;
  97. /* Process Unlocked */
  98. __HAL_UNLOCK(hi2c);
  99. return HAL_OK;
  100. }
  101. else
  102. {
  103. return HAL_BUSY;
  104. }
  105. }
  106. /**
  107. * @brief Configure I2C Digital noise filter.
  108. * @param hi2c Pointer to a I2C_HandleTypeDef structure that contains
  109. * the configuration information for the specified I2Cx peripheral.
  110. * @param DigitalFilter Coefficient of digital noise filter between Min_Data=0x00 and Max_Data=0x0F.
  111. * @retval HAL status
  112. */
  113. HAL_StatusTypeDef HAL_I2CEx_ConfigDigitalFilter(I2C_HandleTypeDef *hi2c, uint32_t DigitalFilter)
  114. {
  115. uint32_t tmpreg;
  116. /* Check the parameters */
  117. assert_param(IS_I2C_ALL_INSTANCE(hi2c->Instance));
  118. assert_param(IS_I2C_DIGITAL_FILTER(DigitalFilter));
  119. if (hi2c->State == HAL_I2C_STATE_READY)
  120. {
  121. /* Process Locked */
  122. __HAL_LOCK(hi2c);
  123. hi2c->State = HAL_I2C_STATE_BUSY;
  124. /* Disable the selected I2C peripheral */
  125. __HAL_I2C_DISABLE(hi2c);
  126. /* Get the old register value */
  127. tmpreg = hi2c->Instance->CR1;
  128. /* Reset I2Cx DNF bits [11:8] */
  129. tmpreg &= ~(I2C_CR1_DNF);
  130. /* Set I2Cx DNF coefficient */
  131. tmpreg |= DigitalFilter << 8U;
  132. /* Store the new register value */
  133. hi2c->Instance->CR1 = tmpreg;
  134. __HAL_I2C_ENABLE(hi2c);
  135. hi2c->State = HAL_I2C_STATE_READY;
  136. /* Process Unlocked */
  137. __HAL_UNLOCK(hi2c);
  138. return HAL_OK;
  139. }
  140. else
  141. {
  142. return HAL_BUSY;
  143. }
  144. }
  145. /**
  146. * @}
  147. */
  148. #if (defined(SYSCFG_PMC_I2C_PB6_FMP) || defined(SYSCFG_PMC_I2C_PB7_FMP)) || (defined(SYSCFG_PMC_I2C_PB8_FMP) || defined(SYSCFG_PMC_I2C_PB9_FMP)) || (defined(SYSCFG_PMC_I2C1_FMP)) || (defined(SYSCFG_PMC_I2C2_FMP)) || defined(SYSCFG_PMC_I2C3_FMP) || defined(SYSCFG_PMC_I2C4_FMP)
  149. /** @defgroup I2CEx_Exported_Functions_Group3 Fast Mode Plus Functions
  150. * @brief Fast Mode Plus Functions
  151. *
  152. @verbatim
  153. ===============================================================================
  154. ##### Fast Mode Plus Functions #####
  155. ===============================================================================
  156. [..] This section provides functions allowing to:
  157. (+) Configure Fast Mode Plus
  158. @endverbatim
  159. * @{
  160. */
  161. /**
  162. * @brief Enable the I2C fast mode plus driving capability.
  163. * @param ConfigFastModePlus Selects the pin.
  164. * This parameter can be one of the @ref I2CEx_FastModePlus values
  165. * @note For I2C1, fast mode plus driving capability can be enabled on all selected
  166. * I2C1 pins using I2C_FASTMODEPLUS_I2C1 parameter or independently
  167. * on each one of the following pins PB6, PB7, PB8 and PB9.
  168. * @note For remaining I2C1 pins (PA14, PA15...) fast mode plus driving capability
  169. * can be enabled only by using I2C_FASTMODEPLUS_I2C1 parameter.
  170. * @note For all I2C2 pins fast mode plus driving capability can be enabled
  171. * only by using I2C_FASTMODEPLUS_I2C2 parameter.
  172. * @note For all I2C3 pins fast mode plus driving capability can be enabled
  173. * only by using I2C_FASTMODEPLUS_I2C3 parameter.
  174. * @note For all I2C4 pins fast mode plus driving capability can be enabled
  175. * only by using I2C_FASTMODEPLUS_I2C4 parameter.
  176. * @retval None
  177. */
  178. void HAL_I2CEx_EnableFastModePlus(uint32_t ConfigFastModePlus)
  179. {
  180. /* Check the parameter */
  181. assert_param(IS_I2C_FASTMODEPLUS(ConfigFastModePlus));
  182. /* Enable SYSCFG clock */
  183. __HAL_RCC_SYSCFG_CLK_ENABLE();
  184. /* Enable fast mode plus driving capability for selected pin */
  185. SET_BIT(SYSCFG->PMC, (uint32_t)ConfigFastModePlus);
  186. }
  187. /**
  188. * @brief Disable the I2C fast mode plus driving capability.
  189. * @param ConfigFastModePlus Selects the pin.
  190. * This parameter can be one of the @ref I2CEx_FastModePlus values
  191. * @note For I2C1, fast mode plus driving capability can be disabled on all selected
  192. * I2C1 pins using I2C_FASTMODEPLUS_I2C1 parameter or independently
  193. * on each one of the following pins PB6, PB7, PB8 and PB9.
  194. * @note For remaining I2C1 pins (PA14, PA15...) fast mode plus driving capability
  195. * can be disabled only by using I2C_FASTMODEPLUS_I2C1 parameter.
  196. * @note For all I2C2 pins fast mode plus driving capability can be disabled
  197. * only by using I2C_FASTMODEPLUS_I2C2 parameter.
  198. * @note For all I2C3 pins fast mode plus driving capability can be disabled
  199. * only by using I2C_FASTMODEPLUS_I2C3 parameter.
  200. * @note For all I2C4 pins fast mode plus driving capability can be disabled
  201. * only by using I2C_FASTMODEPLUS_I2C4 parameter.
  202. * @retval None
  203. */
  204. void HAL_I2CEx_DisableFastModePlus(uint32_t ConfigFastModePlus)
  205. {
  206. /* Check the parameter */
  207. assert_param(IS_I2C_FASTMODEPLUS(ConfigFastModePlus));
  208. /* Enable SYSCFG clock */
  209. __HAL_RCC_SYSCFG_CLK_ENABLE();
  210. /* Disable fast mode plus driving capability for selected pin */
  211. CLEAR_BIT(SYSCFG->PMC, (uint32_t)ConfigFastModePlus);
  212. }
  213. /**
  214. * @}
  215. */
  216. #endif /* Fast Mode Plus Availability */
  217. /**
  218. * @}
  219. */
  220. #endif /* HAL_I2C_MODULE_ENABLED */
  221. /**
  222. * @}
  223. */
  224. /**
  225. * @}
  226. */