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.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215
  1. /**
  2. * Marlin 3D Printer Firmware
  3. *
  4. * Copyright (c) 2020 MarlinFirmware [https://github.com/MarlinFirmware/Marlin]
  5. * Copyright (c) 2016 Bob Cousins bobcousins42@googlemail.com
  6. * Copyright (c) 2015-2016 Nico Tonnhofer wurstnase.reprap@gmail.com
  7. * Copyright (c) 2017 Victor Perez
  8. *
  9. * This program is free software: you can redistribute it and/or modify
  10. * it under the terms of the GNU General Public License as published by
  11. * the Free Software Foundation, either version 3 of the License, or
  12. * (at your option) any later version.
  13. *
  14. * This program is distributed in the hope that it will be useful,
  15. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  16. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  17. * GNU General Public License for more details.
  18. *
  19. * You should have received a copy of the GNU General Public License
  20. * along with this program. If not, see <https://www.gnu.org/licenses/>.
  21. *
  22. */
  23. #pragma once
  24. #define CPU_32_BIT
  25. #include "../../core/macros.h"
  26. #include "../shared/Marduino.h"
  27. #include "../shared/math_32bit.h"
  28. #include "../shared/HAL_SPI.h"
  29. #include "fastio.h"
  30. #include "Servo.h"
  31. #include "watchdog.h"
  32. #include "MarlinSerial.h"
  33. #include "../../inc/MarlinConfigPre.h"
  34. #include <stdint.h>
  35. #ifdef USBCON
  36. #include <USBSerial.h>
  37. #include "../../core/serial_hook.h"
  38. typedef ForwardSerial1Class< decltype(SerialUSB) > DefaultSerial1;
  39. extern DefaultSerial1 MSerial0;
  40. #endif
  41. // ------------------------
  42. // Defines
  43. // ------------------------
  44. #define _MSERIAL(X) MSerial##X
  45. #define MSERIAL(X) _MSERIAL(X)
  46. #if SERIAL_PORT == -1
  47. #define MYSERIAL1 MSerial0
  48. #elif WITHIN(SERIAL_PORT, 1, 6)
  49. #define MYSERIAL1 MSERIAL(SERIAL_PORT)
  50. #else
  51. #error "SERIAL_PORT must be -1 or from 1 to 6. Please update your configuration."
  52. #endif
  53. #ifdef SERIAL_PORT_2
  54. #if SERIAL_PORT_2 == -1
  55. #define MYSERIAL2 MSerial0
  56. #elif WITHIN(SERIAL_PORT_2, 1, 6)
  57. #define MYSERIAL2 MSERIAL(SERIAL_PORT_2)
  58. #else
  59. #error "SERIAL_PORT_2 must be -1 or from 1 to 6. Please update your configuration."
  60. #endif
  61. #endif
  62. #ifdef MMU2_SERIAL_PORT
  63. #if MMU2_SERIAL_PORT == -1
  64. #define MMU2_SERIAL MSerial0
  65. #elif WITHIN(MMU2_SERIAL_PORT, 1, 6)
  66. #define MMU2_SERIAL MSERIAL(MMU2_SERIAL_PORT)
  67. #else
  68. #error "MMU2_SERIAL_PORT must be -1 or from 1 to 6. Please update your configuration."
  69. #endif
  70. #endif
  71. #ifdef LCD_SERIAL_PORT
  72. #if LCD_SERIAL_PORT == -1
  73. #define LCD_SERIAL MSerial0
  74. #elif WITHIN(LCD_SERIAL_PORT, 1, 6)
  75. #define LCD_SERIAL MSERIAL(LCD_SERIAL_PORT)
  76. #else
  77. #error "LCD_SERIAL_PORT must be -1 or from 1 to 6. Please update your configuration."
  78. #endif
  79. #if HAS_DGUS_LCD
  80. #define SERIAL_GET_TX_BUFFER_FREE() LCD_SERIAL.availableForWrite()
  81. #endif
  82. #endif
  83. /**
  84. * TODO: review this to return 1 for pins that are not analog input
  85. */
  86. #ifndef analogInputToDigitalPin
  87. #define analogInputToDigitalPin(p) (p)
  88. #endif
  89. #define CRITICAL_SECTION_START() uint32_t primask = __get_PRIMASK(); __disable_irq()
  90. #define CRITICAL_SECTION_END() if (!primask) __enable_irq()
  91. #define ISRS_ENABLED() (!__get_PRIMASK())
  92. #define ENABLE_ISRS() __enable_irq()
  93. #define DISABLE_ISRS() __disable_irq()
  94. #define cli() __disable_irq()
  95. #define sei() __enable_irq()
  96. // On AVR this is in math.h?
  97. #define square(x) ((x)*(x))
  98. // ------------------------
  99. // Types
  100. // ------------------------
  101. typedef int16_t pin_t;
  102. #define HAL_SERVO_LIB libServo
  103. #define PAUSE_SERVO_OUTPUT() libServo::pause_all_servos()
  104. #define RESUME_SERVO_OUTPUT() libServo::resume_all_servos()
  105. // ------------------------
  106. // Public Variables
  107. // ------------------------
  108. // result of last ADC conversion
  109. extern uint16_t HAL_adc_result;
  110. // ------------------------
  111. // Public functions
  112. // ------------------------
  113. // Memory related
  114. #define __bss_end __bss_end__
  115. // Enable hooks into setup for HAL
  116. void HAL_init();
  117. #define HAL_IDLETASK 1
  118. void HAL_idletask();
  119. // Clear reset reason
  120. void HAL_clear_reset_source();
  121. // Reset reason
  122. uint8_t HAL_get_reset_source();
  123. inline void HAL_reboot() {} // reboot the board or restart the bootloader
  124. void _delay_ms(const int delay);
  125. extern "C" char* _sbrk(int incr);
  126. #pragma GCC diagnostic push
  127. #pragma GCC diagnostic ignored "-Wunused-function"
  128. static inline int freeMemory() {
  129. volatile char top;
  130. return &top - reinterpret_cast<char*>(_sbrk(0));
  131. }
  132. #pragma GCC diagnostic pop
  133. //
  134. // ADC
  135. //
  136. #define HAL_ANALOG_SELECT(pin) pinMode(pin, INPUT)
  137. #define HAL_ADC_VREF 3.3
  138. #define HAL_ADC_RESOLUTION ADC_RESOLUTION // 12
  139. #define HAL_START_ADC(pin) HAL_adc_start_conversion(pin)
  140. #define HAL_READ_ADC() HAL_adc_result
  141. #define HAL_ADC_READY() true
  142. inline void HAL_adc_init() { analogReadResolution(HAL_ADC_RESOLUTION); }
  143. void HAL_adc_start_conversion(const uint8_t adc_pin);
  144. uint16_t HAL_adc_get_result();
  145. #define GET_PIN_MAP_PIN(index) index
  146. #define GET_PIN_MAP_INDEX(pin) pin
  147. #define PARSED_PIN_INDEX(code, dval) parser.intval(code, dval)
  148. #ifdef STM32F1xx
  149. #define JTAG_DISABLE() AFIO_DBGAFR_CONFIG(AFIO_MAPR_SWJ_CFG_JTAGDISABLE)
  150. #define JTAGSWD_DISABLE() AFIO_DBGAFR_CONFIG(AFIO_MAPR_SWJ_CFG_DISABLE)
  151. #endif
  152. #define PLATFORM_M997_SUPPORT
  153. void flashFirmware(const int16_t);
  154. // Maple Compatibility
  155. typedef void (*systickCallback_t)(void);
  156. void systick_attach_callback(systickCallback_t cb);
  157. void HAL_SYSTICK_Callback();
  158. extern volatile uint32_t systick_uptime_millis;
  159. #define HAL_CAN_SET_PWM_FREQ // This HAL supports PWM Frequency adjustment
  160. /**
  161. * set_pwm_frequency
  162. * Set the frequency of the timer corresponding to the provided pin
  163. * All Timer PWM pins run at the same frequency
  164. */
  165. void set_pwm_frequency(const pin_t pin, int f_desired);
  166. /**
  167. * set_pwm_duty
  168. * Set 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);