stm32f3xx_ll_i2c.c 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255
  1. /**
  2. ******************************************************************************
  3. * @file stm32f3xx_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 "stm32f3xx_ll_i2c.h"
  38. #include "stm32f3xx_ll_bus.h"
  39. #ifdef USE_FULL_ASSERT
  40. #include "stm32_assert.h"
  41. #else
  42. #define assert_param(expr) ((void)0U)
  43. #endif
  44. /** @addtogroup STM32F3xx_LL_Driver
  45. * @{
  46. */
  47. #if defined (I2C1) || defined (I2C2) || defined (I2C3)
  48. /** @defgroup I2C_LL I2C
  49. * @{
  50. */
  51. /* Private types -------------------------------------------------------------*/
  52. /* Private variables ---------------------------------------------------------*/
  53. /* Private constants ---------------------------------------------------------*/
  54. /* Private macros ------------------------------------------------------------*/
  55. /** @addtogroup I2C_LL_Private_Macros
  56. * @{
  57. */
  58. #define IS_LL_I2C_PERIPHERAL_MODE(__VALUE__) (((__VALUE__) == LL_I2C_MODE_I2C) || \
  59. ((__VALUE__) == LL_I2C_MODE_SMBUS_HOST) || \
  60. ((__VALUE__) == LL_I2C_MODE_SMBUS_DEVICE) || \
  61. ((__VALUE__) == LL_I2C_MODE_SMBUS_DEVICE_ARP))
  62. #define IS_LL_I2C_ANALOG_FILTER(__VALUE__) (((__VALUE__) == LL_I2C_ANALOGFILTER_ENABLE) || \
  63. ((__VALUE__) == LL_I2C_ANALOGFILTER_DISABLE))
  64. #define IS_LL_I2C_DIGITAL_FILTER(__VALUE__) ((__VALUE__) <= 0x0000000FU)
  65. #define IS_LL_I2C_OWN_ADDRESS1(__VALUE__) ((__VALUE__) <= 0x000003FFU)
  66. #define IS_LL_I2C_TYPE_ACKNOWLEDGE(__VALUE__) (((__VALUE__) == LL_I2C_ACK) || \
  67. ((__VALUE__) == LL_I2C_NACK))
  68. #define IS_LL_I2C_OWN_ADDRSIZE(__VALUE__) (((__VALUE__) == LL_I2C_OWNADDRESS1_7BIT) || \
  69. ((__VALUE__) == LL_I2C_OWNADDRESS1_10BIT))
  70. /**
  71. * @}
  72. */
  73. /* Private function prototypes -----------------------------------------------*/
  74. /* Exported functions --------------------------------------------------------*/
  75. /** @addtogroup I2C_LL_Exported_Functions
  76. * @{
  77. */
  78. /** @addtogroup I2C_LL_EF_Init
  79. * @{
  80. */
  81. /**
  82. * @brief De-initialize the I2C registers to their default reset values.
  83. * @param I2Cx I2C Instance.
  84. * @retval An ErrorStatus enumeration value:
  85. * - SUCCESS: I2C registers are de-initialized
  86. * - ERROR: I2C registers are not de-initialized
  87. */
  88. uint32_t LL_I2C_DeInit(I2C_TypeDef *I2Cx)
  89. {
  90. ErrorStatus status = SUCCESS;
  91. /* Check the I2C Instance I2Cx */
  92. assert_param(IS_I2C_ALL_INSTANCE(I2Cx));
  93. if (I2Cx == I2C1)
  94. {
  95. /* Force reset of I2C clock */
  96. LL_APB1_GRP1_ForceReset(LL_APB1_GRP1_PERIPH_I2C1);
  97. /* Release reset of I2C clock */
  98. LL_APB1_GRP1_ReleaseReset(LL_APB1_GRP1_PERIPH_I2C1);
  99. }
  100. #if defined(I2C2)
  101. else if (I2Cx == I2C2)
  102. {
  103. /* Force reset of I2C clock */
  104. LL_APB1_GRP1_ForceReset(LL_APB1_GRP1_PERIPH_I2C2);
  105. /* Release reset of I2C clock */
  106. LL_APB1_GRP1_ReleaseReset(LL_APB1_GRP1_PERIPH_I2C2);
  107. }
  108. #endif
  109. #if defined(I2C3)
  110. else if (I2Cx == I2C3)
  111. {
  112. /* Force reset of I2C clock */
  113. LL_APB1_GRP1_ForceReset(LL_APB1_GRP1_PERIPH_I2C3);
  114. /* Release reset of I2C clock */
  115. LL_APB1_GRP1_ReleaseReset(LL_APB1_GRP1_PERIPH_I2C3);
  116. }
  117. #endif
  118. else
  119. {
  120. status = ERROR;
  121. }
  122. return status;
  123. }
  124. /**
  125. * @brief Initialize the I2C registers according to the specified parameters in I2C_InitStruct.
  126. * @param I2Cx I2C Instance.
  127. * @param I2C_InitStruct pointer to a @ref LL_I2C_InitTypeDef structure.
  128. * @retval An ErrorStatus enumeration value:
  129. * - SUCCESS: I2C registers are initialized
  130. * - ERROR: Not applicable
  131. */
  132. uint32_t LL_I2C_Init(I2C_TypeDef *I2Cx, LL_I2C_InitTypeDef *I2C_InitStruct)
  133. {
  134. /* Check the I2C Instance I2Cx */
  135. assert_param(IS_I2C_ALL_INSTANCE(I2Cx));
  136. /* Check the I2C parameters from I2C_InitStruct */
  137. assert_param(IS_LL_I2C_PERIPHERAL_MODE(I2C_InitStruct->PeripheralMode));
  138. assert_param(IS_LL_I2C_ANALOG_FILTER(I2C_InitStruct->AnalogFilter));
  139. assert_param(IS_LL_I2C_DIGITAL_FILTER(I2C_InitStruct->DigitalFilter));
  140. assert_param(IS_LL_I2C_OWN_ADDRESS1(I2C_InitStruct->OwnAddress1));
  141. assert_param(IS_LL_I2C_TYPE_ACKNOWLEDGE(I2C_InitStruct->TypeAcknowledge));
  142. assert_param(IS_LL_I2C_OWN_ADDRSIZE(I2C_InitStruct->OwnAddrSize));
  143. /* Disable the selected I2Cx Peripheral */
  144. LL_I2C_Disable(I2Cx);
  145. /*---------------------------- I2Cx CR1 Configuration ------------------------
  146. * Configure the analog and digital noise filters with parameters :
  147. * - AnalogFilter: I2C_CR1_ANFOFF bit
  148. * - DigitalFilter: I2C_CR1_DNF[3:0] bits
  149. */
  150. LL_I2C_ConfigFilters(I2Cx, I2C_InitStruct->AnalogFilter, I2C_InitStruct->DigitalFilter);
  151. /*---------------------------- I2Cx TIMINGR Configuration --------------------
  152. * Configure the SDA setup, hold time and the SCL high, low period with parameter :
  153. * - Timing: I2C_TIMINGR_PRESC[3:0], I2C_TIMINGR_SCLDEL[3:0], I2C_TIMINGR_SDADEL[3:0],
  154. * I2C_TIMINGR_SCLH[7:0] and I2C_TIMINGR_SCLL[7:0] bits
  155. */
  156. LL_I2C_SetTiming(I2Cx, I2C_InitStruct->Timing);
  157. /* Enable the selected I2Cx Peripheral */
  158. LL_I2C_Enable(I2Cx);
  159. /*---------------------------- I2Cx OAR1 Configuration -----------------------
  160. * Disable, Configure and Enable I2Cx device own address 1 with parameters :
  161. * - OwnAddress1: I2C_OAR1_OA1[9:0] bits
  162. * - OwnAddrSize: I2C_OAR1_OA1MODE bit
  163. */
  164. LL_I2C_DisableOwnAddress1(I2Cx);
  165. LL_I2C_SetOwnAddress1(I2Cx, I2C_InitStruct->OwnAddress1, I2C_InitStruct->OwnAddrSize);
  166. /* OwnAdress1 == 0 is reserved for General Call address */
  167. if (I2C_InitStruct->OwnAddress1 != 0U)
  168. {
  169. LL_I2C_EnableOwnAddress1(I2Cx);
  170. }
  171. /*---------------------------- I2Cx MODE Configuration -----------------------
  172. * Configure I2Cx peripheral mode with parameter :
  173. * - PeripheralMode: I2C_CR1_SMBDEN and I2C_CR1_SMBHEN bits
  174. */
  175. LL_I2C_SetMode(I2Cx, I2C_InitStruct->PeripheralMode);
  176. /*---------------------------- I2Cx CR2 Configuration ------------------------
  177. * Configure the ACKnowledge or Non ACKnowledge condition
  178. * after the address receive match code or next received byte with parameter :
  179. * - TypeAcknowledge: I2C_CR2_NACK bit
  180. */
  181. LL_I2C_AcknowledgeNextData(I2Cx, I2C_InitStruct->TypeAcknowledge);
  182. return SUCCESS;
  183. }
  184. /**
  185. * @brief Set each @ref LL_I2C_InitTypeDef field to default value.
  186. * @param I2C_InitStruct Pointer to a @ref LL_I2C_InitTypeDef structure.
  187. * @retval None
  188. */
  189. void LL_I2C_StructInit(LL_I2C_InitTypeDef *I2C_InitStruct)
  190. {
  191. /* Set I2C_InitStruct fields to default values */
  192. I2C_InitStruct->PeripheralMode = LL_I2C_MODE_I2C;
  193. I2C_InitStruct->Timing = 0U;
  194. I2C_InitStruct->AnalogFilter = LL_I2C_ANALOGFILTER_ENABLE;
  195. I2C_InitStruct->DigitalFilter = 0U;
  196. I2C_InitStruct->OwnAddress1 = 0U;
  197. I2C_InitStruct->TypeAcknowledge = LL_I2C_NACK;
  198. I2C_InitStruct->OwnAddrSize = LL_I2C_OWNADDRESS1_7BIT;
  199. }
  200. /**
  201. * @}
  202. */
  203. /**
  204. * @}
  205. */
  206. /**
  207. * @}
  208. */
  209. #endif /* I2C1 || I2C2 || I2C3 */
  210. /**
  211. * @}
  212. */
  213. #endif /* USE_FULL_LL_DRIVER */
  214. /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/