HAL.h 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337
  1. /* **************************************************************************
  2. Marlin 3D Printer Firmware
  3. Copyright (C) 2016 MarlinFirmware [https://github.com/MarlinFirmware/Marlin]
  4. Copyright (c) 2016 Bob Cousins bobcousins42@googlemail.com
  5. This program is free software: you can redistribute it and/or modify
  6. it under the terms of the GNU General Public License as published by
  7. the Free Software Foundation, either version 3 of the License, or
  8. (at your option) any later version.
  9. This program is distributed in the hope that it will be useful,
  10. but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. GNU General Public License for more details.
  13. You should have received a copy of the GNU General Public License
  14. along with this program. If not, see <http://www.gnu.org/licenses/>.
  15. ****************************************************************************/
  16. /**
  17. * Description: HAL for __AVR__
  18. */
  19. #ifndef _HAL_AVR_H_
  20. #define _HAL_AVR_H_
  21. // --------------------------------------------------------------------------
  22. // Includes
  23. // --------------------------------------------------------------------------
  24. #include "fastio.h"
  25. #include <stdint.h>
  26. #include <Arduino.h>
  27. #include <util/delay.h>
  28. #include <avr/eeprom.h>
  29. #include <avr/pgmspace.h>
  30. #include <avr/interrupt.h>
  31. #include <avr/io.h>
  32. // --------------------------------------------------------------------------
  33. // Defines
  34. // --------------------------------------------------------------------------
  35. //#define analogInputToDigitalPin(IO) IO
  36. // Bracket code that shouldn't be interrupted
  37. #ifndef CRITICAL_SECTION_START
  38. #define CRITICAL_SECTION_START unsigned char _sreg = SREG; cli()
  39. #define CRITICAL_SECTION_END SREG = _sreg
  40. #endif
  41. #define ISRS_ENABLED() TEST(SREG, SREG_I)
  42. #define ENABLE_ISRS() sei()
  43. #define DISABLE_ISRS() cli()
  44. // --------------------------------------------------------------------------
  45. // Types
  46. // --------------------------------------------------------------------------
  47. typedef uint16_t hal_timer_t;
  48. #define HAL_TIMER_TYPE_MAX 0xFFFF
  49. typedef int8_t pin_t;
  50. #define HAL_SERVO_LIB Servo
  51. // --------------------------------------------------------------------------
  52. // Public Variables
  53. // --------------------------------------------------------------------------
  54. //extern uint8_t MCUSR;
  55. // --------------------------------------------------------------------------
  56. // Public functions
  57. // --------------------------------------------------------------------------
  58. //void cli(void);
  59. //void _delay_ms(const int delay);
  60. inline void HAL_clear_reset_source(void) { MCUSR = 0; }
  61. inline uint8_t HAL_get_reset_source(void) { return MCUSR; }
  62. // eeprom
  63. //void eeprom_write_byte(unsigned char *pos, unsigned char value);
  64. //unsigned char eeprom_read_byte(unsigned char *pos);
  65. // timers
  66. #define HAL_TIMER_RATE ((F_CPU) / 8) // i.e., 2MHz or 2.5MHz
  67. #define STEP_TIMER_NUM 1
  68. #define TEMP_TIMER_NUM 0
  69. #define PULSE_TIMER_NUM STEP_TIMER_NUM
  70. #define TEMP_TIMER_FREQUENCY ((F_CPU) / 64.0 / 256.0)
  71. #define STEPPER_TIMER_RATE HAL_TIMER_RATE
  72. #define STEPPER_TIMER_PRESCALE 8
  73. #define STEPPER_TIMER_TICKS_PER_US ((STEPPER_TIMER_RATE) / 1000000) // Cannot be of type double
  74. #define PULSE_TIMER_RATE STEPPER_TIMER_RATE // frequency of pulse timer
  75. #define PULSE_TIMER_PRESCALE STEPPER_TIMER_PRESCALE
  76. #define PULSE_TIMER_TICKS_PER_US STEPPER_TIMER_TICKS_PER_US
  77. #define ENABLE_STEPPER_DRIVER_INTERRUPT() SBI(TIMSK1, OCIE1A)
  78. #define DISABLE_STEPPER_DRIVER_INTERRUPT() CBI(TIMSK1, OCIE1A)
  79. #define STEPPER_ISR_ENABLED() TEST(TIMSK1, OCIE1A)
  80. #define ENABLE_TEMPERATURE_INTERRUPT() SBI(TIMSK0, OCIE0B)
  81. #define DISABLE_TEMPERATURE_INTERRUPT() CBI(TIMSK0, OCIE0B)
  82. #define TEMPERATURE_ISR_ENABLED() TEST(TIMSK0, OCIE0B)
  83. FORCE_INLINE void HAL_timer_start(const uint8_t timer_num, const uint32_t frequency) {
  84. UNUSED(frequency);
  85. switch (timer_num) {
  86. case STEP_TIMER_NUM:
  87. // waveform generation = 0100 = CTC
  88. SET_WGM(1, CTC_OCRnA);
  89. // output mode = 00 (disconnected)
  90. SET_COMA(1, NORMAL);
  91. // Set the timer pre-scaler
  92. // Generally we use a divider of 8, resulting in a 2MHz timer
  93. // frequency on a 16MHz MCU. If you are going to change this, be
  94. // sure to regenerate speed_lookuptable.h with
  95. // create_speed_lookuptable.py
  96. SET_CS(1, PRESCALER_8); // CS 2 = 1/8 prescaler
  97. // Init Stepper ISR to 122 Hz for quick starting
  98. // (F_CPU) / (STEPPER_TIMER_PRESCALE) / frequency
  99. OCR1A = 0x4000;
  100. TCNT1 = 0;
  101. break;
  102. case TEMP_TIMER_NUM:
  103. // Use timer0 for temperature measurement
  104. // Interleave temperature interrupt with millies interrupt
  105. OCR0B = 128;
  106. break;
  107. }
  108. }
  109. #define TIMER_OCR_1 OCR1A
  110. #define TIMER_COUNTER_1 TCNT1
  111. #define TIMER_OCR_0 OCR0A
  112. #define TIMER_COUNTER_0 TCNT0
  113. #define _CAT(a, ...) a ## __VA_ARGS__
  114. #define HAL_timer_set_compare(timer, compare) (_CAT(TIMER_OCR_, timer) = compare)
  115. #define HAL_timer_get_compare(timer) _CAT(TIMER_OCR_, timer)
  116. #define HAL_timer_get_count(timer) _CAT(TIMER_COUNTER_, timer)
  117. /**
  118. * On AVR there is no hardware prioritization and preemption of
  119. * interrupts, so this emulates it. The UART has first priority
  120. * (otherwise, characters will be lost due to UART overflow).
  121. * Then: Stepper, Endstops, Temperature, and -finally- all others.
  122. */
  123. #define HAL_timer_isr_prologue(TIMER_NUM)
  124. #define HAL_timer_isr_epilogue(TIMER_NUM)
  125. /* 18 cycles maximum latency */
  126. #define HAL_STEP_TIMER_ISR \
  127. extern "C" void TIMER1_COMPA_vect (void) __attribute__ ((signal, naked, used, externally_visible)); \
  128. extern "C" void TIMER1_COMPA_vect_bottom (void) asm ("TIMER1_COMPA_vect_bottom") __attribute__ ((used, externally_visible, noinline)); \
  129. void TIMER1_COMPA_vect (void) { \
  130. __asm__ __volatile__ ( \
  131. A("push r16") /* 2 Save R16 */ \
  132. A("in r16, __SREG__") /* 1 Get SREG */ \
  133. A("push r16") /* 2 Save SREG into stack */ \
  134. A("lds r16, %[timsk0]") /* 2 Load into R0 the Temperature timer Interrupt mask register */ \
  135. A("push r16") /* 2 Save TIMSK0 into the stack */ \
  136. A("andi r16,~%[msk0]") /* 1 Disable the temperature ISR */ \
  137. A("sts %[timsk0], r16") /* 2 And set the new value */ \
  138. A("lds r16, %[timsk1]") /* 2 Load into R0 the stepper timer Interrupt mask register [TIMSK1] */ \
  139. A("andi r16,~%[msk1]") /* 1 Disable the stepper ISR */ \
  140. A("sts %[timsk1], r16") /* 2 And set the new value */ \
  141. A("push r16") /* 2 Save TIMSK1 into stack */ \
  142. A("in r16, 0x3B") /* 1 Get RAMPZ register */ \
  143. A("push r16") /* 2 Save RAMPZ into stack */ \
  144. A("in r16, 0x3C") /* 1 Get EIND register */ \
  145. A("push r0") /* C runtime can modify all the following registers without restoring them */ \
  146. A("push r1") \
  147. A("push r18") \
  148. A("push r19") \
  149. A("push r20") \
  150. A("push r21") \
  151. A("push r22") \
  152. A("push r23") \
  153. A("push r24") \
  154. A("push r25") \
  155. A("push r26") \
  156. A("push r27") \
  157. A("push r30") \
  158. A("push r31") \
  159. A("clr r1") /* C runtime expects this register to be 0 */ \
  160. A("call TIMER1_COMPA_vect_bottom") /* Call the bottom handler - No inlining allowed, otherwise registers used are not saved */ \
  161. A("pop r31") \
  162. A("pop r30") \
  163. A("pop r27") \
  164. A("pop r26") \
  165. A("pop r25") \
  166. A("pop r24") \
  167. A("pop r23") \
  168. A("pop r22") \
  169. A("pop r21") \
  170. A("pop r20") \
  171. A("pop r19") \
  172. A("pop r18") \
  173. A("pop r1") \
  174. A("pop r0") \
  175. A("out 0x3C, r16") /* 1 Restore EIND register */ \
  176. A("pop r16") /* 2 Get the original RAMPZ register value */ \
  177. A("out 0x3B, r16") /* 1 Restore RAMPZ register to its original value */ \
  178. A("pop r16") /* 2 Get the original TIMSK1 value but with stepper ISR disabled */ \
  179. A("ori r16,%[msk1]") /* 1 Reenable the stepper ISR */ \
  180. 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 */ \
  181. A("sts %[timsk1], r16") /* 2 And restore the old value - This reenables the stepper ISR */ \
  182. A("pop r16") /* 2 Get the temperature timer Interrupt mask register [TIMSK0] */ \
  183. A("sts %[timsk0], r16") /* 2 And restore the old value - This reenables the temperature ISR */ \
  184. A("pop r16") /* 2 Get the old SREG value */ \
  185. A("out __SREG__, r16") /* 1 And restore the SREG value */ \
  186. A("pop r16") /* 2 Restore R16 value */ \
  187. A("reti") /* 4 Return from interrupt */ \
  188. : \
  189. : [timsk0] "i" ((uint16_t)&TIMSK0), \
  190. [timsk1] "i" ((uint16_t)&TIMSK1), \
  191. [msk0] "M" ((uint8_t)(1<<OCIE0B)),\
  192. [msk1] "M" ((uint8_t)(1<<OCIE1A)) \
  193. : \
  194. ); \
  195. } \
  196. void TIMER1_COMPA_vect_bottom(void)
  197. /* 14 cycles maximum latency */
  198. #define HAL_TEMP_TIMER_ISR \
  199. extern "C" void TIMER0_COMPB_vect (void) __attribute__ ((signal, naked, used, externally_visible)); \
  200. extern "C" void TIMER0_COMPB_vect_bottom(void) asm ("TIMER0_COMPB_vect_bottom") __attribute__ ((used, externally_visible, noinline)); \
  201. void TIMER0_COMPB_vect (void) { \
  202. __asm__ __volatile__ ( \
  203. A("push r16") /* 2 Save R16 */ \
  204. A("in r16, __SREG__") /* 1 Get SREG */ \
  205. A("push r16") /* 2 Save SREG into stack */ \
  206. A("lds r16, %[timsk0]") /* 2 Load into R0 the Temperature timer Interrupt mask register */ \
  207. A("andi r16,~%[msk0]") /* 1 Disable the temperature ISR */ \
  208. A("sts %[timsk0], r16") /* 2 And set the new value */ \
  209. A("sei") /* 1 Enable global interrupts - It is safe, as the temperature ISR is disabled, so we cannot reenter it */ \
  210. A("push r16") /* 2 Save TIMSK0 into stack */ \
  211. A("in r16, 0x3B") /* 1 Get RAMPZ register */ \
  212. A("push r16") /* 2 Save RAMPZ into stack */ \
  213. A("in r16, 0x3C") /* 1 Get EIND register */ \
  214. A("push r0") /* C runtime can modify all the following registers without restoring them */ \
  215. A("push r1") \
  216. A("push r18") \
  217. A("push r19") \
  218. A("push r20") \
  219. A("push r21") \
  220. A("push r22") \
  221. A("push r23") \
  222. A("push r24") \
  223. A("push r25") \
  224. A("push r26") \
  225. A("push r27") \
  226. A("push r30") \
  227. A("push r31") \
  228. A("clr r1") /* C runtime expects this register to be 0 */ \
  229. A("call TIMER0_COMPB_vect_bottom") /* Call the bottom handler - No inlining allowed, otherwise registers used are not saved */ \
  230. A("pop r31") \
  231. A("pop r30") \
  232. A("pop r27") \
  233. A("pop r26") \
  234. A("pop r25") \
  235. A("pop r24") \
  236. A("pop r23") \
  237. A("pop r22") \
  238. A("pop r21") \
  239. A("pop r20") \
  240. A("pop r19") \
  241. A("pop r18") \
  242. A("pop r1") \
  243. A("pop r0") \
  244. A("out 0x3C, r16") /* 1 Restore EIND register */ \
  245. A("pop r16") /* 2 Get the original RAMPZ register value */ \
  246. A("out 0x3B, r16") /* 1 Restore RAMPZ register to its original value */ \
  247. A("pop r16") /* 2 Get the original TIMSK0 value but with temperature ISR disabled */ \
  248. A("ori r16,%[msk0]") /* 1 Enable temperature ISR */ \
  249. 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 */ \
  250. A("sts %[timsk0], r16") /* 2 And restore the old value */ \
  251. A("pop r16") /* 2 Get the old SREG */ \
  252. A("out __SREG__, r16") /* 1 And restore the SREG value */ \
  253. A("pop r16") /* 2 Restore R16 */ \
  254. A("reti") /* 4 Return from interrupt */ \
  255. : \
  256. : [timsk0] "i"((uint16_t)&TIMSK0), \
  257. [msk0] "M" ((uint8_t)(1<<OCIE0B)) \
  258. : \
  259. ); \
  260. } \
  261. void TIMER0_COMPB_vect_bottom(void)
  262. // ADC
  263. #ifdef DIDR2
  264. #define HAL_ANALOG_SELECT(pin) do{ if (pin < 8) SBI(DIDR0, pin); else SBI(DIDR2, pin & 0x07); }while(0)
  265. #else
  266. #define HAL_ANALOG_SELECT(pin) do{ SBI(DIDR0, pin); }while(0)
  267. #endif
  268. inline void HAL_adc_init(void) {
  269. ADCSRA = _BV(ADEN) | _BV(ADSC) | _BV(ADIF) | 0x07;
  270. DIDR0 = 0;
  271. #ifdef DIDR2
  272. DIDR2 = 0;
  273. #endif
  274. }
  275. #define SET_ADMUX_ADCSRA(pin) ADMUX = _BV(REFS0) | (pin & 0x07); SBI(ADCSRA, ADSC)
  276. #ifdef MUX5
  277. #define HAL_START_ADC(pin) if (pin > 7) ADCSRB = _BV(MUX5); else ADCSRB = 0; SET_ADMUX_ADCSRA(pin)
  278. #else
  279. #define HAL_START_ADC(pin) ADCSRB = 0; SET_ADMUX_ADCSRA(pin)
  280. #endif
  281. #define HAL_READ_ADC() ADC
  282. #define HAL_ADC_READY() !TEST(ADCSRA, ADSC)
  283. #define GET_PIN_MAP_PIN(index) index
  284. #define GET_PIN_MAP_INDEX(pin) pin
  285. #define PARSED_PIN_INDEX(code, dval) parser.intval(code, dval)
  286. #define HAL_SENSITIVE_PINS 0, 1
  287. #endif // _HAL_AVR_H_