i2c.c 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. /**
  2. ******************************************************************************
  3. * File Name : I2C.c
  4. * Description : 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 hi2c2;
  24. /* I2C2 init function */
  25. void MX_I2C2_Init(void)
  26. {
  27. hi2c2.Instance = I2C2;
  28. hi2c2.Init.ClockSpeed = 100000;
  29. hi2c2.Init.DutyCycle = I2C_DUTYCYCLE_2;
  30. hi2c2.Init.OwnAddress1 = 0;
  31. hi2c2.Init.AddressingMode = I2C_ADDRESSINGMODE_7BIT;
  32. hi2c2.Init.DualAddressMode = I2C_DUALADDRESS_DISABLE;
  33. hi2c2.Init.OwnAddress2 = 0;
  34. hi2c2.Init.GeneralCallMode = I2C_GENERALCALL_DISABLE;
  35. hi2c2.Init.NoStretchMode = I2C_NOSTRETCH_DISABLE;
  36. if (HAL_I2C_Init(&hi2c2) != HAL_OK)
  37. {
  38. Error_Handler();
  39. }
  40. }
  41. void HAL_I2C_MspInit(I2C_HandleTypeDef* i2cHandle)
  42. {
  43. GPIO_InitTypeDef GPIO_InitStruct = {0};
  44. if(i2cHandle->Instance==I2C2)
  45. {
  46. /* USER CODE BEGIN I2C2_MspInit 0 */
  47. /* USER CODE END I2C2_MspInit 0 */
  48. __HAL_RCC_GPIOF_CLK_ENABLE();
  49. /**I2C2 GPIO Configuration
  50. PF0 ------> I2C2_SDA
  51. PF1 ------> I2C2_SCL
  52. */
  53. GPIO_InitStruct.Pin = GPIO_PIN_0|GPIO_PIN_1;
  54. GPIO_InitStruct.Mode = GPIO_MODE_AF_OD;
  55. GPIO_InitStruct.Pull = GPIO_PULLUP;
  56. GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_VERY_HIGH;
  57. GPIO_InitStruct.Alternate = GPIO_AF4_I2C2;
  58. HAL_GPIO_Init(GPIOF, &GPIO_InitStruct);
  59. /* I2C2 clock enable */
  60. __HAL_RCC_I2C2_CLK_ENABLE();
  61. /* USER CODE BEGIN I2C2_MspInit 1 */
  62. /* USER CODE END I2C2_MspInit 1 */
  63. }
  64. }
  65. void HAL_I2C_MspDeInit(I2C_HandleTypeDef* i2cHandle)
  66. {
  67. if(i2cHandle->Instance==I2C2)
  68. {
  69. /* USER CODE BEGIN I2C2_MspDeInit 0 */
  70. /* USER CODE END I2C2_MspDeInit 0 */
  71. /* Peripheral clock disable */
  72. __HAL_RCC_I2C2_CLK_DISABLE();
  73. /**I2C2 GPIO Configuration
  74. PF0 ------> I2C2_SDA
  75. PF1 ------> I2C2_SCL
  76. */
  77. HAL_GPIO_DeInit(GPIOF, GPIO_PIN_0|GPIO_PIN_1);
  78. /* USER CODE BEGIN I2C2_MspDeInit 1 */
  79. /* USER CODE END I2C2_MspDeInit 1 */
  80. }
  81. }
  82. /* USER CODE BEGIN 1 */
  83. /* USER CODE END 1 */
  84. /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/