stm32f4xx_hal_i2c_ex.c 7.1 KB

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