是可能写到控制台没有stdlibs? c / c ++ [英] is it possible write to console without stdlibs? c/c++
问题描述
我在arm微处理器上编程,并尝试通过UART调试打印语句。我不想添加 stdlibs
只是为了调试。有没有办法打印到控制台,不用 stdio.h
/ iostream.h
?我可以写我自己的 printf()
?
I am programming on an arm microprocessor and am trying to debug using print statements via UART. I do not want to add stdlibs
just for debugging. Is there a way to print to the console without stdio.h
/iostream.h
? Is it possible for me to write my own printf()
?
或者我可以使用DMA控制器并直接写入UART。但我想避免这是可能的。使用内置的测试函数echo或
remote loop-back我知道我正确配置了UART。
Alternatively I can do this using a DMA controller and writing to the UART directly. However I would like to avoid that is possible. Using the built in test function "echo" or "remote loop-back" I know I have the UART configured properly.
推荐答案
简单的答案:是的,完全可以做到你的两个解决方案。
Short answer: Yes, it's entirely possible to do both of your solutions.
如果您要支持所有的数据类型和格式,printf函数非常复杂。但是编写可以输出一个字符串或一个整数在几个不同的基础(大多数人只需要十进制和十六进制,但八进制可能只有添加另外3-4行代码一旦你有十进制和十六进制)不是很难。
The printf function is quite complex if you want to support all of the data types and formats. But it's not that hard to write something that can output a string or an integer in a few different bases (most people only need decimal and hex, but octal probably only adds another 3-4 lines of code once you have decimal and hex).
通常,printf是这样写的:
Typically, printf is written like this:
int printf(const char *fmt, ...)
{
int ret;
va_list args;
va_start(args, fmt)
ret = do_xprintf(outputfunc, NULL, fmt, args);
va_end(args);
return ret;
}
然后 do_xprintf()
对所有变体(printf,sprintf,fprintf等)执行所有的工作
And then the do_xprintf()
does all the hard work for all variants (printf, sprintf, fprintf, etc)
int do_xprintf(void (*outputfunc)(void *extra, char c), void *extra, const char *fmt, va_list args)
{
char *ptr = fmt;
while(1)
{
char c = *ptr++;
if (c == '%')
{
c = *ptr++; // Get next character from format string.
switch(c)
{
case 's':
char *str = va_arg(args, const char *);
while(*str)
{
count++;
outputfunc(extra, *str);
str++;
}
break;
case 'x':
base = 16;
goto output_number;
case 'd':
base = 10;
output_number:
int i = va_arg(args, int);
// magical code to output 'i' in 'base'.
break;
default:
count++;
outputfunc(extra, c);
break;
}
else
count++;
outputfunc(extra, c);
}
return count;
}
现在,您需要做的就是填入上面的几个位代码并写一个outputfunc()输出到你的串口。
Now, all you need to do is fill in a few bits of the above code and write an outputfunc() that outputs to your serial port.
注意这是粗略的草图,我确定代码中有一些错误 - 如果你想支持浮点或宽度,你将不得不工作多一点...
Note that this is rough sketch, and I'm sure there are some bugs in the code - and if you want to support floating point or "widths", you will have to work a bit more at it...
(注意额外的参数 - 输出到a FILE *
,这将是文件指针,对于 sprintf
,您可以传递缓冲区的结构,缓冲区,或类似的东西)
(Note on the extra parameter - for output to a FILE *
that would be the filepointer, for sprintf
, you can pass a structure for the buffer and position in the buffer, or something like that)
这篇关于是可能写到控制台没有stdlibs? c / c ++的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!