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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303
  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 <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 <HardwareSerial.h>
  29. #include "../../inc/MarlinConfigPre.h"
  30. #if ENABLED(WIFISUPPORT)
  31. #include <ESPAsyncWebServer.h>
  32. #include "wifi.h"
  33. #if ENABLED(OTASUPPORT)
  34. #include "ota.h"
  35. #endif
  36. #if ENABLED(WEBSUPPORT)
  37. #include "spiffs.h"
  38. #include "web.h"
  39. #endif
  40. #endif
  41. // ------------------------
  42. // Externs
  43. // ------------------------
  44. portMUX_TYPE spinlock = portMUX_INITIALIZER_UNLOCKED;
  45. // ------------------------
  46. // Local defines
  47. // ------------------------
  48. #define V_REF 1100
  49. // ------------------------
  50. // Public Variables
  51. // ------------------------
  52. uint16_t HAL_adc_result;
  53. // ------------------------
  54. // Private Variables
  55. // ------------------------
  56. esp_adc_cal_characteristics_t characteristics[ADC_ATTEN_MAX];
  57. adc_atten_t attenuations[ADC1_CHANNEL_MAX] = {};
  58. uint32_t thresholds[ADC_ATTEN_MAX];
  59. volatile int numPWMUsed = 0,
  60. pwmPins[MAX_PWM_PINS],
  61. pwmValues[MAX_PWM_PINS];
  62. // ------------------------
  63. // Public functions
  64. // ------------------------
  65. #if ENABLED(WIFI_CUSTOM_COMMAND)
  66. bool wifi_custom_command(char * const command_ptr) {
  67. #if ENABLED(ESP3D_WIFISUPPORT)
  68. return esp3dlib.parse(command_ptr);
  69. #else
  70. UNUSED(command_ptr);
  71. return false;
  72. #endif
  73. }
  74. #endif
  75. void HAL_init() { i2s_init(); }
  76. void HAL_init_board() {
  77. #if ENABLED(ESP3D_WIFISUPPORT)
  78. esp3dlib.init();
  79. #elif ENABLED(WIFISUPPORT)
  80. wifi_init();
  81. #if ENABLED(OTASUPPORT)
  82. OTA_init();
  83. #endif
  84. #if ENABLED(WEBSUPPORT)
  85. spiffs_init();
  86. web_init();
  87. #endif
  88. server.begin();
  89. #endif
  90. // ESP32 uses a GPIO matrix that allows pins to be assigned to hardware serial ports.
  91. // The following code initializes hardware Serial1 and Serial2 to use user-defined pins
  92. // if they have been defined.
  93. #if defined(HARDWARE_SERIAL1_RX) && defined(HARDWARE_SERIAL1_TX)
  94. HardwareSerial Serial1(1);
  95. #ifdef TMC_BAUD_RATE // use TMC_BAUD_RATE for Serial1 if defined
  96. Serial1.begin(TMC_BAUD_RATE, SERIAL_8N1, HARDWARE_SERIAL1_RX, HARDWARE_SERIAL1_TX);
  97. #else // use default BAUDRATE if TMC_BAUD_RATE not defined
  98. Serial1.begin(BAUDRATE, SERIAL_8N1, HARDWARE_SERIAL1_RX, HARDWARE_SERIAL1_TX);
  99. #endif
  100. #endif
  101. #if defined(HARDWARE_SERIAL2_RX) && defined(HARDWARE_SERIAL2_TX)
  102. HardwareSerial Serial2(2);
  103. #ifdef TMC_BAUD_RATE // use TMC_BAUD_RATE for Serial1 if defined
  104. Serial2.begin(TMC_BAUD_RATE, SERIAL_8N1, HARDWARE_SERIAL2_RX, HARDWARE_SERIAL2_TX);
  105. #else // use default BAUDRATE if TMC_BAUD_RATE not defined
  106. Serial2.begin(BAUDRATE, SERIAL_8N1, HARDWARE_SERIAL2_RX, HARDWARE_SERIAL2_TX);
  107. #endif
  108. #endif
  109. }
  110. void HAL_idletask() {
  111. #if BOTH(WIFISUPPORT, OTASUPPORT)
  112. OTA_handle();
  113. #endif
  114. #if ENABLED(ESP3D_WIFISUPPORT)
  115. esp3dlib.idletask();
  116. #endif
  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. #if HAS_TEMP_ADC_0
  149. adc1_set_attenuation(get_channel(TEMP_0_PIN), ADC_ATTEN_11db);
  150. #endif
  151. #if HAS_TEMP_ADC_1
  152. adc1_set_attenuation(get_channel(TEMP_1_PIN), ADC_ATTEN_11db);
  153. #endif
  154. #if HAS_TEMP_ADC_2
  155. adc1_set_attenuation(get_channel(TEMP_2_PIN), ADC_ATTEN_11db);
  156. #endif
  157. #if HAS_TEMP_ADC_3
  158. adc1_set_attenuation(get_channel(TEMP_3_PIN), ADC_ATTEN_11db);
  159. #endif
  160. #if HAS_TEMP_ADC_4
  161. adc1_set_attenuation(get_channel(TEMP_4_PIN), ADC_ATTEN_11db);
  162. #endif
  163. #if HAS_TEMP_ADC_5
  164. adc1_set_attenuation(get_channel(TEMP_5_PIN), ADC_ATTEN_11db);
  165. #endif
  166. #if HAS_TEMP_ADC_6
  167. adc2_set_attenuation(get_channel(TEMP_6_PIN), ADC_ATTEN_11db);
  168. #endif
  169. #if HAS_TEMP_ADC_7
  170. adc3_set_attenuation(get_channel(TEMP_7_PIN), ADC_ATTEN_11db);
  171. #endif
  172. #if HAS_HEATED_BED
  173. adc1_set_attenuation(get_channel(TEMP_BED_PIN), ADC_ATTEN_11db);
  174. #endif
  175. #if HAS_TEMP_CHAMBER
  176. adc1_set_attenuation(get_channel(TEMP_CHAMBER_PIN), ADC_ATTEN_11db);
  177. #endif
  178. #if ENABLED(FILAMENT_WIDTH_SENSOR)
  179. adc1_set_attenuation(get_channel(FILWIDTH_PIN), ADC_ATTEN_11db);
  180. #endif
  181. // Note that adc2 is shared with the WiFi module, which has higher priority, so the conversion may fail.
  182. // That's why we're not setting it up here.
  183. // Calculate ADC characteristics (i.e., gain and offset factors for each attenuation level)
  184. for (int i = 0; i < ADC_ATTEN_MAX; i++) {
  185. esp_adc_cal_characterize(ADC_UNIT_1, (adc_atten_t)i, ADC_WIDTH_BIT_12, V_REF, &characteristics[i]);
  186. // Change attenuation 100mV below the calibrated threshold
  187. thresholds[i] = esp_adc_cal_raw_to_voltage(4095, &characteristics[i]);
  188. }
  189. }
  190. void HAL_adc_start_conversion(const uint8_t adc_pin) {
  191. const adc1_channel_t chan = get_channel(adc_pin);
  192. uint32_t mv;
  193. esp_adc_cal_get_voltage((adc_channel_t)chan, &characteristics[attenuations[chan]], &mv);
  194. HAL_adc_result = mv * 1023.0 / 3300.0;
  195. // Change the attenuation level based on the new reading
  196. adc_atten_t atten;
  197. if (mv < thresholds[ADC_ATTEN_DB_0] - 100)
  198. atten = ADC_ATTEN_DB_0;
  199. else if (mv > thresholds[ADC_ATTEN_DB_0] - 50 && mv < thresholds[ADC_ATTEN_DB_2_5] - 100)
  200. atten = ADC_ATTEN_DB_2_5;
  201. else if (mv > thresholds[ADC_ATTEN_DB_2_5] - 50 && mv < thresholds[ADC_ATTEN_DB_6] - 100)
  202. atten = ADC_ATTEN_DB_6;
  203. else if (mv > thresholds[ADC_ATTEN_DB_6] - 50)
  204. atten = ADC_ATTEN_DB_11;
  205. else return;
  206. adc1_set_attenuation(chan, atten);
  207. }
  208. void analogWrite(pin_t pin, int value) {
  209. // Use ledc hardware for internal pins
  210. if (pin < 34) {
  211. static int cnt_channel = 1, pin_to_channel[40] = { 0 };
  212. if (pin_to_channel[pin] == 0) {
  213. ledcAttachPin(pin, cnt_channel);
  214. ledcSetup(cnt_channel, 490, 8);
  215. ledcWrite(cnt_channel, value);
  216. pin_to_channel[pin] = cnt_channel++;
  217. }
  218. ledcWrite(pin_to_channel[pin], value);
  219. return;
  220. }
  221. int idx = -1;
  222. // Search Pin
  223. for (int i = 0; i < numPWMUsed; ++i)
  224. if (pwmPins[i] == pin) { idx = i; break; }
  225. // not found ?
  226. if (idx < 0) {
  227. // No slots remaining
  228. if (numPWMUsed >= MAX_PWM_PINS) return;
  229. // Take new slot for pin
  230. idx = numPWMUsed;
  231. pwmPins[idx] = pin;
  232. // Start timer on first use
  233. if (idx == 0) HAL_timer_start(PWM_TIMER_NUM, PWM_TIMER_FREQUENCY);
  234. ++numPWMUsed;
  235. }
  236. // Use 7bit internal value - add 1 to have 100% high at 255
  237. pwmValues[idx] = (value + 1) / 2;
  238. }
  239. // Handle PWM timer interrupt
  240. HAL_PWM_TIMER_ISR() {
  241. HAL_timer_isr_prologue(PWM_TIMER_NUM);
  242. static uint8_t count = 0;
  243. for (int i = 0; i < numPWMUsed; ++i) {
  244. if (count == 0) // Start of interval
  245. WRITE(pwmPins[i], pwmValues[i] ? HIGH : LOW);
  246. else if (pwmValues[i] == count) // End of duration
  247. WRITE(pwmPins[i], LOW);
  248. }
  249. // 128 for 7 Bit resolution
  250. count = (count + 1) & 0x7F;
  251. HAL_timer_isr_epilogue(PWM_TIMER_NUM);
  252. }
  253. #endif // ARDUINO_ARCH_ESP32