i2c.c 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  1. /* USER CODE BEGIN Header */
  2. /**
  3. ******************************************************************************
  4. * @file i2c.c
  5. * @brief This file provides code for the configuration
  6. * of the I2C instances.
  7. ******************************************************************************
  8. * @attention
  9. *
  10. * Copyright (c) 2025 STMicroelectronics.
  11. * All rights reserved.
  12. *
  13. * This software is licensed under terms that can be found in the LICENSE file
  14. * in the root directory of this software component.
  15. * If no LICENSE file comes with this software, it is provided AS-IS.
  16. *
  17. ******************************************************************************
  18. */
  19. /* USER CODE END Header */
  20. /* Includes ------------------------------------------------------------------*/
  21. #include "i2c.h"
  22. /* USER CODE BEGIN 0 */
  23. /* USER CODE END 0 */
  24. I2C_HandleTypeDef hi2c2;
  25. /* I2C2 init function */
  26. void MX_I2C2_Init(void)
  27. {
  28. /* USER CODE BEGIN I2C2_Init 0 */
  29. /* USER CODE END I2C2_Init 0 */
  30. /* USER CODE BEGIN I2C2_Init 1 */
  31. /* USER CODE END I2C2_Init 1 */
  32. hi2c2.Instance = I2C2;
  33. hi2c2.Init.Timing = 0x30A54E69;
  34. hi2c2.Init.OwnAddress1 = 0;
  35. hi2c2.Init.AddressingMode = I2C_ADDRESSINGMODE_7BIT;
  36. hi2c2.Init.DualAddressMode = I2C_DUALADDRESS_DISABLE;
  37. hi2c2.Init.OwnAddress2 = 0;
  38. hi2c2.Init.OwnAddress2Masks = I2C_OA2_NOMASK;
  39. hi2c2.Init.GeneralCallMode = I2C_GENERALCALL_DISABLE;
  40. hi2c2.Init.NoStretchMode = I2C_NOSTRETCH_DISABLE;
  41. if (HAL_I2C_Init(&hi2c2) != HAL_OK)
  42. {
  43. Error_Handler();
  44. }
  45. /** Configure Analogue filter
  46. */
  47. if (HAL_I2CEx_ConfigAnalogFilter(&hi2c2, I2C_ANALOGFILTER_ENABLE) != HAL_OK)
  48. {
  49. Error_Handler();
  50. }
  51. /** Configure Digital filter
  52. */
  53. if (HAL_I2CEx_ConfigDigitalFilter(&hi2c2, 0) != HAL_OK)
  54. {
  55. Error_Handler();
  56. }
  57. /* USER CODE BEGIN I2C2_Init 2 */
  58. /* USER CODE END I2C2_Init 2 */
  59. }
  60. void HAL_I2C_MspInit(I2C_HandleTypeDef* i2cHandle)
  61. {
  62. GPIO_InitTypeDef GPIO_InitStruct = {0};
  63. RCC_PeriphCLKInitTypeDef PeriphClkInit = {0};
  64. if(i2cHandle->Instance==I2C2)
  65. {
  66. /* USER CODE BEGIN I2C2_MspInit 0 */
  67. /* USER CODE END I2C2_MspInit 0 */
  68. /** Initializes the peripherals clock
  69. */
  70. PeriphClkInit.PeriphClockSelection = RCC_PERIPHCLK_I2C2;
  71. PeriphClkInit.I2c2ClockSelection = RCC_I2C2CLKSOURCE_PCLK1;
  72. if (HAL_RCCEx_PeriphCLKConfig(&PeriphClkInit) != HAL_OK)
  73. {
  74. Error_Handler();
  75. }
  76. __HAL_RCC_GPIOB_CLK_ENABLE();
  77. /**I2C2 GPIO Configuration
  78. PB13 ------> I2C2_SCL
  79. PB14 ------> I2C2_SDA
  80. */
  81. GPIO_InitStruct.Pin = GPIO_PIN_13|GPIO_PIN_14;
  82. GPIO_InitStruct.Mode = GPIO_MODE_AF_OD;
  83. GPIO_InitStruct.Pull = GPIO_NOPULL;
  84. GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW;
  85. GPIO_InitStruct.Alternate = GPIO_AF4_I2C2;
  86. HAL_GPIO_Init(GPIOB, &GPIO_InitStruct);
  87. /* I2C2 clock enable */
  88. __HAL_RCC_I2C2_CLK_ENABLE();
  89. /* USER CODE BEGIN I2C2_MspInit 1 */
  90. /* USER CODE END I2C2_MspInit 1 */
  91. }
  92. }
  93. void HAL_I2C_MspDeInit(I2C_HandleTypeDef* i2cHandle)
  94. {
  95. if(i2cHandle->Instance==I2C2)
  96. {
  97. /* USER CODE BEGIN I2C2_MspDeInit 0 */
  98. /* USER CODE END I2C2_MspDeInit 0 */
  99. /* Peripheral clock disable */
  100. __HAL_RCC_I2C2_CLK_DISABLE();
  101. /**I2C2 GPIO Configuration
  102. PB13 ------> I2C2_SCL
  103. PB14 ------> I2C2_SDA
  104. */
  105. HAL_GPIO_DeInit(GPIOB, GPIO_PIN_13);
  106. HAL_GPIO_DeInit(GPIOB, GPIO_PIN_14);
  107. /* USER CODE BEGIN I2C2_MspDeInit 1 */
  108. /* USER CODE END I2C2_MspDeInit 1 */
  109. }
  110. }
  111. /* USER CODE BEGIN 1 */
  112. /* USER CODE END 1 */