py32f002b_ll_i2c.c 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202
  1. /**
  2. ******************************************************************************
  3. * @file py32f002b_ll_i2c.c
  4. * @author MCU Application Team
  5. * @brief I2C LL module driver.
  6. ******************************************************************************
  7. * @attention
  8. *
  9. * <h2><center>&copy; Copyright (c) 2023 Puya Semiconductor Co.
  10. * All rights reserved.</center></h2>
  11. *
  12. * This software component is licensed by Puya 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. * @attention
  19. *
  20. * <h2><center>&copy; Copyright (c) 2016 STMicroelectronics.
  21. * All rights reserved.</center></h2>
  22. *
  23. * This software component is licensed by ST under BSD 3-Clause license,
  24. * the "License"; You may not use this file except in compliance with the
  25. * License. You may obtain a copy of the License at:
  26. * opensource.org/licenses/BSD-3-Clause
  27. *
  28. ******************************************************************************
  29. */
  30. #if defined(USE_FULL_LL_DRIVER)
  31. /* Includes ------------------------------------------------------------------*/
  32. #include "py32f002b_ll_i2c.h"
  33. #include "py32f002b_ll_bus.h"
  34. #include "py32f002b_ll_rcc.h"
  35. #ifdef USE_FULL_ASSERT
  36. #include "py32_assert.h"
  37. #else
  38. #define assert_param(expr) ((void)0U)
  39. #endif
  40. /** @addtogroup PY32F002B_LL_Driver
  41. * @{
  42. */
  43. #if defined (I2C1)
  44. /** @defgroup I2C_LL I2C
  45. * @{
  46. */
  47. /* Private types -------------------------------------------------------------*/
  48. /* Private variables ---------------------------------------------------------*/
  49. /* Private constants ---------------------------------------------------------*/
  50. /* Private macros ------------------------------------------------------------*/
  51. /** @addtogroup I2C_LL_Private_Macros
  52. * @{
  53. */
  54. #define IS_LL_I2C_CLOCK_SPEED(__VALUE__) (((__VALUE__) > 0U) && ((__VALUE__) <= LL_I2C_MAX_SPEED_FAST))
  55. #define IS_LL_I2C_DUTY_CYCLE(__VALUE__) (((__VALUE__) == LL_I2C_DUTYCYCLE_2) || \
  56. ((__VALUE__) == LL_I2C_DUTYCYCLE_16_9))
  57. #define IS_LL_I2C_OWN_ADDRESS1(__VALUE__) ((__VALUE__) <= 0x000003FFU)
  58. #define IS_LL_I2C_TYPE_ACKNOWLEDGE(__VALUE__) (((__VALUE__) == LL_I2C_ACK) || \
  59. ((__VALUE__) == LL_I2C_NACK))
  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
  91. {
  92. status = ERROR;
  93. }
  94. return status;
  95. }
  96. /**
  97. * @brief Initialize the I2C registers according to the specified parameters in I2C_InitStruct.
  98. * @param I2Cx I2C Instance.
  99. * @param I2C_InitStruct pointer to a @ref LL_I2C_InitTypeDef structure.
  100. * @retval An ErrorStatus enumeration value:
  101. * - SUCCESS I2C registers are initialized
  102. * - ERROR Not applicable
  103. */
  104. uint32_t LL_I2C_Init(I2C_TypeDef *I2Cx, LL_I2C_InitTypeDef *I2C_InitStruct)
  105. {
  106. LL_RCC_ClocksTypeDef rcc_clocks;
  107. /* Check the I2C Instance I2Cx */
  108. assert_param(IS_I2C_ALL_INSTANCE(I2Cx));
  109. /* Check the I2C parameters from I2C_InitStruct */
  110. assert_param(IS_LL_I2C_CLOCK_SPEED(I2C_InitStruct->ClockSpeed));
  111. assert_param(IS_LL_I2C_DUTY_CYCLE(I2C_InitStruct->DutyCycle));
  112. assert_param(IS_LL_I2C_OWN_ADDRESS1(I2C_InitStruct->OwnAddress1));
  113. assert_param(IS_LL_I2C_TYPE_ACKNOWLEDGE(I2C_InitStruct->TypeAcknowledge));
  114. /* Disable the selected I2Cx Peripheral */
  115. LL_I2C_Disable(I2Cx);
  116. /* Retrieve Clock frequencies */
  117. LL_RCC_GetSystemClocksFreq(&rcc_clocks);
  118. /*---------------------------- I2Cx SCL Clock Speed Configuration ------------
  119. * Configure the SCL speed :
  120. * - ClockSpeed: I2C_CR2_FREQ[5:0], I2C_TRISE_TRISE[5:0], I2C_CCR_FS,
  121. * and I2C_CCR_CCR[11:0] bits
  122. * - DutyCycle: I2C_CCR_DUTY[7:0] bits
  123. */
  124. LL_I2C_ConfigSpeed(I2Cx, rcc_clocks.PCLK1_Frequency, I2C_InitStruct->ClockSpeed, I2C_InitStruct->DutyCycle);
  125. /*---------------------------- I2Cx OAR1 Configuration -----------------------
  126. * Disable, Configure and Enable I2Cx device own address 1 with parameters :
  127. * - OwnAddress1: I2C_OAR1_ADD[9:8], I2C_OAR1_ADD[7:1] and I2C_OAR1_ADD0 bits
  128. * - OwnAddrSize: I2C_OAR1_ADDMODE bit
  129. */
  130. LL_I2C_SetOwnAddress1(I2Cx, I2C_InitStruct->OwnAddress1, 0);
  131. /* Enable the selected I2Cx Peripheral */
  132. LL_I2C_Enable(I2Cx);
  133. /*---------------------------- I2Cx CR2 Configuration ------------------------
  134. * Configure the ACKnowledge or Non ACKnowledge condition
  135. * after the address receive match code or next received byte with parameter :
  136. * - TypeAcknowledge: I2C_CR2_NACK bit
  137. */
  138. LL_I2C_AcknowledgeNextData(I2Cx, I2C_InitStruct->TypeAcknowledge);
  139. return SUCCESS;
  140. }
  141. /**
  142. * @brief Set each @ref LL_I2C_InitTypeDef field to default value.
  143. * @param I2C_InitStruct Pointer to a @ref LL_I2C_InitTypeDef structure.
  144. * @retval None
  145. */
  146. void LL_I2C_StructInit(LL_I2C_InitTypeDef *I2C_InitStruct)
  147. {
  148. /* Set I2C_InitStruct fields to default values */
  149. I2C_InitStruct->ClockSpeed = 5000U;
  150. I2C_InitStruct->DutyCycle = LL_I2C_DUTYCYCLE_2;
  151. I2C_InitStruct->OwnAddress1 = 0U;
  152. I2C_InitStruct->TypeAcknowledge = LL_I2C_NACK;
  153. }
  154. /**
  155. * @}
  156. */
  157. /**
  158. * @}
  159. */
  160. /**
  161. * @}
  162. */
  163. #endif /* I2C1 */
  164. /**
  165. * @}
  166. */
  167. #endif /* USE_FULL_LL_DRIVER */
  168. /************************ (C) COPYRIGHT Puya *****END OF FILE****/