stm32f4xx_ll_i2c.c 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253
  1. /**
  2. ******************************************************************************
  3. * @file stm32f4xx_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 "stm32f4xx_ll_i2c.h"
  22. #include "stm32f4xx_ll_bus.h"
  23. #include "stm32f4xx_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 STM32F4xx_LL_Driver
  30. * @{
  31. */
  32. #if defined (I2C1) || defined (I2C2) || defined (I2C3)
  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. #if defined(I2C_FLTR_ANOFF)&&defined(I2C_FLTR_DNF)
  51. #define IS_LL_I2C_ANALOG_FILTER(__VALUE__) (((__VALUE__) == LL_I2C_ANALOGFILTER_ENABLE) || \
  52. ((__VALUE__) == LL_I2C_ANALOGFILTER_DISABLE))
  53. #define IS_LL_I2C_DIGITAL_FILTER(__VALUE__) ((__VALUE__) <= 0x0000000FU)
  54. #endif
  55. #define IS_LL_I2C_OWN_ADDRESS1(__VALUE__) ((__VALUE__) <= 0x000003FFU)
  56. #define IS_LL_I2C_TYPE_ACKNOWLEDGE(__VALUE__) (((__VALUE__) == LL_I2C_ACK) || \
  57. ((__VALUE__) == LL_I2C_NACK))
  58. #define IS_LL_I2C_OWN_ADDRSIZE(__VALUE__) (((__VALUE__) == LL_I2C_OWNADDRESS1_7BIT) || \
  59. ((__VALUE__) == LL_I2C_OWNADDRESS1_10BIT))
  60. /**
  61. * @}
  62. */
  63. /* Private function prototypes -----------------------------------------------*/
  64. /* Exported functions --------------------------------------------------------*/
  65. /** @addtogroup I2C_LL_Exported_Functions
  66. * @{
  67. */
  68. /** @addtogroup I2C_LL_EF_Init
  69. * @{
  70. */
  71. /**
  72. * @brief De-initialize the I2C registers to their default reset values.
  73. * @param I2Cx I2C Instance.
  74. * @retval An ErrorStatus enumeration value:
  75. * - SUCCESS I2C registers are de-initialized
  76. * - ERROR I2C registers are not de-initialized
  77. */
  78. uint32_t LL_I2C_DeInit(I2C_TypeDef *I2Cx)
  79. {
  80. ErrorStatus status = SUCCESS;
  81. /* Check the I2C Instance I2Cx */
  82. assert_param(IS_I2C_ALL_INSTANCE(I2Cx));
  83. if (I2Cx == I2C1)
  84. {
  85. /* Force reset of I2C clock */
  86. LL_APB1_GRP1_ForceReset(LL_APB1_GRP1_PERIPH_I2C1);
  87. /* Release reset of I2C clock */
  88. LL_APB1_GRP1_ReleaseReset(LL_APB1_GRP1_PERIPH_I2C1);
  89. }
  90. else if (I2Cx == I2C2)
  91. {
  92. /* Force reset of I2C clock */
  93. LL_APB1_GRP1_ForceReset(LL_APB1_GRP1_PERIPH_I2C2);
  94. /* Release reset of I2C clock */
  95. LL_APB1_GRP1_ReleaseReset(LL_APB1_GRP1_PERIPH_I2C2);
  96. }
  97. #if defined(I2C3)
  98. else if (I2Cx == I2C3)
  99. {
  100. /* Force reset of I2C clock */
  101. LL_APB1_GRP1_ForceReset(LL_APB1_GRP1_PERIPH_I2C3);
  102. /* Release reset of I2C clock */
  103. LL_APB1_GRP1_ReleaseReset(LL_APB1_GRP1_PERIPH_I2C3);
  104. }
  105. #endif
  106. else
  107. {
  108. status = ERROR;
  109. }
  110. return status;
  111. }
  112. /**
  113. * @brief Initialize the I2C registers according to the specified parameters in I2C_InitStruct.
  114. * @param I2Cx I2C Instance.
  115. * @param I2C_InitStruct pointer to a @ref LL_I2C_InitTypeDef structure.
  116. * @retval An ErrorStatus enumeration value:
  117. * - SUCCESS I2C registers are initialized
  118. * - ERROR Not applicable
  119. */
  120. uint32_t LL_I2C_Init(I2C_TypeDef *I2Cx, LL_I2C_InitTypeDef *I2C_InitStruct)
  121. {
  122. LL_RCC_ClocksTypeDef rcc_clocks;
  123. /* Check the I2C Instance I2Cx */
  124. assert_param(IS_I2C_ALL_INSTANCE(I2Cx));
  125. /* Check the I2C parameters from I2C_InitStruct */
  126. assert_param(IS_LL_I2C_PERIPHERAL_MODE(I2C_InitStruct->PeripheralMode));
  127. assert_param(IS_LL_I2C_CLOCK_SPEED(I2C_InitStruct->ClockSpeed));
  128. assert_param(IS_LL_I2C_DUTY_CYCLE(I2C_InitStruct->DutyCycle));
  129. #if defined(I2C_FLTR_ANOFF)&&defined(I2C_FLTR_DNF)
  130. assert_param(IS_LL_I2C_ANALOG_FILTER(I2C_InitStruct->AnalogFilter));
  131. assert_param(IS_LL_I2C_DIGITAL_FILTER(I2C_InitStruct->DigitalFilter));
  132. #endif
  133. assert_param(IS_LL_I2C_OWN_ADDRESS1(I2C_InitStruct->OwnAddress1));
  134. assert_param(IS_LL_I2C_TYPE_ACKNOWLEDGE(I2C_InitStruct->TypeAcknowledge));
  135. assert_param(IS_LL_I2C_OWN_ADDRSIZE(I2C_InitStruct->OwnAddrSize));
  136. /* Disable the selected I2Cx Peripheral */
  137. LL_I2C_Disable(I2Cx);
  138. /* Retrieve Clock frequencies */
  139. LL_RCC_GetSystemClocksFreq(&rcc_clocks);
  140. #if defined(I2C_FLTR_ANOFF)&&defined(I2C_FLTR_DNF)
  141. /*---------------------------- I2Cx FLTR Configuration -----------------------
  142. * Configure the analog and digital noise filters with parameters :
  143. * - AnalogFilter: I2C_FLTR_ANFOFF bit
  144. * - DigitalFilter: I2C_FLTR_DNF[3:0] bits
  145. */
  146. LL_I2C_ConfigFilters(I2Cx, I2C_InitStruct->AnalogFilter, I2C_InitStruct->DigitalFilter);
  147. #endif
  148. /*---------------------------- I2Cx SCL Clock Speed Configuration ------------
  149. * Configure the SCL speed :
  150. * - ClockSpeed: I2C_CR2_FREQ[5:0], I2C_TRISE_TRISE[5:0], I2C_CCR_FS,
  151. * and I2C_CCR_CCR[11:0] bits
  152. * - DutyCycle: I2C_CCR_DUTY[7:0] bits
  153. */
  154. LL_I2C_ConfigSpeed(I2Cx, rcc_clocks.PCLK1_Frequency, I2C_InitStruct->ClockSpeed, I2C_InitStruct->DutyCycle);
  155. /*---------------------------- I2Cx OAR1 Configuration -----------------------
  156. * Disable, Configure and Enable I2Cx device own address 1 with parameters :
  157. * - OwnAddress1: I2C_OAR1_ADD[9:8], I2C_OAR1_ADD[7:1] and I2C_OAR1_ADD0 bits
  158. * - OwnAddrSize: I2C_OAR1_ADDMODE bit
  159. */
  160. LL_I2C_SetOwnAddress1(I2Cx, I2C_InitStruct->OwnAddress1, I2C_InitStruct->OwnAddrSize);
  161. /*---------------------------- I2Cx MODE Configuration -----------------------
  162. * Configure I2Cx peripheral mode with parameter :
  163. * - PeripheralMode: I2C_CR1_SMBUS, I2C_CR1_SMBTYPE and I2C_CR1_ENARP bits
  164. */
  165. LL_I2C_SetMode(I2Cx, I2C_InitStruct->PeripheralMode);
  166. /* Enable the selected I2Cx Peripheral */
  167. LL_I2C_Enable(I2Cx);
  168. /*---------------------------- I2Cx CR2 Configuration ------------------------
  169. * Configure the ACKnowledge or Non ACKnowledge condition
  170. * after the address receive match code or next received byte with parameter :
  171. * - TypeAcknowledge: I2C_CR2_NACK bit
  172. */
  173. LL_I2C_AcknowledgeNextData(I2Cx, I2C_InitStruct->TypeAcknowledge);
  174. return SUCCESS;
  175. }
  176. /**
  177. * @brief Set each @ref LL_I2C_InitTypeDef field to default value.
  178. * @param I2C_InitStruct Pointer to a @ref LL_I2C_InitTypeDef structure.
  179. * @retval None
  180. */
  181. void LL_I2C_StructInit(LL_I2C_InitTypeDef *I2C_InitStruct)
  182. {
  183. /* Set I2C_InitStruct fields to default values */
  184. I2C_InitStruct->PeripheralMode = LL_I2C_MODE_I2C;
  185. I2C_InitStruct->ClockSpeed = 5000U;
  186. I2C_InitStruct->DutyCycle = LL_I2C_DUTYCYCLE_2;
  187. #if defined(I2C_FLTR_ANOFF)&&defined(I2C_FLTR_DNF)
  188. I2C_InitStruct->AnalogFilter = LL_I2C_ANALOGFILTER_ENABLE;
  189. I2C_InitStruct->DigitalFilter = 0U;
  190. #endif
  191. I2C_InitStruct->OwnAddress1 = 0U;
  192. I2C_InitStruct->TypeAcknowledge = LL_I2C_NACK;
  193. I2C_InitStruct->OwnAddrSize = LL_I2C_OWNADDRESS1_7BIT;
  194. }
  195. /**
  196. * @}
  197. */
  198. /**
  199. * @}
  200. */
  201. /**
  202. * @}
  203. */
  204. #endif /* I2C1 || I2C2 || I2C3 */
  205. /**
  206. * @}
  207. */
  208. #endif /* USE_FULL_LL_DRIVER */
  209. /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/