You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 

276 lines
9.7 KiB

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