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

HAL.h 7.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277
  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 Arduino AVR
  23. */
  24. #include "../shared/Marduino.h"
  25. #include "../shared/HAL_SPI.h"
  26. #include "fastio.h"
  27. #include "math.h"
  28. #ifdef USBCON
  29. #include <HardwareSerial.h>
  30. #else
  31. #include "MarlinSerial.h"
  32. #endif
  33. #include <stdint.h>
  34. #include <util/delay.h>
  35. #include <avr/eeprom.h>
  36. #include <avr/pgmspace.h>
  37. #include <avr/interrupt.h>
  38. #include <avr/io.h>
  39. //
  40. // Default graphical display delays
  41. //
  42. #if F_CPU >= 20000000
  43. #define CPU_ST7920_DELAY_1 150
  44. #define CPU_ST7920_DELAY_2 0
  45. #define CPU_ST7920_DELAY_3 150
  46. #elif F_CPU == 16000000
  47. #define CPU_ST7920_DELAY_1 125
  48. #define CPU_ST7920_DELAY_2 0
  49. #define CPU_ST7920_DELAY_3 188
  50. #endif
  51. #ifndef pgm_read_ptr
  52. // Compatibility for avr-libc 1.8.0-4.1 included with Ubuntu for
  53. // Windows Subsystem for Linux on Windows 10 as of 10/18/2019
  54. #define pgm_read_ptr_far(address_long) (void*)__ELPM_word((uint32_t)(address_long))
  55. #define pgm_read_ptr_near(address_short) (void*)__LPM_word((uint16_t)(address_short))
  56. #define pgm_read_ptr(address_short) pgm_read_ptr_near(address_short)
  57. #endif
  58. // ------------------------
  59. // Defines
  60. // ------------------------
  61. // AVR PROGMEM extension for sprintf_P
  62. #define S_FMT "%S"
  63. // AVR PROGMEM extension for string define
  64. #define PGMSTR(NAM,STR) const char NAM[] PROGMEM = STR
  65. #ifndef CRITICAL_SECTION_START
  66. #define CRITICAL_SECTION_START() unsigned char _sreg = SREG; cli()
  67. #define CRITICAL_SECTION_END() SREG = _sreg
  68. #endif
  69. #define HAL_CAN_SET_PWM_FREQ // This HAL supports PWM Frequency adjustment
  70. #define PWM_FREQUENCY 1000 // Default PWM frequency when set_pwm_duty() is called without set_pwm_frequency()
  71. // ------------------------
  72. // Types
  73. // ------------------------
  74. typedef int8_t pin_t;
  75. #define SHARED_SERVOS HAS_SERVOS // Use shared/servos.cpp
  76. class Servo;
  77. typedef Servo hal_servo_t;
  78. // ------------------------
  79. // Serial ports
  80. // ------------------------
  81. #ifdef USBCON
  82. #include "../../core/serial_hook.h"
  83. typedef ForwardSerial1Class< decltype(Serial) > DefaultSerial1;
  84. extern DefaultSerial1 MSerial0;
  85. #ifdef BLUETOOTH
  86. typedef ForwardSerial1Class< decltype(bluetoothSerial) > BTSerial;
  87. extern BTSerial btSerial;
  88. #endif
  89. #define MYSERIAL1 TERN(BLUETOOTH, btSerial, MSerial0)
  90. #else
  91. #if !WITHIN(SERIAL_PORT, -1, 3)
  92. #error "SERIAL_PORT must be from 0 to 3, or -1 for USB Serial."
  93. #endif
  94. #define MYSERIAL1 customizedSerial1
  95. #ifdef SERIAL_PORT_2
  96. #if !WITHIN(SERIAL_PORT_2, -1, 3)
  97. #error "SERIAL_PORT_2 must be from 0 to 3, or -1 for USB Serial."
  98. #endif
  99. #define MYSERIAL2 customizedSerial2
  100. #endif
  101. #ifdef SERIAL_PORT_3
  102. #if !WITHIN(SERIAL_PORT_3, -1, 3)
  103. #error "SERIAL_PORT_3 must be from 0 to 3, or -1 for USB Serial."
  104. #endif
  105. #define MYSERIAL3 customizedSerial3
  106. #endif
  107. #endif
  108. #ifdef MMU2_SERIAL_PORT
  109. #if !WITHIN(MMU2_SERIAL_PORT, -1, 3)
  110. #error "MMU2_SERIAL_PORT must be from 0 to 3, or -1 for USB Serial."
  111. #endif
  112. #define MMU2_SERIAL mmuSerial
  113. #endif
  114. #ifdef LCD_SERIAL_PORT
  115. #if !WITHIN(LCD_SERIAL_PORT, -1, 3)
  116. #error "LCD_SERIAL_PORT must be from 0 to 3, or -1 for USB Serial."
  117. #endif
  118. #define LCD_SERIAL lcdSerial
  119. #if HAS_DGUS_LCD
  120. #define SERIAL_GET_TX_BUFFER_FREE() LCD_SERIAL.get_tx_buffer_free()
  121. #endif
  122. #endif
  123. //
  124. // ADC
  125. //
  126. #define HAL_ADC_VREF 5.0
  127. #define HAL_ADC_RESOLUTION 10
  128. //
  129. // Pin Mapping for M42, M43, M226
  130. //
  131. #define GET_PIN_MAP_PIN(index) index
  132. #define GET_PIN_MAP_INDEX(pin) pin
  133. #define PARSED_PIN_INDEX(code, dval) parser.intval(code, dval)
  134. #define HAL_SENSITIVE_PINS 0, 1,
  135. #ifdef __AVR_AT90USB1286__
  136. #define JTAG_DISABLE() do{ MCUCR = 0x80; MCUCR = 0x80; }while(0)
  137. #endif
  138. // AVR compatibility
  139. #define strtof strtod
  140. // ------------------------
  141. // Free Memory Accessor
  142. // ------------------------
  143. #pragma GCC diagnostic push
  144. #if GCC_VERSION <= 50000
  145. #pragma GCC diagnostic ignored "-Wunused-function"
  146. #endif
  147. extern "C" int freeMemory();
  148. #pragma GCC diagnostic pop
  149. // ------------------------
  150. // MarlinHAL Class
  151. // ------------------------
  152. class MarlinHAL {
  153. public:
  154. // Earliest possible init, before setup()
  155. MarlinHAL() {}
  156. // Watchdog
  157. static void watchdog_init() IF_DISABLED(USE_WATCHDOG, {});
  158. static void watchdog_refresh() IF_DISABLED(USE_WATCHDOG, {});
  159. static void init(); // Called early in setup()
  160. static void init_board() {} // Called less early in setup()
  161. static void reboot(); // Restart the firmware from 0x0
  162. // Interrupts
  163. static bool isr_state() { return TEST(SREG, SREG_I); }
  164. static void isr_on() { sei(); }
  165. static void isr_off() { cli(); }
  166. static void delay_ms(const int ms) { _delay_ms(ms); }
  167. // Tasks, called from idle()
  168. static void idletask() {}
  169. // Reset
  170. static uint8_t reset_reason;
  171. static uint8_t get_reset_source() { return reset_reason; }
  172. static void clear_reset_source() { MCUSR = 0; }
  173. // Free SRAM
  174. static int freeMemory() { return ::freeMemory(); }
  175. //
  176. // ADC Methods
  177. //
  178. // Called by Temperature::init once at startup
  179. static void adc_init() {
  180. ADCSRA = _BV(ADEN) | _BV(ADSC) | _BV(ADIF) | 0x07;
  181. DIDR0 = 0;
  182. #ifdef DIDR2
  183. DIDR2 = 0;
  184. #endif
  185. }
  186. // Called by Temperature::init for each sensor at startup
  187. static void adc_enable(const uint8_t ch) {
  188. #ifdef DIDR2
  189. if (ch > 7) { SBI(DIDR2, ch & 0x07); return; }
  190. #endif
  191. SBI(DIDR0, ch);
  192. }
  193. // Begin ADC sampling on the given channel. Called from Temperature::isr!
  194. static void adc_start(const uint8_t ch) {
  195. #ifdef MUX5
  196. ADCSRB = ch > 7 ? _BV(MUX5) : 0;
  197. #else
  198. ADCSRB = 0;
  199. #endif
  200. ADMUX = _BV(REFS0) | (ch & 0x07);
  201. SBI(ADCSRA, ADSC);
  202. }
  203. // Is the ADC ready for reading?
  204. static bool adc_ready() { return !TEST(ADCSRA, ADSC); }
  205. // The current value of the ADC register
  206. static __typeof__(ADC) adc_value() { return ADC; }
  207. /**
  208. * init_pwm_timers
  209. * Set the default frequency for timers 2-5 to 1000HZ
  210. */
  211. static void init_pwm_timers();
  212. /**
  213. * Set the PWM duty cycle for the pin to the given value.
  214. * Optionally invert the duty cycle [default = false]
  215. * Optionally change the scale of the provided value to enable finer PWM duty control [default = 255]
  216. */
  217. static void set_pwm_duty(const pin_t pin, const uint16_t v, const uint16_t v_size=255, const bool invert=false);
  218. /**
  219. * Set the frequency of the timer for the given pin as close as
  220. * possible to the provided desired frequency. Internally calculate
  221. * the required waveform generation mode, prescaler, and resolution
  222. * values and set timer registers accordingly.
  223. * NOTE that the frequency is applied to all pins on the timer (Ex OC3A, OC3B and OC3B)
  224. * NOTE that there are limitations, particularly if using TIMER2. (see Configuration_adv.h -> FAST_PWM_FAN Settings)
  225. */
  226. static void set_pwm_frequency(const pin_t pin, const uint16_t f_desired);
  227. };