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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278
  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. #ifdef USE_USB_COMPOSITE
  38. #include "msc_sd.h"
  39. #endif
  40. // ------------------------
  41. // Defines
  42. // ------------------------
  43. #ifndef STM32_FLASH_SIZE
  44. #if defined(MCU_STM32F103RE) || defined(MCU_STM32F103VE)
  45. #define STM32_FLASH_SIZE 512
  46. #else
  47. #define STM32_FLASH_SIZE 256
  48. #endif
  49. #endif
  50. #ifdef SERIAL_USB
  51. #ifndef USE_USB_COMPOSITE
  52. #define UsbSerial Serial
  53. #else
  54. #define UsbSerial MarlinCompositeSerial
  55. #endif
  56. #define MSerial1 Serial1
  57. #define MSerial2 Serial2
  58. #define MSerial3 Serial3
  59. #define MSerial4 Serial4
  60. #define MSerial5 Serial5
  61. #else
  62. #define MSerial1 Serial
  63. #define MSerial2 Serial1
  64. #define MSerial3 Serial2
  65. #define MSerial4 Serial3
  66. #define MSerial5 Serial4
  67. #endif
  68. #if SERIAL_PORT == 0
  69. #error "SERIAL_PORT cannot be 0. (Port 0 does not exist.) Please update your configuration."
  70. #elif SERIAL_PORT == -1
  71. #define MYSERIAL0 UsbSerial
  72. #elif SERIAL_PORT == 1
  73. #define MYSERIAL0 MSerial1
  74. #elif SERIAL_PORT == 2
  75. #define MYSERIAL0 MSerial2
  76. #elif SERIAL_PORT == 3
  77. #define MYSERIAL0 MSerial3
  78. #elif SERIAL_PORT == 4
  79. #define MYSERIAL0 MSerial4
  80. #elif SERIAL_PORT == 5
  81. #define MYSERIAL0 MSerial5
  82. #else
  83. #error "SERIAL_PORT must be from -1 to 5. Please update your configuration."
  84. #endif
  85. #ifdef SERIAL_PORT_2
  86. #if SERIAL_PORT_2 == 0
  87. #error "SERIAL_PORT_2 cannot be 0. (Port 0 does not exist.) Please update your configuration."
  88. #elif SERIAL_PORT_2 == SERIAL_PORT
  89. #error "SERIAL_PORT_2 must be different than SERIAL_PORT. Please update your configuration."
  90. #elif SERIAL_PORT_2 == -1
  91. #define MYSERIAL1 UsbSerial
  92. #elif SERIAL_PORT_2 == 1
  93. #define MYSERIAL1 MSerial1
  94. #elif SERIAL_PORT_2 == 2
  95. #define MYSERIAL1 MSerial2
  96. #elif SERIAL_PORT_2 == 3
  97. #define MYSERIAL1 MSerial3
  98. #elif SERIAL_PORT_2 == 4
  99. #define MYSERIAL1 MSerial4
  100. #elif SERIAL_PORT_2 == 5
  101. #define MYSERIAL1 MSerial5
  102. #else
  103. #error "SERIAL_PORT_2 must be from -1 to 5. Please update your configuration."
  104. #endif
  105. #define NUM_SERIAL 2
  106. #else
  107. #define NUM_SERIAL 1
  108. #endif
  109. #ifdef DGUS_SERIAL
  110. #if DGUS_SERIAL_PORT == 0
  111. #error "DGUS_SERIAL_PORT cannot be 0. (Port 0 does not exist.) Please update your configuration."
  112. #elif DGUS_SERIAL_PORT == SERIAL_PORT
  113. #error "DGUS_SERIAL_PORT must be different than SERIAL_PORT. Please update your configuration."
  114. #elif defined(SERIAL_PORT_2) && DGUS_SERIAL_PORT == SERIAL_PORT_2
  115. #error "DGUS_SERIAL_PORT must be different than SERIAL_PORT_2. Please update your configuration."
  116. #elif DGUS_SERIAL_PORT == -1
  117. #define DGUS_SERIAL UsbSerial
  118. #elif DGUS_SERIAL_PORT == 1
  119. #define DGUS_SERIAL MSerial1
  120. #elif DGUS_SERIAL_PORT == 2
  121. #define DGUS_SERIAL MSerial2
  122. #elif DGUS_SERIAL_PORT == 3
  123. #define DGUS_SERIAL MSerial3
  124. #elif DGUS_SERIAL_PORT == 4
  125. #define DGUS_SERIAL MSerial4
  126. #elif DGUS_SERIAL_PORT == 5
  127. #define DGUS_SERIAL MSerial5
  128. #else
  129. #error "DGUS_SERIAL_PORT must be from -1 to 5. Please update your configuration."
  130. #endif
  131. #endif
  132. // Set interrupt grouping for this MCU
  133. void HAL_init();
  134. #define HAL_IDLETASK 1
  135. void HAL_idletask();
  136. /**
  137. * TODO: review this to return 1 for pins that are not analog input
  138. */
  139. #ifndef analogInputToDigitalPin
  140. #define analogInputToDigitalPin(p) (p)
  141. #endif
  142. #ifndef digitalPinHasPWM
  143. #define digitalPinHasPWM(P) (PIN_MAP[P].timer_device != nullptr)
  144. #define NO_COMPILE_TIME_PWM
  145. #endif
  146. #define CRITICAL_SECTION_START() uint32_t primask = __get_primask(); (void)__iCliRetVal()
  147. #define CRITICAL_SECTION_END() if (!primask) (void)__iSeiRetVal()
  148. #define ISRS_ENABLED() (!__get_primask())
  149. #define ENABLE_ISRS() ((void)__iSeiRetVal())
  150. #define DISABLE_ISRS() ((void)__iCliRetVal())
  151. // On AVR this is in math.h?
  152. #define square(x) ((x)*(x))
  153. #ifndef strncpy_P
  154. #define strncpy_P(dest, src, num) strncpy((dest), (src), (num))
  155. #endif
  156. // Fix bug in pgm_read_ptr
  157. #undef pgm_read_ptr
  158. #define pgm_read_ptr(addr) (*(addr))
  159. #define RST_POWER_ON 1
  160. #define RST_EXTERNAL 2
  161. #define RST_BROWN_OUT 4
  162. #define RST_WATCHDOG 8
  163. #define RST_JTAG 16
  164. #define RST_SOFTWARE 32
  165. #define RST_BACKUP 64
  166. // ------------------------
  167. // Types
  168. // ------------------------
  169. typedef int8_t pin_t;
  170. // ------------------------
  171. // Public Variables
  172. // ------------------------
  173. // Result of last ADC conversion
  174. extern uint16_t HAL_adc_result;
  175. // ------------------------
  176. // Public functions
  177. // ------------------------
  178. // Disable interrupts
  179. #define cli() noInterrupts()
  180. // Enable interrupts
  181. #define sei() interrupts()
  182. // Memory related
  183. #define __bss_end __bss_end__
  184. // Clear reset reason
  185. void HAL_clear_reset_source();
  186. // Reset reason
  187. uint8_t HAL_get_reset_source();
  188. void _delay_ms(const int delay);
  189. #pragma GCC diagnostic push
  190. #pragma GCC diagnostic ignored "-Wunused-function"
  191. /*
  192. extern "C" {
  193. int freeMemory();
  194. }
  195. */
  196. extern "C" char* _sbrk(int incr);
  197. /*
  198. static int freeMemory() {
  199. volatile int top;
  200. top = (int)((char*)&top - reinterpret_cast<char*>(_sbrk(0)));
  201. return top;
  202. }
  203. */
  204. static int freeMemory() {
  205. volatile char top;
  206. return &top - reinterpret_cast<char*>(_sbrk(0));
  207. }
  208. #pragma GCC diagnostic pop
  209. //
  210. // ADC
  211. //
  212. #define HAL_ANALOG_SELECT(pin) pinMode(pin, INPUT_ANALOG);
  213. void HAL_adc_init();
  214. #define HAL_ADC_VREF 3.3
  215. #define HAL_ADC_RESOLUTION 10
  216. #define HAL_START_ADC(pin) HAL_adc_start_conversion(pin)
  217. #define HAL_READ_ADC() HAL_adc_result
  218. #define HAL_ADC_READY() true
  219. void HAL_adc_start_conversion(const uint8_t adc_pin);
  220. uint16_t HAL_adc_get_result();
  221. uint16_t analogRead(pin_t pin); // need HAL_ANALOG_SELECT() first
  222. void analogWrite(pin_t pin, int pwm_val8); // PWM only! mul by 257 in maple!?
  223. #define GET_PIN_MAP_PIN(index) index
  224. #define GET_PIN_MAP_INDEX(pin) pin
  225. #define PARSED_PIN_INDEX(code, dval) parser.intval(code, dval)
  226. #define JTAG_DISABLE() afio_cfg_debug_ports(AFIO_DEBUG_SW_ONLY)
  227. #define JTAGSWD_DISABLE() afio_cfg_debug_ports(AFIO_DEBUG_NONE)
  228. #define PLATFORM_M997_SUPPORT
  229. void flashFirmware(const int16_t);