训练营PLSR题目
Nevar pievienot vairāk kā 25 tēmas Tēmai ir jāsākas ar burtu vai ciparu, tā var saturēt domu zīmes ('-') un var būt līdz 35 simboliem gara.
 
 
 
 
 
 

252 rindas
8.3 KiB

  1. /**
  2. ******************************************************************************
  3. * @file stm32f4xx_ll_i2c.c
  4. * @author MCD Application Team
  5. * @brief I2C LL module driver.
  6. ******************************************************************************
  7. * @attention
  8. *
  9. * Copyright (c) 2016 STMicroelectronics.
  10. * All rights reserved.
  11. *
  12. * This software is licensed under terms that can be found in the LICENSE file
  13. * in the root directory of this software component.
  14. * If no LICENSE file comes with this software, it is provided AS-IS.
  15. *
  16. ******************************************************************************
  17. */
  18. #if defined(USE_FULL_LL_DRIVER)
  19. /* Includes ------------------------------------------------------------------*/
  20. #include "stm32f4xx_ll_i2c.h"
  21. #include "stm32f4xx_ll_bus.h"
  22. #include "stm32f4xx_ll_rcc.h"
  23. #ifdef USE_FULL_ASSERT
  24. #include "stm32_assert.h"
  25. #else
  26. #define assert_param(expr) ((void)0U)
  27. #endif
  28. /** @addtogroup STM32F4xx_LL_Driver
  29. * @{
  30. */
  31. #if defined (I2C1) || defined (I2C2) || defined (I2C3)
  32. /** @defgroup I2C_LL I2C
  33. * @{
  34. */
  35. /* Private types -------------------------------------------------------------*/
  36. /* Private variables ---------------------------------------------------------*/
  37. /* Private constants ---------------------------------------------------------*/
  38. /* Private macros ------------------------------------------------------------*/
  39. /** @addtogroup I2C_LL_Private_Macros
  40. * @{
  41. */
  42. #define IS_LL_I2C_PERIPHERAL_MODE(__VALUE__) (((__VALUE__) == LL_I2C_MODE_I2C) || \
  43. ((__VALUE__) == LL_I2C_MODE_SMBUS_HOST) || \
  44. ((__VALUE__) == LL_I2C_MODE_SMBUS_DEVICE) || \
  45. ((__VALUE__) == LL_I2C_MODE_SMBUS_DEVICE_ARP))
  46. #define IS_LL_I2C_CLOCK_SPEED(__VALUE__) (((__VALUE__) > 0U) && ((__VALUE__) <= LL_I2C_MAX_SPEED_FAST))
  47. #define IS_LL_I2C_DUTY_CYCLE(__VALUE__) (((__VALUE__) == LL_I2C_DUTYCYCLE_2) || \
  48. ((__VALUE__) == LL_I2C_DUTYCYCLE_16_9))
  49. #if defined(I2C_FLTR_ANOFF)&&defined(I2C_FLTR_DNF)
  50. #define IS_LL_I2C_ANALOG_FILTER(__VALUE__) (((__VALUE__) == LL_I2C_ANALOGFILTER_ENABLE) || \
  51. ((__VALUE__) == LL_I2C_ANALOGFILTER_DISABLE))
  52. #define IS_LL_I2C_DIGITAL_FILTER(__VALUE__) ((__VALUE__) <= 0x0000000FU)
  53. #endif
  54. #define IS_LL_I2C_OWN_ADDRESS1(__VALUE__) ((__VALUE__) <= 0x000003FFU)
  55. #define IS_LL_I2C_TYPE_ACKNOWLEDGE(__VALUE__) (((__VALUE__) == LL_I2C_ACK) || \
  56. ((__VALUE__) == LL_I2C_NACK))
  57. #define IS_LL_I2C_OWN_ADDRSIZE(__VALUE__) (((__VALUE__) == LL_I2C_OWNADDRESS1_7BIT) || \
  58. ((__VALUE__) == LL_I2C_OWNADDRESS1_10BIT))
  59. /**
  60. * @}
  61. */
  62. /* Private function prototypes -----------------------------------------------*/
  63. /* Exported functions --------------------------------------------------------*/
  64. /** @addtogroup I2C_LL_Exported_Functions
  65. * @{
  66. */
  67. /** @addtogroup I2C_LL_EF_Init
  68. * @{
  69. */
  70. /**
  71. * @brief De-initialize the I2C registers to their default reset values.
  72. * @param I2Cx I2C Instance.
  73. * @retval An ErrorStatus enumeration value:
  74. * - SUCCESS I2C registers are de-initialized
  75. * - ERROR I2C registers are not de-initialized
  76. */
  77. uint32_t LL_I2C_DeInit(I2C_TypeDef *I2Cx)
  78. {
  79. ErrorStatus status = SUCCESS;
  80. /* Check the I2C Instance I2Cx */
  81. assert_param(IS_I2C_ALL_INSTANCE(I2Cx));
  82. if (I2Cx == I2C1)
  83. {
  84. /* Force reset of I2C clock */
  85. LL_APB1_GRP1_ForceReset(LL_APB1_GRP1_PERIPH_I2C1);
  86. /* Release reset of I2C clock */
  87. LL_APB1_GRP1_ReleaseReset(LL_APB1_GRP1_PERIPH_I2C1);
  88. }
  89. else if (I2Cx == I2C2)
  90. {
  91. /* Force reset of I2C clock */
  92. LL_APB1_GRP1_ForceReset(LL_APB1_GRP1_PERIPH_I2C2);
  93. /* Release reset of I2C clock */
  94. LL_APB1_GRP1_ReleaseReset(LL_APB1_GRP1_PERIPH_I2C2);
  95. }
  96. #if defined(I2C3)
  97. else if (I2Cx == I2C3)
  98. {
  99. /* Force reset of I2C clock */
  100. LL_APB1_GRP1_ForceReset(LL_APB1_GRP1_PERIPH_I2C3);
  101. /* Release reset of I2C clock */
  102. LL_APB1_GRP1_ReleaseReset(LL_APB1_GRP1_PERIPH_I2C3);
  103. }
  104. #endif
  105. else
  106. {
  107. status = ERROR;
  108. }
  109. return status;
  110. }
  111. /**
  112. * @brief Initialize the I2C registers according to the specified parameters in I2C_InitStruct.
  113. * @param I2Cx I2C Instance.
  114. * @param I2C_InitStruct pointer to a @ref LL_I2C_InitTypeDef structure.
  115. * @retval An ErrorStatus enumeration value:
  116. * - SUCCESS I2C registers are initialized
  117. * - ERROR Not applicable
  118. */
  119. uint32_t LL_I2C_Init(I2C_TypeDef *I2Cx, LL_I2C_InitTypeDef *I2C_InitStruct)
  120. {
  121. LL_RCC_ClocksTypeDef rcc_clocks;
  122. /* Check the I2C Instance I2Cx */
  123. assert_param(IS_I2C_ALL_INSTANCE(I2Cx));
  124. /* Check the I2C parameters from I2C_InitStruct */
  125. assert_param(IS_LL_I2C_PERIPHERAL_MODE(I2C_InitStruct->PeripheralMode));
  126. assert_param(IS_LL_I2C_CLOCK_SPEED(I2C_InitStruct->ClockSpeed));
  127. assert_param(IS_LL_I2C_DUTY_CYCLE(I2C_InitStruct->DutyCycle));
  128. #if defined(I2C_FLTR_ANOFF)&&defined(I2C_FLTR_DNF)
  129. assert_param(IS_LL_I2C_ANALOG_FILTER(I2C_InitStruct->AnalogFilter));
  130. assert_param(IS_LL_I2C_DIGITAL_FILTER(I2C_InitStruct->DigitalFilter));
  131. #endif
  132. assert_param(IS_LL_I2C_OWN_ADDRESS1(I2C_InitStruct->OwnAddress1));
  133. assert_param(IS_LL_I2C_TYPE_ACKNOWLEDGE(I2C_InitStruct->TypeAcknowledge));
  134. assert_param(IS_LL_I2C_OWN_ADDRSIZE(I2C_InitStruct->OwnAddrSize));
  135. /* Disable the selected I2Cx Peripheral */
  136. LL_I2C_Disable(I2Cx);
  137. /* Retrieve Clock frequencies */
  138. LL_RCC_GetSystemClocksFreq(&rcc_clocks);
  139. #if defined(I2C_FLTR_ANOFF)&&defined(I2C_FLTR_DNF)
  140. /*---------------------------- I2Cx FLTR Configuration -----------------------
  141. * Configure the analog and digital noise filters with parameters :
  142. * - AnalogFilter: I2C_FLTR_ANFOFF bit
  143. * - DigitalFilter: I2C_FLTR_DNF[3:0] bits
  144. */
  145. LL_I2C_ConfigFilters(I2Cx, I2C_InitStruct->AnalogFilter, I2C_InitStruct->DigitalFilter);
  146. #endif
  147. /*---------------------------- I2Cx SCL Clock Speed Configuration ------------
  148. * Configure the SCL speed :
  149. * - ClockSpeed: I2C_CR2_FREQ[5:0], I2C_TRISE_TRISE[5:0], I2C_CCR_FS,
  150. * and I2C_CCR_CCR[11:0] bits
  151. * - DutyCycle: I2C_CCR_DUTY[7:0] bits
  152. */
  153. LL_I2C_ConfigSpeed(I2Cx, rcc_clocks.PCLK1_Frequency, I2C_InitStruct->ClockSpeed, I2C_InitStruct->DutyCycle);
  154. /*---------------------------- I2Cx OAR1 Configuration -----------------------
  155. * Disable, Configure and Enable I2Cx device own address 1 with parameters :
  156. * - OwnAddress1: I2C_OAR1_ADD[9:8], I2C_OAR1_ADD[7:1] and I2C_OAR1_ADD0 bits
  157. * - OwnAddrSize: I2C_OAR1_ADDMODE bit
  158. */
  159. LL_I2C_SetOwnAddress1(I2Cx, I2C_InitStruct->OwnAddress1, I2C_InitStruct->OwnAddrSize);
  160. /*---------------------------- I2Cx MODE Configuration -----------------------
  161. * Configure I2Cx peripheral mode with parameter :
  162. * - PeripheralMode: I2C_CR1_SMBUS, I2C_CR1_SMBTYPE and I2C_CR1_ENARP bits
  163. */
  164. LL_I2C_SetMode(I2Cx, I2C_InitStruct->PeripheralMode);
  165. /* Enable the selected I2Cx Peripheral */
  166. LL_I2C_Enable(I2Cx);
  167. /*---------------------------- I2Cx CR2 Configuration ------------------------
  168. * Configure the ACKnowledge or Non ACKnowledge condition
  169. * after the address receive match code or next received byte with parameter :
  170. * - TypeAcknowledge: I2C_CR2_NACK bit
  171. */
  172. LL_I2C_AcknowledgeNextData(I2Cx, I2C_InitStruct->TypeAcknowledge);
  173. return SUCCESS;
  174. }
  175. /**
  176. * @brief Set each @ref LL_I2C_InitTypeDef field to default value.
  177. * @param I2C_InitStruct Pointer to a @ref LL_I2C_InitTypeDef structure.
  178. * @retval None
  179. */
  180. void LL_I2C_StructInit(LL_I2C_InitTypeDef *I2C_InitStruct)
  181. {
  182. /* Set I2C_InitStruct fields to default values */
  183. I2C_InitStruct->PeripheralMode = LL_I2C_MODE_I2C;
  184. I2C_InitStruct->ClockSpeed = 5000U;
  185. I2C_InitStruct->DutyCycle = LL_I2C_DUTYCYCLE_2;
  186. #if defined(I2C_FLTR_ANOFF)&&defined(I2C_FLTR_DNF)
  187. I2C_InitStruct->AnalogFilter = LL_I2C_ANALOGFILTER_ENABLE;
  188. I2C_InitStruct->DigitalFilter = 0U;
  189. #endif
  190. I2C_InitStruct->OwnAddress1 = 0U;
  191. I2C_InitStruct->TypeAcknowledge = LL_I2C_NACK;
  192. I2C_InitStruct->OwnAddrSize = LL_I2C_OWNADDRESS1_7BIT;
  193. }
  194. /**
  195. * @}
  196. */
  197. /**
  198. * @}
  199. */
  200. /**
  201. * @}
  202. */
  203. #endif /* I2C1 || I2C2 || I2C3 */
  204. /**
  205. * @}
  206. */
  207. #endif /* USE_FULL_LL_DRIVER */