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.

HAL.cpp 14KB

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