Teensy 4.1 Ethernet support (#19801)

This commit is contained in:
bilsef 2020-10-20 12:35:29 -07:00 committed by Scott Lahteine
parent 53a4b8fa33
commit c6f17ac01e
15 changed files with 478 additions and 11 deletions

View file

@ -107,7 +107,8 @@
/**
* Select a secondary serial port on the board to use for communication with the host.
* :[-1, 0, 1, 2, 3, 4, 5, 6, 7]
* Currently Ethernet (-2) is only supported on Teensy 4.1 boards.
* :[-2, -1, 0, 1, 2, 3, 4, 5, 6, 7]
*/
//#define SERIAL_PORT_2 -1

View file

@ -3456,6 +3456,13 @@
// Default behavior is limited to Z axis only.
#endif
/**
* Ethernet. Use M552 to enable and set the IP address.
*/
#if HAS_ETHERNET
#define MAC_ADDRESS { 0xDE, 0xAD, 0xBE, 0xEF, 0xF0, 0x0D } // A MAC address unique to your network
#endif
/**
* WiFi Support (Espressif ESP32 WiFi)
*/

View file

@ -66,6 +66,8 @@
#ifdef SERIAL_PORT_2
#if SERIAL_PORT_2 == -1
#define MYSERIAL1 usbSerial
#elif SERIAL_PORT_2 == -2
#define MYSERIAL1 ethernet.telnetClient
#elif WITHIN(SERIAL_PORT_2, 0, 8)
#define MYSERIAL1 MSERIAL(SERIAL_PORT_2)
#else

View file

@ -77,6 +77,10 @@
#include "lcd/dwin/e3v2/rotary_encoder.h"
#endif
#if HAS_ETHERNET
#include "feature/ethernet.h"
#endif
#if ENABLED(IIC_BL24CXX_EEPROM)
#include "libs/BL24CXX.h"
#endif
@ -713,6 +717,9 @@ void idle(TERN_(ADVANCED_PAUSE_FEATURE, bool no_stepper_sleep/*=false*/)) {
HAL_idletask();
#endif
// Check network connection
TERN_(HAS_ETHERNET, ethernet.check());
// Handle Power-Loss Recovery
#if ENABLED(POWER_LOSS_RECOVERY) && PIN_EXISTS(POWER_LOSS)
if (printJobOngoing()) recovery.outage();
@ -968,7 +975,7 @@ void setup() {
MYSERIAL0.begin(BAUDRATE);
uint32_t serial_connect_timeout = millis() + 1000UL;
while (!MYSERIAL0 && PENDING(millis(), serial_connect_timeout)) { /*nada*/ }
#if HAS_MULTI_SERIAL
#if HAS_MULTI_SERIAL && !HAS_ETHERNET
MYSERIAL1.begin(BAUDRATE);
serial_connect_timeout = millis() + 1000UL;
while (!MYSERIAL1 && PENDING(millis(), serial_connect_timeout)) { /*nada*/ }
@ -1090,6 +1097,10 @@ void setup() {
SETUP_RUN(settings.first_load()); // Load data from EEPROM if available (or use defaults)
// This also updates variables in the planner, elsewhere
#if HAS_ETHERNET
SETUP_RUN(ethernet.init());
#endif
#if HAS_TOUCH_XPT2046
SETUP_RUN(touch.init());
#endif

View file

@ -23,6 +23,10 @@
#include "../inc/MarlinConfig.h"
#if HAS_ETHERNET
#include "../feature/ethernet.h"
#endif
/**
* Define debug bit-masks
*/
@ -56,8 +60,9 @@ extern uint8_t marlin_debug_flags;
#define SERIAL_OUT(WHAT, V...) (void)CAT(MYSERIAL,SERIAL_CATCHALL).WHAT(V)
#else
#define SERIAL_OUT(WHAT, V...) do{ \
if (!serial_port_index || serial_port_index == SERIAL_BOTH) (void)MYSERIAL0.WHAT(V); \
if ( serial_port_index) (void)MYSERIAL1.WHAT(V); \
const bool port2_open = TERN1(HAS_ETHERNET, ethernet.have_telnet_client); \
if ( serial_port_index == 0 || serial_port_index == SERIAL_BOTH) (void)MYSERIAL0.WHAT(V); \
if ((serial_port_index == 1 || serial_port_index == SERIAL_BOTH) && port2_open) (void)MYSERIAL1.WHAT(V); \
}while(0)
#endif

View file

@ -0,0 +1,175 @@
/**
* Marlin 3D Printer Firmware
* Copyright (c) 2020 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/>.
*
*/
#include "../inc/MarlinConfigPre.h"
#if HAS_ETHERNET
#include "ethernet.h"
#include "../core/serial.h"
#define DEBUG_OUT ENABLED(DEBUG_ETHERNET)
#include "../core/debug_out.h"
bool MarlinEthernet::hardware_enabled, // = false
MarlinEthernet::have_telnet_client; // = false
IPAddress MarlinEthernet::ip,
MarlinEthernet::myDns,
MarlinEthernet::gateway,
MarlinEthernet::subnet;
EthernetClient MarlinEthernet::telnetClient; // connected client
MarlinEthernet ethernet;
EthernetServer server(23); // telnet server
enum linkStates { UNLINKED, LINKING, LINKED, CONNECTING, CONNECTED, NO_HARDWARE } linkState;
#ifdef __IMXRT1062__
static void teensyMAC(uint8_t * const mac) {
const uint32_t m1 = HW_OCOTP_MAC1, m2 = HW_OCOTP_MAC0;
mac[0] = m1 >> 8;
mac[1] = m1 >> 0;
mac[2] = m2 >> 24;
mac[3] = m2 >> 16;
mac[4] = m2 >> 8;
mac[5] = m2 >> 0;
}
#else
byte mac[] = MAC_ADDRESS;
#endif
void ethernet_cable_error() { SERIAL_ERROR_MSG("Ethernet cable is not connected."); }
void MarlinEthernet::init() {
if (!hardware_enabled) return;
SERIAL_ECHO_MSG("Starting network...");
// Init the Ethernet device
#ifdef __IMXRT1062__
uint8_t mac[6];
teensyMAC(mac);
#endif
if (!ip) {
Ethernet.begin(mac); // use DHCP
}
else {
if (!gateway) {
gateway = ip;
gateway[3] = 1;
myDns = gateway;
subnet = IPAddress(255,255,255,0);
}
if (!myDns) myDns = gateway;
if (!subnet) subnet = IPAddress(255,255,255,0);
Ethernet.begin(mac, ip, myDns, gateway, subnet);
}
// Check for Ethernet hardware present
if (Ethernet.hardwareStatus() == EthernetNoHardware) {
SERIAL_ERROR_MSG("No Ethernet hardware found.");
linkState = NO_HARDWARE;
return;
}
linkState = UNLINKED;
if (Ethernet.linkStatus() == LinkOFF)
ethernet_cable_error();
}
void MarlinEthernet::check() {
if (!hardware_enabled) return;
switch (linkState) {
case NO_HARDWARE:
break;
case UNLINKED:
if (Ethernet.linkStatus() == LinkOFF) break;
SERIAL_ECHOLNPGM("Ethernet cable connected");
server.begin();
linkState = LINKING;
break;
case LINKING:
if (!Ethernet.localIP()) break;
SERIAL_ECHOPGM("Successfully started telnet server with IP ");
MYSERIAL0.println(Ethernet.localIP());
linkState = LINKED;
break;
case LINKED:
if (Ethernet.linkStatus() == LinkOFF) {
ethernet_cable_error();
linkState = UNLINKED;
break;
}
telnetClient = server.accept();
if (telnetClient) linkState = CONNECTING;
break;
case CONNECTING:
telnetClient.println("Marlin " SHORT_BUILD_VERSION);
#if defined(STRING_DISTRIBUTION_DATE) && defined(STRING_CONFIG_H_AUTHOR)
telnetClient.println(
" Last Updated: " STRING_DISTRIBUTION_DATE
" | Author: " STRING_CONFIG_H_AUTHOR
);
#endif
telnetClient.println("Compiled: " __DATE__);
SERIAL_ECHOLNPGM("Client connected");
have_telnet_client = true;
linkState = CONNECTED;
break;
case CONNECTED:
if (telnetClient && !telnetClient.connected()) {
SERIAL_ECHOLNPGM("Client disconnected");
telnetClient.stop();
have_telnet_client = false;
linkState = LINKED;
}
if (Ethernet.linkStatus() == LinkOFF) {
ethernet_cable_error();
if (telnetClient) telnetClient.stop();
linkState = UNLINKED;
}
break;
default: break;
}
}
#endif // HAS_ETHERNET

View file

@ -0,0 +1,39 @@
/**
* Marlin 3D Printer Firmware
* Copyright (c) 2020 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
#ifdef __IMXRT1062__
#include <NativeEthernet.h>
#endif
// Teensy 4.1 uses internal MAC Address
class MarlinEthernet {
public:
static bool hardware_enabled, have_telnet_client;
static IPAddress ip, myDns, gateway, subnet;
static EthernetClient telnetClient;
static void init();
static void check();
};
extern MarlinEthernet ethernet;

View file

@ -0,0 +1,125 @@
/**
* Marlin 3D Printer Firmware
* Copyright (c) 2020 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/>.
*
*/
#include "../../../inc/MarlinConfigPre.h"
#if HAS_ETHERNET
#include "../../../feature/ethernet.h"
#include "../../../core/serial.h"
#include "../../gcode.h"
void say_ethernet() { SERIAL_ECHOPGM(" Ethernet "); }
void ETH0_report() {
say_ethernet();
SERIAL_ECHO_TERNARY(ethernet.hardware_enabled, "port ", "en", "dis", "abled.\n");
if (ethernet.hardware_enabled) {
say_ethernet();
SERIAL_ECHO_TERNARY(ethernet.have_telnet_client, "client ", "en", "dis", "abled.\n");
}
else
SERIAL_ECHOLNPGM("Send 'M552 S1' to enable.");
}
void MAC_report() {
uint8_t mac[6];
if (ethernet.hardware_enabled) {
Ethernet.MACAddress(mac);
SERIAL_ECHOPGM(" MAC: ");
LOOP_L_N(i, 6) {
SERIAL_PRINTF("%02X", mac[i]);
if (i < 5) SERIAL_CHAR(':');
}
}
SERIAL_EOL();
}
// Display current values when the link is active,
// otherwise show the stored values
void ip_report(const uint16_t cmd, PGM_P const post, const IPAddress &ipo) {
SERIAL_CHAR('M'); SERIAL_ECHO(cmd); SERIAL_CHAR(' ');
LOOP_L_N(i, 4) {
SERIAL_ECHO(ipo[i]);
if (i < 3) SERIAL_CHAR('.');
}
SERIAL_ECHOPGM(" ; ");
SERIAL_ECHOPGM_P(post);
SERIAL_EOL();
}
void M552_report() {
ip_report(552, PSTR("ip address"), Ethernet.linkStatus() == LinkON ? Ethernet.localIP() : ethernet.ip);
}
void M553_report() {
ip_report(553, PSTR("subnet mask"), Ethernet.linkStatus() == LinkON ? Ethernet.subnetMask() : ethernet.subnet);
}
void M554_report() {
ip_report(554, PSTR("gateway"), Ethernet.linkStatus() == LinkON ? Ethernet.gatewayIP() : ethernet.gateway);
}
/**
* M552: Set IP address, enable/disable network interface
*
* S0 : disable networking
* S1 : enable networking
* S-1 : reset network interface
*
* Pnnn : Set IP address, 0.0.0.0 means acquire an IP address using DHCP
*/
void GcodeSuite::M552() {
const bool seenP = parser.seenval('P');
if (seenP) ethernet.ip.fromString(parser.value_string());
const bool seenS = parser.seenval('S');
if (seenS) {
switch (parser.value_int()) {
case -1:
if (ethernet.telnetClient) ethernet.telnetClient.stop();
ethernet.init();
break;
case 0: ethernet.hardware_enabled = false; break;
case 1: ethernet.hardware_enabled = true; break;
default: break;
}
}
const bool nopar = !seenS && !seenP;
if (nopar || seenS) ETH0_report();
if (nopar || seenP) M552_report();
}
/**
* M553 Pnnn - Set netmask
*/
void GcodeSuite::M553() {
if (parser.seenval('P')) ethernet.subnet.fromString(parser.value_string());
M553_report();
}
/**
* M554 Pnnn - Set Gateway
*/
void GcodeSuite::M554() {
if (parser.seenval('P')) ethernet.gateway.fromString(parser.value_string());
M554_report();
}
#endif // HAS_ETHERNET

View file

@ -766,6 +766,12 @@ void GcodeSuite::process_parsed_command(const bool no_ok/*=false*/) {
case 540: M540(); break; // M540: Set abort on endstop hit for SD printing
#endif
#if HAS_ETHERNET
case 552: M552(); break; // M552: Set IP address
case 553: M553(); break; // M553: Set gateway
case 554: M554(); break; // M554: Set netmask
#endif
#if ENABLED(BAUD_RATE_GCODE)
case 575: M575(); break; // M575: Set serial baudrate
#endif

View file

@ -230,6 +230,9 @@
* M512 - Set/Change/Remove Password
* M524 - Abort the current SD print job started with M24. (Requires SDSUPPORT)
* M540 - Enable/disable SD card abort on endstop hit: "M540 S<state>". (Requires SD_ABORT_ON_ENDSTOP_HIT)
* M552 - Get or set IP address. Enable/disable network interface. (Requires enabled Ethernet port)
* M553 - Get or set IP netmask. (Requires enabled Ethernet port)
* M554 - Get or set IP gateway. (Requires enabled Ethernet port)
* M569 - Enable stealthChop on an axis. (Requires at least one _DRIVER_TYPE to be TMC2130/2160/2208/2209/5130/5160)
* M600 - Pause for filament change: "M600 X<pos> Y<pos> Z<raise> E<first_retract> L<later_retract>". (Requires ADVANCED_PAUSE_FEATURE)
* M603 - Configure filament change: "M603 T<tool> U<unload_length> L<load_length>". (Requires ADVANCED_PAUSE_FEATURE)
@ -778,6 +781,12 @@ private:
TERN_(SD_ABORT_ON_ENDSTOP_HIT, static void M540());
#if HAS_ETHERNET
static void M552();
static void M553();
static void M554();
#endif
TERN_(BAUD_RATE_GCODE, static void M575());
#if ENABLED(ADVANCED_PAUSE_FEATURE)

View file

@ -39,6 +39,10 @@ GCodeQueue queue;
#include "../feature/leds/printer_event_leds.h"
#endif
#if HAS_ETHERNET
#include "../feature/ethernet.h"
#endif
#if ENABLED(BINARY_FILE_TRANSFER)
#include "../feature/binary_stream.h"
#endif
@ -312,15 +316,24 @@ void GCodeQueue::flush_and_request_resend() {
}
inline bool serial_data_available() {
return MYSERIAL0.available() || TERN0(HAS_MULTI_SERIAL, MYSERIAL1.available());
byte data_available = 0;
if (MYSERIAL0.available()) data_available++;
#ifdef SERIAL_PORT_2
const bool port2_open = TERN1(HAS_ETHERNET, ethernet.have_telnet_client);
if (port2_open && MYSERIAL1.available()) data_available++;
#endif
return data_available > 0;
}
inline int read_serial(const uint8_t index) {
switch (index) {
case 0: return MYSERIAL0.read();
#if HAS_MULTI_SERIAL
case 1: return MYSERIAL1.read();
#endif
case 1: {
#if HAS_MULTI_SERIAL
const bool port2_open = TERN1(HAS_ETHERNET, ethernet.have_telnet_client);
if (port2_open) return MYSERIAL1.read();
#endif
}
default: return -1;
}
}

View file

@ -777,6 +777,9 @@
#if SERIAL_PORT == -1 || SERIAL_PORT_2 == -1
#define HAS_USB_SERIAL 1
#endif
#if SERIAL_PORT_2 == -2
#define HAS_ETHERNET 1
#endif
// Fallback Stepper Driver types
#ifndef X_DRIVER_TYPE

View file

@ -150,8 +150,20 @@
#include "../lcd/tft/touch.h"
#endif
#if HAS_ETHERNET
#include "../feature/ethernet.h"
#endif
#pragma pack(push, 1) // No padding between variables
#if HAS_ETHERNET
void ETH0_report();
void MAC_report();
void M552_report();
void M553_report();
void M554_report();
#endif
typedef struct { uint16_t X, Y, Z, X2, Y2, Z2, Z3, Z4, E0, E1, E2, E3, E4, E5, E6, E7; } tmc_stepper_current_t;
typedef struct { uint32_t X, Y, Z, X2, Y2, Z2, Z3, Z4, E0, E1, E2, E3, E4, E5, E6, E7; } tmc_hybrid_threshold_t;
typedef struct { int16_t X, Y, Z, X2, Y2, Z2, Z3, Z4; } tmc_sgt_t;
@ -431,6 +443,15 @@ typedef struct SettingsDataStruct {
touch_calibration_t touch_calibration;
#endif
// Ethernet settings
#if HAS_ETHERNET
bool ethernet_hardware_enabled; // M552 S
uint32_t ethernet_ip, // M552 P
ethernet_dns,
ethernet_gateway, // M553 P
ethernet_subnet; // M554 P
#endif
} SettingsData;
//static_assert(sizeof(SettingsData) <= MARLIN_EEPROM_SIZE, "EEPROM too small to contain SettingsData!");
@ -1384,7 +1405,26 @@ void MarlinSettings::postprocess() {
#endif
//
// Validate CRC and Data Size
// Ethernet network info
//
#if HAS_ETHERNET
{
_FIELD_TEST(ethernet_hardware_enabled);
const bool ethernet_hardware_enabled = ethernet.hardware_enabled;
const uint32_t ethernet_ip = ethernet.ip,
ethernet_dns = ethernet.myDns,
ethernet_gateway = ethernet.gateway,
ethernet_subnet = ethernet.subnet;
EEPROM_WRITE(ethernet_hardware_enabled);
EEPROM_WRITE(ethernet_ip);
EEPROM_WRITE(ethernet_dns);
EEPROM_WRITE(ethernet_gateway);
EEPROM_WRITE(ethernet_subnet);
}
#endif
//
// Report final CRC and Data Size
//
if (!eeprom_error) {
const uint16_t eeprom_size = eeprom_index - (EEPROM_OFFSET),
@ -2241,6 +2281,22 @@ void MarlinSettings::postprocess() {
EEPROM_READ(touch.calibration);
#endif
//
// Ethernet network info
//
#if HAS_ETHERNET
_FIELD_TEST(ethernet_hardware_enabled);
uint32_t ethernet_ip, ethernet_dns, ethernet_gateway, ethernet_subnet;
EEPROM_READ(ethernet.hardware_enabled);
EEPROM_READ(ethernet_ip); ethernet.ip = ethernet_ip;
EEPROM_READ(ethernet_dns); ethernet.myDns = ethernet_dns;
EEPROM_READ(ethernet_gateway); ethernet.gateway = ethernet_gateway;
EEPROM_READ(ethernet_subnet); ethernet.subnet = ethernet_subnet;
#endif
//
// Validate Final Size and CRC
//
eeprom_error = size_error(eeprom_index - (EEPROM_OFFSET));
if (eeprom_error) {
DEBUG_ECHO_START();
@ -3784,6 +3840,15 @@ void MarlinSettings::reset() {
#endif
);
#endif
#if HAS_ETHERNET
CONFIG_ECHO_HEADING("Ethernet:");
if (!forReplay) { CONFIG_ECHO_START(); ETH0_report(); }
CONFIG_ECHO_START(); SERIAL_ECHO_SP(2); MAC_report();
CONFIG_ECHO_START(); SERIAL_ECHO_SP(2); M552_report();
CONFIG_ECHO_START(); SERIAL_ECHO_SP(2); M553_report();
CONFIG_ECHO_START(); SERIAL_ECHO_SP(2); M554_report();
#endif
}
#endif // !DISABLE_M503

View file

@ -63,8 +63,9 @@ restore_configs
opt_set MOTHERBOARD BOARD_TEENSY41
opt_set EXTRUDERS 2
opt_set TEMP_SENSOR_1 1
opt_enable MAGNETIC_PARKING_EXTRUDER
exec_test $1 $2 "MAGNETIC_PARKING_EXTRUDER with LCD"
opt_set SERIAL_PORT_2 -2
opt_enable EEPROM_SETTINGS MAGNETIC_PARKING_EXTRUDER
exec_test $1 $2 "Ethernet, EEPROM, Magnetic Parking Extruder, No LCD"
#
# Mixing Extruder

View file

@ -284,6 +284,7 @@ EMERGENCY_PARSER = src_filter=+<src/feature/e_parser.cpp> -<src/gcode/con
I2C_POSITION_ENCODERS = src_filter=+<src/feature/encoder_i2c.cpp>
IIC_BL24CXX_EEPROM = src_filter=+<src/libs/BL24CXX.cpp>
HAS_SPI_FLASH = src_filter=+<src/libs/W25Qxx.cpp>
HAS_ETHERNET = src_filter=+<src/feature/ethernet.cpp>
HAS_FANMUX = src_filter=+<src/feature/fanmux.cpp>
FILAMENT_WIDTH_SENSOR = src_filter=+<src/feature/filwidth.cpp> +<src/gcode/feature/filwidth>
FWRETRACT = src_filter=+<src/feature/fwretract.cpp> +<src/gcode/feature/fwretract>
@ -1321,6 +1322,7 @@ platform = espressif32@1.11.2
board = esp32dev
build_flags = ${common.build_flags} -DCORE_DEBUG_LEVEL=0
src_filter = ${common.default_src_filter} +<src/HAL/ESP32>
lib_ignore = NativeEthernet
upload_speed = 115200
#upload_port = marlinesp.local
#board_build.flash_mode = qio
@ -1332,6 +1334,7 @@ upload_speed = 115200
platform = teensy
board = teensy31
src_filter = ${common.default_src_filter} +<src/HAL/TEENSY31_32>
lib_ignore = NativeEthernet
#
# Teensy 3.5 / 3.6 (ARM Cortex-M4)
@ -1340,11 +1343,13 @@ src_filter = ${common.default_src_filter} +<src/HAL/TEENSY31_32>
platform = teensy
board = teensy35
src_filter = ${common.default_src_filter} +<src/HAL/TEENSY35_36>
lib_ignore = NativeEthernet
[env:teensy36]
platform = teensy
board = teensy36
src_filter = ${common.default_src_filter} +<src/HAL/TEENSY35_36>
lib_ignore = NativeEthernet
#
# Teensy 4.0 / 4.1 (ARM Cortex-M7)