i2c.c 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202
  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 BSD 3-Clause license,
  13. * the "License"; You may not use this file except in compliance with the
  14. * License. You may obtain a copy of the License at:
  15. * opensource.org/licenses/BSD-3-Clause
  16. *
  17. ******************************************************************************
  18. */
  19. /* Includes ------------------------------------------------------------------*/
  20. #include "i2c.h"
  21. /* USER CODE BEGIN 0 */
  22. #include "RTC.h"
  23. /* USER CODE END 0 */
  24. I2C_HandleTypeDef hi2c2;
  25. I2C_HandleTypeDef hi2c3;
  26. /* I2C2 init function */
  27. void MX_I2C2_Init(void)
  28. {
  29. hi2c2.Instance = I2C2;
  30. hi2c2.Init.ClockSpeed = 100000;
  31. hi2c2.Init.DutyCycle = I2C_DUTYCYCLE_2;
  32. hi2c2.Init.OwnAddress1 = 0;
  33. hi2c2.Init.AddressingMode = I2C_ADDRESSINGMODE_7BIT;
  34. hi2c2.Init.DualAddressMode = I2C_DUALADDRESS_DISABLE;
  35. hi2c2.Init.OwnAddress2 = 0;
  36. hi2c2.Init.GeneralCallMode = I2C_GENERALCALL_DISABLE;
  37. hi2c2.Init.NoStretchMode = I2C_NOSTRETCH_DISABLE;
  38. if (HAL_I2C_Init(&hi2c2) != HAL_OK)
  39. {
  40. Error_Handler();
  41. }
  42. }
  43. /* I2C3 init function */
  44. void MX_I2C3_Init(void)
  45. {
  46. hi2c3.Instance = I2C3;
  47. hi2c3.Init.ClockSpeed = 50000;
  48. hi2c3.Init.DutyCycle = I2C_DUTYCYCLE_2;
  49. hi2c3.Init.OwnAddress1 = 0;
  50. hi2c3.Init.AddressingMode = I2C_ADDRESSINGMODE_7BIT;
  51. hi2c3.Init.DualAddressMode = I2C_DUALADDRESS_DISABLE;
  52. hi2c3.Init.OwnAddress2 = 0;
  53. hi2c3.Init.GeneralCallMode = I2C_GENERALCALL_DISABLE;
  54. hi2c3.Init.NoStretchMode = I2C_NOSTRETCH_DISABLE;
  55. if (HAL_I2C_Init(&hi2c3) != HAL_OK)
  56. {
  57. Error_Handler();
  58. }
  59. }
  60. void HAL_I2C_MspInit(I2C_HandleTypeDef* i2cHandle)
  61. {
  62. GPIO_InitTypeDef GPIO_InitStruct = {0};
  63. if(i2cHandle->Instance==I2C2)
  64. {
  65. /* USER CODE BEGIN I2C2_MspInit 0 */
  66. /* USER CODE END I2C2_MspInit 0 */
  67. __HAL_RCC_GPIOB_CLK_ENABLE();
  68. /**I2C2 GPIO Configuration
  69. PB10 ------> I2C2_SCL
  70. PB3 ------> I2C2_SDA
  71. */
  72. GPIO_InitStruct.Pin = GPIO_PIN_10;
  73. GPIO_InitStruct.Mode = GPIO_MODE_AF_OD;
  74. GPIO_InitStruct.Pull = GPIO_PULLUP;
  75. GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_VERY_HIGH;
  76. GPIO_InitStruct.Alternate = GPIO_AF4_I2C2;
  77. HAL_GPIO_Init(GPIOB, &GPIO_InitStruct);
  78. GPIO_InitStruct.Pin = GPIO_PIN_3;
  79. GPIO_InitStruct.Mode = GPIO_MODE_AF_OD;
  80. GPIO_InitStruct.Pull = GPIO_PULLUP;
  81. GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_VERY_HIGH;
  82. GPIO_InitStruct.Alternate = GPIO_AF9_I2C2;
  83. HAL_GPIO_Init(GPIOB, &GPIO_InitStruct);
  84. /* I2C2 clock enable */
  85. __HAL_RCC_I2C2_CLK_ENABLE();
  86. /* USER CODE BEGIN I2C2_MspInit 1 */
  87. /* USER CODE END I2C2_MspInit 1 */
  88. }
  89. else if(i2cHandle->Instance==I2C3)
  90. {
  91. /* USER CODE BEGIN I2C3_MspInit 0 */
  92. /* USER CODE END I2C3_MspInit 0 */
  93. __HAL_RCC_GPIOA_CLK_ENABLE();
  94. __HAL_RCC_GPIOB_CLK_ENABLE();
  95. /**I2C3 GPIO Configuration
  96. PA8 ------> I2C3_SCL
  97. PB4 ------> I2C3_SDA
  98. */
  99. GPIO_InitStruct.Pin = GPIO_PIN_8;
  100. GPIO_InitStruct.Mode = GPIO_MODE_AF_OD;
  101. GPIO_InitStruct.Pull = GPIO_PULLUP;
  102. GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_VERY_HIGH;
  103. GPIO_InitStruct.Alternate = GPIO_AF4_I2C3;
  104. HAL_GPIO_Init(GPIOA, &GPIO_InitStruct);
  105. GPIO_InitStruct.Pin = GPIO_PIN_4;
  106. GPIO_InitStruct.Mode = GPIO_MODE_AF_OD;
  107. GPIO_InitStruct.Pull = GPIO_PULLUP;
  108. GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_VERY_HIGH;
  109. GPIO_InitStruct.Alternate = GPIO_AF9_I2C3;
  110. HAL_GPIO_Init(GPIOB, &GPIO_InitStruct);
  111. /* I2C3 clock enable */
  112. __HAL_RCC_I2C3_CLK_ENABLE();
  113. /* USER CODE BEGIN I2C3_MspInit 1 */
  114. /* USER CODE END I2C3_MspInit 1 */
  115. }
  116. }
  117. void HAL_I2C_MspDeInit(I2C_HandleTypeDef* i2cHandle)
  118. {
  119. if(i2cHandle->Instance==I2C2)
  120. {
  121. /* USER CODE BEGIN I2C2_MspDeInit 0 */
  122. /* USER CODE END I2C2_MspDeInit 0 */
  123. /* Peripheral clock disable */
  124. __HAL_RCC_I2C2_CLK_DISABLE();
  125. /**I2C2 GPIO Configuration
  126. PB10 ------> I2C2_SCL
  127. PB3 ------> I2C2_SDA
  128. */
  129. HAL_GPIO_DeInit(GPIOB, GPIO_PIN_10|GPIO_PIN_3);
  130. /* USER CODE BEGIN I2C2_MspDeInit 1 */
  131. /* USER CODE END I2C2_MspDeInit 1 */
  132. }
  133. else if(i2cHandle->Instance==I2C3)
  134. {
  135. /* USER CODE BEGIN I2C3_MspDeInit 0 */
  136. /* USER CODE END I2C3_MspDeInit 0 */
  137. /* Peripheral clock disable */
  138. __HAL_RCC_I2C3_CLK_DISABLE();
  139. /**I2C3 GPIO Configuration
  140. PA8 ------> I2C3_SCL
  141. PB4 ------> I2C3_SDA
  142. */
  143. HAL_GPIO_DeInit(GPIOA, GPIO_PIN_8);
  144. HAL_GPIO_DeInit(GPIOB, GPIO_PIN_4);
  145. /* USER CODE BEGIN I2C3_MspDeInit 1 */
  146. /* USER CODE END I2C3_MspDeInit 1 */
  147. }
  148. }
  149. /* USER CODE BEGIN 1 */
  150. void readRTC(uint8_t add, uint8_t *tab, int len){
  151. uint8_t ad[1];
  152. ad[0]=add;
  153. HAL_StatusTypeDef res;
  154. res = HAL_I2C_Master_Transmit(&hi2c3,RTC_ADD,ad,1,1000);
  155. res = HAL_I2C_Master_Receive(&hi2c3,RTC_ADD,tab,len,1000);
  156. }
  157. void writeRTC(uint8_t add, uint8_t *tab, int len){
  158. uint8_t ad[len+1];
  159. ad[0]=add;
  160. for (int i = 0; i< len;i++){
  161. ad[i+1]=tab[i];
  162. }
  163. HAL_StatusTypeDef res;
  164. res = HAL_I2C_Master_Transmit(&hi2c3,RTC_ADD,ad,len+1,1000);
  165. }
  166. /* USER CODE END 1 */
  167. /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/