stm32f4xx_hal_i2c_ex.c 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204
  1. /**
  2. ******************************************************************************
  3. * @file stm32f4xx_hal_i2c_ex.c
  4. * @author MCD Application Team
  5. * @brief I2C Extension HAL module driver.
  6. * This file provides firmware functions to manage the following
  7. * functionalities of I2C extension peripheral:
  8. * + Extension features functions
  9. *
  10. @verbatim
  11. ==============================================================================
  12. ##### I2C peripheral extension features #####
  13. ==============================================================================
  14. [..] Comparing to other previous devices, the I2C interface for STM32F427xx/437xx/
  15. 429xx/439xx devices contains the following additional features :
  16. (+) Possibility to disable or enable Analog Noise Filter
  17. (+) Use of a configured Digital Noise Filter
  18. ##### How to use this driver #####
  19. ==============================================================================
  20. [..] This driver provides functions to configure Noise Filter
  21. (#) Configure I2C Analog noise filter using the function HAL_I2C_AnalogFilter_Config()
  22. (#) Configure I2C Digital noise filter using the function HAL_I2C_DigitalFilter_Config()
  23. @endverbatim
  24. ******************************************************************************
  25. * @attention
  26. *
  27. * <h2><center>&copy; COPYRIGHT(c) 2017 STMicroelectronics</center></h2>
  28. *
  29. * Redistribution and use in source and binary forms, with or without modification,
  30. * are permitted provided that the following conditions are met:
  31. * 1. Redistributions of source code must retain the above copyright notice,
  32. * this list of conditions and the following disclaimer.
  33. * 2. Redistributions in binary form must reproduce the above copyright notice,
  34. * this list of conditions and the following disclaimer in the documentation
  35. * and/or other materials provided with the distribution.
  36. * 3. Neither the name of STMicroelectronics nor the names of its contributors
  37. * may be used to endorse or promote products derived from this software
  38. * without specific prior written permission.
  39. *
  40. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  41. * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  42. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  43. * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
  44. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  45. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
  46. * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  47. * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
  48. * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  49. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  50. *
  51. ******************************************************************************
  52. */
  53. /* Includes ------------------------------------------------------------------*/
  54. #include "stm32f4xx_hal.h"
  55. /** @addtogroup STM32F4xx_HAL_Driver
  56. * @{
  57. */
  58. /** @defgroup I2CEx I2CEx
  59. * @brief I2C HAL module driver
  60. * @{
  61. */
  62. #ifdef HAL_I2C_MODULE_ENABLED
  63. #if defined(STM32F427xx) || defined(STM32F437xx) || defined(STM32F429xx) || defined(STM32F439xx) ||\
  64. defined(STM32F401xC) || defined(STM32F401xE) || defined(STM32F411xE) || defined(STM32F446xx) ||\
  65. defined(STM32F469xx) || defined(STM32F479xx) || defined(STM32F413xx) || defined(STM32F423xx)
  66. /* Private typedef -----------------------------------------------------------*/
  67. /* Private define ------------------------------------------------------------*/
  68. /* Private macro -------------------------------------------------------------*/
  69. /* Private variables ---------------------------------------------------------*/
  70. /* Private function prototypes -----------------------------------------------*/
  71. /* Exported functions --------------------------------------------------------*/
  72. /** @defgroup I2CEx_Exported_Functions I2C Exported Functions
  73. * @{
  74. */
  75. /** @defgroup I2CEx_Exported_Functions_Group1 Extension features functions
  76. * @brief Extension features functions
  77. *
  78. @verbatim
  79. ===============================================================================
  80. ##### Extension features functions #####
  81. ===============================================================================
  82. [..] This section provides functions allowing to:
  83. (+) Configure Noise Filters
  84. @endverbatim
  85. * @{
  86. */
  87. /**
  88. * @brief Configures I2C Analog noise filter.
  89. * @param hi2c pointer to a I2C_HandleTypeDef structure that contains
  90. * the configuration information for the specified I2Cx peripheral.
  91. * @param AnalogFilter new state of the Analog filter.
  92. * @retval HAL status
  93. */
  94. HAL_StatusTypeDef HAL_I2CEx_ConfigAnalogFilter(I2C_HandleTypeDef *hi2c, uint32_t AnalogFilter)
  95. {
  96. /* Check the parameters */
  97. assert_param(IS_I2C_ALL_INSTANCE(hi2c->Instance));
  98. assert_param(IS_I2C_ANALOG_FILTER(AnalogFilter));
  99. if(hi2c->State == HAL_I2C_STATE_READY)
  100. {
  101. hi2c->State = HAL_I2C_STATE_BUSY;
  102. /* Disable the selected I2C peripheral */
  103. __HAL_I2C_DISABLE(hi2c);
  104. /* Reset I2Cx ANOFF bit */
  105. hi2c->Instance->FLTR &= ~(I2C_FLTR_ANOFF);
  106. /* Disable the analog filter */
  107. hi2c->Instance->FLTR |= AnalogFilter;
  108. __HAL_I2C_ENABLE(hi2c);
  109. hi2c->State = HAL_I2C_STATE_READY;
  110. return HAL_OK;
  111. }
  112. else
  113. {
  114. return HAL_BUSY;
  115. }
  116. }
  117. /**
  118. * @brief Configures I2C Digital noise filter.
  119. * @param hi2c pointer to a I2C_HandleTypeDef structure that contains
  120. * the configuration information for the specified I2Cx peripheral.
  121. * @param DigitalFilter Coefficient of digital noise filter between 0x00 and 0x0F.
  122. * @retval HAL status
  123. */
  124. HAL_StatusTypeDef HAL_I2CEx_ConfigDigitalFilter(I2C_HandleTypeDef *hi2c, uint32_t DigitalFilter)
  125. {
  126. uint16_t tmpreg = 0;
  127. /* Check the parameters */
  128. assert_param(IS_I2C_ALL_INSTANCE(hi2c->Instance));
  129. assert_param(IS_I2C_DIGITAL_FILTER(DigitalFilter));
  130. if(hi2c->State == HAL_I2C_STATE_READY)
  131. {
  132. hi2c->State = HAL_I2C_STATE_BUSY;
  133. /* Disable the selected I2C peripheral */
  134. __HAL_I2C_DISABLE(hi2c);
  135. /* Get the old register value */
  136. tmpreg = hi2c->Instance->FLTR;
  137. /* Reset I2Cx DNF bit [3:0] */
  138. tmpreg &= ~(I2C_FLTR_DNF);
  139. /* Set I2Cx DNF coefficient */
  140. tmpreg |= DigitalFilter;
  141. /* Store the new register value */
  142. hi2c->Instance->FLTR = tmpreg;
  143. __HAL_I2C_ENABLE(hi2c);
  144. hi2c->State = HAL_I2C_STATE_READY;
  145. return HAL_OK;
  146. }
  147. else
  148. {
  149. return HAL_BUSY;
  150. }
  151. }
  152. /**
  153. * @}
  154. */
  155. /**
  156. * @}
  157. */
  158. #endif /* STM32F427xx || STM32F429xx || STM32F437xx || STM32F439xx || STM32F401xC ||\
  159. STM32F401xE || STM32F446xx || STM32F469xx || STM32F479xx || STM32F413xx ||\
  160. STM32F423xx */
  161. #endif /* HAL_I2C_MODULE_ENABLED */
  162. /**
  163. * @}
  164. */
  165. /**
  166. * @}
  167. */
  168. /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/