i2c.c 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. /**
  2. ******************************************************************************
  3. * @file i2c.c
  4. * @brief 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 Ultimate Liberty license
  13. * SLA0044, the "License"; You may not use this file except in compliance with
  14. * the License. You may obtain a copy of the License at:
  15. * www.st.com/SLA0044
  16. *
  17. ******************************************************************************
  18. */
  19. /* Includes ------------------------------------------------------------------*/
  20. #include "i2c.h"
  21. /* USER CODE BEGIN 0 */
  22. /* USER CODE END 0 */
  23. I2C_HandleTypeDef hi2c1;
  24. /* I2C1 init function */
  25. void MX_I2C1_Init(void)
  26. {
  27. hi2c1.Instance = I2C1;
  28. hi2c1.Init.Timing = 0x10707DBC;
  29. hi2c1.Init.OwnAddress1 = 0;
  30. hi2c1.Init.AddressingMode = I2C_ADDRESSINGMODE_7BIT;
  31. hi2c1.Init.DualAddressMode = I2C_DUALADDRESS_DISABLE;
  32. hi2c1.Init.OwnAddress2 = 0;
  33. hi2c1.Init.OwnAddress2Masks = I2C_OA2_NOMASK;
  34. hi2c1.Init.GeneralCallMode = I2C_GENERALCALL_DISABLE;
  35. hi2c1.Init.NoStretchMode = I2C_NOSTRETCH_DISABLE;
  36. if (HAL_I2C_Init(&hi2c1) != HAL_OK)
  37. {
  38. Error_Handler();
  39. }
  40. /** Configure Analogue filter
  41. */
  42. if (HAL_I2CEx_ConfigAnalogFilter(&hi2c1, I2C_ANALOGFILTER_ENABLE) != HAL_OK)
  43. {
  44. Error_Handler();
  45. }
  46. /** Configure Digital filter
  47. */
  48. if (HAL_I2CEx_ConfigDigitalFilter(&hi2c1, 0) != HAL_OK)
  49. {
  50. Error_Handler();
  51. }
  52. }
  53. void HAL_I2C_MspInit(I2C_HandleTypeDef* i2cHandle)
  54. {
  55. GPIO_InitTypeDef GPIO_InitStruct = {0};
  56. if(i2cHandle->Instance==I2C1)
  57. {
  58. /* USER CODE BEGIN I2C1_MspInit 0 */
  59. /* USER CODE END I2C1_MspInit 0 */
  60. __HAL_RCC_GPIOA_CLK_ENABLE();
  61. /**I2C1 GPIO Configuration
  62. PA9 ------> I2C1_SCL
  63. PA10 ------> I2C1_SDA
  64. */
  65. GPIO_InitStruct.Pin = I2C_SCL_Pin|I2C_SDA_Pin;
  66. GPIO_InitStruct.Mode = GPIO_MODE_AF_OD;
  67. GPIO_InitStruct.Pull = GPIO_PULLUP;
  68. GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_VERY_HIGH;
  69. GPIO_InitStruct.Alternate = GPIO_AF4_I2C1;
  70. HAL_GPIO_Init(GPIOA, &GPIO_InitStruct);
  71. /* I2C1 clock enable */
  72. __HAL_RCC_I2C1_CLK_ENABLE();
  73. /* USER CODE BEGIN I2C1_MspInit 1 */
  74. /* USER CODE END I2C1_MspInit 1 */
  75. }
  76. }
  77. void HAL_I2C_MspDeInit(I2C_HandleTypeDef* i2cHandle)
  78. {
  79. if(i2cHandle->Instance==I2C1)
  80. {
  81. /* USER CODE BEGIN I2C1_MspDeInit 0 */
  82. /* USER CODE END I2C1_MspDeInit 0 */
  83. /* Peripheral clock disable */
  84. __HAL_RCC_I2C1_CLK_DISABLE();
  85. /**I2C1 GPIO Configuration
  86. PA9 ------> I2C1_SCL
  87. PA10 ------> I2C1_SDA
  88. */
  89. HAL_GPIO_DeInit(I2C_SCL_GPIO_Port, I2C_SCL_Pin);
  90. HAL_GPIO_DeInit(I2C_SDA_GPIO_Port, I2C_SDA_Pin);
  91. /* USER CODE BEGIN I2C1_MspDeInit 1 */
  92. /* USER CODE END I2C1_MspDeInit 1 */
  93. }
  94. }
  95. /* USER CODE BEGIN 1 */
  96. /* USER CODE END 1 */
  97. /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/