main.c 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290
  1. /* USER CODE BEGIN Header */
  2. /**
  3. ******************************************************************************
  4. * @file : main.c
  5. * @brief : Main program body
  6. ******************************************************************************
  7. * @attention
  8. *
  9. * <h2><center>&copy; Copyright (c) 2020 STMicroelectronics.
  10. * All rights reserved.</center></h2>
  11. *
  12. * This software component is licensed by ST under BSD 3-Clause license,
  13. * the "License"; You may not use this file except in compliance with the
  14. * License. You may obtain a copy of the License at:
  15. * opensource.org/licenses/BSD-3-Clause
  16. *
  17. ******************************************************************************
  18. */
  19. /* USER CODE END Header */
  20. /* Includes ------------------------------------------------------------------*/
  21. #include "main.h"
  22. /* Private includes ----------------------------------------------------------*/
  23. /* USER CODE BEGIN Includes */
  24. /* USER CODE END Includes */
  25. /* Private typedef -----------------------------------------------------------*/
  26. /* USER CODE BEGIN PTD */
  27. /* USER CODE END PTD */
  28. /* Private define ------------------------------------------------------------*/
  29. /* USER CODE BEGIN PD */
  30. #define HSEM_ID_0 (0U) /* HW semaphore 0*/
  31. /* USER CODE END PD */
  32. /* Private macro -------------------------------------------------------------*/
  33. /* USER CODE BEGIN PM */
  34. /* USER CODE END PM */
  35. /* Private variables ---------------------------------------------------------*/
  36. UART_HandleTypeDef huart1;
  37. /* USER CODE BEGIN PV */
  38. /* USER CODE END PV */
  39. /* Private function prototypes -----------------------------------------------*/
  40. void SystemClock_Config(void);
  41. static void MX_GPIO_Init(void);
  42. static void MX_USART1_UART_Init(void);
  43. /* USER CODE BEGIN PFP */
  44. /* USER CODE END PFP */
  45. /* Private user code ---------------------------------------------------------*/
  46. /* USER CODE BEGIN 0 */
  47. /* USER CODE END 0 */
  48. /**
  49. * @brief The application entry point.
  50. * @retval int
  51. */
  52. int main(void)
  53. {
  54. /* USER CODE BEGIN 1 */
  55. /* USER CODE END 1 */
  56. /* USER CODE BEGIN Boot_Mode_Sequence_0 */
  57. int32_t timeout;
  58. /* USER CODE END Boot_Mode_Sequence_0 */
  59. /* USER CODE BEGIN Boot_Mode_Sequence_1 */
  60. /* Wait until CPU2 boots and enters in stop mode or timeout*/
  61. timeout = 0xFFFF;
  62. while((__HAL_RCC_GET_FLAG(RCC_FLAG_D2CKRDY) != RESET) && (timeout-- > 0));
  63. if ( timeout < 0 )
  64. {
  65. Error_Handler();
  66. }
  67. /* USER CODE END Boot_Mode_Sequence_1 */
  68. /* MCU Configuration--------------------------------------------------------*/
  69. /* Reset of all peripherals, Initializes the Flash interface and the Systick. */
  70. HAL_Init();
  71. /* USER CODE BEGIN Init */
  72. /* USER CODE END Init */
  73. /* Configure the system clock */
  74. SystemClock_Config();
  75. /* USER CODE BEGIN Boot_Mode_Sequence_2 */
  76. /* When system initialization is finished, Cortex-M7 will release Cortex-M4 by means of
  77. HSEM notification */
  78. /*HW semaphore Clock enable*/
  79. __HAL_RCC_HSEM_CLK_ENABLE();
  80. /*Take HSEM */
  81. HAL_HSEM_FastTake(HSEM_ID_0);
  82. /*Release HSEM in order to notify the CPU2(CM4)*/
  83. HAL_HSEM_Release(HSEM_ID_0,0);
  84. /* wait until CPU2 wakes up from stop mode */
  85. timeout = 0xFFFF;
  86. while((__HAL_RCC_GET_FLAG(RCC_FLAG_D2CKRDY) == RESET) && (timeout-- > 0));
  87. if ( timeout < 0 )
  88. {
  89. Error_Handler();
  90. }
  91. /* USER CODE END Boot_Mode_Sequence_2 */
  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. /* USER CODE BEGIN 2 */
  98. /* USER CODE END 2 */
  99. /* Infinite loop */
  100. /* USER CODE BEGIN WHILE */
  101. while (1)
  102. {
  103. /* USER CODE END WHILE */
  104. /* USER CODE BEGIN 3 */
  105. }
  106. /* USER CODE END 3 */
  107. }
  108. /**
  109. * @brief System Clock Configuration
  110. * @retval None
  111. */
  112. void SystemClock_Config(void)
  113. {
  114. RCC_OscInitTypeDef RCC_OscInitStruct = {0};
  115. RCC_ClkInitTypeDef RCC_ClkInitStruct = {0};
  116. RCC_PeriphCLKInitTypeDef PeriphClkInitStruct = {0};
  117. /** Supply configuration update enable
  118. */
  119. HAL_PWREx_ConfigSupply(PWR_DIRECT_SMPS_SUPPLY);
  120. /** Configure the main internal regulator output voltage
  121. */
  122. __HAL_PWR_VOLTAGESCALING_CONFIG(PWR_REGULATOR_VOLTAGE_SCALE1);
  123. while(!__HAL_PWR_GET_FLAG(PWR_FLAG_VOSRDY)) {}
  124. /** Initializes the CPU, AHB and APB busses clocks
  125. */
  126. RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_HSI;
  127. RCC_OscInitStruct.HSIState = RCC_HSI_DIV1;
  128. RCC_OscInitStruct.HSICalibrationValue = RCC_HSICALIBRATION_DEFAULT;
  129. RCC_OscInitStruct.PLL.PLLState = RCC_PLL_NONE;
  130. if (HAL_RCC_OscConfig(&RCC_OscInitStruct) != HAL_OK)
  131. {
  132. Error_Handler();
  133. }
  134. /** Initializes the CPU, AHB and APB busses clocks
  135. */
  136. RCC_ClkInitStruct.ClockType = RCC_CLOCKTYPE_HCLK|RCC_CLOCKTYPE_SYSCLK
  137. |RCC_CLOCKTYPE_PCLK1|RCC_CLOCKTYPE_PCLK2
  138. |RCC_CLOCKTYPE_D3PCLK1|RCC_CLOCKTYPE_D1PCLK1;
  139. RCC_ClkInitStruct.SYSCLKSource = RCC_SYSCLKSOURCE_HSI;
  140. RCC_ClkInitStruct.SYSCLKDivider = RCC_SYSCLK_DIV1;
  141. RCC_ClkInitStruct.AHBCLKDivider = RCC_HCLK_DIV1;
  142. RCC_ClkInitStruct.APB3CLKDivider = RCC_APB3_DIV1;
  143. RCC_ClkInitStruct.APB1CLKDivider = RCC_APB1_DIV1;
  144. RCC_ClkInitStruct.APB2CLKDivider = RCC_APB2_DIV1;
  145. RCC_ClkInitStruct.APB4CLKDivider = RCC_APB4_DIV1;
  146. if (HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_0) != HAL_OK)
  147. {
  148. Error_Handler();
  149. }
  150. PeriphClkInitStruct.PeriphClockSelection = RCC_PERIPHCLK_USART1;
  151. PeriphClkInitStruct.Usart16ClockSelection = RCC_USART16CLKSOURCE_D2PCLK2;
  152. if (HAL_RCCEx_PeriphCLKConfig(&PeriphClkInitStruct) != HAL_OK)
  153. {
  154. Error_Handler();
  155. }
  156. }
  157. /**
  158. * @brief USART1 Initialization Function
  159. * @param None
  160. * @retval None
  161. */
  162. static void MX_USART1_UART_Init(void)
  163. {
  164. /* USER CODE BEGIN USART1_Init 0 */
  165. /* USER CODE END USART1_Init 0 */
  166. /* USER CODE BEGIN USART1_Init 1 */
  167. /* USER CODE END USART1_Init 1 */
  168. huart1.Instance = USART1;
  169. huart1.Init.BaudRate = 115200;
  170. huart1.Init.WordLength = UART_WORDLENGTH_8B;
  171. huart1.Init.StopBits = UART_STOPBITS_1;
  172. huart1.Init.Parity = UART_PARITY_NONE;
  173. huart1.Init.Mode = UART_MODE_TX_RX;
  174. huart1.Init.HwFlowCtl = UART_HWCONTROL_NONE;
  175. huart1.Init.OverSampling = UART_OVERSAMPLING_16;
  176. huart1.Init.OneBitSampling = UART_ONE_BIT_SAMPLE_DISABLE;
  177. huart1.Init.ClockPrescaler = UART_PRESCALER_DIV1;
  178. huart1.AdvancedInit.AdvFeatureInit = UART_ADVFEATURE_NO_INIT;
  179. if (HAL_UART_Init(&huart1) != HAL_OK)
  180. {
  181. Error_Handler();
  182. }
  183. if (HAL_UARTEx_SetTxFifoThreshold(&huart1, UART_TXFIFO_THRESHOLD_1_8) != HAL_OK)
  184. {
  185. Error_Handler();
  186. }
  187. if (HAL_UARTEx_SetRxFifoThreshold(&huart1, UART_RXFIFO_THRESHOLD_1_8) != HAL_OK)
  188. {
  189. Error_Handler();
  190. }
  191. if (HAL_UARTEx_DisableFifoMode(&huart1) != HAL_OK)
  192. {
  193. Error_Handler();
  194. }
  195. /* USER CODE BEGIN USART1_Init 2 */
  196. /* USER CODE END USART1_Init 2 */
  197. }
  198. /**
  199. * @brief GPIO Initialization Function
  200. * @param None
  201. * @retval None
  202. */
  203. static void MX_GPIO_Init(void)
  204. {
  205. /* GPIO Ports Clock Enable */
  206. __HAL_RCC_GPIOA_CLK_ENABLE();
  207. __HAL_RCC_GPIOH_CLK_ENABLE();
  208. }
  209. /* USER CODE BEGIN 4 */
  210. /* USER CODE END 4 */
  211. /**
  212. * @brief This function is executed in case of error occurrence.
  213. * @retval None
  214. */
  215. void Error_Handler(void)
  216. {
  217. /* USER CODE BEGIN Error_Handler_Debug */
  218. /* User can add his own implementation to report the HAL error return state */
  219. /* USER CODE END Error_Handler_Debug */
  220. }
  221. #ifdef USE_FULL_ASSERT
  222. /**
  223. * @brief Reports the name of the source file and the source line number
  224. * where the assert_param error has occurred.
  225. * @param file: pointer to the source file name
  226. * @param line: assert_param error line source number
  227. * @retval None
  228. */
  229. void assert_failed(uint8_t *file, uint32_t line)
  230. {
  231. /* USER CODE BEGIN 6 */
  232. /* User can add his own implementation to report the file name and line number,
  233. tex: printf("Wrong parameters value: file %s on line %d\r\n", file, line) */
  234. /* USER CODE END 6 */
  235. }
  236. #endif /* USE_FULL_ASSERT */
  237. /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/