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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286
  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. #ifdef SERIAL_USB
  58. typedef ForwardSerial1Class< USBSerial > DefaultSerial1;
  59. extern DefaultSerial1 MSerial0;
  60. #if HAS_SD_HOST_DRIVE
  61. #define UsbSerial MarlinCompositeSerial
  62. #else
  63. #define UsbSerial MSerial0
  64. #endif
  65. #endif
  66. #define _MSERIAL(X) MSerial##X
  67. #define MSERIAL(X) _MSERIAL(X)
  68. #if EITHER(STM32_HIGH_DENSITY, STM32_XL_DENSITY)
  69. #define NUM_UARTS 5
  70. #else
  71. #define NUM_UARTS 3
  72. #endif
  73. #if SERIAL_PORT == -1
  74. #define MYSERIAL1 UsbSerial
  75. #elif WITHIN(SERIAL_PORT, 1, NUM_UARTS)
  76. #define MYSERIAL1 MSERIAL(SERIAL_PORT)
  77. #else
  78. #define MYSERIAL1 MSERIAL(1) // dummy port
  79. static_assert(false, "SERIAL_PORT must be from 1 to " STRINGIFY(NUM_UARTS) ". You can also use -1 if the board supports Native USB.")
  80. #endif
  81. #ifdef SERIAL_PORT_2
  82. #if SERIAL_PORT_2 == -1
  83. #define MYSERIAL2 UsbSerial
  84. #elif WITHIN(SERIAL_PORT_2, 1, NUM_UARTS)
  85. #define MYSERIAL2 MSERIAL(SERIAL_PORT_2)
  86. #else
  87. #define MYSERIAL2 MSERIAL(1) // dummy port
  88. 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.")
  89. #endif
  90. #endif
  91. #ifdef SERIAL_PORT_3
  92. #if SERIAL_PORT_3 == -1
  93. #define MYSERIAL3 UsbSerial
  94. #elif WITHIN(SERIAL_PORT_3, 1, NUM_UARTS)
  95. #define MYSERIAL3 MSERIAL(SERIAL_PORT_3)
  96. #else
  97. #define MYSERIAL3 MSERIAL(1) // dummy port
  98. 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.")
  99. #endif
  100. #endif
  101. #ifdef MMU2_SERIAL_PORT
  102. #if MMU2_SERIAL_PORT == -1
  103. #define MMU2_SERIAL UsbSerial
  104. #elif WITHIN(MMU2_SERIAL_PORT, 1, NUM_UARTS)
  105. #define MMU2_SERIAL MSERIAL(MMU2_SERIAL_PORT)
  106. #else
  107. #define MMU2_SERIAL MSERIAL(1) // dummy port
  108. 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.")
  109. #endif
  110. #endif
  111. #ifdef LCD_SERIAL_PORT
  112. #if LCD_SERIAL_PORT == -1
  113. #define LCD_SERIAL UsbSerial
  114. #elif WITHIN(LCD_SERIAL_PORT, 1, NUM_UARTS)
  115. #define LCD_SERIAL MSERIAL(LCD_SERIAL_PORT)
  116. #else
  117. #define LCD_SERIAL MSERIAL(1) // dummy port
  118. 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.")
  119. #endif
  120. #if HAS_DGUS_LCD
  121. #define SERIAL_GET_TX_BUFFER_FREE() LCD_SERIAL.availableForWrite()
  122. #endif
  123. #endif
  124. // Set interrupt grouping for this MCU
  125. void HAL_init();
  126. #define HAL_IDLETASK 1
  127. void HAL_idletask();
  128. /**
  129. * TODO: review this to return 1 for pins that are not analog input
  130. */
  131. #ifndef analogInputToDigitalPin
  132. #define analogInputToDigitalPin(p) (p)
  133. #endif
  134. #ifndef digitalPinHasPWM
  135. #define digitalPinHasPWM(P) !!PIN_MAP[P].timer_device
  136. #define NO_COMPILE_TIME_PWM
  137. #endif
  138. #define CRITICAL_SECTION_START() uint32_t primask = __get_primask(); (void)__iCliRetVal()
  139. #define CRITICAL_SECTION_END() if (!primask) (void)__iSeiRetVal()
  140. #define ISRS_ENABLED() (!__get_primask())
  141. #define ENABLE_ISRS() ((void)__iSeiRetVal())
  142. #define DISABLE_ISRS() ((void)__iCliRetVal())
  143. // On AVR this is in math.h?
  144. #define square(x) ((x)*(x))
  145. #define RST_POWER_ON 1
  146. #define RST_EXTERNAL 2
  147. #define RST_BROWN_OUT 4
  148. #define RST_WATCHDOG 8
  149. #define RST_JTAG 16
  150. #define RST_SOFTWARE 32
  151. #define RST_BACKUP 64
  152. // ------------------------
  153. // Types
  154. // ------------------------
  155. typedef int8_t pin_t;
  156. // ------------------------
  157. // Public Variables
  158. // ------------------------
  159. // Result of last ADC conversion
  160. extern uint16_t HAL_adc_result;
  161. // ------------------------
  162. // Public functions
  163. // ------------------------
  164. // Disable interrupts
  165. #define cli() noInterrupts()
  166. // Enable interrupts
  167. #define sei() interrupts()
  168. // Memory related
  169. #define __bss_end __bss_end__
  170. // Clear reset reason
  171. void HAL_clear_reset_source();
  172. // Reset reason
  173. uint8_t HAL_get_reset_source();
  174. void HAL_reboot();
  175. void _delay_ms(const int delay);
  176. #pragma GCC diagnostic push
  177. #pragma GCC diagnostic ignored "-Wunused-function"
  178. /*
  179. extern "C" {
  180. int freeMemory();
  181. }
  182. */
  183. extern "C" char* _sbrk(int incr);
  184. static inline int freeMemory() {
  185. volatile char top;
  186. return &top - _sbrk(0);
  187. }
  188. #pragma GCC diagnostic pop
  189. //
  190. // ADC
  191. //
  192. #define HAL_ANALOG_SELECT(pin) pinMode(pin, INPUT_ANALOG);
  193. void HAL_adc_init();
  194. #ifdef ADC_RESOLUTION
  195. #define HAL_ADC_RESOLUTION ADC_RESOLUTION
  196. #else
  197. #define HAL_ADC_RESOLUTION 12
  198. #endif
  199. #define HAL_ADC_VREF 3.3
  200. #define HAL_START_ADC(pin) HAL_adc_start_conversion(pin)
  201. #define HAL_READ_ADC() HAL_adc_result
  202. #define HAL_ADC_READY() true
  203. void HAL_adc_start_conversion(const uint8_t adc_pin);
  204. uint16_t HAL_adc_get_result();
  205. uint16_t analogRead(pin_t pin); // need HAL_ANALOG_SELECT() first
  206. void analogWrite(pin_t pin, int pwm_val8); // PWM only! mul by 257 in maple!?
  207. #define GET_PIN_MAP_PIN(index) index
  208. #define GET_PIN_MAP_INDEX(pin) pin
  209. #define PARSED_PIN_INDEX(code, dval) parser.intval(code, dval)
  210. #define JTAG_DISABLE() afio_cfg_debug_ports(AFIO_DEBUG_SW_ONLY)
  211. #define JTAGSWD_DISABLE() afio_cfg_debug_ports(AFIO_DEBUG_NONE)
  212. #define PLATFORM_M997_SUPPORT
  213. void flashFirmware(const int16_t);
  214. #ifndef PWM_FREQUENCY
  215. #define PWM_FREQUENCY 1000 // Default PWM Frequency
  216. #endif
  217. #define HAL_CAN_SET_PWM_FREQ // This HAL supports PWM Frequency adjustment
  218. /**
  219. * set_pwm_frequency
  220. * Set the frequency of the timer corresponding to the provided pin
  221. * All Timer PWM pins run at the same frequency
  222. */
  223. void set_pwm_frequency(const pin_t pin, const uint16_t f_desired);
  224. /**
  225. * set_pwm_duty
  226. * Set the PWM duty cycle of the provided pin to the provided value
  227. * Optionally allows inverting the duty cycle [default = false]
  228. * Optionally allows changing the maximum size of the provided value to enable finer PWM duty control [default = 255]
  229. * The timer must be pre-configured with set_pwm_frequency() if the default frequency is not desired.
  230. */
  231. void set_pwm_duty(const pin_t pin, const uint16_t v, const uint16_t v_size=255, const bool invert=false);