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.
 
 
 

259 lines
9.1 KiB

  1. /**
  2. ******************************************************************************
  3. * @file stm32f7xx_ll_i2c.c
  4. * @author MCD Application Team
  5. * @version V1.2.2
  6. * @date 14-April-2017
  7. * @brief I2C LL module driver.
  8. ******************************************************************************
  9. * @attention
  10. *
  11. * <h2><center>&copy; COPYRIGHT(c) 2017 STMicroelectronics</center></h2>
  12. *
  13. * Redistribution and use in source and binary forms, with or without modification,
  14. * are permitted provided that the following conditions are met:
  15. * 1. Redistributions of source code must retain the above copyright notice,
  16. * this list of conditions and the following disclaimer.
  17. * 2. Redistributions in binary form must reproduce the above copyright notice,
  18. * this list of conditions and the following disclaimer in the documentation
  19. * and/or other materials provided with the distribution.
  20. * 3. Neither the name of STMicroelectronics nor the names of its contributors
  21. * may be used to endorse or promote products derived from this software
  22. * without specific prior written permission.
  23. *
  24. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  25. * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  26. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  27. * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
  28. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  29. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
  30. * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  31. * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
  32. * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  33. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  34. *
  35. ******************************************************************************
  36. */
  37. #if defined(USE_FULL_LL_DRIVER)
  38. /* Includes ------------------------------------------------------------------*/
  39. #include "stm32f7xx_ll_i2c.h"
  40. #include "stm32f7xx_ll_bus.h"
  41. #ifdef USE_FULL_ASSERT
  42. #include "stm32_assert.h"
  43. #else
  44. #define assert_param(expr) ((void)0U)
  45. #endif
  46. /** @addtogroup STM32F7xx_LL_Driver
  47. * @{
  48. */
  49. #if defined (I2C1) || defined (I2C2) || defined (I2C3) || defined (I2C4)
  50. /** @defgroup I2C_LL I2C
  51. * @{
  52. */
  53. /* Private types -------------------------------------------------------------*/
  54. /* Private variables ---------------------------------------------------------*/
  55. /* Private constants ---------------------------------------------------------*/
  56. /* Private macros ------------------------------------------------------------*/
  57. /** @addtogroup I2C_LL_Private_Macros
  58. * @{
  59. */
  60. #define IS_LL_I2C_PERIPHERAL_MODE(__VALUE__) (((__VALUE__) == LL_I2C_MODE_I2C) || \
  61. ((__VALUE__) == LL_I2C_MODE_SMBUS_HOST) || \
  62. ((__VALUE__) == LL_I2C_MODE_SMBUS_DEVICE) || \
  63. ((__VALUE__) == LL_I2C_MODE_SMBUS_DEVICE_ARP))
  64. #define IS_LL_I2C_ANALOG_FILTER(__VALUE__) (((__VALUE__) == LL_I2C_ANALOGFILTER_ENABLE) || \
  65. ((__VALUE__) == LL_I2C_ANALOGFILTER_DISABLE))
  66. #define IS_LL_I2C_DIGITAL_FILTER(__VALUE__) ((__VALUE__) <= 0x0000000FU)
  67. #define IS_LL_I2C_OWN_ADDRESS1(__VALUE__) ((__VALUE__) <= 0x000003FFU)
  68. #define IS_LL_I2C_TYPE_ACKNOWLEDGE(__VALUE__) (((__VALUE__) == LL_I2C_ACK) || \
  69. ((__VALUE__) == LL_I2C_NACK))
  70. #define IS_LL_I2C_OWN_ADDRSIZE(__VALUE__) (((__VALUE__) == LL_I2C_OWNADDRESS1_7BIT) || \
  71. ((__VALUE__) == LL_I2C_OWNADDRESS1_10BIT))
  72. /**
  73. * @}
  74. */
  75. /* Private function prototypes -----------------------------------------------*/
  76. /* Exported functions --------------------------------------------------------*/
  77. /** @addtogroup I2C_LL_Exported_Functions
  78. * @{
  79. */
  80. /** @addtogroup I2C_LL_EF_Init
  81. * @{
  82. */
  83. /**
  84. * @brief De-initialize the I2C registers to their default reset values.
  85. * @param I2Cx I2C Instance.
  86. * @retval An ErrorStatus enumeration value:
  87. * - SUCCESS: I2C registers are de-initialized
  88. * - ERROR: I2C registers are not de-initialized
  89. */
  90. uint32_t LL_I2C_DeInit(I2C_TypeDef *I2Cx)
  91. {
  92. ErrorStatus status = SUCCESS;
  93. /* Check the I2C Instance I2Cx */
  94. assert_param(IS_I2C_ALL_INSTANCE(I2Cx));
  95. if (I2Cx == I2C1)
  96. {
  97. /* Force reset of I2C clock */
  98. LL_APB1_GRP1_ForceReset(LL_APB1_GRP1_PERIPH_I2C1);
  99. /* Release reset of I2C clock */
  100. LL_APB1_GRP1_ReleaseReset(LL_APB1_GRP1_PERIPH_I2C1);
  101. }
  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. else if (I2Cx == I2C3)
  110. {
  111. /* Force reset of I2C clock */
  112. LL_APB1_GRP1_ForceReset(LL_APB1_GRP1_PERIPH_I2C3);
  113. /* Release reset of I2C clock */
  114. LL_APB1_GRP1_ReleaseReset(LL_APB1_GRP1_PERIPH_I2C3);
  115. }
  116. #if defined(I2C4)
  117. else if (I2Cx == I2C4)
  118. {
  119. /* Force reset of I2C clock */
  120. LL_APB1_GRP1_ForceReset(LL_APB1_GRP1_PERIPH_I2C4);
  121. /* Release reset of I2C clock */
  122. LL_APB1_GRP1_ReleaseReset(LL_APB1_GRP1_PERIPH_I2C4);
  123. }
  124. #endif
  125. else
  126. {
  127. status = ERROR;
  128. }
  129. return status;
  130. }
  131. /**
  132. * @brief Initialize the I2C registers according to the specified parameters in I2C_InitStruct.
  133. * @param I2Cx I2C Instance.
  134. * @param I2C_InitStruct pointer to a @ref LL_I2C_InitTypeDef structure.
  135. * @retval An ErrorStatus enumeration value:
  136. * - SUCCESS: I2C registers are initialized
  137. * - ERROR: Not applicable
  138. */
  139. uint32_t LL_I2C_Init(I2C_TypeDef *I2Cx, LL_I2C_InitTypeDef *I2C_InitStruct)
  140. {
  141. /* Check the I2C Instance I2Cx */
  142. assert_param(IS_I2C_ALL_INSTANCE(I2Cx));
  143. /* Check the I2C parameters from I2C_InitStruct */
  144. assert_param(IS_LL_I2C_PERIPHERAL_MODE(I2C_InitStruct->PeripheralMode));
  145. assert_param(IS_LL_I2C_ANALOG_FILTER(I2C_InitStruct->AnalogFilter));
  146. assert_param(IS_LL_I2C_DIGITAL_FILTER(I2C_InitStruct->DigitalFilter));
  147. assert_param(IS_LL_I2C_OWN_ADDRESS1(I2C_InitStruct->OwnAddress1));
  148. assert_param(IS_LL_I2C_TYPE_ACKNOWLEDGE(I2C_InitStruct->TypeAcknowledge));
  149. assert_param(IS_LL_I2C_OWN_ADDRSIZE(I2C_InitStruct->OwnAddrSize));
  150. /* Disable the selected I2Cx Peripheral */
  151. LL_I2C_Disable(I2Cx);
  152. /*---------------------------- I2Cx CR1 Configuration ------------------------
  153. * Configure the analog and digital noise filters with parameters :
  154. * - AnalogFilter: I2C_CR1_ANFOFF bit
  155. * - DigitalFilter: I2C_CR1_DNF[3:0] bits
  156. */
  157. LL_I2C_ConfigFilters(I2Cx, I2C_InitStruct->AnalogFilter, I2C_InitStruct->DigitalFilter);
  158. /*---------------------------- I2Cx TIMINGR Configuration --------------------
  159. * Configure the SDA setup, hold time and the SCL high, low period with parameter :
  160. * - Timing: I2C_TIMINGR_PRESC[3:0], I2C_TIMINGR_SCLDEL[3:0], I2C_TIMINGR_SDADEL[3:0],
  161. * I2C_TIMINGR_SCLH[7:0] and I2C_TIMINGR_SCLL[7:0] bits
  162. */
  163. LL_I2C_SetTiming(I2Cx, I2C_InitStruct->Timing);
  164. /* Enable the selected I2Cx Peripheral */
  165. LL_I2C_Enable(I2Cx);
  166. /*---------------------------- I2Cx OAR1 Configuration -----------------------
  167. * Disable, Configure and Enable I2Cx device own address 1 with parameters :
  168. * - OwnAddress1: I2C_OAR1_OA1[9:0] bits
  169. * - OwnAddrSize: I2C_OAR1_OA1MODE bit
  170. */
  171. LL_I2C_DisableOwnAddress1(I2Cx);
  172. LL_I2C_SetOwnAddress1(I2Cx, I2C_InitStruct->OwnAddress1, I2C_InitStruct->OwnAddrSize);
  173. LL_I2C_EnableOwnAddress1(I2Cx);
  174. /*---------------------------- I2Cx MODE Configuration -----------------------
  175. * Configure I2Cx peripheral mode with parameter :
  176. * - PeripheralMode: I2C_CR1_SMBDEN and I2C_CR1_SMBHEN bits
  177. */
  178. LL_I2C_SetMode(I2Cx, I2C_InitStruct->PeripheralMode);
  179. /*---------------------------- I2Cx CR2 Configuration ------------------------
  180. * Configure the ACKnowledge or Non ACKnowledge condition
  181. * after the address receive match code or next received byte with parameter :
  182. * - TypeAcknowledge: I2C_CR2_NACK bit
  183. */
  184. LL_I2C_AcknowledgeNextData(I2Cx, I2C_InitStruct->TypeAcknowledge);
  185. return SUCCESS;
  186. }
  187. /**
  188. * @brief Set each @ref LL_I2C_InitTypeDef field to default value.
  189. * @param I2C_InitStruct Pointer to a @ref LL_I2C_InitTypeDef structure.
  190. * @retval None
  191. */
  192. void LL_I2C_StructInit(LL_I2C_InitTypeDef *I2C_InitStruct)
  193. {
  194. /* Set I2C_InitStruct fields to default values */
  195. I2C_InitStruct->PeripheralMode = LL_I2C_MODE_I2C;
  196. I2C_InitStruct->Timing = 0U;
  197. I2C_InitStruct->AnalogFilter = LL_I2C_ANALOGFILTER_ENABLE;
  198. I2C_InitStruct->DigitalFilter = 0U;
  199. I2C_InitStruct->OwnAddress1 = 0U;
  200. I2C_InitStruct->TypeAcknowledge = LL_I2C_NACK;
  201. I2C_InitStruct->OwnAddrSize = LL_I2C_OWNADDRESS1_7BIT;
  202. }
  203. /**
  204. * @}
  205. */
  206. /**
  207. * @}
  208. */
  209. /**
  210. * @}
  211. */
  212. #endif /* I2C1 || I2C2 || I2C3 || I2C4 */
  213. /**
  214. * @}
  215. */
  216. #endif /* USE_FULL_LL_DRIVER */
  217. /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/