main.c 14 KB

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