2
0

console.c 1.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  1. #include <stddef.h>
  2. #include <stdint.h>
  3. #include <string.h>
  4. #include <stdio.h>
  5. #include <stdarg.h>
  6. #include "console.h"
  7. #include "io.h"
  8. void con_set_baudrate(uint32_t b)
  9. {
  10. uint32_t bauddiv;
  11. /*
  12. * Produce a CON_BAUD_BITS binary fraction. The +1 produces better
  13. * rounding behavior: see Hacker's Delight.
  14. */
  15. bauddiv = (b * ((1ULL << (32+CON_BAUD_BITS))/CON_BAUD_BASE+1)) >> 32;
  16. /*
  17. * Not really a divisor, but a fractional multiplier. The -1
  18. * is simply a technicality of the implementation.
  19. */
  20. CON_BAUDDIV = bauddiv - 1;
  21. }
  22. void con_putc(char c)
  23. {
  24. /* Wait for FIFO space */
  25. while (CON_STATUS & (1 << 4))
  26. pause();
  27. if (c == '\n')
  28. CONSOLE = '\r';
  29. CONSOLE = c;
  30. }
  31. void con_puts(const char *str)
  32. {
  33. while (*str)
  34. con_putc(*str++);
  35. }
  36. void con_vprintf(const char *fmt, va_list ap)
  37. {
  38. char buf[128]; /* Maximum text size */
  39. unsigned int len;
  40. const char *p;
  41. vsnprintf(buf, sizeof buf, fmt, ap);
  42. con_puts(buf);
  43. }
  44. void con_printf(const char *fmt, ...)
  45. {
  46. va_list ap;
  47. va_start(ap, fmt);
  48. con_vprintf(fmt, ap);
  49. va_end(ap);
  50. }