main.c 28 KB

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