stm32l4xx_ll_i2c.c 7.9 KB

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