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.

stm32l1xx_ll_i2c.c 7.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219
  1. /**
  2. ******************************************************************************
  3. * @file stm32l1xx_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 "stm32l1xx_ll_i2c.h"
  22. #include "stm32l1xx_ll_bus.h"
  23. #include "stm32l1xx_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 STM32L1xx_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. 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. else
  93. {
  94. status = ERROR;
  95. }
  96. return status;
  97. }
  98. /**
  99. * @brief Initialize the I2C registers according to the specified parameters in I2C_InitStruct.
  100. * @param I2Cx I2C Instance.
  101. * @param I2C_InitStruct pointer to a @ref LL_I2C_InitTypeDef structure.
  102. * @retval An ErrorStatus enumeration value:
  103. * - SUCCESS I2C registers are initialized
  104. * - ERROR Not applicable
  105. */
  106. uint32_t LL_I2C_Init(I2C_TypeDef *I2Cx, LL_I2C_InitTypeDef *I2C_InitStruct)
  107. {
  108. LL_RCC_ClocksTypeDef rcc_clocks;
  109. /* Check the I2C Instance I2Cx */
  110. assert_param(IS_I2C_ALL_INSTANCE(I2Cx));
  111. /* Check the I2C parameters from I2C_InitStruct */
  112. assert_param(IS_LL_I2C_PERIPHERAL_MODE(I2C_InitStruct->PeripheralMode));
  113. assert_param(IS_LL_I2C_CLOCK_SPEED(I2C_InitStruct->ClockSpeed));
  114. assert_param(IS_LL_I2C_DUTY_CYCLE(I2C_InitStruct->DutyCycle));
  115. assert_param(IS_LL_I2C_OWN_ADDRESS1(I2C_InitStruct->OwnAddress1));
  116. assert_param(IS_LL_I2C_TYPE_ACKNOWLEDGE(I2C_InitStruct->TypeAcknowledge));
  117. assert_param(IS_LL_I2C_OWN_ADDRSIZE(I2C_InitStruct->OwnAddrSize));
  118. /* Disable the selected I2Cx Peripheral */
  119. LL_I2C_Disable(I2Cx);
  120. /* Retrieve Clock frequencies */
  121. LL_RCC_GetSystemClocksFreq(&rcc_clocks);
  122. /*---------------------------- I2Cx SCL Clock Speed Configuration ------------
  123. * Configure the SCL speed :
  124. * - ClockSpeed: I2C_CR2_FREQ[5:0], I2C_TRISE_TRISE[5:0], I2C_CCR_FS,
  125. * and I2C_CCR_CCR[11:0] bits
  126. * - DutyCycle: I2C_CCR_DUTY[7:0] bits
  127. */
  128. LL_I2C_ConfigSpeed(I2Cx, rcc_clocks.PCLK1_Frequency, I2C_InitStruct->ClockSpeed, I2C_InitStruct->DutyCycle);
  129. /*---------------------------- I2Cx OAR1 Configuration -----------------------
  130. * Disable, Configure and Enable I2Cx device own address 1 with parameters :
  131. * - OwnAddress1: I2C_OAR1_ADD[9:8], I2C_OAR1_ADD[7:1] and I2C_OAR1_ADD0 bits
  132. * - OwnAddrSize: I2C_OAR1_ADDMODE bit
  133. */
  134. LL_I2C_SetOwnAddress1(I2Cx, I2C_InitStruct->OwnAddress1, I2C_InitStruct->OwnAddrSize);
  135. /*---------------------------- I2Cx MODE Configuration -----------------------
  136. * Configure I2Cx peripheral mode with parameter :
  137. * - PeripheralMode: I2C_CR1_SMBUS, I2C_CR1_SMBTYPE and I2C_CR1_ENARP bits
  138. */
  139. LL_I2C_SetMode(I2Cx, I2C_InitStruct->PeripheralMode);
  140. /* Enable the selected I2Cx Peripheral */
  141. LL_I2C_Enable(I2Cx);
  142. /*---------------------------- I2Cx CR2 Configuration ------------------------
  143. * Configure the ACKnowledge or Non ACKnowledge condition
  144. * after the address receive match code or next received byte with parameter :
  145. * - TypeAcknowledge: I2C_CR2_NACK bit
  146. */
  147. LL_I2C_AcknowledgeNextData(I2Cx, I2C_InitStruct->TypeAcknowledge);
  148. return SUCCESS;
  149. }
  150. /**
  151. * @brief Set each @ref LL_I2C_InitTypeDef field to default value.
  152. * @param I2C_InitStruct Pointer to a @ref LL_I2C_InitTypeDef structure.
  153. * @retval None
  154. */
  155. void LL_I2C_StructInit(LL_I2C_InitTypeDef *I2C_InitStruct)
  156. {
  157. /* Set I2C_InitStruct fields to default values */
  158. I2C_InitStruct->PeripheralMode = LL_I2C_MODE_I2C;
  159. I2C_InitStruct->ClockSpeed = 5000U;
  160. I2C_InitStruct->DutyCycle = LL_I2C_DUTYCYCLE_2;
  161. I2C_InitStruct->OwnAddress1 = 0U;
  162. I2C_InitStruct->TypeAcknowledge = LL_I2C_NACK;
  163. I2C_InitStruct->OwnAddrSize = LL_I2C_OWNADDRESS1_7BIT;
  164. }
  165. /**
  166. * @}
  167. */
  168. /**
  169. * @}
  170. */
  171. /**
  172. * @}
  173. */
  174. #endif /* I2C1 || I2C2 */
  175. /**
  176. * @}
  177. */
  178. #endif /* USE_FULL_LL_DRIVER */
  179. /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/