main.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421
  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. IWDG_HandleTypeDef hiwdg;
  57. RTC_HandleTypeDef hrtc;
  58. SPI_HandleTypeDef hspi2;
  59. UART_HandleTypeDef huart1;
  60. /* USER CODE BEGIN PV */
  61. /* Private variables ---------------------------------------------------------*/
  62. /* USER CODE END PV */
  63. /* Private function prototypes -----------------------------------------------*/
  64. void SystemClock_Config(void);
  65. static void MX_GPIO_Init(void);
  66. static void MX_USART1_UART_Init(void);
  67. static void MX_SPI2_Init(void);
  68. static void MX_RTC_Init(void);
  69. static void MX_IWDG_Init(void);
  70. static void MX_ADC1_Init(void);
  71. /* USER CODE BEGIN PFP */
  72. /* Private function prototypes -----------------------------------------------*/
  73. /* USER CODE END PFP */
  74. /* Private user code ---------------------------------------------------------*/
  75. /* USER CODE BEGIN 0 */
  76. /* USER CODE END 0 */
  77. /**
  78. * @brief The application entry point.
  79. * @retval int
  80. */
  81. int main(void)
  82. {
  83. /* USER CODE BEGIN 1 */
  84. /* USER CODE END 1 */
  85. /* MCU Configuration--------------------------------------------------------*/
  86. /* Reset of all peripherals, Initializes the Flash interface and the Systick. */
  87. HAL_Init();
  88. /* USER CODE BEGIN Init */
  89. /* USER CODE END Init */
  90. /* Configure the system clock */
  91. SystemClock_Config();
  92. /* USER CODE BEGIN SysInit */
  93. /* USER CODE END SysInit */
  94. /* Initialize all configured peripherals */
  95. MX_GPIO_Init();
  96. MX_USART1_UART_Init();
  97. MX_SPI2_Init();
  98. MX_RTC_Init();
  99. MX_IWDG_Init();
  100. MX_ADC1_Init();
  101. /* USER CODE BEGIN 2 */
  102. /* USER CODE END 2 */
  103. /* Infinite loop */
  104. /* USER CODE BEGIN WHILE */
  105. while (1)
  106. {
  107. /* USER CODE END WHILE */
  108. /* USER CODE BEGIN 3 */
  109. }
  110. /* USER CODE END 3 */
  111. }
  112. /**
  113. * @brief System Clock Configuration
  114. * @retval None
  115. */
  116. void SystemClock_Config(void)
  117. {
  118. RCC_OscInitTypeDef RCC_OscInitStruct = {0};
  119. RCC_ClkInitTypeDef RCC_ClkInitStruct = {0};
  120. RCC_PeriphCLKInitTypeDef PeriphClkInit = {0};
  121. /**Initializes the CPU, AHB and APB busses clocks
  122. */
  123. RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_LSI|RCC_OSCILLATORTYPE_HSE
  124. |RCC_OSCILLATORTYPE_LSE;
  125. RCC_OscInitStruct.HSEState = RCC_HSE_ON;
  126. RCC_OscInitStruct.HSEPredivValue = RCC_HSE_PREDIV_DIV1;
  127. RCC_OscInitStruct.LSEState = RCC_LSE_ON;
  128. RCC_OscInitStruct.HSIState = RCC_HSI_ON;
  129. RCC_OscInitStruct.LSIState = RCC_LSI_ON;
  130. RCC_OscInitStruct.PLL.PLLState = RCC_PLL_ON;
  131. RCC_OscInitStruct.PLL.PLLSource = RCC_PLLSOURCE_HSE;
  132. RCC_OscInitStruct.PLL.PLLMUL = RCC_PLL_MUL9;
  133. if (HAL_RCC_OscConfig(&RCC_OscInitStruct) != HAL_OK)
  134. {
  135. Error_Handler();
  136. }
  137. /**Initializes the CPU, AHB and APB busses clocks
  138. */
  139. RCC_ClkInitStruct.ClockType = RCC_CLOCKTYPE_HCLK|RCC_CLOCKTYPE_SYSCLK
  140. |RCC_CLOCKTYPE_PCLK1|RCC_CLOCKTYPE_PCLK2;
  141. RCC_ClkInitStruct.SYSCLKSource = RCC_SYSCLKSOURCE_PLLCLK;
  142. RCC_ClkInitStruct.AHBCLKDivider = RCC_SYSCLK_DIV1;
  143. RCC_ClkInitStruct.APB1CLKDivider = RCC_HCLK_DIV2;
  144. RCC_ClkInitStruct.APB2CLKDivider = RCC_HCLK_DIV1;
  145. if (HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_2) != HAL_OK)
  146. {
  147. Error_Handler();
  148. }
  149. PeriphClkInit.PeriphClockSelection = RCC_PERIPHCLK_RTC|RCC_PERIPHCLK_ADC;
  150. PeriphClkInit.RTCClockSelection = RCC_RTCCLKSOURCE_LSE;
  151. PeriphClkInit.AdcClockSelection = RCC_ADCPCLK2_DIV2;
  152. if (HAL_RCCEx_PeriphCLKConfig(&PeriphClkInit) != HAL_OK)
  153. {
  154. Error_Handler();
  155. }
  156. }
  157. /**
  158. * @brief ADC1 Initialization Function
  159. * @param None
  160. * @retval None
  161. */
  162. static void MX_ADC1_Init(void)
  163. {
  164. /* USER CODE BEGIN ADC1_Init 0 */
  165. /* USER CODE END ADC1_Init 0 */
  166. ADC_ChannelConfTypeDef sConfig = {0};
  167. /* USER CODE BEGIN ADC1_Init 1 */
  168. /* USER CODE END ADC1_Init 1 */
  169. /**Common config
  170. */
  171. hadc1.Instance = ADC1;
  172. hadc1.Init.ScanConvMode = ADC_SCAN_DISABLE;
  173. hadc1.Init.ContinuousConvMode = DISABLE;
  174. hadc1.Init.DiscontinuousConvMode = DISABLE;
  175. hadc1.Init.ExternalTrigConv = ADC_SOFTWARE_START;
  176. hadc1.Init.DataAlign = ADC_DATAALIGN_RIGHT;
  177. hadc1.Init.NbrOfConversion = 1;
  178. if (HAL_ADC_Init(&hadc1) != HAL_OK)
  179. {
  180. Error_Handler();
  181. }
  182. /**Configure Regular Channel
  183. */
  184. sConfig.Channel = ADC_CHANNEL_9;
  185. sConfig.Rank = ADC_REGULAR_RANK_1;
  186. sConfig.SamplingTime = ADC_SAMPLETIME_1CYCLE_5;
  187. if (HAL_ADC_ConfigChannel(&hadc1, &sConfig) != HAL_OK)
  188. {
  189. Error_Handler();
  190. }
  191. /* USER CODE BEGIN ADC1_Init 2 */
  192. /* USER CODE END ADC1_Init 2 */
  193. }
  194. /**
  195. * @brief IWDG Initialization Function
  196. * @param None
  197. * @retval None
  198. */
  199. static void MX_IWDG_Init(void)
  200. {
  201. /* USER CODE BEGIN IWDG_Init 0 */
  202. /* USER CODE END IWDG_Init 0 */
  203. /* USER CODE BEGIN IWDG_Init 1 */
  204. /* USER CODE END IWDG_Init 1 */
  205. hiwdg.Instance = IWDG;
  206. hiwdg.Init.Prescaler = IWDG_PRESCALER_4;
  207. hiwdg.Init.Reload = 4095;
  208. if (HAL_IWDG_Init(&hiwdg) != HAL_OK)
  209. {
  210. Error_Handler();
  211. }
  212. /* USER CODE BEGIN IWDG_Init 2 */
  213. /* USER CODE END IWDG_Init 2 */
  214. }
  215. /**
  216. * @brief RTC Initialization Function
  217. * @param None
  218. * @retval None
  219. */
  220. static void MX_RTC_Init(void)
  221. {
  222. /* USER CODE BEGIN RTC_Init 0 */
  223. /* USER CODE END RTC_Init 0 */
  224. /* USER CODE BEGIN RTC_Init 1 */
  225. /* USER CODE END RTC_Init 1 */
  226. /**Initialize RTC Only
  227. */
  228. hrtc.Instance = RTC;
  229. hrtc.Init.AsynchPrediv = RTC_AUTO_1_SECOND;
  230. hrtc.Init.OutPut = RTC_OUTPUTSOURCE_ALARM;
  231. if (HAL_RTC_Init(&hrtc) != HAL_OK)
  232. {
  233. Error_Handler();
  234. }
  235. /* USER CODE BEGIN RTC_Init 2 */
  236. /* USER CODE END RTC_Init 2 */
  237. }
  238. /**
  239. * @brief SPI2 Initialization Function
  240. * @param None
  241. * @retval None
  242. */
  243. static void MX_SPI2_Init(void)
  244. {
  245. /* USER CODE BEGIN SPI2_Init 0 */
  246. /* USER CODE END SPI2_Init 0 */
  247. /* USER CODE BEGIN SPI2_Init 1 */
  248. /* USER CODE END SPI2_Init 1 */
  249. /* SPI2 parameter configuration*/
  250. hspi2.Instance = SPI2;
  251. hspi2.Init.Mode = SPI_MODE_MASTER;
  252. hspi2.Init.Direction = SPI_DIRECTION_2LINES;
  253. hspi2.Init.DataSize = SPI_DATASIZE_8BIT;
  254. hspi2.Init.CLKPolarity = SPI_POLARITY_LOW;
  255. hspi2.Init.CLKPhase = SPI_PHASE_1EDGE;
  256. hspi2.Init.NSS = SPI_NSS_SOFT;
  257. hspi2.Init.BaudRatePrescaler = SPI_BAUDRATEPRESCALER_2;
  258. hspi2.Init.FirstBit = SPI_FIRSTBIT_MSB;
  259. hspi2.Init.TIMode = SPI_TIMODE_DISABLE;
  260. hspi2.Init.CRCCalculation = SPI_CRCCALCULATION_DISABLE;
  261. hspi2.Init.CRCPolynomial = 10;
  262. if (HAL_SPI_Init(&hspi2) != HAL_OK)
  263. {
  264. Error_Handler();
  265. }
  266. /* USER CODE BEGIN SPI2_Init 2 */
  267. /* USER CODE END SPI2_Init 2 */
  268. }
  269. /**
  270. * @brief USART1 Initialization Function
  271. * @param None
  272. * @retval None
  273. */
  274. static void MX_USART1_UART_Init(void)
  275. {
  276. /* USER CODE BEGIN USART1_Init 0 */
  277. /* USER CODE END USART1_Init 0 */
  278. /* USER CODE BEGIN USART1_Init 1 */
  279. /* USER CODE END USART1_Init 1 */
  280. huart1.Instance = USART1;
  281. huart1.Init.BaudRate = 115200;
  282. huart1.Init.WordLength = UART_WORDLENGTH_8B;
  283. huart1.Init.StopBits = UART_STOPBITS_1;
  284. huart1.Init.Parity = UART_PARITY_NONE;
  285. huart1.Init.Mode = UART_MODE_TX_RX;
  286. huart1.Init.HwFlowCtl = UART_HWCONTROL_NONE;
  287. huart1.Init.OverSampling = UART_OVERSAMPLING_16;
  288. if (HAL_UART_Init(&huart1) != HAL_OK)
  289. {
  290. Error_Handler();
  291. }
  292. /* USER CODE BEGIN USART1_Init 2 */
  293. /* USER CODE END USART1_Init 2 */
  294. }
  295. /**
  296. * @brief GPIO Initialization Function
  297. * @param None
  298. * @retval None
  299. */
  300. static void MX_GPIO_Init(void)
  301. {
  302. /* GPIO Ports Clock Enable */
  303. __HAL_RCC_GPIOC_CLK_ENABLE();
  304. __HAL_RCC_GPIOD_CLK_ENABLE();
  305. __HAL_RCC_GPIOB_CLK_ENABLE();
  306. __HAL_RCC_GPIOA_CLK_ENABLE();
  307. }
  308. /* USER CODE BEGIN 4 */
  309. /* USER CODE END 4 */
  310. /**
  311. * @brief This function is executed in case of error occurrence.
  312. * @retval None
  313. */
  314. void Error_Handler(void)
  315. {
  316. /* USER CODE BEGIN Error_Handler_Debug */
  317. /* User can add his own implementation to report the HAL error return state */
  318. while(1)
  319. {
  320. }
  321. /* USER CODE END Error_Handler_Debug */
  322. }
  323. #ifdef USE_FULL_ASSERT
  324. /**
  325. * @brief Reports the name of the source file and the source line number
  326. * where the assert_param error has occurred.
  327. * @param file: pointer to the source file name
  328. * @param line: assert_param error line source number
  329. * @retval None
  330. */
  331. void assert_failed(uint8_t *file, uint32_t line)
  332. {
  333. /* USER CODE BEGIN 6 */
  334. /* User can add his own implementation to report the file name and line number,
  335. tex: printf("Wrong parameters value: file %s on line %d\r\n", file, line) */
  336. /* USER CODE END 6 */
  337. }
  338. #endif /* USE_FULL_ASSERT */
  339. /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/