My Marlin configs for Fabrikator Mini and CTC i3 Pro B
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

ethernet.cpp 4.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  1. /**
  2. * Marlin 3D Printer Firmware
  3. * Copyright (c) 2020 MarlinFirmware [https://github.com/MarlinFirmware/Marlin]
  4. *
  5. * Based on Sprinter and grbl.
  6. * Copyright (c) 2011 Camiel Gubbels / Erik van der Zalm
  7. *
  8. * This program is free software: you can redistribute it and/or modify
  9. * it under the terms of the GNU General Public License as published by
  10. * the Free Software Foundation, either version 3 of the License, or
  11. * (at your option) any later version.
  12. *
  13. * This program is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. * GNU General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU General Public License
  19. * along with this program. If not, see <https://www.gnu.org/licenses/>.
  20. *
  21. */
  22. #include "../inc/MarlinConfigPre.h"
  23. #if HAS_ETHERNET
  24. #include "ethernet.h"
  25. #include "../core/serial.h"
  26. #define DEBUG_OUT ENABLED(DEBUG_ETHERNET)
  27. #include "../core/debug_out.h"
  28. bool MarlinEthernet::hardware_enabled, // = false
  29. MarlinEthernet::have_telnet_client; // = false
  30. IPAddress MarlinEthernet::ip,
  31. MarlinEthernet::myDns,
  32. MarlinEthernet::gateway,
  33. MarlinEthernet::subnet;
  34. EthernetClient MarlinEthernet::telnetClient; // connected client
  35. MarlinEthernet ethernet;
  36. EthernetServer server(23); // telnet server
  37. enum linkStates { UNLINKED, LINKING, LINKED, CONNECTING, CONNECTED, NO_HARDWARE } linkState;
  38. #ifdef __IMXRT1062__
  39. static void teensyMAC(uint8_t * const mac) {
  40. const uint32_t m1 = HW_OCOTP_MAC1, m2 = HW_OCOTP_MAC0;
  41. mac[0] = m1 >> 8;
  42. mac[1] = m1 >> 0;
  43. mac[2] = m2 >> 24;
  44. mac[3] = m2 >> 16;
  45. mac[4] = m2 >> 8;
  46. mac[5] = m2 >> 0;
  47. }
  48. #else
  49. byte mac[] = MAC_ADDRESS;
  50. #endif
  51. void ethernet_cable_error() { SERIAL_ERROR_MSG("Ethernet cable is not connected."); }
  52. void MarlinEthernet::init() {
  53. if (!hardware_enabled) return;
  54. SERIAL_ECHO_MSG("Starting network...");
  55. // Init the Ethernet device
  56. #ifdef __IMXRT1062__
  57. uint8_t mac[6];
  58. teensyMAC(mac);
  59. #endif
  60. if (!ip) {
  61. Ethernet.begin(mac); // use DHCP
  62. }
  63. else {
  64. if (!gateway) {
  65. gateway = ip;
  66. gateway[3] = 1;
  67. myDns = gateway;
  68. subnet = IPAddress(255,255,255,0);
  69. }
  70. if (!myDns) myDns = gateway;
  71. if (!subnet) subnet = IPAddress(255,255,255,0);
  72. Ethernet.begin(mac, ip, myDns, gateway, subnet);
  73. }
  74. // Check for Ethernet hardware present
  75. if (Ethernet.hardwareStatus() == EthernetNoHardware) {
  76. SERIAL_ERROR_MSG("No Ethernet hardware found.");
  77. linkState = NO_HARDWARE;
  78. return;
  79. }
  80. linkState = UNLINKED;
  81. if (Ethernet.linkStatus() == LinkOFF)
  82. ethernet_cable_error();
  83. }
  84. void MarlinEthernet::check() {
  85. if (!hardware_enabled) return;
  86. switch (linkState) {
  87. case NO_HARDWARE:
  88. break;
  89. case UNLINKED:
  90. if (Ethernet.linkStatus() == LinkOFF) break;
  91. SERIAL_ECHOLNPGM("Ethernet cable connected");
  92. server.begin();
  93. linkState = LINKING;
  94. break;
  95. case LINKING:
  96. if (!Ethernet.localIP()) break;
  97. SERIAL_ECHOPGM("Successfully started telnet server with IP ");
  98. MYSERIAL1.println(Ethernet.localIP());
  99. linkState = LINKED;
  100. break;
  101. case LINKED:
  102. if (Ethernet.linkStatus() == LinkOFF) {
  103. ethernet_cable_error();
  104. linkState = UNLINKED;
  105. break;
  106. }
  107. telnetClient = server.accept();
  108. if (telnetClient) linkState = CONNECTING;
  109. break;
  110. case CONNECTING:
  111. telnetClient.println("Marlin " SHORT_BUILD_VERSION);
  112. #if defined(STRING_DISTRIBUTION_DATE) && defined(STRING_CONFIG_H_AUTHOR)
  113. telnetClient.println(
  114. " Last Updated: " STRING_DISTRIBUTION_DATE
  115. " | Author: " STRING_CONFIG_H_AUTHOR
  116. );
  117. #endif
  118. telnetClient.println(" Compiled: " __DATE__);
  119. SERIAL_ECHOLNPGM("Client connected");
  120. have_telnet_client = true;
  121. linkState = CONNECTED;
  122. break;
  123. case CONNECTED:
  124. if (telnetClient && !telnetClient.connected()) {
  125. SERIAL_ECHOLNPGM("Client disconnected");
  126. telnetClient.stop();
  127. have_telnet_client = false;
  128. linkState = LINKED;
  129. }
  130. if (Ethernet.linkStatus() == LinkOFF) {
  131. ethernet_cable_error();
  132. if (telnetClient) telnetClient.stop();
  133. linkState = UNLINKED;
  134. }
  135. break;
  136. default: break;
  137. }
  138. }
  139. #endif // HAS_ETHERNET