i2c.c 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206
  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) 2018 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. #include "gpio.h"
  52. /* USER CODE BEGIN 0 */
  53. /* USER CODE END 0 */
  54. I2C_HandleTypeDef hi2c1;
  55. I2C_HandleTypeDef hi2c2;
  56. /* I2C1 init function */
  57. void MX_I2C1_Init(void)
  58. {
  59. hi2c1.Instance = I2C1;
  60. hi2c1.Init.ClockSpeed = 100000;
  61. hi2c1.Init.DutyCycle = I2C_DUTYCYCLE_2;
  62. hi2c1.Init.OwnAddress1 = 0;
  63. hi2c1.Init.AddressingMode = I2C_ADDRESSINGMODE_7BIT;
  64. hi2c1.Init.DualAddressMode = I2C_DUALADDRESS_DISABLE;
  65. hi2c1.Init.OwnAddress2 = 0;
  66. hi2c1.Init.GeneralCallMode = I2C_GENERALCALL_DISABLE;
  67. hi2c1.Init.NoStretchMode = I2C_NOSTRETCH_DISABLE;
  68. if (HAL_I2C_Init(&hi2c1) != HAL_OK)
  69. {
  70. _Error_Handler(__FILE__, __LINE__);
  71. }
  72. }
  73. /* I2C2 init function */
  74. void MX_I2C2_Init(void)
  75. {
  76. hi2c2.Instance = I2C2;
  77. hi2c2.Init.ClockSpeed = 100000;
  78. hi2c2.Init.DutyCycle = I2C_DUTYCYCLE_2;
  79. hi2c2.Init.OwnAddress1 = 0;
  80. hi2c2.Init.AddressingMode = I2C_ADDRESSINGMODE_7BIT;
  81. hi2c2.Init.DualAddressMode = I2C_DUALADDRESS_DISABLE;
  82. hi2c2.Init.OwnAddress2 = 0;
  83. hi2c2.Init.GeneralCallMode = I2C_GENERALCALL_DISABLE;
  84. hi2c2.Init.NoStretchMode = I2C_NOSTRETCH_DISABLE;
  85. if (HAL_I2C_Init(&hi2c2) != HAL_OK)
  86. {
  87. _Error_Handler(__FILE__, __LINE__);
  88. }
  89. }
  90. void HAL_I2C_MspInit(I2C_HandleTypeDef* i2cHandle)
  91. {
  92. GPIO_InitTypeDef GPIO_InitStruct;
  93. if(i2cHandle->Instance==I2C1)
  94. {
  95. /* USER CODE BEGIN I2C1_MspInit 0 */
  96. /* USER CODE END I2C1_MspInit 0 */
  97. /**I2C1 GPIO Configuration
  98. PB6 ------> I2C1_SCL
  99. PB7 ------> I2C1_SDA
  100. */
  101. GPIO_InitStruct.Pin = GPIO_PIN_6|GPIO_PIN_7;
  102. GPIO_InitStruct.Mode = GPIO_MODE_AF_OD;
  103. GPIO_InitStruct.Pull = GPIO_PULLUP;
  104. GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_VERY_HIGH;
  105. GPIO_InitStruct.Alternate = GPIO_AF4_I2C1;
  106. HAL_GPIO_Init(GPIOB, &GPIO_InitStruct);
  107. /* I2C1 clock enable */
  108. __HAL_RCC_I2C1_CLK_ENABLE();
  109. /* USER CODE BEGIN I2C1_MspInit 1 */
  110. /* USER CODE END I2C1_MspInit 1 */
  111. }
  112. else if(i2cHandle->Instance==I2C2)
  113. {
  114. /* USER CODE BEGIN I2C2_MspInit 0 */
  115. /* USER CODE END I2C2_MspInit 0 */
  116. /**I2C2 GPIO Configuration
  117. PF0 ------> I2C2_SDA
  118. PF1 ------> I2C2_SCL
  119. */
  120. GPIO_InitStruct.Pin = GPIO_PIN_0|GPIO_PIN_1;
  121. GPIO_InitStruct.Mode = GPIO_MODE_AF_OD;
  122. GPIO_InitStruct.Pull = GPIO_PULLUP;
  123. GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_VERY_HIGH;
  124. GPIO_InitStruct.Alternate = GPIO_AF4_I2C2;
  125. HAL_GPIO_Init(GPIOF, &GPIO_InitStruct);
  126. /* I2C2 clock enable */
  127. __HAL_RCC_I2C2_CLK_ENABLE();
  128. /* USER CODE BEGIN I2C2_MspInit 1 */
  129. /* USER CODE END I2C2_MspInit 1 */
  130. }
  131. }
  132. void HAL_I2C_MspDeInit(I2C_HandleTypeDef* i2cHandle)
  133. {
  134. if(i2cHandle->Instance==I2C1)
  135. {
  136. /* USER CODE BEGIN I2C1_MspDeInit 0 */
  137. /* USER CODE END I2C1_MspDeInit 0 */
  138. /* Peripheral clock disable */
  139. __HAL_RCC_I2C1_CLK_DISABLE();
  140. /**I2C1 GPIO Configuration
  141. PB6 ------> I2C1_SCL
  142. PB7 ------> I2C1_SDA
  143. */
  144. HAL_GPIO_DeInit(GPIOB, GPIO_PIN_6|GPIO_PIN_7);
  145. /* USER CODE BEGIN I2C1_MspDeInit 1 */
  146. /* USER CODE END I2C1_MspDeInit 1 */
  147. }
  148. else if(i2cHandle->Instance==I2C2)
  149. {
  150. /* USER CODE BEGIN I2C2_MspDeInit 0 */
  151. /* USER CODE END I2C2_MspDeInit 0 */
  152. /* Peripheral clock disable */
  153. __HAL_RCC_I2C2_CLK_DISABLE();
  154. /**I2C2 GPIO Configuration
  155. PF0 ------> I2C2_SDA
  156. PF1 ------> I2C2_SCL
  157. */
  158. HAL_GPIO_DeInit(GPIOF, GPIO_PIN_0|GPIO_PIN_1);
  159. /* USER CODE BEGIN I2C2_MspDeInit 1 */
  160. /* USER CODE END I2C2_MspDeInit 1 */
  161. }
  162. }
  163. /* USER CODE BEGIN 1 */
  164. /* USER CODE END 1 */
  165. /**
  166. * @}
  167. */
  168. /**
  169. * @}
  170. */
  171. /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/