PlatformIO package of the Teensy core framework compatible with GCC 10 & C++20
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.

debugprintf.c 2.0KB

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