main.c 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343
  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. #include <stdio.h>
  25. #include <uart.h>
  26. /* USER CODE END Includes */
  27. /* Private typedef -----------------------------------------------------------*/
  28. /* USER CODE BEGIN PTD */
  29. /* USER CODE END PTD */
  30. /* Private define ------------------------------------------------------------*/
  31. /* USER CODE BEGIN PD */
  32. /* USER CODE END PD */
  33. /* Private macro -------------------------------------------------------------*/
  34. /* USER CODE BEGIN PM */
  35. /* USER CODE END PM */
  36. /* Private variables ---------------------------------------------------------*/
  37. TIM_HandleTypeDef htim6;
  38. UART_HandleTypeDef huart1;
  39. DMA_HandleTypeDef hdma_usart1_rx;
  40. DMA_HandleTypeDef hdma_usart1_tx;
  41. /* USER CODE BEGIN PV */
  42. volatile uint32_t LedTimerCnt = 0;
  43. volatile uint32_t FirmwareTimerCnt = 0;
  44. volatile uint32_t UartTimerCnt = 0;
  45. /* USER CODE END PV */
  46. /* Private function prototypes -----------------------------------------------*/
  47. void SystemClock_Config(void);
  48. static void MX_GPIO_Init(void);
  49. static void MX_DMA_Init(void);
  50. static void MX_USART1_UART_Init(void);
  51. static void MX_TIM6_Init(void);
  52. static void MX_NVIC_Init(void);
  53. /* USER CODE BEGIN PFP */
  54. /* USER CODE END PFP */
  55. /* Private user code ---------------------------------------------------------*/
  56. /* USER CODE BEGIN 0 */
  57. void HAL_TIM_PeriodElapsedCallback(TIM_HandleTypeDef *htim)
  58. {
  59. if(htim->Instance == TIM6){
  60. UartTimerCnt++;
  61. LedTimerCnt++;
  62. FirmwareTimerCnt++;
  63. }
  64. }
  65. int _write (int file, uint8_t *ptr, uint16_t len)
  66. {
  67. HAL_UART_Transmit (&huart1, ptr, len, 10);
  68. return len;
  69. }
  70. extern UARTQUEUE TerminalQueue;
  71. /* USER CODE END 0 */
  72. /**
  73. * @brief The application entry point.
  74. * @retval int
  75. */
  76. int main(void)
  77. {
  78. /* USER CODE BEGIN 1 */
  79. uint8_t tempdata[] = {0x00,0x01,0x02,0x03,0x04,0x05,0x06,0x07,0x08,0x09,0x0A};
  80. /* USER CODE END 1 */
  81. /* MCU Configuration--------------------------------------------------------*/
  82. /* Reset of all peripherals, Initializes the Flash interface and the Systick. */
  83. HAL_Init();
  84. /* USER CODE BEGIN Init */
  85. /* USER CODE END Init */
  86. /* Configure the system clock */
  87. SystemClock_Config();
  88. /* USER CODE BEGIN SysInit */
  89. /* USER CODE END SysInit */
  90. /* Initialize all configured peripherals */
  91. MX_GPIO_Init();
  92. MX_DMA_Init();
  93. MX_USART1_UART_Init();
  94. MX_TIM6_Init();
  95. /* Initialize interrupts */
  96. MX_NVIC_Init();
  97. /* USER CODE BEGIN 2 */
  98. HAL_TIM_Base_Start_IT(&htim6);
  99. setbuf(stdout, NULL);
  100. /*printf("Uart Start \r\n");
  101. printf("Crc generate %x \r\n",CRC16_Generate(tempdata,11));*/
  102. Firmware_BootStart_Signal();
  103. InitUartQueue(&TerminalQueue);
  104. /* USER CODE END 2 */
  105. /* Infinite loop */
  106. /* USER CODE BEGIN WHILE */
  107. while (1)
  108. {
  109. // printf("Uart Start \r\n");
  110. if(LedTimerCnt > 500){HAL_GPIO_TogglePin(BOOT_LED_GPIO_Port,BOOT_LED_Pin);LedTimerCnt = 0;}
  111. while (TerminalQueue.data > 0 && UartTimerCnt > 30) GetDataFromUartQueue(&hTerminal);
  112. while(FirmwareTimerCnt > 3000) Jump_App();
  113. //HAL_Delay(500);
  114. /* USER CODE END WHILE */
  115. /* USER CODE BEGIN 3 */
  116. }
  117. /* USER CODE END 3 */
  118. }
  119. /**
  120. * @brief System Clock Configuration
  121. * @retval None
  122. */
  123. void SystemClock_Config(void)
  124. {
  125. RCC_OscInitTypeDef RCC_OscInitStruct = {0};
  126. RCC_ClkInitTypeDef RCC_ClkInitStruct = {0};
  127. /** Initializes the CPU, AHB and APB busses clocks
  128. */
  129. RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_HSI;
  130. RCC_OscInitStruct.HSIState = RCC_HSI_ON;
  131. RCC_OscInitStruct.HSICalibrationValue = RCC_HSICALIBRATION_DEFAULT;
  132. RCC_OscInitStruct.PLL.PLLState = RCC_PLL_ON;
  133. RCC_OscInitStruct.PLL.PLLSource = RCC_PLLSOURCE_HSI_DIV2;
  134. RCC_OscInitStruct.PLL.PLLMUL = RCC_PLL_MUL15;
  135. if (HAL_RCC_OscConfig(&RCC_OscInitStruct) != HAL_OK)
  136. {
  137. Error_Handler();
  138. }
  139. /** Initializes the CPU, AHB and APB busses clocks
  140. */
  141. RCC_ClkInitStruct.ClockType = RCC_CLOCKTYPE_HCLK|RCC_CLOCKTYPE_SYSCLK
  142. |RCC_CLOCKTYPE_PCLK1|RCC_CLOCKTYPE_PCLK2;
  143. RCC_ClkInitStruct.SYSCLKSource = RCC_SYSCLKSOURCE_PLLCLK;
  144. RCC_ClkInitStruct.AHBCLKDivider = RCC_SYSCLK_DIV1;
  145. RCC_ClkInitStruct.APB1CLKDivider = RCC_HCLK_DIV2;
  146. RCC_ClkInitStruct.APB2CLKDivider = RCC_HCLK_DIV1;
  147. if (HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_2) != HAL_OK)
  148. {
  149. Error_Handler();
  150. }
  151. }
  152. /**
  153. * @brief NVIC Configuration.
  154. * @retval None
  155. */
  156. static void MX_NVIC_Init(void)
  157. {
  158. /* DMA1_Channel4_IRQn interrupt configuration */
  159. HAL_NVIC_SetPriority(DMA1_Channel4_IRQn, 0, 0);
  160. HAL_NVIC_EnableIRQ(DMA1_Channel4_IRQn);
  161. /* USART1_IRQn interrupt configuration */
  162. HAL_NVIC_SetPriority(USART1_IRQn, 0, 0);
  163. HAL_NVIC_EnableIRQ(USART1_IRQn);
  164. /* DMA1_Channel5_IRQn interrupt configuration */
  165. HAL_NVIC_SetPriority(DMA1_Channel5_IRQn, 0, 0);
  166. HAL_NVIC_EnableIRQ(DMA1_Channel5_IRQn);
  167. }
  168. /**
  169. * @brief TIM6 Initialization Function
  170. * @param None
  171. * @retval None
  172. */
  173. static void MX_TIM6_Init(void)
  174. {
  175. /* USER CODE BEGIN TIM6_Init 0 */
  176. /* USER CODE END TIM6_Init 0 */
  177. TIM_MasterConfigTypeDef sMasterConfig = {0};
  178. /* USER CODE BEGIN TIM6_Init 1 */
  179. /* USER CODE END TIM6_Init 1 */
  180. htim6.Instance = TIM6;
  181. htim6.Init.Prescaler = 6000 - 1;
  182. htim6.Init.CounterMode = TIM_COUNTERMODE_UP;
  183. htim6.Init.Period = 10 - 1;
  184. htim6.Init.AutoReloadPreload = TIM_AUTORELOAD_PRELOAD_DISABLE;
  185. if (HAL_TIM_Base_Init(&htim6) != HAL_OK)
  186. {
  187. Error_Handler();
  188. }
  189. sMasterConfig.MasterOutputTrigger = TIM_TRGO_RESET;
  190. sMasterConfig.MasterSlaveMode = TIM_MASTERSLAVEMODE_DISABLE;
  191. if (HAL_TIMEx_MasterConfigSynchronization(&htim6, &sMasterConfig) != HAL_OK)
  192. {
  193. Error_Handler();
  194. }
  195. /* USER CODE BEGIN TIM6_Init 2 */
  196. /* USER CODE END TIM6_Init 2 */
  197. }
  198. /**
  199. * @brief USART1 Initialization Function
  200. * @param None
  201. * @retval None
  202. */
  203. static void MX_USART1_UART_Init(void)
  204. {
  205. /* USER CODE BEGIN USART1_Init 0 */
  206. /* USER CODE END USART1_Init 0 */
  207. /* USER CODE BEGIN USART1_Init 1 */
  208. /* USER CODE END USART1_Init 1 */
  209. huart1.Instance = USART1;
  210. huart1.Init.BaudRate = 921600;
  211. huart1.Init.WordLength = UART_WORDLENGTH_8B;
  212. huart1.Init.StopBits = UART_STOPBITS_1;
  213. huart1.Init.Parity = UART_PARITY_NONE;
  214. huart1.Init.Mode = UART_MODE_TX_RX;
  215. huart1.Init.HwFlowCtl = UART_HWCONTROL_NONE;
  216. huart1.Init.OverSampling = UART_OVERSAMPLING_16;
  217. if (HAL_UART_Init(&huart1) != HAL_OK)
  218. {
  219. Error_Handler();
  220. }
  221. /* USER CODE BEGIN USART1_Init 2 */
  222. /* USER CODE END USART1_Init 2 */
  223. }
  224. /**
  225. * Enable DMA controller clock
  226. */
  227. static void MX_DMA_Init(void)
  228. {
  229. /* DMA controller clock enable */
  230. __HAL_RCC_DMA1_CLK_ENABLE();
  231. }
  232. /**
  233. * @brief GPIO Initialization Function
  234. * @param None
  235. * @retval None
  236. */
  237. static void MX_GPIO_Init(void)
  238. {
  239. GPIO_InitTypeDef GPIO_InitStruct = {0};
  240. /* GPIO Ports Clock Enable */
  241. __HAL_RCC_GPIOC_CLK_ENABLE();
  242. __HAL_RCC_GPIOA_CLK_ENABLE();
  243. /*Configure GPIO pin Output Level */
  244. HAL_GPIO_WritePin(BOOT_LED_GPIO_Port, BOOT_LED_Pin, GPIO_PIN_RESET);
  245. /*Configure GPIO pin : BOOT_LED_Pin */
  246. GPIO_InitStruct.Pin = BOOT_LED_Pin;
  247. GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP;
  248. GPIO_InitStruct.Pull = GPIO_NOPULL;
  249. GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW;
  250. HAL_GPIO_Init(BOOT_LED_GPIO_Port, &GPIO_InitStruct);
  251. }
  252. /* USER CODE BEGIN 4 */
  253. /* USER CODE END 4 */
  254. /**
  255. * @brief This function is executed in case of error occurrence.
  256. * @retval None
  257. */
  258. void Error_Handler(void)
  259. {
  260. /* USER CODE BEGIN Error_Handler_Debug */
  261. /* User can add his own implementation to report the HAL error return state */
  262. /* USER CODE END Error_Handler_Debug */
  263. }
  264. #ifdef USE_FULL_ASSERT
  265. /**
  266. * @brief Reports the name of the source file and the source line number
  267. * where the assert_param error has occurred.
  268. * @param file: pointer to the source file name
  269. * @param line: assert_param error line source number
  270. * @retval None
  271. */
  272. void assert_failed(uint8_t *file, uint32_t line)
  273. {
  274. /* USER CODE BEGIN 6 */
  275. /* User can add his own implementation to report the file name and line number,
  276. tex: printf("Wrong parameters value: file %s on line %d\r\n", file, line) */
  277. /* USER CODE END 6 */
  278. }
  279. #endif /* USE_FULL_ASSERT */
  280. /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/