main.c 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044
  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 <stdio.h>
  27. /* USER CODE END Includes */
  28. /* Private typedef -----------------------------------------------------------*/
  29. /* USER CODE BEGIN PTD */
  30. /* USER CODE END PTD */
  31. /* Private define ------------------------------------------------------------*/
  32. /* USER CODE BEGIN PD */
  33. /* USER CODE END PD */
  34. /* Private macro -------------------------------------------------------------*/
  35. /* USER CODE BEGIN PM */
  36. /* USER CODE END PM */
  37. /* Private variables ---------------------------------------------------------*/
  38. ADC_HandleTypeDef hadc1;
  39. ADC_HandleTypeDef hadc2;
  40. DMA_HandleTypeDef hdma_adc1;
  41. CRC_HandleTypeDef hcrc;
  42. DAC_HandleTypeDef hdac;
  43. I2C_HandleTypeDef hi2c1;
  44. RTC_HandleTypeDef hrtc;
  45. SPI_HandleTypeDef hspi1;
  46. SPI_HandleTypeDef hspi2;
  47. TIM_HandleTypeDef htim2;
  48. TIM_HandleTypeDef htim3;
  49. TIM_HandleTypeDef htim4;
  50. /* USER CODE BEGIN PV */
  51. volatile struct FlagsStr {
  52. uint8_t Adc1_CC;
  53. uint8_t Adc2_CC;
  54. uint8_t Tim3_PE;
  55. uint8_t Ext;
  56. } Flags;
  57. uint32_t raw_adc[4];
  58. uint32_t VccVal_mV;
  59. volatile uint32_t ticks;
  60. /* USER CODE END PV */
  61. /* Private function prototypes -----------------------------------------------*/
  62. void SystemClock_Config(void);
  63. static void MX_GPIO_Init(void);
  64. static void MX_DMA_Init(void);
  65. static void MX_ADC1_Init(void);
  66. static void MX_ADC2_Init(void);
  67. static void MX_DAC_Init(void);
  68. static void MX_I2C1_Init(void);
  69. static void MX_RTC_Init(void);
  70. static void MX_SPI1_Init(void);
  71. static void MX_SPI2_Init(void);
  72. static void MX_TIM2_Init(void);
  73. static void MX_TIM3_Init(void);
  74. static void MX_CRC_Init(void);
  75. static void MX_TIM4_Init(void);
  76. /* USER CODE BEGIN PFP */
  77. /* USER CODE END PFP */
  78. /* Private user code ---------------------------------------------------------*/
  79. /* USER CODE BEGIN 0 */
  80. int _write(int fd, char* ptr, int len)
  81. {
  82. CDC_Transmit_FS((uint8_t*)ptr, len);
  83. return len;
  84. }
  85. void HAL_GPIO_EXTI_Callback(uint16_t GPIO_Pin)
  86. {
  87. HAL_NVIC_DisableIRQ(EXTI1_IRQn);
  88. HAL_NVIC_ClearPendingIRQ(EXTI1_IRQn);
  89. Flags.Ext = 1;
  90. }
  91. void HAL_ADC_ConvCpltCallback(ADC_HandleTypeDef* hadc)
  92. {
  93. if(hadc->Instance == ADC1)
  94. {
  95. HAL_ADC_Stop_DMA(hadc);
  96. Flags.Adc1_CC = 1;
  97. }
  98. if(hadc->Instance == ADC2)
  99. {
  100. HAL_ADC_Stop_IT(hadc);
  101. Flags.Adc2_CC = 1;
  102. }
  103. }
  104. void HAL_TIM_PeriodElapsedCallback(TIM_HandleTypeDef* htim)
  105. {
  106. if(htim->Instance == TIM3)
  107. {
  108. Flags.Tim3_PE = 1;
  109. ticks++;
  110. }
  111. if(htim->Instance == TIM4)
  112. {
  113. LED_TurnOff(LED_BLUE);
  114. HAL_TIM_Base_Stop_IT(&htim4);
  115. }
  116. }
  117. void LED_TurnOn(uint8_t color)
  118. {
  119. switch(color)
  120. {
  121. case LED_BLUE:
  122. HAL_GPIO_WritePin(GPIOB, GPIO_PIN_6, GPIO_PIN_SET);
  123. HAL_GPIO_TogglePin(GPIOA, GPIO_PIN_10); // !!! DEBUG !!!
  124. break;
  125. case LED_GREEN:
  126. HAL_GPIO_WritePin(GPIOB, GPIO_PIN_7, GPIO_PIN_SET);
  127. break;
  128. case LED_YELLOW:
  129. HAL_GPIO_WritePin(GPIOC, GPIO_PIN_13, GPIO_PIN_SET);
  130. break;
  131. }
  132. }
  133. void LED_TurnOff(uint8_t color)
  134. {
  135. switch(color)
  136. {
  137. case LED_BLUE:
  138. HAL_GPIO_WritePin(GPIOB, GPIO_PIN_6, GPIO_PIN_RESET);
  139. break;
  140. case LED_GREEN:
  141. HAL_GPIO_WritePin(GPIOB, GPIO_PIN_7, GPIO_PIN_RESET);
  142. break;
  143. case LED_YELLOW:
  144. HAL_GPIO_WritePin(GPIOC, GPIO_PIN_13, GPIO_PIN_RESET);
  145. break;
  146. }
  147. }
  148. void LED_Toggle(uint8_t color)
  149. {
  150. switch(color)
  151. {
  152. case LED_BLUE:
  153. HAL_GPIO_TogglePin(GPIOB, GPIO_PIN_6);
  154. break;
  155. case LED_GREEN:
  156. HAL_GPIO_TogglePin(GPIOB, GPIO_PIN_7);
  157. break;
  158. case LED_YELLOW:
  159. HAL_GPIO_TogglePin(GPIOC, GPIO_PIN_13);
  160. break;
  161. }
  162. }
  163. HAL_StatusTypeDef LM73_GetTemp(uint32_t *temp)
  164. {
  165. uint8_t rbytes[2] = { 0, 0 };
  166. HAL_StatusTypeDef status;
  167. status = HAL_I2C_Master_Transmit(&hi2c1, LM73_I2C_ADDR, &rbytes[0], 1, 10);
  168. if(status == HAL_OK)
  169. {
  170. status = HAL_I2C_Master_Receive(&hi2c1, LM73_I2C_ADDR, &rbytes[0], 2, 10);
  171. }
  172. if(status == HAL_OK)
  173. {
  174. *temp = ((((uint16_t)rbytes[0] << 8) | (uint16_t)rbytes[1]) >> 5) * 25UL;
  175. return HAL_OK;
  176. }
  177. else
  178. {
  179. return status;
  180. }
  181. }
  182. void SetSIPMVoltage(uint32_t voltage)
  183. {
  184. voltage = voltage / 11; // OpAmp gain = 11;
  185. uint32_t dac_val = (voltage * 4095UL) / VccVal_mV;
  186. HAL_DAC_SetValue(&hdac, DAC_CHANNEL_1, DAC_ALIGN_12B_R, dac_val);
  187. }
  188. void SetThresholdVoltage(uint32_t voltage)
  189. {
  190. uint32_t dac_val = (voltage * 4095UL) / VccVal_mV;
  191. HAL_DAC_SetValue(&hdac, DAC_CHANNEL_2, DAC_ALIGN_12B_R, dac_val);
  192. }
  193. /* USER CODE END 0 */
  194. /**
  195. * @brief The application entry point.
  196. * @retval int
  197. */
  198. int main(void)
  199. {
  200. /* USER CODE BEGIN 1 */
  201. char tempstr[32];
  202. uint8_t counts = 0;
  203. uint8_t spectrum[4096];
  204. uint32_t temperature;
  205. /* USER CODE END 1 */
  206. /* MCU Configuration--------------------------------------------------------*/
  207. /* Reset of all peripherals, Initializes the Flash interface and the Systick. */
  208. HAL_Init();
  209. /* USER CODE BEGIN Init */
  210. /* USER CODE END Init */
  211. /* Configure the system clock */
  212. SystemClock_Config();
  213. /* USER CODE BEGIN SysInit */
  214. /* USER CODE END SysInit */
  215. /* Initialize all configured peripherals */
  216. MX_GPIO_Init();
  217. MX_DMA_Init();
  218. MX_ADC1_Init();
  219. MX_ADC2_Init();
  220. MX_DAC_Init();
  221. MX_I2C1_Init();
  222. MX_RTC_Init();
  223. MX_SPI1_Init();
  224. MX_SPI2_Init();
  225. MX_USB_DEVICE_Init();
  226. MX_TIM2_Init();
  227. MX_TIM3_Init();
  228. MX_CRC_Init();
  229. MX_TIM4_Init();
  230. /* USER CODE BEGIN 2 */
  231. ST7735_Init();
  232. ST7735_FillScreen(ST7735_BLACK);
  233. ST7735_WriteString(0, 0, "Display ready", Font_7x10, ST7735_GREEN, ST7735_BLACK);
  234. /* USER CODE END 2 */
  235. /* Infinite loop */
  236. /* USER CODE BEGIN WHILE */
  237. HAL_TIM_Base_Start_IT(&htim3);
  238. HAL_DAC_Start(&hdac, DAC_CHANNEL_1);
  239. HAL_DAC_Start(&hdac, DAC_CHANNEL_2);
  240. HAL_DAC_SetValue(&hdac, DAC_CHANNEL_1, DAC_ALIGN_12B_R, 0);
  241. HAL_DAC_SetValue(&hdac, DAC_CHANNEL_2, DAC_ALIGN_12B_R, 0);
  242. EnableHV();
  243. HAL_Delay(100);
  244. HAL_ADC_Start_DMA(&hadc1, raw_adc, 1);
  245. while(Flags.Adc1_CC == 0);
  246. HAL_ADC_Start_DMA(&hadc1, raw_adc, 1);
  247. VccVal_mV = *VREFINT_CAL_ADDR * VREFINT_CAL_VREF / raw_adc[0];
  248. SetSIPMVoltage(SIPM_VBR + 4300);
  249. SetThresholdVoltage(50);
  250. while (1)
  251. {
  252. if(Flags.Adc1_CC)
  253. {
  254. Flags.Adc1_CC = 0;
  255. VccVal_mV = *VREFINT_CAL_ADDR * VREFINT_CAL_VREF / raw_adc[0];
  256. HAL_ADC_Start_DMA(&hadc1, raw_adc, 1);
  257. }
  258. if(Flags.Ext)
  259. {
  260. Flags.Ext = 0;
  261. LED_TurnOn(LED_BLUE);
  262. HAL_Delay(10);
  263. LED_TurnOff(LED_BLUE);
  264. HAL_NVIC_ClearPendingIRQ(EXTI1_IRQn);
  265. HAL_NVIC_EnableIRQ(EXTI1_IRQn);
  266. /*if(HAL_ADC_GetState(&hadc2) & HAL_ADC_STATE_READY)
  267. HAL_ADC_Start_IT(&hadc2);*/
  268. }
  269. /*
  270. if(Flags.Adc2_CC)
  271. {
  272. Flags.Adc2_CC = 0;
  273. uint32_t val = HAL_ADC_GetValue(&hadc2);
  274. spectrum[val]++;
  275. counts++;
  276. ResetPeakDetector();
  277. printf("%lu\r\n", val);
  278. SetPeakDetector();
  279. HAL_Delay(1);
  280. LED_TurnOff(LED_BLUE);
  281. HAL_NVIC_ClearPendingIRQ(EXTI1_IRQn);
  282. HAL_NVIC_EnableIRQ(EXTI1_IRQn);
  283. }
  284. */
  285. if(Flags.Tim3_PE)
  286. {
  287. Flags.Tim3_PE = 0;
  288. LM73_GetTemp(&temperature);
  289. sprintf(tempstr, "Temp: - %06.2f -", (float)temperature / 100.0);
  290. ST7735_WriteString(0, 10, tempstr, Font_7x10, ST7735_CYAN, ST7735_BLACK);
  291. sprintf(tempstr, "Vcc: - %04.2f -", (float)VccVal_mV / 1000.0);
  292. ST7735_WriteString(0, 20, tempstr, Font_7x10, ST7735_RED, ST7735_BLACK);
  293. if(ticks % 10 == 0)
  294. {
  295. sprintf(tempstr, "- %.3u -", counts);
  296. ST7735_WriteString(0, 50, tempstr, Font_16x26, ST7735_RED, ST7735_BLACK);
  297. sprintf(tempstr, "- %lX -", HAL_ADC_GetState(&hadc2));
  298. ST7735_WriteString(0, 80, tempstr, Font_7x10, ST7735_RED, ST7735_BLACK);
  299. counts = 0;
  300. }
  301. }
  302. /* USER CODE END WHILE */
  303. /* USER CODE BEGIN 3 */
  304. }
  305. /* USER CODE END 3 */
  306. }
  307. /**
  308. * @brief System Clock Configuration
  309. * @retval None
  310. */
  311. void SystemClock_Config(void)
  312. {
  313. RCC_OscInitTypeDef RCC_OscInitStruct = {0};
  314. RCC_ClkInitTypeDef RCC_ClkInitStruct = {0};
  315. /** Configure the main internal regulator output voltage
  316. */
  317. __HAL_RCC_PWR_CLK_ENABLE();
  318. __HAL_PWR_VOLTAGESCALING_CONFIG(PWR_REGULATOR_VOLTAGE_SCALE3);
  319. /** Initializes the RCC Oscillators according to the specified parameters
  320. * in the RCC_OscInitTypeDef structure.
  321. */
  322. RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_HSE|RCC_OSCILLATORTYPE_LSE;
  323. RCC_OscInitStruct.HSEState = RCC_HSE_ON;
  324. RCC_OscInitStruct.LSEState = RCC_LSE_ON;
  325. RCC_OscInitStruct.PLL.PLLState = RCC_PLL_ON;
  326. RCC_OscInitStruct.PLL.PLLSource = RCC_PLLSOURCE_HSE;
  327. RCC_OscInitStruct.PLL.PLLM = 4;
  328. RCC_OscInitStruct.PLL.PLLN = 80;
  329. RCC_OscInitStruct.PLL.PLLP = RCC_PLLP_DIV2;
  330. RCC_OscInitStruct.PLL.PLLQ = 2;
  331. RCC_OscInitStruct.PLL.PLLR = 2;
  332. if (HAL_RCC_OscConfig(&RCC_OscInitStruct) != HAL_OK)
  333. {
  334. Error_Handler();
  335. }
  336. /** Initializes the CPU, AHB and APB buses clocks
  337. */
  338. RCC_ClkInitStruct.ClockType = RCC_CLOCKTYPE_HCLK|RCC_CLOCKTYPE_SYSCLK
  339. |RCC_CLOCKTYPE_PCLK1|RCC_CLOCKTYPE_PCLK2;
  340. RCC_ClkInitStruct.SYSCLKSource = RCC_SYSCLKSOURCE_PLLCLK;
  341. RCC_ClkInitStruct.AHBCLKDivider = RCC_SYSCLK_DIV1;
  342. RCC_ClkInitStruct.APB1CLKDivider = RCC_HCLK_DIV4;
  343. RCC_ClkInitStruct.APB2CLKDivider = RCC_HCLK_DIV2;
  344. if (HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_2) != HAL_OK)
  345. {
  346. Error_Handler();
  347. }
  348. }
  349. /**
  350. * @brief ADC1 Initialization Function
  351. * @param None
  352. * @retval None
  353. */
  354. static void MX_ADC1_Init(void)
  355. {
  356. /* USER CODE BEGIN ADC1_Init 0 */
  357. /* USER CODE END ADC1_Init 0 */
  358. ADC_ChannelConfTypeDef sConfig = {0};
  359. /* USER CODE BEGIN ADC1_Init 1 */
  360. /* USER CODE END ADC1_Init 1 */
  361. /** Configure the global features of the ADC (Clock, Resolution, Data Alignment and number of conversion)
  362. */
  363. hadc1.Instance = ADC1;
  364. hadc1.Init.ClockPrescaler = ADC_CLOCK_SYNC_PCLK_DIV4;
  365. hadc1.Init.Resolution = ADC_RESOLUTION_12B;
  366. hadc1.Init.ScanConvMode = DISABLE;
  367. hadc1.Init.ContinuousConvMode = DISABLE;
  368. hadc1.Init.DiscontinuousConvMode = DISABLE;
  369. hadc1.Init.ExternalTrigConvEdge = ADC_EXTERNALTRIGCONVEDGE_NONE;
  370. hadc1.Init.ExternalTrigConv = ADC_SOFTWARE_START;
  371. hadc1.Init.DataAlign = ADC_DATAALIGN_RIGHT;
  372. hadc1.Init.NbrOfConversion = 1;
  373. hadc1.Init.DMAContinuousRequests = DISABLE;
  374. hadc1.Init.EOCSelection = ADC_EOC_SINGLE_CONV;
  375. if (HAL_ADC_Init(&hadc1) != HAL_OK)
  376. {
  377. Error_Handler();
  378. }
  379. /** Configure for the selected ADC regular channel its corresponding rank in the sequencer and its sample time.
  380. */
  381. sConfig.Channel = ADC_CHANNEL_VREFINT;
  382. sConfig.Rank = 1;
  383. sConfig.SamplingTime = ADC_SAMPLETIME_480CYCLES;
  384. if (HAL_ADC_ConfigChannel(&hadc1, &sConfig) != HAL_OK)
  385. {
  386. Error_Handler();
  387. }
  388. /* USER CODE BEGIN ADC1_Init 2 */
  389. /* USER CODE END ADC1_Init 2 */
  390. }
  391. /**
  392. * @brief ADC2 Initialization Function
  393. * @param None
  394. * @retval None
  395. */
  396. static void MX_ADC2_Init(void)
  397. {
  398. /* USER CODE BEGIN ADC2_Init 0 */
  399. /* USER CODE END ADC2_Init 0 */
  400. ADC_ChannelConfTypeDef sConfig = {0};
  401. /* USER CODE BEGIN ADC2_Init 1 */
  402. /* USER CODE END ADC2_Init 1 */
  403. /** Configure the global features of the ADC (Clock, Resolution, Data Alignment and number of conversion)
  404. */
  405. hadc2.Instance = ADC2;
  406. hadc2.Init.ClockPrescaler = ADC_CLOCK_SYNC_PCLK_DIV4;
  407. hadc2.Init.Resolution = ADC_RESOLUTION_12B;
  408. hadc2.Init.ScanConvMode = DISABLE;
  409. hadc2.Init.ContinuousConvMode = DISABLE;
  410. hadc2.Init.DiscontinuousConvMode = DISABLE;
  411. hadc2.Init.ExternalTrigConvEdge = ADC_EXTERNALTRIGCONVEDGE_NONE;
  412. hadc2.Init.ExternalTrigConv = ADC_SOFTWARE_START;
  413. hadc2.Init.DataAlign = ADC_DATAALIGN_RIGHT;
  414. hadc2.Init.NbrOfConversion = 1;
  415. hadc2.Init.DMAContinuousRequests = DISABLE;
  416. hadc2.Init.EOCSelection = ADC_EOC_SINGLE_CONV;
  417. if (HAL_ADC_Init(&hadc2) != HAL_OK)
  418. {
  419. Error_Handler();
  420. }
  421. /** Configure for the selected ADC regular channel its corresponding rank in the sequencer and its sample time.
  422. */
  423. sConfig.Channel = ADC_CHANNEL_14;
  424. sConfig.Rank = 1;
  425. sConfig.SamplingTime = ADC_SAMPLETIME_480CYCLES;
  426. if (HAL_ADC_ConfigChannel(&hadc2, &sConfig) != HAL_OK)
  427. {
  428. Error_Handler();
  429. }
  430. /* USER CODE BEGIN ADC2_Init 2 */
  431. /* USER CODE END ADC2_Init 2 */
  432. }
  433. /**
  434. * @brief CRC Initialization Function
  435. * @param None
  436. * @retval None
  437. */
  438. static void MX_CRC_Init(void)
  439. {
  440. /* USER CODE BEGIN CRC_Init 0 */
  441. /* USER CODE END CRC_Init 0 */
  442. /* USER CODE BEGIN CRC_Init 1 */
  443. /* USER CODE END CRC_Init 1 */
  444. hcrc.Instance = CRC;
  445. if (HAL_CRC_Init(&hcrc) != HAL_OK)
  446. {
  447. Error_Handler();
  448. }
  449. /* USER CODE BEGIN CRC_Init 2 */
  450. /* USER CODE END CRC_Init 2 */
  451. }
  452. /**
  453. * @brief DAC Initialization Function
  454. * @param None
  455. * @retval None
  456. */
  457. static void MX_DAC_Init(void)
  458. {
  459. /* USER CODE BEGIN DAC_Init 0 */
  460. /* USER CODE END DAC_Init 0 */
  461. DAC_ChannelConfTypeDef sConfig = {0};
  462. /* USER CODE BEGIN DAC_Init 1 */
  463. /* USER CODE END DAC_Init 1 */
  464. /** DAC Initialization
  465. */
  466. hdac.Instance = DAC;
  467. if (HAL_DAC_Init(&hdac) != HAL_OK)
  468. {
  469. Error_Handler();
  470. }
  471. /** DAC channel OUT1 config
  472. */
  473. sConfig.DAC_Trigger = DAC_TRIGGER_NONE;
  474. sConfig.DAC_OutputBuffer = DAC_OUTPUTBUFFER_DISABLE;
  475. if (HAL_DAC_ConfigChannel(&hdac, &sConfig, DAC_CHANNEL_1) != HAL_OK)
  476. {
  477. Error_Handler();
  478. }
  479. /** DAC channel OUT2 config
  480. */
  481. if (HAL_DAC_ConfigChannel(&hdac, &sConfig, DAC_CHANNEL_2) != HAL_OK)
  482. {
  483. Error_Handler();
  484. }
  485. /* USER CODE BEGIN DAC_Init 2 */
  486. /* USER CODE END DAC_Init 2 */
  487. }
  488. /**
  489. * @brief I2C1 Initialization Function
  490. * @param None
  491. * @retval None
  492. */
  493. static void MX_I2C1_Init(void)
  494. {
  495. /* USER CODE BEGIN I2C1_Init 0 */
  496. /* USER CODE END I2C1_Init 0 */
  497. /* USER CODE BEGIN I2C1_Init 1 */
  498. /* USER CODE END I2C1_Init 1 */
  499. hi2c1.Instance = I2C1;
  500. hi2c1.Init.ClockSpeed = 50000;
  501. hi2c1.Init.DutyCycle = I2C_DUTYCYCLE_2;
  502. hi2c1.Init.OwnAddress1 = 0;
  503. hi2c1.Init.AddressingMode = I2C_ADDRESSINGMODE_7BIT;
  504. hi2c1.Init.DualAddressMode = I2C_DUALADDRESS_DISABLE;
  505. hi2c1.Init.OwnAddress2 = 0;
  506. hi2c1.Init.GeneralCallMode = I2C_GENERALCALL_DISABLE;
  507. hi2c1.Init.NoStretchMode = I2C_NOSTRETCH_DISABLE;
  508. if (HAL_I2C_Init(&hi2c1) != HAL_OK)
  509. {
  510. Error_Handler();
  511. }
  512. /* USER CODE BEGIN I2C1_Init 2 */
  513. /* USER CODE END I2C1_Init 2 */
  514. }
  515. /**
  516. * @brief RTC Initialization Function
  517. * @param None
  518. * @retval None
  519. */
  520. static void MX_RTC_Init(void)
  521. {
  522. /* USER CODE BEGIN RTC_Init 0 */
  523. /* USER CODE END RTC_Init 0 */
  524. RTC_TimeTypeDef sTime = {0};
  525. RTC_DateTypeDef sDate = {0};
  526. /* USER CODE BEGIN RTC_Init 1 */
  527. /* USER CODE END RTC_Init 1 */
  528. /** Initialize RTC Only
  529. */
  530. hrtc.Instance = RTC;
  531. hrtc.Init.HourFormat = RTC_HOURFORMAT_24;
  532. hrtc.Init.AsynchPrediv = 127;
  533. hrtc.Init.SynchPrediv = 255;
  534. hrtc.Init.OutPut = RTC_OUTPUT_DISABLE;
  535. hrtc.Init.OutPutPolarity = RTC_OUTPUT_POLARITY_HIGH;
  536. hrtc.Init.OutPutType = RTC_OUTPUT_TYPE_OPENDRAIN;
  537. if (HAL_RTC_Init(&hrtc) != HAL_OK)
  538. {
  539. Error_Handler();
  540. }
  541. /* USER CODE BEGIN Check_RTC_BKUP */
  542. /* USER CODE END Check_RTC_BKUP */
  543. /** Initialize RTC and set the Time and Date
  544. */
  545. sTime.Hours = 0x0;
  546. sTime.Minutes = 0x0;
  547. sTime.Seconds = 0x0;
  548. sTime.DayLightSaving = RTC_DAYLIGHTSAVING_NONE;
  549. sTime.StoreOperation = RTC_STOREOPERATION_RESET;
  550. if (HAL_RTC_SetTime(&hrtc, &sTime, RTC_FORMAT_BCD) != HAL_OK)
  551. {
  552. Error_Handler();
  553. }
  554. sDate.WeekDay = RTC_WEEKDAY_MONDAY;
  555. sDate.Month = RTC_MONTH_JANUARY;
  556. sDate.Date = 0x1;
  557. sDate.Year = 0x0;
  558. if (HAL_RTC_SetDate(&hrtc, &sDate, RTC_FORMAT_BCD) != HAL_OK)
  559. {
  560. Error_Handler();
  561. }
  562. /* USER CODE BEGIN RTC_Init 2 */
  563. /* USER CODE END RTC_Init 2 */
  564. }
  565. /**
  566. * @brief SPI1 Initialization Function
  567. * @param None
  568. * @retval None
  569. */
  570. static void MX_SPI1_Init(void)
  571. {
  572. /* USER CODE BEGIN SPI1_Init 0 */
  573. /* USER CODE END SPI1_Init 0 */
  574. /* USER CODE BEGIN SPI1_Init 1 */
  575. /* USER CODE END SPI1_Init 1 */
  576. /* SPI1 parameter configuration*/
  577. hspi1.Instance = SPI1;
  578. hspi1.Init.Mode = SPI_MODE_MASTER;
  579. hspi1.Init.Direction = SPI_DIRECTION_2LINES;
  580. hspi1.Init.DataSize = SPI_DATASIZE_8BIT;
  581. hspi1.Init.CLKPolarity = SPI_POLARITY_LOW;
  582. hspi1.Init.CLKPhase = SPI_PHASE_1EDGE;
  583. hspi1.Init.NSS = SPI_NSS_SOFT;
  584. hspi1.Init.BaudRatePrescaler = SPI_BAUDRATEPRESCALER_2;
  585. hspi1.Init.FirstBit = SPI_FIRSTBIT_MSB;
  586. hspi1.Init.TIMode = SPI_TIMODE_DISABLE;
  587. hspi1.Init.CRCCalculation = SPI_CRCCALCULATION_DISABLE;
  588. hspi1.Init.CRCPolynomial = 10;
  589. if (HAL_SPI_Init(&hspi1) != HAL_OK)
  590. {
  591. Error_Handler();
  592. }
  593. /* USER CODE BEGIN SPI1_Init 2 */
  594. /* USER CODE END SPI1_Init 2 */
  595. }
  596. /**
  597. * @brief SPI2 Initialization Function
  598. * @param None
  599. * @retval None
  600. */
  601. static void MX_SPI2_Init(void)
  602. {
  603. /* USER CODE BEGIN SPI2_Init 0 */
  604. /* USER CODE END SPI2_Init 0 */
  605. /* USER CODE BEGIN SPI2_Init 1 */
  606. /* USER CODE END SPI2_Init 1 */
  607. /* SPI2 parameter configuration*/
  608. hspi2.Instance = SPI2;
  609. hspi2.Init.Mode = SPI_MODE_MASTER;
  610. hspi2.Init.Direction = SPI_DIRECTION_2LINES;
  611. hspi2.Init.DataSize = SPI_DATASIZE_8BIT;
  612. hspi2.Init.CLKPolarity = SPI_POLARITY_LOW;
  613. hspi2.Init.CLKPhase = SPI_PHASE_1EDGE;
  614. hspi2.Init.NSS = SPI_NSS_SOFT;
  615. hspi2.Init.BaudRatePrescaler = SPI_BAUDRATEPRESCALER_2;
  616. hspi2.Init.FirstBit = SPI_FIRSTBIT_MSB;
  617. hspi2.Init.TIMode = SPI_TIMODE_DISABLE;
  618. hspi2.Init.CRCCalculation = SPI_CRCCALCULATION_DISABLE;
  619. hspi2.Init.CRCPolynomial = 10;
  620. if (HAL_SPI_Init(&hspi2) != HAL_OK)
  621. {
  622. Error_Handler();
  623. }
  624. /* USER CODE BEGIN SPI2_Init 2 */
  625. /* USER CODE END SPI2_Init 2 */
  626. }
  627. /**
  628. * @brief TIM2 Initialization Function
  629. * @param None
  630. * @retval None
  631. */
  632. static void MX_TIM2_Init(void)
  633. {
  634. /* USER CODE BEGIN TIM2_Init 0 */
  635. /* USER CODE END TIM2_Init 0 */
  636. TIM_SlaveConfigTypeDef sSlaveConfig = {0};
  637. TIM_MasterConfigTypeDef sMasterConfig = {0};
  638. /* USER CODE BEGIN TIM2_Init 1 */
  639. /* USER CODE END TIM2_Init 1 */
  640. htim2.Instance = TIM2;
  641. htim2.Init.Prescaler = 0;
  642. htim2.Init.CounterMode = TIM_COUNTERMODE_UP;
  643. htim2.Init.Period = 100;
  644. htim2.Init.ClockDivision = TIM_CLOCKDIVISION_DIV1;
  645. htim2.Init.AutoReloadPreload = TIM_AUTORELOAD_PRELOAD_DISABLE;
  646. if (HAL_TIM_Base_Init(&htim2) != HAL_OK)
  647. {
  648. Error_Handler();
  649. }
  650. sSlaveConfig.SlaveMode = TIM_SLAVEMODE_EXTERNAL1;
  651. sSlaveConfig.InputTrigger = TIM_TS_ETRF;
  652. sSlaveConfig.TriggerPolarity = TIM_TRIGGERPOLARITY_NONINVERTED;
  653. sSlaveConfig.TriggerPrescaler = TIM_TRIGGERPRESCALER_DIV1;
  654. sSlaveConfig.TriggerFilter = 0;
  655. if (HAL_TIM_SlaveConfigSynchro(&htim2, &sSlaveConfig) != HAL_OK)
  656. {
  657. Error_Handler();
  658. }
  659. sMasterConfig.MasterOutputTrigger = TIM_TRGO_UPDATE;
  660. sMasterConfig.MasterSlaveMode = TIM_MASTERSLAVEMODE_DISABLE;
  661. if (HAL_TIMEx_MasterConfigSynchronization(&htim2, &sMasterConfig) != HAL_OK)
  662. {
  663. Error_Handler();
  664. }
  665. /* USER CODE BEGIN TIM2_Init 2 */
  666. /* USER CODE END TIM2_Init 2 */
  667. }
  668. /**
  669. * @brief TIM3 Initialization Function
  670. * @param None
  671. * @retval None
  672. */
  673. static void MX_TIM3_Init(void)
  674. {
  675. /* USER CODE BEGIN TIM3_Init 0 */
  676. /* USER CODE END TIM3_Init 0 */
  677. TIM_ClockConfigTypeDef sClockSourceConfig = {0};
  678. TIM_MasterConfigTypeDef sMasterConfig = {0};
  679. /* USER CODE BEGIN TIM3_Init 1 */
  680. /* USER CODE END TIM3_Init 1 */
  681. htim3.Instance = TIM3;
  682. htim3.Init.Prescaler = 8000;
  683. htim3.Init.CounterMode = TIM_COUNTERMODE_DOWN;
  684. htim3.Init.Period = 1000;
  685. htim3.Init.ClockDivision = TIM_CLOCKDIVISION_DIV1;
  686. htim3.Init.AutoReloadPreload = TIM_AUTORELOAD_PRELOAD_ENABLE;
  687. if (HAL_TIM_Base_Init(&htim3) != HAL_OK)
  688. {
  689. Error_Handler();
  690. }
  691. sClockSourceConfig.ClockSource = TIM_CLOCKSOURCE_INTERNAL;
  692. if (HAL_TIM_ConfigClockSource(&htim3, &sClockSourceConfig) != HAL_OK)
  693. {
  694. Error_Handler();
  695. }
  696. sMasterConfig.MasterOutputTrigger = TIM_TRGO_UPDATE;
  697. sMasterConfig.MasterSlaveMode = TIM_MASTERSLAVEMODE_DISABLE;
  698. if (HAL_TIMEx_MasterConfigSynchronization(&htim3, &sMasterConfig) != HAL_OK)
  699. {
  700. Error_Handler();
  701. }
  702. /* USER CODE BEGIN TIM3_Init 2 */
  703. /* USER CODE END TIM3_Init 2 */
  704. }
  705. /**
  706. * @brief TIM4 Initialization Function
  707. * @param None
  708. * @retval None
  709. */
  710. static void MX_TIM4_Init(void)
  711. {
  712. /* USER CODE BEGIN TIM4_Init 0 */
  713. /* USER CODE END TIM4_Init 0 */
  714. TIM_ClockConfigTypeDef sClockSourceConfig = {0};
  715. TIM_MasterConfigTypeDef sMasterConfig = {0};
  716. /* USER CODE BEGIN TIM4_Init 1 */
  717. /* USER CODE END TIM4_Init 1 */
  718. htim4.Instance = TIM4;
  719. htim4.Init.Prescaler = 4000;
  720. htim4.Init.CounterMode = TIM_COUNTERMODE_DOWN;
  721. htim4.Init.Period = 1000;
  722. htim4.Init.ClockDivision = TIM_CLOCKDIVISION_DIV1;
  723. htim4.Init.AutoReloadPreload = TIM_AUTORELOAD_PRELOAD_ENABLE;
  724. if (HAL_TIM_Base_Init(&htim4) != HAL_OK)
  725. {
  726. Error_Handler();
  727. }
  728. sClockSourceConfig.ClockSource = TIM_CLOCKSOURCE_INTERNAL;
  729. if (HAL_TIM_ConfigClockSource(&htim4, &sClockSourceConfig) != HAL_OK)
  730. {
  731. Error_Handler();
  732. }
  733. if (HAL_TIM_OnePulse_Init(&htim4, TIM_OPMODE_SINGLE) != HAL_OK)
  734. {
  735. Error_Handler();
  736. }
  737. sMasterConfig.MasterOutputTrigger = TIM_TRGO_UPDATE;
  738. sMasterConfig.MasterSlaveMode = TIM_MASTERSLAVEMODE_DISABLE;
  739. if (HAL_TIMEx_MasterConfigSynchronization(&htim4, &sMasterConfig) != HAL_OK)
  740. {
  741. Error_Handler();
  742. }
  743. /* USER CODE BEGIN TIM4_Init 2 */
  744. /* USER CODE END TIM4_Init 2 */
  745. }
  746. /**
  747. * Enable DMA controller clock
  748. */
  749. static void MX_DMA_Init(void)
  750. {
  751. /* DMA controller clock enable */
  752. __HAL_RCC_DMA2_CLK_ENABLE();
  753. /* DMA interrupt init */
  754. /* DMA2_Stream0_IRQn interrupt configuration */
  755. HAL_NVIC_SetPriority(DMA2_Stream0_IRQn, 0, 0);
  756. HAL_NVIC_EnableIRQ(DMA2_Stream0_IRQn);
  757. }
  758. /**
  759. * @brief GPIO Initialization Function
  760. * @param None
  761. * @retval None
  762. */
  763. static void MX_GPIO_Init(void)
  764. {
  765. GPIO_InitTypeDef GPIO_InitStruct = {0};
  766. /* GPIO Ports Clock Enable */
  767. __HAL_RCC_GPIOC_CLK_ENABLE();
  768. __HAL_RCC_GPIOH_CLK_ENABLE();
  769. __HAL_RCC_GPIOA_CLK_ENABLE();
  770. __HAL_RCC_GPIOB_CLK_ENABLE();
  771. __HAL_RCC_GPIOD_CLK_ENABLE();
  772. /*Configure GPIO pin Output Level */
  773. HAL_GPIO_WritePin(GPIOC, GPIO_PIN_13|GPIO_PIN_1|GPIO_PIN_3|GPIO_PIN_10
  774. |GPIO_PIN_11|GPIO_PIN_12, GPIO_PIN_RESET);
  775. /*Configure GPIO pin Output Level */
  776. HAL_GPIO_WritePin(GPIOC, GPIO_PIN_0, GPIO_PIN_SET);
  777. /*Configure GPIO pin Output Level */
  778. HAL_GPIO_WritePin(GPIOB, GPIO_PIN_1|GPIO_PIN_2|GPIO_PIN_10|GPIO_PIN_12
  779. |GPIO_PIN_6|GPIO_PIN_7, GPIO_PIN_RESET);
  780. /*Configure GPIO pin Output Level */
  781. HAL_GPIO_WritePin(GPIOA, GPIO_PIN_10|GPIO_PIN_15, GPIO_PIN_RESET);
  782. /*Configure GPIO pin Output Level */
  783. HAL_GPIO_WritePin(GPIOD, GPIO_PIN_2, GPIO_PIN_RESET);
  784. /*Configure GPIO pins : PC13 PC0 PC1 PC3
  785. PC10 PC11 PC12 */
  786. GPIO_InitStruct.Pin = GPIO_PIN_13|GPIO_PIN_0|GPIO_PIN_1|GPIO_PIN_3
  787. |GPIO_PIN_10|GPIO_PIN_11|GPIO_PIN_12;
  788. GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP;
  789. GPIO_InitStruct.Pull = GPIO_NOPULL;
  790. GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW;
  791. HAL_GPIO_Init(GPIOC, &GPIO_InitStruct);
  792. /*Configure GPIO pin : PC2 */
  793. GPIO_InitStruct.Pin = GPIO_PIN_2;
  794. GPIO_InitStruct.Mode = GPIO_MODE_INPUT;
  795. GPIO_InitStruct.Pull = GPIO_NOPULL;
  796. HAL_GPIO_Init(GPIOC, &GPIO_InitStruct);
  797. /*Configure GPIO pin : PA1 */
  798. GPIO_InitStruct.Pin = GPIO_PIN_1;
  799. GPIO_InitStruct.Mode = GPIO_MODE_IT_RISING;
  800. GPIO_InitStruct.Pull = GPIO_NOPULL;
  801. HAL_GPIO_Init(GPIOA, &GPIO_InitStruct);
  802. /*Configure GPIO pins : PB1 PB2 PB10 PB12
  803. PB6 PB7 */
  804. GPIO_InitStruct.Pin = GPIO_PIN_1|GPIO_PIN_2|GPIO_PIN_10|GPIO_PIN_12
  805. |GPIO_PIN_6|GPIO_PIN_7;
  806. GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP;
  807. GPIO_InitStruct.Pull = GPIO_NOPULL;
  808. GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW;
  809. HAL_GPIO_Init(GPIOB, &GPIO_InitStruct);
  810. /*Configure GPIO pins : PC6 PC7 PC9 */
  811. GPIO_InitStruct.Pin = GPIO_PIN_6|GPIO_PIN_7|GPIO_PIN_9;
  812. GPIO_InitStruct.Mode = GPIO_MODE_IT_FALLING;
  813. GPIO_InitStruct.Pull = GPIO_PULLUP;
  814. HAL_GPIO_Init(GPIOC, &GPIO_InitStruct);
  815. /*Configure GPIO pin : PC8 */
  816. GPIO_InitStruct.Pin = GPIO_PIN_8;
  817. GPIO_InitStruct.Mode = GPIO_MODE_INPUT;
  818. GPIO_InitStruct.Pull = GPIO_PULLUP;
  819. HAL_GPIO_Init(GPIOC, &GPIO_InitStruct);
  820. /*Configure GPIO pin : PA8 */
  821. GPIO_InitStruct.Pin = GPIO_PIN_8;
  822. GPIO_InitStruct.Mode = GPIO_MODE_IT_FALLING;
  823. GPIO_InitStruct.Pull = GPIO_PULLUP;
  824. HAL_GPIO_Init(GPIOA, &GPIO_InitStruct);
  825. /*Configure GPIO pins : PA10 PA15 */
  826. GPIO_InitStruct.Pin = GPIO_PIN_10|GPIO_PIN_15;
  827. GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP;
  828. GPIO_InitStruct.Pull = GPIO_NOPULL;
  829. GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW;
  830. HAL_GPIO_Init(GPIOA, &GPIO_InitStruct);
  831. /*Configure GPIO pin : PD2 */
  832. GPIO_InitStruct.Pin = GPIO_PIN_2;
  833. GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP;
  834. GPIO_InitStruct.Pull = GPIO_NOPULL;
  835. GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW;
  836. HAL_GPIO_Init(GPIOD, &GPIO_InitStruct);
  837. /* EXTI interrupt init*/
  838. HAL_NVIC_SetPriority(EXTI1_IRQn, 0, 0);
  839. HAL_NVIC_EnableIRQ(EXTI1_IRQn);
  840. }
  841. /* USER CODE BEGIN 4 */
  842. /* USER CODE END 4 */
  843. /**
  844. * @brief This function is executed in case of error occurrence.
  845. * @retval None
  846. */
  847. void Error_Handler(void)
  848. {
  849. /* USER CODE BEGIN Error_Handler_Debug */
  850. /* User can add his own implementation to report the HAL error return state */
  851. __disable_irq();
  852. while (1)
  853. {
  854. }
  855. /* USER CODE END Error_Handler_Debug */
  856. }
  857. #ifdef USE_FULL_ASSERT
  858. /**
  859. * @brief Reports the name of the source file and the source line number
  860. * where the assert_param error has occurred.
  861. * @param file: pointer to the source file name
  862. * @param line: assert_param error line source number
  863. * @retval None
  864. */
  865. void assert_failed(uint8_t *file, uint32_t line)
  866. {
  867. /* USER CODE BEGIN 6 */
  868. /* User can add his own implementation to report the file name and line number,
  869. ex: printf("Wrong parameters value: file %s on line %d\r\n", file, line) */
  870. /* USER CODE END 6 */
  871. }
  872. #endif /* USE_FULL_ASSERT */
  873. /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/