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

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