/* USER CODE BEGIN Header */ /** ****************************************************************************** * @file : main.c * @brief : Main program body ****************************************************************************** * @attention * * Copyright (c) 2025 STMicroelectronics. * All rights reserved. * * This software is licensed under terms that can be found in the LICENSE file * in the root directory of this software component. * If no LICENSE file comes with this software, it is provided AS-IS. * ****************************************************************************** */ /* USER CODE END Header */ /* Includes ------------------------------------------------------------------*/ #include "main.h" #include "dma.h" #include "tim.h" #include "usart.h" #include "gpio.h" /* Private includes ----------------------------------------------------------*/ /* USER CODE BEGIN Includes */ #include "PLSR.h" #include "includes.h" #include "modbus.h" /* USER CODE END Includes */ /* Private typedef -----------------------------------------------------------*/ /* USER CODE BEGIN PTD */ uint8_t ArrFlag = 0; /* 加速度Flag */ float Acc; /* 加速度 */ uint32_t AllPulseCNT = 0; uint8_t EndFlag = 0; uint8_t FullFlag = 0; /* USER CODE END PTD */ /* Private define ------------------------------------------------------------*/ /* USER CODE BEGIN PD */ /* USER CODE END PD */ /* Private macro -------------------------------------------------------------*/ /* USER CODE BEGIN PM */ /* USER CODE END PM */ /* Private variables ---------------------------------------------------------*/ /* USER CODE BEGIN PV */ /* USER CODE END PV */ /* Private function prototypes -----------------------------------------------*/ void SystemClock_Config(void); /* USER CODE BEGIN PFP */ void PVD_Init(void); /* USER CODE END PFP */ /* Private user code ---------------------------------------------------------*/ /* USER CODE BEGIN 0 */ /* 任务优先级 */ #define TASK_Y1Direction 4 #define TASK_PulseStart 4 #define TASK_DirOutput 4 /* 任务堆栈大小 */ #define Task_Y1Direction_SIZE 256 #define Task_PulseStart_SIZE 256 #define Task_DirOutput_SIZE 256 /* 任务控制块 */ OS_TCB Y1Direction_Tsk; OS_TCB PulseStart_Tsk; OS_TCB DirOutput_Tsk; /* 任务栈 */ CPU_STK Task_Y1Direction_STK[Task_Y1Direction_SIZE]; CPU_STK Task_PulseStart_STK[Task_PulseStart_SIZE]; CPU_STK Task_DirOutput_STK[Task_DirOutput_SIZE]; /* 任务函数 */ void Y1Direction(void *p_arg); void PulseStartTsk(void *p_arg); void DirOutput(void *p_arg); /* USER CODE END 0 */ /** * @brief The application entry point. * @retval int */ int main(void) { /* USER CODE BEGIN 1 */ OS_ERR err; /* USER CODE END 1 */ /* MCU Configuration--------------------------------------------------------*/ /* Reset of all peripherals, Initializes the Flash interface and the Systick. */ HAL_Init(); /* USER CODE BEGIN Init */ PVD_Init(); /* USER CODE END Init */ /* Configure the system clock */ SystemClock_Config(); /* USER CODE BEGIN SysInit */ HAL_PWR_EnableBkUpAccess();/* 使能备份域访问 */ __HAL_RCC_BKPSRAM_CLK_ENABLE();/* 使能备份SRAM时钟 */ HAL_PWREx_EnableBkUpReg();/* 使能备份SRAM */ /* USER CODE END SysInit */ /* Initialize all configured peripherals */ MX_GPIO_Init(); MX_DMA_Init(); MX_USART1_UART_Init(); MX_TIM10_Init(); MX_TIM2_Init(); /* USER CODE BEGIN 2 */ PulseBaseInit(0, 0, 200);//设置为第0段脉冲开始,相对模式, 最大脉冲为0 AccCount = 50; PulseInit(0, 1, 5, 0); //第几段脉冲,频率,数量,下一段脉冲 AddPulse(1, 100, -200, 0); AddPulse(2, 1, 55, 0); BSP_Init(); OSInit(&err); /* 任务1 */ OSTaskCreate((OS_TCB * )&Y1Direction_Tsk, /* 任务控制块 */ (CPU_CHAR* )"Y1Direction", /* 任务名字 */ (OS_TASK_PTR)Y1Direction, /* 任务函数 */ (void * )0, /* 传递给任务函数的参数 */ (OS_PRIO )TASK_Y1Direction, /* 任务优先级 */ (CPU_STK * )&Task_Y1Direction_STK[0], /* 任务堆栈基地址 */ (CPU_STK_SIZE)Task_Y1Direction_SIZE/10, /* 任务堆栈深度限位 */ (CPU_STK_SIZE)Task_Y1Direction_SIZE, /* 任务堆栈大小 */ (OS_MSG_QTY)0, /* 任务内部消息队列能够接收的最大消息数目,为0时禁止接收消息 */ (OS_TICK )0, /* 当使能时间片轮转时的时间片长度,为0时为默认长度 */ (void * )0, /* 用户补充的存储区 */ (OS_OPT )OS_OPT_TASK_STK_CHK|OS_OPT_TASK_STK_CLR, /* 任务选项 */ (OS_ERR * )&err); /* 存放该函数错误时的返回值 */ /* 任务2 */ OSTaskCreate((OS_TCB * )&PulseStart_Tsk, (CPU_CHAR* )"PulseStartTsk", (OS_TASK_PTR)PulseStartTsk, (void * )0, (OS_PRIO )TASK_PulseStart, (CPU_STK * )&Task_PulseStart_STK[0], (CPU_STK_SIZE)Task_PulseStart_SIZE/10, (CPU_STK_SIZE)Task_PulseStart_SIZE, (OS_MSG_QTY)0, (OS_TICK )0, (void * )0, (OS_OPT )OS_OPT_TASK_STK_CHK|OS_OPT_TASK_STK_CLR, (OS_ERR * )&err); /* 任务3 */ OSTaskCreate((OS_TCB * )&DirOutput_Tsk, (CPU_CHAR* )"DirOutput_Tsk", (OS_TASK_PTR)DirOutput, (void * )0, (OS_PRIO )TASK_DirOutput, (CPU_STK * )&Task_DirOutput_STK[0], (CPU_STK_SIZE)Task_DirOutput_SIZE/10, (CPU_STK_SIZE)Task_DirOutput_SIZE, (OS_MSG_QTY)0, (OS_TICK )0, (void * )0, (OS_OPT )OS_OPT_TASK_STK_CHK|OS_OPT_TASK_STK_CLR, (OS_ERR * )&err); OSStart(&err); /* USER CODE END 2 */ /* Infinite loop */ /* USER CODE BEGIN WHILE */ while (1) { /* USER CODE END WHILE */ /* USER CODE BEGIN 3 */ } /* USER CODE END 3 */ } /** * @brief System Clock Configuration * @retval None */ void SystemClock_Config(void) { RCC_OscInitTypeDef RCC_OscInitStruct = {0}; RCC_ClkInitTypeDef RCC_ClkInitStruct = {0}; /** Configure the main internal regulator output voltage */ __HAL_RCC_PWR_CLK_ENABLE(); __HAL_PWR_VOLTAGESCALING_CONFIG(PWR_REGULATOR_VOLTAGE_SCALE1); /** Initializes the RCC Oscillators according to the specified parameters * in the RCC_OscInitTypeDef structure. */ RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_HSE; RCC_OscInitStruct.HSEState = RCC_HSE_ON; RCC_OscInitStruct.PLL.PLLState = RCC_PLL_ON; RCC_OscInitStruct.PLL.PLLSource = RCC_PLLSOURCE_HSE; RCC_OscInitStruct.PLL.PLLM = 6; RCC_OscInitStruct.PLL.PLLN = 72; RCC_OscInitStruct.PLL.PLLP = RCC_PLLP_DIV2; RCC_OscInitStruct.PLL.PLLQ = 4; if (HAL_RCC_OscConfig(&RCC_OscInitStruct) != HAL_OK) { Error_Handler(); } /** Initializes the CPU, AHB and APB buses clocks */ RCC_ClkInitStruct.ClockType = RCC_CLOCKTYPE_HCLK|RCC_CLOCKTYPE_SYSCLK |RCC_CLOCKTYPE_PCLK1|RCC_CLOCKTYPE_PCLK2; RCC_ClkInitStruct.SYSCLKSource = RCC_SYSCLKSOURCE_PLLCLK; RCC_ClkInitStruct.AHBCLKDivider = RCC_SYSCLK_DIV1; RCC_ClkInitStruct.APB1CLKDivider = RCC_HCLK_DIV2; RCC_ClkInitStruct.APB2CLKDivider = RCC_HCLK_DIV1; if (HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_2) != HAL_OK) { Error_Handler(); } } /* USER CODE BEGIN 4 */ /** * @brief 任务1,获取总脉冲数, 脉冲加减速 * @return 无 */ void Y1Direction(void *p_arg) { p_arg = p_arg; OS_ERR err; while(1) { /* 脉冲加减速 */ if (ArrFlag == 1) { if(TIM2->CNT < AccCount) { SetFrequency(0, PulseOutput[PrePulse].Frequency + TIM2->CNT * Acc); } else { SetFrequency(0, PulseOutput[NowPulse].Frequency); ArrFlag = 0; } } /* 获取总的脉冲数 */ if(EndFlag || FullFlag) { if(EndFlag) { AllPulseCNT = GetBase(NowPulse) + PulseOutput[NowPulse].PulseCount; } if(FullFlag) { AllPulseCNT = MAX_Pulse; } } else { AllPulseCNT = TIM2->CNT + GetBase(NowPulse); } OSTimeDly(100, OS_OPT_TIME_DLY, &err); // 延时 100 个节拍 } } /** * @brief 任务2,脉冲开始 * @return 无 */ void PulseStartTsk(void *p_arg) { p_arg = p_arg; OS_ERR err; uint8_t EN = 1; while(1) { if(Register_L[3000] == 0x01 && EN == 1) { PulseStart(); EN = 0; } else if(Register_L[3000] == 0x02 && EN == 0) { EN = 1; } OSTimeDly(100, OS_OPT_TIME_DLY, &err); // 延时 100 个节拍 } } /** * @brief 任务3,脉冲方向输出 * @return 无 */ void DirOutput(void *p_arg) { p_arg = p_arg; OS_ERR err; while(1) { if (Options.DirPost == 0) { if (Options.Dir == 1) { HAL_GPIO_WritePin(GPIOH, Y12_Pin, GPIO_PIN_SET); } else { HAL_GPIO_WritePin(GPIOH, Y12_Pin, GPIO_PIN_RESET); } } if (Options.DirPost == 1) { if (Options.Dir == 1) { HAL_GPIO_WritePin(GPIOH, Y13_Pin, GPIO_PIN_SET); } else { HAL_GPIO_WritePin(GPIOH, Y13_Pin, GPIO_PIN_RESET); } } if (Options.DirPost == 2) { if (Options.Dir == 1) { HAL_GPIO_WritePin(GPIOH, Y14_Pin, GPIO_PIN_SET); } else { HAL_GPIO_WritePin(GPIOH, Y14_Pin, GPIO_PIN_RESET); } } if (Options.DirPost == 3) { if (Options.Dir == 1) { HAL_GPIO_WritePin(GPIOH, Y15_Pin, GPIO_PIN_SET); } else { HAL_GPIO_WritePin(GPIOH, Y15_Pin, GPIO_PIN_RESET); } } OSTimeDly(100, OS_OPT_TIME_DLY, &err); // 延时 100 个节拍 } } #if 0 /** * @brief 定时器中断回调(PWM计数) * @return 无 */ void HAL_TIM_PWM_PulseFinishedCallback(TIM_HandleTypeDef *htim) { if (htim->Instance == TIM10) { PulseCount++; if (1 == Base.PulseMod) /* 如果是绝对模式 */ { if (TIM2->CNT == Base.MAX_Pulse) /* 达到了最大脉冲数 */ { HAL_TIM_PWM_Stop_IT(&htim10,TIM_CHANNEL_1); /* 停止PWM输出 */ } else if(PulseCount == PulseOutput[NowPulse].PulseCount) /* 如果当前段的计数等于要求的计数 */ { PulseCount = 0; HAL_TIM_PWM_Stop_IT(&htim10,TIM_CHANNEL_1); /* 停止PWM输出 */ if(Base.PulseNum - NowPulse >= 1) { Base.PrePulse = NowPulse; /* 保存之前的脉冲段 */ if(PulseOutput[NowPulse].NextPulse == 0) { NowPulse = NowPulse+ 1; } else { NowPulse = PulseOutput[NowPulse].NextPulse; /* 进入下一段脉冲 */ } Acc = GetAcc(PulseOutput[Base.PrePulse].Frequency, PulseOutput[NowPulse].Frequency, Base.AccCount); /* 计算Acc */ ArrFlag = 1; PulseStart(); //开始产生脉冲 } } } else if (0 == Base.PulseMod) /* 如果是相对模式 */ { if (PulseCount == PulseOutput[NowPulse].PulseCount) /* 如果当前段的计数等于要求的计数 */ { PulseCount = 0; HAL_TIM_PWM_Stop_IT(&htim10,TIM_CHANNEL_1); /* 停止PWM输出 */ if(Base.PulseNum - NowPulse > 1) { Base.PrePulse = NowPulse; /* 保存之前的脉冲段 */ if(PulseOutput[NowPulse].NextPulse == 0) { NowPulse = NowPulse+ 1; } else { NowPulse = PulseOutput[NowPulse].NextPulse; /* 进入下一段脉冲 */ } Acc = GetAcc(PulseOutput[Base.PrePulse].Frequency, PulseOutput[NowPulse].Frequency, Base.AccCount); /* 计算Acc */ ArrFlag = 1; PulseStart(); //开始产生脉冲 } else { PulseCount = 0; HAL_TIM_PWM_Stop_IT(&htim10,TIM_CHANNEL_1); /* 停止PWM输出 */ } } } if (ArrFlag == 1) { if(PulseCount < Base.AccCount) { SetFrequency(0, PulseOutput[Base.PrePulse].Frequency + PulseCount * Acc); } else { SetFrequency(0, PulseOutput[NowPulse].Frequency); ArrFlag = 0; } } } } #endif /** * @brief 掉电中断 * @return 无 */ void HAL_PWR_PVDCallback(void) { OSIntEnter(); /* 进入中断 */ // 检查电压是否低于阈值 if (__HAL_PWR_GET_FLAG(PWR_FLAG_PVDO)) { /* 掉电处理内容 */ PLSRSramSave(); } OSIntExit(); /* 退出中断 */ } /** * @brief PVD配置 * @return 无 */ void PVD_Init(void) { PWR_PVDTypeDef PvdStruct; HAL_PWR_EnablePVD(); /* 使能PVD */ PvdStruct.PVDLevel = PWR_PVDLEVEL_3; /* PVD阈值3.1V */ PvdStruct.Mode = PWR_PVD_MODE_IT_RISING; /* 检测掉电 */ HAL_PWR_ConfigPVD(&PvdStruct); HAL_NVIC_SetPriority(PVD_IRQn, 0, 0); /* 配置PVD中断优先级 */ HAL_NVIC_EnableIRQ(PVD_IRQn); /* 使能PVD中断 */ } /* USER CODE END 4 */ /** * @brief This function is executed in case of error occurrence. * @retval None */ void Error_Handler(void) { /* USER CODE BEGIN Error_Handler_Debug */ /* User can add his own implementation to report the HAL error return state */ __disable_irq(); while (1) { } /* USER CODE END Error_Handler_Debug */ } #ifdef USE_FULL_ASSERT /** * @brief Reports the name of the source file and the source line number * where the assert_param error has occurred. * @param file: pointer to the source file name * @param line: assert_param error line source number * @retval None */ void assert_failed(uint8_t *file, uint32_t line) { /* USER CODE BEGIN 6 */ /* User can add his own implementation to report the file name and line number, ex: printf("Wrong parameters value: file %s on line %d\r\n", file, line) */ /* USER CODE END 6 */ } #endif /* USE_FULL_ASSERT */