main.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351
  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. /* USER CODE END 1 */
  102. /* MCU Configuration--------------------------------------------------------*/
  103. /* Reset of all peripherals, Initializes the Flash interface and the Systick. */
  104. HAL_Init();
  105. /* USER CODE BEGIN Init */
  106. /* USER CODE END Init */
  107. /* Configure the system clock */
  108. SystemClock_Config();
  109. /* USER CODE BEGIN SysInit */
  110. /* USER CODE END SysInit */
  111. /* Initialize all configured peripherals */
  112. MX_GPIO_Init();
  113. MX_DMA_Init();
  114. MX_ADC1_Init();
  115. MX_UART4_Init();
  116. MX_USART1_UART_Init();
  117. MX_USART2_UART_Init();
  118. MX_I2C1_Init();
  119. MX_I2C2_Init();
  120. MX_TIM1_Init();
  121. MX_TIM2_Init();
  122. MX_TIM3_Init();
  123. MX_TIM4_Init();
  124. /* USER CODE BEGIN 2 */
  125. /* 启动定时器2的中断模式 */
  126. dev_load_allinf();
  127. light_set(MODE_BLINK, 250, COLOR_YELLOW, 0);
  128. light_set(MODE_BLINK, 250, COLOR_YELLOW, 1);
  129. light_refresh(COLOR_YELLOW, COLOR_YELLOW);
  130. con_sysinf_debugUart((void *)&huart1);
  131. Van_Debug_UART_Init((UART_HandleTypeDef *)sysinf.debugUart);
  132. ADC_Inner_start();
  133. load_com_pwm_Cfg(&sysinf.devdesc);
  134. sysinf.scannerUart = &huart4;
  135. svanner_receive_Rec_Start(sysinf.scannerUart);
  136. sysinf.ESPUart = &huart2;
  137. esp_receive_Rec_Start(sysinf.ESPUart);
  138. SensorInitial();
  139. /* USER CODE END 2 */
  140. /* Infinite loop */
  141. /* USER CODE BEGIN WHILE */
  142. Van_Device_Printf(VAN_LOG_ALLPRINT, "\n-----------------------------\n\r");
  143. Van_Device_Printf(VAN_LOG_ALLPRINT,"* build %s %s *\n",__DATE__,__TIME__);
  144. Van_Device_Printf(VAN_LOG_ALLPRINT, "ATCG ControlBoard\n\r");
  145. #ifdef ONE_CHIP_GIT_SHA
  146. Van_Device_Printf(VAN_LOG_ALLPRINT, "git sha: %s\n\r", ONE_CHIP_GIT_SHA);
  147. #endif
  148. Van_Device_Printf(VAN_LOG_ALLPRINT, "desversion 0x%x \n\r",sysinf.devdesc.desversion);
  149. Van_Device_Printf(VAN_LOG_ALLPRINT, "dev type:%d ver:%d\n\r",sysinf.devdesc.deveice_type,sysinf.devdesc.deveice_ver);
  150. Van_Device_Printf(VAN_LOG_ALLPRINT, "structure type:%d structure ver:%d\n\r",sysinf.devdesc.structure_type,sysinf.devdesc.structure_ver);
  151. Van_Device_Printf(VAN_LOG_ALLPRINT, "hard type:%d hard ver :%d \n\r",sysinf.devdesc.hardware_type,sysinf.devdesc.hardware_ver);
  152. 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);
  153. if(sysinf.resetByIwdg){
  154. Van_Device_Printf(VAN_LOG_ALLPRINT, "**************************************************************************\n\r");
  155. Van_Device_Printf(VAN_LOG_ALLPRINT, "* *\n\r");
  156. Van_Device_Printf(VAN_LOG_ALLPRINT, "* iwdg reset *\n\r");
  157. Van_Device_Printf(VAN_LOG_ALLPRINT, "* *\n\r");
  158. Van_Device_Printf(VAN_LOG_ALLPRINT, "**************************************************************************\n\r");
  159. }
  160. cfg_param_init();
  161. Motor_Init();
  162. updatParm_lysis();
  163. updatParm_heater_master();
  164. check_heater_master_needPreheat();
  165. if (HAL_TIM_Base_Start_IT(&htim2) != HAL_OK) {
  166. /* 启动失败,可在此处添加错误处理 */
  167. Error_Handler();
  168. }
  169. // if( SensorSelfCheck()){
  170. // Van_Device_Printf(VAN_LOG_ALLPRINT, "SensorSelfCheck failed\n\r");
  171. // }else{
  172. // Van_Device_Printf(VAN_LOG_ALLPRINT, "SensorSelfCheck pass\n\r");
  173. // }
  174. sysinf.psmMaster.state = SW_Read_To_Board_Selftest;
  175. while (1)
  176. {
  177. /* USER CODE END WHILE */
  178. /* USER CODE BEGIN 3 */
  179. svanner_receive_analysis();
  180. Van_Debug_UART1_test();
  181. if(((cmd_rx_last + CMD_BUF_SIZE - cmd_rx_header) % CMD_BUF_SIZE) >= 8){
  182. uint32_t uart0_recOK = DecodeNew(&uart0_massage,uart0_data,g_cmd_rx_buf, &cmd_rx_header,cmd_rx_last,CMD_BUF_SIZE);
  183. if(uart0_recOK){
  184. uart0_recOK = 0;
  185. sysinf.pc_channel_recv = PC_CONNECT_TYPE_UART;
  186. sysinf.pc_link_timeout = 0;
  187. uart0_massage.data = (char *)uart0_data;
  188. deal_mUnion_cmd(&uart0_massage);
  189. query_pc_cmd(&uart0_massage);
  190. }
  191. }
  192. if(sysinf.deal_espFlag){
  193. sysinf.deal_espFlag =0;
  194. WIFI_process();
  195. }
  196. if( sysinf.s1_flag == 1){
  197. sysinf.s1_flag = 0;
  198. HAL_GPIO_TogglePin((GPIO_TypeDef *)sysinf.run_led_port, sysinf.run_led_pin); /*green*/
  199. update_work();
  200. resetSampleData(&sysinf.psmMaster);
  201. }
  202. if(sysinf.resendFlag == 1 ){
  203. sysinf.resendFlag = 0;
  204. reSend_QR();
  205. resend_error_update();
  206. re_send_state();
  207. }
  208. if( sysinf.ms10_flag == 1){
  209. sysinf.ms10_flag = 0;
  210. if(sysinf.QRBeepTimeer){
  211. sysinf.QRBeepTimeer --;
  212. if(sysinf.QRBeepTimeer == 0){
  213. HAL_GPIO_WritePin((GPIO_TypeDef *)sysinf.beep_port, sysinf.beep_pin,GPIO_PIN_RESET);
  214. }
  215. }
  216. }
  217. if( sysinf.upheart_flag == 1){
  218. sysinf.upheart_flag = 0;
  219. if((sysinf.pc_channel_recv == PC_CONNECT_TYPE_NULL)||(sysinf.pc_channel_recv == PC_CONNECT_TYPE_WIFI && sysinf.pc_link_timeout > (OFFLINE_TIME/3)) ){
  220. send_Devinf_pc();
  221. }
  222. }
  223. if( sysinf.s10_flag == 1){
  224. sysinf.s10_flag = 0;
  225. if(sysinf.pc_link_timeout > OFFLINE_TIME){
  226. send_Devinf_pc();
  227. }
  228. }
  229. usr_state_machine(&sysinf.psmMaster,&sysinf.heaterMaster);
  230. }
  231. /* USER CODE END 3 */
  232. }
  233. /**
  234. * @brief System Clock Configuration
  235. * @retval None
  236. */
  237. void SystemClock_Config(void)
  238. {
  239. RCC_OscInitTypeDef RCC_OscInitStruct = {0};
  240. RCC_ClkInitTypeDef RCC_ClkInitStruct = {0};
  241. RCC_PeriphCLKInitTypeDef PeriphClkInit = {0};
  242. /** Initializes the RCC Oscillators according to the specified parameters
  243. * in the RCC_OscInitTypeDef structure.
  244. */
  245. RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_HSE;
  246. RCC_OscInitStruct.HSEState = RCC_HSE_ON;
  247. RCC_OscInitStruct.HSEPredivValue = RCC_HSE_PREDIV_DIV1;
  248. RCC_OscInitStruct.HSIState = RCC_HSI_ON;
  249. RCC_OscInitStruct.PLL.PLLState = RCC_PLL_ON;
  250. RCC_OscInitStruct.PLL.PLLSource = RCC_PLLSOURCE_HSE;
  251. RCC_OscInitStruct.PLL.PLLMUL = RCC_PLL_MUL2;
  252. if (HAL_RCC_OscConfig(&RCC_OscInitStruct) != HAL_OK)
  253. {
  254. Error_Handler();
  255. }
  256. /** Initializes the CPU, AHB and APB buses clocks
  257. */
  258. RCC_ClkInitStruct.ClockType = RCC_CLOCKTYPE_HCLK|RCC_CLOCKTYPE_SYSCLK
  259. |RCC_CLOCKTYPE_PCLK1|RCC_CLOCKTYPE_PCLK2;
  260. RCC_ClkInitStruct.SYSCLKSource = RCC_SYSCLKSOURCE_PLLCLK;
  261. RCC_ClkInitStruct.AHBCLKDivider = RCC_SYSCLK_DIV1;
  262. RCC_ClkInitStruct.APB1CLKDivider = RCC_HCLK_DIV2;
  263. RCC_ClkInitStruct.APB2CLKDivider = RCC_HCLK_DIV1;
  264. if (HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_0) != HAL_OK)
  265. {
  266. Error_Handler();
  267. }
  268. PeriphClkInit.PeriphClockSelection = RCC_PERIPHCLK_ADC;
  269. PeriphClkInit.AdcClockSelection = RCC_ADCPCLK2_DIV2;
  270. if (HAL_RCCEx_PeriphCLKConfig(&PeriphClkInit) != HAL_OK)
  271. {
  272. Error_Handler();
  273. }
  274. }
  275. /* USER CODE BEGIN 4 */
  276. /* USER CODE END 4 */
  277. /**
  278. * @brief This function is executed in case of error occurrence.
  279. * @retval None
  280. */
  281. void Error_Handler(void)
  282. {
  283. /* USER CODE BEGIN Error_Handler_Debug */
  284. /* User can add his own implementation to report the HAL error return state */
  285. __disable_irq();
  286. while (1)
  287. {
  288. }
  289. /* USER CODE END Error_Handler_Debug */
  290. }
  291. #ifdef USE_FULL_ASSERT
  292. /**
  293. * @brief Reports the name of the source file and the source line number
  294. * where the assert_param error has occurred.
  295. * @param file: pointer to the source file name
  296. * @param line: assert_param error line source number
  297. * @retval None
  298. */
  299. void assert_failed(uint8_t *file, uint32_t line)
  300. {
  301. /* USER CODE BEGIN 6 */
  302. /* User can add his own implementation to report the file name and line number,
  303. ex: printf("Wrong parameters value: file %s on line %d\r\n", file, line) */
  304. /* USER CODE END 6 */
  305. }
  306. #endif /* USE_FULL_ASSERT */