main.c 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983
  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. #include "usbd_cdc_if.h"
  26. #include "uartio.h"
  27. #include "commprt.h"
  28. #include <stdlib.h>
  29. #include <stdio.h>
  30. /* USER CODE END Includes */
  31. /* Private typedef -----------------------------------------------------------*/
  32. /* USER CODE BEGIN PTD */
  33. /* USER CODE END PTD */
  34. /* Private define ------------------------------------------------------------*/
  35. /* USER CODE BEGIN PD */
  36. #define VREFINT_CAL_VALUE (*((uint16_t*)VREFINT_CAL_ADDR_CMSIS))
  37. #define VREFINT_CAL_VREF 3000UL
  38. /* USER CODE END PD */
  39. /* Private macro -------------------------------------------------------------*/
  40. /* USER CODE BEGIN PM */
  41. /* USER CODE END PM */
  42. /* Private variables ---------------------------------------------------------*/
  43. ADC_HandleTypeDef hadc;
  44. DMA_HandleTypeDef hdma_adc;
  45. COMP_HandleTypeDef hcomp2;
  46. CRC_HandleTypeDef hcrc;
  47. DAC_HandleTypeDef hdac;
  48. I2C_HandleTypeDef hi2c1;
  49. I2C_HandleTypeDef hi2c2;
  50. TIM_HandleTypeDef htim2;
  51. TIM_HandleTypeDef htim3;
  52. TIM_HandleTypeDef htim4;
  53. TIM_HandleTypeDef htim6;
  54. UART_HandleTypeDef huart1;
  55. DMA_HandleTypeDef hdma_usart1_rx;
  56. DMA_HandleTypeDef hdma_usart1_tx;
  57. /* USER CODE BEGIN PV */
  58. struct {
  59. uint32_t CH0;
  60. uint32_t TEMP;
  61. uint32_t VREF;
  62. } adc_meas;
  63. volatile struct {
  64. uint16_t Tim2;
  65. uint16_t Tim3;
  66. uint16_t Tim4;
  67. uint16_t Tim6;
  68. uint16_t Comp;
  69. uint16_t Exti;
  70. } Flags;
  71. volatile uint32_t pulses = 0;
  72. /* USER CODE END PV */
  73. /* Private function prototypes -----------------------------------------------*/
  74. void SystemClock_Config(void);
  75. static void MX_DMA_Init(void);
  76. static void MX_GPIO_Init(void);
  77. static void MX_ADC_Init(void);
  78. static void MX_DAC_Init(void);
  79. static void MX_I2C2_Init(void);
  80. static void MX_COMP2_Init(void);
  81. static void MX_USART1_UART_Init(void);
  82. static void MX_TIM6_Init(void);
  83. static void MX_TIM4_Init(void);
  84. static void MX_TIM3_Init(void);
  85. static void MX_TIM2_Init(void);
  86. static void MX_I2C1_Init(void);
  87. static void MX_CRC_Init(void);
  88. /* USER CODE BEGIN PFP */
  89. /* USER CODE END PFP */
  90. /* Private user code ---------------------------------------------------------*/
  91. /* USER CODE BEGIN 0 */
  92. void HAL_COMP_TriggerCallback(COMP_HandleTypeDef *hcomp)
  93. {
  94. pulses++;
  95. }
  96. void HAL_GPIO_EXTI_Callback(uint16_t GPIO_Pin)
  97. {
  98. Flags.Exti |= GPIO_Pin;
  99. }
  100. void HAL_TIM_PeriodElapsedCallback(TIM_HandleTypeDef *htim)
  101. {
  102. if(htim == &htim4)
  103. {
  104. Flags.Tim4++;
  105. }
  106. if(htim == &htim6)
  107. {
  108. Flags.Tim6 = 1;
  109. }
  110. }
  111. uint32_t GetVcc(void)
  112. {
  113. return (VREFINT_CAL_VALUE * VREFINT_CAL_VREF / adc_meas.VREF);
  114. }
  115. void SetDACVoltage(uint32_t channel, uint32_t voltage)
  116. {
  117. HAL_DAC_SetValue(&hdac, channel, DAC_ALIGN_12B_R, (voltage * 4095UL) / GetVcc());
  118. }
  119. void Switch3V3Regulator(OnOff_t state)
  120. {
  121. if(state == ON)
  122. HAL_GPIO_WritePin(GPIOA, GPIO_PIN_7, GPIO_PIN_SET);
  123. else
  124. HAL_GPIO_WritePin(GPIOA, GPIO_PIN_7, GPIO_PIN_RESET);
  125. }
  126. void SwitchPeriphSupply(OnOff_t state)
  127. {
  128. if(state == ON)
  129. HAL_GPIO_WritePin(GPIOB, GPIO_PIN_0, GPIO_PIN_RESET);
  130. else
  131. HAL_GPIO_WritePin(GPIOB, GPIO_PIN_0, GPIO_PIN_SET);
  132. }
  133. void Beep()
  134. {
  135. HAL_TIM_Base_Stop(&htim2);
  136. HAL_TIM_Base_Start(&htim2);
  137. }
  138. /* USER CODE END 0 */
  139. /**
  140. * @brief The application entry point.
  141. * @retval int
  142. */
  143. int main(void)
  144. {
  145. /* USER CODE BEGIN 1 */
  146. /* USER CODE END 1 */
  147. /* MCU Configuration--------------------------------------------------------*/
  148. /* Reset of all peripherals, Initializes the Flash interface and the Systick. */
  149. HAL_Init();
  150. /* USER CODE BEGIN Init */
  151. /* USER CODE END Init */
  152. /* Configure the system clock */
  153. SystemClock_Config();
  154. /* USER CODE BEGIN SysInit */
  155. /* USER CODE END SysInit */
  156. /* Initialize all configured peripherals */
  157. MX_DMA_Init();
  158. MX_GPIO_Init();
  159. MX_ADC_Init();
  160. MX_DAC_Init();
  161. MX_COMP2_Init();
  162. MX_USART1_UART_Init();
  163. MX_TIM6_Init();
  164. MX_TIM4_Init();
  165. MX_TIM3_Init();
  166. MX_TIM2_Init();
  167. MX_I2C1_Init();
  168. MX_USB_DEVICE_Init();
  169. MX_CRC_Init();
  170. /* USER CODE BEGIN 2 */
  171. Switch3V3Regulator(ON);
  172. SwitchPeriphSupply(ON);
  173. HAL_Delay(100);
  174. MX_I2C2_Init();
  175. ssd1306_Init();
  176. ssd1306_Fill(Black);
  177. ssd1306_UpdateScreen();
  178. HAL_DAC_Start(&hdac, DAC_CHANNEL_1);
  179. HAL_DAC_Start(&hdac, DAC_CHANNEL_2);
  180. HAL_DAC_SetValue(&hdac, DAC_CHANNEL_1, DAC_ALIGN_12B_R, 0xFFF);
  181. HAL_DAC_SetValue(&hdac, DAC_CHANNEL_2, DAC_ALIGN_12B_R, 0xFFF);
  182. HAL_Delay(10);
  183. HAL_ADC_Start_DMA(&hadc, (uint32_t*)&adc_meas, 3);
  184. HAL_Delay(10);
  185. HAL_ADC_Stop_DMA(&hadc);
  186. SetDACVoltage(DAC_CHANNEL_1, SIPM_BIAS_VOLTAGE_OFFSET);
  187. SetDACVoltage(DAC_CHANNEL_2, COMP_THRESHOLD_VOLTAGE);
  188. HAL_COMP_Start_IT(&hcomp2);
  189. HAL_TIM_Base_Start_IT(&htim6);
  190. HAL_TIM_Base_Start_IT(&htim4);
  191. HAL_TIM_OC_Start(&htim3, TIM_CHANNEL_4);
  192. UARTIO_Init(&huart1);
  193. /* USER CODE END 2 */
  194. /* Infinite loop */
  195. /* USER CODE BEGIN WHILE */
  196. char tempstr[32];
  197. uint32_t pps = 0;
  198. uint32_t pps_avg = 0;
  199. uint32_t total_dose = 0;
  200. uint8_t pwdn_cnt = 0;
  201. uint8_t window = 0;
  202. Flags.Exti = 0;
  203. while (1)
  204. {
  205. if(Flags.Exti)
  206. {
  207. uint16_t btn = Flags.Exti;
  208. Flags.Exti = 0;
  209. if(btn == GPIO_PIN_15)
  210. {
  211. window ^= 1;
  212. }
  213. }
  214. if(Flags.Tim6)
  215. {
  216. Flags.Tim6 = 0;
  217. HAL_ADC_Stop_DMA(&hadc);
  218. if(HAL_GPIO_ReadPin(GPIOB, GPIO_PIN_14) == GPIO_PIN_RESET)
  219. {
  220. if(++pwdn_cnt > 10)
  221. {
  222. ssd1306_Reset();
  223. SwitchPeriphSupply(OFF);
  224. HAL_I2C_DeInit(&hi2c1);
  225. HAL_I2C_DeInit(&hi2c2);
  226. HAL_UART_DeInit(&huart1);
  227. HAL_GPIO_DeInit(GPIOB, GPIO_PIN_All);
  228. HAL_GPIO_DeInit(GPIOA, GPIO_PIN_All);
  229. HAL_PWR_EnterSTOPMode(PWR_MAINREGULATOR_ON, PWR_STOPENTRY_WFI);
  230. }
  231. }
  232. else
  233. {
  234. pwdn_cnt = 0;
  235. }
  236. if((pps / pps_avg > 2) || (pps_avg / pps > 2))
  237. {
  238. pps_avg = pps;
  239. }
  240. else
  241. {
  242. pps_avg = (pps + pps_avg) / 2;
  243. }
  244. ssd1306_Fill(Black);
  245. ssd1306_DrawRectangle(0, 0, SSD1306_WIDTH - 1, SSD1306_HEIGHT - 1, White);
  246. if(window == 0)
  247. {
  248. ssd1306_SetCursor(1, 7);
  249. sprintf(tempstr, "%5.2f uSv/h", (float)pps_avg / 100.0);
  250. ssd1306_WriteString(tempstr, Font_11x18, White);
  251. }
  252. else
  253. {
  254. ssd1306_SetCursor(2, 2);
  255. ssd1306_WriteString(tempstr, Font_7x10, White);
  256. }
  257. ssd1306_UpdateScreen();
  258. SetDACVoltage(DAC_CHANNEL_1, SIPM_BIAS_VOLTAGE_OFFSET);
  259. SetDACVoltage(DAC_CHANNEL_2, COMP_THRESHOLD_VOLTAGE);
  260. HAL_ADC_Start_DMA(&hadc, (uint32_t*)&adc_meas, 3);
  261. sprintf(tempstr, "%5.2f uSv/h\r\n", (float)pps_avg / 100.0);
  262. COMM_Transmit((uint8_t*)&pps_avg, sizeof(pps_avg), COMM_USB);
  263. }
  264. if(pulses > 50 || Flags.Tim4 > 3)
  265. {
  266. uint32_t elapsed = htim4.Instance->CNT + (Flags.Tim4 * 50000);
  267. HAL_TIM_Base_Stop(&htim4);
  268. pps = (pulses * 100000) / elapsed;
  269. pulses = 0;
  270. Flags.Tim4 = 0;
  271. htim4.Instance->CNT = 0;
  272. HAL_TIM_Base_Start(&htim4);
  273. Beep();
  274. }
  275. /* USER CODE END WHILE */
  276. /* USER CODE BEGIN 3 */
  277. }
  278. /* USER CODE END 3 */
  279. }
  280. /**
  281. * @brief System Clock Configuration
  282. * @retval None
  283. */
  284. void SystemClock_Config(void)
  285. {
  286. RCC_OscInitTypeDef RCC_OscInitStruct = {0};
  287. RCC_ClkInitTypeDef RCC_ClkInitStruct = {0};
  288. /** Configure the main internal regulator output voltage
  289. */
  290. __HAL_PWR_VOLTAGESCALING_CONFIG(PWR_REGULATOR_VOLTAGE_SCALE1);
  291. /** Initializes the RCC Oscillators according to the specified parameters
  292. * in the RCC_OscInitTypeDef structure.
  293. */
  294. RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_HSI|RCC_OSCILLATORTYPE_HSE;
  295. RCC_OscInitStruct.HSEState = RCC_HSE_ON;
  296. RCC_OscInitStruct.HSIState = RCC_HSI_ON;
  297. RCC_OscInitStruct.HSICalibrationValue = RCC_HSICALIBRATION_DEFAULT;
  298. RCC_OscInitStruct.PLL.PLLState = RCC_PLL_ON;
  299. RCC_OscInitStruct.PLL.PLLSource = RCC_PLLSOURCE_HSE;
  300. RCC_OscInitStruct.PLL.PLLMUL = RCC_PLL_MUL12;
  301. RCC_OscInitStruct.PLL.PLLDIV = RCC_PLL_DIV4;
  302. if (HAL_RCC_OscConfig(&RCC_OscInitStruct) != HAL_OK)
  303. {
  304. Error_Handler();
  305. }
  306. /** Initializes the CPU, AHB and APB buses clocks
  307. */
  308. RCC_ClkInitStruct.ClockType = RCC_CLOCKTYPE_HCLK|RCC_CLOCKTYPE_SYSCLK
  309. |RCC_CLOCKTYPE_PCLK1|RCC_CLOCKTYPE_PCLK2;
  310. RCC_ClkInitStruct.SYSCLKSource = RCC_SYSCLKSOURCE_PLLCLK;
  311. RCC_ClkInitStruct.AHBCLKDivider = RCC_SYSCLK_DIV1;
  312. RCC_ClkInitStruct.APB1CLKDivider = RCC_HCLK_DIV1;
  313. RCC_ClkInitStruct.APB2CLKDivider = RCC_HCLK_DIV1;
  314. if (HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_1) != HAL_OK)
  315. {
  316. Error_Handler();
  317. }
  318. }
  319. /**
  320. * @brief ADC Initialization Function
  321. * @param None
  322. * @retval None
  323. */
  324. static void MX_ADC_Init(void)
  325. {
  326. /* USER CODE BEGIN ADC_Init 0 */
  327. /* USER CODE END ADC_Init 0 */
  328. ADC_ChannelConfTypeDef sConfig = {0};
  329. /* USER CODE BEGIN ADC_Init 1 */
  330. /* USER CODE END ADC_Init 1 */
  331. /** Configure the global features of the ADC (Clock, Resolution, Data Alignment and number of conversion)
  332. */
  333. hadc.Instance = ADC1;
  334. hadc.Init.ClockPrescaler = ADC_CLOCK_ASYNC_DIV1;
  335. hadc.Init.Resolution = ADC_RESOLUTION_12B;
  336. hadc.Init.DataAlign = ADC_DATAALIGN_RIGHT;
  337. hadc.Init.ScanConvMode = ADC_SCAN_ENABLE;
  338. hadc.Init.EOCSelection = ADC_EOC_SEQ_CONV;
  339. hadc.Init.LowPowerAutoWait = ADC_AUTOWAIT_DISABLE;
  340. hadc.Init.LowPowerAutoPowerOff = ADC_AUTOPOWEROFF_DISABLE;
  341. hadc.Init.ChannelsBank = ADC_CHANNELS_BANK_A;
  342. hadc.Init.ContinuousConvMode = DISABLE;
  343. hadc.Init.NbrOfConversion = 3;
  344. hadc.Init.DiscontinuousConvMode = DISABLE;
  345. hadc.Init.ExternalTrigConv = ADC_SOFTWARE_START;
  346. hadc.Init.ExternalTrigConvEdge = ADC_EXTERNALTRIGCONVEDGE_NONE;
  347. hadc.Init.DMAContinuousRequests = DISABLE;
  348. if (HAL_ADC_Init(&hadc) != HAL_OK)
  349. {
  350. Error_Handler();
  351. }
  352. /** Configure for the selected ADC regular channel its corresponding rank in the sequencer and its sample time.
  353. */
  354. sConfig.Channel = ADC_CHANNEL_0;
  355. sConfig.Rank = ADC_REGULAR_RANK_1;
  356. sConfig.SamplingTime = ADC_SAMPLETIME_48CYCLES;
  357. if (HAL_ADC_ConfigChannel(&hadc, &sConfig) != HAL_OK)
  358. {
  359. Error_Handler();
  360. }
  361. /** Configure for the selected ADC regular channel its corresponding rank in the sequencer and its sample time.
  362. */
  363. sConfig.Channel = ADC_CHANNEL_TEMPSENSOR;
  364. sConfig.Rank = ADC_REGULAR_RANK_2;
  365. if (HAL_ADC_ConfigChannel(&hadc, &sConfig) != HAL_OK)
  366. {
  367. Error_Handler();
  368. }
  369. /** Configure for the selected ADC regular channel its corresponding rank in the sequencer and its sample time.
  370. */
  371. sConfig.Channel = ADC_CHANNEL_VREFINT;
  372. sConfig.Rank = ADC_REGULAR_RANK_3;
  373. if (HAL_ADC_ConfigChannel(&hadc, &sConfig) != HAL_OK)
  374. {
  375. Error_Handler();
  376. }
  377. /* USER CODE BEGIN ADC_Init 2 */
  378. /* USER CODE END ADC_Init 2 */
  379. }
  380. /**
  381. * @brief COMP2 Initialization Function
  382. * @param None
  383. * @retval None
  384. */
  385. static void MX_COMP2_Init(void)
  386. {
  387. /* USER CODE BEGIN COMP2_Init 0 */
  388. /* USER CODE END COMP2_Init 0 */
  389. /* USER CODE BEGIN COMP2_Init 1 */
  390. /* USER CODE END COMP2_Init 1 */
  391. hcomp2.Instance = COMP2;
  392. hcomp2.Init.InvertingInput = COMP_INVERTINGINPUT_DAC2;
  393. hcomp2.Init.NonInvertingInput = COMP_NONINVERTINGINPUT_PB4;
  394. hcomp2.Init.Output = COMP_OUTPUT_NONE;
  395. hcomp2.Init.Mode = COMP_MODE_HIGHSPEED;
  396. hcomp2.Init.WindowMode = COMP_WINDOWMODE_DISABLE;
  397. hcomp2.Init.TriggerMode = COMP_TRIGGERMODE_IT_RISING;
  398. if (HAL_COMP_Init(&hcomp2) != HAL_OK)
  399. {
  400. Error_Handler();
  401. }
  402. /* USER CODE BEGIN COMP2_Init 2 */
  403. /* USER CODE END COMP2_Init 2 */
  404. }
  405. /**
  406. * @brief CRC Initialization Function
  407. * @param None
  408. * @retval None
  409. */
  410. static void MX_CRC_Init(void)
  411. {
  412. /* USER CODE BEGIN CRC_Init 0 */
  413. /* USER CODE END CRC_Init 0 */
  414. /* USER CODE BEGIN CRC_Init 1 */
  415. /* USER CODE END CRC_Init 1 */
  416. hcrc.Instance = CRC;
  417. if (HAL_CRC_Init(&hcrc) != HAL_OK)
  418. {
  419. Error_Handler();
  420. }
  421. /* USER CODE BEGIN CRC_Init 2 */
  422. /* USER CODE END CRC_Init 2 */
  423. }
  424. /**
  425. * @brief DAC Initialization Function
  426. * @param None
  427. * @retval None
  428. */
  429. static void MX_DAC_Init(void)
  430. {
  431. /* USER CODE BEGIN DAC_Init 0 */
  432. /* USER CODE END DAC_Init 0 */
  433. DAC_ChannelConfTypeDef sConfig = {0};
  434. /* USER CODE BEGIN DAC_Init 1 */
  435. /* USER CODE END DAC_Init 1 */
  436. /** DAC Initialization
  437. */
  438. hdac.Instance = DAC;
  439. if (HAL_DAC_Init(&hdac) != HAL_OK)
  440. {
  441. Error_Handler();
  442. }
  443. /** DAC channel OUT1 config
  444. */
  445. sConfig.DAC_Trigger = DAC_TRIGGER_NONE;
  446. sConfig.DAC_OutputBuffer = DAC_OUTPUTBUFFER_ENABLE;
  447. if (HAL_DAC_ConfigChannel(&hdac, &sConfig, DAC_CHANNEL_1) != HAL_OK)
  448. {
  449. Error_Handler();
  450. }
  451. /** DAC channel OUT2 config
  452. */
  453. sConfig.DAC_OutputBuffer = DAC_OUTPUTBUFFER_DISABLE;
  454. if (HAL_DAC_ConfigChannel(&hdac, &sConfig, DAC_CHANNEL_2) != HAL_OK)
  455. {
  456. Error_Handler();
  457. }
  458. /* USER CODE BEGIN DAC_Init 2 */
  459. /* USER CODE END DAC_Init 2 */
  460. }
  461. /**
  462. * @brief I2C1 Initialization Function
  463. * @param None
  464. * @retval None
  465. */
  466. static void MX_I2C1_Init(void)
  467. {
  468. /* USER CODE BEGIN I2C1_Init 0 */
  469. /* USER CODE END I2C1_Init 0 */
  470. /* USER CODE BEGIN I2C1_Init 1 */
  471. /* USER CODE END I2C1_Init 1 */
  472. hi2c1.Instance = I2C1;
  473. hi2c1.Init.ClockSpeed = 400000;
  474. hi2c1.Init.DutyCycle = I2C_DUTYCYCLE_2;
  475. hi2c1.Init.OwnAddress1 = 0;
  476. hi2c1.Init.AddressingMode = I2C_ADDRESSINGMODE_7BIT;
  477. hi2c1.Init.DualAddressMode = I2C_DUALADDRESS_DISABLE;
  478. hi2c1.Init.OwnAddress2 = 0;
  479. hi2c1.Init.GeneralCallMode = I2C_GENERALCALL_DISABLE;
  480. hi2c1.Init.NoStretchMode = I2C_NOSTRETCH_DISABLE;
  481. if (HAL_I2C_Init(&hi2c1) != HAL_OK)
  482. {
  483. Error_Handler();
  484. }
  485. /* USER CODE BEGIN I2C1_Init 2 */
  486. /* USER CODE END I2C1_Init 2 */
  487. }
  488. /**
  489. * @brief I2C2 Initialization Function
  490. * @param None
  491. * @retval None
  492. */
  493. static void MX_I2C2_Init(void)
  494. {
  495. /* USER CODE BEGIN I2C2_Init 0 */
  496. /* USER CODE END I2C2_Init 0 */
  497. /* USER CODE BEGIN I2C2_Init 1 */
  498. /* USER CODE END I2C2_Init 1 */
  499. hi2c2.Instance = I2C2;
  500. hi2c2.Init.ClockSpeed = 100000;
  501. hi2c2.Init.DutyCycle = I2C_DUTYCYCLE_2;
  502. hi2c2.Init.OwnAddress1 = 0;
  503. hi2c2.Init.AddressingMode = I2C_ADDRESSINGMODE_7BIT;
  504. hi2c2.Init.DualAddressMode = I2C_DUALADDRESS_DISABLE;
  505. hi2c2.Init.OwnAddress2 = 0;
  506. hi2c2.Init.GeneralCallMode = I2C_GENERALCALL_DISABLE;
  507. hi2c2.Init.NoStretchMode = I2C_NOSTRETCH_DISABLE;
  508. if (HAL_I2C_Init(&hi2c2) != HAL_OK)
  509. {
  510. Error_Handler();
  511. }
  512. /* USER CODE BEGIN I2C2_Init 2 */
  513. /* USER CODE END I2C2_Init 2 */
  514. }
  515. /**
  516. * @brief TIM2 Initialization Function
  517. * @param None
  518. * @retval None
  519. */
  520. static void MX_TIM2_Init(void)
  521. {
  522. /* USER CODE BEGIN TIM2_Init 0 */
  523. /* USER CODE END TIM2_Init 0 */
  524. TIM_ClockConfigTypeDef sClockSourceConfig = {0};
  525. TIM_MasterConfigTypeDef sMasterConfig = {0};
  526. /* USER CODE BEGIN TIM2_Init 1 */
  527. /* USER CODE END TIM2_Init 1 */
  528. htim2.Instance = TIM2;
  529. htim2.Init.Prescaler = 600;
  530. htim2.Init.CounterMode = TIM_COUNTERMODE_UP;
  531. htim2.Init.Period = 1000;
  532. htim2.Init.ClockDivision = TIM_CLOCKDIVISION_DIV1;
  533. htim2.Init.AutoReloadPreload = TIM_AUTORELOAD_PRELOAD_DISABLE;
  534. if (HAL_TIM_Base_Init(&htim2) != HAL_OK)
  535. {
  536. Error_Handler();
  537. }
  538. sClockSourceConfig.ClockSource = TIM_CLOCKSOURCE_INTERNAL;
  539. if (HAL_TIM_ConfigClockSource(&htim2, &sClockSourceConfig) != HAL_OK)
  540. {
  541. Error_Handler();
  542. }
  543. if (HAL_TIM_OnePulse_Init(&htim2, TIM_OPMODE_SINGLE) != HAL_OK)
  544. {
  545. Error_Handler();
  546. }
  547. sMasterConfig.MasterOutputTrigger = TIM_TRGO_ENABLE;
  548. sMasterConfig.MasterSlaveMode = TIM_MASTERSLAVEMODE_DISABLE;
  549. if (HAL_TIMEx_MasterConfigSynchronization(&htim2, &sMasterConfig) != HAL_OK)
  550. {
  551. Error_Handler();
  552. }
  553. /* USER CODE BEGIN TIM2_Init 2 */
  554. /* USER CODE END TIM2_Init 2 */
  555. }
  556. /**
  557. * @brief TIM3 Initialization Function
  558. * @param None
  559. * @retval None
  560. */
  561. static void MX_TIM3_Init(void)
  562. {
  563. /* USER CODE BEGIN TIM3_Init 0 */
  564. /* USER CODE END TIM3_Init 0 */
  565. TIM_ClockConfigTypeDef sClockSourceConfig = {0};
  566. TIM_SlaveConfigTypeDef sSlaveConfig = {0};
  567. TIM_MasterConfigTypeDef sMasterConfig = {0};
  568. TIM_OC_InitTypeDef sConfigOC = {0};
  569. /* USER CODE BEGIN TIM3_Init 1 */
  570. /* USER CODE END TIM3_Init 1 */
  571. htim3.Instance = TIM3;
  572. htim3.Init.Prescaler = 48;
  573. htim3.Init.CounterMode = TIM_COUNTERMODE_UP;
  574. htim3.Init.Period = 1000;
  575. htim3.Init.ClockDivision = TIM_CLOCKDIVISION_DIV1;
  576. htim3.Init.AutoReloadPreload = TIM_AUTORELOAD_PRELOAD_DISABLE;
  577. if (HAL_TIM_Base_Init(&htim3) != HAL_OK)
  578. {
  579. Error_Handler();
  580. }
  581. sClockSourceConfig.ClockSource = TIM_CLOCKSOURCE_INTERNAL;
  582. if (HAL_TIM_ConfigClockSource(&htim3, &sClockSourceConfig) != HAL_OK)
  583. {
  584. Error_Handler();
  585. }
  586. if (HAL_TIM_PWM_Init(&htim3) != HAL_OK)
  587. {
  588. Error_Handler();
  589. }
  590. sSlaveConfig.SlaveMode = TIM_SLAVEMODE_GATED;
  591. sSlaveConfig.InputTrigger = TIM_TS_ITR1;
  592. if (HAL_TIM_SlaveConfigSynchro(&htim3, &sSlaveConfig) != HAL_OK)
  593. {
  594. Error_Handler();
  595. }
  596. sMasterConfig.MasterOutputTrigger = TIM_TRGO_RESET;
  597. sMasterConfig.MasterSlaveMode = TIM_MASTERSLAVEMODE_DISABLE;
  598. if (HAL_TIMEx_MasterConfigSynchronization(&htim3, &sMasterConfig) != HAL_OK)
  599. {
  600. Error_Handler();
  601. }
  602. sConfigOC.OCMode = TIM_OCMODE_PWM1;
  603. sConfigOC.Pulse = 10;
  604. sConfigOC.OCPolarity = TIM_OCPOLARITY_HIGH;
  605. sConfigOC.OCFastMode = TIM_OCFAST_DISABLE;
  606. if (HAL_TIM_PWM_ConfigChannel(&htim3, &sConfigOC, TIM_CHANNEL_4) != HAL_OK)
  607. {
  608. Error_Handler();
  609. }
  610. /* USER CODE BEGIN TIM3_Init 2 */
  611. /* USER CODE END TIM3_Init 2 */
  612. HAL_TIM_MspPostInit(&htim3);
  613. }
  614. /**
  615. * @brief TIM4 Initialization Function
  616. * @param None
  617. * @retval None
  618. */
  619. static void MX_TIM4_Init(void)
  620. {
  621. /* USER CODE BEGIN TIM4_Init 0 */
  622. /* USER CODE END TIM4_Init 0 */
  623. TIM_ClockConfigTypeDef sClockSourceConfig = {0};
  624. TIM_MasterConfigTypeDef sMasterConfig = {0};
  625. /* USER CODE BEGIN TIM4_Init 1 */
  626. /* USER CODE END TIM4_Init 1 */
  627. htim4.Instance = TIM4;
  628. htim4.Init.Prescaler = 240;
  629. htim4.Init.CounterMode = TIM_COUNTERMODE_UP;
  630. htim4.Init.Period = 50000-1;
  631. htim4.Init.ClockDivision = TIM_CLOCKDIVISION_DIV1;
  632. htim4.Init.AutoReloadPreload = TIM_AUTORELOAD_PRELOAD_DISABLE;
  633. if (HAL_TIM_Base_Init(&htim4) != HAL_OK)
  634. {
  635. Error_Handler();
  636. }
  637. sClockSourceConfig.ClockSource = TIM_CLOCKSOURCE_INTERNAL;
  638. if (HAL_TIM_ConfigClockSource(&htim4, &sClockSourceConfig) != HAL_OK)
  639. {
  640. Error_Handler();
  641. }
  642. sMasterConfig.MasterOutputTrigger = TIM_TRGO_UPDATE;
  643. sMasterConfig.MasterSlaveMode = TIM_MASTERSLAVEMODE_DISABLE;
  644. if (HAL_TIMEx_MasterConfigSynchronization(&htim4, &sMasterConfig) != HAL_OK)
  645. {
  646. Error_Handler();
  647. }
  648. /* USER CODE BEGIN TIM4_Init 2 */
  649. /* USER CODE END TIM4_Init 2 */
  650. }
  651. /**
  652. * @brief TIM6 Initialization Function
  653. * @param None
  654. * @retval None
  655. */
  656. static void MX_TIM6_Init(void)
  657. {
  658. /* USER CODE BEGIN TIM6_Init 0 */
  659. /* USER CODE END TIM6_Init 0 */
  660. TIM_MasterConfigTypeDef sMasterConfig = {0};
  661. /* USER CODE BEGIN TIM6_Init 1 */
  662. /* USER CODE END TIM6_Init 1 */
  663. htim6.Instance = TIM6;
  664. htim6.Init.Prescaler = 600;
  665. htim6.Init.CounterMode = TIM_COUNTERMODE_UP;
  666. htim6.Init.Period = 20000;
  667. htim6.Init.AutoReloadPreload = TIM_AUTORELOAD_PRELOAD_DISABLE;
  668. if (HAL_TIM_Base_Init(&htim6) != HAL_OK)
  669. {
  670. Error_Handler();
  671. }
  672. sMasterConfig.MasterOutputTrigger = TIM_TRGO_UPDATE;
  673. sMasterConfig.MasterSlaveMode = TIM_MASTERSLAVEMODE_DISABLE;
  674. if (HAL_TIMEx_MasterConfigSynchronization(&htim6, &sMasterConfig) != HAL_OK)
  675. {
  676. Error_Handler();
  677. }
  678. /* USER CODE BEGIN TIM6_Init 2 */
  679. /* USER CODE END TIM6_Init 2 */
  680. }
  681. /**
  682. * @brief USART1 Initialization Function
  683. * @param None
  684. * @retval None
  685. */
  686. static void MX_USART1_UART_Init(void)
  687. {
  688. /* USER CODE BEGIN USART1_Init 0 */
  689. /* USER CODE END USART1_Init 0 */
  690. /* USER CODE BEGIN USART1_Init 1 */
  691. /* USER CODE END USART1_Init 1 */
  692. huart1.Instance = USART1;
  693. huart1.Init.BaudRate = 115200;
  694. huart1.Init.WordLength = UART_WORDLENGTH_8B;
  695. huart1.Init.StopBits = UART_STOPBITS_1;
  696. huart1.Init.Parity = UART_PARITY_NONE;
  697. huart1.Init.Mode = UART_MODE_TX_RX;
  698. huart1.Init.HwFlowCtl = UART_HWCONTROL_NONE;
  699. huart1.Init.OverSampling = UART_OVERSAMPLING_16;
  700. if (HAL_UART_Init(&huart1) != HAL_OK)
  701. {
  702. Error_Handler();
  703. }
  704. /* USER CODE BEGIN USART1_Init 2 */
  705. /* USER CODE END USART1_Init 2 */
  706. }
  707. /**
  708. * Enable DMA controller clock
  709. */
  710. static void MX_DMA_Init(void)
  711. {
  712. /* DMA controller clock enable */
  713. __HAL_RCC_DMA1_CLK_ENABLE();
  714. /* DMA interrupt init */
  715. /* DMA1_Channel1_IRQn interrupt configuration */
  716. HAL_NVIC_SetPriority(DMA1_Channel1_IRQn, 0, 0);
  717. HAL_NVIC_EnableIRQ(DMA1_Channel1_IRQn);
  718. /* DMA1_Channel4_IRQn interrupt configuration */
  719. HAL_NVIC_SetPriority(DMA1_Channel4_IRQn, 0, 0);
  720. HAL_NVIC_EnableIRQ(DMA1_Channel4_IRQn);
  721. /* DMA1_Channel5_IRQn interrupt configuration */
  722. HAL_NVIC_SetPriority(DMA1_Channel5_IRQn, 0, 0);
  723. HAL_NVIC_EnableIRQ(DMA1_Channel5_IRQn);
  724. }
  725. /**
  726. * @brief GPIO Initialization Function
  727. * @param None
  728. * @retval None
  729. */
  730. static void MX_GPIO_Init(void)
  731. {
  732. GPIO_InitTypeDef GPIO_InitStruct = {0};
  733. /* GPIO Ports Clock Enable */
  734. __HAL_RCC_GPIOC_CLK_ENABLE();
  735. __HAL_RCC_GPIOH_CLK_ENABLE();
  736. __HAL_RCC_GPIOA_CLK_ENABLE();
  737. __HAL_RCC_GPIOB_CLK_ENABLE();
  738. /*Configure GPIO pin Output Level */
  739. HAL_GPIO_WritePin(GPIOA, GPIO_PIN_7, GPIO_PIN_RESET);
  740. /*Configure GPIO pin Output Level */
  741. HAL_GPIO_WritePin(GPIOB, GPIO_PIN_0, GPIO_PIN_SET);
  742. /*Configure GPIO pins : PC13 PC14 PC15 */
  743. GPIO_InitStruct.Pin = GPIO_PIN_13|GPIO_PIN_14|GPIO_PIN_15;
  744. GPIO_InitStruct.Mode = GPIO_MODE_ANALOG;
  745. GPIO_InitStruct.Pull = GPIO_NOPULL;
  746. HAL_GPIO_Init(GPIOC, &GPIO_InitStruct);
  747. /*Configure GPIO pins : PA1 PA2 PA3 PA6
  748. PA8 PA15 */
  749. GPIO_InitStruct.Pin = GPIO_PIN_1|GPIO_PIN_2|GPIO_PIN_3|GPIO_PIN_6
  750. |GPIO_PIN_8|GPIO_PIN_15;
  751. GPIO_InitStruct.Mode = GPIO_MODE_ANALOG;
  752. GPIO_InitStruct.Pull = GPIO_NOPULL;
  753. HAL_GPIO_Init(GPIOA, &GPIO_InitStruct);
  754. /*Configure GPIO pin : PA7 */
  755. GPIO_InitStruct.Pin = GPIO_PIN_7;
  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(GPIOA, &GPIO_InitStruct);
  760. /*Configure GPIO pin : PB0 */
  761. GPIO_InitStruct.Pin = GPIO_PIN_0;
  762. GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP;
  763. GPIO_InitStruct.Pull = GPIO_NOPULL;
  764. GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW;
  765. HAL_GPIO_Init(GPIOB, &GPIO_InitStruct);
  766. /*Configure GPIO pins : PB2 PB12 PB13 PB3
  767. PB5 PB8 PB9 */
  768. GPIO_InitStruct.Pin = GPIO_PIN_2|GPIO_PIN_12|GPIO_PIN_13|GPIO_PIN_3
  769. |GPIO_PIN_5|GPIO_PIN_8|GPIO_PIN_9;
  770. GPIO_InitStruct.Mode = GPIO_MODE_ANALOG;
  771. GPIO_InitStruct.Pull = GPIO_NOPULL;
  772. HAL_GPIO_Init(GPIOB, &GPIO_InitStruct);
  773. /*Configure GPIO pins : PB14 PB15 */
  774. GPIO_InitStruct.Pin = GPIO_PIN_14|GPIO_PIN_15;
  775. GPIO_InitStruct.Mode = GPIO_MODE_IT_FALLING;
  776. GPIO_InitStruct.Pull = GPIO_PULLUP;
  777. HAL_GPIO_Init(GPIOB, &GPIO_InitStruct);
  778. /* EXTI interrupt init*/
  779. HAL_NVIC_SetPriority(EXTI15_10_IRQn, 0, 0);
  780. HAL_NVIC_EnableIRQ(EXTI15_10_IRQn);
  781. }
  782. /* USER CODE BEGIN 4 */
  783. /* USER CODE END 4 */
  784. /**
  785. * @brief This function is executed in case of error occurrence.
  786. * @retval None
  787. */
  788. void Error_Handler(void)
  789. {
  790. /* USER CODE BEGIN Error_Handler_Debug */
  791. /* User can add his own implementation to report the HAL error return state */
  792. __disable_irq();
  793. while (1)
  794. {
  795. }
  796. /* USER CODE END Error_Handler_Debug */
  797. }
  798. #ifdef USE_FULL_ASSERT
  799. /**
  800. * @brief Reports the name of the source file and the source line number
  801. * where the assert_param error has occurred.
  802. * @param file: pointer to the source file name
  803. * @param line: assert_param error line source number
  804. * @retval None
  805. */
  806. void assert_failed(uint8_t *file, uint32_t line)
  807. {
  808. /* USER CODE BEGIN 6 */
  809. /* User can add his own implementation to report the file name and line number,
  810. ex: printf("Wrong parameters value: file %s on line %d\r\n", file, line) */
  811. /* USER CODE END 6 */
  812. }
  813. #endif /* USE_FULL_ASSERT */