main.c 12 KB

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