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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272
  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 MACROS_H
  23. #define 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. #ifndef M_PI
  70. #define M_PI 3.14159265358979323846
  71. #endif
  72. #define RADIANS(d) ((d)*M_PI/180.0)
  73. #define DEGREES(r) ((r)*180.0/M_PI)
  74. #define HYPOT2(x,y) (sq(x)+sq(y))
  75. #define CIRCLE_AREA(R) (M_PI * sq(R))
  76. #define CIRCLE_CIRC(R) (2.0 * M_PI * (R))
  77. #define SIGN(a) ((a>0)-(a<0))
  78. #define IS_POWER_OF_2(x) ((x) && !((x) & ((x) - 1)))
  79. // Macros to constrain values
  80. // Avoid double evaluation of arguments to NOMORE/NOLESS/LIMIT
  81. #undef NOMORE
  82. #undef NOLESS
  83. #undef LIMIT
  84. #ifdef __cplusplus
  85. // C++11 solution that is standards compliant.
  86. template <class V, class N> static inline constexpr void NOLESS(V& v, const N n) {
  87. if (v < n) v = n;
  88. }
  89. template <class V, class N> static inline constexpr void NOMORE(V& v, const N n) {
  90. if (v > n) v = n;
  91. }
  92. template <class V, class N1, class N2> static inline constexpr void LIMIT(V& v, const N1 n1, const N2 n2) {
  93. if (v < n1) v = n1;
  94. else if (v > n2) v = n2;
  95. }
  96. #else
  97. // Using GCC extensions, but Travis GCC version does not like it and gives
  98. // "error: statement-expressions are not allowed outside functions nor in template-argument lists"
  99. #define NOLESS(v, n) \
  100. do { \
  101. __typeof__(n) _n = (n); \
  102. if (v < _n) v = _n; \
  103. } while(0)
  104. #define NOMORE(v, n) \
  105. do { \
  106. __typeof__(n) _n = (n); \
  107. if (v > _n) v = _n; \
  108. } while(0)
  109. #define LIMIT(v, n1, n2) \
  110. do { \
  111. __typeof__(n1) _n1 = (n1); \
  112. __typeof__(n2) _n2 = (n2); \
  113. if (v < _n1) v = _n1; \
  114. else if (v > _n2) v = _n2; \
  115. } while(0)
  116. #endif
  117. // Macros to support option testing
  118. #define _CAT(a, ...) a ## __VA_ARGS__
  119. #define SWITCH_ENABLED_false 0
  120. #define SWITCH_ENABLED_true 1
  121. #define SWITCH_ENABLED_0 0
  122. #define SWITCH_ENABLED_1 1
  123. #define SWITCH_ENABLED_0x0 0
  124. #define SWITCH_ENABLED_0x1 1
  125. #define SWITCH_ENABLED_ 1
  126. #define ENABLED(b) _CAT(SWITCH_ENABLED_, b)
  127. #define DISABLED(b) !ENABLED(b)
  128. #define WITHIN(V,L,H) ((V) >= (L) && (V) <= (H))
  129. #define NUMERIC(a) WITHIN(a, '0', '9')
  130. #define DECIMAL(a) (NUMERIC(a) || a == '.')
  131. #define NUMERIC_SIGNED(a) (NUMERIC(a) || (a) == '-' || (a) == '+')
  132. #define DECIMAL_SIGNED(a) (DECIMAL(a) || (a) == '-' || (a) == '+')
  133. #define COUNT(a) (sizeof(a)/sizeof(*a))
  134. #define ZERO(a) memset(a,0,sizeof(a))
  135. #define COPY(a,b) memcpy(a,b,MIN(sizeof(a),sizeof(b)))
  136. // Macros for initializing arrays
  137. #define ARRAY_6(v1, v2, v3, v4, v5, v6, ...) { v1, v2, v3, v4, v5, v6 }
  138. #define ARRAY_5(v1, v2, v3, v4, v5, ...) { v1, v2, v3, v4, v5 }
  139. #define ARRAY_4(v1, v2, v3, v4, ...) { v1, v2, v3, v4 }
  140. #define ARRAY_3(v1, v2, v3, ...) { v1, v2, v3 }
  141. #define ARRAY_2(v1, v2, ...) { v1, v2 }
  142. #define ARRAY_1(v1, ...) { v1 }
  143. #define _ARRAY_N(N, ...) ARRAY_ ##N(__VA_ARGS__)
  144. #define ARRAY_N(N, ...) _ARRAY_N(N, __VA_ARGS__)
  145. // Macros for adding
  146. #define INC_0 1
  147. #define INC_1 2
  148. #define INC_2 3
  149. #define INC_3 4
  150. #define INC_4 5
  151. #define INC_5 6
  152. #define INC_6 7
  153. #define INC_7 8
  154. #define INC_8 9
  155. #define INCREMENT_(n) INC_ ##n
  156. #define INCREMENT(n) INCREMENT_(n)
  157. // Macros for subtracting
  158. #define DEC_1 0
  159. #define DEC_2 1
  160. #define DEC_3 2
  161. #define DEC_4 3
  162. #define DEC_5 4
  163. #define DEC_6 5
  164. #define DEC_7 6
  165. #define DEC_8 7
  166. #define DEC_9 8
  167. #define DECREMENT_(n) DEC_ ##n
  168. #define DECREMENT(n) DECREMENT_(n)
  169. #define PIN_EXISTS(PN) (defined(PN ##_PIN) && PN ##_PIN >= 0)
  170. #define PENDING(NOW,SOON) ((long)(NOW-(SOON))<0)
  171. #define ELAPSED(NOW,SOON) (!PENDING(NOW,SOON))
  172. #define MMM_TO_MMS(MM_M) ((MM_M)/60.0)
  173. #define MMS_TO_MMM(MM_S) ((MM_S)*60.0)
  174. #define NOOP do{} while(0)
  175. #define CEILING(x,y) (((x) + (y) - 1) / (y))
  176. // Avoid double evaluation of arguments on MIN/MAX/ABS
  177. #undef MIN
  178. #undef MAX
  179. #undef ABS
  180. #ifdef __cplusplus
  181. // C++11 solution that is standards compliant. Return type is deduced automatically
  182. template <class L, class R> static inline constexpr auto MIN(const L lhs, const R rhs) -> decltype(lhs + rhs) {
  183. return lhs < rhs ? lhs : rhs;
  184. }
  185. template <class L, class R> static inline constexpr auto MAX(const L lhs, const R rhs) -> decltype(lhs + rhs){
  186. return lhs > rhs ? lhs : rhs;
  187. }
  188. template <class T> static inline constexpr const T ABS(const T v) {
  189. return v >= 0 ? v : -v;
  190. }
  191. #else
  192. // Using GCC extensions, but Travis GCC version does not like it and gives
  193. // "error: statement-expressions are not allowed outside functions nor in template-argument lists"
  194. #define MIN(a, b) \
  195. ({__typeof__(a) _a = (a); \
  196. __typeof__(b) _b = (b); \
  197. _a < _b ? _a : _b;})
  198. #define MAX(a, b) \
  199. ({__typeof__(a) _a = (a); \
  200. __typeof__(b) _b = (b); \
  201. _a > _b ? _a : _b;})
  202. #define ABS(a) \
  203. ({__typeof__(a) _a = (a); \
  204. _a >= 0 ? _a : -_a;})
  205. #endif
  206. #define MIN3(a, b, c) MIN(MIN(a, b), c)
  207. #define MIN4(a, b, c, d) MIN(MIN3(a, b, c), d)
  208. #define MIN5(a, b, c, d, e) MIN(MIN4(a, b, c, d), e)
  209. #define MAX3(a, b, c) MAX(MAX(a, b), c)
  210. #define MAX4(a, b, c, d) MAX(MAX3(a, b, c), d)
  211. #define MAX5(a, b, c, d, e) MAX(MAX4(a, b, c, d), e)
  212. #define UNEAR_ZERO(x) ((x) < 0.000001)
  213. #define NEAR_ZERO(x) WITHIN(x, -0.000001, 0.000001)
  214. #define NEAR(x,y) NEAR_ZERO((x)-(y))
  215. #define RECIPROCAL(x) (NEAR_ZERO(x) ? 0.0 : 1.0 / (x))
  216. #define FIXFLOAT(f) (f + (f < 0.0 ? -0.00005 : 0.00005))
  217. //
  218. // Maths macros that can be overridden by HAL
  219. //
  220. #define ATAN2(y, x) atan2(y, x)
  221. #define POW(x, y) pow(x, y)
  222. #define SQRT(x) sqrt(x)
  223. #define CEIL(x) ceil(x)
  224. #define FLOOR(x) floor(x)
  225. #define LROUND(x) lround(x)
  226. #define FMOD(x, y) fmod(x, y)
  227. #define HYPOT(x,y) SQRT(HYPOT2(x,y))
  228. #endif //__MACROS_H