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.

macros.h 7.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273
  1. /**
  2. * Marlin 3D Printer Firmware
  3. * Copyright (C) 2016 MarlinFirmware [https://github.com/MarlinFirmware/Marlin]
  4. *
  5. * Based on Sprinter and grbl.
  6. * Copyright (C) 2011 Camiel Gubbels / Erik van der Zalm
  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 <http://www.gnu.org/licenses/>.
  20. *
  21. */
  22. #ifndef _CORE_MACROS_H_
  23. #define _CORE_MACROS_H_
  24. #define NUM_AXIS 4
  25. #define ABCE 4
  26. #define XYZE 4
  27. #define ABC 3
  28. #define XYZ 3
  29. #define _AXIS(A) (A##_AXIS)
  30. #define _XMIN_ 100
  31. #define _YMIN_ 200
  32. #define _ZMIN_ 300
  33. #define _XMAX_ 101
  34. #define _YMAX_ 201
  35. #define _ZMAX_ 301
  36. #define _FORCE_INLINE_ __attribute__((__always_inline__)) __inline__
  37. #define FORCE_INLINE __attribute__((always_inline)) inline
  38. #define _UNUSED __attribute__((unused))
  39. #define _O0 __attribute__((optimize("O0")))
  40. #define _Os __attribute__((optimize("Os")))
  41. #define _O1 __attribute__((optimize("O1")))
  42. #define _O2 __attribute__((optimize("O2")))
  43. #define _O3 __attribute__((optimize("O3")))
  44. // Clock speed factors
  45. #if !defined(CYCLES_PER_MICROSECOND) && !defined(__STM32F1__)
  46. #define CYCLES_PER_MICROSECOND (F_CPU / 1000000L) // 16 or 20 on AVR
  47. #endif
  48. // Nanoseconds per cycle
  49. #define NANOSECONDS_PER_CYCLE (1000000000.0 / F_CPU)
  50. // Remove compiler warning on an unused variable
  51. #define UNUSED(x) (void) (x)
  52. // Macros to make a string from a macro
  53. #define STRINGIFY_(M) #M
  54. #define STRINGIFY(M) STRINGIFY_(M)
  55. #define A(CODE) " " CODE "\n\t"
  56. #define L(CODE) CODE ":\n\t"
  57. // Macros for bit masks
  58. #undef _BV
  59. #define _BV(n) (1<<(n))
  60. #define TEST(n,b) !!((n)&_BV(b))
  61. #define SBI(n,b) (n |= _BV(b))
  62. #define CBI(n,b) (n &= ~_BV(b))
  63. #define SET_BIT_TO(N,B,TF) do{ if (TF) SBI(N,B); else CBI(N,B); }while(0)
  64. #define _BV32(b) (1UL << (b))
  65. #define TEST32(n,b) !!((n)&_BV32(b))
  66. #define SBI32(n,b) (n |= _BV32(b))
  67. #define CBI32(n,b) (n &= ~_BV32(b))
  68. // Macros for maths shortcuts
  69. #undef M_PI
  70. #define M_PI 3.14159265358979323846f
  71. #define RADIANS(d) ((d)*float(M_PI)/180.0f)
  72. #define DEGREES(r) ((r)*180.0f/float(M_PI))
  73. #define HYPOT2(x,y) (sq(x)+sq(y))
  74. #define CIRCLE_AREA(R) (float(M_PI) * sq(float(R)))
  75. #define CIRCLE_CIRC(R) (2 * float(M_PI) * float(R))
  76. #define SIGN(a) ((a>0)-(a<0))
  77. #define IS_POWER_OF_2(x) ((x) && !((x) & ((x) - 1)))
  78. // Macros to constrain values
  79. // Avoid double evaluation of arguments to NOMORE/NOLESS/LIMIT
  80. #undef NOMORE
  81. #undef NOLESS
  82. #undef LIMIT
  83. #ifdef __cplusplus
  84. // C++11 solution that is standards compliant.
  85. template <class V, class N> static inline constexpr void NOLESS(V& v, const N n) {
  86. if (v < n) v = n;
  87. }
  88. template <class V, class N> static inline constexpr void NOMORE(V& v, const N n) {
  89. if (v > n) v = n;
  90. }
  91. template <class V, class N1, class N2> static inline constexpr void LIMIT(V& v, const N1 n1, const N2 n2) {
  92. if (v < n1) v = n1;
  93. else if (v > n2) v = n2;
  94. }
  95. #else
  96. // Using GCC extensions, but Travis GCC version does not like it and gives
  97. // "error: statement-expressions are not allowed outside functions nor in template-argument lists"
  98. #define NOLESS(v, n) \
  99. do { \
  100. __typeof__(n) _n = (n); \
  101. if (v < _n) v = _n; \
  102. } while(0)
  103. #define NOMORE(v, n) \
  104. do { \
  105. __typeof__(n) _n = (n); \
  106. if (v > _n) v = _n; \
  107. } while(0)
  108. #define LIMIT(v, n1, n2) \
  109. do { \
  110. __typeof__(n1) _n1 = (n1); \
  111. __typeof__(n2) _n2 = (n2); \
  112. if (v < _n1) v = _n1; \
  113. else if (v > _n2) v = _n2; \
  114. } while(0)
  115. #endif
  116. // Macros to support option testing
  117. #define _CAT(a, ...) a ## __VA_ARGS__
  118. #define SWITCH_ENABLED_false 0
  119. #define SWITCH_ENABLED_true 1
  120. #define SWITCH_ENABLED_0 0
  121. #define SWITCH_ENABLED_1 1
  122. #define SWITCH_ENABLED_0x0 0
  123. #define SWITCH_ENABLED_0x1 1
  124. #define SWITCH_ENABLED_ 1
  125. #define ENABLED(b) _CAT(SWITCH_ENABLED_, b)
  126. #define DISABLED(b) !ENABLED(b)
  127. #define WITHIN(V,L,H) ((V) >= (L) && (V) <= (H))
  128. #define NUMERIC(a) WITHIN(a, '0', '9')
  129. #define DECIMAL(a) (NUMERIC(a) || a == '.')
  130. #define NUMERIC_SIGNED(a) (NUMERIC(a) || (a) == '-' || (a) == '+')
  131. #define DECIMAL_SIGNED(a) (DECIMAL(a) || (a) == '-' || (a) == '+')
  132. #define COUNT(a) (sizeof(a)/sizeof(*a))
  133. #define ZERO(a) memset(a,0,sizeof(a))
  134. #define COPY(a,b) memcpy(a,b,MIN(sizeof(a),sizeof(b)))
  135. // Macros for initializing arrays
  136. #define ARRAY_6(v1, v2, v3, v4, v5, v6, ...) { v1, v2, v3, v4, v5, v6 }
  137. #define ARRAY_5(v1, v2, v3, v4, v5, ...) { v1, v2, v3, v4, v5 }
  138. #define ARRAY_4(v1, v2, v3, v4, ...) { v1, v2, v3, v4 }
  139. #define ARRAY_3(v1, v2, v3, ...) { v1, v2, v3 }
  140. #define ARRAY_2(v1, v2, ...) { v1, v2 }
  141. #define ARRAY_1(v1, ...) { v1 }
  142. #define _ARRAY_N(N, ...) ARRAY_ ##N(__VA_ARGS__)
  143. #define ARRAY_N(N, ...) _ARRAY_N(N, __VA_ARGS__)
  144. // Macros for adding
  145. #define INC_0 1
  146. #define INC_1 2
  147. #define INC_2 3
  148. #define INC_3 4
  149. #define INC_4 5
  150. #define INC_5 6
  151. #define INC_6 7
  152. #define INC_7 8
  153. #define INC_8 9
  154. #define INCREMENT_(n) INC_ ##n
  155. #define INCREMENT(n) INCREMENT_(n)
  156. // Macros for subtracting
  157. #define DEC_1 0
  158. #define DEC_2 1
  159. #define DEC_3 2
  160. #define DEC_4 3
  161. #define DEC_5 4
  162. #define DEC_6 5
  163. #define DEC_7 6
  164. #define DEC_8 7
  165. #define DEC_9 8
  166. #define DECREMENT_(n) DEC_ ##n
  167. #define DECREMENT(n) DECREMENT_(n)
  168. #define PIN_EXISTS(PN) (defined(PN ##_PIN) && PN ##_PIN >= 0)
  169. #define PENDING(NOW,SOON) ((long)(NOW-(SOON))<0)
  170. #define ELAPSED(NOW,SOON) (!PENDING(NOW,SOON))
  171. #define MMM_TO_MMS(MM_M) ((MM_M)/60.0f)
  172. #define MMS_TO_MMM(MM_S) ((MM_S)*60.0f)
  173. #define NOOP do{} while(0)
  174. #define CEILING(x,y) (((x) + (y) - 1) / (y))
  175. // Avoid double evaluation of arguments on MIN/MAX/ABS
  176. #undef MIN
  177. #undef MAX
  178. #undef ABS
  179. #ifdef __cplusplus
  180. // C++11 solution that is standards compliant. Return type is deduced automatically
  181. template <class L, class R> static inline constexpr auto MIN(const L lhs, const R rhs) -> decltype(lhs + rhs) {
  182. return lhs < rhs ? lhs : rhs;
  183. }
  184. template <class L, class R> static inline constexpr auto MAX(const L lhs, const R rhs) -> decltype(lhs + rhs){
  185. return lhs > rhs ? lhs : rhs;
  186. }
  187. template <class T> static inline constexpr const T ABS(const T v) {
  188. return v >= 0 ? v : -v;
  189. }
  190. #else
  191. // Using GCC extensions, but Travis GCC version does not like it and gives
  192. // "error: statement-expressions are not allowed outside functions nor in template-argument lists"
  193. #define MIN(a, b) \
  194. ({__typeof__(a) _a = (a); \
  195. __typeof__(b) _b = (b); \
  196. _a < _b ? _a : _b;})
  197. #define MAX(a, b) \
  198. ({__typeof__(a) _a = (a); \
  199. __typeof__(b) _b = (b); \
  200. _a > _b ? _a : _b;})
  201. #define ABS(a) \
  202. ({__typeof__(a) _a = (a); \
  203. _a >= 0 ? _a : -_a;})
  204. #endif
  205. #define MIN3(a, b, c) MIN(MIN(a, b), c)
  206. #define MIN4(a, b, c, d) MIN(MIN3(a, b, c), d)
  207. #define MIN5(a, b, c, d, e) MIN(MIN4(a, b, c, d), e)
  208. #define MAX3(a, b, c) MAX(MAX(a, b), c)
  209. #define MAX4(a, b, c, d) MAX(MAX3(a, b, c), d)
  210. #define MAX5(a, b, c, d, e) MAX(MAX4(a, b, c, d), e)
  211. #define UNEAR_ZERO(x) ((x) < 0.000001f)
  212. #define NEAR_ZERO(x) WITHIN(x, -0.000001f, 0.000001f)
  213. #define NEAR(x,y) NEAR_ZERO((x)-(y))
  214. #define RECIPROCAL(x) (NEAR_ZERO(x) ? 0 : (1 / float(x)))
  215. #define FIXFLOAT(f) (f + (f < 0 ? -0.00005f : 0.00005f))
  216. //
  217. // Maths macros that can be overridden by HAL
  218. //
  219. #define ATAN2(y, x) atan2f(y, x)
  220. #define POW(x, y) powf(x, y)
  221. #define SQRT(x) sqrtf(x)
  222. #define RSQRT(x) (1 / sqrtf(x))
  223. #define CEIL(x) ceilf(x)
  224. #define FLOOR(x) floorf(x)
  225. #define LROUND(x) lroundf(x)
  226. #define FMOD(x, y) fmodf(x, y)
  227. #define HYPOT(x,y) SQRT(HYPOT2(x,y))
  228. #endif // _CORE_MACROS_H_