stm32f0xx_hal_i2c_ex.c 11 KB

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