stm32l4xx_hal_i2c_ex.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355
  1. /**
  2. ******************************************************************************
  3. * @file stm32l4xx_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 STM32L4xx
  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) 2017 STMicroelectronics</center></h2>
  36. *
  37. * Redistribution and use in source and binary forms, with or without modification,
  38. * are permitted provided that the following conditions are met:
  39. * 1. Redistributions of source code must retain the above copyright notice,
  40. * this list of conditions and the following disclaimer.
  41. * 2. Redistributions in binary form must reproduce the above copyright notice,
  42. * this list of conditions and the following disclaimer in the documentation
  43. * and/or other materials provided with the distribution.
  44. * 3. Neither the name of STMicroelectronics nor the names of its contributors
  45. * may be used to endorse or promote products derived from this software
  46. * without specific prior written permission.
  47. *
  48. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  49. * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  50. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  51. * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
  52. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  53. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
  54. * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  55. * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
  56. * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  57. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  58. *
  59. ******************************************************************************
  60. */
  61. /* Includes ------------------------------------------------------------------*/
  62. #include "stm32l4xx_hal.h"
  63. /** @addtogroup STM32L4xx_HAL_Driver
  64. * @{
  65. */
  66. /** @defgroup I2CEx I2CEx
  67. * @brief I2C Extended HAL module driver
  68. * @{
  69. */
  70. #ifdef HAL_I2C_MODULE_ENABLED
  71. /* Private typedef -----------------------------------------------------------*/
  72. /* Private define ------------------------------------------------------------*/
  73. /* Private macro -------------------------------------------------------------*/
  74. /* Private variables ---------------------------------------------------------*/
  75. /* Private function prototypes -----------------------------------------------*/
  76. /* Private functions ---------------------------------------------------------*/
  77. /** @defgroup I2CEx_Exported_Functions I2C Extended Exported Functions
  78. * @{
  79. */
  80. /** @defgroup I2CEx_Exported_Functions_Group1 Extended features functions
  81. * @brief Extended features functions
  82. *
  83. @verbatim
  84. ===============================================================================
  85. ##### Extended features functions #####
  86. ===============================================================================
  87. [..] This section provides functions allowing to:
  88. (+) Configure Noise Filters
  89. (+) Configure Wake Up Feature
  90. (+) Configure Fast Mode Plus
  91. @endverbatim
  92. * @{
  93. */
  94. /**
  95. * @brief Configure I2C Analog noise filter.
  96. * @param hi2c Pointer to a I2C_HandleTypeDef structure that contains
  97. * the configuration information for the specified I2Cx peripheral.
  98. * @param AnalogFilter New state of the Analog filter.
  99. * @retval HAL status
  100. */
  101. HAL_StatusTypeDef HAL_I2CEx_ConfigAnalogFilter(I2C_HandleTypeDef *hi2c, uint32_t AnalogFilter)
  102. {
  103. /* Check the parameters */
  104. assert_param(IS_I2C_ALL_INSTANCE(hi2c->Instance));
  105. assert_param(IS_I2C_ANALOG_FILTER(AnalogFilter));
  106. if (hi2c->State == HAL_I2C_STATE_READY)
  107. {
  108. /* Process Locked */
  109. __HAL_LOCK(hi2c);
  110. hi2c->State = HAL_I2C_STATE_BUSY;
  111. /* Disable the selected I2C peripheral */
  112. __HAL_I2C_DISABLE(hi2c);
  113. /* Reset I2Cx ANOFF bit */
  114. hi2c->Instance->CR1 &= ~(I2C_CR1_ANFOFF);
  115. /* Set analog filter bit*/
  116. hi2c->Instance->CR1 |= AnalogFilter;
  117. __HAL_I2C_ENABLE(hi2c);
  118. hi2c->State = HAL_I2C_STATE_READY;
  119. /* Process Unlocked */
  120. __HAL_UNLOCK(hi2c);
  121. return HAL_OK;
  122. }
  123. else
  124. {
  125. return HAL_BUSY;
  126. }
  127. }
  128. /**
  129. * @brief Configure I2C Digital noise filter.
  130. * @param hi2c Pointer to a I2C_HandleTypeDef structure that contains
  131. * the configuration information for the specified I2Cx peripheral.
  132. * @param DigitalFilter Coefficient of digital noise filter between Min_Data=0x00 and Max_Data=0x0F.
  133. * @retval HAL status
  134. */
  135. HAL_StatusTypeDef HAL_I2CEx_ConfigDigitalFilter(I2C_HandleTypeDef *hi2c, uint32_t DigitalFilter)
  136. {
  137. uint32_t tmpreg = 0U;
  138. /* Check the parameters */
  139. assert_param(IS_I2C_ALL_INSTANCE(hi2c->Instance));
  140. assert_param(IS_I2C_DIGITAL_FILTER(DigitalFilter));
  141. if (hi2c->State == HAL_I2C_STATE_READY)
  142. {
  143. /* Process Locked */
  144. __HAL_LOCK(hi2c);
  145. hi2c->State = HAL_I2C_STATE_BUSY;
  146. /* Disable the selected I2C peripheral */
  147. __HAL_I2C_DISABLE(hi2c);
  148. /* Get the old register value */
  149. tmpreg = hi2c->Instance->CR1;
  150. /* Reset I2Cx DNF bits [11:8] */
  151. tmpreg &= ~(I2C_CR1_DNF);
  152. /* Set I2Cx DNF coefficient */
  153. tmpreg |= DigitalFilter << 8U;
  154. /* Store the new register value */
  155. hi2c->Instance->CR1 = tmpreg;
  156. __HAL_I2C_ENABLE(hi2c);
  157. hi2c->State = HAL_I2C_STATE_READY;
  158. /* Process Unlocked */
  159. __HAL_UNLOCK(hi2c);
  160. return HAL_OK;
  161. }
  162. else
  163. {
  164. return HAL_BUSY;
  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. * @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. * @note For all I2C3 pins fast mode plus driving capability can be enabled
  239. * only by using I2C_FASTMODEPLUS_I2C3 parameter.
  240. * @note For all I2C4 pins fast mode plus driving capability can be enabled
  241. * only by using I2C_FASTMODEPLUS_I2C4 parameter.
  242. * @retval None
  243. */
  244. void HAL_I2CEx_EnableFastModePlus(uint32_t ConfigFastModePlus)
  245. {
  246. /* Check the parameter */
  247. assert_param(IS_I2C_FASTMODEPLUS(ConfigFastModePlus));
  248. /* Enable SYSCFG clock */
  249. __HAL_RCC_SYSCFG_CLK_ENABLE();
  250. /* Enable fast mode plus driving capability for selected pin */
  251. SET_BIT(SYSCFG->CFGR1, (uint32_t)ConfigFastModePlus);
  252. }
  253. /**
  254. * @brief Disable the I2C fast mode plus driving capability.
  255. * @param ConfigFastModePlus Selects the pin.
  256. * This parameter can be one of the @ref I2CEx_FastModePlus values
  257. * @note For I2C1, fast mode plus driving capability can be disabled on all selected
  258. * I2C1 pins using I2C_FASTMODEPLUS_I2C1 parameter or independently
  259. * on each one of the following pins PB6, PB7, PB8 and PB9.
  260. * @note For remaining I2C1 pins (PA14, PA15...) fast mode plus driving capability
  261. * can be disabled only by using I2C_FASTMODEPLUS_I2C1 parameter.
  262. * @note For all I2C2 pins fast mode plus driving capability can be disabled
  263. * only by using I2C_FASTMODEPLUS_I2C2 parameter.
  264. * @note For all I2C3 pins fast mode plus driving capability can be disabled
  265. * only by using I2C_FASTMODEPLUS_I2C3 parameter.
  266. * @note For all I2C4 pins fast mode plus driving capability can be disabled
  267. * only by using I2C_FASTMODEPLUS_I2C4 parameter.
  268. * @retval None
  269. */
  270. void HAL_I2CEx_DisableFastModePlus(uint32_t ConfigFastModePlus)
  271. {
  272. /* Check the parameter */
  273. assert_param(IS_I2C_FASTMODEPLUS(ConfigFastModePlus));
  274. /* Enable SYSCFG clock */
  275. __HAL_RCC_SYSCFG_CLK_ENABLE();
  276. /* Disable fast mode plus driving capability for selected pin */
  277. CLEAR_BIT(SYSCFG->CFGR1, (uint32_t)ConfigFastModePlus);
  278. }
  279. /**
  280. * @}
  281. */
  282. /**
  283. * @}
  284. */
  285. #endif /* HAL_I2C_MODULE_ENABLED */
  286. /**
  287. * @}
  288. */
  289. /**
  290. * @}
  291. */
  292. /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/