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.
This repo is archived. You can view files and clone it, but cannot push or open issues/pull-requests.

macros.h 3.9KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  1. /* Copyright (C) 2011 Circuits At Home, LTD. All rights reserved.
  2. This software may be distributed and modified under the terms of the GNU
  3. General Public License version 2 (GPL2) as published by the Free Software
  4. Foundation and appearing in the file GPL2.TXT included in the packaging of
  5. this file. Please note that GPL2 Section 2[b] requires that all works based
  6. on this software must also be made publicly available under the terms of
  7. the GPL2 ("Copyleft").
  8. Contact information
  9. -------------------
  10. Circuits At Home, LTD
  11. Web : http://www.circuitsathome.com
  12. e-mail : [email protected]
  13. */
  14. #if !defined(_usb_h_) || defined(MACROS_H)
  15. #error "Never include macros.h directly; include Usb.h instead"
  16. #else
  17. #define MACROS_H
  18. ////////////////////////////////////////////////////////////////////////////////
  19. // HANDY MACROS
  20. ////////////////////////////////////////////////////////////////////////////////
  21. #define VALUE_BETWEEN(v,l,h) (((v)>(l)) && ((v)<(h)))
  22. #define VALUE_WITHIN(v,l,h) (((v)>=(l)) && ((v)<=(h)))
  23. #define output_pgm_message(wa,fp,mp,el) wa = &mp, fp((char *)pgm_read_pointer(wa), el)
  24. #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);
  25. #define SWAP(a, b) (((a) ^= (b)), ((b) ^= (a)), ((a) ^= (b)))
  26. #ifndef __BYTE_GRABBING_DEFINED__
  27. #define __BYTE_GRABBING_DEFINED__ 1
  28. #ifdef BROKEN_OPTIMIZER_LITTLE_ENDIAN
  29. // Note: Use this if your compiler generates horrible assembler!
  30. #define BGRAB0(__usi__) (((uint8_t *)&(__usi__))[0])
  31. #define BGRAB1(__usi__) (((uint8_t *)&(__usi__))[1])
  32. #define BGRAB2(__usi__) (((uint8_t *)&(__usi__))[2])
  33. #define BGRAB3(__usi__) (((uint8_t *)&(__usi__))[3])
  34. #define BGRAB4(__usi__) (((uint8_t *)&(__usi__))[4])
  35. #define BGRAB5(__usi__) (((uint8_t *)&(__usi__))[5])
  36. #define BGRAB6(__usi__) (((uint8_t *)&(__usi__))[6])
  37. #define BGRAB7(__usi__) (((uint8_t *)&(__usi__))[7])
  38. #else
  39. // Note: The cast alone to uint8_t is actually enough.
  40. // GCC throws out the "& 0xff", and the size is no different.
  41. // Some compilers need it.
  42. #define BGRAB0(__usi__) ((uint8_t)((__usi__) & 0xff ))
  43. #define BGRAB1(__usi__) ((uint8_t)(((__usi__) >> 8) & 0xff))
  44. #define BGRAB2(__usi__) ((uint8_t)(((__usi__) >> 16) & 0xff))
  45. #define BGRAB3(__usi__) ((uint8_t)(((__usi__) >> 24) & 0xff))
  46. #define BGRAB4(__usi__) ((uint8_t)(((__usi__) >> 32) & 0xff))
  47. #define BGRAB5(__usi__) ((uint8_t)(((__usi__) >> 40) & 0xff))
  48. #define BGRAB6(__usi__) ((uint8_t)(((__usi__) >> 48) & 0xff))
  49. #define BGRAB7(__usi__) ((uint8_t)(((__usi__) >> 56) & 0xff))
  50. #endif
  51. #define BOVER1(__usi__) ((uint16_t)(__usi__) << 8)
  52. #define BOVER2(__usi__) ((uint32_t)(__usi__) << 16)
  53. #define BOVER3(__usi__) ((uint32_t)(__usi__) << 24)
  54. #define BOVER4(__usi__) ((uint64_t)(__usi__) << 32)
  55. #define BOVER5(__usi__) ((uint64_t)(__usi__) << 40)
  56. #define BOVER6(__usi__) ((uint64_t)(__usi__) << 48)
  57. #define BOVER7(__usi__) ((uint64_t)(__usi__) << 56)
  58. // These are the smallest and fastest ways I have found so far in pure C/C++.
  59. #define BMAKE16(__usc1__,__usc0__) ((uint16_t)((uint16_t)(__usc0__) | (uint16_t)BOVER1(__usc1__)))
  60. #define BMAKE32(__usc3__,__usc2__,__usc1__,__usc0__) ((uint32_t)((uint32_t)(__usc0__) | (uint32_t)BOVER1(__usc1__) | (uint32_t)BOVER2(__usc2__) | (uint32_t)BOVER3(__usc3__)))
  61. #define BMAKE64(__usc7__,__usc6__,__usc5__,__usc4__,__usc3__,__usc2__,__usc1__,__usc0__) ((uint64_t)((uint64_t)__usc0__ | (uint64_t)BOVER1(__usc1__) | (uint64_t)BOVER2(__usc2__) | (uint64_t)BOVER3(__usc3__) | (uint64_t)BOVER4(__usc4__) | (uint64_t)BOVER5(__usc5__) | (uint64_t)BOVER6(__usc6__) | (uint64_t)BOVER1(__usc7__)))
  62. #endif
  63. /*
  64. * Debug macros: Strings are stored in progmem (flash) instead of RAM.
  65. */
  66. #define USBTRACE(s) (Notify(PSTR(s), 0x80))
  67. #define USBTRACE1(s,l) (Notify(PSTR(s), l))
  68. #define USBTRACE2(s,r) (Notify(PSTR(s), 0x80), D_PrintHex((r), 0x80), Notify(PSTR("\r\n"), 0x80))
  69. #define USBTRACE3(s,r,l) (Notify(PSTR(s), l), D_PrintHex((r), l), Notify(PSTR("\r\n"), l))
  70. #endif /* MACROS_H */