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.
 
 
 

351 lines
11 KiB

  1. /**
  2. ******************************************************************************
  3. * @file stm32l4xx_hal_i2c_ex.c
  4. * @author MCD Application Team
  5. * @version V1.3.0
  6. * @date 29-January-2016
  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_DisbleFastModePlus()
  32. @endverbatim
  33. ******************************************************************************
  34. * @attention
  35. *
  36. * <h2><center>&copy; COPYRIGHT(c) 2016 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. @endverbatim
  91. * @{
  92. */
  93. /**
  94. * @brief Configure I2C Analog noise filter.
  95. * @param hi2c Pointer to a I2C_HandleTypeDef structure that contains
  96. * the configuration information for the specified I2Cx peripheral.
  97. * @param AnalogFilter New state of the Analog filter.
  98. * @retval HAL status
  99. */
  100. HAL_StatusTypeDef HAL_I2CEx_ConfigAnalogFilter(I2C_HandleTypeDef *hi2c, uint32_t AnalogFilter)
  101. {
  102. /* Check the parameters */
  103. assert_param(IS_I2C_ALL_INSTANCE(hi2c->Instance));
  104. assert_param(IS_I2C_ANALOG_FILTER(AnalogFilter));
  105. if(hi2c->State == HAL_I2C_STATE_READY)
  106. {
  107. /* Process Locked */
  108. __HAL_LOCK(hi2c);
  109. hi2c->State = HAL_I2C_STATE_BUSY;
  110. /* Disable the selected I2C peripheral */
  111. __HAL_I2C_DISABLE(hi2c);
  112. /* Reset I2Cx ANOFF bit */
  113. hi2c->Instance->CR1 &= ~(I2C_CR1_ANFOFF);
  114. /* Set analog filter bit*/
  115. hi2c->Instance->CR1 |= AnalogFilter;
  116. __HAL_I2C_ENABLE(hi2c);
  117. hi2c->State = HAL_I2C_STATE_READY;
  118. /* Process Unlocked */
  119. __HAL_UNLOCK(hi2c);
  120. return HAL_OK;
  121. }
  122. else
  123. {
  124. return HAL_BUSY;
  125. }
  126. }
  127. /**
  128. * @brief Configure I2C Digital noise filter.
  129. * @param hi2c Pointer to a I2C_HandleTypeDef structure that contains
  130. * the configuration information for the specified I2Cx peripheral.
  131. * @param DigitalFilter Coefficient of digital noise filter between 0x00 and 0x0F.
  132. * @retval HAL status
  133. */
  134. HAL_StatusTypeDef HAL_I2CEx_ConfigDigitalFilter(I2C_HandleTypeDef *hi2c, uint32_t DigitalFilter)
  135. {
  136. uint32_t tmpreg = 0;
  137. /* Check the parameters */
  138. assert_param(IS_I2C_ALL_INSTANCE(hi2c->Instance));
  139. assert_param(IS_I2C_DIGITAL_FILTER(DigitalFilter));
  140. if(hi2c->State == HAL_I2C_STATE_READY)
  141. {
  142. /* Process Locked */
  143. __HAL_LOCK(hi2c);
  144. hi2c->State = HAL_I2C_STATE_BUSY;
  145. /* Disable the selected I2C peripheral */
  146. __HAL_I2C_DISABLE(hi2c);
  147. /* Get the old register value */
  148. tmpreg = hi2c->Instance->CR1;
  149. /* Reset I2Cx DNF bits [11:8] */
  150. tmpreg &= ~(I2C_CR1_DNF);
  151. /* Set I2Cx DNF coefficient */
  152. tmpreg |= DigitalFilter << 8;
  153. /* Store the new register value */
  154. hi2c->Instance->CR1 = tmpreg;
  155. __HAL_I2C_ENABLE(hi2c);
  156. hi2c->State = HAL_I2C_STATE_READY;
  157. /* Process Unlocked */
  158. __HAL_UNLOCK(hi2c);
  159. return HAL_OK;
  160. }
  161. else
  162. {
  163. return HAL_BUSY;
  164. }
  165. }
  166. /**
  167. * @brief Enable I2C wakeup from stop mode.
  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_ALL_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.
  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_ALL_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. * @brief Enable the I2C fast mode plus driving capability.
  228. * @param ConfigFastModePlus Selects the pin.
  229. * This parameter can be one of the @ref I2CEx_FastModePlus values
  230. * @note For I2C1, fast mode plus driving capability can be enabled on all selected
  231. * I2C1 pins using I2C_FASTMODEPLUS_I2C1 parameter or independently
  232. * on each one of the following pins PB6, PB7, PB8 and PB9.
  233. * @note For remaining I2C1 pins (PA14, PA15...) fast mode plus driving capability
  234. * can be enabled only by using I2C_FASTMODEPLUS_I2C1 parameter.
  235. * @note For all I2C2 pins fast mode plus driving capability can be enabled
  236. * only by using I2C_FASTMODEPLUS_I2C2 parameter.
  237. * @note For all I2C3 pins fast mode plus driving capability can be enabled
  238. * only by using I2C_FASTMODEPLUS_I2C3 parameter.
  239. * @retval None
  240. */
  241. void HAL_I2CEx_EnableFastModePlus(uint32_t ConfigFastModePlus)
  242. {
  243. /* Check the parameter */
  244. assert_param(IS_I2C_FASTMODEPLUS(ConfigFastModePlus));
  245. /* Enable SYSCFG clock */
  246. __HAL_RCC_SYSCFG_CLK_ENABLE();
  247. /* Enable fast mode plus driving capability for selected pin */
  248. SET_BIT(SYSCFG->CFGR1, (uint32_t)ConfigFastModePlus);
  249. }
  250. /**
  251. * @brief Disable the I2C fast mode plus driving capability.
  252. * @param ConfigFastModePlus Selects the pin.
  253. * This parameter can be one of the @ref I2CEx_FastModePlus values
  254. * @note For I2C1, fast mode plus driving capability can be disabled on all selected
  255. * I2C1 pins using I2C_FASTMODEPLUS_I2C1 parameter or independently
  256. * on each one of the following pins PB6, PB7, PB8 and PB9.
  257. * @note For remaining I2C1 pins (PA14, PA15...) fast mode plus driving capability
  258. * can be disabled only by using I2C_FASTMODEPLUS_I2C1 parameter.
  259. * @note For all I2C2 pins fast mode plus driving capability can be disabled
  260. * only by using I2C_FASTMODEPLUS_I2C2 parameter.
  261. * @note For all I2C3 pins fast mode plus driving capability can be disabled
  262. * only by using I2C_FASTMODEPLUS_I2C3 parameter.
  263. * @retval None
  264. */
  265. void HAL_I2CEx_DisableFastModePlus(uint32_t ConfigFastModePlus)
  266. {
  267. /* Check the parameter */
  268. assert_param(IS_I2C_FASTMODEPLUS(ConfigFastModePlus));
  269. /* Enable SYSCFG clock */
  270. __HAL_RCC_SYSCFG_CLK_ENABLE();
  271. /* Disable fast mode plus driving capability for selected pin */
  272. CLEAR_BIT(SYSCFG->CFGR1, (uint32_t)ConfigFastModePlus);
  273. }
  274. /**
  275. * @}
  276. */
  277. /**
  278. * @}
  279. */
  280. #endif /* HAL_I2C_MODULE_ENABLED */
  281. /**
  282. * @}
  283. */
  284. /**
  285. * @}
  286. */
  287. /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/