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.
 
 
 

356 lines
12 KiB

  1. /**
  2. ******************************************************************************
  3. * @file stm32l4xx_hal_i2c_ex.c
  4. * @author MCD Application Team
  5. * @version V1.7.2
  6. * @date 16-June-2017
  7. * @brief I2C Extended HAL module driver.
  8. * This file provides firmware functions to manage the following
  9. * functionalities of I2C Extended peripheral:
  10. * + Extended features functions
  11. *
  12. @verbatim
  13. ==============================================================================
  14. ##### I2C peripheral Extended features #####
  15. ==============================================================================
  16. [..] Comparing to other previous devices, the I2C interface for STM32L4xx
  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 modes
  21. ##### How to use this driver #####
  22. ==============================================================================
  23. [..] This driver provides functions to configure Noise Filter and Wake Up Feature
  24. (#) Configure I2C Analog noise filter using the function HAL_I2CEx_ConfigAnalogFilter()
  25. (#) Configure I2C Digital noise filter using the function HAL_I2CEx_ConfigDigitalFilter()
  26. (#) Configure the enable or disable of I2C Wake Up Mode using the functions :
  27. (++) HAL_I2CEx_EnableWakeUp()
  28. (++) HAL_I2CEx_DisableWakeUp()
  29. (#) Configure the enable or disable of fast mode plus driving capability using the functions :
  30. (++) HAL_I2CEx_EnableFastModePlus()
  31. (++) HAL_I2CEx_DisableFastModePlus()
  32. @endverbatim
  33. ******************************************************************************
  34. * @attention
  35. *
  36. * <h2><center>&copy; COPYRIGHT(c) 2017 STMicroelectronics</center></h2>
  37. *
  38. * Redistribution and use in source and binary forms, with or without modification,
  39. * are permitted provided that the following conditions are met:
  40. * 1. Redistributions of source code must retain the above copyright notice,
  41. * this list of conditions and the following disclaimer.
  42. * 2. Redistributions in binary form must reproduce the above copyright notice,
  43. * this list of conditions and the following disclaimer in the documentation
  44. * and/or other materials provided with the distribution.
  45. * 3. Neither the name of STMicroelectronics nor the names of its contributors
  46. * may be used to endorse or promote products derived from this software
  47. * without specific prior written permission.
  48. *
  49. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  50. * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  51. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  52. * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
  53. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  54. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
  55. * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  56. * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
  57. * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  58. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  59. *
  60. ******************************************************************************
  61. */
  62. /* Includes ------------------------------------------------------------------*/
  63. #include "stm32l4xx_hal.h"
  64. /** @addtogroup STM32L4xx_HAL_Driver
  65. * @{
  66. */
  67. /** @defgroup I2CEx I2CEx
  68. * @brief I2C Extended HAL module driver
  69. * @{
  70. */
  71. #ifdef HAL_I2C_MODULE_ENABLED
  72. /* Private typedef -----------------------------------------------------------*/
  73. /* Private define ------------------------------------------------------------*/
  74. /* Private macro -------------------------------------------------------------*/
  75. /* Private variables ---------------------------------------------------------*/
  76. /* Private function prototypes -----------------------------------------------*/
  77. /* Private functions ---------------------------------------------------------*/
  78. /** @defgroup I2CEx_Exported_Functions I2C Extended Exported Functions
  79. * @{
  80. */
  81. /** @defgroup I2CEx_Exported_Functions_Group1 Extended features functions
  82. * @brief Extended features functions
  83. *
  84. @verbatim
  85. ===============================================================================
  86. ##### Extended features functions #####
  87. ===============================================================================
  88. [..] This section provides functions allowing to:
  89. (+) Configure Noise Filters
  90. (+) Configure Wake Up Feature
  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.
  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.
  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****/