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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202
  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. #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. #ifndef pgm_read_ptr
  38. // Compatibility for avr-libc 1.8.0-4.1 included with Ubuntu for
  39. // Windows Subsystem for Linux on Windows 10 as of 10/18/2019
  40. #define pgm_read_ptr_far(address_long) (void*)__ELPM_word((uint32_t)(address_long))
  41. #define pgm_read_ptr_near(address_short) (void*)__LPM_word((uint16_t)(address_short))
  42. #define pgm_read_ptr(address_short) pgm_read_ptr_near(address_short)
  43. #endif
  44. // ------------------------
  45. // Defines
  46. // ------------------------
  47. // AVR PROGMEM extension for sprintf_P
  48. #define S_FMT "%S"
  49. // AVR PROGMEM extension for string define
  50. #define PGMSTR(NAM,STR) const char NAM[] PROGMEM = STR
  51. #ifndef CRITICAL_SECTION_START
  52. #define CRITICAL_SECTION_START() unsigned char _sreg = SREG; cli()
  53. #define CRITICAL_SECTION_END() SREG = _sreg
  54. #endif
  55. #define ISRS_ENABLED() TEST(SREG, SREG_I)
  56. #define ENABLE_ISRS() sei()
  57. #define DISABLE_ISRS() cli()
  58. // ------------------------
  59. // Types
  60. // ------------------------
  61. typedef int8_t pin_t;
  62. #define SHARED_SERVOS HAS_SERVOS
  63. #define HAL_SERVO_LIB Servo
  64. // ------------------------
  65. // Public Variables
  66. // ------------------------
  67. //extern uint8_t MCUSR;
  68. // Serial ports
  69. #ifdef USBCON
  70. #if ENABLED(BLUETOOTH)
  71. #define MYSERIAL0 bluetoothSerial
  72. #else
  73. #define MYSERIAL0 Serial
  74. #endif
  75. #define NUM_SERIAL 1
  76. #else
  77. #if !WITHIN(SERIAL_PORT, -1, 3)
  78. #error "SERIAL_PORT must be from -1 to 3. Please update your configuration."
  79. #endif
  80. #define MYSERIAL0 customizedSerial1
  81. #ifdef SERIAL_PORT_2
  82. #if !WITHIN(SERIAL_PORT_2, -1, 3)
  83. #error "SERIAL_PORT_2 must be from -1 to 3. Please update your configuration."
  84. #elif SERIAL_PORT_2 == SERIAL_PORT
  85. #error "SERIAL_PORT_2 must be different than SERIAL_PORT. Please update your configuration."
  86. #endif
  87. #define MYSERIAL1 customizedSerial2
  88. #define NUM_SERIAL 2
  89. #else
  90. #define NUM_SERIAL 1
  91. #endif
  92. #endif
  93. #ifdef DGUS_SERIAL_PORT
  94. #if !WITHIN(DGUS_SERIAL_PORT, -1, 3)
  95. #error "DGUS_SERIAL_PORT must be from -1 to 3. Please update your configuration."
  96. #elif DGUS_SERIAL_PORT == SERIAL_PORT
  97. #error "DGUS_SERIAL_PORT must be different than SERIAL_PORT. Please update your configuration."
  98. #elif defined(SERIAL_PORT_2) && DGUS_SERIAL_PORT == SERIAL_PORT_2
  99. #error "DGUS_SERIAL_PORT must be different than SERIAL_PORT_2. Please update your configuration."
  100. #endif
  101. #define DGUS_SERIAL internalDgusSerial
  102. #define DGUS_SERIAL_GET_TX_BUFFER_FREE DGUS_SERIAL.get_tx_buffer_free
  103. #endif
  104. // ------------------------
  105. // Public functions
  106. // ------------------------
  107. void HAL_init();
  108. //void cli();
  109. //void _delay_ms(const int delay);
  110. inline void HAL_clear_reset_source() { MCUSR = 0; }
  111. inline uint8_t HAL_get_reset_source() { return MCUSR; }
  112. #pragma GCC diagnostic push
  113. #pragma GCC diagnostic ignored "-Wunused-function"
  114. extern "C" {
  115. int freeMemory();
  116. }
  117. #pragma GCC diagnostic pop
  118. // ADC
  119. #ifdef DIDR2
  120. #define HAL_ANALOG_SELECT(ind) do{ if (ind < 8) SBI(DIDR0, ind); else SBI(DIDR2, ind & 0x07); }while(0)
  121. #else
  122. #define HAL_ANALOG_SELECT(ind) SBI(DIDR0, ind);
  123. #endif
  124. inline void HAL_adc_init() {
  125. ADCSRA = _BV(ADEN) | _BV(ADSC) | _BV(ADIF) | 0x07;
  126. DIDR0 = 0;
  127. #ifdef DIDR2
  128. DIDR2 = 0;
  129. #endif
  130. }
  131. #define SET_ADMUX_ADCSRA(ch) ADMUX = _BV(REFS0) | (ch & 0x07); SBI(ADCSRA, ADSC)
  132. #ifdef MUX5
  133. #define HAL_START_ADC(ch) if (ch > 7) ADCSRB = _BV(MUX5); else ADCSRB = 0; SET_ADMUX_ADCSRA(ch)
  134. #else
  135. #define HAL_START_ADC(ch) ADCSRB = 0; SET_ADMUX_ADCSRA(ch)
  136. #endif
  137. #define HAL_ADC_VREF 5.0
  138. #define HAL_ADC_RESOLUTION 10
  139. #define HAL_READ_ADC() ADC
  140. #define HAL_ADC_READY() !TEST(ADCSRA, ADSC)
  141. #define GET_PIN_MAP_PIN(index) index
  142. #define GET_PIN_MAP_INDEX(pin) pin
  143. #define PARSED_PIN_INDEX(code, dval) parser.intval(code, dval)
  144. #define HAL_SENSITIVE_PINS 0, 1
  145. #ifdef __AVR_AT90USB1286__
  146. #define JTAG_DISABLE() do{ MCUCR = 0x80; MCUCR = 0x80; }while(0)
  147. #endif
  148. // AVR compatibility
  149. #define strtof strtod
  150. #define HAL_CAN_SET_PWM_FREQ // This HAL supports PWM Frequency adjustment
  151. /**
  152. * set_pwm_frequency
  153. * Sets the frequency of the timer corresponding to the provided pin
  154. * as close as possible to the provided desired frequency. Internally
  155. * calculates the required waveform generation mode, prescaler and
  156. * resolution values required and sets the timer registers accordingly.
  157. * NOTE that the frequency is applied to all pins on the timer (Ex OC3A, OC3B and OC3B)
  158. * NOTE that there are limitations, particularly if using TIMER2. (see Configuration_adv.h -> FAST FAN PWM Settings)
  159. */
  160. void set_pwm_frequency(const pin_t pin, int f_desired);
  161. /**
  162. * set_pwm_duty
  163. * Sets the PWM duty cycle of the provided pin to the provided value
  164. * Optionally allows inverting the duty cycle [default = false]
  165. * Optionally allows changing the maximum size of the provided value to enable finer PWM duty control [default = 255]
  166. */
  167. void set_pwm_duty(const pin_t pin, const uint16_t v, const uint16_t v_size=255, const bool invert=false);