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.4KB

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