Du kan inte välja fler än 25 ämnen Ämnen måste starta med en bokstav eller siffra, kan innehålla bindestreck ('-') och vara max 35 tecken långa.

debugprintf.c 2.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. #include "debug/printf.h"
  2. #ifdef PRINT_DEBUG_STUFF
  3. #include "avr/pgmspace.h"
  4. #include <stdarg.h>
  5. #include "imxrt.h"
  6. #include "usb_desc.h"
  7. void putchar_debug(char c);
  8. static void puint_debug(unsigned int num);
  9. FLASHMEM void printf_debug(const char *format, ...)
  10. {
  11. va_list args;
  12. unsigned int val;
  13. int n;
  14. va_start(args, format);
  15. for (; *format != 0; format++) { // no-frills stand-alone printf
  16. if (*format == '%') {
  17. ++format;
  18. if (*format == '%') goto out;
  19. if (*format == '-') format++; // ignore size
  20. while (*format >= '0' && *format <= '9') format++; // ignore size
  21. if (*format == 'l') format++; // ignore long
  22. if (*format == '\0') break;
  23. if (*format == 's') {
  24. printf_debug((char *)va_arg(args, int));
  25. } else if (*format == 'd') {
  26. n = va_arg(args, int);
  27. if (n < 0) {
  28. n = -n;
  29. putchar_debug('-');
  30. }
  31. puint_debug(n);
  32. } else if (*format == 'u') {
  33. puint_debug(va_arg(args, unsigned int));
  34. } else if (*format == 'x' || *format == 'X') {
  35. val = va_arg(args, unsigned int);
  36. for (n=0; n < 8; n++) {
  37. unsigned int d = (val >> 28) & 15;
  38. putchar_debug((d < 10) ? d + '0' : d - 10 + 'A');
  39. val <<= 4;
  40. }
  41. } else if (*format == 'c' ) {
  42. putchar_debug((char)va_arg(args, int));
  43. }
  44. } else {
  45. out:
  46. if (*format == '\n') putchar_debug('\r');
  47. putchar_debug(*format);
  48. }
  49. }
  50. va_end(args);
  51. }
  52. static void puint_debug(unsigned int num)
  53. {
  54. char buf[12];
  55. unsigned int i = sizeof(buf)-2;
  56. buf[sizeof(buf)-1] = 0;
  57. while (1) {
  58. buf[i] = (num % 10) + '0';
  59. num /= 10;
  60. if (num == 0) break;
  61. i--;
  62. }
  63. printf_debug(buf + i);
  64. }
  65. // first is this normal Serial?
  66. #if defined(PRINT_DEBUG_USING_USB) && defined(CDC_STATUS_INTERFACE) && defined(CDC_DATA_INTERFACE)
  67. #include "usb_dev.h"
  68. #include "usb_serial.h"
  69. FLASHMEM void putchar_debug(char c)
  70. {
  71. usb_serial_putchar(c);
  72. }
  73. FLASHMEM void printf_debug_init(void) {}
  74. #elif defined(PRINT_DEBUG_USING_USB) && defined(SEREMU_INTERFACE) && !defined(CDC_STATUS_INTERFACE) && !defined(CDC_DATA_INTERFACE)
  75. #include "usb_dev.h"
  76. #include "usb_seremu.h"
  77. FLASHMEM void putchar_debug(char c)
  78. {
  79. usb_seremu_putchar(c);
  80. }
  81. FLASHMEM void printf_debug_init(void) {}
  82. #else
  83. FLASHMEM void putchar_debug(char c)
  84. {
  85. while (!(LPUART3_STAT & LPUART_STAT_TDRE)) ; // wait
  86. LPUART3_DATA = c;
  87. }
  88. FLASHMEM void printf_debug_init(void)
  89. {
  90. CCM_CCGR0 |= CCM_CCGR0_LPUART3(CCM_CCGR_ON); // turn on Serial4
  91. IOMUXC_SW_MUX_CTL_PAD_GPIO_AD_B1_06 = 2; // Arduino pin 17
  92. LPUART3_BAUD = LPUART_BAUD_OSR(25) | LPUART_BAUD_SBR(8); // ~115200 baud
  93. LPUART3_CTRL = LPUART_CTRL_TE;
  94. }
  95. #endif
  96. #endif // PRINT_DEBUG_STUFF