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.h 7.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246
  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 "i2s.h"
  31. #if ENABLED(WIFISUPPORT)
  32. #include "WebSocketSerial.h"
  33. #endif
  34. #if ENABLED(ESP3D_WIFISUPPORT)
  35. #include "esp3dlib.h"
  36. #endif
  37. #include "FlushableHardwareSerial.h"
  38. // ------------------------
  39. // Defines
  40. // ------------------------
  41. #define MYSERIAL1 flushableSerial
  42. #if EITHER(WIFISUPPORT, ESP3D_WIFISUPPORT)
  43. #if ENABLED(ESP3D_WIFISUPPORT)
  44. typedef ForwardSerial1Class< decltype(Serial2Socket) > DefaultSerial1;
  45. extern DefaultSerial1 MSerial0;
  46. #define MYSERIAL2 MSerial0
  47. #else
  48. #define MYSERIAL2 webSocketSerial
  49. #endif
  50. #endif
  51. #define CRITICAL_SECTION_START() portENTER_CRITICAL(&spinlock)
  52. #define CRITICAL_SECTION_END() portEXIT_CRITICAL(&spinlock)
  53. #define HAL_CAN_SET_PWM_FREQ // This HAL supports PWM Frequency adjustment
  54. #define PWM_FREQUENCY 1000u // Default PWM frequency when set_pwm_duty() is called without set_pwm_frequency()
  55. #define PWM_RESOLUTION 10u // Default PWM bit resolution
  56. #define CHANNEL_MAX_NUM 15u // max PWM channel # to allocate (7 to only use low speed, 15 to use low & high)
  57. #define MAX_PWM_IOPIN 33u // hardware pwm pins < 34
  58. #ifndef MAX_EXPANDER_BITS
  59. #define MAX_EXPANDER_BITS 32 // I2S expander bit width (max 32)
  60. #endif
  61. // ------------------------
  62. // Types
  63. // ------------------------
  64. typedef double isr_float_t; // FPU ops are used for single-precision, so use double for ISRs.
  65. typedef int16_t pin_t;
  66. typedef struct pwm_pin {
  67. uint32_t pwm_cycle_ticks = 1000000UL / (PWM_FREQUENCY) / 4; // # ticks per pwm cycle
  68. uint32_t pwm_tick_count = 0; // current tick count
  69. uint32_t pwm_duty_ticks = 0; // # of ticks for current duty cycle
  70. } pwm_pin_t;
  71. class Servo;
  72. typedef Servo hal_servo_t;
  73. // ------------------------
  74. // Public functions
  75. // ------------------------
  76. //
  77. // Tone
  78. //
  79. void tone(const pin_t _pin, const unsigned int frequency, const unsigned long duration=0);
  80. void noTone(const pin_t _pin);
  81. int8_t get_pwm_channel(const pin_t pin, const uint32_t freq, const uint16_t res);
  82. void analogWrite(const pin_t pin, const uint16_t value, const uint32_t freq=PWM_FREQUENCY, const uint16_t res=8);
  83. //
  84. // Pin Mapping for M42, M43, M226
  85. //
  86. #define GET_PIN_MAP_PIN(index) index
  87. #define GET_PIN_MAP_INDEX(pin) pin
  88. #define PARSED_PIN_INDEX(code, dval) parser.intval(code, dval)
  89. #if ENABLED(USE_ESP32_EXIO)
  90. void Write_EXIO(uint8_t IO, uint8_t v);
  91. #endif
  92. //
  93. // Delay in cycles (used by DELAY_NS / DELAY_US)
  94. //
  95. FORCE_INLINE static void DELAY_CYCLES(uint32_t x) {
  96. unsigned long start, ccount, stop;
  97. /**
  98. * It's important to care for race conditions (and overflows) here.
  99. * Race condition example: If `stop` calculates to being close to the upper boundary of
  100. * `uint32_t` and if at the same time a longer loop interruption kicks in (e.g. due to other
  101. * FreeRTOS tasks or interrupts), `ccount` might overflow (and therefore be below `stop` again)
  102. * without the loop ever being able to notice that `ccount` had already been above `stop` once
  103. * (and that therefore the number of cycles to delay has already passed).
  104. * As DELAY_CYCLES (through DELAY_NS / DELAY_US) is used by software SPI bit banging to drive
  105. * LCDs and therefore might be called very, very often, this seemingly improbable situation did
  106. * actually happen in reality. It resulted in apparently random print pauses of ~17.9 seconds
  107. * (0x100000000 / 240 MHz) or multiples thereof, essentially ruining the current print by causing
  108. * large blobs of filament.
  109. */
  110. __asm__ __volatile__ ( "rsr %0, ccount" : "=a" (start) );
  111. stop = start + x;
  112. ccount = start;
  113. if (stop >= start) {
  114. // no overflow, so only loop while in between start and stop:
  115. // 0x00000000 -----------------start****stop-- 0xFFFFFFFF
  116. while (ccount >= start && ccount < stop) {
  117. __asm__ __volatile__ ( "rsr %0, ccount" : "=a" (ccount) );
  118. }
  119. }
  120. else {
  121. // stop did overflow, so only loop while outside of stop and start:
  122. // 0x00000000 **stop-------------------start** 0xFFFFFFFF
  123. while (ccount >= start || ccount < stop) {
  124. __asm__ __volatile__ ( "rsr %0, ccount" : "=a" (ccount) );
  125. }
  126. }
  127. }
  128. // ------------------------
  129. // Class Utilities
  130. // ------------------------
  131. #pragma GCC diagnostic push
  132. #if GCC_VERSION <= 50000
  133. #pragma GCC diagnostic ignored "-Wunused-function"
  134. #endif
  135. int freeMemory();
  136. #pragma GCC diagnostic pop
  137. void _delay_ms(const int ms);
  138. // ------------------------
  139. // MarlinHAL Class
  140. // ------------------------
  141. #define HAL_ADC_VREF 3.3
  142. #define HAL_ADC_RESOLUTION 10
  143. class MarlinHAL {
  144. public:
  145. // Earliest possible init, before setup()
  146. MarlinHAL() {}
  147. // Watchdog
  148. static void watchdog_init() IF_DISABLED(USE_WATCHDOG, {});
  149. static void watchdog_refresh() IF_DISABLED(USE_WATCHDOG, {});
  150. static void init() {} // Called early in setup()
  151. static void init_board(); // Called less early in setup()
  152. static void reboot(); // Restart the firmware
  153. // Interrupts
  154. static portMUX_TYPE spinlock;
  155. static bool isr_state() { return spinlock.owner == portMUX_FREE_VAL; }
  156. static void isr_on() { if (spinlock.owner != portMUX_FREE_VAL) portEXIT_CRITICAL(&spinlock); }
  157. static void isr_off() { portENTER_CRITICAL(&spinlock); }
  158. static void delay_ms(const int ms) { _delay_ms(ms); }
  159. // Tasks, called from idle()
  160. static void idletask();
  161. // Reset
  162. static uint8_t get_reset_source();
  163. static void clear_reset_source() {}
  164. // Free SRAM
  165. static int freeMemory();
  166. static pwm_pin_t pwm_pin_data[MAX_EXPANDER_BITS];
  167. //
  168. // ADC Methods
  169. //
  170. static uint16_t adc_result;
  171. // Called by Temperature::init once at startup
  172. static void adc_init();
  173. // Called by Temperature::init for each sensor at startup
  174. static void adc_enable(const pin_t pin) {}
  175. // Begin ADC sampling on the given pin. Called from Temperature::isr!
  176. static void adc_start(const pin_t pin);
  177. // Is the ADC ready for reading?
  178. static bool adc_ready() { return true; }
  179. // The current value of the ADC register
  180. static uint16_t adc_value() { return adc_result; }
  181. /**
  182. * If not already allocated, allocate a hardware PWM channel
  183. * to the pin and set the duty cycle..
  184. * Optionally invert the duty cycle [default = false]
  185. * Optionally change the scale of the provided value to enable finer PWM duty control [default = 255]
  186. */
  187. static void set_pwm_duty(const pin_t pin, const uint16_t v, const uint16_t v_size=255, const bool invert=false);
  188. /**
  189. * Allocate and set the frequency of a hardware PWM pin
  190. * Returns -1 if no pin available.
  191. */
  192. static int8_t set_pwm_frequency(const pin_t pin, const uint32_t f_desired);
  193. };