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.
 
 
 

348 lines
11 KiB

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