main.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357
  1. /* USER CODE BEGIN Header */
  2. /**
  3. ******************************************************************************
  4. * @file : main.c
  5. * @brief : Main program body
  6. ******************************************************************************
  7. * @attention
  8. *
  9. * Copyright (c) 2026 STMicroelectronics.
  10. * All rights reserved.
  11. *
  12. * This software is licensed under terms that can be found in the LICENSE file
  13. * in the root directory of this software component.
  14. * If no LICENSE file comes with this software, it is provided AS-IS.
  15. *
  16. ******************************************************************************
  17. */
  18. #define ONECHIP_GLOBAL_DEF_FLAG
  19. /* USER CODE END Header */
  20. /* Includes ------------------------------------------------------------------*/
  21. #include "main.h"
  22. #include "adc.h"
  23. #include "dma.h"
  24. #include "i2c.h"
  25. #include "tim.h"
  26. #include "usart.h"
  27. #include "gpio.h"
  28. /* Private includes ----------------------------------------------------------*/
  29. /* USER CODE BEGIN Includes */
  30. #include "onechip_include_header.h"
  31. /* USER CODE END Includes */
  32. /* Private typedef -----------------------------------------------------------*/
  33. /* USER CODE BEGIN PTD */
  34. #define SAMPLE_TIME 10000
  35. /* USER CODE END PTD */
  36. /* Private define ------------------------------------------------------------*/
  37. /* USER CODE BEGIN PD */
  38. /* USER CODE END PD */
  39. /* Private macro -------------------------------------------------------------*/
  40. /* USER CODE BEGIN PM */
  41. /* USER CODE END PM */
  42. /* Private variables ---------------------------------------------------------*/
  43. /* USER CODE BEGIN PV */
  44. /* USER CODE END PV */
  45. /* Private function prototypes -----------------------------------------------*/
  46. void SystemClock_Config(void);
  47. /* USER CODE BEGIN PFP */
  48. /* USER CODE END PFP */
  49. /* Private user code ---------------------------------------------------------*/
  50. /* USER CODE BEGIN 0 */
  51. /* 定时器2溢出中断回调函数 */
  52. void HAL_TIM_PeriodElapsedCallback(TIM_HandleTypeDef *htim)
  53. {
  54. /* 检查触发中断的定时器是否为 TIM2 */
  55. if (htim->Instance == TIM2) {
  56. sysinf.ms_conter++;
  57. if(sysinf.ms_conter%1000 == 5){
  58. sysinf.resendFlag = 1;
  59. }
  60. if(sysinf.ms_conter%10 == 0){
  61. sysinf.ms10_flag = 1;
  62. sysinf.conter10ms++;
  63. if(sysinf.ms_conter%1000 == 0){
  64. sysinf.s1_flag = 1;
  65. if(sysinf.ms_conter%SAMPLE_TIME == 0){
  66. sysinf.samp_flag = 1;
  67. }
  68. if(sysinf.ms_conter%10000 == 0){
  69. sysinf.s10_flag = 1;
  70. }
  71. }
  72. Motor_mix_work();
  73. }
  74. if(sysinf.ms_conter%100 == 1){
  75. float temp = getADC_Chanel_value(0);
  76. sysinf.heaterMaster.pid.realtemp = temp + sysinf.heaterMaster.offset;
  77. heater_work(&sysinf.heaterMaster);
  78. }
  79. if(sysinf.ms_conter%100 == 2){
  80. lysis_work();
  81. }
  82. if(sysinf.ms_conter%100 == 1){
  83. sysinf.deal_espFlag=1;
  84. }
  85. if(sysinf.ms_conter%50 == 0){
  86. light_ctrl();
  87. if(sysinf.ms_conter%(OFFLINE_TIME/2) == 0){
  88. sysinf.upheart_flag = 1;
  89. }
  90. }
  91. }
  92. }
  93. /* USER CODE END 0 */
  94. /**
  95. * @brief The application entry point.
  96. * @retval int
  97. */
  98. int main(void)
  99. {
  100. /* USER CODE BEGIN 1 */
  101. // while(1){
  102. // for(unsigned int i=0;i<10000;i++){
  103. // i++;
  104. // i--;
  105. // }
  106. // HAL_GPIO_TogglePin(GPIOB, GPIO_PIN_14);
  107. // }
  108. SCB->VTOR = 0x8010000;
  109. __enable_irq();
  110. /* USER CODE END 1 */
  111. /* MCU Configuration--------------------------------------------------------*/
  112. /* Reset of all peripherals, Initializes the Flash interface and the Systick. */
  113. HAL_Init();
  114. /* USER CODE BEGIN Init */
  115. /* USER CODE END Init */
  116. /* Configure the system clock */
  117. SystemClock_Config();
  118. /* USER CODE BEGIN SysInit */
  119. /* USER CODE END SysInit */
  120. /* Initialize all configured peripherals */
  121. MX_GPIO_Init();
  122. MX_DMA_Init();
  123. MX_ADC1_Init();
  124. MX_UART4_Init();
  125. MX_USART1_UART_Init();
  126. MX_USART2_UART_Init();
  127. MX_I2C1_Init();
  128. MX_I2C2_Init();
  129. MX_TIM1_Init();
  130. MX_TIM2_Init();
  131. MX_TIM3_Init();
  132. MX_TIM4_Init();
  133. /* USER CODE BEGIN 2 */
  134. /* 启动定时器2的中断模式 */
  135. dev_load_allinf();
  136. light_set(MODE_BLINK, 250, COLOR_YELLOW, 0);
  137. light_set(MODE_BLINK, 250, COLOR_YELLOW, 1);
  138. light_refresh(COLOR_YELLOW, COLOR_YELLOW);
  139. con_sysinf_debugUart((void *)&huart1);
  140. Van_Debug_UART_Init((UART_HandleTypeDef *)sysinf.debugUart);
  141. ADC_Inner_start();
  142. load_com_pwm_Cfg(&sysinf.devdesc);
  143. sysinf.scannerUart = &huart4;
  144. svanner_receive_Rec_Start(sysinf.scannerUart);
  145. sysinf.ESPUart = &huart2;
  146. esp_receive_Rec_Start(sysinf.ESPUart);
  147. SensorInitial();
  148. /* USER CODE END 2 */
  149. /* Infinite loop */
  150. /* USER CODE BEGIN WHILE */
  151. Van_Device_Printf(VAN_LOG_ALLPRINT, "\n-----------------------------\n\r");
  152. Van_Device_Printf(VAN_LOG_ALLPRINT,"* build %s %s *\n",__DATE__,__TIME__);
  153. Van_Device_Printf(VAN_LOG_ALLPRINT, "ATCG ControlBoard\n\r");
  154. #ifdef ONE_CHIP_GIT_SHA
  155. Van_Device_Printf(VAN_LOG_ALLPRINT, "git sha: %s\n\r", ONE_CHIP_GIT_SHA);
  156. #endif
  157. Van_Device_Printf(VAN_LOG_ALLPRINT, "desversion 0x%x \n\r",sysinf.devdesc.desversion);
  158. Van_Device_Printf(VAN_LOG_ALLPRINT, "dev type:%d ver:%d\n\r",sysinf.devdesc.deveice_type,sysinf.devdesc.deveice_ver);
  159. Van_Device_Printf(VAN_LOG_ALLPRINT, "structure type:%d structure ver:%d\n\r",sysinf.devdesc.structure_type,sysinf.devdesc.structure_ver);
  160. Van_Device_Printf(VAN_LOG_ALLPRINT, "hard type:%d hard ver :%d \n\r",sysinf.devdesc.hardware_type,sysinf.devdesc.hardware_ver);
  161. Van_Device_Printf(VAN_LOG_ALLPRINT, "Soft Version: V%d.%d.%d.%d\n\r",SOFT_VERSION_1, SOFT_VERSION_2, SOFT_VERSION_3, SOFT_VERSION_4);
  162. if(sysinf.resetByIwdg){
  163. Van_Device_Printf(VAN_LOG_ALLPRINT, "**************************************************************************\n\r");
  164. Van_Device_Printf(VAN_LOG_ALLPRINT, "* *\n\r");
  165. Van_Device_Printf(VAN_LOG_ALLPRINT, "* iwdg reset *\n\r");
  166. Van_Device_Printf(VAN_LOG_ALLPRINT, "* *\n\r");
  167. Van_Device_Printf(VAN_LOG_ALLPRINT, "**************************************************************************\n\r");
  168. }
  169. cfg_param_init();
  170. Motor_Init();
  171. updatParm_lysis();
  172. updatParm_heater_master();
  173. check_heater_master_needPreheat();
  174. if (HAL_TIM_Base_Start_IT(&htim2) != HAL_OK) {
  175. /* 启动失败,可在此处添加错误处理 */
  176. Error_Handler();
  177. }
  178. // if( SensorSelfCheck()){
  179. // Van_Device_Printf(VAN_LOG_ALLPRINT, "SensorSelfCheck failed\n\r");
  180. // }else{
  181. // Van_Device_Printf(VAN_LOG_ALLPRINT, "SensorSelfCheck pass\n\r");
  182. // }
  183. sysinf.psmMaster.state = SW_Read_To_Board_Selftest;
  184. while (1)
  185. {
  186. /* USER CODE END WHILE */
  187. /* USER CODE BEGIN 3 */
  188. svanner_receive_analysis();
  189. Van_Debug_UART1_test();
  190. if(((cmd_rx_last + CMD_BUF_SIZE - cmd_rx_header) % CMD_BUF_SIZE) >= 8){
  191. uint32_t uart0_recOK = DecodeNew(&uart0_massage,uart0_data,g_cmd_rx_buf, &cmd_rx_header,cmd_rx_last,CMD_BUF_SIZE);
  192. if(uart0_recOK){
  193. uart0_recOK = 0;
  194. sysinf.pc_channel_recv = PC_CONNECT_TYPE_UART;
  195. sysinf.pc_link_timeout = 0;
  196. uart0_massage.data = (char *)uart0_data;
  197. deal_mUnion_cmd(&uart0_massage);
  198. query_pc_cmd(&uart0_massage);
  199. }
  200. }
  201. if(sysinf.deal_espFlag){
  202. sysinf.deal_espFlag =0;
  203. WIFI_process();
  204. }
  205. if( sysinf.s1_flag == 1){
  206. sysinf.s1_flag = 0;
  207. HAL_GPIO_TogglePin((GPIO_TypeDef *)sysinf.run_led_port, sysinf.run_led_pin); /*green*/
  208. update_work();
  209. resetSampleData(&sysinf.psmMaster);
  210. }
  211. if(sysinf.resendFlag == 1 ){
  212. sysinf.resendFlag = 0;
  213. reSend_QR();
  214. resend_error_update();
  215. re_send_state();
  216. }
  217. if( sysinf.ms10_flag == 1){
  218. sysinf.ms10_flag = 0;
  219. if(sysinf.QRBeepTimeer){
  220. sysinf.QRBeepTimeer --;
  221. if(sysinf.QRBeepTimeer == 0){
  222. HAL_GPIO_WritePin((GPIO_TypeDef *)sysinf.beep_port, sysinf.beep_pin,GPIO_PIN_RESET);
  223. }
  224. }
  225. }
  226. if( sysinf.upheart_flag == 1){
  227. sysinf.upheart_flag = 0;
  228. if((sysinf.pc_channel_recv == PC_CONNECT_TYPE_NULL)||(sysinf.pc_channel_recv == PC_CONNECT_TYPE_WIFI && sysinf.pc_link_timeout > (OFFLINE_TIME/3)) ){
  229. send_Devinf_pc();
  230. }
  231. }
  232. if( sysinf.s10_flag == 1){
  233. sysinf.s10_flag = 0;
  234. if(sysinf.pc_link_timeout > OFFLINE_TIME){
  235. send_Devinf_pc();
  236. }
  237. }
  238. usr_state_machine(&sysinf.psmMaster,&sysinf.heaterMaster);
  239. }
  240. /* USER CODE END 3 */
  241. }
  242. /**
  243. * @brief System Clock Configuration
  244. * @retval None
  245. */
  246. void SystemClock_Config(void)
  247. {
  248. RCC_OscInitTypeDef RCC_OscInitStruct = {0};
  249. RCC_ClkInitTypeDef RCC_ClkInitStruct = {0};
  250. RCC_PeriphCLKInitTypeDef PeriphClkInit = {0};
  251. /** Initializes the RCC Oscillators according to the specified parameters
  252. * in the RCC_OscInitTypeDef structure.
  253. */
  254. RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_HSE;
  255. RCC_OscInitStruct.HSEState = RCC_HSE_ON;
  256. RCC_OscInitStruct.HSEPredivValue = RCC_HSE_PREDIV_DIV1;
  257. RCC_OscInitStruct.HSIState = RCC_HSI_ON;
  258. RCC_OscInitStruct.PLL.PLLState = RCC_PLL_ON;
  259. RCC_OscInitStruct.PLL.PLLSource = RCC_PLLSOURCE_HSE;
  260. RCC_OscInitStruct.PLL.PLLMUL = RCC_PLL_MUL2;
  261. if (HAL_RCC_OscConfig(&RCC_OscInitStruct) != HAL_OK)
  262. {
  263. Error_Handler();
  264. }
  265. /** Initializes the CPU, AHB and APB buses clocks
  266. */
  267. RCC_ClkInitStruct.ClockType = RCC_CLOCKTYPE_HCLK|RCC_CLOCKTYPE_SYSCLK
  268. |RCC_CLOCKTYPE_PCLK1|RCC_CLOCKTYPE_PCLK2;
  269. RCC_ClkInitStruct.SYSCLKSource = RCC_SYSCLKSOURCE_PLLCLK;
  270. RCC_ClkInitStruct.AHBCLKDivider = RCC_SYSCLK_DIV1;
  271. RCC_ClkInitStruct.APB1CLKDivider = RCC_HCLK_DIV2;
  272. RCC_ClkInitStruct.APB2CLKDivider = RCC_HCLK_DIV1;
  273. if (HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_0) != HAL_OK)
  274. {
  275. Error_Handler();
  276. }
  277. PeriphClkInit.PeriphClockSelection = RCC_PERIPHCLK_ADC;
  278. PeriphClkInit.AdcClockSelection = RCC_ADCPCLK2_DIV2;
  279. if (HAL_RCCEx_PeriphCLKConfig(&PeriphClkInit) != HAL_OK)
  280. {
  281. Error_Handler();
  282. }
  283. }
  284. /* USER CODE BEGIN 4 */
  285. /* USER CODE END 4 */
  286. /**
  287. * @brief This function is executed in case of error occurrence.
  288. * @retval None
  289. */
  290. void Error_Handler(void)
  291. {
  292. /* USER CODE BEGIN Error_Handler_Debug */
  293. /* User can add his own implementation to report the HAL error return state */
  294. __disable_irq();
  295. while (1)
  296. {
  297. }
  298. /* USER CODE END Error_Handler_Debug */
  299. }
  300. #ifdef USE_FULL_ASSERT
  301. /**
  302. * @brief Reports the name of the source file and the source line number
  303. * where the assert_param error has occurred.
  304. * @param file: pointer to the source file name
  305. * @param line: assert_param error line source number
  306. * @retval None
  307. */
  308. void assert_failed(uint8_t *file, uint32_t line)
  309. {
  310. /* USER CODE BEGIN 6 */
  311. /* User can add his own implementation to report the file name and line number,
  312. ex: printf("Wrong parameters value: file %s on line %d\r\n", file, line) */
  313. /* USER CODE END 6 */
  314. }
  315. #endif /* USE_FULL_ASSERT */