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

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