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.

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377
  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. #ifdef ARDUINO_ARCH_ESP32
  23. #include "../../inc/MarlinConfig.h"
  24. #include <rom/rtc.h>
  25. #include <driver/adc.h>
  26. #include <esp_adc_cal.h>
  27. #include <HardwareSerial.h>
  28. #if ENABLED(USE_ESP32_TASK_WDT)
  29. #include <esp_task_wdt.h>
  30. #endif
  31. #if ENABLED(WIFISUPPORT)
  32. #include <ESPAsyncWebServer.h>
  33. #include "wifi.h"
  34. #if ENABLED(OTASUPPORT)
  35. #include "ota.h"
  36. #endif
  37. #if ENABLED(WEBSUPPORT)
  38. #include "spiffs.h"
  39. #include "web.h"
  40. #endif
  41. #endif
  42. #if ENABLED(ESP3D_WIFISUPPORT)
  43. DefaultSerial1 MSerial0(false, Serial2Socket);
  44. #endif
  45. // ------------------------
  46. // Externs
  47. // ------------------------
  48. portMUX_TYPE MarlinHAL::spinlock = portMUX_INITIALIZER_UNLOCKED;
  49. // ------------------------
  50. // Local defines
  51. // ------------------------
  52. #define V_REF 1100
  53. // ------------------------
  54. // Public Variables
  55. // ------------------------
  56. uint16_t MarlinHAL::adc_result;
  57. // ------------------------
  58. // Private Variables
  59. // ------------------------
  60. esp_adc_cal_characteristics_t characteristics[ADC_ATTEN_MAX];
  61. adc_atten_t attenuations[ADC1_CHANNEL_MAX] = {};
  62. uint32_t thresholds[ADC_ATTEN_MAX];
  63. volatile int numPWMUsed = 0;
  64. volatile struct { pin_t pin; int value; } pwmState[MAX_PWM_PINS];
  65. pin_t chan_pin[CHANNEL_MAX_NUM + 1] = { 0 }; // PWM capable IOpins - not 0 or >33 on ESP32
  66. struct {
  67. uint32_t freq; // ledcReadFreq doesn't work if a duty hasn't been set yet!
  68. uint16_t res;
  69. } pwmInfo[(CHANNEL_MAX_NUM + 1) / 2];
  70. // ------------------------
  71. // Public functions
  72. // ------------------------
  73. #if ENABLED(WIFI_CUSTOM_COMMAND)
  74. bool wifi_custom_command(char * const command_ptr) {
  75. #if ENABLED(ESP3D_WIFISUPPORT)
  76. return esp3dlib.parse(command_ptr);
  77. #else
  78. UNUSED(command_ptr);
  79. return false;
  80. #endif
  81. }
  82. #endif
  83. #if ENABLED(USE_ESP32_EXIO)
  84. HardwareSerial YSerial2(2);
  85. void Write_EXIO(uint8_t IO, uint8_t v) {
  86. if (hal.isr_state()) {
  87. hal.isr_off();
  88. YSerial2.write(0x80 | (((char)v) << 5) | (IO - 100));
  89. hal.isr_on();
  90. }
  91. else
  92. YSerial2.write(0x80 | (((char)v) << 5) | (IO - 100));
  93. }
  94. #endif
  95. void MarlinHAL::init_board() {
  96. #if ENABLED(USE_ESP32_TASK_WDT)
  97. esp_task_wdt_init(10, true);
  98. #endif
  99. #if ENABLED(ESP3D_WIFISUPPORT)
  100. esp3dlib.init();
  101. #elif ENABLED(WIFISUPPORT)
  102. wifi_init();
  103. TERN_(OTASUPPORT, OTA_init());
  104. #if ENABLED(WEBSUPPORT)
  105. spiffs_init();
  106. web_init();
  107. #endif
  108. server.begin();
  109. #endif
  110. // ESP32 uses a GPIO matrix that allows pins to be assigned to hardware serial ports.
  111. // The following code initializes hardware Serial1 and Serial2 to use user-defined pins
  112. // if they have been defined.
  113. #if defined(HARDWARE_SERIAL1_RX) && defined(HARDWARE_SERIAL1_TX)
  114. HardwareSerial Serial1(1);
  115. #ifdef TMC_BAUD_RATE // use TMC_BAUD_RATE for Serial1 if defined
  116. Serial1.begin(TMC_BAUD_RATE, SERIAL_8N1, HARDWARE_SERIAL1_RX, HARDWARE_SERIAL1_TX);
  117. #else // use default BAUDRATE if TMC_BAUD_RATE not defined
  118. Serial1.begin(BAUDRATE, SERIAL_8N1, HARDWARE_SERIAL1_RX, HARDWARE_SERIAL1_TX);
  119. #endif
  120. #endif
  121. #if defined(HARDWARE_SERIAL2_RX) && defined(HARDWARE_SERIAL2_TX)
  122. HardwareSerial Serial2(2);
  123. #ifdef TMC_BAUD_RATE // use TMC_BAUD_RATE for Serial1 if defined
  124. Serial2.begin(TMC_BAUD_RATE, SERIAL_8N1, HARDWARE_SERIAL2_RX, HARDWARE_SERIAL2_TX);
  125. #else // use default BAUDRATE if TMC_BAUD_RATE not defined
  126. Serial2.begin(BAUDRATE, SERIAL_8N1, HARDWARE_SERIAL2_RX, HARDWARE_SERIAL2_TX);
  127. #endif
  128. #endif
  129. // Initialize the i2s peripheral only if the I2S stepper stream is enabled.
  130. // The following initialization is performed after Serial1 and Serial2 are defined as
  131. // their native pins might conflict with the i2s stream even when they are remapped.
  132. #if ENABLED(USE_ESP32_EXIO)
  133. YSerial2.begin(460800 * 3, SERIAL_8N1, 16, 17);
  134. #elif ENABLED(I2S_STEPPER_STREAM)
  135. i2s_init();
  136. #endif
  137. }
  138. void MarlinHAL::idletask() {
  139. #if BOTH(WIFISUPPORT, OTASUPPORT)
  140. OTA_handle();
  141. #endif
  142. TERN_(ESP3D_WIFISUPPORT, esp3dlib.idletask());
  143. }
  144. uint8_t MarlinHAL::get_reset_source() { return rtc_get_reset_reason(1); }
  145. void MarlinHAL::reboot() { ESP.restart(); }
  146. void _delay_ms(int delay_ms) { delay(delay_ms); }
  147. // return free memory between end of heap (or end bss) and whatever is current
  148. int MarlinHAL::freeMemory() { return ESP.getFreeHeap(); }
  149. // ------------------------
  150. // ADC
  151. // ------------------------
  152. #define ADC1_CHANNEL(pin) ADC1_GPIO ## pin ## _CHANNEL
  153. adc1_channel_t get_channel(int pin) {
  154. switch (pin) {
  155. case 39: return ADC1_CHANNEL(39);
  156. case 36: return ADC1_CHANNEL(36);
  157. case 35: return ADC1_CHANNEL(35);
  158. case 34: return ADC1_CHANNEL(34);
  159. case 33: return ADC1_CHANNEL(33);
  160. case 32: return ADC1_CHANNEL(32);
  161. }
  162. return ADC1_CHANNEL_MAX;
  163. }
  164. void adc1_set_attenuation(adc1_channel_t chan, adc_atten_t atten) {
  165. if (attenuations[chan] != atten) {
  166. adc1_config_channel_atten(chan, atten);
  167. attenuations[chan] = atten;
  168. }
  169. }
  170. void MarlinHAL::adc_init() {
  171. // Configure ADC
  172. adc1_config_width(ADC_WIDTH_12Bit);
  173. // Configure channels only if used as (re-)configuring a pin for ADC that is used elsewhere might have adverse effects
  174. TERN_(HAS_TEMP_ADC_0, adc1_set_attenuation(get_channel(TEMP_0_PIN), ADC_ATTEN_11db));
  175. TERN_(HAS_TEMP_ADC_1, adc1_set_attenuation(get_channel(TEMP_1_PIN), ADC_ATTEN_11db));
  176. TERN_(HAS_TEMP_ADC_2, adc1_set_attenuation(get_channel(TEMP_2_PIN), ADC_ATTEN_11db));
  177. TERN_(HAS_TEMP_ADC_3, adc1_set_attenuation(get_channel(TEMP_3_PIN), ADC_ATTEN_11db));
  178. TERN_(HAS_TEMP_ADC_4, adc1_set_attenuation(get_channel(TEMP_4_PIN), ADC_ATTEN_11db));
  179. TERN_(HAS_TEMP_ADC_5, adc1_set_attenuation(get_channel(TEMP_5_PIN), ADC_ATTEN_11db));
  180. TERN_(HAS_TEMP_ADC_6, adc2_set_attenuation(get_channel(TEMP_6_PIN), ADC_ATTEN_11db));
  181. TERN_(HAS_TEMP_ADC_7, adc3_set_attenuation(get_channel(TEMP_7_PIN), ADC_ATTEN_11db));
  182. TERN_(HAS_HEATED_BED, adc1_set_attenuation(get_channel(TEMP_BED_PIN), ADC_ATTEN_11db));
  183. TERN_(HAS_TEMP_CHAMBER, adc1_set_attenuation(get_channel(TEMP_CHAMBER_PIN), ADC_ATTEN_11db));
  184. TERN_(HAS_TEMP_PROBE, adc1_set_attenuation(get_channel(TEMP_PROBE_PIN), ADC_ATTEN_11db));
  185. TERN_(HAS_TEMP_COOLER, adc1_set_attenuation(get_channel(TEMP_COOLER_PIN), ADC_ATTEN_11db));
  186. TERN_(HAS_TEMP_BOARD, adc1_set_attenuation(get_channel(TEMP_BOARD_PIN), ADC_ATTEN_11db));
  187. TERN_(FILAMENT_WIDTH_SENSOR, adc1_set_attenuation(get_channel(FILWIDTH_PIN), ADC_ATTEN_11db));
  188. // Note that adc2 is shared with the WiFi module, which has higher priority, so the conversion may fail.
  189. // That's why we're not setting it up here.
  190. // Calculate ADC characteristics (i.e., gain and offset factors for each attenuation level)
  191. for (int i = 0; i < ADC_ATTEN_MAX; i++) {
  192. esp_adc_cal_characterize(ADC_UNIT_1, (adc_atten_t)i, ADC_WIDTH_BIT_12, V_REF, &characteristics[i]);
  193. // Change attenuation 100mV below the calibrated threshold
  194. thresholds[i] = esp_adc_cal_raw_to_voltage(4095, &characteristics[i]);
  195. }
  196. }
  197. #ifndef ADC_REFERENCE_VOLTAGE
  198. #define ADC_REFERENCE_VOLTAGE 3.3
  199. #endif
  200. void MarlinHAL::adc_start(const pin_t pin) {
  201. const adc1_channel_t chan = get_channel(pin);
  202. uint32_t mv;
  203. esp_adc_cal_get_voltage((adc_channel_t)chan, &characteristics[attenuations[chan]], &mv);
  204. adc_result = mv * 1023.0f / float(ADC_REFERENCE_VOLTAGE) / 1000.0f;
  205. // Change the attenuation level based on the new reading
  206. adc_atten_t atten;
  207. if (mv < thresholds[ADC_ATTEN_DB_0] - 100)
  208. atten = ADC_ATTEN_DB_0;
  209. else if (mv > thresholds[ADC_ATTEN_DB_0] - 50 && mv < thresholds[ADC_ATTEN_DB_2_5] - 100)
  210. atten = ADC_ATTEN_DB_2_5;
  211. else if (mv > thresholds[ADC_ATTEN_DB_2_5] - 50 && mv < thresholds[ADC_ATTEN_DB_6] - 100)
  212. atten = ADC_ATTEN_DB_6;
  213. else if (mv > thresholds[ADC_ATTEN_DB_6] - 50)
  214. atten = ADC_ATTEN_DB_11;
  215. else return;
  216. adc1_set_attenuation(chan, atten);
  217. }
  218. // ------------------------
  219. // PWM
  220. // ------------------------
  221. int8_t channel_for_pin(const uint8_t pin) {
  222. for (int i = 0; i <= CHANNEL_MAX_NUM; i++)
  223. if (chan_pin[i] == pin) return i;
  224. return -1;
  225. }
  226. // get PWM channel for pin - if none then attach a new one
  227. // return -1 if fail or invalid pin#, channel # (0-15) if success
  228. int8_t get_pwm_channel(const pin_t pin, const uint32_t freq, const uint16_t res) {
  229. if (!WITHIN(pin, 1, MAX_PWM_IOPIN)) return -1; // Not a hardware PWM pin!
  230. int8_t cid = channel_for_pin(pin);
  231. if (cid >= 0) return cid;
  232. // Find an empty adjacent channel (same timer & freq/res)
  233. for (int i = 0; i <= CHANNEL_MAX_NUM; i++) {
  234. if (chan_pin[i] == 0) {
  235. if (chan_pin[i ^ 0x1] != 0) {
  236. if (pwmInfo[i / 2].freq == freq && pwmInfo[i / 2].res == res) {
  237. chan_pin[i] = pin; // Allocate PWM to this channel
  238. ledcAttachPin(pin, i);
  239. return i;
  240. }
  241. }
  242. else if (cid == -1) // Pair of empty channels?
  243. cid = i & 0xFE; // Save lower channel number
  244. }
  245. }
  246. // not attached, is an empty timer slot avail?
  247. if (cid >= 0) {
  248. chan_pin[cid] = pin;
  249. pwmInfo[cid / 2].freq = freq;
  250. pwmInfo[cid / 2].res = res;
  251. ledcSetup(cid, freq, res);
  252. ledcAttachPin(pin, cid);
  253. }
  254. return cid; // -1 if no channel avail
  255. }
  256. void MarlinHAL::set_pwm_duty(const pin_t pin, const uint16_t v, const uint16_t v_size/*=_BV(PWM_RESOLUTION)-1*/, const bool invert/*=false*/) {
  257. const int8_t cid = get_pwm_channel(pin, PWM_FREQUENCY, PWM_RESOLUTION);
  258. if (cid >= 0) {
  259. uint32_t duty = map(invert ? v_size - v : v, 0, v_size, 0, _BV(PWM_RESOLUTION)-1);
  260. ledcWrite(cid, duty);
  261. }
  262. }
  263. int8_t MarlinHAL::set_pwm_frequency(const pin_t pin, const uint32_t f_desired) {
  264. const int8_t cid = channel_for_pin(pin);
  265. if (cid >= 0) {
  266. if (f_desired == ledcReadFreq(cid)) return cid; // no freq change
  267. ledcDetachPin(chan_pin[cid]);
  268. chan_pin[cid] = 0; // remove old freq channel
  269. }
  270. return get_pwm_channel(pin, f_desired, PWM_RESOLUTION); // try for new one
  271. }
  272. // use hardware PWM if avail, if not then ISR
  273. void analogWrite(const pin_t pin, const uint16_t value, const uint32_t freq/*=PWM_FREQUENCY*/, const uint16_t res/*=8*/) { // always 8 bit resolution!
  274. // Use ledc hardware for internal pins
  275. const int8_t cid = get_pwm_channel(pin, freq, res);
  276. if (cid >= 0) {
  277. ledcWrite(cid, value); // set duty value
  278. return;
  279. }
  280. // not a hardware PWM pin OR no PWM channels available
  281. int idx = -1;
  282. // Search Pin
  283. for (int i = 0; i < numPWMUsed; ++i)
  284. if (pwmState[i].pin == pin) { idx = i; break; }
  285. // not found ?
  286. if (idx < 0) {
  287. // No slots remaining
  288. if (numPWMUsed >= MAX_PWM_PINS) return;
  289. // Take new slot for pin
  290. idx = numPWMUsed;
  291. pwmState[idx].pin = pin;
  292. // Start timer on first use
  293. if (idx == 0) HAL_timer_start(MF_TIMER_PWM, PWM_TIMER_FREQUENCY);
  294. ++numPWMUsed;
  295. }
  296. // Use 7bit internal value - add 1 to have 100% high at 255
  297. pwmState[idx].value = (value + 1) / 2;
  298. }
  299. // Handle PWM timer interrupt
  300. HAL_PWM_TIMER_ISR() {
  301. HAL_timer_isr_prologue(MF_TIMER_PWM);
  302. static uint8_t count = 0;
  303. for (int i = 0; i < numPWMUsed; ++i) {
  304. if (count == 0) // Start of interval
  305. digitalWrite(pwmState[i].pin, pwmState[i].value ? HIGH : LOW);
  306. else if (pwmState[i].value == count) // End of duration
  307. digitalWrite(pwmState[i].pin, LOW);
  308. }
  309. // 128 for 7 Bit resolution
  310. count = (count + 1) & 0x7F;
  311. HAL_timer_isr_epilogue(MF_TIMER_PWM);
  312. }
  313. #endif // ARDUINO_ARCH_ESP32