main.c 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945
  1. /* USER CODE BEGIN Header */
  2. /**
  3. ******************************************************************************
  4. * @file : main.c
  5. * @brief : Main program body
  6. ******************************************************************************
  7. * @attention
  8. *
  9. * <h2><center>&copy; Copyright (c) 2021 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. /* USER CODE END Header */
  20. /* Includes ------------------------------------------------------------------*/
  21. #include "main.h"
  22. #include "usb_device.h"
  23. /* Private includes ----------------------------------------------------------*/
  24. /* USER CODE BEGIN Includes */
  25. /* USER CODE END Includes */
  26. /* Private typedef -----------------------------------------------------------*/
  27. /* USER CODE BEGIN PTD */
  28. /* USER CODE END PTD */
  29. /* Private define ------------------------------------------------------------*/
  30. /* USER CODE BEGIN PD */
  31. /* USER CODE END PD */
  32. /* Private macro -------------------------------------------------------------*/
  33. /* USER CODE BEGIN PM */
  34. /* USER CODE END PM */
  35. /* Private variables ---------------------------------------------------------*/
  36. ADC_HandleTypeDef hadc1;
  37. ADC_HandleTypeDef hadc2;
  38. ADC_HandleTypeDef hadc3;
  39. COMP_HandleTypeDef hcomp1;
  40. COMP_HandleTypeDef hcomp2;
  41. DAC_HandleTypeDef hdac1;
  42. DAC_HandleTypeDef hdac3;
  43. SMBUS_HandleTypeDef hsmbus1;
  44. SPI_HandleTypeDef hspi1;
  45. SPI_HandleTypeDef hspi2;
  46. TIM_HandleTypeDef htim1;
  47. TIM_HandleTypeDef htim2;
  48. UART_HandleTypeDef huart1;
  49. /* USER CODE BEGIN PV */
  50. /* USER CODE END PV */
  51. /* Private function prototypes -----------------------------------------------*/
  52. void SystemClock_Config(void);
  53. static void MX_GPIO_Init(void);
  54. static void MX_ADC2_Init(void);
  55. static void MX_ADC3_Init(void);
  56. static void MX_COMP1_Init(void);
  57. static void MX_DAC1_Init(void);
  58. static void MX_DAC3_Init(void);
  59. static void MX_SPI1_Init(void);
  60. static void MX_SPI2_Init(void);
  61. static void MX_TIM1_Init(void);
  62. static void MX_TIM2_Init(void);
  63. static void MX_USART1_UART_Init(void);
  64. static void MX_COMP2_Init(void);
  65. static void MX_ADC1_Init(void);
  66. static void MX_I2C1_SMBUS_Init(void);
  67. /* USER CODE BEGIN PFP */
  68. /* USER CODE END PFP */
  69. /* Private user code ---------------------------------------------------------*/
  70. /* USER CODE BEGIN 0 */
  71. /* USER CODE END 0 */
  72. /**
  73. * @brief The application entry point.
  74. * @retval int
  75. */
  76. int main(void)
  77. {
  78. /* USER CODE BEGIN 1 */
  79. /* USER CODE END 1 */
  80. /* MCU Configuration--------------------------------------------------------*/
  81. /* Reset of all peripherals, Initializes the Flash interface and the Systick. */
  82. HAL_Init();
  83. /* USER CODE BEGIN Init */
  84. /* USER CODE END Init */
  85. /* Configure the system clock */
  86. SystemClock_Config();
  87. /* USER CODE BEGIN SysInit */
  88. /* USER CODE END SysInit */
  89. /* Initialize all configured peripherals */
  90. MX_GPIO_Init();
  91. MX_ADC2_Init();
  92. MX_ADC3_Init();
  93. MX_COMP1_Init();
  94. MX_DAC1_Init();
  95. MX_DAC3_Init();
  96. MX_SPI1_Init();
  97. MX_SPI2_Init();
  98. MX_TIM1_Init();
  99. MX_TIM2_Init();
  100. MX_USART1_UART_Init();
  101. MX_USB_Device_Init();
  102. MX_COMP2_Init();
  103. MX_ADC1_Init();
  104. MX_I2C1_SMBUS_Init();
  105. /* USER CODE BEGIN 2 */
  106. /* USER CODE END 2 */
  107. /* Infinite loop */
  108. /* USER CODE BEGIN WHILE */
  109. while (1)
  110. {
  111. /* USER CODE END WHILE */
  112. /* USER CODE BEGIN 3 */
  113. }
  114. /* USER CODE END 3 */
  115. }
  116. /**
  117. * @brief System Clock Configuration
  118. * @retval None
  119. */
  120. void SystemClock_Config(void)
  121. {
  122. RCC_OscInitTypeDef RCC_OscInitStruct = {0};
  123. RCC_ClkInitTypeDef RCC_ClkInitStruct = {0};
  124. RCC_PeriphCLKInitTypeDef PeriphClkInit = {0};
  125. /** Configure the main internal regulator output voltage
  126. */
  127. HAL_PWREx_ControlVoltageScaling(PWR_REGULATOR_VOLTAGE_SCALE1_BOOST);
  128. /** Initializes the RCC Oscillators according to the specified parameters
  129. * in the RCC_OscInitTypeDef structure.
  130. */
  131. RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_HSI48|RCC_OSCILLATORTYPE_HSE;
  132. RCC_OscInitStruct.HSEState = RCC_HSE_ON;
  133. RCC_OscInitStruct.HSI48State = RCC_HSI48_ON;
  134. RCC_OscInitStruct.PLL.PLLState = RCC_PLL_ON;
  135. RCC_OscInitStruct.PLL.PLLSource = RCC_PLLSOURCE_HSE;
  136. RCC_OscInitStruct.PLL.PLLM = RCC_PLLM_DIV2;
  137. RCC_OscInitStruct.PLL.PLLN = 80;
  138. RCC_OscInitStruct.PLL.PLLP = RCC_PLLP_DIV2;
  139. RCC_OscInitStruct.PLL.PLLQ = RCC_PLLQ_DIV2;
  140. RCC_OscInitStruct.PLL.PLLR = RCC_PLLR_DIV2;
  141. if (HAL_RCC_OscConfig(&RCC_OscInitStruct) != HAL_OK)
  142. {
  143. Error_Handler();
  144. }
  145. /** Initializes the CPU, AHB and APB buses clocks
  146. */
  147. RCC_ClkInitStruct.ClockType = RCC_CLOCKTYPE_HCLK|RCC_CLOCKTYPE_SYSCLK
  148. |RCC_CLOCKTYPE_PCLK1|RCC_CLOCKTYPE_PCLK2;
  149. RCC_ClkInitStruct.SYSCLKSource = RCC_SYSCLKSOURCE_PLLCLK;
  150. RCC_ClkInitStruct.AHBCLKDivider = RCC_SYSCLK_DIV1;
  151. RCC_ClkInitStruct.APB1CLKDivider = RCC_HCLK_DIV1;
  152. RCC_ClkInitStruct.APB2CLKDivider = RCC_HCLK_DIV1;
  153. if (HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_4) != HAL_OK)
  154. {
  155. Error_Handler();
  156. }
  157. /** Initializes the peripherals clocks
  158. */
  159. PeriphClkInit.PeriphClockSelection = RCC_PERIPHCLK_USART1|RCC_PERIPHCLK_I2C1
  160. |RCC_PERIPHCLK_USB|RCC_PERIPHCLK_ADC12
  161. |RCC_PERIPHCLK_ADC345;
  162. PeriphClkInit.Usart1ClockSelection = RCC_USART1CLKSOURCE_PCLK2;
  163. PeriphClkInit.I2c1ClockSelection = RCC_I2C1CLKSOURCE_PCLK1;
  164. PeriphClkInit.UsbClockSelection = RCC_USBCLKSOURCE_HSI48;
  165. PeriphClkInit.Adc12ClockSelection = RCC_ADC12CLKSOURCE_SYSCLK;
  166. PeriphClkInit.Adc345ClockSelection = RCC_ADC345CLKSOURCE_SYSCLK;
  167. if (HAL_RCCEx_PeriphCLKConfig(&PeriphClkInit) != HAL_OK)
  168. {
  169. Error_Handler();
  170. }
  171. }
  172. /**
  173. * @brief ADC1 Initialization Function
  174. * @param None
  175. * @retval None
  176. */
  177. static void MX_ADC1_Init(void)
  178. {
  179. /* USER CODE BEGIN ADC1_Init 0 */
  180. /* USER CODE END ADC1_Init 0 */
  181. ADC_MultiModeTypeDef multimode = {0};
  182. ADC_ChannelConfTypeDef sConfig = {0};
  183. /* USER CODE BEGIN ADC1_Init 1 */
  184. /* USER CODE END ADC1_Init 1 */
  185. /** Common config
  186. */
  187. hadc1.Instance = ADC1;
  188. hadc1.Init.ClockPrescaler = ADC_CLOCK_SYNC_PCLK_DIV4;
  189. hadc1.Init.Resolution = ADC_RESOLUTION_12B;
  190. hadc1.Init.DataAlign = ADC_DATAALIGN_RIGHT;
  191. hadc1.Init.GainCompensation = 0;
  192. hadc1.Init.ScanConvMode = ADC_SCAN_DISABLE;
  193. hadc1.Init.EOCSelection = ADC_EOC_SINGLE_CONV;
  194. hadc1.Init.LowPowerAutoWait = DISABLE;
  195. hadc1.Init.ContinuousConvMode = DISABLE;
  196. hadc1.Init.NbrOfConversion = 1;
  197. hadc1.Init.DiscontinuousConvMode = DISABLE;
  198. hadc1.Init.ExternalTrigConv = ADC_SOFTWARE_START;
  199. hadc1.Init.ExternalTrigConvEdge = ADC_EXTERNALTRIGCONVEDGE_NONE;
  200. hadc1.Init.DMAContinuousRequests = DISABLE;
  201. hadc1.Init.Overrun = ADC_OVR_DATA_PRESERVED;
  202. hadc1.Init.OversamplingMode = DISABLE;
  203. if (HAL_ADC_Init(&hadc1) != HAL_OK)
  204. {
  205. Error_Handler();
  206. }
  207. /** Configure the ADC multi-mode
  208. */
  209. multimode.Mode = ADC_MODE_INDEPENDENT;
  210. if (HAL_ADCEx_MultiModeConfigChannel(&hadc1, &multimode) != HAL_OK)
  211. {
  212. Error_Handler();
  213. }
  214. /** Configure Regular Channel
  215. */
  216. sConfig.Channel = ADC_CHANNEL_VBAT;
  217. sConfig.Rank = ADC_REGULAR_RANK_1;
  218. sConfig.SamplingTime = ADC_SAMPLETIME_2CYCLES_5;
  219. sConfig.SingleDiff = ADC_SINGLE_ENDED;
  220. sConfig.OffsetNumber = ADC_OFFSET_NONE;
  221. sConfig.Offset = 0;
  222. if (HAL_ADC_ConfigChannel(&hadc1, &sConfig) != HAL_OK)
  223. {
  224. Error_Handler();
  225. }
  226. /* USER CODE BEGIN ADC1_Init 2 */
  227. /* USER CODE END ADC1_Init 2 */
  228. }
  229. /**
  230. * @brief ADC2 Initialization Function
  231. * @param None
  232. * @retval None
  233. */
  234. static void MX_ADC2_Init(void)
  235. {
  236. /* USER CODE BEGIN ADC2_Init 0 */
  237. /* USER CODE END ADC2_Init 0 */
  238. ADC_ChannelConfTypeDef sConfig = {0};
  239. /* USER CODE BEGIN ADC2_Init 1 */
  240. /* USER CODE END ADC2_Init 1 */
  241. /** Common config
  242. */
  243. hadc2.Instance = ADC2;
  244. hadc2.Init.ClockPrescaler = ADC_CLOCK_SYNC_PCLK_DIV4;
  245. hadc2.Init.Resolution = ADC_RESOLUTION_12B;
  246. hadc2.Init.DataAlign = ADC_DATAALIGN_RIGHT;
  247. hadc2.Init.GainCompensation = 0;
  248. hadc2.Init.ScanConvMode = ADC_SCAN_DISABLE;
  249. hadc2.Init.EOCSelection = ADC_EOC_SINGLE_CONV;
  250. hadc2.Init.LowPowerAutoWait = DISABLE;
  251. hadc2.Init.ContinuousConvMode = DISABLE;
  252. hadc2.Init.NbrOfConversion = 1;
  253. hadc2.Init.DiscontinuousConvMode = DISABLE;
  254. hadc2.Init.ExternalTrigConv = ADC_SOFTWARE_START;
  255. hadc2.Init.ExternalTrigConvEdge = ADC_EXTERNALTRIGCONVEDGE_NONE;
  256. hadc2.Init.DMAContinuousRequests = DISABLE;
  257. hadc2.Init.Overrun = ADC_OVR_DATA_PRESERVED;
  258. hadc2.Init.OversamplingMode = DISABLE;
  259. if (HAL_ADC_Init(&hadc2) != HAL_OK)
  260. {
  261. Error_Handler();
  262. }
  263. /** Configure Regular Channel
  264. */
  265. sConfig.Channel = ADC_CHANNEL_3;
  266. sConfig.Rank = ADC_REGULAR_RANK_1;
  267. sConfig.SamplingTime = ADC_SAMPLETIME_2CYCLES_5;
  268. sConfig.SingleDiff = ADC_SINGLE_ENDED;
  269. sConfig.OffsetNumber = ADC_OFFSET_NONE;
  270. sConfig.Offset = 0;
  271. if (HAL_ADC_ConfigChannel(&hadc2, &sConfig) != HAL_OK)
  272. {
  273. Error_Handler();
  274. }
  275. /* USER CODE BEGIN ADC2_Init 2 */
  276. /* USER CODE END ADC2_Init 2 */
  277. }
  278. /**
  279. * @brief ADC3 Initialization Function
  280. * @param None
  281. * @retval None
  282. */
  283. static void MX_ADC3_Init(void)
  284. {
  285. /* USER CODE BEGIN ADC3_Init 0 */
  286. /* USER CODE END ADC3_Init 0 */
  287. ADC_ChannelConfTypeDef sConfig = {0};
  288. /* USER CODE BEGIN ADC3_Init 1 */
  289. /* USER CODE END ADC3_Init 1 */
  290. /** Common config
  291. */
  292. hadc3.Instance = ADC3;
  293. hadc3.Init.ClockPrescaler = ADC_CLOCK_SYNC_PCLK_DIV4;
  294. hadc3.Init.Resolution = ADC_RESOLUTION_12B;
  295. hadc3.Init.DataAlign = ADC_DATAALIGN_RIGHT;
  296. hadc3.Init.GainCompensation = 0;
  297. hadc3.Init.ScanConvMode = ADC_SCAN_DISABLE;
  298. hadc3.Init.EOCSelection = ADC_EOC_SINGLE_CONV;
  299. hadc3.Init.LowPowerAutoWait = DISABLE;
  300. hadc3.Init.ContinuousConvMode = DISABLE;
  301. hadc3.Init.NbrOfConversion = 1;
  302. hadc3.Init.DiscontinuousConvMode = DISABLE;
  303. hadc3.Init.ExternalTrigConv = ADC_SOFTWARE_START;
  304. hadc3.Init.ExternalTrigConvEdge = ADC_EXTERNALTRIGCONVEDGE_NONE;
  305. hadc3.Init.DMAContinuousRequests = DISABLE;
  306. hadc3.Init.Overrun = ADC_OVR_DATA_PRESERVED;
  307. hadc3.Init.OversamplingMode = DISABLE;
  308. if (HAL_ADC_Init(&hadc3) != HAL_OK)
  309. {
  310. Error_Handler();
  311. }
  312. /** Configure Regular Channel
  313. */
  314. sConfig.Channel = ADC_CHANNEL_1;
  315. sConfig.Rank = ADC_REGULAR_RANK_1;
  316. sConfig.SamplingTime = ADC_SAMPLETIME_2CYCLES_5;
  317. sConfig.SingleDiff = ADC_SINGLE_ENDED;
  318. sConfig.OffsetNumber = ADC_OFFSET_NONE;
  319. sConfig.Offset = 0;
  320. if (HAL_ADC_ConfigChannel(&hadc3, &sConfig) != HAL_OK)
  321. {
  322. Error_Handler();
  323. }
  324. /* USER CODE BEGIN ADC3_Init 2 */
  325. /* USER CODE END ADC3_Init 2 */
  326. }
  327. /**
  328. * @brief COMP1 Initialization Function
  329. * @param None
  330. * @retval None
  331. */
  332. static void MX_COMP1_Init(void)
  333. {
  334. /* USER CODE BEGIN COMP1_Init 0 */
  335. /* USER CODE END COMP1_Init 0 */
  336. /* USER CODE BEGIN COMP1_Init 1 */
  337. /* USER CODE END COMP1_Init 1 */
  338. hcomp1.Instance = COMP1;
  339. hcomp1.Init.InputPlus = COMP_INPUT_PLUS_IO1;
  340. hcomp1.Init.InputMinus = COMP_INPUT_MINUS_DAC3_CH1;
  341. hcomp1.Init.OutputPol = COMP_OUTPUTPOL_NONINVERTED;
  342. hcomp1.Init.Hysteresis = COMP_HYSTERESIS_NONE;
  343. hcomp1.Init.BlankingSrce = COMP_BLANKINGSRC_NONE;
  344. hcomp1.Init.TriggerMode = COMP_TRIGGERMODE_NONE;
  345. if (HAL_COMP_Init(&hcomp1) != HAL_OK)
  346. {
  347. Error_Handler();
  348. }
  349. /* USER CODE BEGIN COMP1_Init 2 */
  350. /* USER CODE END COMP1_Init 2 */
  351. }
  352. /**
  353. * @brief COMP2 Initialization Function
  354. * @param None
  355. * @retval None
  356. */
  357. static void MX_COMP2_Init(void)
  358. {
  359. /* USER CODE BEGIN COMP2_Init 0 */
  360. /* USER CODE END COMP2_Init 0 */
  361. /* USER CODE BEGIN COMP2_Init 1 */
  362. /* USER CODE END COMP2_Init 1 */
  363. hcomp2.Instance = COMP2;
  364. hcomp2.Init.InputPlus = COMP_INPUT_PLUS_IO2;
  365. hcomp2.Init.InputMinus = COMP_INPUT_MINUS_DAC3_CH2;
  366. hcomp2.Init.OutputPol = COMP_OUTPUTPOL_NONINVERTED;
  367. hcomp2.Init.Hysteresis = COMP_HYSTERESIS_NONE;
  368. hcomp2.Init.BlankingSrce = COMP_BLANKINGSRC_NONE;
  369. hcomp2.Init.TriggerMode = COMP_TRIGGERMODE_NONE;
  370. if (HAL_COMP_Init(&hcomp2) != HAL_OK)
  371. {
  372. Error_Handler();
  373. }
  374. /* USER CODE BEGIN COMP2_Init 2 */
  375. /* USER CODE END COMP2_Init 2 */
  376. }
  377. /**
  378. * @brief DAC1 Initialization Function
  379. * @param None
  380. * @retval None
  381. */
  382. static void MX_DAC1_Init(void)
  383. {
  384. /* USER CODE BEGIN DAC1_Init 0 */
  385. /* USER CODE END DAC1_Init 0 */
  386. DAC_ChannelConfTypeDef sConfig = {0};
  387. /* USER CODE BEGIN DAC1_Init 1 */
  388. /* USER CODE END DAC1_Init 1 */
  389. /** DAC Initialization
  390. */
  391. hdac1.Instance = DAC1;
  392. if (HAL_DAC_Init(&hdac1) != HAL_OK)
  393. {
  394. Error_Handler();
  395. }
  396. /** DAC channel OUT1 config
  397. */
  398. sConfig.DAC_HighFrequency = DAC_HIGH_FREQUENCY_INTERFACE_MODE_AUTOMATIC;
  399. sConfig.DAC_DMADoubleDataMode = DISABLE;
  400. sConfig.DAC_SignedFormat = DISABLE;
  401. sConfig.DAC_SampleAndHold = DAC_SAMPLEANDHOLD_DISABLE;
  402. sConfig.DAC_Trigger = DAC_TRIGGER_NONE;
  403. sConfig.DAC_Trigger2 = DAC_TRIGGER_NONE;
  404. sConfig.DAC_OutputBuffer = DAC_OUTPUTBUFFER_ENABLE;
  405. sConfig.DAC_ConnectOnChipPeripheral = DAC_CHIPCONNECT_EXTERNAL;
  406. sConfig.DAC_UserTrimming = DAC_TRIMMING_FACTORY;
  407. if (HAL_DAC_ConfigChannel(&hdac1, &sConfig, DAC_CHANNEL_1) != HAL_OK)
  408. {
  409. Error_Handler();
  410. }
  411. /** DAC channel OUT2 config
  412. */
  413. if (HAL_DAC_ConfigChannel(&hdac1, &sConfig, DAC_CHANNEL_2) != HAL_OK)
  414. {
  415. Error_Handler();
  416. }
  417. /* USER CODE BEGIN DAC1_Init 2 */
  418. /* USER CODE END DAC1_Init 2 */
  419. }
  420. /**
  421. * @brief DAC3 Initialization Function
  422. * @param None
  423. * @retval None
  424. */
  425. static void MX_DAC3_Init(void)
  426. {
  427. /* USER CODE BEGIN DAC3_Init 0 */
  428. /* USER CODE END DAC3_Init 0 */
  429. DAC_ChannelConfTypeDef sConfig = {0};
  430. /* USER CODE BEGIN DAC3_Init 1 */
  431. /* USER CODE END DAC3_Init 1 */
  432. /** DAC Initialization
  433. */
  434. hdac3.Instance = DAC3;
  435. if (HAL_DAC_Init(&hdac3) != HAL_OK)
  436. {
  437. Error_Handler();
  438. }
  439. /** DAC channel OUT1 config
  440. */
  441. sConfig.DAC_HighFrequency = DAC_HIGH_FREQUENCY_INTERFACE_MODE_AUTOMATIC;
  442. sConfig.DAC_DMADoubleDataMode = DISABLE;
  443. sConfig.DAC_SignedFormat = DISABLE;
  444. sConfig.DAC_SampleAndHold = DAC_SAMPLEANDHOLD_DISABLE;
  445. sConfig.DAC_Trigger = DAC_TRIGGER_NONE;
  446. sConfig.DAC_Trigger2 = DAC_TRIGGER_NONE;
  447. sConfig.DAC_OutputBuffer = DAC_OUTPUTBUFFER_DISABLE;
  448. sConfig.DAC_ConnectOnChipPeripheral = DAC_CHIPCONNECT_INTERNAL;
  449. sConfig.DAC_UserTrimming = DAC_TRIMMING_FACTORY;
  450. if (HAL_DAC_ConfigChannel(&hdac3, &sConfig, DAC_CHANNEL_1) != HAL_OK)
  451. {
  452. Error_Handler();
  453. }
  454. /** DAC channel OUT2 config
  455. */
  456. if (HAL_DAC_ConfigChannel(&hdac3, &sConfig, DAC_CHANNEL_2) != HAL_OK)
  457. {
  458. Error_Handler();
  459. }
  460. /* USER CODE BEGIN DAC3_Init 2 */
  461. /* USER CODE END DAC3_Init 2 */
  462. }
  463. /**
  464. * @brief I2C1 Initialization Function
  465. * @param None
  466. * @retval None
  467. */
  468. static void MX_I2C1_SMBUS_Init(void)
  469. {
  470. /* USER CODE BEGIN I2C1_Init 0 */
  471. /* USER CODE END I2C1_Init 0 */
  472. /* USER CODE BEGIN I2C1_Init 1 */
  473. /* USER CODE END I2C1_Init 1 */
  474. hsmbus1.Instance = I2C1;
  475. hsmbus1.Init.Timing = 0x30909DEC;
  476. hsmbus1.Init.AnalogFilter = SMBUS_ANALOGFILTER_ENABLE;
  477. hsmbus1.Init.OwnAddress1 = 2;
  478. hsmbus1.Init.AddressingMode = SMBUS_ADDRESSINGMODE_7BIT;
  479. hsmbus1.Init.DualAddressMode = SMBUS_DUALADDRESS_DISABLE;
  480. hsmbus1.Init.OwnAddress2 = 0;
  481. hsmbus1.Init.OwnAddress2Masks = SMBUS_OA2_NOMASK;
  482. hsmbus1.Init.GeneralCallMode = SMBUS_GENERALCALL_DISABLE;
  483. hsmbus1.Init.NoStretchMode = SMBUS_NOSTRETCH_DISABLE;
  484. hsmbus1.Init.PacketErrorCheckMode = SMBUS_PEC_DISABLE;
  485. hsmbus1.Init.PeripheralMode = SMBUS_PERIPHERAL_MODE_SMBUS_SLAVE;
  486. hsmbus1.Init.SMBusTimeout = 0x000087A1;
  487. if (HAL_SMBUS_Init(&hsmbus1) != HAL_OK)
  488. {
  489. Error_Handler();
  490. }
  491. /* USER CODE BEGIN I2C1_Init 2 */
  492. /* USER CODE END I2C1_Init 2 */
  493. }
  494. /**
  495. * @brief SPI1 Initialization Function
  496. * @param None
  497. * @retval None
  498. */
  499. static void MX_SPI1_Init(void)
  500. {
  501. /* USER CODE BEGIN SPI1_Init 0 */
  502. /* USER CODE END SPI1_Init 0 */
  503. /* USER CODE BEGIN SPI1_Init 1 */
  504. /* USER CODE END SPI1_Init 1 */
  505. /* SPI1 parameter configuration*/
  506. hspi1.Instance = SPI1;
  507. hspi1.Init.Mode = SPI_MODE_MASTER;
  508. hspi1.Init.Direction = SPI_DIRECTION_2LINES;
  509. hspi1.Init.DataSize = SPI_DATASIZE_8BIT;
  510. hspi1.Init.CLKPolarity = SPI_POLARITY_LOW;
  511. hspi1.Init.CLKPhase = SPI_PHASE_1EDGE;
  512. hspi1.Init.NSS = SPI_NSS_SOFT;
  513. hspi1.Init.BaudRatePrescaler = SPI_BAUDRATEPRESCALER_8;
  514. hspi1.Init.FirstBit = SPI_FIRSTBIT_MSB;
  515. hspi1.Init.TIMode = SPI_TIMODE_DISABLE;
  516. hspi1.Init.CRCCalculation = SPI_CRCCALCULATION_DISABLE;
  517. hspi1.Init.CRCPolynomial = 7;
  518. hspi1.Init.CRCLength = SPI_CRC_LENGTH_DATASIZE;
  519. hspi1.Init.NSSPMode = SPI_NSS_PULSE_ENABLE;
  520. if (HAL_SPI_Init(&hspi1) != HAL_OK)
  521. {
  522. Error_Handler();
  523. }
  524. /* USER CODE BEGIN SPI1_Init 2 */
  525. /* USER CODE END SPI1_Init 2 */
  526. }
  527. /**
  528. * @brief SPI2 Initialization Function
  529. * @param None
  530. * @retval None
  531. */
  532. static void MX_SPI2_Init(void)
  533. {
  534. /* USER CODE BEGIN SPI2_Init 0 */
  535. /* USER CODE END SPI2_Init 0 */
  536. /* USER CODE BEGIN SPI2_Init 1 */
  537. /* USER CODE END SPI2_Init 1 */
  538. /* SPI2 parameter configuration*/
  539. hspi2.Instance = SPI2;
  540. hspi2.Init.Mode = SPI_MODE_MASTER;
  541. hspi2.Init.Direction = SPI_DIRECTION_2LINES;
  542. hspi2.Init.DataSize = SPI_DATASIZE_8BIT;
  543. hspi2.Init.CLKPolarity = SPI_POLARITY_LOW;
  544. hspi2.Init.CLKPhase = SPI_PHASE_1EDGE;
  545. hspi2.Init.NSS = SPI_NSS_SOFT;
  546. hspi2.Init.BaudRatePrescaler = SPI_BAUDRATEPRESCALER_16;
  547. hspi2.Init.FirstBit = SPI_FIRSTBIT_MSB;
  548. hspi2.Init.TIMode = SPI_TIMODE_DISABLE;
  549. hspi2.Init.CRCCalculation = SPI_CRCCALCULATION_DISABLE;
  550. hspi2.Init.CRCPolynomial = 7;
  551. hspi2.Init.CRCLength = SPI_CRC_LENGTH_DATASIZE;
  552. hspi2.Init.NSSPMode = SPI_NSS_PULSE_ENABLE;
  553. if (HAL_SPI_Init(&hspi2) != HAL_OK)
  554. {
  555. Error_Handler();
  556. }
  557. /* USER CODE BEGIN SPI2_Init 2 */
  558. /* USER CODE END SPI2_Init 2 */
  559. }
  560. /**
  561. * @brief TIM1 Initialization Function
  562. * @param None
  563. * @retval None
  564. */
  565. static void MX_TIM1_Init(void)
  566. {
  567. /* USER CODE BEGIN TIM1_Init 0 */
  568. /* USER CODE END TIM1_Init 0 */
  569. TIM_SlaveConfigTypeDef sSlaveConfig = {0};
  570. TIM_MasterConfigTypeDef sMasterConfig = {0};
  571. /* USER CODE BEGIN TIM1_Init 1 */
  572. /* USER CODE END TIM1_Init 1 */
  573. htim1.Instance = TIM1;
  574. htim1.Init.Prescaler = 0;
  575. htim1.Init.CounterMode = TIM_COUNTERMODE_UP;
  576. htim1.Init.Period = 65535;
  577. htim1.Init.ClockDivision = TIM_CLOCKDIVISION_DIV1;
  578. htim1.Init.RepetitionCounter = 0;
  579. htim1.Init.AutoReloadPreload = TIM_AUTORELOAD_PRELOAD_DISABLE;
  580. if (HAL_TIM_Base_Init(&htim1) != HAL_OK)
  581. {
  582. Error_Handler();
  583. }
  584. sSlaveConfig.SlaveMode = TIM_SLAVEMODE_EXTERNAL1;
  585. sSlaveConfig.InputTrigger = TIM_TS_ITR0;
  586. if (HAL_TIM_SlaveConfigSynchro(&htim1, &sSlaveConfig) != HAL_OK)
  587. {
  588. Error_Handler();
  589. }
  590. sMasterConfig.MasterOutputTrigger = TIM_TRGO_RESET;
  591. sMasterConfig.MasterOutputTrigger2 = TIM_TRGO2_RESET;
  592. sMasterConfig.MasterSlaveMode = TIM_MASTERSLAVEMODE_DISABLE;
  593. if (HAL_TIMEx_MasterConfigSynchronization(&htim1, &sMasterConfig) != HAL_OK)
  594. {
  595. Error_Handler();
  596. }
  597. /* USER CODE BEGIN TIM1_Init 2 */
  598. /* USER CODE END TIM1_Init 2 */
  599. }
  600. /**
  601. * @brief TIM2 Initialization Function
  602. * @param None
  603. * @retval None
  604. */
  605. static void MX_TIM2_Init(void)
  606. {
  607. /* USER CODE BEGIN TIM2_Init 0 */
  608. /* USER CODE END TIM2_Init 0 */
  609. TIM_SlaveConfigTypeDef sSlaveConfig = {0};
  610. TIM_MasterConfigTypeDef sMasterConfig = {0};
  611. TIM_OC_InitTypeDef sConfigOC = {0};
  612. /* USER CODE BEGIN TIM2_Init 1 */
  613. /* USER CODE END TIM2_Init 1 */
  614. htim2.Instance = TIM2;
  615. htim2.Init.Prescaler = 0;
  616. htim2.Init.CounterMode = TIM_COUNTERMODE_UP;
  617. htim2.Init.Period = 4.294967295E9;
  618. htim2.Init.ClockDivision = TIM_CLOCKDIVISION_DIV1;
  619. htim2.Init.AutoReloadPreload = TIM_AUTORELOAD_PRELOAD_DISABLE;
  620. if (HAL_TIM_Base_Init(&htim2) != HAL_OK)
  621. {
  622. Error_Handler();
  623. }
  624. if (HAL_TIM_OC_Init(&htim2) != HAL_OK)
  625. {
  626. Error_Handler();
  627. }
  628. sSlaveConfig.SlaveMode = TIM_SLAVEMODE_EXTERNAL1;
  629. sSlaveConfig.InputTrigger = TIM_TS_ETRF;
  630. sSlaveConfig.TriggerPolarity = TIM_TRIGGERPOLARITY_NONINVERTED;
  631. sSlaveConfig.TriggerPrescaler = TIM_TRIGGERPRESCALER_DIV1;
  632. sSlaveConfig.TriggerFilter = 0;
  633. if (HAL_TIM_SlaveConfigSynchro(&htim2, &sSlaveConfig) != HAL_OK)
  634. {
  635. Error_Handler();
  636. }
  637. sMasterConfig.MasterOutputTrigger = TIM_TRGO_RESET;
  638. sMasterConfig.MasterSlaveMode = TIM_MASTERSLAVEMODE_DISABLE;
  639. if (HAL_TIMEx_MasterConfigSynchronization(&htim2, &sMasterConfig) != HAL_OK)
  640. {
  641. Error_Handler();
  642. }
  643. sConfigOC.OCMode = TIM_OCMODE_TIMING;
  644. sConfigOC.Pulse = 0;
  645. sConfigOC.OCPolarity = TIM_OCPOLARITY_HIGH;
  646. sConfigOC.OCFastMode = TIM_OCFAST_DISABLE;
  647. if (HAL_TIM_OC_ConfigChannel(&htim2, &sConfigOC, TIM_CHANNEL_1) != HAL_OK)
  648. {
  649. Error_Handler();
  650. }
  651. /* USER CODE BEGIN TIM2_Init 2 */
  652. /* USER CODE END TIM2_Init 2 */
  653. }
  654. /**
  655. * @brief USART1 Initialization Function
  656. * @param None
  657. * @retval None
  658. */
  659. static void MX_USART1_UART_Init(void)
  660. {
  661. /* USER CODE BEGIN USART1_Init 0 */
  662. /* USER CODE END USART1_Init 0 */
  663. /* USER CODE BEGIN USART1_Init 1 */
  664. /* USER CODE END USART1_Init 1 */
  665. huart1.Instance = USART1;
  666. huart1.Init.BaudRate = 115200;
  667. huart1.Init.WordLength = UART_WORDLENGTH_8B;
  668. huart1.Init.StopBits = UART_STOPBITS_1;
  669. huart1.Init.Parity = UART_PARITY_NONE;
  670. huart1.Init.Mode = UART_MODE_TX_RX;
  671. huart1.Init.HwFlowCtl = UART_HWCONTROL_NONE;
  672. huart1.Init.OverSampling = UART_OVERSAMPLING_16;
  673. huart1.Init.OneBitSampling = UART_ONE_BIT_SAMPLE_DISABLE;
  674. huart1.Init.ClockPrescaler = UART_PRESCALER_DIV1;
  675. huart1.AdvancedInit.AdvFeatureInit = UART_ADVFEATURE_NO_INIT;
  676. if (HAL_UART_Init(&huart1) != HAL_OK)
  677. {
  678. Error_Handler();
  679. }
  680. if (HAL_UARTEx_SetTxFifoThreshold(&huart1, UART_TXFIFO_THRESHOLD_1_8) != HAL_OK)
  681. {
  682. Error_Handler();
  683. }
  684. if (HAL_UARTEx_SetRxFifoThreshold(&huart1, UART_RXFIFO_THRESHOLD_1_8) != HAL_OK)
  685. {
  686. Error_Handler();
  687. }
  688. if (HAL_UARTEx_DisableFifoMode(&huart1) != HAL_OK)
  689. {
  690. Error_Handler();
  691. }
  692. /* USER CODE BEGIN USART1_Init 2 */
  693. /* USER CODE END USART1_Init 2 */
  694. }
  695. /**
  696. * @brief GPIO Initialization Function
  697. * @param None
  698. * @retval None
  699. */
  700. static void MX_GPIO_Init(void)
  701. {
  702. GPIO_InitTypeDef GPIO_InitStruct = {0};
  703. /* GPIO Ports Clock Enable */
  704. __HAL_RCC_GPIOC_CLK_ENABLE();
  705. __HAL_RCC_GPIOF_CLK_ENABLE();
  706. __HAL_RCC_GPIOA_CLK_ENABLE();
  707. __HAL_RCC_GPIOB_CLK_ENABLE();
  708. __HAL_RCC_GPIOD_CLK_ENABLE();
  709. /*Configure GPIO pin Output Level */
  710. HAL_GPIO_WritePin(GPIOC, GPIO_PIN_13|GPIO_PIN_0|GPIO_PIN_1|GPIO_PIN_2
  711. |GPIO_PIN_3|GPIO_PIN_4|GPIO_PIN_5|GPIO_PIN_10
  712. |GPIO_PIN_11|GPIO_PIN_12, GPIO_PIN_RESET);
  713. /*Configure GPIO pin Output Level */
  714. HAL_GPIO_WritePin(GPIOB, GPIO_PIN_0|GPIO_PIN_2|GPIO_PIN_10|GPIO_PIN_11
  715. |GPIO_PIN_12|GPIO_PIN_6|GPIO_PIN_7, GPIO_PIN_RESET);
  716. /*Configure GPIO pin Output Level */
  717. HAL_GPIO_WritePin(GPIOA, GPIO_PIN_15, GPIO_PIN_RESET);
  718. /*Configure GPIO pin Output Level */
  719. HAL_GPIO_WritePin(GPIOD, GPIO_PIN_2, GPIO_PIN_RESET);
  720. /*Configure GPIO pins : PC13 PC0 PC1 PC2
  721. PC3 PC4 PC5 PC10
  722. PC11 PC12 */
  723. GPIO_InitStruct.Pin = GPIO_PIN_13|GPIO_PIN_0|GPIO_PIN_1|GPIO_PIN_2
  724. |GPIO_PIN_3|GPIO_PIN_4|GPIO_PIN_5|GPIO_PIN_10
  725. |GPIO_PIN_11|GPIO_PIN_12;
  726. GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP;
  727. GPIO_InitStruct.Pull = GPIO_NOPULL;
  728. GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW;
  729. HAL_GPIO_Init(GPIOC, &GPIO_InitStruct);
  730. /*Configure GPIO pins : PB0 PB2 PB10 PB11
  731. PB12 PB6 PB7 */
  732. GPIO_InitStruct.Pin = GPIO_PIN_0|GPIO_PIN_2|GPIO_PIN_10|GPIO_PIN_11
  733. |GPIO_PIN_12|GPIO_PIN_6|GPIO_PIN_7;
  734. GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP;
  735. GPIO_InitStruct.Pull = GPIO_NOPULL;
  736. GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW;
  737. HAL_GPIO_Init(GPIOB, &GPIO_InitStruct);
  738. /*Configure GPIO pins : PC6 PC7 PC8 PC9 */
  739. GPIO_InitStruct.Pin = GPIO_PIN_6|GPIO_PIN_7|GPIO_PIN_8|GPIO_PIN_9;
  740. GPIO_InitStruct.Mode = GPIO_MODE_INPUT;
  741. GPIO_InitStruct.Pull = GPIO_PULLUP;
  742. HAL_GPIO_Init(GPIOC, &GPIO_InitStruct);
  743. /*Configure GPIO pin : PA8 */
  744. GPIO_InitStruct.Pin = GPIO_PIN_8;
  745. GPIO_InitStruct.Mode = GPIO_MODE_INPUT;
  746. GPIO_InitStruct.Pull = GPIO_PULLUP;
  747. HAL_GPIO_Init(GPIOA, &GPIO_InitStruct);
  748. /*Configure GPIO pin : PA15 */
  749. GPIO_InitStruct.Pin = GPIO_PIN_15;
  750. GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP;
  751. GPIO_InitStruct.Pull = GPIO_NOPULL;
  752. GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW;
  753. HAL_GPIO_Init(GPIOA, &GPIO_InitStruct);
  754. /*Configure GPIO pin : PD2 */
  755. GPIO_InitStruct.Pin = GPIO_PIN_2;
  756. GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP;
  757. GPIO_InitStruct.Pull = GPIO_NOPULL;
  758. GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW;
  759. HAL_GPIO_Init(GPIOD, &GPIO_InitStruct);
  760. }
  761. /* USER CODE BEGIN 4 */
  762. /* USER CODE END 4 */
  763. /**
  764. * @brief This function is executed in case of error occurrence.
  765. * @retval None
  766. */
  767. void Error_Handler(void)
  768. {
  769. /* USER CODE BEGIN Error_Handler_Debug */
  770. /* User can add his own implementation to report the HAL error return state */
  771. __disable_irq();
  772. while (1)
  773. {
  774. }
  775. /* USER CODE END Error_Handler_Debug */
  776. }
  777. #ifdef USE_FULL_ASSERT
  778. /**
  779. * @brief Reports the name of the source file and the source line number
  780. * where the assert_param error has occurred.
  781. * @param file: pointer to the source file name
  782. * @param line: assert_param error line source number
  783. * @retval None
  784. */
  785. void assert_failed(uint8_t *file, uint32_t line)
  786. {
  787. /* USER CODE BEGIN 6 */
  788. /* User can add his own implementation to report the file name and line number,
  789. ex: printf("Wrong parameters value: file %s on line %d\r\n", file, line) */
  790. /* USER CODE END 6 */
  791. }
  792. #endif /* USE_FULL_ASSERT */
  793. /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/