stm32f1xx_ll_i2c.c 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221
  1. /**
  2. ******************************************************************************
  3. * @file stm32f1xx_ll_i2c.c
  4. * @author MCD Application Team
  5. * @brief I2C LL module driver.
  6. ******************************************************************************
  7. * @attention
  8. *
  9. * <h2><center>&copy; Copyright (c) 2016 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 "stm32f1xx_ll_i2c.h"
  22. #include "stm32f1xx_ll_bus.h"
  23. #include "stm32f1xx_ll_rcc.h"
  24. #ifdef USE_FULL_ASSERT
  25. #include "stm32_assert.h"
  26. #else
  27. #define assert_param(expr) ((void)0U)
  28. #endif
  29. /** @addtogroup STM32F1xx_LL_Driver
  30. * @{
  31. */
  32. #if defined (I2C1) || defined (I2C2)
  33. /** @defgroup I2C_LL I2C
  34. * @{
  35. */
  36. /* Private types -------------------------------------------------------------*/
  37. /* Private variables ---------------------------------------------------------*/
  38. /* Private constants ---------------------------------------------------------*/
  39. /* Private macros ------------------------------------------------------------*/
  40. /** @addtogroup I2C_LL_Private_Macros
  41. * @{
  42. */
  43. #define IS_LL_I2C_PERIPHERAL_MODE(__VALUE__) (((__VALUE__) == LL_I2C_MODE_I2C) || \
  44. ((__VALUE__) == LL_I2C_MODE_SMBUS_HOST) || \
  45. ((__VALUE__) == LL_I2C_MODE_SMBUS_DEVICE) || \
  46. ((__VALUE__) == LL_I2C_MODE_SMBUS_DEVICE_ARP))
  47. #define IS_LL_I2C_CLOCK_SPEED(__VALUE__) (((__VALUE__) > 0U) && ((__VALUE__) <= LL_I2C_MAX_SPEED_FAST))
  48. #define IS_LL_I2C_DUTY_CYCLE(__VALUE__) (((__VALUE__) == LL_I2C_DUTYCYCLE_2) || \
  49. ((__VALUE__) == LL_I2C_DUTYCYCLE_16_9))
  50. #define IS_LL_I2C_OWN_ADDRESS1(__VALUE__) ((__VALUE__) <= 0x000003FFU)
  51. #define IS_LL_I2C_TYPE_ACKNOWLEDGE(__VALUE__) (((__VALUE__) == LL_I2C_ACK) || \
  52. ((__VALUE__) == LL_I2C_NACK))
  53. #define IS_LL_I2C_OWN_ADDRSIZE(__VALUE__) (((__VALUE__) == LL_I2C_OWNADDRESS1_7BIT) || \
  54. ((__VALUE__) == LL_I2C_OWNADDRESS1_10BIT))
  55. /**
  56. * @}
  57. */
  58. /* Private function prototypes -----------------------------------------------*/
  59. /* Exported functions --------------------------------------------------------*/
  60. /** @addtogroup I2C_LL_Exported_Functions
  61. * @{
  62. */
  63. /** @addtogroup I2C_LL_EF_Init
  64. * @{
  65. */
  66. /**
  67. * @brief De-initialize the I2C registers to their default reset values.
  68. * @param I2Cx I2C Instance.
  69. * @retval An ErrorStatus enumeration value:
  70. * - SUCCESS I2C registers are de-initialized
  71. * - ERROR I2C registers are not de-initialized
  72. */
  73. uint32_t LL_I2C_DeInit(I2C_TypeDef *I2Cx)
  74. {
  75. ErrorStatus status = SUCCESS;
  76. /* Check the I2C Instance I2Cx */
  77. assert_param(IS_I2C_ALL_INSTANCE(I2Cx));
  78. if (I2Cx == I2C1)
  79. {
  80. /* Force reset of I2C clock */
  81. LL_APB1_GRP1_ForceReset(LL_APB1_GRP1_PERIPH_I2C1);
  82. /* Release reset of I2C clock */
  83. LL_APB1_GRP1_ReleaseReset(LL_APB1_GRP1_PERIPH_I2C1);
  84. }
  85. #if defined(I2C2)
  86. else if (I2Cx == I2C2)
  87. {
  88. /* Force reset of I2C clock */
  89. LL_APB1_GRP1_ForceReset(LL_APB1_GRP1_PERIPH_I2C2);
  90. /* Release reset of I2C clock */
  91. LL_APB1_GRP1_ReleaseReset(LL_APB1_GRP1_PERIPH_I2C2);
  92. }
  93. #endif /* I2C2 */
  94. else
  95. {
  96. status = ERROR;
  97. }
  98. return status;
  99. }
  100. /**
  101. * @brief Initialize the I2C registers according to the specified parameters in I2C_InitStruct.
  102. * @param I2Cx I2C Instance.
  103. * @param I2C_InitStruct pointer to a @ref LL_I2C_InitTypeDef structure.
  104. * @retval An ErrorStatus enumeration value:
  105. * - SUCCESS I2C registers are initialized
  106. * - ERROR Not applicable
  107. */
  108. uint32_t LL_I2C_Init(I2C_TypeDef *I2Cx, LL_I2C_InitTypeDef *I2C_InitStruct)
  109. {
  110. LL_RCC_ClocksTypeDef rcc_clocks;
  111. /* Check the I2C Instance I2Cx */
  112. assert_param(IS_I2C_ALL_INSTANCE(I2Cx));
  113. /* Check the I2C parameters from I2C_InitStruct */
  114. assert_param(IS_LL_I2C_PERIPHERAL_MODE(I2C_InitStruct->PeripheralMode));
  115. assert_param(IS_LL_I2C_CLOCK_SPEED(I2C_InitStruct->ClockSpeed));
  116. assert_param(IS_LL_I2C_DUTY_CYCLE(I2C_InitStruct->DutyCycle));
  117. assert_param(IS_LL_I2C_OWN_ADDRESS1(I2C_InitStruct->OwnAddress1));
  118. assert_param(IS_LL_I2C_TYPE_ACKNOWLEDGE(I2C_InitStruct->TypeAcknowledge));
  119. assert_param(IS_LL_I2C_OWN_ADDRSIZE(I2C_InitStruct->OwnAddrSize));
  120. /* Disable the selected I2Cx Peripheral */
  121. LL_I2C_Disable(I2Cx);
  122. /* Retrieve Clock frequencies */
  123. LL_RCC_GetSystemClocksFreq(&rcc_clocks);
  124. /*---------------------------- I2Cx SCL Clock Speed Configuration ------------
  125. * Configure the SCL speed :
  126. * - ClockSpeed: I2C_CR2_FREQ[5:0], I2C_TRISE_TRISE[5:0], I2C_CCR_FS,
  127. * and I2C_CCR_CCR[11:0] bits
  128. * - DutyCycle: I2C_CCR_DUTY[7:0] bits
  129. */
  130. LL_I2C_ConfigSpeed(I2Cx, rcc_clocks.PCLK1_Frequency, I2C_InitStruct->ClockSpeed, I2C_InitStruct->DutyCycle);
  131. /*---------------------------- I2Cx OAR1 Configuration -----------------------
  132. * Disable, Configure and Enable I2Cx device own address 1 with parameters :
  133. * - OwnAddress1: I2C_OAR1_ADD[9:8], I2C_OAR1_ADD[7:1] and I2C_OAR1_ADD0 bits
  134. * - OwnAddrSize: I2C_OAR1_ADDMODE bit
  135. */
  136. LL_I2C_SetOwnAddress1(I2Cx, I2C_InitStruct->OwnAddress1, I2C_InitStruct->OwnAddrSize);
  137. /*---------------------------- I2Cx MODE Configuration -----------------------
  138. * Configure I2Cx peripheral mode with parameter :
  139. * - PeripheralMode: I2C_CR1_SMBUS, I2C_CR1_SMBTYPE and I2C_CR1_ENARP bits
  140. */
  141. LL_I2C_SetMode(I2Cx, I2C_InitStruct->PeripheralMode);
  142. /* Enable the selected I2Cx Peripheral */
  143. LL_I2C_Enable(I2Cx);
  144. /*---------------------------- I2Cx CR2 Configuration ------------------------
  145. * Configure the ACKnowledge or Non ACKnowledge condition
  146. * after the address receive match code or next received byte with parameter :
  147. * - TypeAcknowledge: I2C_CR2_NACK bit
  148. */
  149. LL_I2C_AcknowledgeNextData(I2Cx, I2C_InitStruct->TypeAcknowledge);
  150. return SUCCESS;
  151. }
  152. /**
  153. * @brief Set each @ref LL_I2C_InitTypeDef field to default value.
  154. * @param I2C_InitStruct Pointer to a @ref LL_I2C_InitTypeDef structure.
  155. * @retval None
  156. */
  157. void LL_I2C_StructInit(LL_I2C_InitTypeDef *I2C_InitStruct)
  158. {
  159. /* Set I2C_InitStruct fields to default values */
  160. I2C_InitStruct->PeripheralMode = LL_I2C_MODE_I2C;
  161. I2C_InitStruct->ClockSpeed = 5000U;
  162. I2C_InitStruct->DutyCycle = LL_I2C_DUTYCYCLE_2;
  163. I2C_InitStruct->OwnAddress1 = 0U;
  164. I2C_InitStruct->TypeAcknowledge = LL_I2C_NACK;
  165. I2C_InitStruct->OwnAddrSize = LL_I2C_OWNADDRESS1_7BIT;
  166. }
  167. /**
  168. * @}
  169. */
  170. /**
  171. * @}
  172. */
  173. /**
  174. * @}
  175. */
  176. #endif /* I2C1 || I2C2 */
  177. /**
  178. * @}
  179. */
  180. #endif /* USE_FULL_LL_DRIVER */
  181. /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/