main.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534
  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) 2019 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 "pdm2pcm.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. CRC_HandleTypeDef hcrc;
  37. I2S_HandleTypeDef hi2s3;
  38. QSPI_HandleTypeDef hqspi;
  39. TIM_HandleTypeDef htim4;
  40. UART_HandleTypeDef huart3;
  41. PCD_HandleTypeDef hpcd_USB_OTG_FS;
  42. SDRAM_HandleTypeDef hsdram1;
  43. /* USER CODE BEGIN PV */
  44. /* USER CODE END PV */
  45. /* Private function prototypes -----------------------------------------------*/
  46. void SystemClock_Config(void);
  47. static void MX_GPIO_Init(void);
  48. static void MX_USART3_UART_Init(void);
  49. static void MX_FMC_Init(void);
  50. static void MX_QUADSPI_Init(void);
  51. static void MX_GFXSIMULATOR_Init(void);
  52. static void MX_CRC_Init(void);
  53. static void MX_I2S3_Init(void);
  54. static void MX_TIM4_Init(void);
  55. static void MX_USB_OTG_FS_PCD_Init(void);
  56. /* USER CODE BEGIN PFP */
  57. /* USER CODE END PFP */
  58. /* Private user code ---------------------------------------------------------*/
  59. /* USER CODE BEGIN 0 */
  60. /* USER CODE END 0 */
  61. /**
  62. * @brief The application entry point.
  63. * @retval int
  64. */
  65. int main(void)
  66. {
  67. /* USER CODE BEGIN 1 */
  68. /* USER CODE END 1 */
  69. /* MCU Configuration--------------------------------------------------------*/
  70. /* Reset of all peripherals, Initializes the Flash interface and the Systick. */
  71. HAL_Init();
  72. /* USER CODE BEGIN Init */
  73. /* USER CODE END Init */
  74. /* Configure the system clock */
  75. SystemClock_Config();
  76. /* USER CODE BEGIN SysInit */
  77. /* USER CODE END SysInit */
  78. /* Initialize all configured peripherals */
  79. MX_GPIO_Init();
  80. MX_USART3_UART_Init();
  81. MX_FMC_Init();
  82. MX_QUADSPI_Init();
  83. MX_GFXSIMULATOR_Init();
  84. MX_CRC_Init();
  85. MX_I2S3_Init();
  86. MX_TIM4_Init();
  87. MX_PDM2PCM_Init();
  88. MX_USB_OTG_FS_PCD_Init();
  89. /* USER CODE BEGIN 2 */
  90. /* USER CODE END 2 */
  91. /* Infinite loop */
  92. /* USER CODE BEGIN WHILE */
  93. while (1)
  94. {
  95. // HAL_GPIO_WritePin(GPIOG, 6, GPIO_PIN_SET);
  96. HAL_GPIO_WritePin(GPIOG, 6, GPIO_PIN_RESET);
  97. /* USER CODE END WHILE */
  98. /* USER CODE BEGIN 3 */
  99. }
  100. /* USER CODE END 3 */
  101. }
  102. /**
  103. * @brief System Clock Configuration
  104. * @retval None
  105. */
  106. void SystemClock_Config(void)
  107. {
  108. RCC_OscInitTypeDef RCC_OscInitStruct = {0};
  109. RCC_ClkInitTypeDef RCC_ClkInitStruct = {0};
  110. RCC_PeriphCLKInitTypeDef PeriphClkInitStruct = {0};
  111. /** Configure the main internal regulator output voltage
  112. */
  113. __HAL_RCC_PWR_CLK_ENABLE();
  114. __HAL_PWR_VOLTAGESCALING_CONFIG(PWR_REGULATOR_VOLTAGE_SCALE1);
  115. /** Initializes the CPU, AHB and APB busses clocks
  116. */
  117. RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_HSE;
  118. RCC_OscInitStruct.HSEState = RCC_HSE_ON;
  119. RCC_OscInitStruct.PLL.PLLState = RCC_PLL_ON;
  120. RCC_OscInitStruct.PLL.PLLSource = RCC_PLLSOURCE_HSE;
  121. RCC_OscInitStruct.PLL.PLLM = 4;
  122. RCC_OscInitStruct.PLL.PLLN = 180;
  123. RCC_OscInitStruct.PLL.PLLP = RCC_PLLP_DIV2;
  124. RCC_OscInitStruct.PLL.PLLQ = 3;
  125. RCC_OscInitStruct.PLL.PLLR = 2;
  126. if (HAL_RCC_OscConfig(&RCC_OscInitStruct) != HAL_OK)
  127. {
  128. Error_Handler();
  129. }
  130. /** Activate the Over-Drive mode
  131. */
  132. if (HAL_PWREx_EnableOverDrive() != HAL_OK)
  133. {
  134. Error_Handler();
  135. }
  136. /** Initializes the CPU, AHB and APB busses clocks
  137. */
  138. RCC_ClkInitStruct.ClockType = RCC_CLOCKTYPE_HCLK|RCC_CLOCKTYPE_SYSCLK
  139. |RCC_CLOCKTYPE_PCLK1|RCC_CLOCKTYPE_PCLK2;
  140. RCC_ClkInitStruct.SYSCLKSource = RCC_SYSCLKSOURCE_PLLCLK;
  141. RCC_ClkInitStruct.AHBCLKDivider = RCC_SYSCLK_DIV1;
  142. RCC_ClkInitStruct.APB1CLKDivider = RCC_HCLK_DIV4;
  143. RCC_ClkInitStruct.APB2CLKDivider = RCC_HCLK_DIV2;
  144. if (HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_5) != HAL_OK)
  145. {
  146. Error_Handler();
  147. }
  148. PeriphClkInitStruct.PeriphClockSelection = RCC_PERIPHCLK_I2S|RCC_PERIPHCLK_CLK48;
  149. PeriphClkInitStruct.PLLI2S.PLLI2SN = 192;
  150. PeriphClkInitStruct.PLLI2S.PLLI2SR = 2;
  151. PeriphClkInitStruct.PLLSAI.PLLSAIN = 96;
  152. PeriphClkInitStruct.PLLSAI.PLLSAIP = RCC_PLLSAIP_DIV4;
  153. PeriphClkInitStruct.Clk48ClockSelection = RCC_CLK48CLKSOURCE_PLLSAIP;
  154. if (HAL_RCCEx_PeriphCLKConfig(&PeriphClkInitStruct) != HAL_OK)
  155. {
  156. Error_Handler();
  157. }
  158. }
  159. /**
  160. * @brief CRC Initialization Function
  161. * @param None
  162. * @retval None
  163. */
  164. static void MX_CRC_Init(void)
  165. {
  166. /* USER CODE BEGIN CRC_Init 0 */
  167. /* USER CODE END CRC_Init 0 */
  168. /* USER CODE BEGIN CRC_Init 1 */
  169. /* USER CODE END CRC_Init 1 */
  170. hcrc.Instance = CRC;
  171. if (HAL_CRC_Init(&hcrc) != HAL_OK)
  172. {
  173. Error_Handler();
  174. }
  175. __HAL_CRC_DR_RESET(&hcrc);
  176. /* USER CODE BEGIN CRC_Init 2 */
  177. /* USER CODE END CRC_Init 2 */
  178. }
  179. /**
  180. * @brief GFXSIMULATOR Initialization Function
  181. * @param None
  182. * @retval None
  183. */
  184. static void MX_GFXSIMULATOR_Init(void)
  185. {
  186. /* USER CODE BEGIN GFXSIMULATOR_Init 0 */
  187. /* USER CODE END GFXSIMULATOR_Init 0 */
  188. /* USER CODE BEGIN GFXSIMULATOR_Init 1 */
  189. /* USER CODE END GFXSIMULATOR_Init 1 */
  190. /* USER CODE BEGIN GFXSIMULATOR_Init 2 */
  191. /* USER CODE END GFXSIMULATOR_Init 2 */
  192. }
  193. /**
  194. * @brief I2S3 Initialization Function
  195. * @param None
  196. * @retval None
  197. */
  198. static void MX_I2S3_Init(void)
  199. {
  200. /* USER CODE BEGIN I2S3_Init 0 */
  201. /* USER CODE END I2S3_Init 0 */
  202. /* USER CODE BEGIN I2S3_Init 1 */
  203. /* USER CODE END I2S3_Init 1 */
  204. hi2s3.Instance = SPI3;
  205. hi2s3.Init.Mode = I2S_MODE_MASTER_RX;
  206. hi2s3.Init.Standard = I2S_STANDARD_LSB;
  207. hi2s3.Init.DataFormat = I2S_DATAFORMAT_16B;
  208. hi2s3.Init.MCLKOutput = I2S_MCLKOUTPUT_DISABLE;
  209. hi2s3.Init.AudioFreq = I2S_AUDIOFREQ_16K;
  210. hi2s3.Init.CPOL = I2S_CPOL_LOW;
  211. hi2s3.Init.ClockSource = I2S_CLOCK_PLL;
  212. hi2s3.Init.FullDuplexMode = I2S_FULLDUPLEXMODE_DISABLE;
  213. if (HAL_I2S_Init(&hi2s3) != HAL_OK)
  214. {
  215. Error_Handler();
  216. }
  217. /* USER CODE BEGIN I2S3_Init 2 */
  218. /* USER CODE END I2S3_Init 2 */
  219. }
  220. /**
  221. * @brief QUADSPI Initialization Function
  222. * @param None
  223. * @retval None
  224. */
  225. static void MX_QUADSPI_Init(void)
  226. {
  227. /* USER CODE BEGIN QUADSPI_Init 0 */
  228. /* USER CODE END QUADSPI_Init 0 */
  229. /* USER CODE BEGIN QUADSPI_Init 1 */
  230. /* USER CODE END QUADSPI_Init 1 */
  231. /* QUADSPI parameter configuration*/
  232. hqspi.Instance = QUADSPI;
  233. hqspi.Init.ClockPrescaler = 1;
  234. hqspi.Init.FifoThreshold = 4;
  235. hqspi.Init.SampleShifting = QSPI_SAMPLE_SHIFTING_HALFCYCLE;
  236. hqspi.Init.FlashSize = 24;
  237. hqspi.Init.ChipSelectHighTime = QSPI_CS_HIGH_TIME_5_CYCLE;
  238. hqspi.Init.ClockMode = QSPI_CLOCK_MODE_0;
  239. hqspi.Init.FlashID = QSPI_FLASH_ID_1;
  240. hqspi.Init.DualFlash = QSPI_DUALFLASH_DISABLE;
  241. if (HAL_QSPI_Init(&hqspi) != HAL_OK)
  242. {
  243. Error_Handler();
  244. }
  245. /* USER CODE BEGIN QUADSPI_Init 2 */
  246. /* USER CODE END QUADSPI_Init 2 */
  247. }
  248. /**
  249. * @brief TIM4 Initialization Function
  250. * @param None
  251. * @retval None
  252. */
  253. static void MX_TIM4_Init(void)
  254. {
  255. /* USER CODE BEGIN TIM4_Init 0 */
  256. /* USER CODE END TIM4_Init 0 */
  257. TIM_SlaveConfigTypeDef sSlaveConfig = {0};
  258. TIM_MasterConfigTypeDef sMasterConfig = {0};
  259. TIM_OC_InitTypeDef sConfigOC = {0};
  260. /* USER CODE BEGIN TIM4_Init 1 */
  261. /* USER CODE END TIM4_Init 1 */
  262. htim4.Instance = TIM4;
  263. htim4.Init.Prescaler = 0;
  264. htim4.Init.CounterMode = TIM_COUNTERMODE_UP;
  265. htim4.Init.Period = 1;
  266. htim4.Init.ClockDivision = TIM_CLOCKDIVISION_DIV1;
  267. htim4.Init.AutoReloadPreload = TIM_AUTORELOAD_PRELOAD_DISABLE;
  268. if (HAL_TIM_Base_Init(&htim4) != HAL_OK)
  269. {
  270. Error_Handler();
  271. }
  272. if (HAL_TIM_PWM_Init(&htim4) != HAL_OK)
  273. {
  274. Error_Handler();
  275. }
  276. sSlaveConfig.SlaveMode = TIM_SLAVEMODE_EXTERNAL1;
  277. sSlaveConfig.InputTrigger = TIM_TS_TI1FP1;
  278. sSlaveConfig.TriggerPolarity = TIM_TRIGGERPOLARITY_FALLING;
  279. sSlaveConfig.TriggerFilter = 0;
  280. if (HAL_TIM_SlaveConfigSynchro(&htim4, &sSlaveConfig) != HAL_OK)
  281. {
  282. Error_Handler();
  283. }
  284. sMasterConfig.MasterOutputTrigger = TIM_TRGO_RESET;
  285. sMasterConfig.MasterSlaveMode = TIM_MASTERSLAVEMODE_DISABLE;
  286. if (HAL_TIMEx_MasterConfigSynchronization(&htim4, &sMasterConfig) != HAL_OK)
  287. {
  288. Error_Handler();
  289. }
  290. sConfigOC.OCMode = TIM_OCMODE_PWM1;
  291. sConfigOC.Pulse = 1;
  292. sConfigOC.OCPolarity = TIM_OCPOLARITY_HIGH;
  293. sConfigOC.OCFastMode = TIM_OCFAST_DISABLE;
  294. if (HAL_TIM_PWM_ConfigChannel(&htim4, &sConfigOC, TIM_CHANNEL_2) != HAL_OK)
  295. {
  296. Error_Handler();
  297. }
  298. /* USER CODE BEGIN TIM4_Init 2 */
  299. /* USER CODE END TIM4_Init 2 */
  300. HAL_TIM_MspPostInit(&htim4);
  301. }
  302. /**
  303. * @brief USART3 Initialization Function
  304. * @param None
  305. * @retval None
  306. */
  307. static void MX_USART3_UART_Init(void)
  308. {
  309. /* USER CODE BEGIN USART3_Init 0 */
  310. /* USER CODE END USART3_Init 0 */
  311. /* USER CODE BEGIN USART3_Init 1 */
  312. /* USER CODE END USART3_Init 1 */
  313. huart3.Instance = USART3;
  314. huart3.Init.BaudRate = 115200;
  315. huart3.Init.WordLength = UART_WORDLENGTH_8B;
  316. huart3.Init.StopBits = UART_STOPBITS_1;
  317. huart3.Init.Parity = UART_PARITY_NONE;
  318. huart3.Init.Mode = UART_MODE_TX_RX;
  319. huart3.Init.HwFlowCtl = UART_HWCONTROL_NONE;
  320. huart3.Init.OverSampling = UART_OVERSAMPLING_16;
  321. if (HAL_UART_Init(&huart3) != HAL_OK)
  322. {
  323. Error_Handler();
  324. }
  325. /* USER CODE BEGIN USART3_Init 2 */
  326. /* USER CODE END USART3_Init 2 */
  327. }
  328. /**
  329. * @brief USB_OTG_FS Initialization Function
  330. * @param None
  331. * @retval None
  332. */
  333. static void MX_USB_OTG_FS_PCD_Init(void)
  334. {
  335. /* USER CODE BEGIN USB_OTG_FS_Init 0 */
  336. /* USER CODE END USB_OTG_FS_Init 0 */
  337. /* USER CODE BEGIN USB_OTG_FS_Init 1 */
  338. /* USER CODE END USB_OTG_FS_Init 1 */
  339. hpcd_USB_OTG_FS.Instance = USB_OTG_FS;
  340. hpcd_USB_OTG_FS.Init.dev_endpoints = 6;
  341. hpcd_USB_OTG_FS.Init.speed = PCD_SPEED_FULL;
  342. hpcd_USB_OTG_FS.Init.dma_enable = DISABLE;
  343. hpcd_USB_OTG_FS.Init.phy_itface = PCD_PHY_EMBEDDED;
  344. hpcd_USB_OTG_FS.Init.Sof_enable = DISABLE;
  345. hpcd_USB_OTG_FS.Init.low_power_enable = DISABLE;
  346. hpcd_USB_OTG_FS.Init.lpm_enable = DISABLE;
  347. hpcd_USB_OTG_FS.Init.vbus_sensing_enable = DISABLE;
  348. hpcd_USB_OTG_FS.Init.use_dedicated_ep1 = DISABLE;
  349. if (HAL_PCD_Init(&hpcd_USB_OTG_FS) != HAL_OK)
  350. {
  351. Error_Handler();
  352. }
  353. /* USER CODE BEGIN USB_OTG_FS_Init 2 */
  354. /* USER CODE END USB_OTG_FS_Init 2 */
  355. }
  356. /* FMC initialization function */
  357. static void MX_FMC_Init(void)
  358. {
  359. FMC_SDRAM_TimingTypeDef SdramTiming;
  360. /** Perform the SDRAM1 memory initialization sequence
  361. */
  362. hsdram1.Instance = FMC_SDRAM_DEVICE;
  363. /* hsdram1.Init */
  364. hsdram1.Init.SDBank = FMC_SDRAM_BANK1;
  365. hsdram1.Init.ColumnBitsNumber = FMC_SDRAM_COLUMN_BITS_NUM_8;
  366. hsdram1.Init.RowBitsNumber = FMC_SDRAM_ROW_BITS_NUM_12;
  367. hsdram1.Init.MemoryDataWidth = FMC_SDRAM_MEM_BUS_WIDTH_32;
  368. hsdram1.Init.InternalBankNumber = FMC_SDRAM_INTERN_BANKS_NUM_4;
  369. hsdram1.Init.CASLatency = FMC_SDRAM_CAS_LATENCY_3;
  370. hsdram1.Init.WriteProtection = FMC_SDRAM_WRITE_PROTECTION_DISABLE;
  371. hsdram1.Init.SDClockPeriod = FMC_SDRAM_CLOCK_PERIOD_2;
  372. hsdram1.Init.ReadBurst = FMC_SDRAM_RBURST_ENABLE;
  373. hsdram1.Init.ReadPipeDelay = FMC_SDRAM_RPIPE_DELAY_0;
  374. /* SdramTiming */
  375. SdramTiming.LoadToActiveDelay = 2;
  376. SdramTiming.ExitSelfRefreshDelay = 7;
  377. SdramTiming.SelfRefreshTime = 4;
  378. SdramTiming.RowCycleDelay = 7;
  379. SdramTiming.WriteRecoveryTime = 3;
  380. SdramTiming.RPDelay = 2;
  381. SdramTiming.RCDDelay = 2;
  382. if (HAL_SDRAM_Init(&hsdram1, &SdramTiming) != HAL_OK)
  383. {
  384. Error_Handler( );
  385. }
  386. }
  387. /**
  388. * @brief GPIO Initialization Function
  389. * @param None
  390. * @retval None
  391. */
  392. static void MX_GPIO_Init(void)
  393. {
  394. /* GPIO Ports Clock Enable */
  395. __HAL_RCC_GPIOE_CLK_ENABLE();
  396. __HAL_RCC_GPIOB_CLK_ENABLE();
  397. __HAL_RCC_GPIOA_CLK_ENABLE();
  398. __HAL_RCC_GPIOG_CLK_ENABLE();
  399. __HAL_RCC_GPIOD_CLK_ENABLE();
  400. __HAL_RCC_GPIOI_CLK_ENABLE();
  401. __HAL_RCC_GPIOF_CLK_ENABLE();
  402. __HAL_RCC_GPIOH_CLK_ENABLE();
  403. __HAL_RCC_GPIOC_CLK_ENABLE();
  404. }
  405. /* USER CODE BEGIN 4 */
  406. /* USER CODE END 4 */
  407. /**
  408. * @brief This function is executed in case of error occurrence.
  409. * @retval None
  410. */
  411. void Error_Handler(void)
  412. {
  413. /* USER CODE BEGIN Error_Handler_Debug */
  414. /* User can add his own implementation to report the HAL error return state */
  415. /* USER CODE END Error_Handler_Debug */
  416. }
  417. #ifdef USE_FULL_ASSERT
  418. /**
  419. * @brief Reports the name of the source file and the source line number
  420. * where the assert_param error has occurred.
  421. * @param file: pointer to the source file name
  422. * @param line: assert_param error line source number
  423. * @retval None
  424. */
  425. void assert_failed(uint8_t *file, uint32_t line)
  426. {
  427. /* USER CODE BEGIN 6 */
  428. /* User can add his own implementation to report the file name and line number,
  429. tex: printf("Wrong parameters value: file %s on line %d\r\n", file, line) */
  430. /* USER CODE END 6 */
  431. }
  432. #endif /* USE_FULL_ASSERT */
  433. /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/