i2c.c 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184
  1. /**
  2. ******************************************************************************
  3. * File Name : I2C.c
  4. * Description : This file provides code for the configuration
  5. * of the I2C instances.
  6. ******************************************************************************
  7. * @attention
  8. *
  9. * <h2><center>&copy; Copyright (c) 2020 STMicroelectronics.
  10. * All rights reserved.</center></h2>
  11. *
  12. * This software component is licensed by ST 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. */
  19. /* Includes ------------------------------------------------------------------*/
  20. #include "i2c.h"
  21. /* USER CODE BEGIN 0 */
  22. /* USER CODE END 0 */
  23. I2C_HandleTypeDef hi2c2;
  24. I2C_HandleTypeDef hi2c3;
  25. /* I2C2 init function */
  26. void MX_I2C2_Init(void)
  27. {
  28. hi2c2.Instance = I2C2;
  29. hi2c2.Init.ClockSpeed = 100000;
  30. hi2c2.Init.DutyCycle = I2C_DUTYCYCLE_2;
  31. hi2c2.Init.OwnAddress1 = 0;
  32. hi2c2.Init.AddressingMode = I2C_ADDRESSINGMODE_7BIT;
  33. hi2c2.Init.DualAddressMode = I2C_DUALADDRESS_DISABLE;
  34. hi2c2.Init.OwnAddress2 = 0;
  35. hi2c2.Init.GeneralCallMode = I2C_GENERALCALL_DISABLE;
  36. hi2c2.Init.NoStretchMode = I2C_NOSTRETCH_DISABLE;
  37. if (HAL_I2C_Init(&hi2c2) != HAL_OK)
  38. {
  39. Error_Handler();
  40. }
  41. }
  42. /* I2C3 init function */
  43. void MX_I2C3_Init(void)
  44. {
  45. hi2c3.Instance = I2C3;
  46. hi2c3.Init.ClockSpeed = 50000;
  47. hi2c3.Init.DutyCycle = I2C_DUTYCYCLE_2;
  48. hi2c3.Init.OwnAddress1 = 0;
  49. hi2c3.Init.AddressingMode = I2C_ADDRESSINGMODE_7BIT;
  50. hi2c3.Init.DualAddressMode = I2C_DUALADDRESS_DISABLE;
  51. hi2c3.Init.OwnAddress2 = 0;
  52. hi2c3.Init.GeneralCallMode = I2C_GENERALCALL_DISABLE;
  53. hi2c3.Init.NoStretchMode = I2C_NOSTRETCH_DISABLE;
  54. if (HAL_I2C_Init(&hi2c3) != HAL_OK)
  55. {
  56. Error_Handler();
  57. }
  58. }
  59. void HAL_I2C_MspInit(I2C_HandleTypeDef* i2cHandle)
  60. {
  61. GPIO_InitTypeDef GPIO_InitStruct = {0};
  62. if(i2cHandle->Instance==I2C2)
  63. {
  64. /* USER CODE BEGIN I2C2_MspInit 0 */
  65. /* USER CODE END I2C2_MspInit 0 */
  66. __HAL_RCC_GPIOB_CLK_ENABLE();
  67. /**I2C2 GPIO Configuration
  68. PB10 ------> I2C2_SCL
  69. PB3 ------> I2C2_SDA
  70. */
  71. GPIO_InitStruct.Pin = GPIO_PIN_10;
  72. GPIO_InitStruct.Mode = GPIO_MODE_AF_OD;
  73. GPIO_InitStruct.Pull = GPIO_PULLUP;
  74. GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_VERY_HIGH;
  75. GPIO_InitStruct.Alternate = GPIO_AF4_I2C2;
  76. HAL_GPIO_Init(GPIOB, &GPIO_InitStruct);
  77. GPIO_InitStruct.Pin = GPIO_PIN_3;
  78. GPIO_InitStruct.Mode = GPIO_MODE_AF_OD;
  79. GPIO_InitStruct.Pull = GPIO_PULLUP;
  80. GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_VERY_HIGH;
  81. GPIO_InitStruct.Alternate = GPIO_AF9_I2C2;
  82. HAL_GPIO_Init(GPIOB, &GPIO_InitStruct);
  83. /* I2C2 clock enable */
  84. __HAL_RCC_I2C2_CLK_ENABLE();
  85. /* USER CODE BEGIN I2C2_MspInit 1 */
  86. /* USER CODE END I2C2_MspInit 1 */
  87. }
  88. else if(i2cHandle->Instance==I2C3)
  89. {
  90. /* USER CODE BEGIN I2C3_MspInit 0 */
  91. /* USER CODE END I2C3_MspInit 0 */
  92. __HAL_RCC_GPIOA_CLK_ENABLE();
  93. __HAL_RCC_GPIOB_CLK_ENABLE();
  94. /**I2C3 GPIO Configuration
  95. PA8 ------> I2C3_SCL
  96. PB4 ------> I2C3_SDA
  97. */
  98. GPIO_InitStruct.Pin = GPIO_PIN_8;
  99. GPIO_InitStruct.Mode = GPIO_MODE_AF_OD;
  100. GPIO_InitStruct.Pull = GPIO_PULLUP;
  101. GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_VERY_HIGH;
  102. GPIO_InitStruct.Alternate = GPIO_AF4_I2C3;
  103. HAL_GPIO_Init(GPIOA, &GPIO_InitStruct);
  104. GPIO_InitStruct.Pin = GPIO_PIN_4;
  105. GPIO_InitStruct.Mode = GPIO_MODE_AF_OD;
  106. GPIO_InitStruct.Pull = GPIO_PULLUP;
  107. GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_VERY_HIGH;
  108. GPIO_InitStruct.Alternate = GPIO_AF9_I2C3;
  109. HAL_GPIO_Init(GPIOB, &GPIO_InitStruct);
  110. /* I2C3 clock enable */
  111. __HAL_RCC_I2C3_CLK_ENABLE();
  112. /* USER CODE BEGIN I2C3_MspInit 1 */
  113. /* USER CODE END I2C3_MspInit 1 */
  114. }
  115. }
  116. void HAL_I2C_MspDeInit(I2C_HandleTypeDef* i2cHandle)
  117. {
  118. if(i2cHandle->Instance==I2C2)
  119. {
  120. /* USER CODE BEGIN I2C2_MspDeInit 0 */
  121. /* USER CODE END I2C2_MspDeInit 0 */
  122. /* Peripheral clock disable */
  123. __HAL_RCC_I2C2_CLK_DISABLE();
  124. /**I2C2 GPIO Configuration
  125. PB10 ------> I2C2_SCL
  126. PB3 ------> I2C2_SDA
  127. */
  128. HAL_GPIO_DeInit(GPIOB, GPIO_PIN_10|GPIO_PIN_3);
  129. /* USER CODE BEGIN I2C2_MspDeInit 1 */
  130. /* USER CODE END I2C2_MspDeInit 1 */
  131. }
  132. else if(i2cHandle->Instance==I2C3)
  133. {
  134. /* USER CODE BEGIN I2C3_MspDeInit 0 */
  135. /* USER CODE END I2C3_MspDeInit 0 */
  136. /* Peripheral clock disable */
  137. __HAL_RCC_I2C3_CLK_DISABLE();
  138. /**I2C3 GPIO Configuration
  139. PA8 ------> I2C3_SCL
  140. PB4 ------> I2C3_SDA
  141. */
  142. HAL_GPIO_DeInit(GPIOA, GPIO_PIN_8);
  143. HAL_GPIO_DeInit(GPIOB, GPIO_PIN_4);
  144. /* USER CODE BEGIN I2C3_MspDeInit 1 */
  145. /* USER CODE END I2C3_MspDeInit 1 */
  146. }
  147. }
  148. /* USER CODE BEGIN 1 */
  149. /* USER CODE END 1 */
  150. /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/