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.4KB

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