stm32f1xx_ll_i2c.c 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237
  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</center></h2>
  10. *
  11. * Redistribution and use in source and binary forms, with or without modification,
  12. * are permitted provided that the following conditions are met:
  13. * 1. Redistributions of source code must retain the above copyright notice,
  14. * this list of conditions and the following disclaimer.
  15. * 2. Redistributions in binary form must reproduce the above copyright notice,
  16. * this list of conditions and the following disclaimer in the documentation
  17. * and/or other materials provided with the distribution.
  18. * 3. Neither the name of STMicroelectronics nor the names of its contributors
  19. * may be used to endorse or promote products derived from this software
  20. * without specific prior written permission.
  21. *
  22. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  23. * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  24. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  25. * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
  26. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  27. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
  28. * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  29. * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
  30. * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  31. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  32. *
  33. ******************************************************************************
  34. */
  35. #if defined(USE_FULL_LL_DRIVER)
  36. /* Includes ------------------------------------------------------------------*/
  37. #include "stm32f1xx_ll_i2c.h"
  38. #include "stm32f1xx_ll_bus.h"
  39. #include "stm32f1xx_ll_rcc.h"
  40. #ifdef USE_FULL_ASSERT
  41. #include "stm32_assert.h"
  42. #else
  43. #define assert_param(expr) ((void)0U)
  44. #endif
  45. /** @addtogroup STM32F1xx_LL_Driver
  46. * @{
  47. */
  48. #if defined (I2C1) || defined (I2C2)
  49. /** @defgroup I2C_LL I2C
  50. * @{
  51. */
  52. /* Private types -------------------------------------------------------------*/
  53. /* Private variables ---------------------------------------------------------*/
  54. /* Private constants ---------------------------------------------------------*/
  55. /* Private macros ------------------------------------------------------------*/
  56. /** @addtogroup I2C_LL_Private_Macros
  57. * @{
  58. */
  59. #define IS_LL_I2C_PERIPHERAL_MODE(__VALUE__) (((__VALUE__) == LL_I2C_MODE_I2C) || \
  60. ((__VALUE__) == LL_I2C_MODE_SMBUS_HOST) || \
  61. ((__VALUE__) == LL_I2C_MODE_SMBUS_DEVICE) || \
  62. ((__VALUE__) == LL_I2C_MODE_SMBUS_DEVICE_ARP))
  63. #define IS_LL_I2C_CLOCK_SPEED(__VALUE__) (((__VALUE__) > 0U) && ((__VALUE__) <= LL_I2C_MAX_SPEED_FAST))
  64. #define IS_LL_I2C_DUTY_CYCLE(__VALUE__) (((__VALUE__) == LL_I2C_DUTYCYCLE_2) || \
  65. ((__VALUE__) == LL_I2C_DUTYCYCLE_16_9))
  66. #define IS_LL_I2C_OWN_ADDRESS1(__VALUE__) ((__VALUE__) <= 0x000003FFU)
  67. #define IS_LL_I2C_TYPE_ACKNOWLEDGE(__VALUE__) (((__VALUE__) == LL_I2C_ACK) || \
  68. ((__VALUE__) == LL_I2C_NACK))
  69. #define IS_LL_I2C_OWN_ADDRSIZE(__VALUE__) (((__VALUE__) == LL_I2C_OWNADDRESS1_7BIT) || \
  70. ((__VALUE__) == LL_I2C_OWNADDRESS1_10BIT))
  71. /**
  72. * @}
  73. */
  74. /* Private function prototypes -----------------------------------------------*/
  75. /* Exported functions --------------------------------------------------------*/
  76. /** @addtogroup I2C_LL_Exported_Functions
  77. * @{
  78. */
  79. /** @addtogroup I2C_LL_EF_Init
  80. * @{
  81. */
  82. /**
  83. * @brief De-initialize the I2C registers to their default reset values.
  84. * @param I2Cx I2C Instance.
  85. * @retval An ErrorStatus enumeration value:
  86. * - SUCCESS I2C registers are de-initialized
  87. * - ERROR I2C registers are not de-initialized
  88. */
  89. uint32_t LL_I2C_DeInit(I2C_TypeDef *I2Cx)
  90. {
  91. ErrorStatus status = SUCCESS;
  92. /* Check the I2C Instance I2Cx */
  93. assert_param(IS_I2C_ALL_INSTANCE(I2Cx));
  94. if (I2Cx == I2C1)
  95. {
  96. /* Force reset of I2C clock */
  97. LL_APB1_GRP1_ForceReset(LL_APB1_GRP1_PERIPH_I2C1);
  98. /* Release reset of I2C clock */
  99. LL_APB1_GRP1_ReleaseReset(LL_APB1_GRP1_PERIPH_I2C1);
  100. }
  101. #if defined(I2C2)
  102. else if (I2Cx == I2C2)
  103. {
  104. /* Force reset of I2C clock */
  105. LL_APB1_GRP1_ForceReset(LL_APB1_GRP1_PERIPH_I2C2);
  106. /* Release reset of I2C clock */
  107. LL_APB1_GRP1_ReleaseReset(LL_APB1_GRP1_PERIPH_I2C2);
  108. }
  109. #endif /* I2C2 */
  110. else
  111. {
  112. status = ERROR;
  113. }
  114. return status;
  115. }
  116. /**
  117. * @brief Initialize the I2C registers according to the specified parameters in I2C_InitStruct.
  118. * @param I2Cx I2C Instance.
  119. * @param I2C_InitStruct pointer to a @ref LL_I2C_InitTypeDef structure.
  120. * @retval An ErrorStatus enumeration value:
  121. * - SUCCESS I2C registers are initialized
  122. * - ERROR Not applicable
  123. */
  124. uint32_t LL_I2C_Init(I2C_TypeDef *I2Cx, LL_I2C_InitTypeDef *I2C_InitStruct)
  125. {
  126. LL_RCC_ClocksTypeDef rcc_clocks;
  127. /* Check the I2C Instance I2Cx */
  128. assert_param(IS_I2C_ALL_INSTANCE(I2Cx));
  129. /* Check the I2C parameters from I2C_InitStruct */
  130. assert_param(IS_LL_I2C_PERIPHERAL_MODE(I2C_InitStruct->PeripheralMode));
  131. assert_param(IS_LL_I2C_CLOCK_SPEED(I2C_InitStruct->ClockSpeed));
  132. assert_param(IS_LL_I2C_DUTY_CYCLE(I2C_InitStruct->DutyCycle));
  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. /*---------------------------- I2Cx SCL Clock Speed Configuration ------------
  141. * Configure the SCL speed :
  142. * - ClockSpeed: I2C_CR2_FREQ[5:0], I2C_TRISE_TRISE[5:0], I2C_CCR_FS,
  143. * and I2C_CCR_CCR[11:0] bits
  144. * - DutyCycle: I2C_CCR_DUTY[7:0] bits
  145. */
  146. LL_I2C_ConfigSpeed(I2Cx, rcc_clocks.PCLK1_Frequency, I2C_InitStruct->ClockSpeed, I2C_InitStruct->DutyCycle);
  147. /*---------------------------- I2Cx OAR1 Configuration -----------------------
  148. * Disable, Configure and Enable I2Cx device own address 1 with parameters :
  149. * - OwnAddress1: I2C_OAR1_ADD[9:8], I2C_OAR1_ADD[7:1] and I2C_OAR1_ADD0 bits
  150. * - OwnAddrSize: I2C_OAR1_ADDMODE bit
  151. */
  152. LL_I2C_SetOwnAddress1(I2Cx, I2C_InitStruct->OwnAddress1, I2C_InitStruct->OwnAddrSize);
  153. /*---------------------------- I2Cx MODE Configuration -----------------------
  154. * Configure I2Cx peripheral mode with parameter :
  155. * - PeripheralMode: I2C_CR1_SMBUS, I2C_CR1_SMBTYPE and I2C_CR1_ENARP bits
  156. */
  157. LL_I2C_SetMode(I2Cx, I2C_InitStruct->PeripheralMode);
  158. /* Enable the selected I2Cx Peripheral */
  159. LL_I2C_Enable(I2Cx);
  160. /*---------------------------- I2Cx CR2 Configuration ------------------------
  161. * Configure the ACKnowledge or Non ACKnowledge condition
  162. * after the address receive match code or next received byte with parameter :
  163. * - TypeAcknowledge: I2C_CR2_NACK bit
  164. */
  165. LL_I2C_AcknowledgeNextData(I2Cx, I2C_InitStruct->TypeAcknowledge);
  166. return SUCCESS;
  167. }
  168. /**
  169. * @brief Set each @ref LL_I2C_InitTypeDef field to default value.
  170. * @param I2C_InitStruct Pointer to a @ref LL_I2C_InitTypeDef structure.
  171. * @retval None
  172. */
  173. void LL_I2C_StructInit(LL_I2C_InitTypeDef *I2C_InitStruct)
  174. {
  175. /* Set I2C_InitStruct fields to default values */
  176. I2C_InitStruct->PeripheralMode = LL_I2C_MODE_I2C;
  177. I2C_InitStruct->ClockSpeed = 5000U;
  178. I2C_InitStruct->DutyCycle = LL_I2C_DUTYCYCLE_2;
  179. I2C_InitStruct->OwnAddress1 = 0U;
  180. I2C_InitStruct->TypeAcknowledge = LL_I2C_NACK;
  181. I2C_InitStruct->OwnAddrSize = LL_I2C_OWNADDRESS1_7BIT;
  182. }
  183. /**
  184. * @}
  185. */
  186. /**
  187. * @}
  188. */
  189. /**
  190. * @}
  191. */
  192. #endif /* I2C1 || I2C2 */
  193. /**
  194. * @}
  195. */
  196. #endif /* USE_FULL_LL_DRIVER */
  197. /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/