My Marlin configs for Fabrikator Mini and CTC i3 Pro B
Você não pode selecionar mais de 25 tópicos Os tópicos devem começar com uma letra ou um número, podem incluir traços ('-') e podem ter até 35 caracteres.

HAL.h 7.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266
  1. /**
  2. * Marlin 3D Printer Firmware
  3. *
  4. * Copyright (c) 2021 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. *
  8. * This program is free software: you can redistribute it and/or modify
  9. * it under the terms of the GNU General Public License as published by
  10. * the Free Software Foundation, either version 3 of the License, or
  11. * (at your option) any later version.
  12. *
  13. * This program is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. * GNU General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU General Public License
  19. * along with this program. If not, see <https://www.gnu.org/licenses/>.
  20. *
  21. */
  22. #pragma once
  23. #include <stdint.h>
  24. #include <stdarg.h>
  25. #undef min
  26. #undef max
  27. #include <algorithm>
  28. #include "pinmapping.h"
  29. void _printf (const char *format, ...);
  30. void _putc(uint8_t c);
  31. uint8_t _getc();
  32. //arduino: Print.h
  33. #define DEC 10
  34. #define HEX 16
  35. #define OCT 8
  36. #define BIN 2
  37. //arduino: binary.h (weird defines)
  38. #define B01 1
  39. #define B10 2
  40. #include "../shared/Marduino.h"
  41. #include "../shared/math_32bit.h"
  42. #include "../shared/HAL_SPI.h"
  43. #include "fastio.h"
  44. #include "serial.h"
  45. // ------------------------
  46. // Defines
  47. // ------------------------
  48. #define CPU_32_BIT
  49. #define SHARED_SERVOS HAS_SERVOS // Use shared/servos.cpp
  50. #define F_CPU 100000000
  51. #define SystemCoreClock F_CPU
  52. #define CPU_ST7920_DELAY_1 600
  53. #define CPU_ST7920_DELAY_2 750
  54. #define CPU_ST7920_DELAY_3 750
  55. // ------------------------
  56. // Serial ports
  57. // ------------------------
  58. extern MSerialT serial_stream_0;
  59. extern MSerialT serial_stream_1;
  60. extern MSerialT serial_stream_2;
  61. extern MSerialT serial_stream_3;
  62. #define _MSERIAL(X) serial_stream_##X
  63. #define MSERIAL(X) _MSERIAL(X)
  64. #if WITHIN(SERIAL_PORT, 0, 3)
  65. #define MYSERIAL1 MSERIAL(SERIAL_PORT)
  66. #else
  67. #error "SERIAL_PORT must be from 0 to 3. Please update your configuration."
  68. #endif
  69. #ifdef SERIAL_PORT_2
  70. #if WITHIN(SERIAL_PORT_2, 0, 3)
  71. #define MYSERIAL2 MSERIAL(SERIAL_PORT_2)
  72. #else
  73. #error "SERIAL_PORT_2 must be from 0 to 3. Please update your configuration."
  74. #endif
  75. #endif
  76. #ifdef MMU2_SERIAL_PORT
  77. #if WITHIN(MMU2_SERIAL_PORT, 0, 3)
  78. #define MMU2_SERIAL MSERIAL(MMU2_SERIAL_PORT)
  79. #else
  80. #error "MMU2_SERIAL_PORT must be from 0 to 3. Please update your configuration."
  81. #endif
  82. #endif
  83. #ifdef LCD_SERIAL_PORT
  84. #if WITHIN(LCD_SERIAL_PORT, 0, 3)
  85. #define LCD_SERIAL MSERIAL(LCD_SERIAL_PORT)
  86. #else
  87. #error "LCD_SERIAL_PORT must be from 0 to 3. Please update your configuration."
  88. #endif
  89. #endif
  90. // ------------------------
  91. // Interrupts
  92. // ------------------------
  93. #define CRITICAL_SECTION_START()
  94. #define CRITICAL_SECTION_END()
  95. // ------------------------
  96. // ADC
  97. // ------------------------
  98. #define HAL_ADC_VREF 5.0
  99. #define HAL_ADC_RESOLUTION 10
  100. /* ---------------- Delay in cycles */
  101. #define DELAY_CYCLES(x) Kernel::delayCycles(x)
  102. #define SYSTEM_YIELD() Kernel::yield()
  103. // Maple Compatibility
  104. typedef void (*systickCallback_t)(void);
  105. void systick_attach_callback(systickCallback_t cb);
  106. extern volatile uint32_t systick_uptime_millis;
  107. // Marlin uses strstr in constexpr context, this is not supported, workaround by defining constexpr versions of the required functions.
  108. #define strstr(a, b) strstr_constexpr((a), (b))
  109. constexpr inline std::size_t strlen_constexpr(const char* str) {
  110. // https://github.com/gcc-mirror/gcc/blob/5c7634a0e5f202935aa6c11b6ea953b8bf80a00a/libstdc%2B%2B-v3/include/bits/char_traits.h#L329
  111. if (str != nullptr) {
  112. std::size_t i = 0;
  113. while (str[i] != '\0') ++i;
  114. return i;
  115. }
  116. return 0;
  117. }
  118. constexpr inline int strncmp_constexpr(const char* lhs, const char* rhs, std::size_t count) {
  119. // https://github.com/gcc-mirror/gcc/blob/13b9cbfc32fe3ac4c81c4dd9c42d141c8fb95db4/libstdc%2B%2B-v3/include/bits/char_traits.h#L655
  120. if (lhs == nullptr || rhs == nullptr)
  121. return rhs != nullptr ? -1 : 1;
  122. for (std::size_t i = 0; i < count; ++i)
  123. if (lhs[i] != rhs[i])
  124. return lhs[i] < rhs[i] ? -1 : 1;
  125. else if (lhs[i] == '\0')
  126. return 0;
  127. return 0;
  128. }
  129. constexpr inline const char* strstr_constexpr(const char* str, const char* target) {
  130. // https://github.com/freebsd/freebsd/blob/master/sys/libkern/strstr.c
  131. if (char c = target != nullptr ? *target++ : '\0'; c != '\0' && str != nullptr) {
  132. std::size_t len = strlen_constexpr(target);
  133. do {
  134. char sc = {};
  135. do {
  136. if ((sc = *str++) == '\0') return nullptr;
  137. } while (sc != c);
  138. } while (strncmp_constexpr(str, target, len) != 0);
  139. --str;
  140. }
  141. return str;
  142. }
  143. constexpr inline char* strstr_constexpr(char* str, const char* target) {
  144. // https://github.com/freebsd/freebsd/blob/master/sys/libkern/strstr.c
  145. if (char c = target != nullptr ? *target++ : '\0'; c != '\0' && str != nullptr) {
  146. std::size_t len = strlen_constexpr(target);
  147. do {
  148. char sc = {};
  149. do {
  150. if ((sc = *str++) == '\0') return nullptr;
  151. } while (sc != c);
  152. } while (strncmp_constexpr(str, target, len) != 0);
  153. --str;
  154. }
  155. return str;
  156. }
  157. // ------------------------
  158. // Free Memory Accessor
  159. // ------------------------
  160. #pragma GCC diagnostic push
  161. #if GCC_VERSION <= 50000
  162. #pragma GCC diagnostic ignored "-Wunused-function"
  163. #endif
  164. int freeMemory();
  165. #pragma GCC diagnostic pop
  166. // ------------------------
  167. // MarlinHAL Class
  168. // ------------------------
  169. class MarlinHAL {
  170. public:
  171. // Earliest possible init, before setup()
  172. MarlinHAL() {}
  173. // Watchdog
  174. static void watchdog_init() IF_DISABLED(USE_WATCHDOG, {});
  175. static void watchdog_refresh() IF_DISABLED(USE_WATCHDOG, {});
  176. static void init() {} // Called early in setup()
  177. static void init_board() {} // Called less early in setup()
  178. static void reboot(); // Restart the firmware from 0x0
  179. // Interrupts
  180. static bool isr_state() { return true; }
  181. static void isr_on() {}
  182. static void isr_off() {}
  183. static void delay_ms(const int ms) { _delay_ms(ms); }
  184. // Tasks, called from idle()
  185. static void idletask();
  186. // Reset
  187. static constexpr uint8_t reset_reason = RST_POWER_ON;
  188. static uint8_t get_reset_source() { return reset_reason; }
  189. static void clear_reset_source() {}
  190. // Free SRAM
  191. static int freeMemory() { return ::freeMemory(); }
  192. //
  193. // ADC Methods
  194. //
  195. static uint8_t active_ch;
  196. // Called by Temperature::init once at startup
  197. static void adc_init();
  198. // Called by Temperature::init for each sensor at startup
  199. static void adc_enable(const uint8_t ch);
  200. // Begin ADC sampling on the given channel. Called from Temperature::isr!
  201. static void adc_start(const uint8_t ch);
  202. // Is the ADC ready for reading?
  203. static bool adc_ready();
  204. // The current value of the ADC register
  205. static uint16_t adc_value();
  206. /**
  207. * Set the PWM duty cycle for the pin to the given value.
  208. * No option to invert the duty cycle [default = false]
  209. * No option to change the scale of the provided value to enable finer PWM duty control [default = 255]
  210. */
  211. static void set_pwm_duty(const pin_t pin, const uint16_t v, const uint16_t=255, const bool=false) {
  212. analogWrite(pin, v);
  213. }
  214. };