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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226
  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 _XMIN_ 100
  30. #define _YMIN_ 200
  31. #define _ZMIN_ 300
  32. #define _XMAX_ 101
  33. #define _YMAX_ 201
  34. #define _ZMAX_ 301
  35. #define _FORCE_INLINE_ __attribute__((__always_inline__)) __inline__
  36. #define FORCE_INLINE __attribute__((always_inline)) inline
  37. #define _UNUSED __attribute__((unused))
  38. #define _O0 __attribute__((optimize("O0")))
  39. #define _Os __attribute__((optimize("Os")))
  40. #define _O1 __attribute__((optimize("O1")))
  41. #define _O2 __attribute__((optimize("O2")))
  42. #define _O3 __attribute__((optimize("O3")))
  43. // Clock speed factors
  44. #ifndef CYCLES_PER_MICROSECOND
  45. #define CYCLES_PER_MICROSECOND (F_CPU / 1000000L) // 16 or 20 on AVR
  46. #endif
  47. // Highly granular delays for step pulses, etc.
  48. #define DELAY_0_NOP NOOP
  49. #define DELAY_1_NOP __asm__("nop\n\t")
  50. #define DELAY_2_NOP DELAY_1_NOP; DELAY_1_NOP
  51. #define DELAY_3_NOP DELAY_1_NOP; DELAY_2_NOP
  52. #define DELAY_4_NOP DELAY_1_NOP; DELAY_3_NOP
  53. #define DELAY_5_NOP DELAY_1_NOP; DELAY_4_NOP
  54. #define DELAY_NOPS(X) \
  55. switch (X) { \
  56. case 20: DELAY_1_NOP; case 19: DELAY_1_NOP; \
  57. case 18: DELAY_1_NOP; case 17: DELAY_1_NOP; \
  58. case 16: DELAY_1_NOP; case 15: DELAY_1_NOP; \
  59. case 14: DELAY_1_NOP; case 13: DELAY_1_NOP; \
  60. case 12: DELAY_1_NOP; case 11: DELAY_1_NOP; \
  61. case 10: DELAY_1_NOP; case 9: DELAY_1_NOP; \
  62. case 8: DELAY_1_NOP; case 7: DELAY_1_NOP; \
  63. case 6: DELAY_1_NOP; case 5: DELAY_1_NOP; \
  64. case 4: DELAY_1_NOP; case 3: DELAY_1_NOP; \
  65. case 2: DELAY_1_NOP; case 1: DELAY_1_NOP; \
  66. }
  67. #define DELAY_10_NOP DELAY_5_NOP; DELAY_5_NOP
  68. #define DELAY_20_NOP DELAY_10_NOP; DELAY_10_NOP
  69. #if CYCLES_PER_MICROSECOND == 16
  70. #define DELAY_1US DELAY_10_NOP; DELAY_5_NOP; DELAY_1_NOP
  71. #else
  72. #define DELAY_1US DELAY_20_NOP
  73. #endif
  74. #define DELAY_2US DELAY_1US; DELAY_1US
  75. #define DELAY_3US DELAY_1US; DELAY_2US
  76. #define DELAY_4US DELAY_1US; DELAY_3US
  77. #define DELAY_5US DELAY_1US; DELAY_4US
  78. #define DELAY_6US DELAY_1US; DELAY_5US
  79. #define DELAY_7US DELAY_1US; DELAY_6US
  80. #define DELAY_8US DELAY_1US; DELAY_7US
  81. #define DELAY_9US DELAY_1US; DELAY_8US
  82. #define DELAY_10US DELAY_1US; DELAY_9US
  83. // Remove compiler warning on an unused variable
  84. #define UNUSED(x) (void) (x)
  85. // Macros to make a string from a macro
  86. #define STRINGIFY_(M) #M
  87. #define STRINGIFY(M) STRINGIFY_(M)
  88. // Macros for bit masks
  89. #undef _BV
  90. #define _BV(b) (1<<(b))
  91. #define TEST(n,b) !!((n)&_BV(b))
  92. #define SBI(n,b) (n |= _BV(b))
  93. #define CBI(n,b) (n &= ~_BV(b))
  94. #define _BV32(b) (1UL << (b))
  95. #define TEST32(n,b) !!((n)&_BV32(b))
  96. #define SBI32(n,b) (n |= _BV32(b))
  97. #define CBI32(n,b) (n &= ~_BV32(b))
  98. // Macros for maths shortcuts
  99. #ifndef M_PI
  100. #define M_PI 3.14159265358979323846
  101. #endif
  102. #define RADIANS(d) ((d)*M_PI/180.0)
  103. #define DEGREES(r) ((r)*180.0/M_PI)
  104. #define HYPOT2(x,y) (sq(x)+sq(y))
  105. #define CIRCLE_AREA(R) (M_PI * sq(R))
  106. #define CIRCLE_CIRC(R) (2.0 * M_PI * (R))
  107. #define SIGN(a) ((a>0)-(a<0))
  108. #define IS_POWER_OF_2(x) ((x) && !((x) & ((x) - 1)))
  109. // Macros to contrain values
  110. #define NOLESS(v,n) do{ if (v < n) v = n; }while(0)
  111. #define NOMORE(v,n) do{ if (v > n) v = n; }while(0)
  112. #define LIMIT(v,n1,n2) do{ if (v < n1) v = n1; else if (v > n2) v = n2; }while(0)
  113. // Macros to support option testing
  114. #define _CAT(a, ...) a ## __VA_ARGS__
  115. #define SWITCH_ENABLED_false 0
  116. #define SWITCH_ENABLED_true 1
  117. #define SWITCH_ENABLED_0 0
  118. #define SWITCH_ENABLED_1 1
  119. #define SWITCH_ENABLED_ 1
  120. #define ENABLED(b) _CAT(SWITCH_ENABLED_, b)
  121. #define DISABLED(b) (!_CAT(SWITCH_ENABLED_, b))
  122. #define WITHIN(V,L,H) ((V) >= (L) && (V) <= (H))
  123. #define NUMERIC(a) WITHIN(a, '0', '9')
  124. #define DECIMAL(a) (NUMERIC(a) || a == '.')
  125. #define NUMERIC_SIGNED(a) (NUMERIC(a) || (a) == '-' || (a) == '+')
  126. #define DECIMAL_SIGNED(a) (DECIMAL(a) || (a) == '-' || (a) == '+')
  127. #define COUNT(a) (sizeof(a)/sizeof(*a))
  128. #define ZERO(a) memset(a,0,sizeof(a))
  129. #define COPY(a,b) memcpy(a,b,min(sizeof(a),sizeof(b)))
  130. // Macros for initializing arrays
  131. #define ARRAY_6(v1, v2, v3, v4, v5, v6, ...) { v1, v2, v3, v4, v5, v6 }
  132. #define ARRAY_5(v1, v2, v3, v4, v5, ...) { v1, v2, v3, v4, v5 }
  133. #define ARRAY_4(v1, v2, v3, v4, ...) { v1, v2, v3, v4 }
  134. #define ARRAY_3(v1, v2, v3, ...) { v1, v2, v3 }
  135. #define ARRAY_2(v1, v2, ...) { v1, v2 }
  136. #define ARRAY_1(v1, ...) { v1 }
  137. #define _ARRAY_N(N, ...) ARRAY_ ##N(__VA_ARGS__)
  138. #define ARRAY_N(N, ...) _ARRAY_N(N, __VA_ARGS__)
  139. // Macros for adding
  140. #define INC_0 1
  141. #define INC_1 2
  142. #define INC_2 3
  143. #define INC_3 4
  144. #define INC_4 5
  145. #define INC_5 6
  146. #define INC_6 7
  147. #define INC_7 8
  148. #define INC_8 9
  149. #define INCREMENT_(n) INC_ ##n
  150. #define INCREMENT(n) INCREMENT_(n)
  151. // Macros for subtracting
  152. #define DEC_1 0
  153. #define DEC_2 1
  154. #define DEC_3 2
  155. #define DEC_4 3
  156. #define DEC_5 4
  157. #define DEC_6 5
  158. #define DEC_7 6
  159. #define DEC_8 7
  160. #define DEC_9 8
  161. #define DECREMENT_(n) DEC_ ##n
  162. #define DECREMENT(n) DECREMENT_(n)
  163. #define PIN_EXISTS(PN) (defined(PN ##_PIN) && PN ##_PIN >= 0)
  164. #define PENDING(NOW,SOON) ((long)(NOW-(SOON))<0)
  165. #define ELAPSED(NOW,SOON) (!PENDING(NOW,SOON))
  166. #define MMM_TO_MMS(MM_M) ((MM_M)/60.0)
  167. #define MMS_TO_MMM(MM_S) ((MM_S)*60.0)
  168. #define NOOP do{} while(0)
  169. #define CEILING(x,y) (((x) + (y) - 1) / (y))
  170. #define MIN3(a, b, c) min(min(a, b), c)
  171. #define MIN4(a, b, c, d) min(MIN3(a, b, c), d)
  172. #define MIN5(a, b, c, d, e) min(MIN4(a, b, c, d), e)
  173. #define MAX3(a, b, c) max(max(a, b), c)
  174. #define MAX4(a, b, c, d) max(MAX3(a, b, c), d)
  175. #define MAX5(a, b, c, d, e) max(MAX4(a, b, c, d), e)
  176. #define UNEAR_ZERO(x) ((x) < 0.000001)
  177. #define NEAR_ZERO(x) WITHIN(x, -0.000001, 0.000001)
  178. #define NEAR(x,y) NEAR_ZERO((x)-(y))
  179. #define RECIPROCAL(x) (NEAR_ZERO(x) ? 0.0 : 1.0 / (x))
  180. #define FIXFLOAT(f) (f + 0.00001)
  181. //
  182. // Maths macros that can be overridden by HAL
  183. //
  184. #define ATAN2(y, x) atan2(y, x)
  185. #define FABS(x) fabs(x)
  186. #define POW(x, y) pow(x, y)
  187. #define SQRT(x) sqrt(x)
  188. #define CEIL(x) ceil(x)
  189. #define FLOOR(x) floor(x)
  190. #define LROUND(x) lround(x)
  191. #define FMOD(x, y) fmod(x, y)
  192. #define HYPOT(x,y) SQRT(HYPOT2(x,y))
  193. #endif //__MACROS_H