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

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