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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242
  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. //
  36. // Default graphical display delays
  37. //
  38. #define CPU_ST7920_DELAY_1 300
  39. #define CPU_ST7920_DELAY_2 40
  40. #define CPU_ST7920_DELAY_3 340
  41. //
  42. // Serial Ports
  43. //
  44. #ifdef USBCON
  45. #include <USBSerial.h>
  46. #include "../../core/serial_hook.h"
  47. typedef ForwardSerial1Class< decltype(SerialUSB) > DefaultSerial1;
  48. extern DefaultSerial1 MSerial0;
  49. #endif
  50. #define _MSERIAL(X) MSerial##X
  51. #define MSERIAL(X) _MSERIAL(X)
  52. #if SERIAL_PORT == -1
  53. #define MYSERIAL1 MSerial0
  54. #elif WITHIN(SERIAL_PORT, 1, 6)
  55. #define MYSERIAL1 MSERIAL(SERIAL_PORT)
  56. #else
  57. #error "SERIAL_PORT must be from 1 to 6. You can also use -1 if the board supports Native USB."
  58. #endif
  59. #ifdef SERIAL_PORT_2
  60. #if SERIAL_PORT_2 == -1
  61. #define MYSERIAL2 MSerial0
  62. #elif WITHIN(SERIAL_PORT_2, 1, 6)
  63. #define MYSERIAL2 MSERIAL(SERIAL_PORT_2)
  64. #else
  65. #error "SERIAL_PORT_2 must be from 1 to 6. You can also use -1 if the board supports Native USB."
  66. #endif
  67. #endif
  68. #ifdef SERIAL_PORT_3
  69. #if SERIAL_PORT_3 == -1
  70. #define MYSERIAL3 MSerial0
  71. #elif WITHIN(SERIAL_PORT_3, 1, 6)
  72. #define MYSERIAL3 MSERIAL(SERIAL_PORT_3)
  73. #else
  74. #error "SERIAL_PORT_3 must be from 1 to 6. You can also use -1 if the board supports Native USB."
  75. #endif
  76. #endif
  77. #ifdef MMU2_SERIAL_PORT
  78. #if MMU2_SERIAL_PORT == -1
  79. #define MMU2_SERIAL MSerial0
  80. #elif WITHIN(MMU2_SERIAL_PORT, 1, 6)
  81. #define MMU2_SERIAL MSERIAL(MMU2_SERIAL_PORT)
  82. #else
  83. #error "MMU2_SERIAL_PORT must be from 1 to 6. You can also use -1 if the board supports Native USB."
  84. #endif
  85. #endif
  86. #ifdef LCD_SERIAL_PORT
  87. #if LCD_SERIAL_PORT == -1
  88. #define LCD_SERIAL MSerial0
  89. #elif WITHIN(LCD_SERIAL_PORT, 1, 6)
  90. #define LCD_SERIAL MSERIAL(LCD_SERIAL_PORT)
  91. #else
  92. #error "LCD_SERIAL_PORT must be from 1 to 6. You can also use -1 if the board supports Native USB."
  93. #endif
  94. #if HAS_DGUS_LCD
  95. #define SERIAL_GET_TX_BUFFER_FREE() LCD_SERIAL.availableForWrite()
  96. #endif
  97. #endif
  98. /**
  99. * TODO: review this to return 1 for pins that are not analog input
  100. */
  101. #ifndef analogInputToDigitalPin
  102. #define analogInputToDigitalPin(p) (p)
  103. #endif
  104. #define CRITICAL_SECTION_START() uint32_t primask = __get_PRIMASK(); __disable_irq()
  105. #define CRITICAL_SECTION_END() if (!primask) __enable_irq()
  106. #define ISRS_ENABLED() (!__get_PRIMASK())
  107. #define ENABLE_ISRS() __enable_irq()
  108. #define DISABLE_ISRS() __disable_irq()
  109. #define cli() __disable_irq()
  110. #define sei() __enable_irq()
  111. // On AVR this is in math.h?
  112. #define square(x) ((x)*(x))
  113. // ------------------------
  114. // Types
  115. // ------------------------
  116. #ifdef STM32G0B1xx
  117. typedef int32_t pin_t;
  118. #else
  119. typedef int16_t pin_t;
  120. #endif
  121. #define HAL_SERVO_LIB libServo
  122. #define PAUSE_SERVO_OUTPUT() libServo::pause_all_servos()
  123. #define RESUME_SERVO_OUTPUT() libServo::resume_all_servos()
  124. // ------------------------
  125. // Public Variables
  126. // ------------------------
  127. // result of last ADC conversion
  128. extern uint16_t HAL_adc_result;
  129. // ------------------------
  130. // Public functions
  131. // ------------------------
  132. // Memory related
  133. #define __bss_end __bss_end__
  134. // Enable hooks into setup for HAL
  135. void HAL_init();
  136. #define HAL_IDLETASK 1
  137. void HAL_idletask();
  138. // Clear reset reason
  139. void HAL_clear_reset_source();
  140. // Reset reason
  141. uint8_t HAL_get_reset_source();
  142. void HAL_reboot();
  143. void _delay_ms(const int delay);
  144. extern "C" char* _sbrk(int incr);
  145. #pragma GCC diagnostic push
  146. #pragma GCC diagnostic ignored "-Wunused-function"
  147. static inline int freeMemory() {
  148. volatile char top;
  149. return &top - reinterpret_cast<char*>(_sbrk(0));
  150. }
  151. #pragma GCC diagnostic pop
  152. //
  153. // ADC
  154. //
  155. #define HAL_ANALOG_SELECT(pin) pinMode(pin, INPUT)
  156. #ifdef ADC_RESOLUTION
  157. #define HAL_ADC_RESOLUTION ADC_RESOLUTION
  158. #else
  159. #define HAL_ADC_RESOLUTION 12
  160. #endif
  161. #define HAL_ADC_VREF 3.3
  162. #define HAL_START_ADC(pin) HAL_adc_start_conversion(pin)
  163. #define HAL_READ_ADC() HAL_adc_result
  164. #define HAL_ADC_READY() true
  165. inline void HAL_adc_init() { analogReadResolution(HAL_ADC_RESOLUTION); }
  166. void HAL_adc_start_conversion(const uint8_t adc_pin);
  167. uint16_t HAL_adc_get_result();
  168. #define GET_PIN_MAP_PIN(index) index
  169. #define GET_PIN_MAP_INDEX(pin) pin
  170. #define PARSED_PIN_INDEX(code, dval) parser.intval(code, dval)
  171. #ifdef STM32F1xx
  172. #define JTAG_DISABLE() AFIO_DBGAFR_CONFIG(AFIO_MAPR_SWJ_CFG_JTAGDISABLE)
  173. #define JTAGSWD_DISABLE() AFIO_DBGAFR_CONFIG(AFIO_MAPR_SWJ_CFG_DISABLE)
  174. #define JTAGSWD_RESET() AFIO_DBGAFR_CONFIG(AFIO_MAPR_SWJ_CFG_RESET); // Reset: FULL SWD+JTAG
  175. #endif
  176. #define PLATFORM_M997_SUPPORT
  177. void flashFirmware(const int16_t);
  178. // Maple Compatibility
  179. typedef void (*systickCallback_t)(void);
  180. void systick_attach_callback(systickCallback_t cb);
  181. void HAL_SYSTICK_Callback();
  182. extern volatile uint32_t systick_uptime_millis;
  183. #define HAL_CAN_SET_PWM_FREQ // This HAL supports PWM Frequency adjustment
  184. /**
  185. * set_pwm_frequency
  186. * Set the frequency of the timer corresponding to the provided pin
  187. * All Timer PWM pins run at the same frequency
  188. */
  189. void set_pwm_frequency(const pin_t pin, int f_desired);
  190. /**
  191. * set_pwm_duty
  192. * Set the PWM duty cycle of the provided pin to the provided value
  193. * Optionally allows inverting the duty cycle [default = false]
  194. * Optionally allows changing the maximum size of the provided value to enable finer PWM duty control [default = 255]
  195. */
  196. void set_pwm_duty(const pin_t pin, const uint16_t v, const uint16_t v_size=255, const bool invert=false);