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.
 
 
 

244 lines
7.9 KiB

  1. /**
  2. ******************************************************************************
  3. * @file stm32h7xx_ll_i2c.c
  4. * @author MCD Application Team
  5. * @brief I2C LL module driver.
  6. ******************************************************************************
  7. * @attention
  8. *
  9. * <h2><center>&copy; Copyright (c) 2017 STMicroelectronics.
  10. * All rights reserved.</center></h2>
  11. *
  12. * This software component is licensed by ST under BSD 3-Clause license,
  13. * the "License"; You may not use this file except in compliance with the
  14. * License. You may obtain a copy of the License at:
  15. * opensource.org/licenses/BSD-3-Clause
  16. *
  17. ******************************************************************************
  18. */
  19. #if defined(USE_FULL_LL_DRIVER)
  20. /* Includes ------------------------------------------------------------------*/
  21. #include "stm32h7xx_ll_i2c.h"
  22. #include "stm32h7xx_ll_bus.h"
  23. #ifdef USE_FULL_ASSERT
  24. #include "stm32_assert.h"
  25. #else
  26. #define assert_param(expr) ((void)0U)
  27. #endif
  28. /** @addtogroup STM32H7xx_LL_Driver
  29. * @{
  30. */
  31. #if defined (I2C1) || defined (I2C2) || defined (I2C3) || defined (I2C4)
  32. /** @defgroup I2C_LL I2C
  33. * @{
  34. */
  35. /* Private types -------------------------------------------------------------*/
  36. /* Private variables ---------------------------------------------------------*/
  37. /* Private constants ---------------------------------------------------------*/
  38. /* Private macros ------------------------------------------------------------*/
  39. /** @addtogroup I2C_LL_Private_Macros
  40. * @{
  41. */
  42. #define IS_LL_I2C_PERIPHERAL_MODE(__VALUE__) (((__VALUE__) == LL_I2C_MODE_I2C) || \
  43. ((__VALUE__) == LL_I2C_MODE_SMBUS_HOST) || \
  44. ((__VALUE__) == LL_I2C_MODE_SMBUS_DEVICE) || \
  45. ((__VALUE__) == LL_I2C_MODE_SMBUS_DEVICE_ARP))
  46. #define IS_LL_I2C_ANALOG_FILTER(__VALUE__) (((__VALUE__) == LL_I2C_ANALOGFILTER_ENABLE) || \
  47. ((__VALUE__) == LL_I2C_ANALOGFILTER_DISABLE))
  48. #define IS_LL_I2C_DIGITAL_FILTER(__VALUE__) ((__VALUE__) <= 0x0000000FU)
  49. #define IS_LL_I2C_OWN_ADDRESS1(__VALUE__) ((__VALUE__) <= 0x000003FFU)
  50. #define IS_LL_I2C_TYPE_ACKNOWLEDGE(__VALUE__) (((__VALUE__) == LL_I2C_ACK) || \
  51. ((__VALUE__) == LL_I2C_NACK))
  52. #define IS_LL_I2C_OWN_ADDRSIZE(__VALUE__) (((__VALUE__) == LL_I2C_OWNADDRESS1_7BIT) || \
  53. ((__VALUE__) == LL_I2C_OWNADDRESS1_10BIT))
  54. /**
  55. * @}
  56. */
  57. /* Private function prototypes -----------------------------------------------*/
  58. /* Exported functions --------------------------------------------------------*/
  59. /** @addtogroup I2C_LL_Exported_Functions
  60. * @{
  61. */
  62. /** @addtogroup I2C_LL_EF_Init
  63. * @{
  64. */
  65. /**
  66. * @brief De-initialize the I2C registers to their default reset values.
  67. * @param I2Cx I2C Instance.
  68. * @retval An ErrorStatus enumeration value:
  69. * - SUCCESS: I2C registers are de-initialized
  70. * - ERROR: I2C registers are not de-initialized
  71. */
  72. ErrorStatus LL_I2C_DeInit(I2C_TypeDef *I2Cx)
  73. {
  74. ErrorStatus status = SUCCESS;
  75. /* Check the I2C Instance I2Cx */
  76. assert_param(IS_I2C_ALL_INSTANCE(I2Cx));
  77. if (I2Cx == I2C1)
  78. {
  79. /* Force reset of I2C clock */
  80. LL_APB1_GRP1_ForceReset(LL_APB1_GRP1_PERIPH_I2C1);
  81. /* Release reset of I2C clock */
  82. LL_APB1_GRP1_ReleaseReset(LL_APB1_GRP1_PERIPH_I2C1);
  83. }
  84. else if (I2Cx == I2C2)
  85. {
  86. /* Force reset of I2C clock */
  87. LL_APB1_GRP1_ForceReset(LL_APB1_GRP1_PERIPH_I2C2);
  88. /* Release reset of I2C clock */
  89. LL_APB1_GRP1_ReleaseReset(LL_APB1_GRP1_PERIPH_I2C2);
  90. }
  91. else if (I2Cx == I2C3)
  92. {
  93. /* Force reset of I2C clock */
  94. LL_APB1_GRP1_ForceReset(LL_APB1_GRP1_PERIPH_I2C3);
  95. /* Release reset of I2C clock */
  96. LL_APB1_GRP1_ReleaseReset(LL_APB1_GRP1_PERIPH_I2C3);
  97. }
  98. else if (I2Cx == I2C4)
  99. {
  100. /* Force reset of I2C clock */
  101. LL_APB4_GRP1_ForceReset(LL_APB4_GRP1_PERIPH_I2C4);
  102. /* Release reset of I2C clock */
  103. LL_APB4_GRP1_ReleaseReset(LL_APB4_GRP1_PERIPH_I2C4);
  104. }
  105. else
  106. {
  107. status = ERROR;
  108. }
  109. return status;
  110. }
  111. /**
  112. * @brief Initialize the I2C registers according to the specified parameters in I2C_InitStruct.
  113. * @param I2Cx I2C Instance.
  114. * @param I2C_InitStruct pointer to a @ref LL_I2C_InitTypeDef structure.
  115. * @retval An ErrorStatus enumeration value:
  116. * - SUCCESS: I2C registers are initialized
  117. * - ERROR: Not applicable
  118. */
  119. ErrorStatus LL_I2C_Init(I2C_TypeDef *I2Cx, LL_I2C_InitTypeDef *I2C_InitStruct)
  120. {
  121. /* Check the I2C Instance I2Cx */
  122. assert_param(IS_I2C_ALL_INSTANCE(I2Cx));
  123. /* Check the I2C parameters from I2C_InitStruct */
  124. assert_param(IS_LL_I2C_PERIPHERAL_MODE(I2C_InitStruct->PeripheralMode));
  125. assert_param(IS_LL_I2C_ANALOG_FILTER(I2C_InitStruct->AnalogFilter));
  126. assert_param(IS_LL_I2C_DIGITAL_FILTER(I2C_InitStruct->DigitalFilter));
  127. assert_param(IS_LL_I2C_OWN_ADDRESS1(I2C_InitStruct->OwnAddress1));
  128. assert_param(IS_LL_I2C_TYPE_ACKNOWLEDGE(I2C_InitStruct->TypeAcknowledge));
  129. assert_param(IS_LL_I2C_OWN_ADDRSIZE(I2C_InitStruct->OwnAddrSize));
  130. /* Disable the selected I2Cx Peripheral */
  131. LL_I2C_Disable(I2Cx);
  132. /*---------------------------- I2Cx CR1 Configuration ------------------------
  133. * Configure the analog and digital noise filters with parameters :
  134. * - AnalogFilter: I2C_CR1_ANFOFF bit
  135. * - DigitalFilter: I2C_CR1_DNF[3:0] bits
  136. */
  137. LL_I2C_ConfigFilters(I2Cx, I2C_InitStruct->AnalogFilter, I2C_InitStruct->DigitalFilter);
  138. /*---------------------------- I2Cx TIMINGR Configuration --------------------
  139. * Configure the SDA setup, hold time and the SCL high, low period with parameter :
  140. * - Timing: I2C_TIMINGR_PRESC[3:0], I2C_TIMINGR_SCLDEL[3:0], I2C_TIMINGR_SDADEL[3:0],
  141. * I2C_TIMINGR_SCLH[7:0] and I2C_TIMINGR_SCLL[7:0] bits
  142. */
  143. LL_I2C_SetTiming(I2Cx, I2C_InitStruct->Timing);
  144. /* Enable the selected I2Cx Peripheral */
  145. LL_I2C_Enable(I2Cx);
  146. /*---------------------------- I2Cx OAR1 Configuration -----------------------
  147. * Disable, Configure and Enable I2Cx device own address 1 with parameters :
  148. * - OwnAddress1: I2C_OAR1_OA1[9:0] bits
  149. * - OwnAddrSize: I2C_OAR1_OA1MODE bit
  150. */
  151. LL_I2C_DisableOwnAddress1(I2Cx);
  152. LL_I2C_SetOwnAddress1(I2Cx, I2C_InitStruct->OwnAddress1, I2C_InitStruct->OwnAddrSize);
  153. /* OwnAdress1 == 0 is reserved for General Call address */
  154. if (I2C_InitStruct->OwnAddress1 != 0U)
  155. {
  156. LL_I2C_EnableOwnAddress1(I2Cx);
  157. }
  158. /*---------------------------- I2Cx MODE Configuration -----------------------
  159. * Configure I2Cx peripheral mode with parameter :
  160. * - PeripheralMode: I2C_CR1_SMBDEN and I2C_CR1_SMBHEN bits
  161. */
  162. LL_I2C_SetMode(I2Cx, I2C_InitStruct->PeripheralMode);
  163. /*---------------------------- I2Cx CR2 Configuration ------------------------
  164. * Configure the ACKnowledge or Non ACKnowledge condition
  165. * after the address receive match code or next received byte with parameter :
  166. * - TypeAcknowledge: I2C_CR2_NACK bit
  167. */
  168. LL_I2C_AcknowledgeNextData(I2Cx, I2C_InitStruct->TypeAcknowledge);
  169. return SUCCESS;
  170. }
  171. /**
  172. * @brief Set each @ref LL_I2C_InitTypeDef field to default value.
  173. * @param I2C_InitStruct Pointer to a @ref LL_I2C_InitTypeDef structure.
  174. * @retval None
  175. */
  176. void LL_I2C_StructInit(LL_I2C_InitTypeDef *I2C_InitStruct)
  177. {
  178. /* Set I2C_InitStruct fields to default values */
  179. I2C_InitStruct->PeripheralMode = LL_I2C_MODE_I2C;
  180. I2C_InitStruct->Timing = 0U;
  181. I2C_InitStruct->AnalogFilter = LL_I2C_ANALOGFILTER_ENABLE;
  182. I2C_InitStruct->DigitalFilter = 0U;
  183. I2C_InitStruct->OwnAddress1 = 0U;
  184. I2C_InitStruct->TypeAcknowledge = LL_I2C_NACK;
  185. I2C_InitStruct->OwnAddrSize = LL_I2C_OWNADDRESS1_7BIT;
  186. }
  187. /**
  188. * @}
  189. */
  190. /**
  191. * @}
  192. */
  193. /**
  194. * @}
  195. */
  196. #endif /* I2C1 || I2C2 || I2C3 || I2C4 */
  197. /**
  198. * @}
  199. */
  200. #endif /* USE_FULL_LL_DRIVER */
  201. /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/