選択できるのは25トピックまでです。 トピックは、先頭が英数字で、英数字とダッシュ('-')を使用した35文字以内のものにしてください。

triceMcuReverse.h 4.2KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  1. /*! \file triceMcuReverse.h
  2. \author Thomas.Hoehenleitner [at] seerose.net
  3. *******************************************************************************/
  4. //! Note: https://gcc.gnu.org/onlinedocs/cpp/_005f_005fhas_005finclude.html
  5. #if defined __has_include //! __has_include is a macro defined in several compilers including GCC and integrated into the C23 standard (Note N2799)
  6. #if __has_include(<byteswap.h>)
  7. #include <byteswap.h> // https://codereview.stackexchange.com/questions/151049/endianness-conversion-in-c
  8. #define TRICE_LIBC_BYTESWAP 1
  9. #endif
  10. #endif
  11. // Swap a 16-bit integer (https://www.oryx-embedded.com/doc/cpu__endian_8h_source.html)
  12. TRICE_INLINE uint16_t TriceReverse16(uint16_t value) {
  13. #if (defined(__GNUC__) && (__GNUC__ * 100 + __GNUC_MINOR__ >= 403)) || (defined(__clang__) && __has_builtin(__builtin_bswap16))
  14. return __builtin_bswap16(value);
  15. #elif (defined(TRICE_LIBC_BYTESWAP) && TRICE_LIBC_BYTESWAP == 1)
  16. return __bswap_16(value);
  17. #else
  18. return (((value & 0x00FF) << 8) |
  19. ((value & 0xFF00) >> 8));
  20. #endif
  21. }
  22. // Swap a 32-bit integer (https://www.oryx-embedded.com/doc/cpu__endian_8h_source.html)
  23. TRICE_INLINE uint32_t TriceReverse32(uint32_t value) {
  24. #if (defined(__GNUC__) && (__GNUC__ * 100 + __GNUC_MINOR__ >= 403)) || (defined(__clang__) && __has_builtin(__builtin_bswap32))
  25. return __builtin_bswap32(value);
  26. #elif (defined(TRICE_LIBC_BYTESWAP) && TRICE_LIBC_BYTESWAP == 1)
  27. return __bswap_32(value);
  28. #else
  29. return (((value & 0x000000FF) << 24) |
  30. ((value & 0x0000FF00) << 8) |
  31. ((value & 0x00FF0000) >> 8) |
  32. ((value & 0xFF000000) >> 24));
  33. #endif
  34. }
  35. #if !defined(TRICE_HTOTS) && !defined(TRICE_HTOTL) && !defined(TRICE_TTOHS)
  36. #define TRICE_HTOTS(x) TriceReverse16(x) //!< TRICE_HTOTS reorders short values from host order into trice transfer order.
  37. #define TRICE_HTOTL(x) TriceReverse32(x) //!< TRICE_HTOTL reorders long values from host order x into trice transfer order.
  38. #define TRICE_TTOHS(x) TriceReverse16(x) //!< TRICE_TTOHS reorders short values from trice transfer order into host order.
  39. #endif
  40. #if TRICE_TRANSFER_ORDER_IS_BIG_ENDIAN == 0
  41. //! TRICE_PUT16_1616 writes a 16-bit value followed by a 32-bit value in 2 16-bit steps to avoid memory alignment hard fault.
  42. #define TRICE_PUT16_1616(x, ts) /* little endian */ \
  43. do { \
  44. uint16_t* p = (uint16_t*)TriceBufferWritePosition; \
  45. *p++ = TRICE_HTOTS(x); \
  46. *p++ = TRICE_HTOTS(ts); /* lo */ \
  47. *p++ = TRICE_HTOTS((ts) >> 16); /* hi */ \
  48. TriceBufferWritePosition = (uint32_t*)p; \
  49. } while (0)
  50. #define TRICE_PUT64(x) \
  51. TRICE_PUT(TRICE_HTOTL((uint32_t)(x))); \
  52. TRICE_PUT(TRICE_HTOTL((uint32_t)((uint64_t)(x) >> 32))); // little endian
  53. #else // #if TRICE_TRANSFER_ORDER_IS_BIG_ENDIAN == 0
  54. //! TRICE_PUT16_1616 writes a 16-bit value followed by a 32-bit value in 2 16-bit steps to avoid memory alignment hard fault.
  55. #define TRICE_PUT16_1616(x, ts) /* big endian */ \
  56. do { \
  57. uint16_t* p = (uint16_t*)TriceBufferWritePosition; \
  58. *p++ = TRICE_HTOTS(x); \
  59. *p++ = TRICE_HTOTS((ts) >> 16); /* hi */ \
  60. *p++ = TRICE_HTOTS(ts); /* lo */ \
  61. TriceBufferWritePosition = (uint32_t*)p; \
  62. } while (0)
  63. #define TRICE_PUT64(x) \
  64. TRICE_PUT(TRICE_HTOTL((uint64_t)(x) >> 32)); \
  65. TRICE_PUT(TRICE_HTOTL((uint32_t)(x))); // big endian
  66. #endif // #else // #if TRICE_TRANSFER_ORDER_IS_BIG_ENDIAN == 0
  67. #define idLH TRICE_HTOTS(0x4000 | (tid)) //!< idLH is the no-stamp tid, byte swapped to be used in TRICE_PUT, when TRICE_REVERSE == 1.
  68. #define IdLH TRICE_HTOTS(0x8000 | (tid)) //!< IdLH is the 16-bit-stamp tid, byte swapped to be used in TRICE_PUT, when TRICE_REVERSE == 1.
  69. #define IDLH TRICE_HTOTS(0xc000 | (tid)) //!< IDLH is the 32-bit-stamp tid, byte swapped to be used in TRICE_PUT, when TRICE_REVERSE == 1.
  70. #define tsL ((0x00ff & ts) << 8)
  71. #define tsH ((0xff00 & ts) >> 8)
  72. #define tsHH ((0xFF000000 & ts) >> 8)
  73. #define tsHL ((0x00FF0000 & ts) << 8)
  74. #define tsLH ((0x0000FF00 & ts) >> 8)
  75. #define tsLL ((0x000000FF & ts) << 8)