Skip to content

Commit

Permalink
clean up
Browse files Browse the repository at this point in the history
  • Loading branch information
thinkyhead committed Nov 26, 2024
1 parent 2647273 commit 9508283
Show file tree
Hide file tree
Showing 17 changed files with 418 additions and 405 deletions.
7 changes: 4 additions & 3 deletions Marlin/Configuration.h
Original file line number Diff line number Diff line change
Expand Up @@ -37,8 +37,6 @@
*/
#define CONFIGURATION_H_VERSION 02010300

// ENABLE CAN SUPPORT HERE
// #define CAN_MASTER
//===========================================================================
//============================= Getting Started =============================
//===========================================================================
Expand Down Expand Up @@ -70,7 +68,7 @@

// Choose the name from boards.h that matches your setup
#ifndef MOTHERBOARD
#define MOTHERBOARD BOARD_MKS_MONSTER8_V2
#define MOTHERBOARD BOARD_RAMPS_14_EFB
#endif

// @section serial
Expand Down Expand Up @@ -126,6 +124,9 @@
//#define RS485_BUS_BUFFER_SIZE 128
#endif

// Enable CAN bus support and protocol
//#define CAN_MASTER

// Enable the Bluetooth serial interface on AT90USB devices
//#define BLUETOOTH

Expand Down
513 changes: 255 additions & 258 deletions Marlin/src/HAL/STM32/CAN.cpp

Large diffs are not rendered by default.

28 changes: 0 additions & 28 deletions Marlin/src/HAL/STM32/CAN.h

This file was deleted.

27 changes: 14 additions & 13 deletions Marlin/src/HAL/STM32/Servo.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,10 @@

#include "Servo.h"

#if ENABLED(CAN_MASTER)
#include "../shared/CAN.h"
#endif

static uint_fast8_t servoCount = 0;
static libServo *servos[NUM_SERVOS] = {0};
constexpr millis_t servoDelay[] = SERVO_DELAY;
Expand Down Expand Up @@ -72,19 +76,16 @@ int8_t libServo::attach(const int pin, const int min, const int max) {

void libServo::move(const int value) {

#ifdef CAN_MASTER // IRON, FORWARD UNDERWATER SERVO COMMAND TO HEAD
int angles[2] = Z_SERVO_ANGLES;

// Translate M280 S10 to M401, M280 S90 to M402
HAL_StatusTypeDef CAN_Send_Gcode_2params(uint32_t Gcode_type, uint32_t Gcode_no, uint32_t parameter1, float value1, uint32_t parameter2, float value2); // PROTOTYPE
if (value == angles[0]) // Deploy angle
CAN_Send_Gcode_2params('M', 401, 0, 0, 0, 0); // IRON, send "M401" instead, enables interrupt etc.
else
if (value == angles[1]) // Stow angle
CAN_Send_Gcode_2params('M', 402, 0, 0, 0, 0); // IRON, send "M401" instead, enables interrupt etc.
else
CAN_Send_Gcode_2params('M', 280, 'S', value, 'P', 0); // M280 S[value] P0
#endif // IRON
#if ENABLED(CAN_MASTER) // Forward direct Servo command to head
constexpr int angles[2] = Z_SERVO_ANGLES;
// Translate M280 S10 to M401, M280 S90 to M402
if (value == angles[0])
CAN_Send_Gcode_2params('M', 401, 0, 0, 0, 0); // Deploy Angle: Send "M401" instead, enables interrupt etc.
else if (value == angles[1])
CAN_Send_Gcode_2params('M', 402, 0, 0, 0, 0); // Stow Angle: Send "M402" instead, enables interrupt etc.
else
CAN_Send_Gcode_2params('M', 280, 'S', value, 'P', 0); // M280 S[value] P0
#endif

if (attach(0) >= 0) {
stm32_servo.write(value);
Expand Down
50 changes: 50 additions & 0 deletions Marlin/src/HAL/shared/CAN.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
/**
* Marlin 3D Printer Firmware
* Copyright (c) 2024 MarlinFirmware [https://github.com/MarlinFirmware/Marlin]
*
* Based on Sprinter and grbl.
* Copyright (c) 2011 Camiel Gubbels / Erik van der Zalm
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*
*/
#pragma once

#include "../../inc/MarlinConfigPre.h"

//#define CAN_DEBUG // Define to show gcodes send to HEAD

#define SOUND_OK 880
#define SOUND_ERROR 40

extern uint32_t CAN_io_state; // CAN virtual IO variable

#define CAN_IO_MASK 0b11111 // Masks for the 5 virtual IO bits
#define CAN_PROBE_MASK 1 // Virtual IO bit
#define CAN_FILAMENT_MASK 2 // Virtual IO bit
#define CAN_X_ENDSTOP_MASK 4 // Virtual IO bit
#define CAN_Y_ENDSTOP_MASK 8 // Virtual IO bit
#define CAN_Z_ENDSTOP_MAS 16 // Virtual IO bit
#define CAN_STRING_MESSAGE_MASK 32 // Signals the head sends a string message
#define CAN_REQUEST_SETUP_MASK 64 // Signals the head requests setup information
#define CAN_TMC_OT_MASK 128 // Signals the head signals a TMC Over Temp error
#define CAN_E0_TARGET_MASK 256 // Signals E0 or E1
#define CAN_ERROR_MASK 512 // Signals the head encountered an error

HAL_StatusTypeDef CAN1_Start(); // FUNCTION PROTOTYPES
HAL_StatusTypeDef CAN1_Stop();
HAL_StatusTypeDef CAN_Send_Gcode();
HAL_StatusTypeDef CAN_Send_Gcode_2params(uint32_t Gcode_type, uint32_t Gcode_no, uint32_t parameter1, float value1, uint32_t parameter2, float value2);
void CAN_Send_Setup(); // Send host configuration to head
void CAN_Idle(); // Idle CAN task
19 changes: 10 additions & 9 deletions Marlin/src/MarlinCore.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,8 @@
#include "HAL/shared/esp_wifi.h"
#include "HAL/shared/cpu_exception/exception_hook.h"

#ifdef CAN_MASTER // IRON, ADDED
#include HAL_PATH(., CAN.h)
#if ENABLED(CAN_MASTER)
#include "HAL/shared/CAN.h"
#endif

#if ENABLED(WIFISUPPORT)
Expand Down Expand Up @@ -1194,14 +1194,15 @@ void setup() {
while (!MYSERIAL3.connected() && PENDING(millis(), serial_connect_timeout)) { /*nada*/ }
#endif
#endif
SERIAL_ECHOLNPGM("\nstart\n"); // IRON, ADDED \n

#ifdef CAN_MASTER // IRON, REPORT CAN START STATUS
if (CAN1_Start() == HAL_OK)
SERIAL_ECHOLNPGM(">>> CAN1 Start: OK");
else
SERIAL_ECHOLNPGM(">>> CAN1 Start: FAILED!");
#endif
SERIAL_ECHOLNPGM(TERN(CAN_MASTER, "\nstart\n", "start"));

#if ENABLED(CAN_MASTER)
SERIAL_ECHOLN(
F(">>> CAN1 Start: "),
CAN1_Start() == HAL_OK ? F("OK") : F("FAILED!")
);
#endif

// Set up these pins early to prevent suicide
#if HAS_KILL
Expand Down
5 changes: 0 additions & 5 deletions Marlin/src/feature/pause.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -225,13 +225,8 @@ bool load_filament(const_float_t slow_load_length/*=0*/, const_float_t fast_load
switch (active_extruder) {
REPEAT_1(NUM_RUNOUT_SENSORS, _CASE_INSERTED)
}
#else
#ifdef CAN_MASTER // IRON, USE VIRTUAL CAN FILAMENT RUNOUT STATUS
if ((!!(CAN_io_state & CAN_FILAMENT_MASK)) != FIL_RUNOUT_STATE)
wait_for_user = false;
#else
if (!FILAMENT_IS_OUT()) wait_for_user = false;
#endif // IRON
#endif
#endif
idle_no_sleep();
Expand Down
18 changes: 9 additions & 9 deletions Marlin/src/feature/runout.h
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,8 @@
#include "pause.h" // for did_pause_print
#include "../MarlinCore.h" // for printingIsActive()

#ifdef CAN_MASTER // IRON, ADDED
#include HAL_PATH(.., CAN.h)
#if ENABLED(CAN_MASTER)
#include "../HAL/shared/CAN.h"
#endif

#include "../inc/MarlinConfig.h"
Expand All @@ -57,6 +57,12 @@

#define FILAMENT_IS_OUT() (READ(FIL_RUNOUT_PIN) == FIL_RUNOUT_STATE)

#if ENABLED(CAN_MASTER)
#define RUNOUT_STATE(N) bool(CAN_io_state & CAN_FILAMENT_MASK) // CAN Virtual Filament Runout pin
#else
#define RUNOUT_STATE(N) READ(FIL_RUNOUT##N##_PIN) // DIO Filament Runout pin
#endif

typedef Flags<
#if NUM_MOTION_SENSORS > NUM_RUNOUT_SENSORS
NUM_MOTION_SENSORS
Expand Down Expand Up @@ -211,13 +217,7 @@ class FilamentSensorBase {

// Return a bitmask of runout pin states
static uint8_t poll_runout_pins() {

#ifdef CAN_MASTER // IRON, VIRTUAL FILAMENT RUNOUT PIN
#define _OR_RUNOUT(N) | ((CAN_io_state & CAN_FILAMENT_MASK) ? _BV((N) - 1) : 0)
#else
#define _OR_RUNOUT(N) | (READ(FIL_RUNOUT##N##_PIN) ? _BV((N) - 1) : 0)
#endif // IRON

#define _OR_RUNOUT(N) | (RUNOUT_STATE(N) ? _BV((N) - 1) : 0)
return (0 REPEAT_1(NUM_RUNOUT_SENSORS, _OR_RUNOUT));
#undef _OR_RUNOUT
}
Expand Down
25 changes: 12 additions & 13 deletions Marlin/src/gcode/gcode.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -28,10 +28,6 @@
#include "gcode.h"
GcodeSuite gcode;

#ifdef CAN_MASTER // IRON
#include HAL_PATH(.., CAN.h)
#endif

#if ENABLED(WIFI_CUSTOM_COMMAND)
extern bool wifi_custom_command(char * const command_ptr);
#endif
Expand Down Expand Up @@ -73,6 +69,11 @@ GcodeSuite gcode;
#include "../feature/fancheck.h"
#endif

#if ENABLED(CAN_MASTER)
#include "../HAL/shared/CAN.h"
#include "../libs/buzzer.h"
#endif

#include "../MarlinCore.h" // for idle, kill

// Inactivity shutdown
Expand Down Expand Up @@ -327,16 +328,14 @@ void GcodeSuite::process_parsed_command(const bool no_ok/*=false*/) {

KEEPALIVE_STATE(IN_HANDLER);

#ifdef CAN_MASTER
if (CAN_Send_Gcode() != HAL_OK) // IRON, SEND COMMAND TO HEAD
{
SERIAL_ECHOPGM("Error: FAILED TO SEND GCODE CAN COMMAND TO HEAD: ");
SERIAL_ECHOLN_P(parser.command_ptr);
#ifndef IRON_DEBUG
BUZZ(1, SOUND_ERROR);
#if ENABLED(CAN_MASTER)
if (CAN_Send_Gcode() != HAL_OK) { // Send command to head
SERIAL_ECHOLN(F("Error: CAN failed to send \""), parser.command_ptr, '"');
#ifndef CAN_DEBUG
BUZZ(1, SOUND_ERROR);
#endif
}
#endif
}
#endif // IRON

/**
* Block all Gcodes except M511 Unlock Printer, if printer is locked
Expand Down
51 changes: 28 additions & 23 deletions Marlin/src/gcode/temp/M306.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@
#include "../gcode.h"
#include "../../lcd/marlinui.h"
#include "../../module/temperature.h"
#include "../../libs/numtostr.h" // IRON, ADDED

/**
* M306: MPC settings and autotune
Expand Down Expand Up @@ -59,23 +58,30 @@ void GcodeSuite::M306() {
#if ENABLED(MPC_AUTOTUNE)
if (parser.seen_test('T')) {

#ifdef CAN_MASTER // IRON, MPC AUTOTUNE INFO
SERIAL_ECHOLNPGM(">>> Forwarding M306 to head board");
SERIAL_ECHOLNPGM(">>> Store MPC setup in the host Configuration.h or use M500");
SERIAL_ECHOLNPGM(">>> MPC heater power is: ", ftostr31ns(MPC_HEATER_POWER), " Watt");
SERIAL_ECHOLNPGM(">>> Please wait for the auto tune results...");
#else
Temperature::MPCTuningType tuning_type;
const uint8_t type = parser.byteval('S', 0);
switch (type) {
case 1: tuning_type = Temperature::MPCTuningType::FORCE_DIFFERENTIAL; break;
case 2: tuning_type = Temperature::MPCTuningType::FORCE_ASYMPTOTIC; break;
default: tuning_type = Temperature::MPCTuningType::AUTO; break;
}
LCD_MESSAGE(MSG_MPC_AUTOTUNE);
thermalManager.MPC_autotune(e, tuning_type);
ui.reset_status();
#endif // IRON
#if ENABLED(CAN_MASTER) // MPC Autotune info

SERIAL_ECHOLNPGM(
">>> Forwarding M306 to head board\n"
">>> Store MPC setup in the host Configuration.h or use M500\n"
">>> MPC heater power is: ", p_float_t(MPC_HEATER_POWER, 1), " Watts\n"
">>> Please wait for the auto tune results..."
);

#else

Temperature::MPCTuningType tuning_type;
const uint8_t type = parser.byteval('S', 0);
switch (type) {
case 1: tuning_type = Temperature::MPCTuningType::FORCE_DIFFERENTIAL; break;
case 2: tuning_type = Temperature::MPCTuningType::FORCE_ASYMPTOTIC; break;
default: tuning_type = Temperature::MPCTuningType::AUTO; break;
}
LCD_MESSAGE(MSG_MPC_AUTOTUNE);
thermalManager.MPC_autotune(e, tuning_type);
ui.reset_status();

#endif

return;
}
#endif
Expand All @@ -101,11 +107,10 @@ void GcodeSuite::M306_report(const bool forReplay/*=true*/) {

report_heading(forReplay, F("Model predictive control"));

#ifdef CAN_MASTER // IRON, MPC AUTOTUNE INFO
if (forReplay)
SERIAL_ECHOLNPGM(">>> HOST M306 MPC SETTINGS:");
#endif

#if ENABLED(CAN_MASTER) // MPC Autotune info
if (forReplay) SERIAL_ECHOLNPGM(">>> Host M306 MPC settings:");
#endif

HOTEND_LOOP() {
report_echo_start(forReplay);
MPC_t &mpc = thermalManager.temp_hotend[e].mpc;
Expand Down
Loading

0 comments on commit 9508283

Please sign in to comment.