stm32h7xx_hal_i2c_ex.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372
  1. /**
  2. ******************************************************************************
  3. * @file stm32h7xx_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) 2017 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 STM32H7xx
  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 "stm32h7xx_hal.h"
  48. /** @addtogroup STM32H7xx_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. /** @defgroup I2CEx_Exported_Functions_Group2 WakeUp Mode Functions
  154. * @brief WakeUp Mode Functions
  155. *
  156. @verbatim
  157. ===============================================================================
  158. ##### WakeUp Mode Functions #####
  159. ===============================================================================
  160. [..] This section provides functions allowing to:
  161. (+) Configure Wake Up Feature
  162. @endverbatim
  163. * @{
  164. */
  165. /**
  166. * @brief Enable I2C wakeup from Stop mode(s).
  167. * @param hi2c Pointer to a I2C_HandleTypeDef structure that contains
  168. * the configuration information for the specified I2Cx peripheral.
  169. * @retval HAL status
  170. */
  171. HAL_StatusTypeDef HAL_I2CEx_EnableWakeUp(I2C_HandleTypeDef *hi2c)
  172. {
  173. /* Check the parameters */
  174. assert_param(IS_I2C_WAKEUP_FROMSTOP_INSTANCE(hi2c->Instance));
  175. if (hi2c->State == HAL_I2C_STATE_READY)
  176. {
  177. /* Process Locked */
  178. __HAL_LOCK(hi2c);
  179. hi2c->State = HAL_I2C_STATE_BUSY;
  180. /* Disable the selected I2C peripheral */
  181. __HAL_I2C_DISABLE(hi2c);
  182. /* Enable wakeup from stop mode */
  183. hi2c->Instance->CR1 |= I2C_CR1_WUPEN;
  184. __HAL_I2C_ENABLE(hi2c);
  185. hi2c->State = HAL_I2C_STATE_READY;
  186. /* Process Unlocked */
  187. __HAL_UNLOCK(hi2c);
  188. return HAL_OK;
  189. }
  190. else
  191. {
  192. return HAL_BUSY;
  193. }
  194. }
  195. /**
  196. * @brief Disable I2C wakeup from Stop mode(s).
  197. * @param hi2c Pointer to a I2C_HandleTypeDef structure that contains
  198. * the configuration information for the specified I2Cx peripheral.
  199. * @retval HAL status
  200. */
  201. HAL_StatusTypeDef HAL_I2CEx_DisableWakeUp(I2C_HandleTypeDef *hi2c)
  202. {
  203. /* Check the parameters */
  204. assert_param(IS_I2C_WAKEUP_FROMSTOP_INSTANCE(hi2c->Instance));
  205. if (hi2c->State == HAL_I2C_STATE_READY)
  206. {
  207. /* Process Locked */
  208. __HAL_LOCK(hi2c);
  209. hi2c->State = HAL_I2C_STATE_BUSY;
  210. /* Disable the selected I2C peripheral */
  211. __HAL_I2C_DISABLE(hi2c);
  212. /* Enable wakeup from stop mode */
  213. hi2c->Instance->CR1 &= ~(I2C_CR1_WUPEN);
  214. __HAL_I2C_ENABLE(hi2c);
  215. hi2c->State = HAL_I2C_STATE_READY;
  216. /* Process Unlocked */
  217. __HAL_UNLOCK(hi2c);
  218. return HAL_OK;
  219. }
  220. else
  221. {
  222. return HAL_BUSY;
  223. }
  224. }
  225. /**
  226. * @}
  227. */
  228. /** @defgroup I2CEx_Exported_Functions_Group3 Fast Mode Plus Functions
  229. * @brief Fast Mode Plus Functions
  230. *
  231. @verbatim
  232. ===============================================================================
  233. ##### Fast Mode Plus Functions #####
  234. ===============================================================================
  235. [..] This section provides functions allowing to:
  236. (+) Configure Fast Mode Plus
  237. @endverbatim
  238. * @{
  239. */
  240. /**
  241. * @brief Enable the I2C fast mode plus driving capability.
  242. * @param ConfigFastModePlus Selects the pin.
  243. * This parameter can be one of the @ref I2CEx_FastModePlus values
  244. * @note For I2C1, fast mode plus driving capability can be enabled on all selected
  245. * I2C1 pins using I2C_FASTMODEPLUS_I2C1 parameter or independently
  246. * on each one of the following pins PB6, PB7, PB8 and PB9.
  247. * @note For remaining I2C1 pins (PA14, PA15...) fast mode plus driving capability
  248. * can be enabled only by using I2C_FASTMODEPLUS_I2C1 parameter.
  249. * @note For all I2C2 pins fast mode plus driving capability can be enabled
  250. * only by using I2C_FASTMODEPLUS_I2C2 parameter.
  251. * @note For all I2C3 pins fast mode plus driving capability can be enabled
  252. * only by using I2C_FASTMODEPLUS_I2C3 parameter.
  253. * @note For all I2C4 pins fast mode plus driving capability can be enabled
  254. * only by using I2C_FASTMODEPLUS_I2C4 parameter.
  255. * @note For all I2C5 pins fast mode plus driving capability can be enabled
  256. * only by using I2C_FASTMODEPLUS_I2C5 parameter.
  257. * @retval None
  258. */
  259. void HAL_I2CEx_EnableFastModePlus(uint32_t ConfigFastModePlus)
  260. {
  261. /* Check the parameter */
  262. assert_param(IS_I2C_FASTMODEPLUS(ConfigFastModePlus));
  263. /* Enable SYSCFG clock */
  264. __HAL_RCC_SYSCFG_CLK_ENABLE();
  265. /* Enable fast mode plus driving capability for selected pin */
  266. SET_BIT(SYSCFG->PMCR, (uint32_t)ConfigFastModePlus);
  267. }
  268. /**
  269. * @brief Disable the I2C fast mode plus driving capability.
  270. * @param ConfigFastModePlus Selects the pin.
  271. * This parameter can be one of the @ref I2CEx_FastModePlus values
  272. * @note For I2C1, fast mode plus driving capability can be disabled on all selected
  273. * I2C1 pins using I2C_FASTMODEPLUS_I2C1 parameter or independently
  274. * on each one of the following pins PB6, PB7, PB8 and PB9.
  275. * @note For remaining I2C1 pins (PA14, PA15...) fast mode plus driving capability
  276. * can be disabled only by using I2C_FASTMODEPLUS_I2C1 parameter.
  277. * @note For all I2C2 pins fast mode plus driving capability can be disabled
  278. * only by using I2C_FASTMODEPLUS_I2C2 parameter.
  279. * @note For all I2C3 pins fast mode plus driving capability can be disabled
  280. * only by using I2C_FASTMODEPLUS_I2C3 parameter.
  281. * @note For all I2C4 pins fast mode plus driving capability can be disabled
  282. * only by using I2C_FASTMODEPLUS_I2C4 parameter.
  283. * @note For all I2C5 pins fast mode plus driving capability can be disabled
  284. * only by using I2C_FASTMODEPLUS_I2C5 parameter.
  285. * @retval None
  286. */
  287. void HAL_I2CEx_DisableFastModePlus(uint32_t ConfigFastModePlus)
  288. {
  289. /* Check the parameter */
  290. assert_param(IS_I2C_FASTMODEPLUS(ConfigFastModePlus));
  291. /* Enable SYSCFG clock */
  292. __HAL_RCC_SYSCFG_CLK_ENABLE();
  293. /* Disable fast mode plus driving capability for selected pin */
  294. CLEAR_BIT(SYSCFG->PMCR, (uint32_t)ConfigFastModePlus);
  295. }
  296. /**
  297. * @}
  298. */
  299. /**
  300. * @}
  301. */
  302. #endif /* HAL_I2C_MODULE_ENABLED */
  303. /**
  304. * @}
  305. */
  306. /**
  307. * @}
  308. */