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

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