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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191
  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 <https://www.gnu.org/licenses/>.
  18. *
  19. */
  20. #pragma once
  21. #include "../shared/Marduino.h"
  22. #include "../shared/HAL_SPI.h"
  23. #include "fastio.h"
  24. #include "watchdog.h"
  25. #include "math.h"
  26. #ifdef IS_AT90USB
  27. #include <HardwareSerial.h>
  28. #else
  29. #define HardwareSerial_h // Hack to prevent HardwareSerial.h header inclusion
  30. #include "MarlinSerial.h"
  31. #endif
  32. #include <stdint.h>
  33. #include <util/delay.h>
  34. #include <avr/eeprom.h>
  35. #include <avr/pgmspace.h>
  36. #include <avr/interrupt.h>
  37. #include <avr/io.h>
  38. #ifndef pgm_read_ptr
  39. // Compatibility for avr-libc 1.8.0-4.1 included with Ubuntu for
  40. // Windows Subsystem for Linux on Windows 10 as of 10/18/2019
  41. #define pgm_read_ptr_far(address_long) (void*)__ELPM_word((uint32_t)(address_long))
  42. #define pgm_read_ptr_near(address_short) (void*)__LPM_word((uint16_t)(address_short))
  43. #define pgm_read_ptr(address_short) pgm_read_ptr_near(address_short)
  44. #endif
  45. // ------------------------
  46. // Defines
  47. // ------------------------
  48. // AVR PROGMEM extension for sprintf_P
  49. #define S_FMT "%S"
  50. // AVR PROGMEM extension for string define
  51. #define PGMSTR(NAM,STR) const char NAM[] PROGMEM = STR
  52. #ifndef CRITICAL_SECTION_START
  53. #define CRITICAL_SECTION_START() unsigned char _sreg = SREG; cli()
  54. #define CRITICAL_SECTION_END() SREG = _sreg
  55. #endif
  56. #define ISRS_ENABLED() TEST(SREG, SREG_I)
  57. #define ENABLE_ISRS() sei()
  58. #define DISABLE_ISRS() cli()
  59. // ------------------------
  60. // Types
  61. // ------------------------
  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 IS_AT90USB
  71. #define MYSERIAL0 TERN(BLUETOOTH, bluetoothSerial, Serial)
  72. #else
  73. #if !WITHIN(SERIAL_PORT, -1, 3)
  74. #error "SERIAL_PORT must be from -1 to 3. Please update your configuration."
  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. Please update your configuration."
  80. #endif
  81. #define MYSERIAL1 customizedSerial2
  82. #endif
  83. #endif
  84. #ifdef LCD_SERIAL_PORT
  85. #if !WITHIN(LCD_SERIAL_PORT, -1, 3)
  86. #error "LCD_SERIAL_PORT must be from -1 to 3. Please update your configuration."
  87. #endif
  88. #define LCD_SERIAL lcdSerial
  89. #if HAS_DGUS_LCD
  90. #define SERIAL_GET_TX_BUFFER_FREE() LCD_SERIAL.get_tx_buffer_free()
  91. #endif
  92. #endif
  93. // ------------------------
  94. // Public functions
  95. // ------------------------
  96. void HAL_init();
  97. //void cli();
  98. //void _delay_ms(const int delay);
  99. inline void HAL_clear_reset_source() { MCUSR = 0; }
  100. inline uint8_t HAL_get_reset_source() { return MCUSR; }
  101. inline void HAL_reboot() {} // reboot the board or restart the bootloader
  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. // ADC
  109. #ifdef DIDR2
  110. #define HAL_ANALOG_SELECT(ind) do{ if (ind < 8) SBI(DIDR0, ind); else SBI(DIDR2, ind & 0x07); }while(0)
  111. #else
  112. #define HAL_ANALOG_SELECT(ind) SBI(DIDR0, ind);
  113. #endif
  114. inline void HAL_adc_init() {
  115. ADCSRA = _BV(ADEN) | _BV(ADSC) | _BV(ADIF) | 0x07;
  116. DIDR0 = 0;
  117. #ifdef DIDR2
  118. DIDR2 = 0;
  119. #endif
  120. }
  121. #define SET_ADMUX_ADCSRA(ch) ADMUX = _BV(REFS0) | (ch & 0x07); SBI(ADCSRA, ADSC)
  122. #ifdef MUX5
  123. #define HAL_START_ADC(ch) if (ch > 7) ADCSRB = _BV(MUX5); else ADCSRB = 0; SET_ADMUX_ADCSRA(ch)
  124. #else
  125. #define HAL_START_ADC(ch) ADCSRB = 0; SET_ADMUX_ADCSRA(ch)
  126. #endif
  127. #define HAL_ADC_VREF 5.0
  128. #define HAL_ADC_RESOLUTION 10
  129. #define HAL_READ_ADC() ADC
  130. #define HAL_ADC_READY() !TEST(ADCSRA, ADSC)
  131. #define GET_PIN_MAP_PIN(index) index
  132. #define GET_PIN_MAP_INDEX(pin) pin
  133. #define PARSED_PIN_INDEX(code, dval) parser.intval(code, dval)
  134. #define HAL_SENSITIVE_PINS 0, 1
  135. #ifdef __AVR_AT90USB1286__
  136. #define JTAG_DISABLE() do{ MCUCR = 0x80; MCUCR = 0x80; }while(0)
  137. #endif
  138. // AVR compatibility
  139. #define strtof strtod
  140. #define HAL_CAN_SET_PWM_FREQ // This HAL supports PWM Frequency adjustment
  141. /**
  142. * set_pwm_frequency
  143. * Sets the frequency of the timer corresponding to the provided pin
  144. * as close as possible to the provided desired frequency. Internally
  145. * calculates the required waveform generation mode, prescaler and
  146. * resolution values required and sets the timer registers accordingly.
  147. * NOTE that the frequency is applied to all pins on the timer (Ex OC3A, OC3B and OC3B)
  148. * NOTE that there are limitations, particularly if using TIMER2. (see Configuration_adv.h -> FAST FAN PWM Settings)
  149. */
  150. void set_pwm_frequency(const pin_t pin, int f_desired);
  151. /**
  152. * set_pwm_duty
  153. * Sets the PWM duty cycle of the provided pin to the provided value
  154. * Optionally allows inverting the duty cycle [default = false]
  155. * Optionally allows changing the maximum size of the provided value to enable finer PWM duty control [default = 255]
  156. */
  157. void set_pwm_duty(const pin_t pin, const uint16_t v, const uint16_t v_size=255, const bool invert=false);