i2c.c 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208
  1. /**
  2. ******************************************************************************
  3. * File Name : I2C.c
  4. * Description : This file provides code for the configuration
  5. * of the I2C instances.
  6. ******************************************************************************
  7. * This notice applies to any and all portions of this file
  8. * that are not between comment pairs USER CODE BEGIN and
  9. * USER CODE END. Other portions of this file, whether
  10. * inserted by the user or by software development tools
  11. * are owned by their respective copyright owners.
  12. *
  13. * Copyright (c) 2019 STMicroelectronics International N.V.
  14. * All rights reserved.
  15. *
  16. * Redistribution and use in source and binary forms, with or without
  17. * modification, are permitted, provided that the following conditions are met:
  18. *
  19. * 1. Redistribution of source code must retain the above copyright notice,
  20. * this list of conditions and the following disclaimer.
  21. * 2. Redistributions in binary form must reproduce the above copyright notice,
  22. * this list of conditions and the following disclaimer in the documentation
  23. * and/or other materials provided with the distribution.
  24. * 3. Neither the name of STMicroelectronics nor the names of other
  25. * contributors to this software may be used to endorse or promote products
  26. * derived from this software without specific written permission.
  27. * 4. This software, including modifications and/or derivative works of this
  28. * software, must execute solely and exclusively on microcontroller or
  29. * microprocessor devices manufactured by or for STMicroelectronics.
  30. * 5. Redistribution and use of this software other than as permitted under
  31. * this license is void and will automatically terminate your rights under
  32. * this license.
  33. *
  34. * THIS SOFTWARE IS PROVIDED BY STMICROELECTRONICS AND CONTRIBUTORS "AS IS"
  35. * AND ANY EXPRESS, IMPLIED OR STATUTORY WARRANTIES, INCLUDING, BUT NOT
  36. * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR A
  37. * PARTICULAR PURPOSE AND NON-INFRINGEMENT OF THIRD PARTY INTELLECTUAL PROPERTY
  38. * RIGHTS ARE DISCLAIMED TO THE FULLEST EXTENT PERMITTED BY LAW. IN NO EVENT
  39. * SHALL STMICROELECTRONICS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
  40. * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  41. * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA,
  42. * OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
  43. * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
  44. * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
  45. * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  46. *
  47. ******************************************************************************
  48. */
  49. /* Includes ------------------------------------------------------------------*/
  50. #include "i2c.h"
  51. /* USER CODE BEGIN 0 */
  52. /* USER CODE END 0 */
  53. I2C_HandleTypeDef hi2c2;
  54. I2C_HandleTypeDef hi2c3;
  55. /* I2C2 init function */
  56. void MX_I2C2_Init(void) {
  57. hi2c2.Instance = I2C2;
  58. hi2c2.Init.ClockSpeed = 100000;
  59. hi2c2.Init.DutyCycle = I2C_DUTYCYCLE_2;
  60. hi2c2.Init.OwnAddress1 = 0;
  61. hi2c2.Init.AddressingMode = I2C_ADDRESSINGMODE_7BIT;
  62. hi2c2.Init.DualAddressMode = I2C_DUALADDRESS_DISABLE;
  63. hi2c2.Init.OwnAddress2 = 0;
  64. hi2c2.Init.GeneralCallMode = I2C_GENERALCALL_DISABLE;
  65. hi2c2.Init.NoStretchMode = I2C_NOSTRETCH_DISABLE;
  66. if (HAL_I2C_Init(&hi2c2) != HAL_OK) {
  67. Error_Handler();
  68. }
  69. }
  70. /* I2C3 init function */
  71. void MX_I2C3_Init(void) {
  72. hi2c3.Instance = I2C3;
  73. hi2c3.Init.ClockSpeed = 50000;
  74. hi2c3.Init.DutyCycle = I2C_DUTYCYCLE_2;
  75. hi2c3.Init.OwnAddress1 = 0;
  76. hi2c3.Init.AddressingMode = I2C_ADDRESSINGMODE_7BIT;
  77. hi2c3.Init.DualAddressMode = I2C_DUALADDRESS_DISABLE;
  78. hi2c3.Init.OwnAddress2 = 0;
  79. hi2c3.Init.GeneralCallMode = I2C_GENERALCALL_DISABLE;
  80. hi2c3.Init.NoStretchMode = I2C_NOSTRETCH_DISABLE;
  81. if (HAL_I2C_Init(&hi2c3) != HAL_OK) {
  82. Error_Handler();
  83. }
  84. }
  85. void HAL_I2C_MspInit(I2C_HandleTypeDef* i2cHandle) {
  86. GPIO_InitTypeDef GPIO_InitStruct = { 0 };
  87. if (i2cHandle->Instance == I2C2) {
  88. /* USER CODE BEGIN I2C2_MspInit 0 */
  89. /* USER CODE END I2C2_MspInit 0 */
  90. __HAL_RCC_GPIOB_CLK_ENABLE()
  91. ;
  92. /**I2C2 GPIO Configuration
  93. PB10 ------> I2C2_SCL
  94. PB3 ------> I2C2_SDA
  95. */
  96. GPIO_InitStruct.Pin = GPIO_PIN_10;
  97. GPIO_InitStruct.Mode = GPIO_MODE_AF_OD;
  98. GPIO_InitStruct.Pull = GPIO_PULLUP;
  99. GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_VERY_HIGH;
  100. GPIO_InitStruct.Alternate = GPIO_AF4_I2C2;
  101. HAL_GPIO_Init(GPIOB, &GPIO_InitStruct);
  102. GPIO_InitStruct.Pin = GPIO_PIN_3;
  103. GPIO_InitStruct.Mode = GPIO_MODE_AF_OD;
  104. GPIO_InitStruct.Pull = GPIO_PULLUP;
  105. GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_VERY_HIGH;
  106. GPIO_InitStruct.Alternate = GPIO_AF9_I2C2;
  107. HAL_GPIO_Init(GPIOB, &GPIO_InitStruct);
  108. /* I2C2 clock enable */
  109. __HAL_RCC_I2C2_CLK_ENABLE()
  110. ;
  111. /* USER CODE BEGIN I2C2_MspInit 1 */
  112. /* USER CODE END I2C2_MspInit 1 */
  113. } else if (i2cHandle->Instance == I2C3) {
  114. /* USER CODE BEGIN I2C3_MspInit 0 */
  115. /* USER CODE END I2C3_MspInit 0 */
  116. __HAL_RCC_GPIOA_CLK_ENABLE()
  117. ;
  118. __HAL_RCC_GPIOB_CLK_ENABLE()
  119. ;
  120. /**I2C3 GPIO Configuration
  121. PA8 ------> I2C3_SCL
  122. PB4 ------> I2C3_SDA
  123. */
  124. GPIO_InitStruct.Pin = GPIO_PIN_8;
  125. GPIO_InitStruct.Mode = GPIO_MODE_AF_OD;
  126. GPIO_InitStruct.Pull = GPIO_PULLUP;
  127. GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_VERY_HIGH;
  128. GPIO_InitStruct.Alternate = GPIO_AF4_I2C3;
  129. HAL_GPIO_Init(GPIOA, &GPIO_InitStruct);
  130. GPIO_InitStruct.Pin = GPIO_PIN_4;
  131. GPIO_InitStruct.Mode = GPIO_MODE_AF_OD;
  132. GPIO_InitStruct.Pull = GPIO_PULLUP;
  133. GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_VERY_HIGH;
  134. GPIO_InitStruct.Alternate = GPIO_AF9_I2C3;
  135. HAL_GPIO_Init(GPIOB, &GPIO_InitStruct);
  136. /* I2C3 clock enable */
  137. __HAL_RCC_I2C3_CLK_ENABLE()
  138. ;
  139. /* USER CODE BEGIN I2C3_MspInit 1 */
  140. /* USER CODE END I2C3_MspInit 1 */
  141. }
  142. }
  143. void HAL_I2C_MspDeInit(I2C_HandleTypeDef* i2cHandle) {
  144. if (i2cHandle->Instance == I2C2) {
  145. /* USER CODE BEGIN I2C2_MspDeInit 0 */
  146. /* USER CODE END I2C2_MspDeInit 0 */
  147. /* Peripheral clock disable */
  148. __HAL_RCC_I2C2_CLK_DISABLE();
  149. /**I2C2 GPIO Configuration
  150. PB10 ------> I2C2_SCL
  151. PB3 ------> I2C2_SDA
  152. */
  153. HAL_GPIO_DeInit(GPIOB, GPIO_PIN_10 | GPIO_PIN_3);
  154. /* USER CODE BEGIN I2C2_MspDeInit 1 */
  155. /* USER CODE END I2C2_MspDeInit 1 */
  156. } else if (i2cHandle->Instance == I2C3) {
  157. /* USER CODE BEGIN I2C3_MspDeInit 0 */
  158. /* USER CODE END I2C3_MspDeInit 0 */
  159. /* Peripheral clock disable */
  160. __HAL_RCC_I2C3_CLK_DISABLE();
  161. /**I2C3 GPIO Configuration
  162. PA8 ------> I2C3_SCL
  163. PB4 ------> I2C3_SDA
  164. */
  165. HAL_GPIO_DeInit(GPIOA, GPIO_PIN_8);
  166. HAL_GPIO_DeInit(GPIOB, GPIO_PIN_4);
  167. /* USER CODE BEGIN I2C3_MspDeInit 1 */
  168. /* USER CODE END I2C3_MspDeInit 1 */
  169. }
  170. }
  171. /* USER CODE BEGIN 1 */
  172. /* USER CODE END 1 */
  173. /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/