misc.c 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  1. /*
  2. * Copyright (c) 2009 Openmoko Inc.
  3. *
  4. * Authors Daniel Mack <daniel@caiaq.de>
  5. *
  6. * This program is free software: you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License as published by
  8. * the Free Software Foundation, either version 3 of the License, or
  9. * (at your option) any later version.
  10. * This program is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU General Public License for more details.
  14. * You should have received a copy of the GNU General Public License
  15. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  16. */
  17. #include "regs.h"
  18. #include "types.h"
  19. #include "samo.h"
  20. #include "misc.h"
  21. int serial_input_available(void) {
  22. return (0 != (REG_EFSIF0_STATUS & RDBFx));
  23. }
  24. int serial_input_char(void)
  25. {
  26. while (!serial_input_available()) {
  27. }
  28. return(REG_EFSIF0_RXD);
  29. }
  30. #define WAIT_FOR_EFSIF0_RDY() \
  31. do { \
  32. } while (0 == (REG_EFSIF0_STATUS & TDBEx))
  33. int print_char(int c)
  34. {
  35. if (c == '\n') {
  36. WAIT_FOR_EFSIF0_RDY();
  37. REG_EFSIF0_TXD = '\r';
  38. }
  39. WAIT_FOR_EFSIF0_RDY();
  40. REG_EFSIF0_TXD = c;
  41. return 0;
  42. }
  43. void print(const char *txt)
  44. {
  45. while (txt && *txt) {
  46. print_char(*txt++);
  47. }
  48. }
  49. static void print_nibble(uint8_t nib)
  50. {
  51. nib &= 0x0f;
  52. if (nib >= 10)
  53. print_char(nib - 10 + 'a');
  54. else
  55. print_char(nib + '0');
  56. }
  57. void print_byte(uint8_t byte)
  58. {
  59. print_nibble(byte >> 4);
  60. print_nibble(byte);
  61. }
  62. void hex_dump(const void *buffer, uint32_t size)
  63. {
  64. int i, l;
  65. char a[2] = "X";
  66. const uint8_t *buf = (const uint8_t *)buffer;
  67. for (l = 0; l < size; l += 16) {
  68. print_byte(l >> 24);
  69. print_byte(l >> 16);
  70. print_byte(l >> 8);
  71. print_byte(l);
  72. print(" ");
  73. for (i = 0; i < 16; i++) {
  74. if (l + i < size) {
  75. print_byte(buf[l + i]);
  76. print(" ");
  77. } else
  78. print(" ");
  79. }
  80. print(" |");
  81. for (i = 0; i < 16; i++) {
  82. if (l + i < size) {
  83. if (buf[l + i] >= ' ' && buf[l + i] <= '~')
  84. a[0] = buf[l + i];
  85. else
  86. a[0] = '.';
  87. } else
  88. a[0] = ' ';
  89. print(a);
  90. }
  91. print("|\n");
  92. }
  93. }
  94. void print_u32(uint32_t val)
  95. {
  96. print("0x");
  97. print_byte(val >> 24);
  98. print_byte(val >> 16);
  99. print_byte(val >> 8);
  100. print_byte(val);
  101. }
  102. void print_int32(int32_t value)
  103. {
  104. if (0 > value) {
  105. print("-");
  106. value = - value;
  107. }
  108. print_dec32(value);
  109. }
  110. void print_dec32(uint32_t value)
  111. {
  112. char c[33];
  113. int i;
  114. c[sizeof(c) - 1] = '\0';
  115. for (i = sizeof(c) - 2; i >= 0; --i) {
  116. c[i] = value % 10 + '0';
  117. value /= 10;
  118. }
  119. for (i = 0; '0' == c[i] && i < sizeof(c) - 2; ++i) {
  120. }
  121. print(&c[i]);
  122. }