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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290
  1. /**
  2. * Marlin 3D Printer Firmware
  3. *
  4. * Copyright (c) 2019 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 <http://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 "timers.h"
  35. #include <stdint.h>
  36. #include <util/atomic.h>
  37. #include "../../inc/MarlinConfigPre.h"
  38. #ifdef USE_USB_COMPOSITE
  39. #include "msc_sd.h"
  40. #endif
  41. // ------------------------
  42. // Defines
  43. // ------------------------
  44. #ifndef STM32_FLASH_SIZE
  45. #ifdef MCU_STM32F103RE
  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. #define MSerial1 Serial1
  58. #define MSerial2 Serial2
  59. #define MSerial3 Serial3
  60. #define MSerial4 Serial4
  61. #define MSerial5 Serial5
  62. #else
  63. #define MSerial1 Serial
  64. #define MSerial2 Serial1
  65. #define MSerial3 Serial2
  66. #define MSerial4 Serial3
  67. #define MSerial5 Serial4
  68. #endif
  69. #if SERIAL_PORT == 0
  70. #error "SERIAL_PORT cannot be 0. (Port 0 does not exist.) Please update your configuration."
  71. #elif SERIAL_PORT == -1
  72. #define MYSERIAL0 UsbSerial
  73. #elif SERIAL_PORT == 1
  74. #define MYSERIAL0 MSerial1
  75. #elif SERIAL_PORT == 2
  76. #define MYSERIAL0 MSerial2
  77. #elif SERIAL_PORT == 3
  78. #define MYSERIAL0 MSerial3
  79. #elif SERIAL_PORT == 4
  80. #define MYSERIAL0 MSerial4
  81. #elif SERIAL_PORT == 5
  82. #define MYSERIAL0 MSerial5
  83. #else
  84. #error "SERIAL_PORT must be from -1 to 5. Please update your configuration."
  85. #endif
  86. #ifdef SERIAL_PORT_2
  87. #if SERIAL_PORT_2 == 0
  88. #error "SERIAL_PORT_2 cannot be 0. (Port 0 does not exist.) Please update your configuration."
  89. #elif SERIAL_PORT_2 == SERIAL_PORT
  90. #error "SERIAL_PORT_2 must be different than SERIAL_PORT. Please update your configuration."
  91. #elif SERIAL_PORT_2 == -1
  92. #define MYSERIAL1 UsbSerial
  93. #elif SERIAL_PORT_2 == 1
  94. #define MYSERIAL1 MSerial1
  95. #elif SERIAL_PORT_2 == 2
  96. #define MYSERIAL1 MSerial2
  97. #elif SERIAL_PORT_2 == 3
  98. #define MYSERIAL1 MSerial3
  99. #elif SERIAL_PORT_2 == 4
  100. #define MYSERIAL1 MSerial4
  101. #elif SERIAL_PORT_2 == 5
  102. #define MYSERIAL1 MSerial5
  103. #else
  104. #error "SERIAL_PORT_2 must be from -1 to 5. Please update your configuration."
  105. #endif
  106. #define NUM_SERIAL 2
  107. #else
  108. #define NUM_SERIAL 1
  109. #endif
  110. #ifdef DGUS_SERIAL
  111. #if DGUS_SERIAL_PORT == 0
  112. #error "DGUS_SERIAL_PORT cannot be 0. (Port 0 does not exist.) Please update your configuration."
  113. #elif DGUS_SERIAL_PORT == SERIAL_PORT
  114. #error "DGUS_SERIAL_PORT must be different than SERIAL_PORT. Please update your configuration."
  115. #elif defined(SERIAL_PORT_2) && DGUS_SERIAL_PORT == SERIAL_PORT_2
  116. #error "DGUS_SERIAL_PORT must be different than SERIAL_PORT_2. Please update your configuration."
  117. #elif DGUS_SERIAL_PORT == -1
  118. #define DGUS_SERIAL UsbSerial
  119. #elif DGUS_SERIAL_PORT == 1
  120. #define DGUS_SERIAL MSerial1
  121. #elif DGUS_SERIAL_PORT == 2
  122. #define DGUS_SERIAL MSerial2
  123. #elif DGUS_SERIAL_PORT == 3
  124. #define DGUS_SERIAL MSerial3
  125. #elif DGUS_SERIAL_PORT == 4
  126. #define DGUS_SERIAL MSerial4
  127. #elif DGUS_SERIAL_PORT == 5
  128. #define DGUS_SERIAL MSerial5
  129. #else
  130. #error "DGUS_SERIAL_PORT must be from -1 to 5. Please update your configuration."
  131. #endif
  132. #endif
  133. // Set interrupt grouping for this MCU
  134. void HAL_init();
  135. #define HAL_IDLETASK 1
  136. void HAL_idletask();
  137. /**
  138. * TODO: review this to return 1 for pins that are not analog input
  139. */
  140. #ifndef analogInputToDigitalPin
  141. #define analogInputToDigitalPin(p) (p)
  142. #endif
  143. #ifndef digitalPinHasPWM
  144. #define digitalPinHasPWM(P) (PIN_MAP[P].timer_device != nullptr)
  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. // EEPROM
  211. //
  212. /**
  213. * TODO: Write all this EEPROM stuff. Can emulate EEPROM in flash as last resort.
  214. * Wire library should work for i2c EEPROMs.
  215. */
  216. void eeprom_write_byte(uint8_t *pos, unsigned char value);
  217. uint8_t eeprom_read_byte(uint8_t *pos);
  218. void eeprom_read_block(void *__dst, const void *__src, size_t __n);
  219. void eeprom_update_block(const void *__src, void *__dst, size_t __n);
  220. //
  221. // ADC
  222. //
  223. #define HAL_ANALOG_SELECT(pin) pinMode(pin, INPUT_ANALOG);
  224. void HAL_adc_init();
  225. #define HAL_START_ADC(pin) HAL_adc_start_conversion(pin)
  226. #define HAL_ADC_RESOLUTION 10
  227. #define HAL_READ_ADC() HAL_adc_result
  228. #define HAL_ADC_READY() true
  229. void HAL_adc_start_conversion(const uint8_t adc_pin);
  230. uint16_t HAL_adc_get_result();
  231. uint16_t analogRead(pin_t pin); // need HAL_ANALOG_SELECT() first
  232. void analogWrite(pin_t pin, int pwm_val8); // PWM only! mul by 257 in maple!?
  233. #define GET_PIN_MAP_PIN(index) index
  234. #define GET_PIN_MAP_INDEX(pin) pin
  235. #define PARSED_PIN_INDEX(code, dval) parser.intval(code, dval)
  236. #define JTAG_DISABLE() afio_cfg_debug_ports(AFIO_DEBUG_SW_ONLY)
  237. #define JTAGSWD_DISABLE() afio_cfg_debug_ports(AFIO_DEBUG_NONE)
  238. #define PLATFORM_M997_SUPPORT
  239. void flashFirmware(int16_t value);