console.c 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  1. /*
  2. * console.c
  3. *
  4. * Copyright (C) 2019 Sylvain Munaut
  5. * All rights reserved.
  6. *
  7. * LGPL v3+, see LICENSE.lgpl3
  8. *
  9. * This program is free software; you can redistribute it and/or
  10. * modify it under the terms of the GNU Lesser General Public
  11. * License as published by the Free Software Foundation; either
  12. * version 3 of the License, or (at your option) any later version.
  13. *
  14. * This program is distributed in the hope that it will be useful,
  15. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  16. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  17. * Lesser General Public License for more details.
  18. *
  19. * You should have received a copy of the GNU Lesser General Public License
  20. * along with this program; if not, write to the Free Software Foundation,
  21. * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
  22. */
  23. #include <stdint.h>
  24. #include "config.h"
  25. #include "mini-printf.h"
  26. struct wb_uart {
  27. uint32_t data;
  28. uint32_t clkdiv;
  29. } __attribute__((packed,aligned(4)));
  30. static volatile struct wb_uart * const uart_regs = (void*)(UART_BASE);
  31. static char _printf_buf[128];
  32. void console_init(void)
  33. {
  34. uart_regs->clkdiv = 22; /* 1 Mbaud with clk=24MHz */
  35. }
  36. char getchar(void)
  37. {
  38. int32_t c;
  39. do {
  40. c = uart_regs->data;
  41. } while (c & 0x80000000);
  42. return c;
  43. }
  44. int getchar_nowait(void)
  45. {
  46. int32_t c;
  47. c = uart_regs->data;
  48. return c & 0x80000000 ? -1 : (c & 0xff);
  49. }
  50. void putchar(char c)
  51. {
  52. uart_regs->data = c;
  53. }
  54. void puts(const char *p)
  55. {
  56. char c;
  57. while ((c = *(p++)) != 0x00) {
  58. if (c == '\n')
  59. uart_regs->data = '\r';
  60. uart_regs->data = c;
  61. }
  62. }
  63. int printf(const char *fmt, ...)
  64. {
  65. va_list va;
  66. int l;
  67. va_start(va, fmt);
  68. l = mini_vsnprintf(_printf_buf, 128, fmt, va);
  69. va_end(va);
  70. puts(_printf_buf);
  71. return l;
  72. }