main.c 12 KB

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