i2c.c 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354
  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) 2024 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 hi2c1;
  25. I2C_HandleTypeDef hi2c2;
  26. I2C_HandleTypeDef hi2c3;
  27. /* I2C1 init function */
  28. void MX_I2C1_Init(void)
  29. {
  30. /* USER CODE BEGIN I2C1_Init 0 */
  31. /* USER CODE END I2C1_Init 0 */
  32. /* USER CODE BEGIN I2C1_Init 1 */
  33. /* USER CODE END I2C1_Init 1 */
  34. hi2c1.Instance = I2C1;
  35. hi2c1.Init.Timing = 0x00401242;
  36. hi2c1.Init.OwnAddress1 = 0;
  37. hi2c1.Init.AddressingMode = I2C_ADDRESSINGMODE_7BIT;
  38. hi2c1.Init.DualAddressMode = I2C_DUALADDRESS_DISABLE;
  39. hi2c1.Init.OwnAddress2 = 0;
  40. hi2c1.Init.OwnAddress2Masks = I2C_OA2_NOMASK;
  41. hi2c1.Init.GeneralCallMode = I2C_GENERALCALL_DISABLE;
  42. hi2c1.Init.NoStretchMode = I2C_NOSTRETCH_DISABLE;
  43. if (HAL_I2C_Init(&hi2c1) != HAL_OK)
  44. {
  45. Error_Handler();
  46. }
  47. /** Configure Analogue filter
  48. */
  49. if (HAL_I2CEx_ConfigAnalogFilter(&hi2c1, I2C_ANALOGFILTER_ENABLE) != HAL_OK)
  50. {
  51. Error_Handler();
  52. }
  53. /** Configure Digital filter
  54. */
  55. if (HAL_I2CEx_ConfigDigitalFilter(&hi2c1, 0) != HAL_OK)
  56. {
  57. Error_Handler();
  58. }
  59. /** I2C Enable Fast Mode Plus
  60. */
  61. HAL_I2CEx_EnableFastModePlus(I2C_FASTMODEPLUS_I2C1);
  62. /* USER CODE BEGIN I2C1_Init 2 */
  63. /* USER CODE END I2C1_Init 2 */
  64. }
  65. /* I2C2 init function */
  66. void MX_I2C2_Init(void)
  67. {
  68. /* USER CODE BEGIN I2C2_Init 0 */
  69. /* USER CODE END I2C2_Init 0 */
  70. /* USER CODE BEGIN I2C2_Init 1 */
  71. /* USER CODE END I2C2_Init 1 */
  72. hi2c2.Instance = I2C2;
  73. hi2c2.Init.Timing = 0x00401242;
  74. hi2c2.Init.OwnAddress1 = 0;
  75. hi2c2.Init.AddressingMode = I2C_ADDRESSINGMODE_7BIT;
  76. hi2c2.Init.DualAddressMode = I2C_DUALADDRESS_DISABLE;
  77. hi2c2.Init.OwnAddress2 = 0;
  78. hi2c2.Init.OwnAddress2Masks = I2C_OA2_NOMASK;
  79. hi2c2.Init.GeneralCallMode = I2C_GENERALCALL_DISABLE;
  80. hi2c2.Init.NoStretchMode = I2C_NOSTRETCH_DISABLE;
  81. if (HAL_I2C_Init(&hi2c2) != HAL_OK)
  82. {
  83. Error_Handler();
  84. }
  85. /** Configure Analogue filter
  86. */
  87. if (HAL_I2CEx_ConfigAnalogFilter(&hi2c2, I2C_ANALOGFILTER_ENABLE) != HAL_OK)
  88. {
  89. Error_Handler();
  90. }
  91. /** Configure Digital filter
  92. */
  93. if (HAL_I2CEx_ConfigDigitalFilter(&hi2c2, 0) != HAL_OK)
  94. {
  95. Error_Handler();
  96. }
  97. /** I2C Enable Fast Mode Plus
  98. */
  99. HAL_I2CEx_EnableFastModePlus(I2C_FASTMODEPLUS_I2C2);
  100. /* USER CODE BEGIN I2C2_Init 2 */
  101. /* USER CODE END I2C2_Init 2 */
  102. }
  103. /* I2C3 init function */
  104. void MX_I2C3_Init(void)
  105. {
  106. /* USER CODE BEGIN I2C3_Init 0 */
  107. /* USER CODE END I2C3_Init 0 */
  108. /* USER CODE BEGIN I2C3_Init 1 */
  109. /* USER CODE END I2C3_Init 1 */
  110. hi2c3.Instance = I2C3;
  111. hi2c3.Init.Timing = 0x00401242;
  112. hi2c3.Init.OwnAddress1 = 0;
  113. hi2c3.Init.AddressingMode = I2C_ADDRESSINGMODE_7BIT;
  114. hi2c3.Init.DualAddressMode = I2C_DUALADDRESS_DISABLE;
  115. hi2c3.Init.OwnAddress2 = 0;
  116. hi2c3.Init.OwnAddress2Masks = I2C_OA2_NOMASK;
  117. hi2c3.Init.GeneralCallMode = I2C_GENERALCALL_DISABLE;
  118. hi2c3.Init.NoStretchMode = I2C_NOSTRETCH_DISABLE;
  119. if (HAL_I2C_Init(&hi2c3) != HAL_OK)
  120. {
  121. Error_Handler();
  122. }
  123. /** Configure Analogue filter
  124. */
  125. if (HAL_I2CEx_ConfigAnalogFilter(&hi2c3, I2C_ANALOGFILTER_ENABLE) != HAL_OK)
  126. {
  127. Error_Handler();
  128. }
  129. /** Configure Digital filter
  130. */
  131. if (HAL_I2CEx_ConfigDigitalFilter(&hi2c3, 0) != HAL_OK)
  132. {
  133. Error_Handler();
  134. }
  135. /** I2C Enable Fast Mode Plus
  136. */
  137. HAL_I2CEx_EnableFastModePlus(I2C_FASTMODEPLUS_I2C3);
  138. /* USER CODE BEGIN I2C3_Init 2 */
  139. /* USER CODE END I2C3_Init 2 */
  140. }
  141. void HAL_I2C_MspInit(I2C_HandleTypeDef* i2cHandle)
  142. {
  143. GPIO_InitTypeDef GPIO_InitStruct = {0};
  144. RCC_PeriphCLKInitTypeDef PeriphClkInitStruct = {0};
  145. if(i2cHandle->Instance==I2C1)
  146. {
  147. /* USER CODE BEGIN I2C1_MspInit 0 */
  148. /* USER CODE END I2C1_MspInit 0 */
  149. /** Initializes the peripherals clock
  150. */
  151. PeriphClkInitStruct.PeriphClockSelection = RCC_PERIPHCLK_I2C1;
  152. PeriphClkInitStruct.I2c123ClockSelection = RCC_I2C123CLKSOURCE_D2PCLK1;
  153. if (HAL_RCCEx_PeriphCLKConfig(&PeriphClkInitStruct) != HAL_OK)
  154. {
  155. Error_Handler();
  156. }
  157. __HAL_RCC_GPIOB_CLK_ENABLE();
  158. /**I2C1 GPIO Configuration
  159. PB7 ------> I2C1_SDA
  160. PB8 ------> I2C1_SCL
  161. */
  162. GPIO_InitStruct.Pin = GPIO_PIN_7|GPIO_PIN_8;
  163. GPIO_InitStruct.Mode = GPIO_MODE_AF_OD;
  164. GPIO_InitStruct.Pull = GPIO_NOPULL;
  165. GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW;
  166. GPIO_InitStruct.Alternate = GPIO_AF4_I2C1;
  167. HAL_GPIO_Init(GPIOB, &GPIO_InitStruct);
  168. /* I2C1 clock enable */
  169. __HAL_RCC_I2C1_CLK_ENABLE();
  170. /* USER CODE BEGIN I2C1_MspInit 1 */
  171. /* USER CODE END I2C1_MspInit 1 */
  172. }
  173. else if(i2cHandle->Instance==I2C2)
  174. {
  175. /* USER CODE BEGIN I2C2_MspInit 0 */
  176. /* USER CODE END I2C2_MspInit 0 */
  177. /** Initializes the peripherals clock
  178. */
  179. PeriphClkInitStruct.PeriphClockSelection = RCC_PERIPHCLK_I2C2;
  180. PeriphClkInitStruct.I2c123ClockSelection = RCC_I2C123CLKSOURCE_D2PCLK1;
  181. if (HAL_RCCEx_PeriphCLKConfig(&PeriphClkInitStruct) != HAL_OK)
  182. {
  183. Error_Handler();
  184. }
  185. __HAL_RCC_GPIOB_CLK_ENABLE();
  186. /**I2C2 GPIO Configuration
  187. PB10 ------> I2C2_SCL
  188. PB11 ------> I2C2_SDA
  189. */
  190. GPIO_InitStruct.Pin = GPIO_PIN_10|GPIO_PIN_11;
  191. GPIO_InitStruct.Mode = GPIO_MODE_AF_OD;
  192. GPIO_InitStruct.Pull = GPIO_NOPULL;
  193. GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW;
  194. GPIO_InitStruct.Alternate = GPIO_AF4_I2C2;
  195. HAL_GPIO_Init(GPIOB, &GPIO_InitStruct);
  196. /* I2C2 clock enable */
  197. __HAL_RCC_I2C2_CLK_ENABLE();
  198. /* USER CODE BEGIN I2C2_MspInit 1 */
  199. /* USER CODE END I2C2_MspInit 1 */
  200. }
  201. else if(i2cHandle->Instance==I2C3)
  202. {
  203. /* USER CODE BEGIN I2C3_MspInit 0 */
  204. /* USER CODE END I2C3_MspInit 0 */
  205. /** Initializes the peripherals clock
  206. */
  207. PeriphClkInitStruct.PeriphClockSelection = RCC_PERIPHCLK_I2C3;
  208. PeriphClkInitStruct.I2c123ClockSelection = RCC_I2C123CLKSOURCE_D2PCLK1;
  209. if (HAL_RCCEx_PeriphCLKConfig(&PeriphClkInitStruct) != HAL_OK)
  210. {
  211. Error_Handler();
  212. }
  213. __HAL_RCC_GPIOC_CLK_ENABLE();
  214. __HAL_RCC_GPIOA_CLK_ENABLE();
  215. /**I2C3 GPIO Configuration
  216. PC9 ------> I2C3_SDA
  217. PA8 ------> I2C3_SCL
  218. */
  219. GPIO_InitStruct.Pin = GPIO_PIN_9;
  220. GPIO_InitStruct.Mode = GPIO_MODE_AF_OD;
  221. GPIO_InitStruct.Pull = GPIO_NOPULL;
  222. GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW;
  223. GPIO_InitStruct.Alternate = GPIO_AF4_I2C3;
  224. HAL_GPIO_Init(GPIOC, &GPIO_InitStruct);
  225. GPIO_InitStruct.Pin = GPIO_PIN_8;
  226. GPIO_InitStruct.Mode = GPIO_MODE_AF_OD;
  227. GPIO_InitStruct.Pull = GPIO_NOPULL;
  228. GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW;
  229. GPIO_InitStruct.Alternate = GPIO_AF4_I2C3;
  230. HAL_GPIO_Init(GPIOA, &GPIO_InitStruct);
  231. /* I2C3 clock enable */
  232. __HAL_RCC_I2C3_CLK_ENABLE();
  233. /* USER CODE BEGIN I2C3_MspInit 1 */
  234. /* USER CODE END I2C3_MspInit 1 */
  235. }
  236. }
  237. void HAL_I2C_MspDeInit(I2C_HandleTypeDef* i2cHandle)
  238. {
  239. if(i2cHandle->Instance==I2C1)
  240. {
  241. /* USER CODE BEGIN I2C1_MspDeInit 0 */
  242. /* USER CODE END I2C1_MspDeInit 0 */
  243. /* Peripheral clock disable */
  244. __HAL_RCC_I2C1_CLK_DISABLE();
  245. /**I2C1 GPIO Configuration
  246. PB7 ------> I2C1_SDA
  247. PB8 ------> I2C1_SCL
  248. */
  249. HAL_GPIO_DeInit(GPIOB, GPIO_PIN_7);
  250. HAL_GPIO_DeInit(GPIOB, GPIO_PIN_8);
  251. /* USER CODE BEGIN I2C1_MspDeInit 1 */
  252. /* USER CODE END I2C1_MspDeInit 1 */
  253. }
  254. else if(i2cHandle->Instance==I2C2)
  255. {
  256. /* USER CODE BEGIN I2C2_MspDeInit 0 */
  257. /* USER CODE END I2C2_MspDeInit 0 */
  258. /* Peripheral clock disable */
  259. __HAL_RCC_I2C2_CLK_DISABLE();
  260. /**I2C2 GPIO Configuration
  261. PB10 ------> I2C2_SCL
  262. PB11 ------> I2C2_SDA
  263. */
  264. HAL_GPIO_DeInit(GPIOB, GPIO_PIN_10);
  265. HAL_GPIO_DeInit(GPIOB, GPIO_PIN_11);
  266. /* USER CODE BEGIN I2C2_MspDeInit 1 */
  267. /* USER CODE END I2C2_MspDeInit 1 */
  268. }
  269. else if(i2cHandle->Instance==I2C3)
  270. {
  271. /* USER CODE BEGIN I2C3_MspDeInit 0 */
  272. /* USER CODE END I2C3_MspDeInit 0 */
  273. /* Peripheral clock disable */
  274. __HAL_RCC_I2C3_CLK_DISABLE();
  275. /**I2C3 GPIO Configuration
  276. PC9 ------> I2C3_SDA
  277. PA8 ------> I2C3_SCL
  278. */
  279. HAL_GPIO_DeInit(GPIOC, GPIO_PIN_9);
  280. HAL_GPIO_DeInit(GPIOA, GPIO_PIN_8);
  281. /* USER CODE BEGIN I2C3_MspDeInit 1 */
  282. /* USER CODE END I2C3_MspDeInit 1 */
  283. }
  284. }
  285. /* USER CODE BEGIN 1 */
  286. /* USER CODE END 1 */