My Marlin configs for Fabrikator Mini and CTC i3 Pro B
Você não pode selecionar mais de 25 tópicos Os tópicos devem começar com uma letra ou um número, podem incluir traços ('-') e podem ter até 35 caracteres.

HAL.cpp 8.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281
  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(WIFISUPPORT)
  29. #include <ESPAsyncWebServer.h>
  30. #include "wifi.h"
  31. #if ENABLED(OTASUPPORT)
  32. #include "ota.h"
  33. #endif
  34. #if ENABLED(WEBSUPPORT)
  35. #include "spiffs.h"
  36. #include "web.h"
  37. #endif
  38. #endif
  39. #if ENABLED(ESP3D_WIFISUPPORT)
  40. DefaultSerial1 MSerial0(false, Serial2Socket);
  41. #endif
  42. // ------------------------
  43. // Externs
  44. // ------------------------
  45. portMUX_TYPE spinlock = portMUX_INITIALIZER_UNLOCKED;
  46. // ------------------------
  47. // Local defines
  48. // ------------------------
  49. #define V_REF 1100
  50. // ------------------------
  51. // Public Variables
  52. // ------------------------
  53. uint16_t HAL_adc_result;
  54. // ------------------------
  55. // Private Variables
  56. // ------------------------
  57. esp_adc_cal_characteristics_t characteristics[ADC_ATTEN_MAX];
  58. adc_atten_t attenuations[ADC1_CHANNEL_MAX] = {};
  59. uint32_t thresholds[ADC_ATTEN_MAX];
  60. volatile int numPWMUsed = 0,
  61. pwmPins[MAX_PWM_PINS],
  62. pwmValues[MAX_PWM_PINS];
  63. // ------------------------
  64. // Public functions
  65. // ------------------------
  66. #if ENABLED(WIFI_CUSTOM_COMMAND)
  67. bool wifi_custom_command(char * const command_ptr) {
  68. #if ENABLED(ESP3D_WIFISUPPORT)
  69. return esp3dlib.parse(command_ptr);
  70. #else
  71. UNUSED(command_ptr);
  72. return false;
  73. #endif
  74. }
  75. #endif
  76. void HAL_init_board() {
  77. #if ENABLED(ESP3D_WIFISUPPORT)
  78. esp3dlib.init();
  79. #elif ENABLED(WIFISUPPORT)
  80. wifi_init();
  81. TERN_(OTASUPPORT, OTA_init());
  82. #if ENABLED(WEBSUPPORT)
  83. spiffs_init();
  84. web_init();
  85. #endif
  86. server.begin();
  87. #endif
  88. // ESP32 uses a GPIO matrix that allows pins to be assigned to hardware serial ports.
  89. // The following code initializes hardware Serial1 and Serial2 to use user-defined pins
  90. // if they have been defined.
  91. #if defined(HARDWARE_SERIAL1_RX) && defined(HARDWARE_SERIAL1_TX)
  92. HardwareSerial Serial1(1);
  93. #ifdef TMC_BAUD_RATE // use TMC_BAUD_RATE for Serial1 if defined
  94. Serial1.begin(TMC_BAUD_RATE, SERIAL_8N1, HARDWARE_SERIAL1_RX, HARDWARE_SERIAL1_TX);
  95. #else // use default BAUDRATE if TMC_BAUD_RATE not defined
  96. Serial1.begin(BAUDRATE, SERIAL_8N1, HARDWARE_SERIAL1_RX, HARDWARE_SERIAL1_TX);
  97. #endif
  98. #endif
  99. #if defined(HARDWARE_SERIAL2_RX) && defined(HARDWARE_SERIAL2_TX)
  100. HardwareSerial Serial2(2);
  101. #ifdef TMC_BAUD_RATE // use TMC_BAUD_RATE for Serial1 if defined
  102. Serial2.begin(TMC_BAUD_RATE, SERIAL_8N1, HARDWARE_SERIAL2_RX, HARDWARE_SERIAL2_TX);
  103. #else // use default BAUDRATE if TMC_BAUD_RATE not defined
  104. Serial2.begin(BAUDRATE, SERIAL_8N1, HARDWARE_SERIAL2_RX, HARDWARE_SERIAL2_TX);
  105. #endif
  106. #endif
  107. // Initialize the i2s peripheral only if the I2S stepper stream is enabled.
  108. // The following initialization is performed after Serial1 and Serial2 are defined as
  109. // their native pins might conflict with the i2s stream even when they are remapped.
  110. TERN_(I2S_STEPPER_STREAM, i2s_init());
  111. }
  112. void HAL_idletask() {
  113. #if BOTH(WIFISUPPORT, OTASUPPORT)
  114. OTA_handle();
  115. #endif
  116. TERN_(ESP3D_WIFISUPPORT, esp3dlib.idletask());
  117. }
  118. void HAL_clear_reset_source() { }
  119. uint8_t HAL_get_reset_source() { return rtc_get_reset_reason(1); }
  120. void _delay_ms(int delay_ms) { delay(delay_ms); }
  121. // return free memory between end of heap (or end bss) and whatever is current
  122. int freeMemory() { return ESP.getFreeHeap(); }
  123. // ------------------------
  124. // ADC
  125. // ------------------------
  126. #define ADC1_CHANNEL(pin) ADC1_GPIO ## pin ## _CHANNEL
  127. adc1_channel_t get_channel(int pin) {
  128. switch (pin) {
  129. case 39: return ADC1_CHANNEL(39);
  130. case 36: return ADC1_CHANNEL(36);
  131. case 35: return ADC1_CHANNEL(35);
  132. case 34: return ADC1_CHANNEL(34);
  133. case 33: return ADC1_CHANNEL(33);
  134. case 32: return ADC1_CHANNEL(32);
  135. }
  136. return ADC1_CHANNEL_MAX;
  137. }
  138. void adc1_set_attenuation(adc1_channel_t chan, adc_atten_t atten) {
  139. if (attenuations[chan] != atten) {
  140. adc1_config_channel_atten(chan, atten);
  141. attenuations[chan] = atten;
  142. }
  143. }
  144. void HAL_adc_init() {
  145. // Configure ADC
  146. adc1_config_width(ADC_WIDTH_12Bit);
  147. // Configure channels only if used as (re-)configuring a pin for ADC that is used elsewhere might have adverse effects
  148. TERN_(HAS_TEMP_ADC_0, adc1_set_attenuation(get_channel(TEMP_0_PIN), ADC_ATTEN_11db));
  149. TERN_(HAS_TEMP_ADC_1, adc1_set_attenuation(get_channel(TEMP_1_PIN), ADC_ATTEN_11db));
  150. TERN_(HAS_TEMP_ADC_2, adc1_set_attenuation(get_channel(TEMP_2_PIN), ADC_ATTEN_11db));
  151. TERN_(HAS_TEMP_ADC_3, adc1_set_attenuation(get_channel(TEMP_3_PIN), ADC_ATTEN_11db));
  152. TERN_(HAS_TEMP_ADC_4, adc1_set_attenuation(get_channel(TEMP_4_PIN), ADC_ATTEN_11db));
  153. TERN_(HAS_TEMP_ADC_5, adc1_set_attenuation(get_channel(TEMP_5_PIN), ADC_ATTEN_11db));
  154. TERN_(HAS_TEMP_ADC_6, adc2_set_attenuation(get_channel(TEMP_6_PIN), ADC_ATTEN_11db));
  155. TERN_(HAS_TEMP_ADC_7, adc3_set_attenuation(get_channel(TEMP_7_PIN), ADC_ATTEN_11db));
  156. TERN_(HAS_HEATED_BED, adc1_set_attenuation(get_channel(TEMP_BED_PIN), ADC_ATTEN_11db));
  157. TERN_(HAS_TEMP_CHAMBER, adc1_set_attenuation(get_channel(TEMP_CHAMBER_PIN), ADC_ATTEN_11db));
  158. TERN_(HAS_TEMP_COOLER, adc1_set_attenuation(get_channel(TEMP_COOLER_PIN), ADC_ATTEN_11db));
  159. TERN_(FILAMENT_WIDTH_SENSOR, adc1_set_attenuation(get_channel(FILWIDTH_PIN), ADC_ATTEN_11db));
  160. // Note that adc2 is shared with the WiFi module, which has higher priority, so the conversion may fail.
  161. // That's why we're not setting it up here.
  162. // Calculate ADC characteristics (i.e., gain and offset factors for each attenuation level)
  163. for (int i = 0; i < ADC_ATTEN_MAX; i++) {
  164. esp_adc_cal_characterize(ADC_UNIT_1, (adc_atten_t)i, ADC_WIDTH_BIT_12, V_REF, &characteristics[i]);
  165. // Change attenuation 100mV below the calibrated threshold
  166. thresholds[i] = esp_adc_cal_raw_to_voltage(4095, &characteristics[i]);
  167. }
  168. }
  169. void HAL_adc_start_conversion(const uint8_t adc_pin) {
  170. const adc1_channel_t chan = get_channel(adc_pin);
  171. uint32_t mv;
  172. esp_adc_cal_get_voltage((adc_channel_t)chan, &characteristics[attenuations[chan]], &mv);
  173. HAL_adc_result = mv * 1023.0 / 3300.0;
  174. // Change the attenuation level based on the new reading
  175. adc_atten_t atten;
  176. if (mv < thresholds[ADC_ATTEN_DB_0] - 100)
  177. atten = ADC_ATTEN_DB_0;
  178. else if (mv > thresholds[ADC_ATTEN_DB_0] - 50 && mv < thresholds[ADC_ATTEN_DB_2_5] - 100)
  179. atten = ADC_ATTEN_DB_2_5;
  180. else if (mv > thresholds[ADC_ATTEN_DB_2_5] - 50 && mv < thresholds[ADC_ATTEN_DB_6] - 100)
  181. atten = ADC_ATTEN_DB_6;
  182. else if (mv > thresholds[ADC_ATTEN_DB_6] - 50)
  183. atten = ADC_ATTEN_DB_11;
  184. else return;
  185. adc1_set_attenuation(chan, atten);
  186. }
  187. void analogWrite(pin_t pin, int value) {
  188. // Use ledc hardware for internal pins
  189. if (pin < 34) {
  190. static int cnt_channel = 1, pin_to_channel[40] = { 0 };
  191. if (pin_to_channel[pin] == 0) {
  192. ledcAttachPin(pin, cnt_channel);
  193. ledcSetup(cnt_channel, 490, 8);
  194. ledcWrite(cnt_channel, value);
  195. pin_to_channel[pin] = cnt_channel++;
  196. }
  197. ledcWrite(pin_to_channel[pin], value);
  198. return;
  199. }
  200. int idx = -1;
  201. // Search Pin
  202. for (int i = 0; i < numPWMUsed; ++i)
  203. if (pwmPins[i] == pin) { idx = i; break; }
  204. // not found ?
  205. if (idx < 0) {
  206. // No slots remaining
  207. if (numPWMUsed >= MAX_PWM_PINS) return;
  208. // Take new slot for pin
  209. idx = numPWMUsed;
  210. pwmPins[idx] = pin;
  211. // Start timer on first use
  212. if (idx == 0) HAL_timer_start(PWM_TIMER_NUM, PWM_TIMER_FREQUENCY);
  213. ++numPWMUsed;
  214. }
  215. // Use 7bit internal value - add 1 to have 100% high at 255
  216. pwmValues[idx] = (value + 1) / 2;
  217. }
  218. // Handle PWM timer interrupt
  219. HAL_PWM_TIMER_ISR() {
  220. HAL_timer_isr_prologue(PWM_TIMER_NUM);
  221. static uint8_t count = 0;
  222. for (int i = 0; i < numPWMUsed; ++i) {
  223. if (count == 0) // Start of interval
  224. WRITE(pwmPins[i], pwmValues[i] ? HIGH : LOW);
  225. else if (pwmValues[i] == count) // End of duration
  226. WRITE(pwmPins[i], LOW);
  227. }
  228. // 128 for 7 Bit resolution
  229. count = (count + 1) & 0x7F;
  230. HAL_timer_isr_epilogue(PWM_TIMER_NUM);
  231. }
  232. #endif // ARDUINO_ARCH_ESP32