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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210
  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 USBCON
  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 USBCON
  71. #include "../../core/serial_hook.h"
  72. typedef ForwardSerial1Class< decltype(Serial) > DefaultSerial1;
  73. extern DefaultSerial1 MSerial0;
  74. #ifdef BLUETOOTH
  75. typedef ForwardSerial1Class< decltype(bluetoothSerial) > BTSerial;
  76. extern BTSerial btSerial;
  77. #endif
  78. #define MYSERIAL1 TERN(BLUETOOTH, btSerial, MSerial0)
  79. #else
  80. #if !WITHIN(SERIAL_PORT, -1, 3)
  81. #error "SERIAL_PORT must be from -1 to 3. Please update your configuration."
  82. #endif
  83. #define MYSERIAL1 customizedSerial1
  84. #ifdef SERIAL_PORT_2
  85. #if !WITHIN(SERIAL_PORT_2, -1, 3)
  86. #error "SERIAL_PORT_2 must be from -1 to 3. Please update your configuration."
  87. #endif
  88. #define MYSERIAL2 customizedSerial2
  89. #endif
  90. #endif
  91. #ifdef MMU2_SERIAL_PORT
  92. #if !WITHIN(MMU2_SERIAL_PORT, -1, 3)
  93. #error "MMU2_SERIAL_PORT must be from -1 to 3. Please update your configuration."
  94. #endif
  95. #define MMU2_SERIAL mmuSerial
  96. #endif
  97. #ifdef LCD_SERIAL_PORT
  98. #if !WITHIN(LCD_SERIAL_PORT, -1, 3)
  99. #error "LCD_SERIAL_PORT must be from -1 to 3. Please update your configuration."
  100. #endif
  101. #define LCD_SERIAL lcdSerial
  102. #if HAS_DGUS_LCD
  103. #define SERIAL_GET_TX_BUFFER_FREE() LCD_SERIAL.get_tx_buffer_free()
  104. #endif
  105. #endif
  106. // ------------------------
  107. // Public functions
  108. // ------------------------
  109. void HAL_init();
  110. //void cli();
  111. //void _delay_ms(const int delay);
  112. inline void HAL_clear_reset_source() { MCUSR = 0; }
  113. inline uint8_t HAL_get_reset_source() { return MCUSR; }
  114. inline void HAL_reboot() {} // reboot the board or restart the bootloader
  115. #if GCC_VERSION <= 50000
  116. #pragma GCC diagnostic push
  117. #pragma GCC diagnostic ignored "-Wunused-function"
  118. #endif
  119. extern "C" int freeMemory();
  120. #if GCC_VERSION <= 50000
  121. #pragma GCC diagnostic pop
  122. #endif
  123. // ADC
  124. #ifdef DIDR2
  125. #define HAL_ANALOG_SELECT(ind) do{ if (ind < 8) SBI(DIDR0, ind); else SBI(DIDR2, ind & 0x07); }while(0)
  126. #else
  127. #define HAL_ANALOG_SELECT(ind) SBI(DIDR0, ind);
  128. #endif
  129. inline void HAL_adc_init() {
  130. ADCSRA = _BV(ADEN) | _BV(ADSC) | _BV(ADIF) | 0x07;
  131. DIDR0 = 0;
  132. #ifdef DIDR2
  133. DIDR2 = 0;
  134. #endif
  135. }
  136. #define SET_ADMUX_ADCSRA(ch) ADMUX = _BV(REFS0) | (ch & 0x07); SBI(ADCSRA, ADSC)
  137. #ifdef MUX5
  138. #define HAL_START_ADC(ch) if (ch > 7) ADCSRB = _BV(MUX5); else ADCSRB = 0; SET_ADMUX_ADCSRA(ch)
  139. #else
  140. #define HAL_START_ADC(ch) ADCSRB = 0; SET_ADMUX_ADCSRA(ch)
  141. #endif
  142. #define HAL_ADC_VREF 5.0
  143. #define HAL_ADC_RESOLUTION 10
  144. #define HAL_READ_ADC() ADC
  145. #define HAL_ADC_READY() !TEST(ADCSRA, ADSC)
  146. #define GET_PIN_MAP_PIN(index) index
  147. #define GET_PIN_MAP_INDEX(pin) pin
  148. #define PARSED_PIN_INDEX(code, dval) parser.intval(code, dval)
  149. #define HAL_SENSITIVE_PINS 0, 1
  150. #ifdef __AVR_AT90USB1286__
  151. #define JTAG_DISABLE() do{ MCUCR = 0x80; MCUCR = 0x80; }while(0)
  152. #endif
  153. // AVR compatibility
  154. #define strtof strtod
  155. #define HAL_CAN_SET_PWM_FREQ // This HAL supports PWM Frequency adjustment
  156. /**
  157. * set_pwm_frequency
  158. * Sets the frequency of the timer corresponding to the provided pin
  159. * as close as possible to the provided desired frequency. Internally
  160. * calculates the required waveform generation mode, prescaler and
  161. * resolution values required and sets the timer registers accordingly.
  162. * NOTE that the frequency is applied to all pins on the timer (Ex OC3A, OC3B and OC3B)
  163. * NOTE that there are limitations, particularly if using TIMER2. (see Configuration_adv.h -> FAST FAN PWM Settings)
  164. */
  165. void set_pwm_frequency(const pin_t pin, int f_desired);
  166. /**
  167. * set_pwm_duty
  168. * Sets the PWM duty cycle of the provided pin to the provided value
  169. * Optionally allows inverting the duty cycle [default = false]
  170. * Optionally allows changing the maximum size of the provided value to enable finer PWM duty control [default = 255]
  171. */
  172. void set_pwm_duty(const pin_t pin, const uint16_t v, const uint16_t v_size=255, const bool invert=false);