console.h 1.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  1. #ifndef CONSOLE_H
  2. #define CONSOLE_H
  3. #include "compiler.h"
  4. #include "io.h"
  5. #define CON_FLOW_CTL 1 /* Block on connected and output full? */
  6. void __fmt_printf(1,0) con_vprintf(const char *, va_list);
  7. void __fmt_printf(1,2) con_printf(const char *, ...);
  8. static __always_inline void __con_wait_tx_ready(void)
  9. {
  10. /*
  11. * If CON_FLOW_CTL is set, wait here if the TX buffer is above
  12. * the high index mark *if* we are connected to a host
  13. */
  14. while (CON_FLOW_CTL &&
  15. (CON_STATUS & (TTY_STATUS_TX_HIGH|TTY_STATUS_DTR_IN))
  16. == (TTY_STATUS_TX_HIGH|TTY_STATUS_DTR_IN))
  17. pause();
  18. }
  19. static __always_inline void __con_putc(char c)
  20. {
  21. if (c == '\n')
  22. CON_DATA = '\r';
  23. CON_DATA = c;
  24. }
  25. static __always_inline void _con_putc(char c)
  26. {
  27. __con_wait_tx_ready();
  28. __con_putc(c);
  29. }
  30. static __always_inline void _con_puts(const char *str)
  31. {
  32. char c;
  33. __con_wait_tx_ready();
  34. while ((c = *str++))
  35. __con_putc(c);
  36. }
  37. #if CON_FLOW_CTL
  38. void con_putc(char c);
  39. void con_puts(const char *);
  40. #else
  41. /* Simple enough to inline if we don't care about flow control */
  42. static inline void con_putc(char c)
  43. {
  44. _con_putc(c);
  45. }
  46. static inline con_puts(const char *str)
  47. {
  48. _con_puts(str);
  49. }
  50. #endif
  51. static __always_inline void con_flush(void)
  52. {
  53. while (CON_FLOW_CTL && !(CON_STATUS & TTY_STATUS_TX_EMPTY))
  54. pause();
  55. }
  56. #endif /* CONSOLE_H */