main.c 26 KB

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