stm32f0xx_hal_i2c_ex.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365
  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. * + Filter Mode Functions
  9. * + WakeUp Mode Functions
  10. * + FastModePlus Functions
  11. *
  12. @verbatim
  13. ==============================================================================
  14. ##### I2C peripheral Extended features #####
  15. ==============================================================================
  16. [..] Comparing to other previous devices, the I2C interface for STM32F0xx
  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 wakeup from Stop mode(s)
  21. (+) Disable or enable Fast Mode Plus
  22. ##### How to use this driver #####
  23. ==============================================================================
  24. [..] This driver provides functions to configure Noise Filter and Wake Up Feature
  25. (#) Configure I2C Analog noise filter using the function HAL_I2CEx_ConfigAnalogFilter()
  26. (#) Configure I2C Digital noise filter using the function HAL_I2CEx_ConfigDigitalFilter()
  27. (#) Configure the enable or disable of I2C Wake Up Mode using the functions :
  28. (++) HAL_I2CEx_EnableWakeUp()
  29. (++) HAL_I2CEx_DisableWakeUp()
  30. (#) Configure the enable or disable of fast mode plus driving capability using the functions :
  31. (++) HAL_I2CEx_EnableFastModePlus()
  32. (++) HAL_I2CEx_DisableFastModePlus()
  33. @endverbatim
  34. ******************************************************************************
  35. * @attention
  36. *
  37. * <h2><center>&copy; Copyright (c) 2016 STMicroelectronics.
  38. * All rights reserved.</center></h2>
  39. *
  40. * This software component is licensed by ST under BSD 3-Clause license,
  41. * the "License"; You may not use this file except in compliance with the
  42. * License. You may obtain a copy of the License at:
  43. * opensource.org/licenses/BSD-3-Clause
  44. *
  45. ******************************************************************************
  46. */
  47. /* Includes ------------------------------------------------------------------*/
  48. #include "stm32f0xx_hal.h"
  49. /** @addtogroup STM32F0xx_HAL_Driver
  50. * @{
  51. */
  52. /** @defgroup I2CEx I2CEx
  53. * @brief I2C Extended HAL module driver
  54. * @{
  55. */
  56. #ifdef HAL_I2C_MODULE_ENABLED
  57. /* Private typedef -----------------------------------------------------------*/
  58. /* Private define ------------------------------------------------------------*/
  59. /* Private macro -------------------------------------------------------------*/
  60. /* Private variables ---------------------------------------------------------*/
  61. /* Private function prototypes -----------------------------------------------*/
  62. /* Private functions ---------------------------------------------------------*/
  63. /** @defgroup I2CEx_Exported_Functions I2C Extended Exported Functions
  64. * @{
  65. */
  66. /** @defgroup I2CEx_Exported_Functions_Group1 Filter Mode Functions
  67. * @brief Filter Mode Functions
  68. *
  69. @verbatim
  70. ===============================================================================
  71. ##### Filter Mode Functions #####
  72. ===============================================================================
  73. [..] This section provides functions allowing to:
  74. (+) Configure Noise Filters
  75. @endverbatim
  76. * @{
  77. */
  78. /**
  79. * @brief Configure I2C Analog noise filter.
  80. * @param hi2c Pointer to a I2C_HandleTypeDef structure that contains
  81. * the configuration information for the specified I2Cx peripheral.
  82. * @param AnalogFilter New state of the Analog filter.
  83. * @retval HAL status
  84. */
  85. HAL_StatusTypeDef HAL_I2CEx_ConfigAnalogFilter(I2C_HandleTypeDef *hi2c, uint32_t AnalogFilter)
  86. {
  87. /* Check the parameters */
  88. assert_param(IS_I2C_ALL_INSTANCE(hi2c->Instance));
  89. assert_param(IS_I2C_ANALOG_FILTER(AnalogFilter));
  90. if (hi2c->State == HAL_I2C_STATE_READY)
  91. {
  92. /* Process Locked */
  93. __HAL_LOCK(hi2c);
  94. hi2c->State = HAL_I2C_STATE_BUSY;
  95. /* Disable the selected I2C peripheral */
  96. __HAL_I2C_DISABLE(hi2c);
  97. /* Reset I2Cx ANOFF bit */
  98. hi2c->Instance->CR1 &= ~(I2C_CR1_ANFOFF);
  99. /* Set analog filter bit*/
  100. hi2c->Instance->CR1 |= AnalogFilter;
  101. __HAL_I2C_ENABLE(hi2c);
  102. hi2c->State = HAL_I2C_STATE_READY;
  103. /* Process Unlocked */
  104. __HAL_UNLOCK(hi2c);
  105. return HAL_OK;
  106. }
  107. else
  108. {
  109. return HAL_BUSY;
  110. }
  111. }
  112. /**
  113. * @brief Configure I2C Digital noise filter.
  114. * @param hi2c Pointer to a I2C_HandleTypeDef structure that contains
  115. * the configuration information for the specified I2Cx peripheral.
  116. * @param DigitalFilter Coefficient of digital noise filter between Min_Data=0x00 and Max_Data=0x0F.
  117. * @retval HAL status
  118. */
  119. HAL_StatusTypeDef HAL_I2CEx_ConfigDigitalFilter(I2C_HandleTypeDef *hi2c, uint32_t DigitalFilter)
  120. {
  121. uint32_t tmpreg;
  122. /* Check the parameters */
  123. assert_param(IS_I2C_ALL_INSTANCE(hi2c->Instance));
  124. assert_param(IS_I2C_DIGITAL_FILTER(DigitalFilter));
  125. if (hi2c->State == HAL_I2C_STATE_READY)
  126. {
  127. /* Process Locked */
  128. __HAL_LOCK(hi2c);
  129. hi2c->State = HAL_I2C_STATE_BUSY;
  130. /* Disable the selected I2C peripheral */
  131. __HAL_I2C_DISABLE(hi2c);
  132. /* Get the old register value */
  133. tmpreg = hi2c->Instance->CR1;
  134. /* Reset I2Cx DNF bits [11:8] */
  135. tmpreg &= ~(I2C_CR1_DNF);
  136. /* Set I2Cx DNF coefficient */
  137. tmpreg |= DigitalFilter << 8U;
  138. /* Store the new register value */
  139. hi2c->Instance->CR1 = tmpreg;
  140. __HAL_I2C_ENABLE(hi2c);
  141. hi2c->State = HAL_I2C_STATE_READY;
  142. /* Process Unlocked */
  143. __HAL_UNLOCK(hi2c);
  144. return HAL_OK;
  145. }
  146. else
  147. {
  148. return HAL_BUSY;
  149. }
  150. }
  151. /**
  152. * @}
  153. */
  154. #if defined(I2C_CR1_WUPEN)
  155. /** @defgroup I2CEx_Exported_Functions_Group2 WakeUp Mode Functions
  156. * @brief WakeUp Mode Functions
  157. *
  158. @verbatim
  159. ===============================================================================
  160. ##### WakeUp Mode Functions #####
  161. ===============================================================================
  162. [..] This section provides functions allowing to:
  163. (+) Configure Wake Up Feature
  164. @endverbatim
  165. * @{
  166. */
  167. /**
  168. * @brief Enable I2C wakeup from Stop mode(s).
  169. * @param hi2c Pointer to a I2C_HandleTypeDef structure that contains
  170. * the configuration information for the specified I2Cx peripheral.
  171. * @retval HAL status
  172. */
  173. HAL_StatusTypeDef HAL_I2CEx_EnableWakeUp(I2C_HandleTypeDef *hi2c)
  174. {
  175. /* Check the parameters */
  176. assert_param(IS_I2C_WAKEUP_FROMSTOP_INSTANCE(hi2c->Instance));
  177. if (hi2c->State == HAL_I2C_STATE_READY)
  178. {
  179. /* Process Locked */
  180. __HAL_LOCK(hi2c);
  181. hi2c->State = HAL_I2C_STATE_BUSY;
  182. /* Disable the selected I2C peripheral */
  183. __HAL_I2C_DISABLE(hi2c);
  184. /* Enable wakeup from stop mode */
  185. hi2c->Instance->CR1 |= I2C_CR1_WUPEN;
  186. __HAL_I2C_ENABLE(hi2c);
  187. hi2c->State = HAL_I2C_STATE_READY;
  188. /* Process Unlocked */
  189. __HAL_UNLOCK(hi2c);
  190. return HAL_OK;
  191. }
  192. else
  193. {
  194. return HAL_BUSY;
  195. }
  196. }
  197. /**
  198. * @brief Disable I2C wakeup from Stop mode(s).
  199. * @param hi2c Pointer to a I2C_HandleTypeDef structure that contains
  200. * the configuration information for the specified I2Cx peripheral.
  201. * @retval HAL status
  202. */
  203. HAL_StatusTypeDef HAL_I2CEx_DisableWakeUp(I2C_HandleTypeDef *hi2c)
  204. {
  205. /* Check the parameters */
  206. assert_param(IS_I2C_WAKEUP_FROMSTOP_INSTANCE(hi2c->Instance));
  207. if (hi2c->State == HAL_I2C_STATE_READY)
  208. {
  209. /* Process Locked */
  210. __HAL_LOCK(hi2c);
  211. hi2c->State = HAL_I2C_STATE_BUSY;
  212. /* Disable the selected I2C peripheral */
  213. __HAL_I2C_DISABLE(hi2c);
  214. /* Enable wakeup from stop mode */
  215. hi2c->Instance->CR1 &= ~(I2C_CR1_WUPEN);
  216. __HAL_I2C_ENABLE(hi2c);
  217. hi2c->State = HAL_I2C_STATE_READY;
  218. /* Process Unlocked */
  219. __HAL_UNLOCK(hi2c);
  220. return HAL_OK;
  221. }
  222. else
  223. {
  224. return HAL_BUSY;
  225. }
  226. }
  227. /**
  228. * @}
  229. */
  230. #endif /* I2C_CR1_WUPEN */
  231. /** @defgroup I2CEx_Exported_Functions_Group3 Fast Mode Plus Functions
  232. * @brief Fast Mode Plus Functions
  233. *
  234. @verbatim
  235. ===============================================================================
  236. ##### Fast Mode Plus Functions #####
  237. ===============================================================================
  238. [..] This section provides functions allowing to:
  239. (+) Configure Fast Mode Plus
  240. @endverbatim
  241. * @{
  242. */
  243. /**
  244. * @brief Enable the I2C fast mode plus driving capability.
  245. * @param ConfigFastModePlus Selects the pin.
  246. * This parameter can be one of the @ref I2CEx_FastModePlus values
  247. * @note For I2C1, fast mode plus driving capability can be enabled on all selected
  248. * I2C1 pins using I2C_FASTMODEPLUS_I2C1 parameter or independently
  249. * on each one of the following pins PB6, PB7, PB8 and PB9.
  250. * @note For remaining I2C1 pins (PA14, PA15...) fast mode plus driving capability
  251. * can be enabled only by using I2C_FASTMODEPLUS_I2C1 parameter.
  252. * @note For all I2C2 pins fast mode plus driving capability can be enabled
  253. * only by using I2C_FASTMODEPLUS_I2C2 parameter.
  254. * @retval None
  255. */
  256. void HAL_I2CEx_EnableFastModePlus(uint32_t ConfigFastModePlus)
  257. {
  258. /* Check the parameter */
  259. assert_param(IS_I2C_FASTMODEPLUS(ConfigFastModePlus));
  260. /* Enable SYSCFG clock */
  261. __HAL_RCC_SYSCFG_CLK_ENABLE();
  262. /* Enable fast mode plus driving capability for selected pin */
  263. SET_BIT(SYSCFG->CFGR1, (uint32_t)ConfigFastModePlus);
  264. }
  265. /**
  266. * @brief Disable the I2C fast mode plus driving capability.
  267. * @param ConfigFastModePlus Selects the pin.
  268. * This parameter can be one of the @ref I2CEx_FastModePlus values
  269. * @note For I2C1, fast mode plus driving capability can be disabled on all selected
  270. * I2C1 pins using I2C_FASTMODEPLUS_I2C1 parameter or independently
  271. * on each one of the following pins PB6, PB7, PB8 and PB9.
  272. * @note For remaining I2C1 pins (PA14, PA15...) fast mode plus driving capability
  273. * can be disabled only by using I2C_FASTMODEPLUS_I2C1 parameter.
  274. * @note For all I2C2 pins fast mode plus driving capability can be disabled
  275. * only by using I2C_FASTMODEPLUS_I2C2 parameter.
  276. * @retval None
  277. */
  278. void HAL_I2CEx_DisableFastModePlus(uint32_t ConfigFastModePlus)
  279. {
  280. /* Check the parameter */
  281. assert_param(IS_I2C_FASTMODEPLUS(ConfigFastModePlus));
  282. /* Enable SYSCFG clock */
  283. __HAL_RCC_SYSCFG_CLK_ENABLE();
  284. /* Disable fast mode plus driving capability for selected pin */
  285. CLEAR_BIT(SYSCFG->CFGR1, (uint32_t)ConfigFastModePlus);
  286. }
  287. /**
  288. * @}
  289. */
  290. /**
  291. * @}
  292. */
  293. #endif /* HAL_I2C_MODULE_ENABLED */
  294. /**
  295. * @}
  296. */
  297. /**
  298. * @}
  299. */
  300. /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/