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.
 
 
 

326 lines
9.7 KiB

  1. /**
  2. ******************************************************************************
  3. * @file stm32wbxx_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 STM32WBxx
  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(s)
  19. (+) Disable or enable Fast Mode Plus
  20. ##### How to use this driver #####
  21. ==============================================================================
  22. [..] This driver provides functions to configure Noise Filter and Wake Up Feature
  23. (#) Configure I2C Analog noise filter using the function HAL_I2CEx_ConfigAnalogFilter()
  24. (#) Configure I2C Digital noise filter using the function HAL_I2CEx_ConfigDigitalFilter()
  25. (#) Configure the enable or disable of I2C Wake Up Mode using the functions :
  26. (++) HAL_I2CEx_EnableWakeUp()
  27. (++) HAL_I2CEx_DisableWakeUp()
  28. (#) Configure the enable or disable of fast mode plus driving capability using the functions :
  29. (++) HAL_I2CEx_EnableFastModePlus()
  30. (++) HAL_I2CEx_DisableFastModePlus()
  31. @endverbatim
  32. ******************************************************************************
  33. * @attention
  34. *
  35. * <h2><center>&copy; Copyright (c) 2019 STMicroelectronics.
  36. * All rights reserved.</center></h2>
  37. *
  38. * This software component is licensed by ST under BSD 3-Clause license,
  39. * the "License"; You may not use this file except in compliance with the
  40. * License. You may obtain a copy of the License at:
  41. * opensource.org/licenses/BSD-3-Clause
  42. *
  43. ******************************************************************************
  44. */
  45. /* Includes ------------------------------------------------------------------*/
  46. #include "stm32wbxx_hal.h"
  47. /** @addtogroup STM32WBxx_HAL_Driver
  48. * @{
  49. */
  50. /** @defgroup I2CEx I2CEx
  51. * @brief I2C Extended HAL module driver
  52. * @{
  53. */
  54. #ifdef HAL_I2C_MODULE_ENABLED
  55. /* Private typedef -----------------------------------------------------------*/
  56. /* Private define ------------------------------------------------------------*/
  57. /* Private macro -------------------------------------------------------------*/
  58. /* Private variables ---------------------------------------------------------*/
  59. /* Private function prototypes -----------------------------------------------*/
  60. /* Private functions ---------------------------------------------------------*/
  61. /** @defgroup I2CEx_Exported_Functions I2C Extended Exported Functions
  62. * @{
  63. */
  64. /** @defgroup I2CEx_Exported_Functions_Group1 Extended features functions
  65. * @brief Extended features functions
  66. *
  67. @verbatim
  68. ===============================================================================
  69. ##### Extended features functions #####
  70. ===============================================================================
  71. [..] This section provides functions allowing to:
  72. (+) Configure Noise Filters
  73. (+) Configure Wake Up Feature
  74. (+) Configure Fast Mode Plus
  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. * @brief Enable I2C wakeup from Stop mode(s).
  153. * @param hi2c Pointer to a I2C_HandleTypeDef structure that contains
  154. * the configuration information for the specified I2Cx peripheral.
  155. * @retval HAL status
  156. */
  157. HAL_StatusTypeDef HAL_I2CEx_EnableWakeUp(I2C_HandleTypeDef *hi2c)
  158. {
  159. /* Check the parameters */
  160. assert_param(IS_I2C_WAKEUP_FROMSTOP_INSTANCE(hi2c->Instance));
  161. if (hi2c->State == HAL_I2C_STATE_READY)
  162. {
  163. /* Process Locked */
  164. __HAL_LOCK(hi2c);
  165. hi2c->State = HAL_I2C_STATE_BUSY;
  166. /* Disable the selected I2C peripheral */
  167. __HAL_I2C_DISABLE(hi2c);
  168. /* Enable wakeup from stop mode */
  169. hi2c->Instance->CR1 |= I2C_CR1_WUPEN;
  170. __HAL_I2C_ENABLE(hi2c);
  171. hi2c->State = HAL_I2C_STATE_READY;
  172. /* Process Unlocked */
  173. __HAL_UNLOCK(hi2c);
  174. return HAL_OK;
  175. }
  176. else
  177. {
  178. return HAL_BUSY;
  179. }
  180. }
  181. /**
  182. * @brief Disable I2C wakeup from Stop mode(s).
  183. * @param hi2c Pointer to a I2C_HandleTypeDef structure that contains
  184. * the configuration information for the specified I2Cx peripheral.
  185. * @retval HAL status
  186. */
  187. HAL_StatusTypeDef HAL_I2CEx_DisableWakeUp(I2C_HandleTypeDef *hi2c)
  188. {
  189. /* Check the parameters */
  190. assert_param(IS_I2C_WAKEUP_FROMSTOP_INSTANCE(hi2c->Instance));
  191. if (hi2c->State == HAL_I2C_STATE_READY)
  192. {
  193. /* Process Locked */
  194. __HAL_LOCK(hi2c);
  195. hi2c->State = HAL_I2C_STATE_BUSY;
  196. /* Disable the selected I2C peripheral */
  197. __HAL_I2C_DISABLE(hi2c);
  198. /* Enable wakeup from stop mode */
  199. hi2c->Instance->CR1 &= ~(I2C_CR1_WUPEN);
  200. __HAL_I2C_ENABLE(hi2c);
  201. hi2c->State = HAL_I2C_STATE_READY;
  202. /* Process Unlocked */
  203. __HAL_UNLOCK(hi2c);
  204. return HAL_OK;
  205. }
  206. else
  207. {
  208. return HAL_BUSY;
  209. }
  210. }
  211. /**
  212. * @brief Enable the I2C fast mode plus driving capability.
  213. * @param ConfigFastModePlus Selects the pin.
  214. * This parameter can be one of the @ref I2CEx_FastModePlus values
  215. * @note For I2C1, fast mode plus driving capability can be enabled on all selected
  216. * I2C1 pins using I2C_FASTMODEPLUS_I2C1 parameter or independently
  217. * on each one of the following pins PB6, PB7, PB8 and PB9.
  218. * @note For remaining I2C1 pins (PA14, PA15...) fast mode plus driving capability
  219. * can be enabled only by using I2C_FASTMODEPLUS_I2C1 parameter.
  220. * @note For all I2C3 pins fast mode plus driving capability can be enabled
  221. * only by using I2C_FASTMODEPLUS_I2C3 parameter.
  222. * @retval None
  223. */
  224. void HAL_I2CEx_EnableFastModePlus(uint32_t ConfigFastModePlus)
  225. {
  226. /* Check the parameter */
  227. assert_param(IS_I2C_FASTMODEPLUS(ConfigFastModePlus));
  228. /* Enable fast mode plus driving capability for selected pin */
  229. SET_BIT(SYSCFG->CFGR1, (uint32_t)ConfigFastModePlus);
  230. }
  231. /**
  232. * @brief Disable the I2C fast mode plus driving capability.
  233. * @param ConfigFastModePlus Selects the pin.
  234. * This parameter can be one of the @ref I2CEx_FastModePlus values
  235. * @note For I2C1, fast mode plus driving capability can be disabled on all selected
  236. * I2C1 pins using I2C_FASTMODEPLUS_I2C1 parameter or independently
  237. * on each one of the following pins PB6, PB7, PB8 and PB9.
  238. * @note For remaining I2C1 pins (PA14, PA15...) fast mode plus driving capability
  239. * can be disabled only by using I2C_FASTMODEPLUS_I2C1 parameter.
  240. * @note For all I2C3 pins fast mode plus driving capability can be disabled
  241. * only by using I2C_FASTMODEPLUS_I2C3 parameter.
  242. * @retval None
  243. */
  244. void HAL_I2CEx_DisableFastModePlus(uint32_t ConfigFastModePlus)
  245. {
  246. /* Check the parameter */
  247. assert_param(IS_I2C_FASTMODEPLUS(ConfigFastModePlus));
  248. /* Disable fast mode plus driving capability for selected pin */
  249. CLEAR_BIT(SYSCFG->CFGR1, (uint32_t)ConfigFastModePlus);
  250. }
  251. /**
  252. * @}
  253. */
  254. /**
  255. * @}
  256. */
  257. #endif /* HAL_I2C_MODULE_ENABLED */
  258. /**
  259. * @}
  260. */
  261. /**
  262. * @}
  263. */
  264. /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/