stm32f1xx_ll_i2c.c 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220
  1. /**
  2. ******************************************************************************
  3. * @file stm32f1xx_ll_i2c.c
  4. * @author MCD Application Team
  5. * @brief I2C LL module driver.
  6. ******************************************************************************
  7. * @attention
  8. *
  9. * Copyright (c) 2016 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 "stm32f1xx_ll_i2c.h"
  21. #include "stm32f1xx_ll_bus.h"
  22. #include "stm32f1xx_ll_rcc.h"
  23. #ifdef USE_FULL_ASSERT
  24. #include "stm32_assert.h"
  25. #else
  26. #define assert_param(expr) ((void)0U)
  27. #endif
  28. /** @addtogroup STM32F1xx_LL_Driver
  29. * @{
  30. */
  31. #if defined (I2C1) || defined (I2C2)
  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_CLOCK_SPEED(__VALUE__) (((__VALUE__) > 0U) && ((__VALUE__) <= LL_I2C_MAX_SPEED_FAST))
  47. #define IS_LL_I2C_DUTY_CYCLE(__VALUE__) (((__VALUE__) == LL_I2C_DUTYCYCLE_2) || \
  48. ((__VALUE__) == LL_I2C_DUTYCYCLE_16_9))
  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. uint32_t 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. #if defined(I2C2)
  85. else if (I2Cx == I2C2)
  86. {
  87. /* Force reset of I2C clock */
  88. LL_APB1_GRP1_ForceReset(LL_APB1_GRP1_PERIPH_I2C2);
  89. /* Release reset of I2C clock */
  90. LL_APB1_GRP1_ReleaseReset(LL_APB1_GRP1_PERIPH_I2C2);
  91. }
  92. #endif /* I2C2 */
  93. else
  94. {
  95. status = ERROR;
  96. }
  97. return status;
  98. }
  99. /**
  100. * @brief Initialize the I2C registers according to the specified parameters in I2C_InitStruct.
  101. * @param I2Cx I2C Instance.
  102. * @param I2C_InitStruct pointer to a @ref LL_I2C_InitTypeDef structure.
  103. * @retval An ErrorStatus enumeration value:
  104. * - SUCCESS I2C registers are initialized
  105. * - ERROR Not applicable
  106. */
  107. uint32_t LL_I2C_Init(I2C_TypeDef *I2Cx, LL_I2C_InitTypeDef *I2C_InitStruct)
  108. {
  109. LL_RCC_ClocksTypeDef rcc_clocks;
  110. /* Check the I2C Instance I2Cx */
  111. assert_param(IS_I2C_ALL_INSTANCE(I2Cx));
  112. /* Check the I2C parameters from I2C_InitStruct */
  113. assert_param(IS_LL_I2C_PERIPHERAL_MODE(I2C_InitStruct->PeripheralMode));
  114. assert_param(IS_LL_I2C_CLOCK_SPEED(I2C_InitStruct->ClockSpeed));
  115. assert_param(IS_LL_I2C_DUTY_CYCLE(I2C_InitStruct->DutyCycle));
  116. assert_param(IS_LL_I2C_OWN_ADDRESS1(I2C_InitStruct->OwnAddress1));
  117. assert_param(IS_LL_I2C_TYPE_ACKNOWLEDGE(I2C_InitStruct->TypeAcknowledge));
  118. assert_param(IS_LL_I2C_OWN_ADDRSIZE(I2C_InitStruct->OwnAddrSize));
  119. /* Disable the selected I2Cx Peripheral */
  120. LL_I2C_Disable(I2Cx);
  121. /* Retrieve Clock frequencies */
  122. LL_RCC_GetSystemClocksFreq(&rcc_clocks);
  123. /*---------------------------- I2Cx SCL Clock Speed Configuration ------------
  124. * Configure the SCL speed :
  125. * - ClockSpeed: I2C_CR2_FREQ[5:0], I2C_TRISE_TRISE[5:0], I2C_CCR_FS,
  126. * and I2C_CCR_CCR[11:0] bits
  127. * - DutyCycle: I2C_CCR_DUTY[7:0] bits
  128. */
  129. LL_I2C_ConfigSpeed(I2Cx, rcc_clocks.PCLK1_Frequency, I2C_InitStruct->ClockSpeed, I2C_InitStruct->DutyCycle);
  130. /*---------------------------- I2Cx OAR1 Configuration -----------------------
  131. * Disable, Configure and Enable I2Cx device own address 1 with parameters :
  132. * - OwnAddress1: I2C_OAR1_ADD[9:8], I2C_OAR1_ADD[7:1] and I2C_OAR1_ADD0 bits
  133. * - OwnAddrSize: I2C_OAR1_ADDMODE bit
  134. */
  135. LL_I2C_SetOwnAddress1(I2Cx, I2C_InitStruct->OwnAddress1, I2C_InitStruct->OwnAddrSize);
  136. /*---------------------------- I2Cx MODE Configuration -----------------------
  137. * Configure I2Cx peripheral mode with parameter :
  138. * - PeripheralMode: I2C_CR1_SMBUS, I2C_CR1_SMBTYPE and I2C_CR1_ENARP bits
  139. */
  140. LL_I2C_SetMode(I2Cx, I2C_InitStruct->PeripheralMode);
  141. /* Enable the selected I2Cx Peripheral */
  142. LL_I2C_Enable(I2Cx);
  143. /*---------------------------- I2Cx CR2 Configuration ------------------------
  144. * Configure the ACKnowledge or Non ACKnowledge condition
  145. * after the address receive match code or next received byte with parameter :
  146. * - TypeAcknowledge: I2C_CR2_NACK bit
  147. */
  148. LL_I2C_AcknowledgeNextData(I2Cx, I2C_InitStruct->TypeAcknowledge);
  149. return SUCCESS;
  150. }
  151. /**
  152. * @brief Set each @ref LL_I2C_InitTypeDef field to default value.
  153. * @param I2C_InitStruct Pointer to a @ref LL_I2C_InitTypeDef structure.
  154. * @retval None
  155. */
  156. void LL_I2C_StructInit(LL_I2C_InitTypeDef *I2C_InitStruct)
  157. {
  158. /* Set I2C_InitStruct fields to default values */
  159. I2C_InitStruct->PeripheralMode = LL_I2C_MODE_I2C;
  160. I2C_InitStruct->ClockSpeed = 5000U;
  161. I2C_InitStruct->DutyCycle = LL_I2C_DUTYCYCLE_2;
  162. I2C_InitStruct->OwnAddress1 = 0U;
  163. I2C_InitStruct->TypeAcknowledge = LL_I2C_NACK;
  164. I2C_InitStruct->OwnAddrSize = LL_I2C_OWNADDRESS1_7BIT;
  165. }
  166. /**
  167. * @}
  168. */
  169. /**
  170. * @}
  171. */
  172. /**
  173. * @}
  174. */
  175. #endif /* I2C1 || I2C2 */
  176. /**
  177. * @}
  178. */
  179. #endif /* USE_FULL_LL_DRIVER */