From a253de07f22de58a34a2f1c235602ca26651d6b4 Mon Sep 17 00:00:00 2001 From: zhaohe Date: Mon, 19 Jun 2023 14:58:27 +0800 Subject: [PATCH] update --- .vscode/c_cpp_properties.json | 103 +++++++++++++++++++--------------------- .vscode/settings.json | 8 +++- dep/libiflytop_micro | 2 +- src/board/hardware.cpp | 70 +++++++++++++++++++-------- src/board/hardware.hpp | 26 +++++----- src/board/hardwarelib.cpp | 28 +++++++++++ src/board/hardwarelib.hpp | 30 ++++++++++++ src/board/project_board.hpp | 52 ++++++++++++++++++-- src/umain.cpp | 108 +++++++++++++++++++++++++++++++++++++++++- src/umain.hpp | 56 ++++++++++++++++------ 10 files changed, 375 insertions(+), 108 deletions(-) create mode 100644 src/board/hardwarelib.cpp create mode 100644 src/board/hardwarelib.hpp diff --git a/.vscode/c_cpp_properties.json b/.vscode/c_cpp_properties.json index f0f9b90..2c6200a 100644 --- a/.vscode/c_cpp_properties.json +++ b/.vscode/c_cpp_properties.json @@ -10,86 +10,81 @@ "./app\\Drivers\\CMSIS\\Include", "./dep", "./src", - "./dep\\libtrinamic\\IFLYTOP-TMC-API", "./app\\MDK-ARM", "./app\\Core\\Src", - "./app\\Drivers\\STM32F1xx_HAL_Driver\\Src" + "./app\\Drivers\\STM32F1xx_HAL_Driver\\Src", + "./dep\\libiflytop_micro\\stm32\\basic", + "./dep\\libiflytop_micro\\stm32\\component\\pid", + "./dep\\libiflytop_micro\\stm32\\component\\iflytop_can_slave_v1", + "./dep\\libiflytop_micro\\stm32\\component\\motor_control", + "./dep\\libiflytop_micro\\stm32\\component\\iflytop_can_slave_module", + "./dep\\libiflytop_micro\\stm32\\component\\gxservo", + "./dep\\libiflytop_micro\\stm32\\component\\m3078", + "./dep\\libiflytop_micro\\stm32\\component\\pwm", + "./dep\\libiflytop_micro\\stm32\\component\\tmc\\ic\\TMC4361A", + "./dep\\libiflytop_micro\\stm32\\component\\tmc\\ic\\TMC5130", + "./dep\\libiflytop_micro\\stm32\\component\\tmc\\ic", + "./dep\\libiflytop_micro\\stm32\\component\\tmcutils", + "./dep\\libiflytop_micro\\stm32\\component\\tmp117", + "./src\\board" ], "defines": [ "USE_HAL_DRIVER", "STM32F103xB", - "__CC_ARM", - "__arm__", - "__align(x)=", - "__ALIGNOF__(x)=", "__alignof__(x)=", "__asm(x)=", + "__asm__(x)=", "__forceinline=", "__restrict=", - "__global_reg(n)=", + "__volatile__=", "__inline=", - "__int64=long long", - "__INTADDR__(expr)=0", - "__irq=", - "__packed=", - "__pure=", - "__smc(n)=", - "__svc(n)=", - "__svc_indirect(n)=", - "__svc_indirect_r7(n)=", - "__value_in_regs=", - "__weak=", - "__writeonly=", + "__inline__=", "__declspec(x)=", "__attribute__(x)=", "__nonnull__(x)=", + "__unaligned=", + "__promise(x)=", + "__irq=", + "__swi=", + "__weak=", "__register=", + "__pure=", + "__value_in_regs=", "__breakpoint(x)=", - "__cdp(x,y,z)=", - "__clrex()=", - "__clz(x)=0U", "__current_pc()=0U", "__current_sp()=0U", "__disable_fiq()=", "__disable_irq()=", - "__dmb(x)=", - "__dsb(x)=", "__enable_fiq()=", "__enable_irq()=", - "__fabs(x)=0.0", - "__fabsf(x)=0.0f", - "__force_loads()=", "__force_stores()=", - "__isb(x)=", - "__ldrex(x)=0U", - "__ldrexd(x)=0U", - "__ldrt(x)=0U", "__memory_changed()=", - "__nop()=", - "__pld(...)=", - "__pli(...)=", - "__qadd(x,y)=0", - "__qdbl(x)=0", - "__qsub(x,y)=0", - "__rbit(x)=0U", - "__rev(x)=0U", - "__return_address()=0U", - "__ror(x,y)=0U", "__schedule_barrier()=", "__semihost(x,y)=0", - "__sev()=", - "__sqrt(x)=0.0", - "__sqrtf(x)=0.0f", - "__ssat(x,y)=0", - "__strex(x,y)=0U", - "__strexd(x,y)=0", - "__strt(x,y)=", - "__swp(x,y)=0U", - "__usat(x,y)=0U", - "__wfe()=", - "__wfi()=", - "__yield()=", - "__vfp_status(x,y)=0" + "__vfp_status(x,y)=0", + "__builtin_arm_nop()=", + "__builtin_arm_wfi()=", + "__builtin_arm_wfe()=", + "__builtin_arm_sev()=", + "__builtin_arm_sevl()=", + "__builtin_arm_yield()=", + "__builtin_arm_isb(x)=", + "__builtin_arm_dsb(x)=", + "__builtin_arm_dmb(x)=", + "__builtin_bswap32(x)=0U", + "__builtin_bswap16(x)=0U", + "__builtin_arm_rbit(x)=0U", + "__builtin_clz(x)=0U", + "__builtin_arm_ldrex(x)=0U", + "__builtin_arm_strex(x,y)=0U", + "__builtin_arm_clrex()=", + "__builtin_arm_ssat(x,y)=0U", + "__builtin_arm_usat(x,y)=0U", + "__builtin_arm_ldaex(x)=0U", + "__builtin_arm_stlex(x,y)=0U", + "__GNUC__=4", + "__GNUC_MINOR__=2", + "__GNUC_PATCHLEVEL__=1" ], "intelliSenseMode": "${default}" } diff --git a/.vscode/settings.json b/.vscode/settings.json index 2bf588b..61b9e11 100644 --- a/.vscode/settings.json +++ b/.vscode/settings.json @@ -1,5 +1,11 @@ { "files.associations": { - "compare": "cpp" + "compare": "cpp", + "system_error": "cpp", + "array": "cpp", + "functional": "cpp", + "tuple": "cpp", + "type_traits": "cpp", + "utility": "cpp" } } \ No newline at end of file diff --git a/dep/libiflytop_micro b/dep/libiflytop_micro index 66cbc54..c0bdaba 160000 --- a/dep/libiflytop_micro +++ b/dep/libiflytop_micro @@ -1 +1 @@ -Subproject commit 66cbc54d57a7a2d1b2e704e1042a98a44ce0959e +Subproject commit c0bdaba2f2561fe2cc97c308ad5c3a9745302a91 diff --git a/src/board/hardware.cpp b/src/board/hardware.cpp index 947f867..8a3fe8d 100644 --- a/src/board/hardware.cpp +++ b/src/board/hardware.cpp @@ -45,32 +45,60 @@ void Hardware::debug_light_periodicJob() { } } +uint8_t Hardware::getDeviceId() { return DEVICE_ID; } + /******************************************************************************* * INPUT SENSOR * *******************************************************************************/ -void Hardware::input_sensor_init() {} -uint32_t Hardware::input_sensor_get_table0() { +void Hardware::input_sensors_init() { + SENSOR_INT0.initAsInput(STM32_GPIO::kInput_noIrq, false /*mirror*/); + SENSOR_INT1.initAsInput(STM32_GPIO::kInput_noIrq, false /*mirror*/); +} +uint32_t Hardware::input_sensors_get_table(int tableNum) { uint32_t val = 0; + val |= SENSOR_INT0.getStateUint32() << 0; + val |= SENSOR_INT1.getStateUint32() << 1; return val; } +void Hardware::input_sensors_table_dump(int tableNum) { + ZLOGI(TAG, "[0]:%d [1]:%d", // + SENSOR_INT0.getStateUint32(), // + SENSOR_INT1.getStateUint32()); +} + +void Hardware::output_switch_init() { +#if 0 + SOLENOID_VALVE00.initAsOutput(STM32_GPIO::kOutput_nopull, true /*mirror*/, false); + SOLENOID_VALVE01.initAsOutput(STM32_GPIO::kOutput_nopull, true /*mirror*/, false); + SOLENOID_VALVE02.initAsOutput(STM32_GPIO::kOutput_nopull, true /*mirror*/, false); + SOLENOID_VALVE03.initAsOutput(STM32_GPIO::kOutput_nopull, true /*mirror*/, false); + SOLENOID_VALVE04.initAsOutput(STM32_GPIO::kOutput_nopull, true /*mirror*/, false); + SOLENOID_VALVE05.initAsOutput(STM32_GPIO::kOutput_nopull, true /*mirror*/, false); + SOLENOID_VALVE06.initAsOutput(STM32_GPIO::kOutput_nopull, true /*mirror*/, false); + SOLENOID_VALVE07.initAsOutput(STM32_GPIO::kOutput_nopull, true /*mirror*/, false); + SOLENOID_VALVE08.initAsOutput(STM32_GPIO::kOutput_nopull, true /*mirror*/, false); + SOLENOID_VALVE09.initAsOutput(STM32_GPIO::kOutput_nopull, true /*mirror*/, false); + SOLENOID_VALVE10.initAsOutput(STM32_GPIO::kOutput_nopull, true /*mirror*/, false); + SOLENOID_VALVE11.initAsOutput(STM32_GPIO::kOutput_nopull, true /*mirror*/, false); +#endif +} +void Hardware::output_switch_set(int tableid, uint16_t mask, uint16_t val) { +#if 0 + if (mask & (1 << 0)) SOLENOID_VALVE00.setState((value >> 0) & 0x1); + if (mask & (1 << 1)) SOLENOID_VALVE01.setState((value >> 1) & 0x1); + if (mask & (1 << 2)) SOLENOID_VALVE02.setState((value >> 2) & 0x1); + if (mask & (1 << 3)) SOLENOID_VALVE03.setState((value >> 3) & 0x1); + if (mask & (1 << 4)) SOLENOID_VALVE04.setState((value >> 4) & 0x1); + if (mask & (1 << 5)) SOLENOID_VALVE05.setState((value >> 5) & 0x1); + if (mask & (1 << 6)) SOLENOID_VALVE06.setState((value >> 6) & 0x1); + if (mask & (1 << 7)) SOLENOID_VALVE07.setState((value >> 7) & 0x1); + if (mask & (1 << 8)) SOLENOID_VALVE08.setState((value >> 8) & 0x1); + if (mask & (1 << 9)) SOLENOID_VALVE09.setState((value >> 9) & 0x1); + if (mask & (1 << 10)) SOLENOID_VALVE10.setState((value >> 10) & 0x1); + if (mask & (1 << 11)) SOLENOID_VALVE11.setState((value >> 11) & 0x1); +#endif +} -void Hardware::input_sensor_table0_dump() { - uint32_t val = input_sensor_get_table0(); - ZLOGI(TAG, "input_sensor_table0_dump:%d%d%d%d %d%d%d%d %d%d%d%d %d%d%d", (val >> 0) & 0x01, // - (val >> 1) & 0x01, // - (val >> 2) & 0x01, // - (val >> 3) & 0x01, // - (val >> 4) & 0x01, // - (val >> 5) & 0x01, // - (val >> 6) & 0x01, // - (val >> 7) & 0x01, // - (val >> 8) & 0x01, // - (val >> 9) & 0x01, // - (val >> 10) & 0x01, // - (val >> 11) & 0x01, // - (val >> 12) & 0x01, // - (val >> 13) & 0x01, // - (val >> 14) & 0x01); - // return val; -} \ No newline at end of file +uint32_t Hardware::output_switch_get(int tableid) { return 0; } +void Hardware::output_switch_dump(int tableid) {} diff --git a/src/board/hardware.hpp b/src/board/hardware.hpp index 676c082..2943ca3 100644 --- a/src/board/hardware.hpp +++ b/src/board/hardware.hpp @@ -1,5 +1,6 @@ #pragma once #include "board/project_board.hpp" +#include "hardwarelib.hpp" #include "libiflytop_micro/stm32/basic/basic.h" #include "libiflytop_micro\stm32\basic\iflytop_micro_os.hpp" #include "libiflytop_micro\stm32\basic\stm32_hal.hpp" @@ -8,18 +9,10 @@ #include "usart.h" namespace iflytop { #define MAX_HARDWARE_LISTENER_NUM 5 -class HardwareListener { - public: - virtual void Hardware_OnHardwareException(){}; - virtual void Hardware_OnCanRxData(void *packet){}; - virtual void Hardware_onHomeRefSwitchISR(){}; -}; - class Hardware : public STM32_HAL_LISTENER, // public IflytopMicroOS, public STM32_GPIO_LISTENER { private: - public: public: @@ -33,13 +26,24 @@ class Hardware : public STM32_HAL_LISTENER, // void debug_light_init(); void debug_light_periodicJob(); + uint8_t getDeviceId(); + public: /******************************************************************************* * INPUT SENSOR * *******************************************************************************/ - void input_sensor_init(); - uint32_t input_sensor_get_table0(); - void input_sensor_table0_dump(); + void input_sensors_init(); + uint32_t input_sensors_get_table(int tableNum); + void input_sensors_table_dump(int tableNum); + + /******************************************************************************* + * OUTPUT SENSOR * + *******************************************************************************/ + void output_switch_init(); + void output_switch_set(int tableid, uint16_t mask, uint16_t val); + void output_switch_dump(int tableid); + uint32_t output_switch_get(int tableid); + public: /******************************************************************************* diff --git a/src/board/hardwarelib.cpp b/src/board/hardwarelib.cpp new file mode 100644 index 0000000..b6c2137 --- /dev/null +++ b/src/board/hardwarelib.cpp @@ -0,0 +1,28 @@ +#include "hardwarelib.hpp" + +#define TAG "hardware" +using namespace iflytop; + +/******************************************************************************* + * TMC5130HImpl * + *******************************************************************************/ + +void TMC5130HImpl::initialize(const char *name, IflytopMicroOS *os, SPI_HandleTypeDef *spi, STM32_GPIO *spics, STM32_GPIO *enn) { + m_name = name; + m_os = os; + m_port.initialize(os, spi, spics, enn); + TMC5130::createDeafultTMC5130Config(&m_config, &m_port); + tmc5130.initialize(0, &m_config); + + uint32_t idVersion0 = tmc5130.readChipVERSION(); + ZLOGI(m_name, "idVersion :%x", idVersion0); + + tmc5130.setMotorShaft(false); + + tmc5130.setAcceleration(30000); + tmc5130.setDeceleration(30000); + + tmc5130.setIHOLD_IRUN(0, 3, 0); +} +void TMC5130HImpl::periodicJob() { tmc5130.periodicJob(m_os->getTicket()); } +TMC5130 *TMC5130HImpl::getTMC5130() { return &tmc5130; } diff --git a/src/board/hardwarelib.hpp b/src/board/hardwarelib.hpp new file mode 100644 index 0000000..dcd0f7d --- /dev/null +++ b/src/board/hardwarelib.hpp @@ -0,0 +1,30 @@ +#pragma once +#include "board/project_board.hpp" +#include "libiflytop_micro/stm32/basic/basic.h" +#include "libiflytop_micro\stm32\basic\iflytop_micro_os.hpp" +#include "libiflytop_micro\stm32\basic\stm32_hal.hpp" +#include "main.h" +#include "usart.h" + +// TMC +#include "libiflytop_micro\stm32\component\tmc\ic\tmc4361A.hpp" +#include "libiflytop_micro\stm32\component\tmc\ic\ztmc2160.hpp" +#include "libiflytop_micro\stm32\component\tmc\ic\ztmc5130.hpp" +#include "libiflytop_micro\stm32\component\tmcutils\tmc51x0_port_impl.hpp" + +namespace iflytop { +class TMC5130HImpl { + public: + const char* m_name; + IflytopMicroOS* m_os; + + TMC51X0PortImpl m_port; + TMC5130::TMC5130Config_t m_config; + TMC5130 tmc5130; + + public: + void initialize(const char* name, IflytopMicroOS* os, SPI_HandleTypeDef* spi, STM32_GPIO* spics, STM32_GPIO* enn); + void periodicJob(); + TMC5130* getTMC5130(); +}; +} // namespace iflytop diff --git a/src/board/project_board.hpp b/src/board/project_board.hpp index 12d846a..ba48191 100644 --- a/src/board/project_board.hpp +++ b/src/board/project_board.hpp @@ -1,10 +1,54 @@ #pragma once #include "libiflytop_micro\stm32\basic\stm32_gpio.hpp" -#define VERSION "v1.0" +#define VERSION "v1.0" +#define DEVICE_ID 200 + // 调试串口 -#define DEBUG_UART huart2 +#define DEBUG_UART huart1 // 调试指示灯 -#define DEBUG_LIGHT_GPIO PA1 +#define DEBUG_LIGHT_GPIO PB3 // 微秒延迟定时器,注意该延时定时器需要按照以下文档进行配置 // http://192.168.1.3:3000/zwikipedia/iflytop_wikipedia/src/branch/master/doc/stm32cubemx_us_timer.md -#define DELAY_US_TIMER htim4 \ No newline at end of file +#define DELAY_US_TIMER htim4 + +#define CONFIG_ENABLE_IFLYTOP_CAN_SLAVE_MODULE 1 +#define CONFIG_ENABLE_TMC_MOTOR 1 + +/******************************************************************************* + * 5130步进电机配置 * + *******************************************************************************/ + +#if CONFIG_ENABLE_TMC_MOTOR +// 电机通道编号 +#define MOTOR_SPI hspi1 +#define TMC5130_MOTOR_NUM 1 +#define MOTOR0_CSN PA4 +#define MOTOR0_ENN PB9 +// #define MOTOR1_SPI_MODE_SELECT + +// 步进电机 +#define MOTOR_R_SENSOR PB13 +#define MOTOR_L_SENSOR PB12 // 作为零点限位 +#endif + +#if CONFIG_ENABLE_IFLYTOP_CAN_SLAVE_MODULE +/******************************************************************************* + * CAN REG LIST * + *******************************************************************************/ +#define DEVICE_BASIC_CTRL_ADD_BASE 0 // 模块控制基本功能 https://iflytop1.feishu.cn/wiki/wikcnBmIF0vqLzbzWBsB3VDMalb +#define REG_MOTOR1_CTRL_ADD_BASE 10000 // 步进电机控制基地址 https://iflytop1.feishu.cn/wiki/wikcnI9UaaRBfWQ7zY2PQfik5hb +#define REG_GPIO_INPUT0 1000 // GPIO输入寄存器0 +#define REG_GPIO_OUTPUT0_WRITE 1100 // GPIO输出寄存器0 0-16,写时,高16位为掩码 +#define REG_GPIO_OUTPUT0_STATE 1101 // GPIO输出寄存器0 0-16,写时,高16位为掩码 +#endif + + +/******************************************************************************* + * GPIO INPUT * + *******************************************************************************/ +#define SENSOR_INT1 PA0 +#define SENSOR_INT0 PA1 + +/******************************************************************************* + * GPIO OUTPUT * + *******************************************************************************/ diff --git a/src/umain.cpp b/src/umain.cpp index 36db638..a9ad837 100644 --- a/src/umain.cpp +++ b/src/umain.cpp @@ -2,6 +2,7 @@ #include #include + #include "iwdg.h" using namespace iflytop; @@ -12,6 +13,31 @@ using namespace std; *******************************************************************************/ #define TAG "umain" int random(int max) { return SysTick->VAL % max; } + +#if CONFIG_ENABLE_IFLYTOP_CAN_SLAVE_MODULE +/******************************************************************************* + * IflytopCanProtocolStackProcesser * + *******************************************************************************/ +icps::error_t Main::onHostRegisterWriteEvent(IflytopCanProtocolStackProcesser *processer, icps::WriteEvent *writeEvent) { + if (m_deviceBaseControlService.isThisRegOwnToMe(writeEvent->reg)) { + return m_deviceBaseControlService.processIflytopCanRegisterWriteEvent(writeEvent); + } else if (writeEvent->reg->add == REG_GPIO_OUTPUT0_WRITE) { + uint32_t mask = (writeEvent->newvalue & 0xFFFF0000) >> 16; + uint32_t value = writeEvent->newvalue & 0xFFFF; + m_hardware.output_switch_set(0, (uint16_t)mask, value); + return icps::kSuccess; + } + return icps::kSuccess; +} +icps::error_t Main::onHostRegisterReadEvent(IflytopCanProtocolStackProcesser *processer, icps::ReadEvent *event) { return icps::kSuccess; } +void Main::onHostRegisterReportEvent(IflytopCanProtocolStackProcesser *processer, icps::ReportEvent *event) {} + +icps::error_t Main::onSetEngineerMode(DeviceBaseControlService *service, int32_t engineer_mode) { + ZLOGI(TAG, "onSetEngineerMode %d", engineer_mode); + return icps::kSuccess; +} +#endif + /******************************************************************************* * MAIN * *******************************************************************************/ @@ -28,10 +54,90 @@ void Main::main(int argc, char const *argv[]) { ZLOGI(TAG, "setup") m_hardware.initialize(); m_hardware.debug_light_init(); - m_hardware.input_sensor_init(); + m_hardware.input_sensors_init(); + m_hardware.output_switch_init(); + +#if CONFIG_ENABLE_TMC_MOTOR + m_motor5130_1.initialize("motor1", &m_hardware, &MOTOR_SPI, &MOTOR0_CSN, &MOTOR0_ENN); +#endif + +#if CONFIG_ENABLE_IFLYTOP_CAN_SLAVE_MODULE + /******************************************************************************* + * m_protocolStack * + *******************************************************************************/ + IflytopCanProtocolStackProcesser::createDefaultConfig(&m_iflytopCanStackConfig, m_hardware.getDeviceId(), m_regList, ZARRAY_SIZE(m_regList)); + m_protocolStack.initialize(&m_hardware, &m_iflytopCanStackConfig); + m_protocolStack.setDumpPacketFlag(false); + m_protocolStack.registerListener(this); + m_protocolStack.activeReg(REG_GPIO_INPUT0, icps::kr, 0); + m_protocolStack.activeReg(REG_GPIO_OUTPUT0_WRITE, icps::kw, 0); + m_protocolStack.activeReg(REG_GPIO_OUTPUT0_STATE, icps::kr, 0); + + /******************************************************************************* + * m_deviceBaseControlService * + *******************************************************************************/ + m_deviceBaseControlService.initialize(&m_hardware, &m_protocolStack, m_hardware.getDeviceId()); + m_deviceBaseControlService.setListener(this); + +#if CONFIG_ENABLE_TMC_MOTOR + /******************************************************************************* + * 电机服务 * + *******************************************************************************/ + { + TMC5130 *tmc_motor = m_motor5130_1.getTMC5130(); + SingleAxisMotorControlerV2 *service = &m_motorService; + MOTOR_L_SENSOR.initAsInput(STM32_GPIO::kInput_risingAndFallingIrq, true /*mirror*/); + STM32_GPIO *homeGpio = &MOTOR_L_SENSOR; + + tmc_motor->setMotorShaft(false); + tmc_motor->setIHOLD_IRUN(2, 3, 0); + + service->initialize( // + "motorService", &m_protocolStack, REG_MOTOR1_CTRL_ADD_BASE, + homeGpio, // ZERO_GPIO + NULL, // LGPIO + NULL, // RGPIO + tmc_motor, // motor + &m_hardware // os + ); + + // service->disableMoveHomeCheck(); + service->cfg_acc->setVal(30000); // 加速度 + service->cfg_dec->setVal(30000); // 减速度 + service->cfg_velocity->setVal(300000); // 位置模式时运行的速度 + service->cfg_zero_shift->setVal(0); // 回零时的偏移量 + service->cfg_runhome_velocity->setVal(50000); // 回零速度 + service->cfg_runtohome_dec->setVal(100000); // 回零减速度 + service->cfg_min_pos->setVal(INT32_MIN); // 最小位置 + service->cfg_max_pos->setVal(INT32_MAX); // 最大位置 + service->cfg_runtohome_max_distance->setVal(256 * 200 * 5); // 回零时移动的最大位移 + service->cfg_runtohome_leave_zero_point_distance->setVal(256 * 200 * 5); // 回零时离开零点的距离 + service->cfg_runtohome_keep_move_distance->setVal(256 * 25 * 1); // 回零时保持运动的距离 + } + +#endif +#endif + while (true) { m_hardware.periodicJob(); m_hardware.debug_light_periodicJob(); + + if (m_ticket % 3000 == 0) { + m_hardware.input_sensors_table_dump(0); + } + +#if CONFIG_ENABLE_IFLYTOP_CAN_SLAVE_MODULE + if (m_ticket % 20 == 0) { + m_protocolStack.writeRegValue(REG_GPIO_INPUT0, m_hardware.input_sensors_get_table(0), false); + m_protocolStack.writeRegValue(REG_GPIO_OUTPUT0_STATE, m_hardware.output_switch_get(0), false); + } + m_protocolStack.periodicJob(); +#endif + +#if CONFIG_ENABLE_TMC_MOTOR + m_motor5130_1.periodicJob(); +#endif + HAL_IWDG_Refresh(&hiwdg); } } diff --git a/src/umain.hpp b/src/umain.hpp index fc75647..72447f3 100644 --- a/src/umain.hpp +++ b/src/umain.hpp @@ -1,39 +1,65 @@ #pragma once #include #include +#include #include "board/project_board.hpp" #include "libiflytop_micro\stm32\basic\basic.h" -#include "libiflytop_micro\stm32\component\tmc\ic\tmc4361A.hpp" // -#include - #include "board/hardware.hpp" +#include "board/project_board.hpp" +#include "libiflytop_micro\stm32\basic\basic.h" #include "libiflytop_micro\stm32\basic\stm32_tim.hpp" #include "libiflytop_micro\stm32\basic\zsignal.hpp" +// +#ifdef HAL_CAN_MODULE_ENABLED #include "libiflytop_micro\stm32\component\iflytop_can_slave_module\device_base_control_service.hpp" #include "libiflytop_micro\stm32\component\iflytop_can_slave_module\idcard_reader_service.hpp" #include "libiflytop_micro\stm32\component\iflytop_can_slave_module\single_axis_motor_control_v2.hpp" #include "libiflytop_micro\stm32\component\iflytop_can_slave_v1\iflytop_can_slave.hpp" -#include "libiflytop_micro\stm32\component\m3078\m3078_code_scaner.hpp" - -#define UPDATE_REG(reg, value, periodms) \ - { \ - static uint32_t lastReportTicket = 0; \ - if (m_hardware.hasPassedMS(lastReportTicket) > (periodms + random(3))) { \ - lastReportTicket = m_hardware.getTicket(); \ - m_protocolStack.writeRegValue(reg, value, false); \ - } \ - } +#endif +// + namespace iflytop { -class Main { +class __class {}; + +class Main : +#if CONFIG_ENABLE_IFLYTOP_CAN_SLAVE_MODULE + public IflytopCanProtocolStackProcesserListener, + public DeviceBaseControlServiceListener, +#endif + public __class { public: Hardware m_hardware; - int32_t m_ticket; // 保证一个数一个数的递增,但不保证时间准确 + +#if CONFIG_ENABLE_IFLYTOP_CAN_SLAVE_MODULE + IflytopCanProtocolStackProcesser::iflytop_can_slave_config_t m_iflytopCanStackConfig; + icps::Reg_t m_regList[128]; + IflytopCanProtocolStackProcesser m_protocolStack; + + DeviceBaseControlService m_deviceBaseControlService; // 设备基础服务 + SingleAxisMotorControlerV2 m_motorService; // 平移电机 +#endif + +#if CONFIG_ENABLE_TMC_MOTOR + TMC5130HImpl m_motor5130_1; +#endif + + int32_t m_ticket; // 保证一个数一个数的递增,但不保证时间准确 Main() {} void main(int argc, char const *argv[]); +#if CONFIG_ENABLE_IFLYTOP_CAN_SLAVE_MODULE + /******************************************************************************* + * IflytopCanProtocolStackProcesser * + *******************************************************************************/ + virtual icps::error_t onHostRegisterWriteEvent(IflytopCanProtocolStackProcesser *processer, icps::WriteEvent *writeEvent); + virtual icps::error_t onHostRegisterReadEvent(IflytopCanProtocolStackProcesser *processer, icps::ReadEvent *writeEvent); + virtual void onHostRegisterReportEvent(IflytopCanProtocolStackProcesser *processer, icps::ReportEvent *writeEvent); + virtual icps::error_t onSetEngineerMode(DeviceBaseControlService *service, int32_t engineer_mode); +#endif + private: void generateScheduleTicket(); };