io.c 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  1. /*
  2. * io.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 "mini-printf.h"
  25. #define NULL ((void*)0)
  26. #define reg_uart_clkdiv (*(volatile uint32_t*)0x81000004)
  27. #define reg_uart_data (*(volatile uint32_t*)0x81000000)
  28. static char _printf_buf[128];
  29. void io_init(void)
  30. {
  31. reg_uart_clkdiv = 23; /* 1 Mbaud with clk=24MHz */
  32. }
  33. char getchar(void)
  34. {
  35. int32_t c;
  36. do {
  37. c = reg_uart_data;
  38. } while (c & 0x80000000);
  39. return c;
  40. }
  41. int getchar_nowait(void)
  42. {
  43. int32_t c;
  44. c = reg_uart_data;
  45. return c & 0x80000000 ? -1 : (c & 0xff);
  46. }
  47. void putchar(char c)
  48. {
  49. if (c == '\n')
  50. putchar('\r');
  51. reg_uart_data = c;
  52. }
  53. void puts(const char *p)
  54. {
  55. while (*p)
  56. putchar(*(p++));
  57. }
  58. int printf(const char *fmt, ...)
  59. {
  60. va_list va;
  61. int l;
  62. va_start(va, fmt);
  63. l = mini_vsnprintf(_printf_buf, 128, fmt, va);
  64. va_end(va);
  65. puts(_printf_buf);
  66. return l;
  67. }