i2c.c 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196
  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 hi2c1;
  25. I2C_HandleTypeDef hi2c2;
  26. /* I2C1 init function */
  27. void MX_I2C1_Init(void)
  28. {
  29. hi2c1.Instance = I2C1;
  30. hi2c1.Init.ClockSpeed = 100000;
  31. hi2c1.Init.DutyCycle = I2C_DUTYCYCLE_2;
  32. hi2c1.Init.OwnAddress1 = 0;
  33. hi2c1.Init.AddressingMode = I2C_ADDRESSINGMODE_7BIT;
  34. hi2c1.Init.DualAddressMode = I2C_DUALADDRESS_DISABLE;
  35. hi2c1.Init.OwnAddress2 = 0;
  36. hi2c1.Init.GeneralCallMode = I2C_GENERALCALL_DISABLE;
  37. hi2c1.Init.NoStretchMode = I2C_NOSTRETCH_DISABLE;
  38. if (HAL_I2C_Init(&hi2c1) != HAL_OK)
  39. {
  40. Error_Handler();
  41. }
  42. }
  43. /* I2C2 init function */
  44. void MX_I2C2_Init(void)
  45. {
  46. hi2c2.Instance = I2C2;
  47. hi2c2.Init.ClockSpeed = 100000;
  48. hi2c2.Init.DutyCycle = I2C_DUTYCYCLE_2;
  49. hi2c2.Init.OwnAddress1 = 0;
  50. hi2c2.Init.AddressingMode = I2C_ADDRESSINGMODE_7BIT;
  51. hi2c2.Init.DualAddressMode = I2C_DUALADDRESS_DISABLE;
  52. hi2c2.Init.OwnAddress2 = 0;
  53. hi2c2.Init.GeneralCallMode = I2C_GENERALCALL_DISABLE;
  54. hi2c2.Init.NoStretchMode = I2C_NOSTRETCH_DISABLE;
  55. if (HAL_I2C_Init(&hi2c2) != 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==I2C1)
  64. {
  65. /* USER CODE BEGIN I2C1_MspInit 0 */
  66. /* USER CODE END I2C1_MspInit 0 */
  67. __HAL_RCC_GPIOB_CLK_ENABLE();
  68. /**I2C1 GPIO Configuration
  69. PB6 ------> I2C1_SCL
  70. PB7 ------> I2C1_SDA
  71. */
  72. GPIO_InitStruct.Pin = GPIO_PIN_6|GPIO_PIN_7;
  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_I2C1;
  77. HAL_GPIO_Init(GPIOB, &GPIO_InitStruct);
  78. /* I2C1 clock enable */
  79. __HAL_RCC_I2C1_CLK_ENABLE();
  80. /* USER CODE BEGIN I2C1_MspInit 1 */
  81. /* USER CODE END I2C1_MspInit 1 */
  82. }
  83. else if(i2cHandle->Instance==I2C2)
  84. {
  85. /* USER CODE BEGIN I2C2_MspInit 0 */
  86. /* USER CODE END I2C2_MspInit 0 */
  87. __HAL_RCC_GPIOB_CLK_ENABLE();
  88. /**I2C2 GPIO Configuration
  89. PB10 ------> I2C2_SCL
  90. PB3 ------> I2C2_SDA
  91. */
  92. GPIO_InitStruct.Pin = GPIO_PIN_10;
  93. GPIO_InitStruct.Mode = GPIO_MODE_AF_OD;
  94. GPIO_InitStruct.Pull = GPIO_PULLUP;
  95. GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_VERY_HIGH;
  96. GPIO_InitStruct.Alternate = GPIO_AF4_I2C2;
  97. HAL_GPIO_Init(GPIOB, &GPIO_InitStruct);
  98. GPIO_InitStruct.Pin = GPIO_PIN_3;
  99. GPIO_InitStruct.Mode = GPIO_MODE_AF_OD;
  100. GPIO_InitStruct.Pull = GPIO_PULLUP;
  101. GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_VERY_HIGH;
  102. GPIO_InitStruct.Alternate = GPIO_AF9_I2C2;
  103. HAL_GPIO_Init(GPIOB, &GPIO_InitStruct);
  104. /* I2C2 clock enable */
  105. __HAL_RCC_I2C2_CLK_ENABLE();
  106. /* USER CODE BEGIN I2C2_MspInit 1 */
  107. /* USER CODE END I2C2_MspInit 1 */
  108. }
  109. }
  110. void HAL_I2C_MspDeInit(I2C_HandleTypeDef* i2cHandle)
  111. {
  112. if(i2cHandle->Instance==I2C1)
  113. {
  114. /* USER CODE BEGIN I2C1_MspDeInit 0 */
  115. /* USER CODE END I2C1_MspDeInit 0 */
  116. /* Peripheral clock disable */
  117. __HAL_RCC_I2C1_CLK_DISABLE();
  118. /**I2C1 GPIO Configuration
  119. PB6 ------> I2C1_SCL
  120. PB7 ------> I2C1_SDA
  121. */
  122. HAL_GPIO_DeInit(GPIOB, GPIO_PIN_6|GPIO_PIN_7);
  123. /* USER CODE BEGIN I2C1_MspDeInit 1 */
  124. /* USER CODE END I2C1_MspDeInit 1 */
  125. }
  126. else if(i2cHandle->Instance==I2C2)
  127. {
  128. /* USER CODE BEGIN I2C2_MspDeInit 0 */
  129. /* USER CODE END I2C2_MspDeInit 0 */
  130. /* Peripheral clock disable */
  131. __HAL_RCC_I2C2_CLK_DISABLE();
  132. /**I2C2 GPIO Configuration
  133. PB10 ------> I2C2_SCL
  134. PB3 ------> I2C2_SDA
  135. */
  136. HAL_GPIO_DeInit(GPIOB, GPIO_PIN_10|GPIO_PIN_3);
  137. /* USER CODE BEGIN I2C2_MspDeInit 1 */
  138. /* USER CODE END I2C2_MspDeInit 1 */
  139. }
  140. }
  141. /* USER CODE BEGIN 1 */
  142. //Fonction de lecture ecriture
  143. void readRTC(uint8_t add, uint8_t *tab, int len){
  144. uint8_t ad[1];
  145. ad[0]=add;
  146. HAL_StatusTypeDef res;
  147. res = HAL_I2C_Master_Transmit(&hi2c2,RTC_ADD,ad,1,1000);
  148. res = HAL_I2C_Master_Receive(&hi2c2,RTC_ADD,tab,len,1000);
  149. }
  150. void writeRTC(uint8_t add, uint8_t *tab, int len){
  151. uint8_t ad[len+1];
  152. ad[0]=add;
  153. for (int i = 0; i< len;i++){
  154. ad[i+1]=tab[i];
  155. }
  156. HAL_StatusTypeDef res;
  157. res = HAL_I2C_Master_Transmit(&hi2c2,RTC_ADD,ad,len+1,1000);
  158. }
  159. /* USER CODE END 1 */
  160. /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/