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.

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