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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415
  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 <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. #ifdef DGUS_SERIAL_PORT
  95. #if !WITHIN(DGUS_SERIAL_PORT, -1, 3)
  96. #error "DGUS_SERIAL_PORT must be from -1 to 3. Please update your configuration."
  97. #elif DGUS_SERIAL_PORT == SERIAL_PORT
  98. #error "DGUS_SERIAL_PORT must be different than SERIAL_PORT. Please update your configuration."
  99. #elif defined(SERIAL_PORT_2) && DGUS_SERIAL_PORT == SERIAL_PORT_2
  100. #error "DGUS_SERIAL_PORT must be different than SERIAL_PORT_2. Please update your configuration."
  101. #endif
  102. #define DGUS_SERIAL internalDgusSerial
  103. #define DGUS_SERIAL_GET_TX_BUFFER_FREE DGUS_SERIAL.get_tx_buffer_free
  104. #endif
  105. // ------------------------
  106. // Public functions
  107. // ------------------------
  108. void HAL_init();
  109. //void cli();
  110. //void _delay_ms(const int delay);
  111. inline void HAL_clear_reset_source() { MCUSR = 0; }
  112. inline uint8_t HAL_get_reset_source() { return MCUSR; }
  113. #pragma GCC diagnostic push
  114. #pragma GCC diagnostic ignored "-Wunused-function"
  115. extern "C" {
  116. int freeMemory();
  117. }
  118. #pragma GCC diagnostic pop
  119. // timers
  120. #define HAL_TIMER_RATE ((F_CPU) / 8) // i.e., 2MHz or 2.5MHz
  121. #define STEP_TIMER_NUM 1
  122. #define TEMP_TIMER_NUM 0
  123. #define PULSE_TIMER_NUM STEP_TIMER_NUM
  124. #define TEMP_TIMER_FREQUENCY ((F_CPU) / 64.0 / 256.0)
  125. #define STEPPER_TIMER_RATE HAL_TIMER_RATE
  126. #define STEPPER_TIMER_PRESCALE 8
  127. #define STEPPER_TIMER_TICKS_PER_US ((STEPPER_TIMER_RATE) / 1000000) // Cannot be of type double
  128. #define PULSE_TIMER_RATE STEPPER_TIMER_RATE // frequency of pulse timer
  129. #define PULSE_TIMER_PRESCALE STEPPER_TIMER_PRESCALE
  130. #define PULSE_TIMER_TICKS_PER_US STEPPER_TIMER_TICKS_PER_US
  131. #define ENABLE_STEPPER_DRIVER_INTERRUPT() SBI(TIMSK1, OCIE1A)
  132. #define DISABLE_STEPPER_DRIVER_INTERRUPT() CBI(TIMSK1, OCIE1A)
  133. #define STEPPER_ISR_ENABLED() TEST(TIMSK1, OCIE1A)
  134. #define ENABLE_TEMPERATURE_INTERRUPT() SBI(TIMSK0, OCIE0B)
  135. #define DISABLE_TEMPERATURE_INTERRUPT() CBI(TIMSK0, OCIE0B)
  136. #define TEMPERATURE_ISR_ENABLED() TEST(TIMSK0, OCIE0B)
  137. FORCE_INLINE void HAL_timer_start(const uint8_t timer_num, const uint32_t) {
  138. switch (timer_num) {
  139. case STEP_TIMER_NUM:
  140. // waveform generation = 0100 = CTC
  141. SET_WGM(1, CTC_OCRnA);
  142. // output mode = 00 (disconnected)
  143. SET_COMA(1, NORMAL);
  144. // Set the timer pre-scaler
  145. // Generally we use a divider of 8, resulting in a 2MHz timer
  146. // frequency on a 16MHz MCU. If you are going to change this, be
  147. // sure to regenerate speed_lookuptable.h with
  148. // create_speed_lookuptable.py
  149. SET_CS(1, PRESCALER_8); // CS 2 = 1/8 prescaler
  150. // Init Stepper ISR to 122 Hz for quick starting
  151. // (F_CPU) / (STEPPER_TIMER_PRESCALE) / frequency
  152. OCR1A = 0x4000;
  153. TCNT1 = 0;
  154. break;
  155. case TEMP_TIMER_NUM:
  156. // Use timer0 for temperature measurement
  157. // Interleave temperature interrupt with millies interrupt
  158. OCR0B = 128;
  159. break;
  160. }
  161. }
  162. #define TIMER_OCR_1 OCR1A
  163. #define TIMER_COUNTER_1 TCNT1
  164. #define TIMER_OCR_0 OCR0A
  165. #define TIMER_COUNTER_0 TCNT0
  166. #define _CAT(a,V...) a##V
  167. #define HAL_timer_set_compare(timer, compare) (_CAT(TIMER_OCR_, timer) = compare)
  168. #define HAL_timer_get_compare(timer) _CAT(TIMER_OCR_, timer)
  169. #define HAL_timer_get_count(timer) _CAT(TIMER_COUNTER_, timer)
  170. /**
  171. * On AVR there is no hardware prioritization and preemption of
  172. * interrupts, so this emulates it. The UART has first priority
  173. * (otherwise, characters will be lost due to UART overflow).
  174. * Then: Stepper, Endstops, Temperature, and -finally- all others.
  175. */
  176. #define HAL_timer_isr_prologue(TIMER_NUM)
  177. #define HAL_timer_isr_epilogue(TIMER_NUM)
  178. /* 18 cycles maximum latency */
  179. #define HAL_STEP_TIMER_ISR() \
  180. extern "C" void TIMER1_COMPA_vect() __attribute__ ((signal, naked, used, externally_visible)); \
  181. extern "C" void TIMER1_COMPA_vect_bottom() asm ("TIMER1_COMPA_vect_bottom") __attribute__ ((used, externally_visible, noinline)); \
  182. void TIMER1_COMPA_vect() { \
  183. __asm__ __volatile__ ( \
  184. A("push r16") /* 2 Save R16 */ \
  185. A("in r16, __SREG__") /* 1 Get SREG */ \
  186. A("push r16") /* 2 Save SREG into stack */ \
  187. A("lds r16, %[timsk0]") /* 2 Load into R0 the Temperature timer Interrupt mask register */ \
  188. A("push r16") /* 2 Save TIMSK0 into the stack */ \
  189. A("andi r16,~%[msk0]") /* 1 Disable the temperature ISR */ \
  190. A("sts %[timsk0], r16") /* 2 And set the new value */ \
  191. A("lds r16, %[timsk1]") /* 2 Load into R0 the stepper timer Interrupt mask register [TIMSK1] */ \
  192. A("andi r16,~%[msk1]") /* 1 Disable the stepper ISR */ \
  193. A("sts %[timsk1], r16") /* 2 And set the new value */ \
  194. A("push r16") /* 2 Save TIMSK1 into stack */ \
  195. A("in r16, 0x3B") /* 1 Get RAMPZ register */ \
  196. A("push r16") /* 2 Save RAMPZ into stack */ \
  197. A("in r16, 0x3C") /* 1 Get EIND register */ \
  198. A("push r0") /* C runtime can modify all the following registers without restoring them */ \
  199. A("push r1") \
  200. A("push r18") \
  201. A("push r19") \
  202. A("push r20") \
  203. A("push r21") \
  204. A("push r22") \
  205. A("push r23") \
  206. A("push r24") \
  207. A("push r25") \
  208. A("push r26") \
  209. A("push r27") \
  210. A("push r30") \
  211. A("push r31") \
  212. A("clr r1") /* C runtime expects this register to be 0 */ \
  213. A("call TIMER1_COMPA_vect_bottom") /* Call the bottom handler - No inlining allowed, otherwise registers used are not saved */ \
  214. A("pop r31") \
  215. A("pop r30") \
  216. A("pop r27") \
  217. A("pop r26") \
  218. A("pop r25") \
  219. A("pop r24") \
  220. A("pop r23") \
  221. A("pop r22") \
  222. A("pop r21") \
  223. A("pop r20") \
  224. A("pop r19") \
  225. A("pop r18") \
  226. A("pop r1") \
  227. A("pop r0") \
  228. A("out 0x3C, r16") /* 1 Restore EIND register */ \
  229. A("pop r16") /* 2 Get the original RAMPZ register value */ \
  230. A("out 0x3B, r16") /* 1 Restore RAMPZ register to its original value */ \
  231. A("pop r16") /* 2 Get the original TIMSK1 value but with stepper ISR disabled */ \
  232. A("ori r16,%[msk1]") /* 1 Reenable the stepper ISR */ \
  233. 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 */ \
  234. A("sts %[timsk1], r16") /* 2 And restore the old value - This reenables the stepper ISR */ \
  235. A("pop r16") /* 2 Get the temperature timer Interrupt mask register [TIMSK0] */ \
  236. A("sts %[timsk0], r16") /* 2 And restore the old value - This reenables the temperature ISR */ \
  237. A("pop r16") /* 2 Get the old SREG value */ \
  238. A("out __SREG__, r16") /* 1 And restore the SREG value */ \
  239. A("pop r16") /* 2 Restore R16 value */ \
  240. A("reti") /* 4 Return from interrupt */ \
  241. : \
  242. : [timsk0] "i" ((uint16_t)&TIMSK0), \
  243. [timsk1] "i" ((uint16_t)&TIMSK1), \
  244. [msk0] "M" ((uint8_t)(1<<OCIE0B)),\
  245. [msk1] "M" ((uint8_t)(1<<OCIE1A)) \
  246. : \
  247. ); \
  248. } \
  249. void TIMER1_COMPA_vect_bottom()
  250. /* 14 cycles maximum latency */
  251. #define HAL_TEMP_TIMER_ISR() \
  252. extern "C" void TIMER0_COMPB_vect() __attribute__ ((signal, naked, used, externally_visible)); \
  253. extern "C" void TIMER0_COMPB_vect_bottom() asm ("TIMER0_COMPB_vect_bottom") __attribute__ ((used, externally_visible, noinline)); \
  254. void TIMER0_COMPB_vect() { \
  255. __asm__ __volatile__ ( \
  256. A("push r16") /* 2 Save R16 */ \
  257. A("in r16, __SREG__") /* 1 Get SREG */ \
  258. A("push r16") /* 2 Save SREG into stack */ \
  259. A("lds r16, %[timsk0]") /* 2 Load into R0 the Temperature timer Interrupt mask register */ \
  260. A("andi r16,~%[msk0]") /* 1 Disable the temperature ISR */ \
  261. A("sts %[timsk0], r16") /* 2 And set the new value */ \
  262. A("sei") /* 1 Enable global interrupts - It is safe, as the temperature ISR is disabled, so we cannot reenter it */ \
  263. A("push r16") /* 2 Save TIMSK0 into stack */ \
  264. A("in r16, 0x3B") /* 1 Get RAMPZ register */ \
  265. A("push r16") /* 2 Save RAMPZ into stack */ \
  266. A("in r16, 0x3C") /* 1 Get EIND register */ \
  267. A("push r0") /* C runtime can modify all the following registers without restoring them */ \
  268. A("push r1") \
  269. A("push r18") \
  270. A("push r19") \
  271. A("push r20") \
  272. A("push r21") \
  273. A("push r22") \
  274. A("push r23") \
  275. A("push r24") \
  276. A("push r25") \
  277. A("push r26") \
  278. A("push r27") \
  279. A("push r30") \
  280. A("push r31") \
  281. A("clr r1") /* C runtime expects this register to be 0 */ \
  282. A("call TIMER0_COMPB_vect_bottom") /* Call the bottom handler - No inlining allowed, otherwise registers used are not saved */ \
  283. A("pop r31") \
  284. A("pop r30") \
  285. A("pop r27") \
  286. A("pop r26") \
  287. A("pop r25") \
  288. A("pop r24") \
  289. A("pop r23") \
  290. A("pop r22") \
  291. A("pop r21") \
  292. A("pop r20") \
  293. A("pop r19") \
  294. A("pop r18") \
  295. A("pop r1") \
  296. A("pop r0") \
  297. A("out 0x3C, r16") /* 1 Restore EIND register */ \
  298. A("pop r16") /* 2 Get the original RAMPZ register value */ \
  299. A("out 0x3B, r16") /* 1 Restore RAMPZ register to its original value */ \
  300. A("pop r16") /* 2 Get the original TIMSK0 value but with temperature ISR disabled */ \
  301. A("ori r16,%[msk0]") /* 1 Enable temperature ISR */ \
  302. 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 */ \
  303. A("sts %[timsk0], r16") /* 2 And restore the old value */ \
  304. A("pop r16") /* 2 Get the old SREG */ \
  305. A("out __SREG__, r16") /* 1 And restore the SREG value */ \
  306. A("pop r16") /* 2 Restore R16 */ \
  307. A("reti") /* 4 Return from interrupt */ \
  308. : \
  309. : [timsk0] "i"((uint16_t)&TIMSK0), \
  310. [msk0] "M" ((uint8_t)(1<<OCIE0B)) \
  311. : \
  312. ); \
  313. } \
  314. void TIMER0_COMPB_vect_bottom()
  315. // ADC
  316. #ifdef DIDR2
  317. #define HAL_ANALOG_SELECT(ind) do{ if (ind < 8) SBI(DIDR0, ind); else SBI(DIDR2, ind & 0x07); }while(0)
  318. #else
  319. #define HAL_ANALOG_SELECT(ind) SBI(DIDR0, ind);
  320. #endif
  321. inline void HAL_adc_init() {
  322. ADCSRA = _BV(ADEN) | _BV(ADSC) | _BV(ADIF) | 0x07;
  323. DIDR0 = 0;
  324. #ifdef DIDR2
  325. DIDR2 = 0;
  326. #endif
  327. }
  328. #define SET_ADMUX_ADCSRA(ch) ADMUX = _BV(REFS0) | (ch & 0x07); SBI(ADCSRA, ADSC)
  329. #ifdef MUX5
  330. #define HAL_START_ADC(ch) if (ch > 7) ADCSRB = _BV(MUX5); else ADCSRB = 0; SET_ADMUX_ADCSRA(ch)
  331. #else
  332. #define HAL_START_ADC(ch) ADCSRB = 0; SET_ADMUX_ADCSRA(ch)
  333. #endif
  334. #define HAL_ADC_RESOLUTION 10
  335. #define HAL_READ_ADC() ADC
  336. #define HAL_ADC_READY() !TEST(ADCSRA, ADSC)
  337. #define GET_PIN_MAP_PIN(index) index
  338. #define GET_PIN_MAP_INDEX(pin) pin
  339. #define PARSED_PIN_INDEX(code, dval) parser.intval(code, dval)
  340. #define HAL_SENSITIVE_PINS 0, 1
  341. #ifdef __AVR_AT90USB1286__
  342. #define JTAG_DISABLE() do{ MCUCR = 0x80; MCUCR = 0x80; }while(0)
  343. #endif
  344. // AVR compatibility
  345. #define strtof strtod
  346. /**
  347. * set_pwm_frequency
  348. * Sets the frequency of the timer corresponding to the provided pin
  349. * as close as possible to the provided desired frequency. Internally
  350. * calculates the required waveform generation mode, prescaler and
  351. * resolution values required and sets the timer registers accordingly.
  352. * NOTE that the frequency is applied to all pins on the timer (Ex OC3A, OC3B and OC3B)
  353. * NOTE that there are limitations, particularly if using TIMER2. (see Configuration_adv.h -> FAST FAN PWM Settings)
  354. */
  355. void set_pwm_frequency(const pin_t pin, int f_desired);
  356. /**
  357. * set_pwm_duty
  358. * Sets the PWM duty cycle of the provided pin to the provided value
  359. * Optionally allows inverting the duty cycle [default = false]
  360. * Optionally allows changing the maximum size of the provided value to enable finer PWM duty control [default = 255]
  361. */
  362. void set_pwm_duty(const pin_t pin, const uint16_t v, const uint16_t v_size=255, const bool invert=false);