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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  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 ENABLED(WIFISUPPORT)
  32. #include <ESPAsyncWebServer.h>
  33. #include "wifi.h"
  34. #if ENABLED(OTASUPPORT)
  35. #include "ota.h"
  36. #endif
  37. #if ENABLED(WEBSUPPORT)
  38. #include "web.h"
  39. #endif
  40. #endif
  41. // --------------------------------------------------------------------------
  42. // Externals
  43. // --------------------------------------------------------------------------
  44. portMUX_TYPE spinlock = portMUX_INITIALIZER_UNLOCKED;
  45. // --------------------------------------------------------------------------
  46. // Local defines
  47. // --------------------------------------------------------------------------
  48. #define V_REF 1100
  49. // --------------------------------------------------------------------------
  50. // Types
  51. // --------------------------------------------------------------------------
  52. // --------------------------------------------------------------------------
  53. // Variables
  54. // --------------------------------------------------------------------------
  55. // --------------------------------------------------------------------------
  56. // Public Variables
  57. // --------------------------------------------------------------------------
  58. uint16_t HAL_adc_result;
  59. // --------------------------------------------------------------------------
  60. // Private Variables
  61. // --------------------------------------------------------------------------
  62. esp_adc_cal_characteristics_t characteristics;
  63. // --------------------------------------------------------------------------
  64. // Function prototypes
  65. // --------------------------------------------------------------------------
  66. // --------------------------------------------------------------------------
  67. // Private functions
  68. // --------------------------------------------------------------------------
  69. // --------------------------------------------------------------------------
  70. // Public functions
  71. // --------------------------------------------------------------------------
  72. void HAL_init(void) {
  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. i2s_init();
  84. }
  85. void HAL_idletask(void) {
  86. #if ENABLED(OTASUPPORT)
  87. OTA_handle();
  88. #endif
  89. }
  90. void HAL_clear_reset_source(void) { }
  91. uint8_t HAL_get_reset_source (void) {
  92. return rtc_get_reset_reason(1);
  93. }
  94. void _delay_ms(int delay_ms) {
  95. delay(delay_ms);
  96. }
  97. // return free memory between end of heap (or end bss) and whatever is current
  98. int freeMemory() {
  99. return ESP.getFreeHeap();
  100. }
  101. // --------------------------------------------------------------------------
  102. // ADC
  103. // --------------------------------------------------------------------------
  104. #define ADC1_CHANNEL(pin) ADC1_GPIO##pin_CHANNEL
  105. adc1_channel_t get_channel(int pin) {
  106. switch (pin) {
  107. case 36: return ADC1_GPIO36_CHANNEL;
  108. case 39: return ADC1_GPIO39_CHANNEL;
  109. }
  110. return ADC1_CHANNEL_MAX;
  111. }
  112. void HAL_adc_init() {
  113. // Configure ADC
  114. adc1_config_width(ADC_WIDTH_12Bit);
  115. adc1_config_channel_atten(get_channel(36), ADC_ATTEN_11db);
  116. adc1_config_channel_atten(get_channel(39), ADC_ATTEN_11db);
  117. // Calculate ADC characteristics i.e. gain and offset factors
  118. esp_adc_cal_characterize(ADC_UNIT_1, ADC_ATTEN_DB_11, ADC_WIDTH_BIT_12, V_REF, &characteristics);
  119. }
  120. void HAL_adc_start_conversion (uint8_t adc_pin) {
  121. uint32_t mv;
  122. esp_adc_cal_get_voltage((adc_channel_t)get_channel(adc_pin), &characteristics, &mv);
  123. HAL_adc_result = mv*1023.0/3300.0;
  124. }
  125. int pin_to_channel[40] = {};
  126. int cnt_channel = 1;
  127. void analogWrite(int pin, int value) {
  128. if (pin_to_channel[pin] == 0) {
  129. ledcAttachPin(pin, cnt_channel);
  130. ledcSetup(cnt_channel, 490, 8);
  131. ledcWrite(cnt_channel, value);
  132. pin_to_channel[pin] = cnt_channel++;
  133. }
  134. ledcWrite(pin_to_channel[pin], value);
  135. }
  136. #endif // ARDUINO_ARCH_ESP32