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

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