Selaa lähdekoodia

feat(common): add usb_hexdump for test

sakumisu 1 vuosi sitten
vanhempi
sitoutus
1efc29be5f
1 muutettua tiedostoa jossa 27 lisäystä ja 0 poistoa
  1. 27 0
      common/usb_log.h

+ 27 - 0
common/usb_log.h

@@ -82,4 +82,31 @@ void usb_assert(const char *filename, int linenum);
             usb_assert(__FILE__, __LINE__); \
             usb_assert(__FILE__, __LINE__); \
     } while (0)
     } while (0)
 
 
+#define ___is_print(ch) ((unsigned int)((ch) - ' ') < 127u - ' ')
+static inline void usb_hexdump(const void *ptr, uint32_t buflen)
+{
+    unsigned char *buf = (unsigned char *)ptr;
+    int i, j;
+
+    for (i = 0; i < buflen; i += 16) {
+        CONFIG_USB_PRINTF("%08X:", i);
+
+        for (j = 0; j < 16; j++)
+            if (i + j < buflen) {
+                if ((j % 8) == 0) {
+                    CONFIG_USB_PRINTF("  ");
+                }
+
+                CONFIG_USB_PRINTF("%02X ", buf[i + j]);
+            } else
+                CONFIG_USB_PRINTF("   ");
+        CONFIG_USB_PRINTF(" ");
+
+        for (j = 0; j < 16; j++)
+            if (i + j < buflen)
+                CONFIG_USB_PRINTF("%c", ___is_print(buf[i + j]) ? buf[i + j] : '.');
+        CONFIG_USB_PRINTF("\n");
+    }
+}
+
 #endif /* USB_LOG_H */
 #endif /* USB_LOG_H */