diff --git a/CMakeLists.txt b/CMakeLists.txt index 36ad9b5..ef7ff08 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -120,6 +120,7 @@ add_module(bspMCU bspMCU OFF OFF) add_module(RBcore RBcore OFF OFF) add_module(optional optional OFF OFF) add_module(Spoolend Spoolend OFF OFF) +add_module(Spoolold Spoolold OFF OFF) if(A_BUILD_MAIN) add_subdirectory(main) diff --git a/Spoolend/Spoolend.c b/Spoolend/Spoolend.c index d492139..c73524b 100644 --- a/Spoolend/Spoolend.c +++ b/Spoolend/Spoolend.c @@ -174,5 +174,10 @@ void SpoolendTask(void) // rd_ComRead(g_ptRS485_2, pcBuffer2, sizeof(pcBuffer2)); rd_ComRead(g_ptFDCAN1, pcBuffer3, sizeof(pcBuffer3)); __enable_irq(); + printf("Hello world\n"); + HAL_GPIO_TogglePin(LED1_GPIO_Port, LED1_Pin); + HAL_GPIO_TogglePin(LED2_GPIO_Port, LED2_Pin); + HAL_GPIO_TogglePin(LED3_GPIO_Port, LED3_Pin); + HAL_Delay(500); } diff --git a/Spoolold/CMakeLists.txt b/Spoolold/CMakeLists.txt new file mode 100644 index 0000000..48e6597 --- /dev/null +++ b/Spoolold/CMakeLists.txt @@ -0,0 +1,15 @@ +cmake_minimum_required(VERSION 3.10) + +set(COMMON_CMAKE_PATH "${CMAKE_CURRENT_SOURCE_DIR}/../library/CMakeLists.txt") + +if(EXISTS ${COMMON_CMAKE_PATH}) + include(${COMMON_CMAKE_PATH}) +else() + message(FATAL_ERROR "Cannot find common build logic at ${COMMON_CMAKE_PATH}") +endif() + +if(TARGET stm32cubemx) + target_link_libraries(Spoolold PUBLIC stm32cubemx) +else() + message(WARNING "[Spoolold] Dependency 'stm32cubemx' not found.") +endif() \ No newline at end of file diff --git a/Spoolold/Spoolold.c b/Spoolold/Spoolold.c new file mode 100644 index 0000000..3db96a3 --- /dev/null +++ b/Spoolold/Spoolold.c @@ -0,0 +1,109 @@ +#include "dmk.h" +#include "flash_operation.h" +#include "modbus.h" +#include "iwdg.h" +#include + +uint32_t Rx_Pre_time = 0; + +void handle_power_on_off() { + // holdingRegisters[5] 决定是否手动关闭电源 + if (holdingRegisters[5] != 0) // 手动关闭电源 + { + + if (holdingRegisters[6] == 0) // 关闭电源 + { + if (holdingRegisters[4] == 0) { + HAL_GPIO_WritePin(K5_GPIO_Port, K5_Pin, GPIO_PIN_SET); + } else { + HAL_GPIO_WritePin(K5_GPIO_Port, K5_Pin, + GPIO_PIN_RESET); // k5设为1 低电平亮灯 + } + } else { + if (holdingRegisters[4] == 0) { + HAL_GPIO_WritePin(K5_GPIO_Port, K5_Pin, GPIO_PIN_RESET); + } else { + HAL_GPIO_WritePin(K5_GPIO_Port, K5_Pin, GPIO_PIN_SET); + } + } + + return; + } + // 自动关闭电源 决定是否手动关闭电源 + if (HAL_GetTick() >= Rx_Pre_time && + HAL_GetTick() - Rx_Pre_time > holdingRegisters[7]) { + if (holdingRegisters[4] == 0) { + HAL_GPIO_WritePin(K5_GPIO_Port, K5_Pin, GPIO_PIN_SET); + } else { + HAL_GPIO_WritePin(K5_GPIO_Port, K5_Pin, GPIO_PIN_RESET); + } + } else { + if (holdingRegisters[4] == 0) { + HAL_GPIO_WritePin(K5_GPIO_Port, K5_Pin, GPIO_PIN_RESET); + } else { + HAL_GPIO_WritePin(K5_GPIO_Port, K5_Pin, GPIO_PIN_SET); + } + } +} + +void SpoolendInit(void) { + Modbus_Init(); + ReadParametersFromFlash((int16_t *)holdingRegisters); + holdingRegisters[8] = 2000; + Rx_Pre_time = HAL_GetTick(); +} + +void SpoolendTask(void) { + HAL_IWDG_Refresh(&hiwdg); + Modbus_Process(); + + if (holdingRegisters[0] == 0) { + HAL_GPIO_WritePin(K1_GPIO_Port, K1_Pin, GPIO_PIN_RESET); + } else { + HAL_GPIO_WritePin(K1_GPIO_Port, K1_Pin, GPIO_PIN_SET); + } + + if (holdingRegisters[1] == 0) { + HAL_GPIO_WritePin(K2_GPIO_Port, K2_Pin, GPIO_PIN_RESET); + } else { + HAL_GPIO_WritePin(K2_GPIO_Port, K2_Pin, GPIO_PIN_SET); + } + + if (holdingRegisters[2] == 0) { + HAL_GPIO_WritePin(K3_GPIO_Port, K3_Pin, GPIO_PIN_RESET); + } else { + HAL_GPIO_WritePin(K3_GPIO_Port, K3_Pin, GPIO_PIN_SET); + } + + if (holdingRegisters[3] == 0) { + HAL_GPIO_WritePin(K4_GPIO_Port, K4_Pin, GPIO_PIN_RESET); + } else { + HAL_GPIO_WritePin(K4_GPIO_Port, K4_Pin, GPIO_PIN_SET); + } + + if (RS485_1_RX) { + RS485_1_RX = 0; + Rx_Pre_time = HAL_GetTick(); + HAL_GPIO_TogglePin(LED1_GPIO_Port, LED1_Pin); + } + + if (RS485_1_Host_Existed) { + RS485_1_Host_Existed = 0; + Rx_Pre_time = HAL_GetTick(); + } + + if (HAL_GetTick() >= Rx_Pre_time && + HAL_GetTick() - Rx_Pre_time > holdingRegisters[7]) // 超时了 + { + MB_WriteHoldingReg(1, 0x033, 0x0); // 停止运行 + } else { + handle_dmk_motor(holdingRegisters[8], holdingRegisters[9]); + } + handle_power_on_off(); + + // only when holdingRegisters[9] == 55 time can be changed + if (holdingRegisters[10] == 55) { + WriteParametersToFlash((int16_t *)holdingRegisters); + holdingRegisters[10] = 1; + } +} \ No newline at end of file diff --git a/Spoolold/dmk.c b/Spoolold/dmk.c new file mode 100644 index 0000000..267be49 --- /dev/null +++ b/Spoolold/dmk.c @@ -0,0 +1,56 @@ +/* + * dmk.c + * + * Created on: Jan 14, 2026 + * Author: akeguo + */ +#include "dmk.h" +#include "main.h" +#include "dma.h" +#include "iwdg.h" +#include "tim.h" +#include "usart.h" +#include "gpio.h" +uint16_t Is_First_Run = 0; + +void handle_dmk_motor(uint16_t velocity, int state) { + +// if (Is_First_Run <= 5) { +// HAL_Delay(50); +// MB_WriteHoldingReg(1, 0, 4);//设置模式为0 +// HAL_Delay(100); +// Is_First_Run++;; +// return; +// } + + char ms_100_state = HAL_GetTick() / 300 % 3; + switch (ms_100_state) { + case 1: { + MB_WriteHoldingReg(1, 0x06a, velocity); + break; + } + case 2: { + if (state == 1) { + //正转 + MB_WriteHoldingReg(1, 0x033, 0x01); + + } else if (state == 0) { + //stop + MB_WriteHoldingReg(1, 0x033, 0x0); + + } else if (state == 2) { + //reverse + MB_WriteHoldingReg(1, 0x033, 0x14); + } + break; + } + case 0: { + MB_WriteHoldingReg(1, 0, 0);//设置模式为0 + break; + } + + } + + +//HAL_Delay(80); +} diff --git a/Spoolend/flash_operation.c b/Spoolold/flash_operation.c similarity index 100% rename from Spoolend/flash_operation.c rename to Spoolold/flash_operation.c diff --git a/Spoolold/include/dmk.h b/Spoolold/include/dmk.h new file mode 100644 index 0000000..8ebb798 --- /dev/null +++ b/Spoolold/include/dmk.h @@ -0,0 +1,20 @@ +/* + * dmk.h + * + * Created on: Jan 14, 2026 + * Author: akeguo + */ + +#ifndef INC_DMK_H_ +#define INC_DMK_H_ +#include "stm32g4xx_hal.h" +#include +#include +#include "modbus.h" +#include "usart.h" + + +extern void handle_dmk_motor(uint16_t velocity, int state); +extern uint16_t Is_First_Run; + +#endif /* INC_DMK_H_ */ diff --git a/Spoolend/include/flash_operation.h b/Spoolold/include/flash_operation.h similarity index 100% rename from Spoolend/include/flash_operation.h rename to Spoolold/include/flash_operation.h diff --git a/Spoolend/include/modbus.h b/Spoolold/include/modbus.h similarity index 100% rename from Spoolend/include/modbus.h rename to Spoolold/include/modbus.h diff --git a/Spoolend/modbus.c b/Spoolold/modbus.c similarity index 100% rename from Spoolend/modbus.c rename to Spoolold/modbus.c