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

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