Formatted print to circular buffer -- xprintf

http://stackoverflow.com/questions/14926294/formatted-print-to-circular-buffer

 

I'm writing embedded code for STM32F3 mc (STM32F3-Discovery). I need to output some data to UART and I'm using DMA for this as this allows me to concentrate on sensors reading and data processing rather than on waiting for byte transmission completion. The issue however is that I have to combine:

  1. Formatted output (i.e. some from of printf)
  2. A number of consecutive prints (that occur before previous print has finished)

So I'm thinking about a circular buffer. But I don't think I know how to make sprintf to respect the end of buffer and continue writing to the beginning of the buffer. I can of course create another temporary buffer, print there, and copy byte-by-byte, but it doesn't look elegant to me.

One solution could be to implement your own sprintf which is able to work with ring buffers. Unfortunately this won't help you out regarding a more basic problem: What would you do if your ringbuffer is full and you invokesprintf?

If your memory situation can afford it, I'd suggest another solution for this problem:

The idea is based on two linked lists of buffers (one list for the free buffers, one list as transmit queue). The buffers are equally sized so they can store a worst case length string. The buffers build a simple heap where allocation/deallocation is only dequeuing/enqueuing an element either from the free or from the transmission list.

Having equally sized buffers guarantees you don't suffer from outer fragmentation effects like "checkerboarding" while allocating memory dynamically. Building your own heap for this job would also give you full control over the total buffer size available for transmission tasks.

I could imagine this running as follows:

  1. You allocate a buffer from the free list to render the data into.
  2. Use your render functions (suchas sprintf) to render the data in the buffer
  3. Append the data to be sent to the transmission queue (and trigger a transmission if necessary)

For the DMA transfer you handle the transfer end IRQ. There you move the buffer just transferred to the "free list" and setup the transfer for next buffer in the queue.

This solution won't be the memory efficientiest but the runtime efficiency is good as you only write to the memory once and the allocation/deallocation is only fetching/storing a pointer somewhere. Of course you'll have to make sure that you don't get race conditions between your application and the IRQ for allocation/deallocation.

Maybe this idea gives you an inspiration to solve your requirements.

One way you could approximate it is to allocate your printf buffer as 2x the size of your ringbuffer, and then use the first half as your ring buffer. Detect an overflow, copy the overflow in the latter half back to the front (the ring portion). Something like this:

 

复制代码
 1 void xprintf(const char *format, ...)
 2 {
 3   int written;
 4   va_list args;
 5   va_start(args, format);
 6   written = vsnprintf(buffer, HALF_BUFFER_SIZE, format, args);
 7   va_end(args);
8 if (buffer + written > bufferStart + HALF_BUFFER_SIZE) 9 { // time to wrap 10 int overflow = (buffer + written) - (bufferStart + HALF_BUFFER_SIZE); 11 memmove(bufferStart, bufferStart + HALF_BUFFER_SIZE, overflow; 12 buffer = bufferStart + overflow; 13 } 14 }
复制代码

 

posted @   IAmAProgrammer  阅读(376)  评论(0编辑  收藏  举报
(评论功能已被禁用)
编辑推荐:
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
· 开发者必知的日志记录最佳实践
· SQL Server 2025 AI相关能力初探
· Linux系列:如何用 C#调用 C方法造成内存泄露
· AI与.NET技术实操系列(二):开始使用ML.NET
阅读排行:
· 被坑几百块钱后,我竟然真的恢复了删除的微信聊天记录!
· 【自荐】一款简洁、开源的在线白板工具 Drawnix
· 没有Manus邀请码?试试免邀请码的MGX或者开源的OpenManus吧
· 园子的第一款AI主题卫衣上架——"HELLO! HOW CAN I ASSIST YOU TODAY
· 无需6万激活码!GitHub神秘组织3小时极速复刻Manus,手把手教你使用OpenManus搭建本
点击右上角即可分享
微信分享提示