main.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456
  1. /* USER CODE BEGIN Header */
  2. /**
  3. ******************************************************************************
  4. * @file : main.c
  5. * @brief : Main program body
  6. ******************************************************************************
  7. ** This notice applies to any and all portions of this file
  8. * that are not between comment pairs USER CODE BEGIN and
  9. * USER CODE END. Other portions of this file, whether
  10. * inserted by the user or by software development tools
  11. * are owned by their respective copyright owners.
  12. *
  13. * COPYRIGHT(c) 2019 STMicroelectronics
  14. *
  15. * Redistribution and use in source and binary forms, with or without modification,
  16. * are permitted provided that the following conditions are met:
  17. * 1. Redistributions of source code must retain the above copyright notice,
  18. * this list of conditions and the following disclaimer.
  19. * 2. Redistributions in binary form must reproduce the above copyright notice,
  20. * this list of conditions and the following disclaimer in the documentation
  21. * and/or other materials provided with the distribution.
  22. * 3. Neither the name of STMicroelectronics nor the names of its contributors
  23. * may be used to endorse or promote products derived from this software
  24. * without specific prior written permission.
  25. *
  26. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  27. * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  28. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  29. * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
  30. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  31. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
  32. * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  33. * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
  34. * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  35. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  36. *
  37. ******************************************************************************
  38. */
  39. /* USER CODE END Header */
  40. /* Includes ------------------------------------------------------------------*/
  41. #include "main.h"
  42. /* Private includes ----------------------------------------------------------*/
  43. /* USER CODE BEGIN Includes */
  44. /* USER CODE END Includes */
  45. /* Private typedef -----------------------------------------------------------*/
  46. /* USER CODE BEGIN PTD */
  47. /* USER CODE END PTD */
  48. /* Private define ------------------------------------------------------------*/
  49. /* USER CODE BEGIN PD */
  50. /* USER CODE END PD */
  51. /* Private macro -------------------------------------------------------------*/
  52. /* USER CODE BEGIN PM */
  53. /* USER CODE END PM */
  54. /* Private variables ---------------------------------------------------------*/
  55. DMA2D_HandleTypeDef hdma2d;
  56. LTDC_HandleTypeDef hltdc;
  57. UART_HandleTypeDef huart1;
  58. SDRAM_HandleTypeDef hsdram1;
  59. /* USER CODE BEGIN PV */
  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_USART1_UART_Init(void);
  65. static void MX_FMC_Init(void);
  66. static void MX_DMA2D_Init(void);
  67. static void MX_LTDC_Init(void);
  68. /* USER CODE BEGIN PFP */
  69. /* USER CODE END PFP */
  70. /* Private user code ---------------------------------------------------------*/
  71. /* USER CODE BEGIN 0 */
  72. /* USER CODE END 0 */
  73. /**
  74. * @brief The application entry point.
  75. * @retval int
  76. */
  77. int main(void)
  78. {
  79. /* USER CODE BEGIN 1 */
  80. /* USER CODE END 1 */
  81. /* MCU Configuration--------------------------------------------------------*/
  82. /* Reset of all peripherals, Initializes the Flash interface and the Systick. */
  83. HAL_Init();
  84. /* USER CODE BEGIN Init */
  85. /* USER CODE END Init */
  86. /* Configure the system clock */
  87. SystemClock_Config();
  88. /* USER CODE BEGIN SysInit */
  89. /* USER CODE END SysInit */
  90. /* Initialize all configured peripherals */
  91. MX_GPIO_Init();
  92. MX_USART1_UART_Init();
  93. MX_FMC_Init();
  94. MX_DMA2D_Init();
  95. MX_LTDC_Init();
  96. /* USER CODE BEGIN 2 */
  97. /* USER CODE END 2 */
  98. /* Infinite loop */
  99. /* USER CODE BEGIN WHILE */
  100. while (1)
  101. {
  102. /* USER CODE END WHILE */
  103. /* USER CODE BEGIN 3 */
  104. }
  105. /* USER CODE END 3 */
  106. }
  107. /**
  108. * @brief System Clock Configuration
  109. * @retval None
  110. */
  111. void SystemClock_Config(void)
  112. {
  113. RCC_OscInitTypeDef RCC_OscInitStruct = {0};
  114. RCC_ClkInitTypeDef RCC_ClkInitStruct = {0};
  115. RCC_PeriphCLKInitTypeDef PeriphClkInitStruct = {0};
  116. /**Supply configuration update enable
  117. */
  118. MODIFY_REG(PWR->CR3, PWR_CR3_SCUEN, 0);
  119. /**Configure the main internal regulator output voltage
  120. */
  121. __HAL_PWR_VOLTAGESCALING_CONFIG(PWR_REGULATOR_VOLTAGE_SCALE1);
  122. while ((PWR->D3CR & (PWR_D3CR_VOSRDY)) != PWR_D3CR_VOSRDY)
  123. {
  124. }
  125. /**Macro to configure the PLL clock source
  126. */
  127. __HAL_RCC_PLL_PLLSOURCE_CONFIG(RCC_PLLSOURCE_HSE);
  128. /**Initializes the CPU, AHB and APB busses clocks
  129. */
  130. RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_HSE;
  131. RCC_OscInitStruct.HSEState = RCC_HSE_ON;
  132. RCC_OscInitStruct.PLL.PLLState = RCC_PLL_ON;
  133. RCC_OscInitStruct.PLL.PLLSource = RCC_PLLSOURCE_HSE;
  134. RCC_OscInitStruct.PLL.PLLM = 5;
  135. RCC_OscInitStruct.PLL.PLLN = 160;
  136. RCC_OscInitStruct.PLL.PLLP = 2;
  137. RCC_OscInitStruct.PLL.PLLQ = 2;
  138. RCC_OscInitStruct.PLL.PLLR = 2;
  139. RCC_OscInitStruct.PLL.PLLRGE = RCC_PLL1VCIRANGE_2;
  140. RCC_OscInitStruct.PLL.PLLVCOSEL = RCC_PLL1VCOWIDE;
  141. RCC_OscInitStruct.PLL.PLLFRACN = 0;
  142. if (HAL_RCC_OscConfig(&RCC_OscInitStruct) != HAL_OK)
  143. {
  144. Error_Handler();
  145. }
  146. /**Initializes the CPU, AHB and APB busses clocks
  147. */
  148. RCC_ClkInitStruct.ClockType = RCC_CLOCKTYPE_HCLK|RCC_CLOCKTYPE_SYSCLK
  149. |RCC_CLOCKTYPE_PCLK1|RCC_CLOCKTYPE_PCLK2
  150. |RCC_CLOCKTYPE_D3PCLK1|RCC_CLOCKTYPE_D1PCLK1;
  151. RCC_ClkInitStruct.SYSCLKSource = RCC_SYSCLKSOURCE_PLLCLK;
  152. RCC_ClkInitStruct.SYSCLKDivider = RCC_SYSCLK_DIV1;
  153. RCC_ClkInitStruct.AHBCLKDivider = RCC_HCLK_DIV2;
  154. RCC_ClkInitStruct.APB3CLKDivider = RCC_APB3_DIV2;
  155. RCC_ClkInitStruct.APB1CLKDivider = RCC_APB1_DIV2;
  156. RCC_ClkInitStruct.APB2CLKDivider = RCC_APB2_DIV2;
  157. RCC_ClkInitStruct.APB4CLKDivider = RCC_APB4_DIV2;
  158. if (HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_2) != HAL_OK)
  159. {
  160. Error_Handler();
  161. }
  162. PeriphClkInitStruct.PeriphClockSelection = RCC_PERIPHCLK_LTDC|RCC_PERIPHCLK_USART1
  163. |RCC_PERIPHCLK_FMC;
  164. PeriphClkInitStruct.PLL3.PLL3M = 5;
  165. PeriphClkInitStruct.PLL3.PLL3N = 160;
  166. PeriphClkInitStruct.PLL3.PLL3P = 2;
  167. PeriphClkInitStruct.PLL3.PLL3Q = 2;
  168. PeriphClkInitStruct.PLL3.PLL3R = 88;
  169. PeriphClkInitStruct.PLL3.PLL3RGE = RCC_PLL3VCIRANGE_2;
  170. PeriphClkInitStruct.PLL3.PLL3VCOSEL = RCC_PLL3VCOWIDE;
  171. PeriphClkInitStruct.PLL3.PLL3FRACN = 0;
  172. PeriphClkInitStruct.FmcClockSelection = RCC_FMCCLKSOURCE_D1HCLK;
  173. PeriphClkInitStruct.Usart16ClockSelection = RCC_USART16CLKSOURCE_D2PCLK2;
  174. if (HAL_RCCEx_PeriphCLKConfig(&PeriphClkInitStruct) != HAL_OK)
  175. {
  176. Error_Handler();
  177. }
  178. }
  179. /**
  180. * @brief DMA2D Initialization Function
  181. * @param None
  182. * @retval None
  183. */
  184. static void MX_DMA2D_Init(void)
  185. {
  186. /* USER CODE BEGIN DMA2D_Init 0 */
  187. /* USER CODE END DMA2D_Init 0 */
  188. /* USER CODE BEGIN DMA2D_Init 1 */
  189. /* USER CODE END DMA2D_Init 1 */
  190. hdma2d.Instance = DMA2D;
  191. hdma2d.Init.Mode = DMA2D_R2M;
  192. hdma2d.Init.ColorMode = DMA2D_OUTPUT_RGB565;
  193. hdma2d.Init.OutputOffset = 0;
  194. if (HAL_DMA2D_Init(&hdma2d) != HAL_OK)
  195. {
  196. Error_Handler();
  197. }
  198. /* USER CODE BEGIN DMA2D_Init 2 */
  199. /* USER CODE END DMA2D_Init 2 */
  200. }
  201. /**
  202. * @brief LTDC Initialization Function
  203. * @param None
  204. * @retval None
  205. */
  206. static void MX_LTDC_Init(void)
  207. {
  208. /* USER CODE BEGIN LTDC_Init 0 */
  209. /* USER CODE END LTDC_Init 0 */
  210. LTDC_LayerCfgTypeDef pLayerCfg = {0};
  211. LTDC_LayerCfgTypeDef pLayerCfg1 = {0};
  212. /* USER CODE BEGIN LTDC_Init 1 */
  213. /* USER CODE END LTDC_Init 1 */
  214. hltdc.Instance = LTDC;
  215. hltdc.Init.HSPolarity = LTDC_HSPOLARITY_AL;
  216. hltdc.Init.VSPolarity = LTDC_VSPOLARITY_AL;
  217. hltdc.Init.DEPolarity = LTDC_DEPOLARITY_AL;
  218. hltdc.Init.PCPolarity = LTDC_PCPOLARITY_IPC;
  219. hltdc.Init.HorizontalSync = 7;
  220. hltdc.Init.VerticalSync = 3;
  221. hltdc.Init.AccumulatedHBP = 14;
  222. hltdc.Init.AccumulatedVBP = 5;
  223. hltdc.Init.AccumulatedActiveW = 654;
  224. hltdc.Init.AccumulatedActiveH = 485;
  225. hltdc.Init.TotalWidth = 660;
  226. hltdc.Init.TotalHeigh = 487;
  227. hltdc.Init.Backcolor.Blue = 0;
  228. hltdc.Init.Backcolor.Green = 0;
  229. hltdc.Init.Backcolor.Red = 0;
  230. if (HAL_LTDC_Init(&hltdc) != HAL_OK)
  231. {
  232. Error_Handler();
  233. }
  234. pLayerCfg.WindowX0 = 0;
  235. pLayerCfg.WindowX1 = 0;
  236. pLayerCfg.WindowY0 = 0;
  237. pLayerCfg.WindowY1 = 0;
  238. pLayerCfg.PixelFormat = LTDC_PIXEL_FORMAT_ARGB8888;
  239. pLayerCfg.Alpha = 0;
  240. pLayerCfg.Alpha0 = 0;
  241. pLayerCfg.BlendingFactor1 = LTDC_BLENDING_FACTOR1_CA;
  242. pLayerCfg.BlendingFactor2 = LTDC_BLENDING_FACTOR2_CA;
  243. pLayerCfg.FBStartAdress = 0;
  244. pLayerCfg.ImageWidth = 0;
  245. pLayerCfg.ImageHeight = 0;
  246. pLayerCfg.Backcolor.Blue = 0;
  247. pLayerCfg.Backcolor.Green = 0;
  248. pLayerCfg.Backcolor.Red = 0;
  249. if (HAL_LTDC_ConfigLayer(&hltdc, &pLayerCfg, 0) != HAL_OK)
  250. {
  251. Error_Handler();
  252. }
  253. pLayerCfg1.WindowX0 = 0;
  254. pLayerCfg1.WindowX1 = 0;
  255. pLayerCfg1.WindowY0 = 0;
  256. pLayerCfg1.WindowY1 = 0;
  257. pLayerCfg1.PixelFormat = LTDC_PIXEL_FORMAT_ARGB8888;
  258. pLayerCfg1.Alpha = 0;
  259. pLayerCfg1.Alpha0 = 0;
  260. pLayerCfg1.BlendingFactor1 = LTDC_BLENDING_FACTOR1_CA;
  261. pLayerCfg1.BlendingFactor2 = LTDC_BLENDING_FACTOR2_CA;
  262. pLayerCfg1.FBStartAdress = 0;
  263. pLayerCfg1.ImageWidth = 0;
  264. pLayerCfg1.ImageHeight = 0;
  265. pLayerCfg1.Backcolor.Blue = 0;
  266. pLayerCfg1.Backcolor.Green = 0;
  267. pLayerCfg1.Backcolor.Red = 0;
  268. if (HAL_LTDC_ConfigLayer(&hltdc, &pLayerCfg1, 1) != HAL_OK)
  269. {
  270. Error_Handler();
  271. }
  272. /* USER CODE BEGIN LTDC_Init 2 */
  273. /* USER CODE END LTDC_Init 2 */
  274. }
  275. /**
  276. * @brief USART1 Initialization Function
  277. * @param None
  278. * @retval None
  279. */
  280. static void MX_USART1_UART_Init(void)
  281. {
  282. /* USER CODE BEGIN USART1_Init 0 */
  283. /* USER CODE END USART1_Init 0 */
  284. /* USER CODE BEGIN USART1_Init 1 */
  285. /* USER CODE END USART1_Init 1 */
  286. huart1.Instance = USART1;
  287. huart1.Init.BaudRate = 115200;
  288. huart1.Init.WordLength = UART_WORDLENGTH_8B;
  289. huart1.Init.StopBits = UART_STOPBITS_1;
  290. huart1.Init.Parity = UART_PARITY_NONE;
  291. huart1.Init.Mode = UART_MODE_TX_RX;
  292. huart1.Init.HwFlowCtl = UART_HWCONTROL_NONE;
  293. huart1.Init.OverSampling = UART_OVERSAMPLING_16;
  294. huart1.Init.OneBitSampling = UART_ONE_BIT_SAMPLE_DISABLE;
  295. huart1.Init.Prescaler = UART_PRESCALER_DIV1;
  296. huart1.Init.FIFOMode = UART_FIFOMODE_DISABLE;
  297. huart1.Init.TXFIFOThreshold = UART_TXFIFO_THRESHOLD_1_8;
  298. huart1.Init.RXFIFOThreshold = UART_RXFIFO_THRESHOLD_1_8;
  299. huart1.AdvancedInit.AdvFeatureInit = UART_ADVFEATURE_NO_INIT;
  300. if (HAL_UART_Init(&huart1) != HAL_OK)
  301. {
  302. Error_Handler();
  303. }
  304. /* USER CODE BEGIN USART1_Init 2 */
  305. /* USER CODE END USART1_Init 2 */
  306. }
  307. /* FMC initialization function */
  308. static void MX_FMC_Init(void)
  309. {
  310. FMC_SDRAM_TimingTypeDef SdramTiming;
  311. /** Perform the SDRAM1 memory initialization sequence
  312. */
  313. hsdram1.Instance = FMC_SDRAM_DEVICE;
  314. /* hsdram1.Init */
  315. hsdram1.Init.SDBank = FMC_SDRAM_BANK1;
  316. hsdram1.Init.ColumnBitsNumber = FMC_SDRAM_COLUMN_BITS_NUM_9;
  317. hsdram1.Init.RowBitsNumber = FMC_SDRAM_ROW_BITS_NUM_13;
  318. hsdram1.Init.MemoryDataWidth = FMC_SDRAM_MEM_BUS_WIDTH_16;
  319. hsdram1.Init.InternalBankNumber = FMC_SDRAM_INTERN_BANKS_NUM_4;
  320. hsdram1.Init.CASLatency = FMC_SDRAM_CAS_LATENCY_2;
  321. hsdram1.Init.WriteProtection = FMC_SDRAM_WRITE_PROTECTION_DISABLE;
  322. hsdram1.Init.SDClockPeriod = FMC_SDRAM_CLOCK_PERIOD_2;
  323. hsdram1.Init.ReadBurst = FMC_SDRAM_RBURST_ENABLE;
  324. hsdram1.Init.ReadPipeDelay = FMC_SDRAM_RPIPE_DELAY_1;
  325. /* SdramTiming */
  326. SdramTiming.LoadToActiveDelay = 2;
  327. SdramTiming.ExitSelfRefreshDelay = 8;
  328. SdramTiming.SelfRefreshTime = 6;
  329. SdramTiming.RowCycleDelay = 6;
  330. SdramTiming.WriteRecoveryTime = 4;
  331. SdramTiming.RPDelay = 2;
  332. SdramTiming.RCDDelay = 2;
  333. if (HAL_SDRAM_Init(&hsdram1, &SdramTiming) != HAL_OK)
  334. {
  335. Error_Handler( );
  336. }
  337. }
  338. /**
  339. * @brief GPIO Initialization Function
  340. * @param None
  341. * @retval None
  342. */
  343. static void MX_GPIO_Init(void)
  344. {
  345. /* GPIO Ports Clock Enable */
  346. __HAL_RCC_GPIOI_CLK_ENABLE();
  347. __HAL_RCC_GPIOF_CLK_ENABLE();
  348. __HAL_RCC_GPIOH_CLK_ENABLE();
  349. __HAL_RCC_GPIOC_CLK_ENABLE();
  350. __HAL_RCC_GPIOG_CLK_ENABLE();
  351. __HAL_RCC_GPIOE_CLK_ENABLE();
  352. __HAL_RCC_GPIOD_CLK_ENABLE();
  353. __HAL_RCC_GPIOA_CLK_ENABLE();
  354. }
  355. /* USER CODE BEGIN 4 */
  356. /* USER CODE END 4 */
  357. /**
  358. * @brief This function is executed in case of error occurrence.
  359. * @retval None
  360. */
  361. void Error_Handler(void)
  362. {
  363. /* USER CODE BEGIN Error_Handler_Debug */
  364. /* User can add his own implementation to report the HAL error return state */
  365. /* USER CODE END Error_Handler_Debug */
  366. }
  367. #ifdef USE_FULL_ASSERT
  368. /**
  369. * @brief Reports the name of the source file and the source line number
  370. * where the assert_param error has occurred.
  371. * @param file: pointer to the source file name
  372. * @param line: assert_param error line source number
  373. * @retval None
  374. */
  375. void assert_failed(uint8_t *file, uint32_t line)
  376. {
  377. /* USER CODE BEGIN 6 */
  378. /* User can add his own implementation to report the file name and line number,
  379. tex: printf("Wrong parameters value: file %s on line %d\r\n", file, line) */
  380. /* USER CODE END 6 */
  381. }
  382. #endif /* USE_FULL_ASSERT */
  383. /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/