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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309
  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. /**
  25. * HAL for stm32duino.com based on Libmaple and compatible (STM32F1)
  26. */
  27. #define CPU_32_BIT
  28. #include "../../core/macros.h"
  29. #include "../shared/Marduino.h"
  30. #include "../shared/math_32bit.h"
  31. #include "../shared/HAL_SPI.h"
  32. #include "fastio.h"
  33. #include <stdint.h>
  34. #include <util/atomic.h>
  35. #include "../../inc/MarlinConfigPre.h"
  36. #if HAS_SD_HOST_DRIVE
  37. #include "msc_sd.h"
  38. #endif
  39. #include "MarlinSerial.h"
  40. // ------------------------
  41. // Defines
  42. // ------------------------
  43. //
  44. // Default graphical display delays
  45. //
  46. #define CPU_ST7920_DELAY_1 300
  47. #define CPU_ST7920_DELAY_2 40
  48. #define CPU_ST7920_DELAY_3 340
  49. #ifndef STM32_FLASH_SIZE
  50. #if ANY(MCU_STM32F103RE, MCU_STM32F103VE, MCU_STM32F103ZE)
  51. #define STM32_FLASH_SIZE 512
  52. #else
  53. #define STM32_FLASH_SIZE 256
  54. #endif
  55. #endif
  56. // ------------------------
  57. // Serial ports
  58. // ------------------------
  59. #ifdef SERIAL_USB
  60. typedef ForwardSerial1Class< USBSerial > DefaultSerial1;
  61. extern DefaultSerial1 MSerial0;
  62. #if HAS_SD_HOST_DRIVE
  63. #define UsbSerial MarlinCompositeSerial
  64. #else
  65. #define UsbSerial MSerial0
  66. #endif
  67. #endif
  68. #define _MSERIAL(X) MSerial##X
  69. #define MSERIAL(X) _MSERIAL(X)
  70. #if EITHER(STM32_HIGH_DENSITY, STM32_XL_DENSITY)
  71. #define NUM_UARTS 5
  72. #else
  73. #define NUM_UARTS 3
  74. #endif
  75. #if SERIAL_PORT == -1
  76. #define MYSERIAL1 UsbSerial
  77. #elif WITHIN(SERIAL_PORT, 1, NUM_UARTS)
  78. #define MYSERIAL1 MSERIAL(SERIAL_PORT)
  79. #else
  80. #define MYSERIAL1 MSERIAL(1) // dummy port
  81. static_assert(false, "SERIAL_PORT must be from 1 to " STRINGIFY(NUM_UARTS) ". You can also use -1 if the board supports Native USB.")
  82. #endif
  83. #ifdef SERIAL_PORT_2
  84. #if SERIAL_PORT_2 == -1
  85. #define MYSERIAL2 UsbSerial
  86. #elif WITHIN(SERIAL_PORT_2, 1, NUM_UARTS)
  87. #define MYSERIAL2 MSERIAL(SERIAL_PORT_2)
  88. #else
  89. #define MYSERIAL2 MSERIAL(1) // dummy port
  90. static_assert(false, "SERIAL_PORT_2 must be from 1 to " STRINGIFY(NUM_UARTS) ". You can also use -1 if the board supports Native USB.")
  91. #endif
  92. #endif
  93. #ifdef SERIAL_PORT_3
  94. #if SERIAL_PORT_3 == -1
  95. #define MYSERIAL3 UsbSerial
  96. #elif WITHIN(SERIAL_PORT_3, 1, NUM_UARTS)
  97. #define MYSERIAL3 MSERIAL(SERIAL_PORT_3)
  98. #else
  99. #define MYSERIAL3 MSERIAL(1) // dummy port
  100. static_assert(false, "SERIAL_PORT_3 must be from 1 to " STRINGIFY(NUM_UARTS) ". You can also use -1 if the board supports Native USB.")
  101. #endif
  102. #endif
  103. #ifdef MMU2_SERIAL_PORT
  104. #if MMU2_SERIAL_PORT == -1
  105. #define MMU2_SERIAL UsbSerial
  106. #elif WITHIN(MMU2_SERIAL_PORT, 1, NUM_UARTS)
  107. #define MMU2_SERIAL MSERIAL(MMU2_SERIAL_PORT)
  108. #else
  109. #define MMU2_SERIAL MSERIAL(1) // dummy port
  110. static_assert(false, "MMU2_SERIAL_PORT must be from 1 to " STRINGIFY(NUM_UARTS) ". You can also use -1 if the board supports Native USB.")
  111. #endif
  112. #endif
  113. #ifdef LCD_SERIAL_PORT
  114. #if LCD_SERIAL_PORT == -1
  115. #define LCD_SERIAL UsbSerial
  116. #elif WITHIN(LCD_SERIAL_PORT, 1, NUM_UARTS)
  117. #define LCD_SERIAL MSERIAL(LCD_SERIAL_PORT)
  118. #else
  119. #define LCD_SERIAL MSERIAL(1) // dummy port
  120. static_assert(false, "LCD_SERIAL_PORT must be from 1 to " STRINGIFY(NUM_UARTS) ". You can also use -1 if the board supports Native USB.")
  121. #endif
  122. #if HAS_DGUS_LCD
  123. #define SERIAL_GET_TX_BUFFER_FREE() LCD_SERIAL.availableForWrite()
  124. #endif
  125. #endif
  126. /**
  127. * TODO: review this to return 1 for pins that are not analog input
  128. */
  129. #ifndef analogInputToDigitalPin
  130. #define analogInputToDigitalPin(p) (p)
  131. #endif
  132. #ifndef digitalPinHasPWM
  133. #define digitalPinHasPWM(P) !!PIN_MAP[P].timer_device
  134. #define NO_COMPILE_TIME_PWM
  135. #endif
  136. // Reset Reason
  137. #define RST_POWER_ON 1
  138. #define RST_EXTERNAL 2
  139. #define RST_BROWN_OUT 4
  140. #define RST_WATCHDOG 8
  141. #define RST_JTAG 16
  142. #define RST_SOFTWARE 32
  143. #define RST_BACKUP 64
  144. // ------------------------
  145. // Types
  146. // ------------------------
  147. typedef int8_t pin_t;
  148. // ------------------------
  149. // Interrupts
  150. // ------------------------
  151. #define CRITICAL_SECTION_START() const bool irqon = !__get_primask(); (void)__iCliRetVal()
  152. #define CRITICAL_SECTION_END() if (!irqon) (void)__iSeiRetVal()
  153. #define cli() noInterrupts()
  154. #define sei() interrupts()
  155. // ------------------------
  156. // ADC
  157. // ------------------------
  158. #ifdef ADC_RESOLUTION
  159. #define HAL_ADC_RESOLUTION ADC_RESOLUTION
  160. #else
  161. #define HAL_ADC_RESOLUTION 12
  162. #endif
  163. #define HAL_ADC_VREF 3.3
  164. uint16_t analogRead(const pin_t pin); // need hal.adc_enable() first
  165. void analogWrite(const pin_t pin, int pwm_val8); // PWM only! mul by 257 in maple!?
  166. //
  167. // Pin Mapping for M42, M43, M226
  168. //
  169. #define GET_PIN_MAP_PIN(index) index
  170. #define GET_PIN_MAP_INDEX(pin) pin
  171. #define PARSED_PIN_INDEX(code, dval) parser.intval(code, dval)
  172. #define JTAG_DISABLE() afio_cfg_debug_ports(AFIO_DEBUG_SW_ONLY)
  173. #define JTAGSWD_DISABLE() afio_cfg_debug_ports(AFIO_DEBUG_NONE)
  174. #define PLATFORM_M997_SUPPORT
  175. void flashFirmware(const int16_t);
  176. #define HAL_CAN_SET_PWM_FREQ // This HAL supports PWM Frequency adjustment
  177. #ifndef PWM_FREQUENCY
  178. #define PWM_FREQUENCY 1000 // Default PWM Frequency
  179. #endif
  180. // ------------------------
  181. // Class Utilities
  182. // ------------------------
  183. // Memory related
  184. #define __bss_end __bss_end__
  185. void _delay_ms(const int ms);
  186. extern "C" char* _sbrk(int incr);
  187. #pragma GCC diagnostic push
  188. #if GCC_VERSION <= 50000
  189. #pragma GCC diagnostic ignored "-Wunused-function"
  190. #endif
  191. static inline int freeMemory() {
  192. volatile char top;
  193. return &top - _sbrk(0);
  194. }
  195. #pragma GCC diagnostic pop
  196. // ------------------------
  197. // MarlinHAL Class
  198. // ------------------------
  199. class MarlinHAL {
  200. public:
  201. // Earliest possible init, before setup()
  202. MarlinHAL() {}
  203. // Watchdog
  204. static void watchdog_init() IF_DISABLED(USE_WATCHDOG, {});
  205. static void watchdog_refresh() IF_DISABLED(USE_WATCHDOG, {});
  206. static void init(); // Called early in setup()
  207. static void init_board() {} // Called less early in setup()
  208. static void reboot(); // Restart the firmware from 0x0
  209. // Interrupts
  210. static bool isr_state() { return !__get_primask(); }
  211. static void isr_on() { ((void)__iSeiRetVal()); }
  212. static void isr_off() { ((void)__iCliRetVal()); }
  213. static void delay_ms(const int ms) { delay(ms); }
  214. // Tasks, called from idle()
  215. static void idletask();
  216. // Reset
  217. static uint8_t get_reset_source() { return RST_POWER_ON; }
  218. static void clear_reset_source() {}
  219. // Free SRAM
  220. static int freeMemory() { return ::freeMemory(); }
  221. //
  222. // ADC Methods
  223. //
  224. static uint16_t adc_result;
  225. // Called by Temperature::init once at startup
  226. static void adc_init();
  227. // Called by Temperature::init for each sensor at startup
  228. static void adc_enable(const pin_t pin) { pinMode(pin, INPUT_ANALOG); }
  229. // Begin ADC sampling on the given pin. Called from Temperature::isr!
  230. static void adc_start(const pin_t pin);
  231. // Is the ADC ready for reading?
  232. static bool adc_ready() { return true; }
  233. // The current value of the ADC register
  234. static uint16_t adc_value() { return adc_result; }
  235. /**
  236. * Set the PWM duty cycle for the pin to the given value.
  237. * Optionally invert the duty cycle [default = false]
  238. * Optionally change the maximum size of the provided value to enable finer PWM duty control [default = 255]
  239. * The timer must be pre-configured with set_pwm_frequency() if the default frequency is not desired.
  240. */
  241. static void set_pwm_duty(const pin_t pin, const uint16_t v, const uint16_t=255, const bool=false);
  242. /**
  243. * Set the frequency of the timer for the given pin.
  244. * All Timer PWM pins run at the same frequency.
  245. */
  246. static void set_pwm_frequency(const pin_t pin, const uint16_t f_desired);
  247. };