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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402
  1. /**
  2. * Marlin 3D Printer Firmware
  3. * Copyright (c) 2019 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 <http://www.gnu.org/licenses/>.
  18. */
  19. #pragma once
  20. #include "../shared/Marduino.h"
  21. #include "../shared/HAL_SPI.h"
  22. #include "fastio.h"
  23. #include "watchdog.h"
  24. #include "math.h"
  25. #ifdef USBCON
  26. #include <HardwareSerial.h>
  27. #else
  28. #define HardwareSerial_h // Hack to prevent HardwareSerial.h header inclusion
  29. #include "MarlinSerial.h"
  30. #endif
  31. #include <stdint.h>
  32. #include <util/delay.h>
  33. #include <avr/eeprom.h>
  34. #include <avr/pgmspace.h>
  35. #include <avr/interrupt.h>
  36. #include <avr/io.h>
  37. #ifndef pgm_read_ptr
  38. // Compatibility for avr-libc 1.8.0-4.1 included with Ubuntu for
  39. // Windows Subsystem for Linux on Windows 10 as of 10/18/2019
  40. #define pgm_read_ptr_far(address_long) (void*)__ELPM_word((uint32_t)(address_long))
  41. #define pgm_read_ptr_near(address_short) (void*)__LPM_word((uint16_t)(address_short))
  42. #define pgm_read_ptr(address_short) pgm_read_ptr_near(address_short)
  43. #endif
  44. // ------------------------
  45. // Defines
  46. // ------------------------
  47. //#define analogInputToDigitalPin(IO) IO
  48. #ifndef CRITICAL_SECTION_START
  49. #define CRITICAL_SECTION_START unsigned char _sreg = SREG; cli()
  50. #define CRITICAL_SECTION_END SREG = _sreg
  51. #endif
  52. #define ISRS_ENABLED() TEST(SREG, SREG_I)
  53. #define ENABLE_ISRS() sei()
  54. #define DISABLE_ISRS() cli()
  55. // On AVR this is in math.h?
  56. //#define square(x) ((x)*(x))
  57. // ------------------------
  58. // Types
  59. // ------------------------
  60. typedef uint16_t hal_timer_t;
  61. #define HAL_TIMER_TYPE_MAX 0xFFFF
  62. typedef int8_t pin_t;
  63. #define SHARED_SERVOS HAS_SERVOS
  64. #define HAL_SERVO_LIB Servo
  65. // ------------------------
  66. // Public Variables
  67. // ------------------------
  68. //extern uint8_t MCUSR;
  69. // Serial ports
  70. #ifdef USBCON
  71. #if ENABLED(BLUETOOTH)
  72. #define MYSERIAL0 bluetoothSerial
  73. #else
  74. #define MYSERIAL0 Serial
  75. #endif
  76. #define NUM_SERIAL 1
  77. #else
  78. #if !WITHIN(SERIAL_PORT, -1, 3)
  79. #error "SERIAL_PORT must be from -1 to 3. Please update your configuration."
  80. #endif
  81. #define MYSERIAL0 customizedSerial1
  82. #ifdef SERIAL_PORT_2
  83. #if !WITHIN(SERIAL_PORT_2, -1, 3)
  84. #error "SERIAL_PORT_2 must be from -1 to 3. Please update your configuration."
  85. #elif SERIAL_PORT_2 == SERIAL_PORT
  86. #error "SERIAL_PORT_2 must be different than SERIAL_PORT. Please update your configuration."
  87. #endif
  88. #define MYSERIAL1 customizedSerial2
  89. #define NUM_SERIAL 2
  90. #else
  91. #define NUM_SERIAL 1
  92. #endif
  93. #endif
  94. // ------------------------
  95. // Public functions
  96. // ------------------------
  97. void HAL_init();
  98. //void cli();
  99. //void _delay_ms(const int delay);
  100. inline void HAL_clear_reset_source() { MCUSR = 0; }
  101. inline uint8_t HAL_get_reset_source() { return MCUSR; }
  102. #pragma GCC diagnostic push
  103. #pragma GCC diagnostic ignored "-Wunused-function"
  104. extern "C" {
  105. int freeMemory();
  106. }
  107. #pragma GCC diagnostic pop
  108. // timers
  109. #define HAL_TIMER_RATE ((F_CPU) / 8) // i.e., 2MHz or 2.5MHz
  110. #define STEP_TIMER_NUM 1
  111. #define TEMP_TIMER_NUM 0
  112. #define PULSE_TIMER_NUM STEP_TIMER_NUM
  113. #define TEMP_TIMER_FREQUENCY ((F_CPU) / 64.0 / 256.0)
  114. #define STEPPER_TIMER_RATE HAL_TIMER_RATE
  115. #define STEPPER_TIMER_PRESCALE 8
  116. #define STEPPER_TIMER_TICKS_PER_US ((STEPPER_TIMER_RATE) / 1000000) // Cannot be of type double
  117. #define PULSE_TIMER_RATE STEPPER_TIMER_RATE // frequency of pulse timer
  118. #define PULSE_TIMER_PRESCALE STEPPER_TIMER_PRESCALE
  119. #define PULSE_TIMER_TICKS_PER_US STEPPER_TIMER_TICKS_PER_US
  120. #define ENABLE_STEPPER_DRIVER_INTERRUPT() SBI(TIMSK1, OCIE1A)
  121. #define DISABLE_STEPPER_DRIVER_INTERRUPT() CBI(TIMSK1, OCIE1A)
  122. #define STEPPER_ISR_ENABLED() TEST(TIMSK1, OCIE1A)
  123. #define ENABLE_TEMPERATURE_INTERRUPT() SBI(TIMSK0, OCIE0B)
  124. #define DISABLE_TEMPERATURE_INTERRUPT() CBI(TIMSK0, OCIE0B)
  125. #define TEMPERATURE_ISR_ENABLED() TEST(TIMSK0, OCIE0B)
  126. FORCE_INLINE void HAL_timer_start(const uint8_t timer_num, const uint32_t) {
  127. switch (timer_num) {
  128. case STEP_TIMER_NUM:
  129. // waveform generation = 0100 = CTC
  130. SET_WGM(1, CTC_OCRnA);
  131. // output mode = 00 (disconnected)
  132. SET_COMA(1, NORMAL);
  133. // Set the timer pre-scaler
  134. // Generally we use a divider of 8, resulting in a 2MHz timer
  135. // frequency on a 16MHz MCU. If you are going to change this, be
  136. // sure to regenerate speed_lookuptable.h with
  137. // create_speed_lookuptable.py
  138. SET_CS(1, PRESCALER_8); // CS 2 = 1/8 prescaler
  139. // Init Stepper ISR to 122 Hz for quick starting
  140. // (F_CPU) / (STEPPER_TIMER_PRESCALE) / frequency
  141. OCR1A = 0x4000;
  142. TCNT1 = 0;
  143. break;
  144. case TEMP_TIMER_NUM:
  145. // Use timer0 for temperature measurement
  146. // Interleave temperature interrupt with millies interrupt
  147. OCR0B = 128;
  148. break;
  149. }
  150. }
  151. #define TIMER_OCR_1 OCR1A
  152. #define TIMER_COUNTER_1 TCNT1
  153. #define TIMER_OCR_0 OCR0A
  154. #define TIMER_COUNTER_0 TCNT0
  155. #define _CAT(a,V...) a##V
  156. #define HAL_timer_set_compare(timer, compare) (_CAT(TIMER_OCR_, timer) = compare)
  157. #define HAL_timer_get_compare(timer) _CAT(TIMER_OCR_, timer)
  158. #define HAL_timer_get_count(timer) _CAT(TIMER_COUNTER_, timer)
  159. /**
  160. * On AVR there is no hardware prioritization and preemption of
  161. * interrupts, so this emulates it. The UART has first priority
  162. * (otherwise, characters will be lost due to UART overflow).
  163. * Then: Stepper, Endstops, Temperature, and -finally- all others.
  164. */
  165. #define HAL_timer_isr_prologue(TIMER_NUM)
  166. #define HAL_timer_isr_epilogue(TIMER_NUM)
  167. /* 18 cycles maximum latency */
  168. #define HAL_STEP_TIMER_ISR() \
  169. extern "C" void TIMER1_COMPA_vect() __attribute__ ((signal, naked, used, externally_visible)); \
  170. extern "C" void TIMER1_COMPA_vect_bottom() asm ("TIMER1_COMPA_vect_bottom") __attribute__ ((used, externally_visible, noinline)); \
  171. void TIMER1_COMPA_vect() { \
  172. __asm__ __volatile__ ( \
  173. A("push r16") /* 2 Save R16 */ \
  174. A("in r16, __SREG__") /* 1 Get SREG */ \
  175. A("push r16") /* 2 Save SREG into stack */ \
  176. A("lds r16, %[timsk0]") /* 2 Load into R0 the Temperature timer Interrupt mask register */ \
  177. A("push r16") /* 2 Save TIMSK0 into the stack */ \
  178. A("andi r16,~%[msk0]") /* 1 Disable the temperature ISR */ \
  179. A("sts %[timsk0], r16") /* 2 And set the new value */ \
  180. A("lds r16, %[timsk1]") /* 2 Load into R0 the stepper timer Interrupt mask register [TIMSK1] */ \
  181. A("andi r16,~%[msk1]") /* 1 Disable the stepper ISR */ \
  182. A("sts %[timsk1], r16") /* 2 And set the new value */ \
  183. A("push r16") /* 2 Save TIMSK1 into stack */ \
  184. A("in r16, 0x3B") /* 1 Get RAMPZ register */ \
  185. A("push r16") /* 2 Save RAMPZ into stack */ \
  186. A("in r16, 0x3C") /* 1 Get EIND register */ \
  187. A("push r0") /* C runtime can modify all the following registers without restoring them */ \
  188. A("push r1") \
  189. A("push r18") \
  190. A("push r19") \
  191. A("push r20") \
  192. A("push r21") \
  193. A("push r22") \
  194. A("push r23") \
  195. A("push r24") \
  196. A("push r25") \
  197. A("push r26") \
  198. A("push r27") \
  199. A("push r30") \
  200. A("push r31") \
  201. A("clr r1") /* C runtime expects this register to be 0 */ \
  202. A("call TIMER1_COMPA_vect_bottom") /* Call the bottom handler - No inlining allowed, otherwise registers used are not saved */ \
  203. A("pop r31") \
  204. A("pop r30") \
  205. A("pop r27") \
  206. A("pop r26") \
  207. A("pop r25") \
  208. A("pop r24") \
  209. A("pop r23") \
  210. A("pop r22") \
  211. A("pop r21") \
  212. A("pop r20") \
  213. A("pop r19") \
  214. A("pop r18") \
  215. A("pop r1") \
  216. A("pop r0") \
  217. A("out 0x3C, r16") /* 1 Restore EIND register */ \
  218. A("pop r16") /* 2 Get the original RAMPZ register value */ \
  219. A("out 0x3B, r16") /* 1 Restore RAMPZ register to its original value */ \
  220. A("pop r16") /* 2 Get the original TIMSK1 value but with stepper ISR disabled */ \
  221. A("ori r16,%[msk1]") /* 1 Reenable the stepper ISR */ \
  222. A("cli") /* 1 Disable global interrupts - Reenabling Stepper ISR can reenter amd temperature can reenter, and we want that, if it happens, after this ISR has ended */ \
  223. A("sts %[timsk1], r16") /* 2 And restore the old value - This reenables the stepper ISR */ \
  224. A("pop r16") /* 2 Get the temperature timer Interrupt mask register [TIMSK0] */ \
  225. A("sts %[timsk0], r16") /* 2 And restore the old value - This reenables the temperature ISR */ \
  226. A("pop r16") /* 2 Get the old SREG value */ \
  227. A("out __SREG__, r16") /* 1 And restore the SREG value */ \
  228. A("pop r16") /* 2 Restore R16 value */ \
  229. A("reti") /* 4 Return from interrupt */ \
  230. : \
  231. : [timsk0] "i" ((uint16_t)&TIMSK0), \
  232. [timsk1] "i" ((uint16_t)&TIMSK1), \
  233. [msk0] "M" ((uint8_t)(1<<OCIE0B)),\
  234. [msk1] "M" ((uint8_t)(1<<OCIE1A)) \
  235. : \
  236. ); \
  237. } \
  238. void TIMER1_COMPA_vect_bottom()
  239. /* 14 cycles maximum latency */
  240. #define HAL_TEMP_TIMER_ISR() \
  241. extern "C" void TIMER0_COMPB_vect() __attribute__ ((signal, naked, used, externally_visible)); \
  242. extern "C" void TIMER0_COMPB_vect_bottom() asm ("TIMER0_COMPB_vect_bottom") __attribute__ ((used, externally_visible, noinline)); \
  243. void TIMER0_COMPB_vect() { \
  244. __asm__ __volatile__ ( \
  245. A("push r16") /* 2 Save R16 */ \
  246. A("in r16, __SREG__") /* 1 Get SREG */ \
  247. A("push r16") /* 2 Save SREG into stack */ \
  248. A("lds r16, %[timsk0]") /* 2 Load into R0 the Temperature timer Interrupt mask register */ \
  249. A("andi r16,~%[msk0]") /* 1 Disable the temperature ISR */ \
  250. A("sts %[timsk0], r16") /* 2 And set the new value */ \
  251. A("sei") /* 1 Enable global interrupts - It is safe, as the temperature ISR is disabled, so we cannot reenter it */ \
  252. A("push r16") /* 2 Save TIMSK0 into stack */ \
  253. A("in r16, 0x3B") /* 1 Get RAMPZ register */ \
  254. A("push r16") /* 2 Save RAMPZ into stack */ \
  255. A("in r16, 0x3C") /* 1 Get EIND register */ \
  256. A("push r0") /* C runtime can modify all the following registers without restoring them */ \
  257. A("push r1") \
  258. A("push r18") \
  259. A("push r19") \
  260. A("push r20") \
  261. A("push r21") \
  262. A("push r22") \
  263. A("push r23") \
  264. A("push r24") \
  265. A("push r25") \
  266. A("push r26") \
  267. A("push r27") \
  268. A("push r30") \
  269. A("push r31") \
  270. A("clr r1") /* C runtime expects this register to be 0 */ \
  271. A("call TIMER0_COMPB_vect_bottom") /* Call the bottom handler - No inlining allowed, otherwise registers used are not saved */ \
  272. A("pop r31") \
  273. A("pop r30") \
  274. A("pop r27") \
  275. A("pop r26") \
  276. A("pop r25") \
  277. A("pop r24") \
  278. A("pop r23") \
  279. A("pop r22") \
  280. A("pop r21") \
  281. A("pop r20") \
  282. A("pop r19") \
  283. A("pop r18") \
  284. A("pop r1") \
  285. A("pop r0") \
  286. A("out 0x3C, r16") /* 1 Restore EIND register */ \
  287. A("pop r16") /* 2 Get the original RAMPZ register value */ \
  288. A("out 0x3B, r16") /* 1 Restore RAMPZ register to its original value */ \
  289. A("pop r16") /* 2 Get the original TIMSK0 value but with temperature ISR disabled */ \
  290. A("ori r16,%[msk0]") /* 1 Enable temperature ISR */ \
  291. A("cli") /* 1 Disable global interrupts - We must do this, as we will reenable the temperature ISR, and we don't want to reenter this handler until the current one is done */ \
  292. A("sts %[timsk0], r16") /* 2 And restore the old value */ \
  293. A("pop r16") /* 2 Get the old SREG */ \
  294. A("out __SREG__, r16") /* 1 And restore the SREG value */ \
  295. A("pop r16") /* 2 Restore R16 */ \
  296. A("reti") /* 4 Return from interrupt */ \
  297. : \
  298. : [timsk0] "i"((uint16_t)&TIMSK0), \
  299. [msk0] "M" ((uint8_t)(1<<OCIE0B)) \
  300. : \
  301. ); \
  302. } \
  303. void TIMER0_COMPB_vect_bottom()
  304. // ADC
  305. #ifdef DIDR2
  306. #define HAL_ANALOG_SELECT(pin) do{ if (pin < 8) SBI(DIDR0, pin); else SBI(DIDR2, pin & 0x07); }while(0)
  307. #else
  308. #define HAL_ANALOG_SELECT(pin) do{ SBI(DIDR0, pin); }while(0)
  309. #endif
  310. inline void HAL_adc_init() {
  311. ADCSRA = _BV(ADEN) | _BV(ADSC) | _BV(ADIF) | 0x07;
  312. DIDR0 = 0;
  313. #ifdef DIDR2
  314. DIDR2 = 0;
  315. #endif
  316. }
  317. #define SET_ADMUX_ADCSRA(pin) ADMUX = _BV(REFS0) | (pin & 0x07); SBI(ADCSRA, ADSC)
  318. #ifdef MUX5
  319. #define HAL_START_ADC(pin) if (pin > 7) ADCSRB = _BV(MUX5); else ADCSRB = 0; SET_ADMUX_ADCSRA(pin)
  320. #else
  321. #define HAL_START_ADC(pin) ADCSRB = 0; SET_ADMUX_ADCSRA(pin)
  322. #endif
  323. #define HAL_ADC_RESOLUTION 10
  324. #define HAL_READ_ADC() ADC
  325. #define HAL_ADC_READY() !TEST(ADCSRA, ADSC)
  326. #define GET_PIN_MAP_PIN(index) index
  327. #define GET_PIN_MAP_INDEX(pin) pin
  328. #define PARSED_PIN_INDEX(code, dval) parser.intval(code, dval)
  329. #define HAL_SENSITIVE_PINS 0, 1
  330. #ifdef __AVR_AT90USB1286__
  331. #define JTAG_DISABLE() do{ MCUCR = 0x80; MCUCR = 0x80; }while(0)
  332. #endif
  333. // AVR compatibility
  334. #define strtof strtod
  335. /**
  336. * set_pwm_frequency
  337. * Sets the frequency of the timer corresponding to the provided pin
  338. * as close as possible to the provided desired frequency. Internally
  339. * calculates the required waveform generation mode, prescaler and
  340. * resolution values required and sets the timer registers accordingly.
  341. * NOTE that the frequency is applied to all pins on the timer (Ex OC3A, OC3B and OC3B)
  342. * NOTE that there are limitations, particularly if using TIMER2. (see Configuration_adv.h -> FAST FAN PWM Settings)
  343. */
  344. void set_pwm_frequency(const pin_t pin, int f_desired);
  345. /**
  346. * set_pwm_duty
  347. * Sets the PWM duty cycle of the provided pin to the provided value
  348. * Optionally allows inverting the duty cycle [default = false]
  349. * Optionally allows changing the maximum size of the provided value to enable finer PWM duty control [default = 255]
  350. */
  351. void set_pwm_duty(const pin_t pin, const uint16_t v, const uint16_t v_size=255, const bool invert=false);