console.c 1.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  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. CON_DATA = '\r';
  29. CON_DATA = c;
  30. }
  31. void con_puts(const char *str)
  32. {
  33. while (*str)
  34. con_putc(*str++);
  35. con_flush();
  36. }
  37. void con_vprintf(const char *fmt, va_list ap)
  38. {
  39. char buf[128]; /* Maximum text size */
  40. unsigned int len;
  41. const char *p;
  42. vsnprintf(buf, sizeof buf, fmt, ap);
  43. con_puts(buf);
  44. }
  45. void con_printf(const char *fmt, ...)
  46. {
  47. va_list ap;
  48. va_start(ap, fmt);
  49. con_vprintf(fmt, ap);
  50. va_end(ap);
  51. }