main.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454
  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) 2018 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. ETH_HandleTypeDef heth;
  56. QSPI_HandleTypeDef hqspi;
  57. SD_HandleTypeDef hsd1;
  58. UART_HandleTypeDef huart1;
  59. SDRAM_HandleTypeDef hsdram1;
  60. /* USER CODE BEGIN PV */
  61. /* USER CODE END PV */
  62. /* Private function prototypes -----------------------------------------------*/
  63. void SystemClock_Config(void);
  64. static void MX_GPIO_Init(void);
  65. static void MX_USART1_UART_Init(void);
  66. static void MX_QUADSPI_Init(void);
  67. static void MX_ETH_Init(void);
  68. static void MX_FMC_Init(void);
  69. static void MX_SDMMC1_SD_Init(void);
  70. /* USER CODE BEGIN PFP */
  71. /* USER CODE END PFP */
  72. /* Private user code ---------------------------------------------------------*/
  73. /* USER CODE BEGIN 0 */
  74. /* USER CODE END 0 */
  75. /**
  76. * @brief The application entry point.
  77. * @retval int
  78. */
  79. int main(void)
  80. {
  81. /* USER CODE BEGIN 1 */
  82. /* USER CODE END 1 */
  83. /* MCU Configuration--------------------------------------------------------*/
  84. /* Reset of all peripherals, Initializes the Flash interface and the Systick. */
  85. HAL_Init();
  86. /* USER CODE BEGIN Init */
  87. /* USER CODE END Init */
  88. /* Configure the system clock */
  89. SystemClock_Config();
  90. /* USER CODE BEGIN SysInit */
  91. /* USER CODE END SysInit */
  92. /* Initialize all configured peripherals */
  93. MX_GPIO_Init();
  94. MX_USART1_UART_Init();
  95. MX_QUADSPI_Init();
  96. MX_ETH_Init();
  97. MX_FMC_Init();
  98. MX_SDMMC1_SD_Init();
  99. /* USER CODE BEGIN 2 */
  100. /* USER CODE END 2 */
  101. /* Infinite loop */
  102. /* USER CODE BEGIN WHILE */
  103. while (1)
  104. {
  105. /* USER CODE END WHILE */
  106. /* USER CODE BEGIN 3 */
  107. }
  108. /* USER CODE END 3 */
  109. }
  110. /**
  111. * @brief System Clock Configuration
  112. * @retval None
  113. */
  114. void SystemClock_Config(void)
  115. {
  116. RCC_OscInitTypeDef RCC_OscInitStruct = {0};
  117. RCC_ClkInitTypeDef RCC_ClkInitStruct = {0};
  118. RCC_PeriphCLKInitTypeDef PeriphClkInitStruct = {0};
  119. /**Configure LSE Drive Capability
  120. */
  121. HAL_PWR_EnableBkUpAccess();
  122. /**Configure the main internal regulator output voltage
  123. */
  124. __HAL_RCC_PWR_CLK_ENABLE();
  125. __HAL_PWR_VOLTAGESCALING_CONFIG(PWR_REGULATOR_VOLTAGE_SCALE3);
  126. /**Initializes the CPU, AHB and APB busses clocks
  127. */
  128. RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_HSE;
  129. RCC_OscInitStruct.HSEState = RCC_HSE_ON;
  130. RCC_OscInitStruct.PLL.PLLState = RCC_PLL_ON;
  131. RCC_OscInitStruct.PLL.PLLSource = RCC_PLLSOURCE_HSE;
  132. RCC_OscInitStruct.PLL.PLLM = 12;
  133. RCC_OscInitStruct.PLL.PLLN = 96;
  134. RCC_OscInitStruct.PLL.PLLP = RCC_PLLP_DIV4;
  135. RCC_OscInitStruct.PLL.PLLQ = 2;
  136. if (HAL_RCC_OscConfig(&RCC_OscInitStruct) != HAL_OK)
  137. {
  138. Error_Handler();
  139. }
  140. /**Initializes the CPU, AHB and APB busses clocks
  141. */
  142. RCC_ClkInitStruct.ClockType = RCC_CLOCKTYPE_HCLK|RCC_CLOCKTYPE_SYSCLK
  143. |RCC_CLOCKTYPE_PCLK1|RCC_CLOCKTYPE_PCLK2;
  144. RCC_ClkInitStruct.SYSCLKSource = RCC_SYSCLKSOURCE_PLLCLK;
  145. RCC_ClkInitStruct.AHBCLKDivider = RCC_SYSCLK_DIV1;
  146. RCC_ClkInitStruct.APB1CLKDivider = RCC_HCLK_DIV4;
  147. RCC_ClkInitStruct.APB2CLKDivider = RCC_HCLK_DIV2;
  148. if (HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_1) != HAL_OK)
  149. {
  150. Error_Handler();
  151. }
  152. PeriphClkInitStruct.PeriphClockSelection = RCC_PERIPHCLK_USART1|RCC_PERIPHCLK_SDMMC1;
  153. PeriphClkInitStruct.Usart1ClockSelection = RCC_USART1CLKSOURCE_PCLK2;
  154. PeriphClkInitStruct.Sdmmc1ClockSelection = RCC_SDMMC1CLKSOURCE_SYSCLK;
  155. if (HAL_RCCEx_PeriphCLKConfig(&PeriphClkInitStruct) != HAL_OK)
  156. {
  157. Error_Handler();
  158. }
  159. }
  160. /**
  161. * @brief ETH Initialization Function
  162. * @param None
  163. * @retval None
  164. */
  165. static void MX_ETH_Init(void)
  166. {
  167. /* USER CODE BEGIN ETH_Init 0 */
  168. /* USER CODE END ETH_Init 0 */
  169. uint8_t MACAddr[6] ;
  170. /* USER CODE BEGIN ETH_Init 1 */
  171. /* USER CODE END ETH_Init 1 */
  172. heth.Instance = ETH;
  173. heth.Init.AutoNegotiation = ETH_AUTONEGOTIATION_ENABLE;
  174. heth.Init.PhyAddress = LAN8742A_PHY_ADDRESS;
  175. MACAddr[0] = 0x00;
  176. MACAddr[1] = 0x80;
  177. MACAddr[2] = 0xE1;
  178. MACAddr[3] = 0x00;
  179. MACAddr[4] = 0x00;
  180. MACAddr[5] = 0x00;
  181. heth.Init.MACAddr = &MACAddr[0];
  182. heth.Init.RxMode = ETH_RXPOLLING_MODE;
  183. heth.Init.ChecksumMode = ETH_CHECKSUM_BY_HARDWARE;
  184. heth.Init.MediaInterface = ETH_MEDIA_INTERFACE_RMII;
  185. /* USER CODE BEGIN MACADDRESS */
  186. /* USER CODE END MACADDRESS */
  187. if (HAL_ETH_Init(&heth) != HAL_OK)
  188. {
  189. Error_Handler();
  190. }
  191. /* USER CODE BEGIN ETH_Init 2 */
  192. /* USER CODE END ETH_Init 2 */
  193. }
  194. /**
  195. * @brief QUADSPI Initialization Function
  196. * @param None
  197. * @retval None
  198. */
  199. static void MX_QUADSPI_Init(void)
  200. {
  201. /* USER CODE BEGIN QUADSPI_Init 0 */
  202. /* USER CODE END QUADSPI_Init 0 */
  203. /* USER CODE BEGIN QUADSPI_Init 1 */
  204. /* USER CODE END QUADSPI_Init 1 */
  205. /* QUADSPI parameter configuration*/
  206. hqspi.Instance = QUADSPI;
  207. hqspi.Init.ClockPrescaler = 1;
  208. hqspi.Init.FifoThreshold = 4;
  209. hqspi.Init.SampleShifting = QSPI_SAMPLE_SHIFTING_HALFCYCLE;
  210. hqspi.Init.FlashSize = 1;
  211. hqspi.Init.ChipSelectHighTime = QSPI_CS_HIGH_TIME_6_CYCLE;
  212. hqspi.Init.ClockMode = QSPI_CLOCK_MODE_0;
  213. hqspi.Init.FlashID = QSPI_FLASH_ID_1;
  214. hqspi.Init.DualFlash = QSPI_DUALFLASH_DISABLE;
  215. if (HAL_QSPI_Init(&hqspi) != HAL_OK)
  216. {
  217. Error_Handler();
  218. }
  219. /* USER CODE BEGIN QUADSPI_Init 2 */
  220. /* USER CODE END QUADSPI_Init 2 */
  221. }
  222. /**
  223. * @brief SDMMC1 Initialization Function
  224. * @param None
  225. * @retval None
  226. */
  227. static void MX_SDMMC1_SD_Init(void)
  228. {
  229. /* USER CODE BEGIN SDMMC1_Init 0 */
  230. /* USER CODE END SDMMC1_Init 0 */
  231. /* USER CODE BEGIN SDMMC1_Init 1 */
  232. /* USER CODE END SDMMC1_Init 1 */
  233. hsd1.Instance = SDMMC1;
  234. hsd1.Init.ClockEdge = SDMMC_CLOCK_EDGE_RISING;
  235. hsd1.Init.ClockBypass = SDMMC_CLOCK_BYPASS_DISABLE;
  236. hsd1.Init.ClockPowerSave = SDMMC_CLOCK_POWER_SAVE_DISABLE;
  237. hsd1.Init.BusWide = SDMMC_BUS_WIDE_1B;
  238. hsd1.Init.HardwareFlowControl = SDMMC_HARDWARE_FLOW_CONTROL_DISABLE;
  239. hsd1.Init.ClockDiv = 0;
  240. if (HAL_SD_Init(&hsd1) != HAL_OK)
  241. {
  242. Error_Handler();
  243. }
  244. if (HAL_SD_ConfigWideBusOperation(&hsd1, SDMMC_BUS_WIDE_4B) != HAL_OK)
  245. {
  246. Error_Handler();
  247. }
  248. /* USER CODE BEGIN SDMMC1_Init 2 */
  249. /* USER CODE END SDMMC1_Init 2 */
  250. }
  251. /**
  252. * @brief USART1 Initialization Function
  253. * @param None
  254. * @retval None
  255. */
  256. static void MX_USART1_UART_Init(void)
  257. {
  258. /* USER CODE BEGIN USART1_Init 0 */
  259. /* USER CODE END USART1_Init 0 */
  260. /* USER CODE BEGIN USART1_Init 1 */
  261. /* USER CODE END USART1_Init 1 */
  262. huart1.Instance = USART1;
  263. huart1.Init.BaudRate = 115200;
  264. huart1.Init.WordLength = UART_WORDLENGTH_8B;
  265. huart1.Init.StopBits = UART_STOPBITS_1;
  266. huart1.Init.Parity = UART_PARITY_NONE;
  267. huart1.Init.Mode = UART_MODE_TX_RX;
  268. huart1.Init.HwFlowCtl = UART_HWCONTROL_NONE;
  269. huart1.Init.OverSampling = UART_OVERSAMPLING_16;
  270. huart1.Init.OneBitSampling = UART_ONE_BIT_SAMPLE_DISABLE;
  271. huart1.AdvancedInit.AdvFeatureInit = UART_ADVFEATURE_NO_INIT;
  272. if (HAL_UART_Init(&huart1) != HAL_OK)
  273. {
  274. Error_Handler();
  275. }
  276. /* USER CODE BEGIN USART1_Init 2 */
  277. /* USER CODE END USART1_Init 2 */
  278. }
  279. /* FMC initialization function */
  280. static void MX_FMC_Init(void)
  281. {
  282. FMC_SDRAM_TimingTypeDef SdramTiming;
  283. /** Perform the SDRAM1 memory initialization sequence
  284. */
  285. hsdram1.Instance = FMC_SDRAM_DEVICE;
  286. /* hsdram1.Init */
  287. hsdram1.Init.SDBank = FMC_SDRAM_BANK1;
  288. hsdram1.Init.ColumnBitsNumber = FMC_SDRAM_COLUMN_BITS_NUM_8;
  289. hsdram1.Init.RowBitsNumber = FMC_SDRAM_ROW_BITS_NUM_11;
  290. hsdram1.Init.MemoryDataWidth = FMC_SDRAM_MEM_BUS_WIDTH_16;
  291. hsdram1.Init.InternalBankNumber = FMC_SDRAM_INTERN_BANKS_NUM_4;
  292. hsdram1.Init.CASLatency = FMC_SDRAM_CAS_LATENCY_1;
  293. hsdram1.Init.WriteProtection = FMC_SDRAM_WRITE_PROTECTION_DISABLE;
  294. hsdram1.Init.SDClockPeriod = FMC_SDRAM_CLOCK_DISABLE;
  295. hsdram1.Init.ReadBurst = FMC_SDRAM_RBURST_DISABLE;
  296. hsdram1.Init.ReadPipeDelay = FMC_SDRAM_RPIPE_DELAY_0;
  297. /* SdramTiming */
  298. SdramTiming.LoadToActiveDelay = 16;
  299. SdramTiming.ExitSelfRefreshDelay = 16;
  300. SdramTiming.SelfRefreshTime = 16;
  301. SdramTiming.RowCycleDelay = 16;
  302. SdramTiming.WriteRecoveryTime = 16;
  303. SdramTiming.RPDelay = 16;
  304. SdramTiming.RCDDelay = 16;
  305. if (HAL_SDRAM_Init(&hsdram1, &SdramTiming) != HAL_OK)
  306. {
  307. Error_Handler( );
  308. }
  309. }
  310. /**
  311. * @brief GPIO Initialization Function
  312. * @param None
  313. * @retval None
  314. */
  315. static void MX_GPIO_Init(void)
  316. {
  317. GPIO_InitTypeDef GPIO_InitStruct = {0};
  318. /* GPIO Ports Clock Enable */
  319. __HAL_RCC_GPIOE_CLK_ENABLE();
  320. __HAL_RCC_GPIOG_CLK_ENABLE();
  321. __HAL_RCC_GPIOC_CLK_ENABLE();
  322. __HAL_RCC_GPIOA_CLK_ENABLE();
  323. __HAL_RCC_GPIOB_CLK_ENABLE();
  324. __HAL_RCC_GPIOD_CLK_ENABLE();
  325. __HAL_RCC_GPIOF_CLK_ENABLE();
  326. __HAL_RCC_GPIOK_CLK_ENABLE();
  327. __HAL_RCC_GPIOH_CLK_ENABLE();
  328. __HAL_RCC_GPIOJ_CLK_ENABLE();
  329. /*Configure GPIO pin : PK1 */
  330. GPIO_InitStruct.Pin = GPIO_PIN_1;
  331. GPIO_InitStruct.Mode = GPIO_MODE_AF_PP;
  332. GPIO_InitStruct.Pull = GPIO_NOPULL;
  333. GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW;
  334. GPIO_InitStruct.Alternate = GPIO_AF15_EVENTOUT;
  335. HAL_GPIO_Init(GPIOK, &GPIO_InitStruct);
  336. /*Configure GPIO pin : PJ8 */
  337. GPIO_InitStruct.Pin = GPIO_PIN_8;
  338. GPIO_InitStruct.Mode = GPIO_MODE_IT_RISING;
  339. GPIO_InitStruct.Pull = GPIO_NOPULL;
  340. HAL_GPIO_Init(GPIOJ, &GPIO_InitStruct);
  341. }
  342. /* USER CODE BEGIN 4 */
  343. /* USER CODE END 4 */
  344. /**
  345. * @brief This function is executed in case of error occurrence.
  346. * @retval None
  347. */
  348. void Error_Handler(void)
  349. {
  350. /* USER CODE BEGIN Error_Handler_Debug */
  351. /* User can add his own implementation to report the HAL error return state */
  352. /* USER CODE END Error_Handler_Debug */
  353. }
  354. #ifdef USE_FULL_ASSERT
  355. /**
  356. * @brief Reports the name of the source file and the source line number
  357. * where the assert_param error has occurred.
  358. * @param file: pointer to the source file name
  359. * @param line: assert_param error line source number
  360. * @retval None
  361. */
  362. void assert_failed(uint8_t *file, uint32_t line)
  363. {
  364. /* USER CODE BEGIN 6 */
  365. /* User can add his own implementation to report the file name and line number,
  366. tex: printf("Wrong parameters value: file %s on line %d\r\n", file, line) */
  367. /* USER CODE END 6 */
  368. }
  369. #endif /* USE_FULL_ASSERT */
  370. /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/