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

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