main.c 12 KB

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