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

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