stm32f7xx_hal_i2c_ex.c 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275
  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. * + Extended features functions
  9. *
  10. @verbatim
  11. ==============================================================================
  12. ##### I2C peripheral Extended features #####
  13. ==============================================================================
  14. [..] Comparing to other previous devices, the I2C interface for STM32F7xx
  15. devices contains the following additional features
  16. (+) Possibility to disable or enable Analog Noise Filter
  17. (+) Use of a configured Digital Noise Filter
  18. (+) Disable or enable Fast Mode Plus
  19. ##### How to use this driver #####
  20. ==============================================================================
  21. [..] This driver provides functions to:
  22. (#) Configure I2C Analog noise filter using the function HAL_I2CEx_ConfigAnalogFilter()
  23. (#) Configure I2C Digital noise filter using the function HAL_I2CEx_ConfigDigitalFilter()
  24. (#) Configure the enable or disable of fast mode plus driving capability using the functions :
  25. (++) HAL_I2CEx_EnableFastModePlus()
  26. (++) HAL_I2CEx_DisableFastModePlus()
  27. @endverbatim
  28. ******************************************************************************
  29. * @attention
  30. *
  31. * <h2><center>&copy; COPYRIGHT(c) 2017 STMicroelectronics</center></h2>
  32. *
  33. * Redistribution and use in source and binary forms, with or without modification,
  34. * are permitted provided that the following conditions are met:
  35. * 1. Redistributions of source code must retain the above copyright notice,
  36. * this list of conditions and the following disclaimer.
  37. * 2. Redistributions in binary form must reproduce the above copyright notice,
  38. * this list of conditions and the following disclaimer in the documentation
  39. * and/or other materials provided with the distribution.
  40. * 3. Neither the name of STMicroelectronics nor the names of its contributors
  41. * may be used to endorse or promote products derived from this software
  42. * without specific prior written permission.
  43. *
  44. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  45. * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  46. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  47. * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
  48. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  49. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
  50. * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  51. * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
  52. * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  53. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  54. *
  55. ******************************************************************************
  56. */
  57. /* Includes ------------------------------------------------------------------*/
  58. #include "stm32f7xx_hal.h"
  59. /** @addtogroup STM32F7xx_HAL_Driver
  60. * @{
  61. */
  62. /** @defgroup I2CEx I2CEx
  63. * @brief I2C Extended HAL module driver
  64. * @{
  65. */
  66. #ifdef HAL_I2C_MODULE_ENABLED
  67. /* Private typedef -----------------------------------------------------------*/
  68. /* Private define ------------------------------------------------------------*/
  69. /* Private macro -------------------------------------------------------------*/
  70. /* Private variables ---------------------------------------------------------*/
  71. /* Private function prototypes -----------------------------------------------*/
  72. /* Private functions ---------------------------------------------------------*/
  73. /** @defgroup I2CEx_Exported_Functions I2C Extended Exported Functions
  74. * @{
  75. */
  76. /** @defgroup I2CEx_Exported_Functions_Group1 Extended features functions
  77. * @brief Extended features functions
  78. *
  79. @verbatim
  80. ===============================================================================
  81. ##### Extended features functions #####
  82. ===============================================================================
  83. [..] This section provides functions allowing to:
  84. (+) Configure Noise Filters
  85. (+) Configure Fast Mode Plus
  86. @endverbatim
  87. * @{
  88. */
  89. /**
  90. * @brief Configure I2C Analog noise filter.
  91. * @param hi2c Pointer to a I2C_HandleTypeDef structure that contains
  92. * the configuration information for the specified I2Cx peripheral.
  93. * @param AnalogFilter New state of the Analog filter.
  94. * @retval HAL status
  95. */
  96. HAL_StatusTypeDef HAL_I2CEx_ConfigAnalogFilter(I2C_HandleTypeDef *hi2c, uint32_t AnalogFilter)
  97. {
  98. /* Check the parameters */
  99. assert_param(IS_I2C_ALL_INSTANCE(hi2c->Instance));
  100. assert_param(IS_I2C_ANALOG_FILTER(AnalogFilter));
  101. if (hi2c->State == HAL_I2C_STATE_READY)
  102. {
  103. /* Process Locked */
  104. __HAL_LOCK(hi2c);
  105. hi2c->State = HAL_I2C_STATE_BUSY;
  106. /* Disable the selected I2C peripheral */
  107. __HAL_I2C_DISABLE(hi2c);
  108. /* Reset I2Cx ANOFF bit */
  109. hi2c->Instance->CR1 &= ~(I2C_CR1_ANFOFF);
  110. /* Set analog filter bit*/
  111. hi2c->Instance->CR1 |= AnalogFilter;
  112. __HAL_I2C_ENABLE(hi2c);
  113. hi2c->State = HAL_I2C_STATE_READY;
  114. /* Process Unlocked */
  115. __HAL_UNLOCK(hi2c);
  116. return HAL_OK;
  117. }
  118. else
  119. {
  120. return HAL_BUSY;
  121. }
  122. }
  123. /**
  124. * @brief Configure I2C Digital noise filter.
  125. * @param hi2c Pointer to a I2C_HandleTypeDef structure that contains
  126. * the configuration information for the specified I2Cx peripheral.
  127. * @param DigitalFilter Coefficient of digital noise filter between Min_Data=0x00 and Max_Data=0x0F.
  128. * @retval HAL status
  129. */
  130. HAL_StatusTypeDef HAL_I2CEx_ConfigDigitalFilter(I2C_HandleTypeDef *hi2c, uint32_t DigitalFilter)
  131. {
  132. uint32_t tmpreg = 0U;
  133. /* Check the parameters */
  134. assert_param(IS_I2C_ALL_INSTANCE(hi2c->Instance));
  135. assert_param(IS_I2C_DIGITAL_FILTER(DigitalFilter));
  136. if (hi2c->State == HAL_I2C_STATE_READY)
  137. {
  138. /* Process Locked */
  139. __HAL_LOCK(hi2c);
  140. hi2c->State = HAL_I2C_STATE_BUSY;
  141. /* Disable the selected I2C peripheral */
  142. __HAL_I2C_DISABLE(hi2c);
  143. /* Get the old register value */
  144. tmpreg = hi2c->Instance->CR1;
  145. /* Reset I2Cx DNF bits [11:8] */
  146. tmpreg &= ~(I2C_CR1_DNF);
  147. /* Set I2Cx DNF coefficient */
  148. tmpreg |= DigitalFilter << 8U;
  149. /* Store the new register value */
  150. hi2c->Instance->CR1 = tmpreg;
  151. __HAL_I2C_ENABLE(hi2c);
  152. hi2c->State = HAL_I2C_STATE_READY;
  153. /* Process Unlocked */
  154. __HAL_UNLOCK(hi2c);
  155. return HAL_OK;
  156. }
  157. else
  158. {
  159. return HAL_BUSY;
  160. }
  161. }
  162. #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)
  163. /**
  164. * @brief Enable the I2C fast mode plus driving capability.
  165. * @param ConfigFastModePlus Selects the pin.
  166. * This parameter can be one of the @ref I2CEx_FastModePlus values
  167. * @note For I2C1, fast mode plus driving capability can be enabled on all selected
  168. * I2C1 pins using I2C_FASTMODEPLUS_I2C1 parameter or independently
  169. * on each one of the following pins PB6, PB7, PB8 and PB9.
  170. * @note For remaining I2C1 pins (PA14, PA15...) fast mode plus driving capability
  171. * can be enabled only by using I2C_FASTMODEPLUS_I2C1 parameter.
  172. * @note For all I2C2 pins fast mode plus driving capability can be enabled
  173. * only by using I2C_FASTMODEPLUS_I2C2 parameter.
  174. * @note For all I2C3 pins fast mode plus driving capability can be enabled
  175. * only by using I2C_FASTMODEPLUS_I2C3 parameter.
  176. * @note For all I2C4 pins fast mode plus driving capability can be enabled
  177. * only by using I2C_FASTMODEPLUS_I2C4 parameter.
  178. * @retval None
  179. */
  180. void HAL_I2CEx_EnableFastModePlus(uint32_t ConfigFastModePlus)
  181. {
  182. /* Check the parameter */
  183. assert_param(IS_I2C_FASTMODEPLUS(ConfigFastModePlus));
  184. /* Enable SYSCFG clock */
  185. __HAL_RCC_SYSCFG_CLK_ENABLE();
  186. /* Enable fast mode plus driving capability for selected pin */
  187. SET_BIT(SYSCFG->PMC, (uint32_t)ConfigFastModePlus);
  188. }
  189. /**
  190. * @brief Disable the I2C fast mode plus driving capability.
  191. * @param ConfigFastModePlus Selects the pin.
  192. * This parameter can be one of the @ref I2CEx_FastModePlus values
  193. * @note For I2C1, fast mode plus driving capability can be disabled on all selected
  194. * I2C1 pins using I2C_FASTMODEPLUS_I2C1 parameter or independently
  195. * on each one of the following pins PB6, PB7, PB8 and PB9.
  196. * @note For remaining I2C1 pins (PA14, PA15...) fast mode plus driving capability
  197. * can be disabled only by using I2C_FASTMODEPLUS_I2C1 parameter.
  198. * @note For all I2C2 pins fast mode plus driving capability can be disabled
  199. * only by using I2C_FASTMODEPLUS_I2C2 parameter.
  200. * @note For all I2C3 pins fast mode plus driving capability can be disabled
  201. * only by using I2C_FASTMODEPLUS_I2C3 parameter.
  202. * @note For all I2C4 pins fast mode plus driving capability can be disabled
  203. * only by using I2C_FASTMODEPLUS_I2C4 parameter.
  204. * @retval None
  205. */
  206. void HAL_I2CEx_DisableFastModePlus(uint32_t ConfigFastModePlus)
  207. {
  208. /* Check the parameter */
  209. assert_param(IS_I2C_FASTMODEPLUS(ConfigFastModePlus));
  210. /* Enable SYSCFG clock */
  211. __HAL_RCC_SYSCFG_CLK_ENABLE();
  212. /* Disable fast mode plus driving capability for selected pin */
  213. CLEAR_BIT(SYSCFG->PMC, (uint32_t)ConfigFastModePlus);
  214. }
  215. #endif
  216. /**
  217. * @}
  218. */
  219. /**
  220. * @}
  221. */
  222. #endif /* HAL_I2C_MODULE_ENABLED */
  223. /**
  224. * @}
  225. */
  226. /**
  227. * @}
  228. */
  229. /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/