Teensy 4.1 core updated for 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.

94 line
2.1KB

  1. #include "debug/printf.h"
  2. #ifdef PRINT_DEBUG_STUFF
  3. #include <stdarg.h>
  4. #include "imxrt.h"
  5. void putchar_debug(char c);
  6. static void puint_debug(unsigned int num);
  7. __attribute__((section(".progmem")))
  8. 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. __attribute__((section(".progmem")))
  65. void putchar_debug(char c)
  66. {
  67. while (!(LPUART3_STAT & LPUART_STAT_TDRE)) ; // wait
  68. LPUART3_DATA = c;
  69. }
  70. __attribute__((section(".progmem")))
  71. void printf_debug_init(void)
  72. {
  73. CCM_CCGR0 |= CCM_CCGR0_LPUART3(CCM_CCGR_ON); // turn on Serial4
  74. IOMUXC_SW_MUX_CTL_PAD_GPIO_AD_B1_06 = 2; // Arduino pin 17
  75. LPUART3_BAUD = LPUART_BAUD_OSR(25) | LPUART_BAUD_SBR(8); // ~115200 baud
  76. LPUART3_CTRL = LPUART_CTRL_TE;
  77. }
  78. #endif // PRINT_DEBUG_STUFF