main.c 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303
  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 International N.V.
  14. * All rights reserved.
  15. *
  16. * Redistribution and use in source and binary forms, with or without
  17. * modification, are permitted, provided that the following conditions are met:
  18. *
  19. * 1. Redistribution of source code must retain the above copyright notice,
  20. * this list of conditions and the following disclaimer.
  21. * 2. Redistributions in binary form must reproduce the above copyright notice,
  22. * this list of conditions and the following disclaimer in the documentation
  23. * and/or other materials provided with the distribution.
  24. * 3. Neither the name of STMicroelectronics nor the names of other
  25. * contributors to this software may be used to endorse or promote products
  26. * derived from this software without specific written permission.
  27. * 4. This software, including modifications and/or derivative works of this
  28. * software, must execute solely and exclusively on microcontroller or
  29. * microprocessor devices manufactured by or for STMicroelectronics.
  30. * 5. Redistribution and use of this software other than as permitted under
  31. * this license is void and will automatically terminate your rights under
  32. * this license.
  33. *
  34. * THIS SOFTWARE IS PROVIDED BY STMICROELECTRONICS AND CONTRIBUTORS "AS IS"
  35. * AND ANY EXPRESS, IMPLIED OR STATUTORY WARRANTIES, INCLUDING, BUT NOT
  36. * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR A
  37. * PARTICULAR PURPOSE AND NON-INFRINGEMENT OF THIRD PARTY INTELLECTUAL PROPERTY
  38. * RIGHTS ARE DISCLAIMED TO THE FULLEST EXTENT PERMITTED BY LAW. IN NO EVENT
  39. * SHALL STMICROELECTRONICS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
  40. * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  41. * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA,
  42. * OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
  43. * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
  44. * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
  45. * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  46. *
  47. ******************************************************************************
  48. */
  49. /* USER CODE END Header */
  50. /* Includes ------------------------------------------------------------------*/
  51. #include "main.h"
  52. /* Private includes ----------------------------------------------------------*/
  53. /* USER CODE BEGIN Includes */
  54. /* USER CODE END Includes */
  55. /* Private typedef -----------------------------------------------------------*/
  56. /* USER CODE BEGIN PTD */
  57. /* USER CODE END PTD */
  58. /* Private define ------------------------------------------------------------*/
  59. /* USER CODE BEGIN PD */
  60. /* USER CODE END PD */
  61. /* Private macro -------------------------------------------------------------*/
  62. /* USER CODE BEGIN PM */
  63. /* USER CODE END PM */
  64. /* Private variables ---------------------------------------------------------*/
  65. SPI_HandleTypeDef hspi1;
  66. UART_HandleTypeDef huart1;
  67. /* USER CODE BEGIN PV */
  68. /* USER CODE END PV */
  69. /* Private function prototypes -----------------------------------------------*/
  70. void SystemClock_Config(void);
  71. static void MX_GPIO_Init(void);
  72. static void MX_SPI1_Init(void);
  73. static void MX_USART1_UART_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_SPI1_Init();
  99. MX_USART1_UART_Init();
  100. /* USER CODE BEGIN 2 */
  101. /* USER CODE END 2 */
  102. /* Infinite loop */
  103. /* USER CODE BEGIN WHILE */
  104. while (1)
  105. {
  106. /* USER CODE END WHILE */
  107. /* USER CODE BEGIN 3 */
  108. }
  109. /* USER CODE END 3 */
  110. }
  111. /**
  112. * @brief System Clock Configuration
  113. * @retval None
  114. */
  115. void SystemClock_Config(void)
  116. {
  117. RCC_OscInitTypeDef RCC_OscInitStruct = {0};
  118. RCC_ClkInitTypeDef RCC_ClkInitStruct = {0};
  119. /**Configure the main internal regulator output voltage
  120. */
  121. __HAL_RCC_PWR_CLK_ENABLE();
  122. __HAL_PWR_VOLTAGESCALING_CONFIG(PWR_REGULATOR_VOLTAGE_SCALE1);
  123. /**Initializes the CPU, AHB and APB busses clocks
  124. */
  125. RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_HSE;
  126. RCC_OscInitStruct.HSEState = RCC_HSE_ON;
  127. RCC_OscInitStruct.PLL.PLLState = RCC_PLL_ON;
  128. RCC_OscInitStruct.PLL.PLLSource = RCC_PLLSOURCE_HSE;
  129. RCC_OscInitStruct.PLL.PLLM = 8;
  130. RCC_OscInitStruct.PLL.PLLN = 336;
  131. RCC_OscInitStruct.PLL.PLLP = RCC_PLLP_DIV2;
  132. RCC_OscInitStruct.PLL.PLLQ = 7;
  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_DIV4;
  144. RCC_ClkInitStruct.APB2CLKDivider = RCC_HCLK_DIV2;
  145. if (HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_5) != HAL_OK)
  146. {
  147. Error_Handler();
  148. }
  149. }
  150. /**
  151. * @brief SPI1 Initialization Function
  152. * @param None
  153. * @retval None
  154. */
  155. static void MX_SPI1_Init(void)
  156. {
  157. /* USER CODE BEGIN SPI1_Init 0 */
  158. /* USER CODE END SPI1_Init 0 */
  159. /* USER CODE BEGIN SPI1_Init 1 */
  160. /* USER CODE END SPI1_Init 1 */
  161. /* SPI1 parameter configuration*/
  162. hspi1.Instance = SPI1;
  163. hspi1.Init.Mode = SPI_MODE_MASTER;
  164. hspi1.Init.Direction = SPI_DIRECTION_2LINES;
  165. hspi1.Init.DataSize = SPI_DATASIZE_8BIT;
  166. hspi1.Init.CLKPolarity = SPI_POLARITY_LOW;
  167. hspi1.Init.CLKPhase = SPI_PHASE_1EDGE;
  168. hspi1.Init.NSS = SPI_NSS_SOFT;
  169. hspi1.Init.BaudRatePrescaler = SPI_BAUDRATEPRESCALER_2;
  170. hspi1.Init.FirstBit = SPI_FIRSTBIT_MSB;
  171. hspi1.Init.TIMode = SPI_TIMODE_DISABLE;
  172. hspi1.Init.CRCCalculation = SPI_CRCCALCULATION_DISABLE;
  173. hspi1.Init.CRCPolynomial = 10;
  174. if (HAL_SPI_Init(&hspi1) != HAL_OK)
  175. {
  176. Error_Handler();
  177. }
  178. /* USER CODE BEGIN SPI1_Init 2 */
  179. /* USER CODE END SPI1_Init 2 */
  180. }
  181. /**
  182. * @brief USART1 Initialization Function
  183. * @param None
  184. * @retval None
  185. */
  186. static void MX_USART1_UART_Init(void)
  187. {
  188. /* USER CODE BEGIN USART1_Init 0 */
  189. /* USER CODE END USART1_Init 0 */
  190. /* USER CODE BEGIN USART1_Init 1 */
  191. /* USER CODE END USART1_Init 1 */
  192. huart1.Instance = USART1;
  193. huart1.Init.BaudRate = 115200;
  194. huart1.Init.WordLength = UART_WORDLENGTH_8B;
  195. huart1.Init.StopBits = UART_STOPBITS_1;
  196. huart1.Init.Parity = UART_PARITY_NONE;
  197. huart1.Init.Mode = UART_MODE_TX_RX;
  198. huart1.Init.HwFlowCtl = UART_HWCONTROL_NONE;
  199. huart1.Init.OverSampling = UART_OVERSAMPLING_16;
  200. if (HAL_UART_Init(&huart1) != HAL_OK)
  201. {
  202. Error_Handler();
  203. }
  204. /* USER CODE BEGIN USART1_Init 2 */
  205. /* USER CODE END USART1_Init 2 */
  206. }
  207. /**
  208. * @brief GPIO Initialization Function
  209. * @param None
  210. * @retval None
  211. */
  212. static void MX_GPIO_Init(void)
  213. {
  214. /* GPIO Ports Clock Enable */
  215. __HAL_RCC_GPIOH_CLK_ENABLE();
  216. __HAL_RCC_GPIOA_CLK_ENABLE();
  217. __HAL_RCC_GPIOB_CLK_ENABLE();
  218. }
  219. /* USER CODE BEGIN 4 */
  220. /* USER CODE END 4 */
  221. /**
  222. * @brief This function is executed in case of error occurrence.
  223. * @retval None
  224. */
  225. void Error_Handler(void)
  226. {
  227. /* USER CODE BEGIN Error_Handler_Debug */
  228. /* User can add his own implementation to report the HAL error return state */
  229. /* USER CODE END Error_Handler_Debug */
  230. }
  231. #ifdef USE_FULL_ASSERT
  232. /**
  233. * @brief Reports the name of the source file and the source line number
  234. * where the assert_param error has occurred.
  235. * @param file: pointer to the source file name
  236. * @param line: assert_param error line source number
  237. * @retval None
  238. */
  239. void assert_failed(uint8_t *file, uint32_t line)
  240. {
  241. /* USER CODE BEGIN 6 */
  242. /* User can add his own implementation to report the file name and line number,
  243. tex: printf("Wrong parameters value: file %s on line %d\r\n", file, line) */
  244. /* USER CODE END 6 */
  245. }
  246. #endif /* USE_FULL_ASSERT */
  247. /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/