main.c 23 KB

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