寄存器自动化测试说明及时使用
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

195 lines
3.9 KiB

  1. /*
  2. * uart.c
  3. */
  4. /*******************************************************/
  5. /* printf�������� */
  6. /*******************************************************/
  7. #include <stdio.h>
  8. #define SKYEYE 1
  9. void uart_putc (const char c)
  10. {
  11. if (SKYEYE)
  12. *(char *)0xd000 = c; /* write char to Transmit Buffer Register */
  13. else
  14. putc(c, stdout);
  15. return;
  16. }
  17. void uart_puts (const char *s)
  18. {
  19. while (*s)
  20. {
  21. uart_putc (*s++);
  22. }
  23. return;
  24. }
  25. int my_strlen(char *str)
  26. {
  27. int i;
  28. for(i = 0; str[i] != '\0'; i++);
  29. return i;
  30. }
  31. char *myitoa(int n, int hex)
  32. {
  33. int num, i = 0, k = 0;
  34. static char str[100];
  35. char temp;
  36. if(n < 0)
  37. {
  38. str[i++] = '-';
  39. n = -1 * n;
  40. }
  41. do
  42. {
  43. num = n;
  44. n = n / hex;
  45. if (num%hex > 9)
  46. str[i+k++] = 'a' + (num%hex - 0xa);
  47. else
  48. str[i + k++] = '0' + num % hex;
  49. }while(n);
  50. str[i + k] = '\0';
  51. k = my_strlen(str) - 1;
  52. for(;i < k; i++, k--)
  53. {
  54. temp = str[i];
  55. str[i] = str[k];
  56. str[k] = temp;
  57. }
  58. return str;
  59. }
  60. unsigned square(unsigned val, unsigned num){
  61. int i;
  62. unsigned ret = val;
  63. for(i = 0; i < num - 1; i++){
  64. ret = ret*val;
  65. }
  66. return ret;
  67. }
  68. char * ftoa(double val, int prec)
  69. {
  70. int sig = 0, index = 0, p, i, j;
  71. static char s[100] = {'\0'};
  72. char temp_ch;
  73. long val_long, num;
  74. double val_temp;
  75. float v;
  76. long val_float;
  77. if(val < 0){
  78. s[index++] = '-';
  79. val_long = ((long)val) * -1;
  80. val_temp = val * -1;
  81. p = 1;
  82. }else{
  83. val_long = (long)val;
  84. val_temp = val;
  85. p = 0;
  86. }
  87. do
  88. {
  89. num = val_long;
  90. val_long = val_long / 10;
  91. s[index++] = '0' + num % 10;
  92. }while(val_long);
  93. for(i = p, j = index - 1; i < j; i++, j--){
  94. temp_ch = s[i];
  95. s[i] = s[j];
  96. s[j] = temp_ch;
  97. }
  98. s[index++] = '.';
  99. p = index;
  100. v = (val_temp - (long)val_temp) * square(10, prec);
  101. val_float = (long)v;
  102. for(i = 0; i < prec; i++){
  103. num = val_float;
  104. val_float = val_float/10;
  105. s[index++] = '0' + num % 10;
  106. }
  107. for(i = p, j = index - 1; i < j; i++, j--){
  108. temp_ch = s[i];
  109. s[i] = s[j];
  110. s[j] = temp_ch;
  111. }
  112. s[index] = '\0';
  113. return s;
  114. }
  115. void uart_printf(const char *format, ...)
  116. {
  117. va_list ap;
  118. char c;
  119. va_start(ap ,format);
  120. while((c = *format++) > 0)
  121. {
  122. if(c == '%')
  123. {
  124. switch(c = *format++){
  125. case 'c':{
  126. char ch = va_arg(ap, int);
  127. uart_putc(ch);
  128. break;
  129. }
  130. case 's':{
  131. char *p = va_arg(ap, char*);
  132. uart_puts(p);
  133. break;
  134. }
  135. case '%':
  136. {
  137. uart_putc(c);
  138. break;
  139. }
  140. case 'd':
  141. {
  142. int num = va_arg(ap, int);
  143. char *p = myitoa(num, 10);
  144. uart_puts(p);
  145. break;
  146. }
  147. case 'x':
  148. {
  149. int num = va_arg(ap, int);
  150. char *p = myitoa(num, 16);
  151. uart_puts(p);
  152. break;
  153. }
  154. case 'f':
  155. {
  156. double fdata = va_arg(ap, double);
  157. char *str = ftoa(fdata, 6);
  158. uart_puts(str);
  159. break;
  160. }
  161. default:
  162. uart_putc(c);
  163. break;
  164. }
  165. }
  166. else if(c == '\\')
  167. {
  168. switch(c = *format++){
  169. case 'n':{
  170. uart_putc('\r');
  171. break;
  172. }
  173. default:
  174. uart_putc(c);
  175. break;
  176. }
  177. }
  178. else
  179. uart_putc(c);
  180. }
  181. va_end(ap);
  182. }