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.

UHS_macros.h 8.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230
  1. /* Copyright (C) 2015-2016 Andrew J. Kroll
  2. and
  3. Copyright (C) 2011 Circuits At Home, LTD. All rights reserved.
  4. This program is free software; you can redistribute it and/or modify
  5. it under the terms of the GNU General Public License as published by
  6. the Free Software Foundation; either version 2 of the License, or
  7. (at your option) any later version.
  8. This program is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. GNU General Public License for more details.
  12. You should have received a copy of the GNU General Public License
  13. along with this program; if not, write to the Free Software
  14. Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  15. Contact information
  16. -------------------
  17. Circuits At Home, LTD
  18. Web : https://www.circuitsathome.com
  19. e-mail : support@circuitsathome.com
  20. */
  21. #ifndef MACROS_H
  22. #define MACROS_H
  23. #include "macro_logic.h"
  24. /*
  25. * Universal Arduino(tm) "IDE" fixups.
  26. */
  27. // Just in case...
  28. #ifndef SERIAL_PORT_MONITOR
  29. #define SERIAL_PORT_MONITOR Serial
  30. #endif
  31. #ifndef INT16_MIN
  32. #define INT16_MIN -32768
  33. #endif
  34. // require 10607+
  35. #if defined(ARDUINO) && ARDUINO >=10607
  36. // nop :-)
  37. #else
  38. #error "Arduino version too old, and must be at least 1.6.7"
  39. #endif
  40. // Nuke screwed up macro junk from the IDE.
  41. #ifdef __cplusplus
  42. #ifdef true
  43. #undef true
  44. #endif
  45. #ifdef false
  46. #undef false
  47. #endif
  48. #endif
  49. #ifndef UHS_DEVICE_WINDOWS_USB_SPEC_VIOLATION_DESCRIPTOR_DEVICE
  50. #ifndef UHS_BIG_FLASH
  51. #if defined(FLASHEND) && defined(FLASHSTART)
  52. #if (FLASHEND - FLASHSTART) > 0x0FFFFU
  53. #define UHS_BIG_FLASH 1
  54. #else
  55. #define UHS_BIG_FLASH 0
  56. #endif
  57. #elif defined(__PIC32_FLASH_SIZE)
  58. #if __PIC32_FLASH_SIZE > 511
  59. #define UHS_BIG_FLASH 1
  60. #else
  61. #define UHS_BIG_FLASH 0
  62. #endif
  63. #elif defined(FLASHEND) && !defined(FLASHSTART)
  64. // Assumes flash starts at 0x00000, is this a safe assumption?
  65. // 192K + should be OK
  66. #if FLASHEND > 0x02FFFFU
  67. #define UHS_BIG_FLASH 1
  68. #else
  69. #define UHS_BIG_FLASH 0
  70. #endif
  71. #elif defined(IFLASH_SIZE)
  72. #if IFLASH_SIZE > 0x0FFFFU
  73. #define UHS_BIG_FLASH 1
  74. #else
  75. #define UHS_BIG_FLASH 0
  76. #endif
  77. #elif defined(ESP8266)
  78. #define UHS_BIG_FLASH 1
  79. #define SYSTEM_OR_SPECIAL_YIELD(...) yield()
  80. #elif defined(__arm__) && defined(CORE_TEENSY)
  81. #define UHS_BIG_FLASH 1
  82. #elif defined(ARDUINO_spresense_ast)
  83. #define UHS_BIG_FLASH 1
  84. #else
  85. // safe default
  86. #warning Small flash?
  87. #define UHS_BIG_FLASH 0
  88. #endif
  89. #endif
  90. #if UHS_BIG_FLASH
  91. #define UHS_DEVICE_WINDOWS_USB_SPEC_VIOLATION_DESCRIPTOR_DEVICE 1
  92. #else
  93. #define UHS_DEVICE_WINDOWS_USB_SPEC_VIOLATION_DESCRIPTOR_DEVICE 0
  94. #endif
  95. #endif
  96. #if defined(__arm__) && defined(CORE_TEENSY)
  97. #define UHS_PIN_WRITE(p, v) digitalWriteFast(p, v)
  98. #define UHS_PIN_READ(p) digitalReadFast(p)
  99. #endif
  100. // TODO: Fast inline code for AVR and SAM based microcontrollers
  101. // This can be done pretty easily.
  102. // For now, this will just work out-of-the-box.
  103. #ifndef UHS_PIN_WRITE
  104. #define UHS_PIN_WRITE(p, v) digitalWrite(p, v)
  105. #endif
  106. #ifndef UHS_PIN_READ
  107. #define UHS_PIN_READ(p) digitalRead(p)
  108. #endif
  109. #if defined( __PIC32MX__ ) && !defined(interrupts) // compiling with Microchip XC32 compiler
  110. #define interrupts() __builtin_enable_interrupts()
  111. #edfine noInterrupts() __builtin_disable_interrupts()
  112. #endif
  113. #ifndef ARDUINO_SAMD_ZERO
  114. #ifdef ARDUINO_AVR_ADK
  115. #define UHS_GET_DPI(x) (x == 54 ? 6 : digitalPinToInterrupt(x))
  116. #else
  117. #define UHS_GET_DPI(x) digitalPinToInterrupt(x)
  118. #endif
  119. #else
  120. #define UHS_GET_DPI(x) (x)
  121. #endif
  122. #include "../../../../HAL/shared/progmem.h"
  123. ////////////////////////////////////////////////////////////////////////////////
  124. // HANDY MACROS
  125. ////////////////////////////////////////////////////////////////////////////////
  126. // Atomically set/clear single bits using bitbands.
  127. // Believe it or not, this boils down to a constant,
  128. // and is less code than using |= &= operators.
  129. // Bonus, it makes code easier to read too.
  130. // Bitbanding is a wonderful thing.
  131. #define BITNR(i) (i&0x1?0:i&0x2?1:i&0x4?2:i&0x8?3:i&0x10?4:i&0x20?5:i&0x40?6:i&0x80?7:i&0x100?8:i&0x200?9:i&0x400?10:i&0x800?11:i&0x1000?12:i&0x2000?13:i&0x4000?14:i&0x8000?15:i&0x10000?16:i&0x20000?17:i&0x40000?18:i&0x80000?19:i&0x100000?20:i&0x200000?21:i&0x400000?22:i&0x800000?23:i&0x1000000?24:i&0x2000000?25:i&0x4000000?26:i&0x8000000?27:i&0x10000000?28:i&0x20000000?29:i&0x40000000?30:i&0x80000000?31:32)
  132. #define UHS_KIO_BITBAND_ADDR(r, i) (((uint32_t)&(r) - 0x40000000) * 32 + (i) * 4 + 0x42000000)
  133. #define UHS_KIO_SETBIT_ATOMIC(r, m) (*(uint32_t *)UHS_KIO_BITBAND_ADDR((r), BITNR((m)))) = 1
  134. #define UHS_KIO_CLRBIT_ATOMIC(r, m) (*(uint32_t *)UHS_KIO_BITBAND_ADDR((r), BITNR((m)))) = 0
  135. #define VALUE_BETWEEN(v,l,h) (((v)>(l)) && ((v)<(h)))
  136. #define VALUE_WITHIN(v,l,h) (((v)>=(l)) && ((v)<=(h)))
  137. #define output_pgm_message(wa,fp,mp,el) wa = &mp, fp((char *)pgm_read_pointer(wa), el)
  138. #define output_if_between(v,l,h,wa,fp,mp,el) if(VALUE_BETWEEN(v,l,h)) output_pgm_message(wa,fp,mp[v-(l+1)],el);
  139. #define UHS_SWAP_VALUES(a, b) (((a) ^= (b)), ((b) ^= (a)), ((a) ^= (b)))
  140. #ifndef __BYTE_GRABBING_DEFINED__
  141. #define __BYTE_GRABBING_DEFINED__ 1
  142. #ifdef BROKEN_OPTIMIZER_LITTLE_ENDIAN
  143. // Note: Use this if your compiler generates horrible assembler!
  144. #define UHS_UINT8_BYTE0(__usi__) (((uint8_t *)&(__usi__))[0])
  145. #define UHS_UINT8_BYTE1(__usi__) (((uint8_t *)&(__usi__))[1])
  146. #define UHS_UINT8_BYTE2(__usi__) (((uint8_t *)&(__usi__))[2])
  147. #define UHS_UINT8_BYTE3(__usi__) (((uint8_t *)&(__usi__))[3])
  148. #define UHS_UINT8_BYTE4(__usi__) (((uint8_t *)&(__usi__))[4])
  149. #define UHS_UINT8_BYTE5(__usi__) (((uint8_t *)&(__usi__))[5])
  150. #define UHS_UINT8_BYTE6(__usi__) (((uint8_t *)&(__usi__))[6])
  151. #define UHS_UINT8_BYTE7(__usi__) (((uint8_t *)&(__usi__))[7])
  152. #else
  153. // Note: The cast alone to uint8_t is actually enough.
  154. // GCC throws out the "& 0xFF", and the size is no different.
  155. // Some compilers need it.
  156. #define UHS_UINT8_BYTE0(__usi__) ((uint8_t)((__usi__) & 0xFF ))
  157. #define UHS_UINT8_BYTE1(__usi__) ((uint8_t)(((__usi__) >> 8) & 0xFF))
  158. #define UHS_UINT8_BYTE2(__usi__) ((uint8_t)(((__usi__) >> 16) & 0xFF))
  159. #define UHS_UINT8_BYTE3(__usi__) ((uint8_t)(((__usi__) >> 24) & 0xFF))
  160. #define UHS_UINT8_BYTE4(__usi__) ((uint8_t)(((__usi__) >> 32) & 0xFF))
  161. #define UHS_UINT8_BYTE5(__usi__) ((uint8_t)(((__usi__) >> 40) & 0xFF))
  162. #define UHS_UINT8_BYTE6(__usi__) ((uint8_t)(((__usi__) >> 48) & 0xFF))
  163. #define UHS_UINT8_BYTE7(__usi__) ((uint8_t)(((__usi__) >> 56) & 0xFF))
  164. #endif
  165. #define UHS_UINT16_SET_BYTE1(__usi__) ((uint16_t)(__usi__) << 8)
  166. #define UHS_UINT32_SET_BYTE1(__usi__) ((uint32_t)(__usi__) << 8)
  167. #define UHS_UINT64_SET_BYTE1(__usi__) ((uint64_t)(__usi__) << 8)
  168. #define UHS_UINT32_SET_BYTE2(__usi__) ((uint32_t)(__usi__) << 16)
  169. #define UHS_UINT64_SET_BYTE2(__usi__) ((uint64_t)(__usi__) << 16)
  170. #define UHS_UINT32_SET_BYTE3(__usi__) ((uint32_t)(__usi__) << 24)
  171. #define UHS_UINT64_SET_BYTE3(__usi__) ((uint64_t)(__usi__) << 24)
  172. #define UHS_UINT64_SET_BYTE4(__usi__) ((uint64_t)(__usi__) << 32)
  173. #define UHS_UINT64_SET_BYTE5(__usi__) ((uint64_t)(__usi__) << 40)
  174. #define UHS_UINT64_SET_BYTE6(__usi__) ((uint64_t)(__usi__) << 48)
  175. #define UHS_UINT64_SET_BYTE7(__usi__) ((uint64_t)(__usi__) << 56)
  176. // These are the smallest and fastest ways I have found so far in pure C/C++.
  177. #define UHS_BYTES_TO_UINT16(__usc1__,__usc0__) ((uint16_t)((uint16_t)(__usc0__) | (uint16_t)UHS_UINT16_SET_BYTE1(__usc1__)))
  178. #define UHS_BYTES_TO_UINT32(__usc3__,__usc2__,__usc1__,__usc0__) ((uint32_t)((uint32_t)(__usc0__) | UHS_UINT32_SET_BYTE1(__usc1__) | UHS_UINT32_SET_BYTE2(__usc2__) | UHS_UINT32_SET_BYTE3(__usc3__)))
  179. #define UHS_BYTES_TO_UINT64(__usc7__,__usc6__,__usc5__,__usc4__,__usc3__,__usc2__,__usc1__,__usc0__) ((uint64_t)((uint64_t)__usc0__ | UHS_UINT64_SET_BYTE1(__usc1__) | UHS_UINT64_SET_BYTE2(__usc2__) | UHS_UINT64_SET_BYTE3(__usc3__) | UHS_UINT64_SET_BYTE4(__usc4__) | UHS_UINT64_SET_BYTE5(__usc5__) | UHS_UINT64_SET_BYTE6(__usc6__) | UHS_UINT64_SET_BYTE7(__usc7__)))
  180. #endif
  181. /*
  182. * Debug macros.
  183. * Useful when porting from UHS2.
  184. * Do not use these for any new code.
  185. * Change to better debugging after port is completed.
  186. * Strings are stored in progmem (flash) instead of RAM.
  187. */
  188. #define USBTRACE1(s,l) (Notify(PSTR(s), l))
  189. #define USBTRACE(s) (USBTRACE1((s), 0x80)); USB_HOST_SERIAL.flush()
  190. #define USBTRACE3(s,r,l) (Notify(PSTR(s), l), D_PrintHex((r), l), Notify(PSTR("\r\n"), l))
  191. #define USBTRACE3X(s,r,l) (Notify(PSTR(s), l), D_PrintHex((r), l))
  192. #define USBTRACE2(s,r) (USBTRACE3((s),(r),0x80)); USB_HOST_SERIAL.flush()
  193. #define USBTRACE2X(s,r) (USBTRACE3X((s),(r),0x80)); USB_HOST_SERIAL.flush()
  194. #define VOID0 ((void)0)
  195. #ifndef NOTUSED
  196. #define NOTUSED(...) __VA_ARGS__ __attribute__((unused))
  197. #endif
  198. #endif /* MACROS_H */