i2c.c 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  1. /**
  2. ******************************************************************************
  3. * File Name : I2C.c
  4. * Description : This file provides code for the configuration
  5. * of the I2C instances.
  6. ******************************************************************************
  7. *
  8. * Copyright (c) 2017 STMicroelectronics International N.V.
  9. * All rights reserved.
  10. *
  11. * Redistribution and use in source and binary forms, with or without
  12. * modification, are permitted, provided that the following conditions are met:
  13. *
  14. * 1. Redistribution of source code must retain the above copyright notice,
  15. * this list of conditions and the following disclaimer.
  16. * 2. Redistributions in binary form must reproduce the above copyright notice,
  17. * this list of conditions and the following disclaimer in the documentation
  18. * and/or other materials provided with the distribution.
  19. * 3. Neither the name of STMicroelectronics nor the names of other
  20. * contributors to this software may be used to endorse or promote products
  21. * derived from this software without specific written permission.
  22. * 4. This software, including modifications and/or derivative works of this
  23. * software, must execute solely and exclusively on microcontroller or
  24. * microprocessor devices manufactured by or for STMicroelectronics.
  25. * 5. Redistribution and use of this software other than as permitted under
  26. * this license is void and will automatically terminate your rights under
  27. * this license.
  28. *
  29. * THIS SOFTWARE IS PROVIDED BY STMICROELECTRONICS AND CONTRIBUTORS "AS IS"
  30. * AND ANY EXPRESS, IMPLIED OR STATUTORY WARRANTIES, INCLUDING, BUT NOT
  31. * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR A
  32. * PARTICULAR PURPOSE AND NON-INFRINGEMENT OF THIRD PARTY INTELLECTUAL PROPERTY
  33. * RIGHTS ARE DISCLAIMED TO THE FULLEST EXTENT PERMITTED BY LAW. IN NO EVENT
  34. * SHALL STMICROELECTRONICS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
  35. * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  36. * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA,
  37. * OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
  38. * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
  39. * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
  40. * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  41. *
  42. ******************************************************************************
  43. */
  44. /* Includes ------------------------------------------------------------------*/
  45. #include "i2c.h"
  46. #include "gpio.h"
  47. /* USER CODE BEGIN 0 */
  48. /* USER CODE END 0 */
  49. I2C_HandleTypeDef hi2c2;
  50. /* I2C2 init function */
  51. void MX_I2C2_Init(void)
  52. {
  53. hi2c2.Instance = I2C2;
  54. hi2c2.Init.Timing = 0x2000090E;
  55. hi2c2.Init.OwnAddress1 = 0;
  56. hi2c2.Init.AddressingMode = I2C_ADDRESSINGMODE_7BIT;
  57. hi2c2.Init.DualAddressMode = I2C_DUALADDRESS_DISABLE;
  58. hi2c2.Init.OwnAddress2 = 0;
  59. hi2c2.Init.OwnAddress2Masks = I2C_OA2_NOMASK;
  60. hi2c2.Init.GeneralCallMode = I2C_GENERALCALL_DISABLE;
  61. hi2c2.Init.NoStretchMode = I2C_NOSTRETCH_DISABLE;
  62. if (HAL_I2C_Init(&hi2c2) != HAL_OK)
  63. {
  64. Error_Handler();
  65. }
  66. /**Configure Analogue filter
  67. */
  68. if (HAL_I2CEx_ConfigAnalogFilter(&hi2c2, I2C_ANALOGFILTER_ENABLE) != HAL_OK)
  69. {
  70. Error_Handler();
  71. }
  72. }
  73. void HAL_I2C_MspInit(I2C_HandleTypeDef* i2cHandle)
  74. {
  75. GPIO_InitTypeDef GPIO_InitStruct;
  76. if(i2cHandle->Instance==I2C2)
  77. {
  78. /* USER CODE BEGIN I2C2_MspInit 0 */
  79. /* USER CODE END I2C2_MspInit 0 */
  80. /**I2C2 GPIO Configuration
  81. PB10 ------> I2C2_SCL
  82. PB11 ------> I2C2_SDA
  83. */
  84. GPIO_InitStruct.Pin = GPIO_PIN_10|GPIO_PIN_11;
  85. GPIO_InitStruct.Mode = GPIO_MODE_AF_OD;
  86. GPIO_InitStruct.Pull = GPIO_PULLUP;
  87. GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_HIGH;
  88. GPIO_InitStruct.Alternate = GPIO_AF1_I2C2;
  89. HAL_GPIO_Init(GPIOB, &GPIO_InitStruct);
  90. /* Peripheral clock enable */
  91. __HAL_RCC_I2C2_CLK_ENABLE();
  92. /* USER CODE BEGIN I2C2_MspInit 1 */
  93. /* USER CODE END I2C2_MspInit 1 */
  94. }
  95. }
  96. void HAL_I2C_MspDeInit(I2C_HandleTypeDef* i2cHandle)
  97. {
  98. if(i2cHandle->Instance==I2C2)
  99. {
  100. /* USER CODE BEGIN I2C2_MspDeInit 0 */
  101. /* USER CODE END I2C2_MspDeInit 0 */
  102. /* Peripheral clock disable */
  103. __HAL_RCC_I2C2_CLK_DISABLE();
  104. /**I2C2 GPIO Configuration
  105. PB10 ------> I2C2_SCL
  106. PB11 ------> I2C2_SDA
  107. */
  108. HAL_GPIO_DeInit(GPIOB, GPIO_PIN_10|GPIO_PIN_11);
  109. }
  110. /* USER CODE BEGIN I2C2_MspDeInit 1 */
  111. /* USER CODE END I2C2_MspDeInit 1 */
  112. }
  113. /* USER CODE BEGIN 1 */
  114. /* USER CODE END 1 */
  115. /**
  116. * @}
  117. */
  118. /**
  119. * @}
  120. */
  121. /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/