main.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502
  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. SPI_HandleTypeDef hspi1;
  57. TIM_HandleTypeDef htim2;
  58. TIM_HandleTypeDef htim3;
  59. TIM_HandleTypeDef htim11;
  60. UART_HandleTypeDef huart2;
  61. /* USER CODE BEGIN PV */
  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_USART2_UART_Init(void);
  67. static void MX_SPI1_Init(void);
  68. static void MX_ADC1_Init(void);
  69. static void MX_TIM2_Init(void);
  70. static void MX_TIM3_Init(void);
  71. static void MX_TIM11_Init(void);
  72. /* USER CODE BEGIN PFP */
  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_USART2_UART_Init();
  97. MX_SPI1_Init();
  98. MX_ADC1_Init();
  99. MX_TIM2_Init();
  100. MX_TIM3_Init();
  101. MX_TIM11_Init();
  102. /* USER CODE BEGIN 2 */
  103. /* USER CODE END 2 */
  104. /* Infinite loop */
  105. /* USER CODE BEGIN WHILE */
  106. while (1)
  107. {
  108. /* USER CODE END WHILE */
  109. /* USER CODE BEGIN 3 */
  110. HAL_GPIO_TogglePin(GPIOA, GPIO_PIN_5);
  111. HAL_Delay(500);
  112. HAL_UART_Transmit(&huart2, "Hello World!\r\n", sizeof("Hello World\r\n"), 0xFFFF);
  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. /** Configure the main internal regulator output voltage
  125. */
  126. __HAL_RCC_PWR_CLK_ENABLE();
  127. __HAL_PWR_VOLTAGESCALING_CONFIG(PWR_REGULATOR_VOLTAGE_SCALE2);
  128. /** Initializes the RCC Oscillators according to the specified parameters
  129. * in the RCC_OscInitTypeDef structure.
  130. */
  131. RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_HSI;
  132. RCC_OscInitStruct.HSIState = RCC_HSI_ON;
  133. RCC_OscInitStruct.HSICalibrationValue = RCC_HSICALIBRATION_DEFAULT;
  134. RCC_OscInitStruct.PLL.PLLState = RCC_PLL_ON;
  135. RCC_OscInitStruct.PLL.PLLSource = RCC_PLLSOURCE_HSI;
  136. RCC_OscInitStruct.PLL.PLLM = 16;
  137. RCC_OscInitStruct.PLL.PLLN = 336;
  138. RCC_OscInitStruct.PLL.PLLP = RCC_PLLP_DIV4;
  139. RCC_OscInitStruct.PLL.PLLQ = 4;
  140. if (HAL_RCC_OscConfig(&RCC_OscInitStruct) != HAL_OK)
  141. {
  142. Error_Handler();
  143. }
  144. /** Initializes the CPU, AHB and APB buses clocks
  145. */
  146. RCC_ClkInitStruct.ClockType = RCC_CLOCKTYPE_HCLK|RCC_CLOCKTYPE_SYSCLK
  147. |RCC_CLOCKTYPE_PCLK1|RCC_CLOCKTYPE_PCLK2;
  148. RCC_ClkInitStruct.SYSCLKSource = RCC_SYSCLKSOURCE_PLLCLK;
  149. RCC_ClkInitStruct.AHBCLKDivider = RCC_SYSCLK_DIV1;
  150. RCC_ClkInitStruct.APB1CLKDivider = RCC_HCLK_DIV2;
  151. RCC_ClkInitStruct.APB2CLKDivider = RCC_HCLK_DIV1;
  152. if (HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_2) != 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. /** Configure the global features of the ADC (Clock, Resolution, Data Alignment and number of conversion)
  170. */
  171. hadc1.Instance = ADC1;
  172. hadc1.Init.ClockPrescaler = ADC_CLOCK_SYNC_PCLK_DIV4;
  173. hadc1.Init.Resolution = ADC_RESOLUTION_12B;
  174. hadc1.Init.ScanConvMode = DISABLE;
  175. hadc1.Init.ContinuousConvMode = DISABLE;
  176. hadc1.Init.DiscontinuousConvMode = DISABLE;
  177. hadc1.Init.ExternalTrigConvEdge = ADC_EXTERNALTRIGCONVEDGE_NONE;
  178. hadc1.Init.ExternalTrigConv = ADC_SOFTWARE_START;
  179. hadc1.Init.DataAlign = ADC_DATAALIGN_RIGHT;
  180. hadc1.Init.NbrOfConversion = 1;
  181. hadc1.Init.DMAContinuousRequests = DISABLE;
  182. hadc1.Init.EOCSelection = ADC_EOC_SINGLE_CONV;
  183. if (HAL_ADC_Init(&hadc1) != HAL_OK)
  184. {
  185. Error_Handler();
  186. }
  187. /** Configure for the selected ADC regular channel its corresponding rank in the sequencer and its sample time.
  188. */
  189. sConfig.Channel = ADC_CHANNEL_0;
  190. sConfig.Rank = 1;
  191. sConfig.SamplingTime = ADC_SAMPLETIME_3CYCLES;
  192. if (HAL_ADC_ConfigChannel(&hadc1, &sConfig) != HAL_OK)
  193. {
  194. Error_Handler();
  195. }
  196. /* USER CODE BEGIN ADC1_Init 2 */
  197. /* USER CODE END ADC1_Init 2 */
  198. }
  199. /**
  200. * @brief SPI1 Initialization Function
  201. * @param None
  202. * @retval None
  203. */
  204. static void MX_SPI1_Init(void)
  205. {
  206. /* USER CODE BEGIN SPI1_Init 0 */
  207. /* USER CODE END SPI1_Init 0 */
  208. /* USER CODE BEGIN SPI1_Init 1 */
  209. /* USER CODE END SPI1_Init 1 */
  210. /* SPI1 parameter configuration*/
  211. hspi1.Instance = SPI1;
  212. hspi1.Init.Mode = SPI_MODE_MASTER;
  213. hspi1.Init.Direction = SPI_DIRECTION_2LINES;
  214. hspi1.Init.DataSize = SPI_DATASIZE_8BIT;
  215. hspi1.Init.CLKPolarity = SPI_POLARITY_LOW;
  216. hspi1.Init.CLKPhase = SPI_PHASE_1EDGE;
  217. hspi1.Init.NSS = SPI_NSS_SOFT;
  218. hspi1.Init.BaudRatePrescaler = SPI_BAUDRATEPRESCALER_2;
  219. hspi1.Init.FirstBit = SPI_FIRSTBIT_MSB;
  220. hspi1.Init.TIMode = SPI_TIMODE_DISABLE;
  221. hspi1.Init.CRCCalculation = SPI_CRCCALCULATION_DISABLE;
  222. hspi1.Init.CRCPolynomial = 10;
  223. if (HAL_SPI_Init(&hspi1) != HAL_OK)
  224. {
  225. Error_Handler();
  226. }
  227. /* USER CODE BEGIN SPI1_Init 2 */
  228. /* USER CODE END SPI1_Init 2 */
  229. }
  230. /**
  231. * @brief TIM2 Initialization Function
  232. * @param None
  233. * @retval None
  234. */
  235. static void MX_TIM2_Init(void)
  236. {
  237. /* USER CODE BEGIN TIM2_Init 0 */
  238. /* USER CODE END TIM2_Init 0 */
  239. TIM_MasterConfigTypeDef sMasterConfig = {0};
  240. TIM_OC_InitTypeDef sConfigOC = {0};
  241. /* USER CODE BEGIN TIM2_Init 1 */
  242. /* USER CODE END TIM2_Init 1 */
  243. htim2.Instance = TIM2;
  244. htim2.Init.Prescaler = 0;
  245. htim2.Init.CounterMode = TIM_COUNTERMODE_UP;
  246. htim2.Init.Period = 4294967295;
  247. htim2.Init.ClockDivision = TIM_CLOCKDIVISION_DIV1;
  248. htim2.Init.AutoReloadPreload = TIM_AUTORELOAD_PRELOAD_DISABLE;
  249. if (HAL_TIM_PWM_Init(&htim2) != HAL_OK)
  250. {
  251. Error_Handler();
  252. }
  253. sMasterConfig.MasterOutputTrigger = TIM_TRGO_RESET;
  254. sMasterConfig.MasterSlaveMode = TIM_MASTERSLAVEMODE_DISABLE;
  255. if (HAL_TIMEx_MasterConfigSynchronization(&htim2, &sMasterConfig) != HAL_OK)
  256. {
  257. Error_Handler();
  258. }
  259. sConfigOC.OCMode = TIM_OCMODE_PWM1;
  260. sConfigOC.Pulse = 0;
  261. sConfigOC.OCPolarity = TIM_OCPOLARITY_HIGH;
  262. sConfigOC.OCFastMode = TIM_OCFAST_DISABLE;
  263. if (HAL_TIM_PWM_ConfigChannel(&htim2, &sConfigOC, TIM_CHANNEL_2) != HAL_OK)
  264. {
  265. Error_Handler();
  266. }
  267. if (HAL_TIM_PWM_ConfigChannel(&htim2, &sConfigOC, TIM_CHANNEL_3) != HAL_OK)
  268. {
  269. Error_Handler();
  270. }
  271. /* USER CODE BEGIN TIM2_Init 2 */
  272. /* USER CODE END TIM2_Init 2 */
  273. HAL_TIM_MspPostInit(&htim2);
  274. }
  275. /**
  276. * @brief TIM3 Initialization Function
  277. * @param None
  278. * @retval None
  279. */
  280. static void MX_TIM3_Init(void)
  281. {
  282. /* USER CODE BEGIN TIM3_Init 0 */
  283. /* USER CODE END TIM3_Init 0 */
  284. TIM_MasterConfigTypeDef sMasterConfig = {0};
  285. TIM_OC_InitTypeDef sConfigOC = {0};
  286. /* USER CODE BEGIN TIM3_Init 1 */
  287. /* USER CODE END TIM3_Init 1 */
  288. htim3.Instance = TIM3;
  289. htim3.Init.Prescaler = 0;
  290. htim3.Init.CounterMode = TIM_COUNTERMODE_UP;
  291. htim3.Init.Period = 65535;
  292. htim3.Init.ClockDivision = TIM_CLOCKDIVISION_DIV1;
  293. htim3.Init.AutoReloadPreload = TIM_AUTORELOAD_PRELOAD_DISABLE;
  294. if (HAL_TIM_PWM_Init(&htim3) != HAL_OK)
  295. {
  296. Error_Handler();
  297. }
  298. sMasterConfig.MasterOutputTrigger = TIM_TRGO_RESET;
  299. sMasterConfig.MasterSlaveMode = TIM_MASTERSLAVEMODE_DISABLE;
  300. if (HAL_TIMEx_MasterConfigSynchronization(&htim3, &sMasterConfig) != HAL_OK)
  301. {
  302. Error_Handler();
  303. }
  304. sConfigOC.OCMode = TIM_OCMODE_PWM1;
  305. sConfigOC.Pulse = 0;
  306. sConfigOC.OCPolarity = TIM_OCPOLARITY_HIGH;
  307. sConfigOC.OCFastMode = TIM_OCFAST_DISABLE;
  308. if (HAL_TIM_PWM_ConfigChannel(&htim3, &sConfigOC, TIM_CHANNEL_1) != HAL_OK)
  309. {
  310. Error_Handler();
  311. }
  312. if (HAL_TIM_PWM_ConfigChannel(&htim3, &sConfigOC, TIM_CHANNEL_2) != HAL_OK)
  313. {
  314. Error_Handler();
  315. }
  316. /* USER CODE BEGIN TIM3_Init 2 */
  317. /* USER CODE END TIM3_Init 2 */
  318. HAL_TIM_MspPostInit(&htim3);
  319. }
  320. /**
  321. * @brief TIM11 Initialization Function
  322. * @param None
  323. * @retval None
  324. */
  325. static void MX_TIM11_Init(void)
  326. {
  327. /* USER CODE BEGIN TIM11_Init 0 */
  328. /* USER CODE END TIM11_Init 0 */
  329. /* USER CODE BEGIN TIM11_Init 1 */
  330. /* USER CODE END TIM11_Init 1 */
  331. htim11.Instance = TIM11;
  332. htim11.Init.Prescaler = 0;
  333. htim11.Init.CounterMode = TIM_COUNTERMODE_UP;
  334. htim11.Init.Period = 65535;
  335. htim11.Init.ClockDivision = TIM_CLOCKDIVISION_DIV1;
  336. htim11.Init.AutoReloadPreload = TIM_AUTORELOAD_PRELOAD_DISABLE;
  337. if (HAL_TIM_Base_Init(&htim11) != HAL_OK)
  338. {
  339. Error_Handler();
  340. }
  341. /* USER CODE BEGIN TIM11_Init 2 */
  342. /* USER CODE END TIM11_Init 2 */
  343. }
  344. /**
  345. * @brief USART2 Initialization Function
  346. * @param None
  347. * @retval None
  348. */
  349. static void MX_USART2_UART_Init(void)
  350. {
  351. /* USER CODE BEGIN USART2_Init 0 */
  352. /* USER CODE END USART2_Init 0 */
  353. /* USER CODE BEGIN USART2_Init 1 */
  354. /* USER CODE END USART2_Init 1 */
  355. huart2.Instance = USART2;
  356. huart2.Init.BaudRate = 115200;
  357. huart2.Init.WordLength = UART_WORDLENGTH_8B;
  358. huart2.Init.StopBits = UART_STOPBITS_1;
  359. huart2.Init.Parity = UART_PARITY_NONE;
  360. huart2.Init.Mode = UART_MODE_TX_RX;
  361. huart2.Init.HwFlowCtl = UART_HWCONTROL_NONE;
  362. huart2.Init.OverSampling = UART_OVERSAMPLING_16;
  363. if (HAL_UART_Init(&huart2) != HAL_OK)
  364. {
  365. Error_Handler();
  366. }
  367. /* USER CODE BEGIN USART2_Init 2 */
  368. /* USER CODE END USART2_Init 2 */
  369. }
  370. /**
  371. * @brief GPIO Initialization Function
  372. * @param None
  373. * @retval None
  374. */
  375. static void MX_GPIO_Init(void)
  376. {
  377. /* GPIO Ports Clock Enable */
  378. __HAL_RCC_GPIOC_CLK_ENABLE();
  379. __HAL_RCC_GPIOH_CLK_ENABLE();
  380. __HAL_RCC_GPIOA_CLK_ENABLE();
  381. __HAL_RCC_GPIOB_CLK_ENABLE();
  382. }
  383. /* USER CODE BEGIN 4 */
  384. /* USER CODE END 4 */
  385. /**
  386. * @brief This function is executed in case of error occurrence.
  387. * @retval None
  388. */
  389. void Error_Handler(void)
  390. {
  391. /* USER CODE BEGIN Error_Handler_Debug */
  392. /* User can add his own implementation to report the HAL error return state */
  393. /* USER CODE END Error_Handler_Debug */
  394. }
  395. #ifdef USE_FULL_ASSERT
  396. /**
  397. * @brief Reports the name of the source file and the source line number
  398. * where the assert_param error has occurred.
  399. * @param file: pointer to the source file name
  400. * @param line: assert_param error line source number
  401. * @retval None
  402. */
  403. void assert_failed(uint8_t *file, uint32_t line)
  404. {
  405. /* USER CODE BEGIN 6 */
  406. /* User can add his own implementation to report the file name and line number,
  407. tex: printf("Wrong parameters value: file %s on line %d\r\n", file, line) */
  408. /* USER CODE END 6 */
  409. }
  410. #endif /* USE_FULL_ASSERT */