main.c 13 KB

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