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 8.5KB

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