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

HAL.h 7.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281
  1. /**
  2. * Marlin 3D Printer Firmware
  3. *
  4. * Copyright (c) 2020 MarlinFirmware [https://github.com/MarlinFirmware/Marlin]
  5. * Copyright (c) 2016 Bob Cousins bobcousins42@googlemail.com
  6. * Copyright (c) 2015-2016 Nico Tonnhofer wurstnase.reprap@gmail.com
  7. * Copyright (c) 2017 Victor Perez
  8. *
  9. * This program is free software: you can redistribute it and/or modify
  10. * it under the terms of the GNU General Public License as published by
  11. * the Free Software Foundation, either version 3 of the License, or
  12. * (at your option) any later version.
  13. *
  14. * This program is distributed in the hope that it will be useful,
  15. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  16. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  17. * GNU General Public License for more details.
  18. *
  19. * You should have received a copy of the GNU General Public License
  20. * along with this program. If not, see <https://www.gnu.org/licenses/>.
  21. *
  22. */
  23. #pragma once
  24. #define CPU_32_BIT
  25. #include "../../core/macros.h"
  26. #include "../shared/Marduino.h"
  27. #include "../shared/math_32bit.h"
  28. #include "../shared/HAL_SPI.h"
  29. #include "fastio.h"
  30. #include "Servo.h"
  31. #include "MarlinSerial.h"
  32. #include "../../inc/MarlinConfigPre.h"
  33. #include <stdint.h>
  34. //
  35. // Default graphical display delays
  36. //
  37. #define CPU_ST7920_DELAY_1 300
  38. #define CPU_ST7920_DELAY_2 40
  39. #define CPU_ST7920_DELAY_3 340
  40. // ------------------------
  41. // Serial ports
  42. // ------------------------
  43. #ifdef USBCON
  44. #include <USBSerial.h>
  45. #include "../../core/serial_hook.h"
  46. typedef ForwardSerial1Class< decltype(SerialUSB) > DefaultSerial1;
  47. extern DefaultSerial1 MSerialUSB;
  48. #endif
  49. #define _MSERIAL(X) MSerial##X
  50. #define MSERIAL(X) _MSERIAL(X)
  51. #if WITHIN(SERIAL_PORT, 1, 6)
  52. #define MYSERIAL1 MSERIAL(SERIAL_PORT)
  53. #elif !defined(USBCON)
  54. #error "SERIAL_PORT must be from 1 to 6."
  55. #elif SERIAL_PORT == -1
  56. #define MYSERIAL1 MSerialUSB
  57. #else
  58. #error "SERIAL_PORT must be from 1 to 6, or -1 for Native USB."
  59. #endif
  60. #ifdef SERIAL_PORT_2
  61. #if WITHIN(SERIAL_PORT_2, 1, 6)
  62. #define MYSERIAL2 MSERIAL(SERIAL_PORT_2)
  63. #elif !defined(USBCON)
  64. #error "SERIAL_PORT must be from 1 to 6."
  65. #elif SERIAL_PORT_2 == -1
  66. #define MYSERIAL2 MSerialUSB
  67. #else
  68. #error "SERIAL_PORT_2 must be from 1 to 6, or -1 for Native USB."
  69. #endif
  70. #endif
  71. #ifdef SERIAL_PORT_3
  72. #if WITHIN(SERIAL_PORT_3, 1, 6)
  73. #define MYSERIAL3 MSERIAL(SERIAL_PORT_3)
  74. #elif !defined(USBCON)
  75. #error "SERIAL_PORT must be from 1 to 6."
  76. #elif SERIAL_PORT_3 == -1
  77. #define MYSERIAL3 MSerialUSB
  78. #else
  79. #error "SERIAL_PORT_3 must be from 1 to 6, or -1 for Native USB."
  80. #endif
  81. #endif
  82. #ifdef MMU2_SERIAL_PORT
  83. #if WITHIN(MMU2_SERIAL_PORT, 1, 6)
  84. #define MMU2_SERIAL MSERIAL(MMU2_SERIAL_PORT)
  85. #elif !defined(USBCON)
  86. #error "SERIAL_PORT must be from 1 to 6."
  87. #elif MMU2_SERIAL_PORT == -1
  88. #define MMU2_SERIAL MSerialUSB
  89. #else
  90. #error "MMU2_SERIAL_PORT must be from 1 to 6, or -1 for Native USB."
  91. #endif
  92. #endif
  93. #ifdef LCD_SERIAL_PORT
  94. #if WITHIN(LCD_SERIAL_PORT, 1, 6)
  95. #define LCD_SERIAL MSERIAL(LCD_SERIAL_PORT)
  96. #elif !defined(USBCON)
  97. #error "SERIAL_PORT must be from 1 to 6."
  98. #elif LCD_SERIAL_PORT == -1
  99. #define LCD_SERIAL MSerialUSB
  100. #else
  101. #error "LCD_SERIAL_PORT must be from 1 to 6, or -1 for Native USB."
  102. #endif
  103. #if HAS_DGUS_LCD
  104. #define SERIAL_GET_TX_BUFFER_FREE() LCD_SERIAL.availableForWrite()
  105. #endif
  106. #endif
  107. /**
  108. * TODO: review this to return 1 for pins that are not analog input
  109. */
  110. #ifndef analogInputToDigitalPin
  111. #define analogInputToDigitalPin(p) (p)
  112. #endif
  113. //
  114. // Interrupts
  115. //
  116. #define CRITICAL_SECTION_START() const bool irqon = !__get_PRIMASK(); __disable_irq()
  117. #define CRITICAL_SECTION_END() if (irqon) __enable_irq()
  118. #define cli() __disable_irq()
  119. #define sei() __enable_irq()
  120. // ------------------------
  121. // Types
  122. // ------------------------
  123. typedef double isr_float_t; // FPU ops are used for single-precision, so use double for ISRs.
  124. #ifdef STM32G0B1xx
  125. typedef int32_t pin_t;
  126. #else
  127. typedef int16_t pin_t;
  128. #endif
  129. class libServo;
  130. typedef libServo hal_servo_t;
  131. #define PAUSE_SERVO_OUTPUT() libServo::pause_all_servos()
  132. #define RESUME_SERVO_OUTPUT() libServo::resume_all_servos()
  133. // ------------------------
  134. // ADC
  135. // ------------------------
  136. #ifdef ADC_RESOLUTION
  137. #define HAL_ADC_RESOLUTION ADC_RESOLUTION
  138. #else
  139. #define HAL_ADC_RESOLUTION 12
  140. #endif
  141. #define HAL_ADC_VREF 3.3
  142. //
  143. // Pin Mapping for M42, M43, M226
  144. //
  145. #define GET_PIN_MAP_PIN(index) index
  146. #define GET_PIN_MAP_INDEX(pin) pin
  147. #define PARSED_PIN_INDEX(code, dval) parser.intval(code, dval)
  148. #ifdef STM32F1xx
  149. #define JTAG_DISABLE() AFIO_DBGAFR_CONFIG(AFIO_MAPR_SWJ_CFG_JTAGDISABLE)
  150. #define JTAGSWD_DISABLE() AFIO_DBGAFR_CONFIG(AFIO_MAPR_SWJ_CFG_DISABLE)
  151. #define JTAGSWD_RESET() AFIO_DBGAFR_CONFIG(AFIO_MAPR_SWJ_CFG_RESET); // Reset: FULL SWD+JTAG
  152. #endif
  153. #define PLATFORM_M997_SUPPORT
  154. void flashFirmware(const int16_t);
  155. // Maple Compatibility
  156. typedef void (*systickCallback_t)(void);
  157. void systick_attach_callback(systickCallback_t cb);
  158. void HAL_SYSTICK_Callback();
  159. extern volatile uint32_t systick_uptime_millis;
  160. #define HAL_CAN_SET_PWM_FREQ // This HAL supports PWM Frequency adjustment
  161. // ------------------------
  162. // Class Utilities
  163. // ------------------------
  164. // Memory related
  165. #define __bss_end __bss_end__
  166. extern "C" char* _sbrk(int incr);
  167. #pragma GCC diagnostic push
  168. #if GCC_VERSION <= 50000
  169. #pragma GCC diagnostic ignored "-Wunused-function"
  170. #endif
  171. static inline int freeMemory() {
  172. volatile char top;
  173. return &top - reinterpret_cast<char*>(_sbrk(0));
  174. }
  175. #pragma GCC diagnostic pop
  176. // ------------------------
  177. // MarlinHAL Class
  178. // ------------------------
  179. class MarlinHAL {
  180. public:
  181. // Earliest possible init, before setup()
  182. MarlinHAL() {}
  183. // Watchdog
  184. static void watchdog_init() IF_DISABLED(USE_WATCHDOG, {});
  185. static void watchdog_refresh() IF_DISABLED(USE_WATCHDOG, {});
  186. static void init(); // Called early in setup()
  187. static void init_board() {} // Called less early in setup()
  188. static void reboot(); // Restart the firmware from 0x0
  189. // Interrupts
  190. static bool isr_state() { return !__get_PRIMASK(); }
  191. static void isr_on() { sei(); }
  192. static void isr_off() { cli(); }
  193. static void delay_ms(const int ms) { delay(ms); }
  194. // Tasks, called from idle()
  195. static void idletask();
  196. // Reset
  197. static uint8_t get_reset_source();
  198. static void clear_reset_source();
  199. // Free SRAM
  200. static int freeMemory() { return ::freeMemory(); }
  201. //
  202. // ADC Methods
  203. //
  204. static uint16_t adc_result;
  205. // Called by Temperature::init once at startup
  206. static void adc_init() {
  207. analogReadResolution(HAL_ADC_RESOLUTION);
  208. }
  209. // Called by Temperature::init for each sensor at startup
  210. static void adc_enable(const pin_t pin) { pinMode(pin, INPUT); }
  211. // Begin ADC sampling on the given pin. Called from Temperature::isr!
  212. static void adc_start(const pin_t pin) { adc_result = analogRead(pin); }
  213. // Is the ADC ready for reading?
  214. static bool adc_ready() { return true; }
  215. // The current value of the ADC register
  216. static uint16_t adc_value() { return adc_result; }
  217. /**
  218. * Set the PWM duty cycle for the pin to the given value.
  219. * Optionally invert the duty cycle [default = false]
  220. * Optionally change the maximum size of the provided value to enable finer PWM duty control [default = 255]
  221. */
  222. static void set_pwm_duty(const pin_t pin, const uint16_t v, const uint16_t v_size=255, const bool invert=false);
  223. /**
  224. * Set the frequency of the timer for the given pin.
  225. * All Timer PWM pins run at the same frequency.
  226. */
  227. static void set_pwm_frequency(const pin_t pin, const uint16_t f_desired);
  228. };