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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190
  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. extern portMUX_TYPE spinlock;
  43. #define MYSERIAL1 flushableSerial
  44. #if EITHER(WIFISUPPORT, ESP3D_WIFISUPPORT)
  45. #if ENABLED(ESP3D_WIFISUPPORT)
  46. typedef ForwardSerial1Class< decltype(Serial2Socket) > DefaultSerial1;
  47. extern DefaultSerial1 MSerial0;
  48. #define MYSERIAL2 MSerial0
  49. #else
  50. #define MYSERIAL2 webSocketSerial
  51. #endif
  52. #endif
  53. #define CRITICAL_SECTION_START() portENTER_CRITICAL(&spinlock)
  54. #define CRITICAL_SECTION_END() portEXIT_CRITICAL(&spinlock)
  55. #define ISRS_ENABLED() (spinlock.owner == portMUX_FREE_VAL)
  56. #define ENABLE_ISRS() if (spinlock.owner != portMUX_FREE_VAL) portEXIT_CRITICAL(&spinlock)
  57. #define DISABLE_ISRS() portENTER_CRITICAL(&spinlock)
  58. // ------------------------
  59. // Types
  60. // ------------------------
  61. typedef int16_t pin_t;
  62. #define HAL_SERVO_LIB Servo
  63. // ------------------------
  64. // Public Variables
  65. // ------------------------
  66. /** result of last ADC conversion */
  67. extern uint16_t HAL_adc_result;
  68. // ------------------------
  69. // Public functions
  70. // ------------------------
  71. //
  72. // Tone
  73. //
  74. void toneInit();
  75. void tone(const pin_t _pin, const unsigned int frequency, const unsigned long duration=0);
  76. void noTone(const pin_t _pin);
  77. // clear reset reason
  78. void HAL_clear_reset_source();
  79. // reset reason
  80. uint8_t HAL_get_reset_source();
  81. void HAL_reboot();
  82. void _delay_ms(int delay);
  83. #pragma GCC diagnostic push
  84. #if GCC_VERSION <= 50000
  85. #pragma GCC diagnostic ignored "-Wunused-function"
  86. #endif
  87. int freeMemory();
  88. #pragma GCC diagnostic pop
  89. void analogWrite(pin_t pin, int value);
  90. // ADC
  91. #define HAL_ANALOG_SELECT(pin)
  92. void HAL_adc_init();
  93. #define HAL_ADC_VREF 3.3
  94. #define HAL_ADC_RESOLUTION 10
  95. #define HAL_START_ADC(pin) HAL_adc_start_conversion(pin)
  96. #define HAL_READ_ADC() HAL_adc_result
  97. #define HAL_ADC_READY() true
  98. void HAL_adc_start_conversion(const uint8_t adc_pin);
  99. // PWM
  100. inline void set_pwm_duty(const pin_t pin, const uint16_t v, const uint16_t=255, const bool=false) { analogWrite(pin, v); }
  101. // Pin Map
  102. #define GET_PIN_MAP_PIN(index) index
  103. #define GET_PIN_MAP_INDEX(pin) pin
  104. #define PARSED_PIN_INDEX(code, dval) parser.intval(code, dval)
  105. // Enable hooks into idle and setup for HAL
  106. #define HAL_IDLETASK 1
  107. #define BOARD_INIT() HAL_init_board();
  108. void HAL_idletask();
  109. inline void HAL_init() {}
  110. void HAL_init_board();
  111. #if ENABLED(USE_ESP32_EXIO)
  112. void Write_EXIO(uint8_t IO, uint8_t v);
  113. #endif
  114. //
  115. // Delay in cycles (used by DELAY_NS / DELAY_US)
  116. //
  117. FORCE_INLINE static void DELAY_CYCLES(uint32_t x) {
  118. unsigned long start, ccount, stop;
  119. /**
  120. * It's important to care for race conditions (and overflows) here.
  121. * Race condition example: If `stop` calculates to being close to the upper boundary of
  122. * `uint32_t` and if at the same time a longer loop interruption kicks in (e.g. due to other
  123. * FreeRTOS tasks or interrupts), `ccount` might overflow (and therefore be below `stop` again)
  124. * without the loop ever being able to notice that `ccount` had already been above `stop` once
  125. * (and that therefore the number of cycles to delay has already passed).
  126. * As DELAY_CYCLES (through DELAY_NS / DELAY_US) is used by software SPI bit banging to drive
  127. * LCDs and therefore might be called very, very often, this seemingly improbable situation did
  128. * actually happen in reality. It resulted in apparently random print pauses of ~17.9 seconds
  129. * (0x100000000 / 240 MHz) or multiples thereof, essentially ruining the current print by causing
  130. * large blobs of filament.
  131. */
  132. __asm__ __volatile__ ( "rsr %0, ccount" : "=a" (start) );
  133. stop = start + x;
  134. ccount = start;
  135. if (stop >= start) {
  136. // no overflow, so only loop while in between start and stop:
  137. // 0x00000000 -----------------start****stop-- 0xFFFFFFFF
  138. while (ccount >= start && ccount < stop) {
  139. __asm__ __volatile__ ( "rsr %0, ccount" : "=a" (ccount) );
  140. }
  141. }
  142. else {
  143. // stop did overflow, so only loop while outside of stop and start:
  144. // 0x00000000 **stop-------------------start** 0xFFFFFFFF
  145. while (ccount >= start || ccount < stop) {
  146. __asm__ __volatile__ ( "rsr %0, ccount" : "=a" (ccount) );
  147. }
  148. }
  149. }