main.c 26 KB

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