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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212
  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. // --------------------------------------------------------------------------
  24. // Includes
  25. // --------------------------------------------------------------------------
  26. #include "HAL.h"
  27. #include <rom/rtc.h>
  28. #include <driver/adc.h>
  29. #include <esp_adc_cal.h>
  30. #include "../../inc/MarlinConfigPre.h"
  31. #if EITHER(EEPROM_SETTINGS, WEBSUPPORT)
  32. #include "spiffs.h"
  33. #endif
  34. #if ENABLED(WIFISUPPORT)
  35. #include <ESPAsyncWebServer.h>
  36. #include "wifi.h"
  37. #if ENABLED(OTASUPPORT)
  38. #include "ota.h"
  39. #endif
  40. #if ENABLED(WEBSUPPORT)
  41. #include "web.h"
  42. #endif
  43. #endif
  44. // --------------------------------------------------------------------------
  45. // Externals
  46. // --------------------------------------------------------------------------
  47. portMUX_TYPE spinlock = portMUX_INITIALIZER_UNLOCKED;
  48. // --------------------------------------------------------------------------
  49. // Local defines
  50. // --------------------------------------------------------------------------
  51. #define V_REF 1100
  52. // --------------------------------------------------------------------------
  53. // Types
  54. // --------------------------------------------------------------------------
  55. // --------------------------------------------------------------------------
  56. // Variables
  57. // --------------------------------------------------------------------------
  58. // --------------------------------------------------------------------------
  59. // Public Variables
  60. // --------------------------------------------------------------------------
  61. uint16_t HAL_adc_result;
  62. // --------------------------------------------------------------------------
  63. // Private Variables
  64. // --------------------------------------------------------------------------
  65. esp_adc_cal_characteristics_t characteristics;
  66. // --------------------------------------------------------------------------
  67. // Function prototypes
  68. // --------------------------------------------------------------------------
  69. // --------------------------------------------------------------------------
  70. // Private functions
  71. // --------------------------------------------------------------------------
  72. // --------------------------------------------------------------------------
  73. // Public functions
  74. // --------------------------------------------------------------------------
  75. void HAL_init(void) {
  76. i2s_init();
  77. }
  78. void HAL_init_board(void) {
  79. #if EITHER(EEPROM_SETTINGS, WEBSUPPORT)
  80. spiffs_init();
  81. #endif
  82. #if ENABLED(WIFISUPPORT)
  83. wifi_init();
  84. #if ENABLED(OTASUPPORT)
  85. OTA_init();
  86. #endif
  87. #if ENABLED(WEBSUPPORT)
  88. web_init();
  89. #endif
  90. server.begin();
  91. #endif
  92. }
  93. void HAL_idletask(void) {
  94. #if ENABLED(OTASUPPORT)
  95. OTA_handle();
  96. #endif
  97. }
  98. void HAL_clear_reset_source(void) { }
  99. uint8_t HAL_get_reset_source(void) { return rtc_get_reset_reason(1); }
  100. void _delay_ms(int delay_ms) { delay(delay_ms); }
  101. // return free memory between end of heap (or end bss) and whatever is current
  102. int freeMemory() { return ESP.getFreeHeap(); }
  103. // --------------------------------------------------------------------------
  104. // ADC
  105. // --------------------------------------------------------------------------
  106. #define ADC1_CHANNEL(pin) ADC1_GPIO ## pin ## _CHANNEL
  107. adc1_channel_t get_channel(int pin) {
  108. switch (pin) {
  109. case 39: return ADC1_CHANNEL(39);
  110. case 36: return ADC1_CHANNEL(36);
  111. case 35: return ADC1_CHANNEL(35);
  112. case 34: return ADC1_CHANNEL(34);
  113. case 33: return ADC1_CHANNEL(33);
  114. case 32: return ADC1_CHANNEL(32);
  115. }
  116. return ADC1_CHANNEL_MAX;
  117. }
  118. void HAL_adc_init() {
  119. // Configure ADC
  120. adc1_config_width(ADC_WIDTH_12Bit);
  121. // Configure channels only if used as (re-)configuring a pin for ADC that is used elsewhere might have adverse effects
  122. #if HAS_TEMP_ADC_0
  123. adc1_config_channel_atten(get_channel(TEMP_0_PIN), ADC_ATTEN_11db);
  124. #endif
  125. #if HAS_TEMP_ADC_1
  126. adc1_config_channel_atten(get_channel(TEMP_1_PIN), ADC_ATTEN_11db);
  127. #endif
  128. #if HAS_TEMP_ADC_2
  129. adc1_config_channel_atten(get_channel(TEMP_2_PIN), ADC_ATTEN_11db);
  130. #endif
  131. #if HAS_TEMP_ADC_3
  132. adc1_config_channel_atten(get_channel(TEMP_3_PIN), ADC_ATTEN_11db);
  133. #endif
  134. #if HAS_TEMP_ADC_4
  135. adc1_config_channel_atten(get_channel(TEMP_4_PIN), ADC_ATTEN_11db);
  136. #endif
  137. #if HAS_TEMP_ADC_5
  138. adc1_config_channel_atten(get_channel(TEMP_5_PIN), ADC_ATTEN_11db);
  139. #endif
  140. #if HAS_HEATED_BED
  141. adc1_config_channel_atten(get_channel(TEMP_BED_PIN), ADC_ATTEN_11db);
  142. #endif
  143. #if HAS_TEMP_CHAMBER
  144. adc1_config_channel_atten(get_channel(TEMP_CHAMBER_PIN), ADC_ATTEN_11db);
  145. #endif
  146. #if ENABLED(FILAMENT_WIDTH_SENSOR)
  147. adc1_config_channel_atten(get_channel(FILWIDTH_PIN), ADC_ATTEN_11db);
  148. #endif
  149. // Note that adc2 is shared with the WiFi module, which has higher priority, so the conversion may fail.
  150. // That's why we're not setting it up here.
  151. // Calculate ADC characteristics i.e. gain and offset factors
  152. esp_adc_cal_characterize(ADC_UNIT_1, ADC_ATTEN_DB_11, ADC_WIDTH_BIT_12, V_REF, &characteristics);
  153. }
  154. void HAL_adc_start_conversion(uint8_t adc_pin) {
  155. uint32_t mv;
  156. esp_adc_cal_get_voltage((adc_channel_t)get_channel(adc_pin), &characteristics, &mv);
  157. HAL_adc_result = mv*1023.0/3300.0;
  158. }
  159. void analogWrite(int pin, int value) {
  160. if (!PWM_PIN(pin)) return;
  161. static int cnt_channel = 1,
  162. pin_to_channel[40] = {};
  163. if (pin_to_channel[pin] == 0) {
  164. ledcAttachPin(pin, cnt_channel);
  165. ledcSetup(cnt_channel, 490, 8);
  166. ledcWrite(cnt_channel, value);
  167. pin_to_channel[pin] = cnt_channel++;
  168. }
  169. ledcWrite(pin_to_channel[pin], value);
  170. }
  171. #endif // ARDUINO_ARCH_ESP32