Preferred method for printf() style writes using character write function?

Go To Last Post
3 posts / 0 new
Author
Message
#1
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Goal:

To wrap printf() output to a specific character-put function to make use of string formatting, variable input and so forth.

 

Background:

 

I have implemented the USB CDC on an SAMD11 chip and want to be able to write variable data to the USB serial port. The USB CDC currently permits two functions udi_cdc_putc() which writes a single character, and udi_cdc_write_buf() which puts an entire string but must know the size.

 

If this were a c++ implementation I would likely create a new class and override the printf() virtual function to pass the data to udi_cdc_putc().

 

I am not familiar with how to accomplish something similar here. What method is the proffered method? I will need to format floating point variables and text primarily.

 

Possible Solutions:

 

  1. sprintf() to a char buffer, loop through until \0 or \n with udi_cdc_putc()
  2. Use a stream override similar to virtual function
  3. Something else??
Last Edited: Mon. Mar 9, 2020 - 09:15 AM
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Printf already implements what you want - you just need to provide specific char in/out functions for printf to use.. Read the docs on printf and the arm library you're using to find these functions.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

I have doubts that a SAMD11 has enough memory to do USB (bloated in ASF?), floating point (bloated for CM0), and printf() (bloated in newlib) all at the same time.

(OTOH, since sprintf and printf end up using the same underlying primitives, you can do a quick size check without worying too much about connected printf() to character IO.)