minimalistic printf library for UC3A / AVR32

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

Hi guys...

I am running out of flash space for one of the UC3A projects I am working on. Its a 256KB chip.

I am using ELF viewer to see what functions been taking up so much flash. It looks like _vfprintf_r is 8.086KB!!! Is there a smaller version of print library for UC3A? Next biggest is __svfscanf_r at 5.466KB.

 

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

luvocean1 wrote:
_vfprintf_r is 8.086KB!!!

worrying about 8K in a 256K chip seems like missing the wood for the trees?

 

So how much string formatting does your application actually do?

 

If it's not much, you could save by just writing the few formats you need; eg, print_decimal_int(); print_hex_byte(); etc, etc, ...

 

But if you're making extensive use of all the options - you're not going to be able to do much?

 

Do you use floating point?

 

luvocean1 wrote:
Next biggest is __svfscanf_r at 5.466KB

 

Again, if you're not using it for much, you could write a few specific routines.

 

 

Top Tips:

  1. How to properly post source code - see: https://www.avrfreaks.net/comment... - also how to properly include images/pictures
  2. "Garbage" characters on a serial terminal are (almost?) invariably due to wrong baud rate - see: https://learn.sparkfun.com/tutorials/serial-communication
  3. Wrong baud rate is usually due to not running at the speed you thought; check by blinking a LED to see if you get the speed you expected
  4. Difference between a crystal, and a crystal oscillatorhttps://www.avrfreaks.net/comment...
  5. When your question is resolved, mark the solution: https://www.avrfreaks.net/comment...
  6. Beginner's "Getting Started" tips: https://www.avrfreaks.net/comment...
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

I am using a fair bit of sprintf and printf in the program. So using the built in library is very convenient.

8K flash space is a lot when I am running out of 256K space!

I am using 1 or two instances of float printing. But I can do away with printing integer values and then printing multiplied integer values to simulate decimal.

Is there a way to cut down on the float printing from printf library?

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

Have you actually looked at what else is consuming space?

 

Concentrating on the "big users" first often yields bigger savings quicker than faffing about with small details.

 

What optimisation settings are you using?

 

 

luvocean1 wrote:
I am using a fair bit of sprintf and printf

The real question is how many different formats you use. 

 

If it is (or could be) just a limited set, then you could benefit by writing your own versions of just those formats.

 

But you can't have it both ways: lots of fancy formatting is bound to take lots of code!

 

 

I am using 1 or two instances of float printing.

You understand that it doesn't really matter how many instances you use: if you use it at all, then the code needs to be included; but, once you have used it once, using it a hundred times doesn't increase it!

 

EDIT - only the other day: https://www.avrfreaks.net/commen...

 

Does the chip have hardware floating-point? If not, that'll be adding some code for the software support ...

 

Is there a way to cut down on the float printing from printf library?

You'd have to RTFM for the toolchain you're using. Usually, the main option is to not have FP support !

Top Tips:

  1. How to properly post source code - see: https://www.avrfreaks.net/comment... - also how to properly include images/pictures
  2. "Garbage" characters on a serial terminal are (almost?) invariably due to wrong baud rate - see: https://learn.sparkfun.com/tutorials/serial-communication
  3. Wrong baud rate is usually due to not running at the speed you thought; check by blinking a LED to see if you get the speed you expected
  4. Difference between a crystal, and a crystal oscillatorhttps://www.avrfreaks.net/comment...
  5. When your question is resolved, mark the solution: https://www.avrfreaks.net/comment...
  6. Beginner's "Getting Started" tips: https://www.avrfreaks.net/comment...
Last Edited: Fri. Oct 9, 2020 - 01:01 PM
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

luvocean1 wrote:
8K flash space is a lot when I am running out of 256K space!

That 8K is almost irrelevant because you must replace it with "something" It's not like you can do without any of the functionality it provides.

 

In a program of that size I was making an assumption there. Where's the flash space going ? Do you have massive tables of constant data or is it all code ?

 

A Junior Engineer colleague of mine was in the same position as you find yourself in but with PIC24 which is already quite efficient in terms of using program space. It took me a week of investigation at assembly level but I was able to get him back over 40K by re-factoring the code. He was dead chuffed.

 

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

As it's GCC the source of the C lib is presumably open? If so I'd just lift the source for vfprintf(), remove code for any formats you don't use and you'll end up with your own cutdown one. As long as that implementation links first it should over-ride the weak link in the C lib.

 

EDIT: a quick google (which actually comes back to Freaks) suggests the C lib for AVR32 may be at:

 

https://github.com/embecosm?q=av...

 

Presumably the 4th one there for "newlib"? If so the stdio stuff looks like being at:

 

https://github.com/embecosm/avr3...

Last Edited: Fri. Oct 9, 2020 - 01:58 PM
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

N.Winterbottom wrote:
It's not like you can do without any of the functionality it provides

depends on how much of the functionality is actually being used.

 

also on how "granular" the particular library is: is it "all-or-nothing", or is it smart enough to only bring in required parts?

 

EDIT

 

I did a quick test, based on my test code from here: https://www.avrfreaks.net/forum/...

 

As-is, it builds to 720 bytes of code.

 

If I change the printing of the welcome message from

usart_txString( "Hello, ATmega328P XPlained Mini world\r\n" );

to

    char hello[40];
    sprintf( hello, "Hello, ATmega328P XPlained Mini world\r\n" );
    usart_txString(hello);

it goes up to 790 bytes - so the trivial use of sprintf adds very little code

 

If I change that to

    char hello[40];
    sprintf( hello, "Hello, ATmega328P XPlained Mini world at %ld baud\r\n",  MYUBRR );
    usart_txString(hello);

if jumps up to 2316 bytes - so that bit of formatting does bring in a significant code overhead

 

Yes, this is AVR8 - but I imagine AVR32 would be similar

 

EDIT 2

 

doing the formatting "manually":

    char hello[40];    
  
    usart_txString( "Hello, ATmega328P XPlained Mini world at " );
    ltoa( MYUBRR, hello, 10 );
    usart_txString(hello);
    usart_txString( " baud\r\n" );

takes it to 948 bytes

 

 

 

 

 

Top Tips:

  1. How to properly post source code - see: https://www.avrfreaks.net/comment... - also how to properly include images/pictures
  2. "Garbage" characters on a serial terminal are (almost?) invariably due to wrong baud rate - see: https://learn.sparkfun.com/tutorials/serial-communication
  3. Wrong baud rate is usually due to not running at the speed you thought; check by blinking a LED to see if you get the speed you expected
  4. Difference between a crystal, and a crystal oscillatorhttps://www.avrfreaks.net/comment...
  5. When your question is resolved, mark the solution: https://www.avrfreaks.net/comment...
  6. Beginner's "Getting Started" tips: https://www.avrfreaks.net/comment...
Last Edited: Fri. Oct 9, 2020 - 02:26 PM
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

luvocean1 wrote:
Is there a way to cut down on the float printing from printf library?
Move format functionality to post-processing or "telemetry" processing?

Application-Specific Data Elements | QTools: QS Target Component (Quantum Leaps)

...

QS_F32(0, 3.1415F);

Outputs a 32-bit float with format "%7.0e" (zero digits after the comma)

...

edit :

MSG_CONF_STREAM | Data Visualizer Software User's Guide

...

 

Table 1. STREAM_DATA_TYPE

...

Float
...

P.S.

A web application can operate on an arbitrary sequence.

Array | TypeScript: Handbook - Basic Types

 


qtools/qspy at master · QuantumLeaps/qtools · GitHub

 

edit2 :

AVR®: Setting up the Data Visualizer - Developer Help

 

"Dare to be naïve." - Buckminster Fuller

Last Edited: Fri. Oct 9, 2020 - 02:55 PM
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

N.Winterbottom wrote:
It took me a week of investigation at assembly level but I was able to get him back over 40K by re-factoring the code.
Well done!

There are editors that ease refactoring.

Refactoring source code in Visual Studio Code (Microsoft)

Eclipse CDT | The Eclipse Foundation

Code Generation & Refactorings - Features | CLion

What C editor to use for novice? | AVR Freaks

 

"Dare to be naïve." - Buckminster Fuller