debug.c 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  1. /*
  2. * CNC-remote-control
  3. * Debug interface
  4. *
  5. * Copyright (C) 2009-2016 Michael Buesch <m@bues.ch>
  6. *
  7. * This program is free software; you can redistribute it and/or
  8. * modify it under the terms of the GNU General Public License
  9. * version 2 as published by the Free Software Foundation.
  10. *
  11. * This program is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU General Public License for more details.
  15. */
  16. #include "debug.h"
  17. #include "main.h"
  18. #include "util.h"
  19. #include "uart.h"
  20. #include <stdio.h>
  21. static uint8_t dbg_ringbuf[256];
  22. static uint8_t dbg_ringbuf_in;
  23. static uint8_t dbg_ringbuf_out;
  24. static uint8_t dbg_ringbuf_used;
  25. uint8_t debug_ringbuf_count(void)
  26. {
  27. /* This is only approximate. Count may change at any time. */
  28. return ATOMIC_LOAD(dbg_ringbuf_used);
  29. }
  30. uint8_t debug_ringbuf_get(void *buf, uint8_t size)
  31. {
  32. uint8_t *outbuf = buf;
  33. uint8_t sreg, count = 0;
  34. sreg = irq_disable_save();
  35. for ( ; dbg_ringbuf_used && size; dbg_ringbuf_used--, size--, count++) {
  36. *outbuf++ = dbg_ringbuf[dbg_ringbuf_out];
  37. if (dbg_ringbuf_out >= ARRAY_SIZE(dbg_ringbuf) - 1)
  38. dbg_ringbuf_out = 0u;
  39. else
  40. dbg_ringbuf_out++;
  41. }
  42. irq_restore(sreg);
  43. return count;
  44. }
  45. static void debug_ringbuf_putchar(char c)
  46. {
  47. uint8_t sreg;
  48. uint16_t used;
  49. if (!devflag_is_set(DEVICE_FLG_USBLOGMSG))
  50. return;
  51. sreg = irq_disable_save();
  52. used = dbg_ringbuf_used;
  53. if (used < ARRAY_SIZE(dbg_ringbuf)) {
  54. dbg_ringbuf[dbg_ringbuf_in] = (uint8_t)c;
  55. if (dbg_ringbuf_in >= ARRAY_SIZE(dbg_ringbuf) - 1)
  56. dbg_ringbuf_in = 0u;
  57. else
  58. dbg_ringbuf_in++;
  59. dbg_ringbuf_used++;
  60. }
  61. irq_restore(sreg);
  62. }
  63. static void debug_putchar(char c)
  64. {
  65. uart_putchar(c);
  66. debug_ringbuf_putchar(c);
  67. }
  68. static int debug_stream_putchar(char c, FILE *stream)
  69. {
  70. debug_putchar(c);
  71. return 0;
  72. }
  73. static FILE debug_fstream = FDEV_SETUP_STREAM(debug_stream_putchar, NULL,
  74. _FDEV_SETUP_WRITE);
  75. void do_debug_printf(const char PROGPTR *fmt, ...)
  76. {
  77. va_list args;
  78. va_start(args, fmt);
  79. vfprintf_P(&debug_fstream, fmt, args);
  80. va_end(args);
  81. }
  82. void debug_dumpmem(const void *_mem, uint8_t size)
  83. {
  84. const uint8_t *mem = _mem;
  85. uint8_t i;
  86. if (!debug_enabled())
  87. return;
  88. if (!mem || !size)
  89. return;
  90. for (i = 0; i < size; i++) {
  91. if (i % 16 == 0) {
  92. if (i != 0)
  93. debug_putchar('\n');
  94. debug_printf("0x%02X: ", i);
  95. }
  96. if (i % 2 == 0)
  97. debug_putchar(' ');
  98. debug_printf("%02X", mem[i]);
  99. }
  100. debug_putchar('\n');
  101. }
  102. void debug_init(void)
  103. {
  104. uart_init();
  105. stdout = &debug_fstream;
  106. stderr = &debug_fstream;
  107. debug_printf("CNC control initializing\n");
  108. }