My Marlin configs for Fabrikator Mini and CTC i3 Pro B
選択できるのは25トピックまでです。 トピックは、先頭が英数字で、英数字とダッシュ('-')を使用した35文字以内のものにしてください。

HAL.h 6.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231
  1. /**
  2. * Marlin 3D Printer Firmware
  3. * Copyright (c) 2020 MarlinFirmware [https://github.com/MarlinFirmware/Marlin]
  4. * Copyright (c) 2016 Bob Cousins bobcousins42@googlemail.com
  5. *
  6. * This program is free software: you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License as published by
  8. * the Free Software Foundation, either version 3 of the License, or
  9. * (at your option) any later version.
  10. *
  11. * This program is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU General Public License
  17. * along with this program. If not, see <https://www.gnu.org/licenses/>.
  18. *
  19. */
  20. #pragma once
  21. /**
  22. * HAL for Espressif ESP32 WiFi
  23. */
  24. #define CPU_32_BIT
  25. #include <stdint.h>
  26. #include "../shared/Marduino.h"
  27. #include "../shared/math_32bit.h"
  28. #include "../shared/HAL_SPI.h"
  29. #include "fastio.h"
  30. #include "watchdog.h"
  31. #include "i2s.h"
  32. #if ENABLED(WIFISUPPORT)
  33. #include "WebSocketSerial.h"
  34. #endif
  35. #if ENABLED(ESP3D_WIFISUPPORT)
  36. #include "esp3dlib.h"
  37. #endif
  38. #include "FlushableHardwareSerial.h"
  39. // ------------------------
  40. // Defines
  41. // ------------------------
  42. #define MYSERIAL1 flushableSerial
  43. #if EITHER(WIFISUPPORT, ESP3D_WIFISUPPORT)
  44. #if ENABLED(ESP3D_WIFISUPPORT)
  45. typedef ForwardSerial1Class< decltype(Serial2Socket) > DefaultSerial1;
  46. extern DefaultSerial1 MSerial0;
  47. #define MYSERIAL2 MSerial0
  48. #else
  49. #define MYSERIAL2 webSocketSerial
  50. #endif
  51. #endif
  52. #define CRITICAL_SECTION_START() portENTER_CRITICAL(&spinlock)
  53. #define CRITICAL_SECTION_END() portEXIT_CRITICAL(&spinlock)
  54. #define HAL_CAN_SET_PWM_FREQ // This HAL supports PWM Frequency adjustment
  55. #define PWM_FREQUENCY 1000u // Default PWM frequency when set_pwm_duty() is called without set_pwm_frequency()
  56. #define PWM_RESOLUTION 10u // Default PWM bit resolution
  57. #define CHANNEL_MAX_NUM 15u // max PWM channel # to allocate (7 to only use low speed, 15 to use low & high)
  58. #define MAX_PWM_IOPIN 33u // hardware pwm pins < 34
  59. // ------------------------
  60. // Types
  61. // ------------------------
  62. typedef int16_t pin_t;
  63. class Servo;
  64. typedef Servo hal_servo_t;
  65. // ------------------------
  66. // Public functions
  67. // ------------------------
  68. //
  69. // Tone
  70. //
  71. void tone(const pin_t _pin, const unsigned int frequency, const unsigned long duration=0);
  72. void noTone(const pin_t _pin);
  73. void analogWrite(const pin_t pin, const uint16_t value, const uint32_t freq=PWM_FREQUENCY, const uint16_t res=8);
  74. //
  75. // Pin Mapping for M42, M43, M226
  76. //
  77. #define GET_PIN_MAP_PIN(index) index
  78. #define GET_PIN_MAP_INDEX(pin) pin
  79. #define PARSED_PIN_INDEX(code, dval) parser.intval(code, dval)
  80. #if ENABLED(USE_ESP32_EXIO)
  81. void Write_EXIO(uint8_t IO, uint8_t v);
  82. #endif
  83. //
  84. // Delay in cycles (used by DELAY_NS / DELAY_US)
  85. //
  86. FORCE_INLINE static void DELAY_CYCLES(uint32_t x) {
  87. unsigned long start, ccount, stop;
  88. /**
  89. * It's important to care for race conditions (and overflows) here.
  90. * Race condition example: If `stop` calculates to being close to the upper boundary of
  91. * `uint32_t` and if at the same time a longer loop interruption kicks in (e.g. due to other
  92. * FreeRTOS tasks or interrupts), `ccount` might overflow (and therefore be below `stop` again)
  93. * without the loop ever being able to notice that `ccount` had already been above `stop` once
  94. * (and that therefore the number of cycles to delay has already passed).
  95. * As DELAY_CYCLES (through DELAY_NS / DELAY_US) is used by software SPI bit banging to drive
  96. * LCDs and therefore might be called very, very often, this seemingly improbable situation did
  97. * actually happen in reality. It resulted in apparently random print pauses of ~17.9 seconds
  98. * (0x100000000 / 240 MHz) or multiples thereof, essentially ruining the current print by causing
  99. * large blobs of filament.
  100. */
  101. __asm__ __volatile__ ( "rsr %0, ccount" : "=a" (start) );
  102. stop = start + x;
  103. ccount = start;
  104. if (stop >= start) {
  105. // no overflow, so only loop while in between start and stop:
  106. // 0x00000000 -----------------start****stop-- 0xFFFFFFFF
  107. while (ccount >= start && ccount < stop) {
  108. __asm__ __volatile__ ( "rsr %0, ccount" : "=a" (ccount) );
  109. }
  110. }
  111. else {
  112. // stop did overflow, so only loop while outside of stop and start:
  113. // 0x00000000 **stop-------------------start** 0xFFFFFFFF
  114. while (ccount >= start || ccount < stop) {
  115. __asm__ __volatile__ ( "rsr %0, ccount" : "=a" (ccount) );
  116. }
  117. }
  118. }
  119. // ------------------------
  120. // Class Utilities
  121. // ------------------------
  122. #pragma GCC diagnostic push
  123. #if GCC_VERSION <= 50000
  124. #pragma GCC diagnostic ignored "-Wunused-function"
  125. #endif
  126. int freeMemory();
  127. #pragma GCC diagnostic pop
  128. void _delay_ms(const int ms);
  129. // ------------------------
  130. // MarlinHAL Class
  131. // ------------------------
  132. #define HAL_ADC_VREF 3.3
  133. #define HAL_ADC_RESOLUTION 10
  134. class MarlinHAL {
  135. public:
  136. // Earliest possible init, before setup()
  137. MarlinHAL() {}
  138. static void init() {} // Called early in setup()
  139. static void init_board(); // Called less early in setup()
  140. static void reboot(); // Restart the firmware
  141. // Interrupts
  142. static portMUX_TYPE spinlock;
  143. static bool isr_state() { return spinlock.owner == portMUX_FREE_VAL; }
  144. static void isr_on() { if (spinlock.owner != portMUX_FREE_VAL) portEXIT_CRITICAL(&spinlock); }
  145. static void isr_off() { portENTER_CRITICAL(&spinlock); }
  146. static void delay_ms(const int ms) { _delay_ms(ms); }
  147. // Tasks, called from idle()
  148. static void idletask();
  149. // Reset
  150. static uint8_t get_reset_source();
  151. static void clear_reset_source() {}
  152. // Free SRAM
  153. static int freeMemory();
  154. //
  155. // ADC Methods
  156. //
  157. static uint16_t adc_result;
  158. // Called by Temperature::init once at startup
  159. static void adc_init();
  160. // Called by Temperature::init for each sensor at startup
  161. static void adc_enable(const pin_t pin) {}
  162. // Begin ADC sampling on the given channel
  163. static void adc_start(const pin_t pin);
  164. // Is the ADC ready for reading?
  165. static bool adc_ready() { return true; }
  166. // The current value of the ADC register
  167. static uint16_t adc_value() { return adc_result; }
  168. /**
  169. * If not already allocated, allocate a hardware PWM channel
  170. * to the pin and set the duty cycle..
  171. * Optionally invert the duty cycle [default = false]
  172. * Optionally change the scale of the provided value to enable finer PWM duty control [default = 255]
  173. */
  174. static void set_pwm_duty(const pin_t pin, const uint16_t v, const uint16_t v_size=255, const bool invert=false);
  175. /**
  176. * Allocate and set the frequency of a hardware PWM pin
  177. * Returns -1 if no pin available.
  178. */
  179. static int8_t set_pwm_frequency(const pin_t pin, const uint32_t f_desired);
  180. };