main.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526
  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. ADC_HandleTypeDef hadc1;
  56. ETH_HandleTypeDef heth;
  57. IWDG_HandleTypeDef hiwdg;
  58. RTC_HandleTypeDef hrtc;
  59. SPI_HandleTypeDef hspi5;
  60. UART_HandleTypeDef huart1;
  61. SDRAM_HandleTypeDef hsdram1;
  62. /* USER CODE BEGIN PV */
  63. /* USER CODE END PV */
  64. /* Private function prototypes -----------------------------------------------*/
  65. void SystemClock_Config(void);
  66. static void MX_GPIO_Init(void);
  67. static void MX_USART1_UART_Init(void);
  68. static void MX_ETH_Init(void);
  69. static void MX_SPI5_Init(void);
  70. static void MX_FMC_Init(void);
  71. static void MX_ADC1_Init(void);
  72. static void MX_RTC_Init(void);
  73. static void MX_IWDG_Init(void);
  74. /* USER CODE BEGIN PFP */
  75. /* USER CODE END PFP */
  76. /* Private user code ---------------------------------------------------------*/
  77. /* USER CODE BEGIN 0 */
  78. /* USER CODE END 0 */
  79. /**
  80. * @brief The application entry point.
  81. * @retval int
  82. */
  83. int main(void)
  84. {
  85. /* USER CODE BEGIN 1 */
  86. /* USER CODE END 1 */
  87. /* MCU Configuration--------------------------------------------------------*/
  88. /* Reset of all peripherals, Initializes the Flash interface and the Systick. */
  89. HAL_Init();
  90. /* USER CODE BEGIN Init */
  91. /* USER CODE END Init */
  92. /* Configure the system clock */
  93. SystemClock_Config();
  94. /* USER CODE BEGIN SysInit */
  95. /* USER CODE END SysInit */
  96. /* Initialize all configured peripherals */
  97. MX_GPIO_Init();
  98. MX_USART1_UART_Init();
  99. MX_ETH_Init();
  100. MX_SPI5_Init();
  101. MX_FMC_Init();
  102. MX_ADC1_Init();
  103. MX_RTC_Init();
  104. MX_IWDG_Init();
  105. /* USER CODE BEGIN 2 */
  106. /* USER CODE END 2 */
  107. /* Infinite loop */
  108. /* USER CODE BEGIN WHILE */
  109. while (1)
  110. {
  111. /* USER CODE END WHILE */
  112. /* USER CODE BEGIN 3 */
  113. }
  114. /* USER CODE END 3 */
  115. }
  116. /**
  117. * @brief System Clock Configuration
  118. * @retval None
  119. */
  120. void SystemClock_Config(void)
  121. {
  122. RCC_OscInitTypeDef RCC_OscInitStruct = {0};
  123. RCC_ClkInitTypeDef RCC_ClkInitStruct = {0};
  124. RCC_PeriphCLKInitTypeDef PeriphClkInitStruct = {0};
  125. /**Configure the main internal regulator output voltage
  126. */
  127. __HAL_RCC_PWR_CLK_ENABLE();
  128. __HAL_PWR_VOLTAGESCALING_CONFIG(PWR_REGULATOR_VOLTAGE_SCALE1);
  129. /**Initializes the CPU, AHB and APB busses clocks
  130. */
  131. RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_LSI|RCC_OSCILLATORTYPE_HSE
  132. |RCC_OSCILLATORTYPE_LSE;
  133. RCC_OscInitStruct.HSEState = RCC_HSE_ON;
  134. RCC_OscInitStruct.LSEState = RCC_LSE_ON;
  135. RCC_OscInitStruct.LSIState = RCC_LSI_ON;
  136. RCC_OscInitStruct.PLL.PLLState = RCC_PLL_ON;
  137. RCC_OscInitStruct.PLL.PLLSource = RCC_PLLSOURCE_HSE;
  138. RCC_OscInitStruct.PLL.PLLM = 15;
  139. RCC_OscInitStruct.PLL.PLLN = 216;
  140. RCC_OscInitStruct.PLL.PLLP = RCC_PLLP_DIV2;
  141. RCC_OscInitStruct.PLL.PLLQ = 4;
  142. if (HAL_RCC_OscConfig(&RCC_OscInitStruct) != HAL_OK)
  143. {
  144. Error_Handler();
  145. }
  146. /**Activate the Over-Drive mode
  147. */
  148. if (HAL_PWREx_EnableOverDrive() != HAL_OK)
  149. {
  150. Error_Handler();
  151. }
  152. /**Initializes the CPU, AHB and APB busses clocks
  153. */
  154. RCC_ClkInitStruct.ClockType = RCC_CLOCKTYPE_HCLK|RCC_CLOCKTYPE_SYSCLK
  155. |RCC_CLOCKTYPE_PCLK1|RCC_CLOCKTYPE_PCLK2;
  156. RCC_ClkInitStruct.SYSCLKSource = RCC_SYSCLKSOURCE_PLLCLK;
  157. RCC_ClkInitStruct.AHBCLKDivider = RCC_SYSCLK_DIV1;
  158. RCC_ClkInitStruct.APB1CLKDivider = RCC_HCLK_DIV4;
  159. RCC_ClkInitStruct.APB2CLKDivider = RCC_HCLK_DIV2;
  160. if (HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_5) != HAL_OK)
  161. {
  162. Error_Handler();
  163. }
  164. PeriphClkInitStruct.PeriphClockSelection = RCC_PERIPHCLK_RTC;
  165. PeriphClkInitStruct.RTCClockSelection = RCC_RTCCLKSOURCE_LSE;
  166. if (HAL_RCCEx_PeriphCLKConfig(&PeriphClkInitStruct) != HAL_OK)
  167. {
  168. Error_Handler();
  169. }
  170. }
  171. /**
  172. * @brief ADC1 Initialization Function
  173. * @param None
  174. * @retval None
  175. */
  176. static void MX_ADC1_Init(void)
  177. {
  178. /* USER CODE BEGIN ADC1_Init 0 */
  179. /* USER CODE END ADC1_Init 0 */
  180. ADC_ChannelConfTypeDef sConfig = {0};
  181. /* USER CODE BEGIN ADC1_Init 1 */
  182. /* USER CODE END ADC1_Init 1 */
  183. /**Configure the global features of the ADC (Clock, Resolution, Data Alignment and number of conversion)
  184. */
  185. hadc1.Instance = ADC1;
  186. hadc1.Init.ClockPrescaler = ADC_CLOCK_SYNC_PCLK_DIV4;
  187. hadc1.Init.Resolution = ADC_RESOLUTION_12B;
  188. hadc1.Init.ScanConvMode = DISABLE;
  189. hadc1.Init.ContinuousConvMode = DISABLE;
  190. hadc1.Init.DiscontinuousConvMode = DISABLE;
  191. hadc1.Init.ExternalTrigConvEdge = ADC_EXTERNALTRIGCONVEDGE_NONE;
  192. hadc1.Init.ExternalTrigConv = ADC_SOFTWARE_START;
  193. hadc1.Init.DataAlign = ADC_DATAALIGN_RIGHT;
  194. hadc1.Init.NbrOfConversion = 1;
  195. hadc1.Init.DMAContinuousRequests = DISABLE;
  196. hadc1.Init.EOCSelection = ADC_EOC_SINGLE_CONV;
  197. if (HAL_ADC_Init(&hadc1) != HAL_OK)
  198. {
  199. Error_Handler();
  200. }
  201. /**Configure for the selected ADC regular channel its corresponding rank in the sequencer and its sample time.
  202. */
  203. sConfig.Channel = ADC_CHANNEL_13;
  204. sConfig.Rank = 1;
  205. sConfig.SamplingTime = ADC_SAMPLETIME_3CYCLES;
  206. if (HAL_ADC_ConfigChannel(&hadc1, &sConfig) != HAL_OK)
  207. {
  208. Error_Handler();
  209. }
  210. /* USER CODE BEGIN ADC1_Init 2 */
  211. /* USER CODE END ADC1_Init 2 */
  212. }
  213. /**
  214. * @brief ETH Initialization Function
  215. * @param None
  216. * @retval None
  217. */
  218. static void MX_ETH_Init(void)
  219. {
  220. /* USER CODE BEGIN ETH_Init 0 */
  221. /* USER CODE END ETH_Init 0 */
  222. uint8_t MACAddr[6] ;
  223. /* USER CODE BEGIN ETH_Init 1 */
  224. /* USER CODE END ETH_Init 1 */
  225. heth.Instance = ETH;
  226. heth.Init.AutoNegotiation = ETH_AUTONEGOTIATION_ENABLE;
  227. heth.Init.PhyAddress = LAN8742A_PHY_ADDRESS;
  228. MACAddr[0] = 0x00;
  229. MACAddr[1] = 0x80;
  230. MACAddr[2] = 0xE1;
  231. MACAddr[3] = 0x00;
  232. MACAddr[4] = 0x00;
  233. MACAddr[5] = 0x00;
  234. heth.Init.MACAddr = &MACAddr[0];
  235. heth.Init.RxMode = ETH_RXPOLLING_MODE;
  236. heth.Init.ChecksumMode = ETH_CHECKSUM_BY_HARDWARE;
  237. heth.Init.MediaInterface = ETH_MEDIA_INTERFACE_RMII;
  238. /* USER CODE BEGIN MACADDRESS */
  239. /* USER CODE END MACADDRESS */
  240. if (HAL_ETH_Init(&heth) != HAL_OK)
  241. {
  242. Error_Handler();
  243. }
  244. /* USER CODE BEGIN ETH_Init 2 */
  245. /* USER CODE END ETH_Init 2 */
  246. }
  247. /**
  248. * @brief IWDG Initialization Function
  249. * @param None
  250. * @retval None
  251. */
  252. static void MX_IWDG_Init(void)
  253. {
  254. /* USER CODE BEGIN IWDG_Init 0 */
  255. /* USER CODE END IWDG_Init 0 */
  256. /* USER CODE BEGIN IWDG_Init 1 */
  257. /* USER CODE END IWDG_Init 1 */
  258. hiwdg.Instance = IWDG;
  259. hiwdg.Init.Prescaler = IWDG_PRESCALER_4;
  260. hiwdg.Init.Reload = 4095;
  261. if (HAL_IWDG_Init(&hiwdg) != HAL_OK)
  262. {
  263. Error_Handler();
  264. }
  265. /* USER CODE BEGIN IWDG_Init 2 */
  266. /* USER CODE END IWDG_Init 2 */
  267. }
  268. /**
  269. * @brief RTC Initialization Function
  270. * @param None
  271. * @retval None
  272. */
  273. static void MX_RTC_Init(void)
  274. {
  275. /* USER CODE BEGIN RTC_Init 0 */
  276. /* USER CODE END RTC_Init 0 */
  277. /* USER CODE BEGIN RTC_Init 1 */
  278. /* USER CODE END RTC_Init 1 */
  279. /**Initialize RTC Only
  280. */
  281. hrtc.Instance = RTC;
  282. hrtc.Init.HourFormat = RTC_HOURFORMAT_24;
  283. hrtc.Init.AsynchPrediv = 127;
  284. hrtc.Init.SynchPrediv = 255;
  285. hrtc.Init.OutPut = RTC_OUTPUT_DISABLE;
  286. hrtc.Init.OutPutPolarity = RTC_OUTPUT_POLARITY_HIGH;
  287. hrtc.Init.OutPutType = RTC_OUTPUT_TYPE_OPENDRAIN;
  288. if (HAL_RTC_Init(&hrtc) != HAL_OK)
  289. {
  290. Error_Handler();
  291. }
  292. /* USER CODE BEGIN RTC_Init 2 */
  293. /* USER CODE END RTC_Init 2 */
  294. }
  295. /**
  296. * @brief SPI5 Initialization Function
  297. * @param None
  298. * @retval None
  299. */
  300. static void MX_SPI5_Init(void)
  301. {
  302. /* USER CODE BEGIN SPI5_Init 0 */
  303. /* USER CODE END SPI5_Init 0 */
  304. /* USER CODE BEGIN SPI5_Init 1 */
  305. /* USER CODE END SPI5_Init 1 */
  306. /* SPI5 parameter configuration*/
  307. hspi5.Instance = SPI5;
  308. hspi5.Init.Mode = SPI_MODE_MASTER;
  309. hspi5.Init.Direction = SPI_DIRECTION_2LINES;
  310. hspi5.Init.DataSize = SPI_DATASIZE_8BIT;
  311. hspi5.Init.CLKPolarity = SPI_POLARITY_LOW;
  312. hspi5.Init.CLKPhase = SPI_PHASE_1EDGE;
  313. hspi5.Init.NSS = SPI_NSS_SOFT;
  314. hspi5.Init.BaudRatePrescaler = SPI_BAUDRATEPRESCALER_2;
  315. hspi5.Init.FirstBit = SPI_FIRSTBIT_MSB;
  316. hspi5.Init.TIMode = SPI_TIMODE_DISABLE;
  317. hspi5.Init.CRCCalculation = SPI_CRCCALCULATION_DISABLE;
  318. hspi5.Init.CRCPolynomial = 10;
  319. if (HAL_SPI_Init(&hspi5) != HAL_OK)
  320. {
  321. Error_Handler();
  322. }
  323. /* USER CODE BEGIN SPI5_Init 2 */
  324. /* USER CODE END SPI5_Init 2 */
  325. }
  326. /**
  327. * @brief USART1 Initialization Function
  328. * @param None
  329. * @retval None
  330. */
  331. static void MX_USART1_UART_Init(void)
  332. {
  333. /* USER CODE BEGIN USART1_Init 0 */
  334. /* USER CODE END USART1_Init 0 */
  335. /* USER CODE BEGIN USART1_Init 1 */
  336. /* USER CODE END USART1_Init 1 */
  337. huart1.Instance = USART1;
  338. huart1.Init.BaudRate = 115200;
  339. huart1.Init.WordLength = UART_WORDLENGTH_8B;
  340. huart1.Init.StopBits = UART_STOPBITS_1;
  341. huart1.Init.Parity = UART_PARITY_NONE;
  342. huart1.Init.Mode = UART_MODE_TX_RX;
  343. huart1.Init.HwFlowCtl = UART_HWCONTROL_NONE;
  344. huart1.Init.OverSampling = UART_OVERSAMPLING_16;
  345. if (HAL_UART_Init(&huart1) != HAL_OK)
  346. {
  347. Error_Handler();
  348. }
  349. /* USER CODE BEGIN USART1_Init 2 */
  350. /* USER CODE END USART1_Init 2 */
  351. }
  352. /* FMC initialization function */
  353. static void MX_FMC_Init(void)
  354. {
  355. FMC_SDRAM_TimingTypeDef SdramTiming;
  356. /** Perform the SDRAM1 memory initialization sequence
  357. */
  358. hsdram1.Instance = FMC_SDRAM_DEVICE;
  359. /* hsdram1.Init */
  360. hsdram1.Init.SDBank = FMC_SDRAM_BANK2;
  361. hsdram1.Init.ColumnBitsNumber = FMC_SDRAM_COLUMN_BITS_NUM_8;
  362. hsdram1.Init.RowBitsNumber = FMC_SDRAM_ROW_BITS_NUM_12;
  363. hsdram1.Init.MemoryDataWidth = FMC_SDRAM_MEM_BUS_WIDTH_16;
  364. hsdram1.Init.InternalBankNumber = FMC_SDRAM_INTERN_BANKS_NUM_4;
  365. hsdram1.Init.CASLatency = FMC_SDRAM_CAS_LATENCY_1;
  366. hsdram1.Init.WriteProtection = FMC_SDRAM_WRITE_PROTECTION_DISABLE;
  367. hsdram1.Init.SDClockPeriod = FMC_SDRAM_CLOCK_DISABLE;
  368. hsdram1.Init.ReadBurst = FMC_SDRAM_RBURST_DISABLE;
  369. hsdram1.Init.ReadPipeDelay = FMC_SDRAM_RPIPE_DELAY_0;
  370. /* SdramTiming */
  371. SdramTiming.LoadToActiveDelay = 16;
  372. SdramTiming.ExitSelfRefreshDelay = 16;
  373. SdramTiming.SelfRefreshTime = 16;
  374. SdramTiming.RowCycleDelay = 16;
  375. SdramTiming.WriteRecoveryTime = 16;
  376. SdramTiming.RPDelay = 16;
  377. SdramTiming.RCDDelay = 16;
  378. if (HAL_SDRAM_Init(&hsdram1, &SdramTiming) != HAL_OK)
  379. {
  380. Error_Handler( );
  381. }
  382. }
  383. /**
  384. * @brief GPIO Initialization Function
  385. * @param None
  386. * @retval None
  387. */
  388. static void MX_GPIO_Init(void)
  389. {
  390. /* GPIO Ports Clock Enable */
  391. __HAL_RCC_GPIOC_CLK_ENABLE();
  392. __HAL_RCC_GPIOF_CLK_ENABLE();
  393. __HAL_RCC_GPIOH_CLK_ENABLE();
  394. __HAL_RCC_GPIOA_CLK_ENABLE();
  395. __HAL_RCC_GPIOG_CLK_ENABLE();
  396. __HAL_RCC_GPIOE_CLK_ENABLE();
  397. __HAL_RCC_GPIOB_CLK_ENABLE();
  398. __HAL_RCC_GPIOD_CLK_ENABLE();
  399. }
  400. /* USER CODE BEGIN 4 */
  401. /* USER CODE END 4 */
  402. /**
  403. * @brief This function is executed in case of error occurrence.
  404. * @retval None
  405. */
  406. void Error_Handler(void)
  407. {
  408. /* USER CODE BEGIN Error_Handler_Debug */
  409. /* User can add his own implementation to report the HAL error return state */
  410. /* USER CODE END Error_Handler_Debug */
  411. }
  412. #ifdef USE_FULL_ASSERT
  413. /**
  414. * @brief Reports the name of the source file and the source line number
  415. * where the assert_param error has occurred.
  416. * @param file: pointer to the source file name
  417. * @param line: assert_param error line source number
  418. * @retval None
  419. */
  420. void assert_failed(uint8_t *file, uint32_t line)
  421. {
  422. /* USER CODE BEGIN 6 */
  423. /* User can add his own implementation to report the file name and line number,
  424. tex: printf("Wrong parameters value: file %s on line %d\r\n", file, line) */
  425. /* USER CODE END 6 */
  426. }
  427. #endif /* USE_FULL_ASSERT */
  428. /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/