简体   繁体   English

C 中的 printf() function 的核心

[英]Core of printf() function in C

Is there a way to get to the core of printf?有没有办法进入 printf 的核心? I need an USB output as fast as possible, but printf() is very slow, so I want to get rid of all the formatting that is happening inside the function.我需要一个 USB output 尽快,但printf()非常慢,所以我想摆脱 function 内部发生的所有格式。 Where is printf declared? printf 在哪里声明? It is most likely in the stdout library, but where can I find it (I´m using visual studio code).它很可能在标准输出库中,但我在哪里可以找到它(我正在使用 Visual Studio 代码)。

Background (Working on Raspberry Pi Pico with SDK lib - but not relevant for this question):背景(使用 SDK 库在 Raspberry Pi Pico 上工作 - 但与此问题无关):

I want to read 16Bit Hex values from SPI1 and write them within an interrupt to USB in "realtime".我想从 SPI1 读取 16 位十六进制值,并在中断中“实时”将它们写入 USB。 So my program kinda looks like this:所以我的程序有点像这样:

Do nothing until SPI Interrupt triggers
Read data from SPI1
Write data to COM19 on PC via USB

The SPI data from the master is coming in at a rate of 4MHz and there is a gap of about 480ns between data words.来自主机的 SPI 数据以 4MHz 的速率输入,数据字之间的间隔约为 480ns。 One data packet is taking about 4,5us (measured from bit1 of word1 to bit1 of word2).一个数据包大约需要 4.5us(从 word1 的 bit1 到 word2 的 bit1 测量)。 Means the maximum time of the interrupt should not be greater than 4,5us.意味着中断的最大时间不应大于4,5us。 The interrupt function with printf() takes about 9us to process, so thats too slow and some data is not getting caught.带有printf()的中断 function 需要大约 9us 来处理,所以这太慢了,有些数据没有被捕获。 Write() is able to process within the time of one cycle and only takes about 2us. Write()能够在一个周期内处理完毕,只需要大约 2us。 The problem with it is, that the data is printed as ASCII on the COM Port (Putty).问题在于,数据在 COM 端口(腻子)上打印为 ASCII。 I disabled a lot of the SDK functions to optimize the runtime and I was wondering can I do the same with printf() ?我禁用了很多 SDK 函数来优化运行时,我想知道我可以对printf()做同样的事情吗? Must be possible to write directly into the TX register and send it to the PC, but I wasn´t able to find anything about this topic on my research.必须可以直接写入 TX 寄存器并将其发送到 PC,但我无法在我的研究中找到有关此主题的任何信息。

Either I need to speed up the printf() function, or find some alternative, because it is clearly slowing down my program.要么我需要加快printf() function,要么找到一些替代方案,因为它显然会减慢我的程序。

How's this for speed..这个速度怎么样。。

char *hex = "0123456789ABCDEF";
char buf[4];
buf[3] = hex[n >> 0 & 0xF];
buf[2] = hex[n >> 4 & 0xF];
buf[1] = hex[n >> 8 & 0xF];
buf[0] = hex[n >>12 & 0xF];
write( fd, buf, sizeof buf ); 

There's 'endianess' to consider, but can't imagine you can get faster than this...需要考虑“字节顺序”,但无法想象你能比这更快......

Since you have not specified all of your requirements and environment conditions, this list collects just ideas.由于您尚未指定所有要求和环境条件,因此此列表仅收集想法。 Some might work, others not.有些可能有效,有些则无效。 It all depends on conditions you did not provide in your question.这完全取决于您在问题中未提供的条件。 If some of the terms are new to you, use them as a starting point for research.如果某些术语对您来说是新的,请将它们用作研究的起点。

  1. Use one or more tables to convert from binary to ASCII, as others suggested.正如其他人建议的那样,使用一个或多个表从二进制转换为 ASCII。 This only speeds up the conversion.这只会加快转换速度。

  2. Don't transmit ASCII, send binary values.不要传输 ASCII,发送二进制值。 An ASCII hex value as you probably use needs at least 5 characters, 4 for the number and 1 separator.您可能使用的 ASCII 十六进制值至少需要 5 个字符,4 个用于数字和 1 个分隔符。 The same value as binary has just 2 bytes.与二进制相同的值只有 2 个字节。 Additionally, you don't need any conversion.此外,您不需要任何转换。

  3. Detach the acquirement of the data from the transmission.从传输中分离数据采集。 For example, you can use a circular buffer big enough to handle the average throughput.例如,您可以使用足够大的循环缓冲区来处理平均吞吐量。 Be aware that you have multiple threads then, design your software carefully.请注意,您有多个线程,请仔细设计您的软件。

In any case, make sure the receiving machine can handle the data rate.在任何情况下,请确保接收机器可以处理数据速率。 PCs are notoriously known to depend on load and even "sleep" time after time, because their OS is not a real-time operating system.众所周知,PC 依赖于负载,甚至一次又一次地“休眠”,因为它们的操作系统不是实时操作系统。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM