<?xml version="1.0" encoding="UTF-8"?>
<rss xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" xmlns:taxo="http://purl.org/rss/1.0/modules/taxonomy/" version="2.0">
  <channel>
    <title>topic Re: sprintf concerns (floating point) in S12 / MagniV Microcontrollers</title>
    <link>https://community.nxp.com/t5/S12-MagniV-Microcontrollers/sprintf-concerns-floating-point/m-p/126414#M522</link>
    <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;Hi:&lt;BR /&gt;&lt;BR /&gt;To avoid problems, I would suggest not to use sprintf calls in your Interrupt Service Routine code. Make the ISR as short as possible.&lt;BR /&gt;&lt;BR /&gt;Regards,&lt;BR /&gt;&lt;BR /&gt;Alex&lt;/BODY&gt;&lt;/HTML&gt;</description>
    <pubDate>Sat, 12 Aug 2006 01:13:06 GMT</pubDate>
    <dc:creator>alex_spotw</dc:creator>
    <dc:date>2006-08-12T01:13:06Z</dc:date>
    <item>
      <title>sprintf concerns (floating point)</title>
      <link>https://community.nxp.com/t5/S12-MagniV-Microcontrollers/sprintf-concerns-floating-point/m-p/126413#M521</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;DIV&gt;In regards to this code below:&lt;/DIV&gt;&lt;DIV&gt;&amp;nbsp;&lt;/DIV&gt;&lt;DIV&gt;&lt;SPAN class="msg_source_code"&gt;&lt;SPAN class="text_smallest"&gt;Code:&lt;/SPAN&gt;&lt;/SPAN&gt;&lt;PRE&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; volatile int cnt;&amp;nbsp;&amp;nbsp;&amp;nbsp; char flt_buf[15] = {0};&amp;nbsp;&amp;nbsp;&amp;nbsp; /* Load the float into a string */&amp;nbsp;&amp;nbsp;&amp;nbsp; __DI();&amp;nbsp;&amp;nbsp;&amp;nbsp; if (dp == 1U) {&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; /* Showing tenths */&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; cnt = sprintf(&amp;amp;flt_buf[0], "%-5.1f", val);&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; /* Override term! */&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; term = 0U;&amp;nbsp;&amp;nbsp;&amp;nbsp; }&amp;nbsp;&amp;nbsp;&amp;nbsp; else {&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; /* No tenths */&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; cnt = sprintf(&amp;amp;flt_buf[0], "%-5.0f", val);&amp;nbsp;&amp;nbsp;&amp;nbsp; }&amp;nbsp;&amp;nbsp;&amp;nbsp; __EI();&lt;/PRE&gt;&lt;BR /&gt;&amp;nbsp;&lt;BR /&gt;1) Is it necessary to protect ansilibb (banked) library calls from interrupts as shown...I seem to have (odd COP resets otherwise)?&lt;/DIV&gt;&lt;DIV&gt;2) Is ansilibb reentrant (if isr code calls a ansilibb function [could be same function] will this fail)?&lt;/DIV&gt;&lt;DIV&gt;3) I'm a bit concerned that the width specifier is a "minimum" value and this could be overrun...what are some good ideas to protect against this other that oversizing the buffer?&lt;/DIV&gt;&lt;DIV&gt;&amp;nbsp;&lt;/DIV&gt;&lt;DIV&gt;Thanks!&lt;/DIV&gt;&lt;DIV&gt;&amp;nbsp;&lt;/DIV&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Fri, 11 Aug 2006 20:41:40 GMT</pubDate>
      <guid>https://community.nxp.com/t5/S12-MagniV-Microcontrollers/sprintf-concerns-floating-point/m-p/126413#M521</guid>
      <dc:creator>rhb3</dc:creator>
      <dc:date>2006-08-11T20:41:40Z</dc:date>
    </item>
    <item>
      <title>Re: sprintf concerns (floating point)</title>
      <link>https://community.nxp.com/t5/S12-MagniV-Microcontrollers/sprintf-concerns-floating-point/m-p/126414#M522</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;Hi:&lt;BR /&gt;&lt;BR /&gt;To avoid problems, I would suggest not to use sprintf calls in your Interrupt Service Routine code. Make the ISR as short as possible.&lt;BR /&gt;&lt;BR /&gt;Regards,&lt;BR /&gt;&lt;BR /&gt;Alex&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Sat, 12 Aug 2006 01:13:06 GMT</pubDate>
      <guid>https://community.nxp.com/t5/S12-MagniV-Microcontrollers/sprintf-concerns-floating-point/m-p/126414#M522</guid>
      <dc:creator>alex_spotw</dc:creator>
      <dc:date>2006-08-12T01:13:06Z</dc:date>
    </item>
    <item>
      <title>Re: sprintf concerns (floating point)</title>
      <link>https://community.nxp.com/t5/S12-MagniV-Microcontrollers/sprintf-concerns-floating-point/m-p/126415#M523</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;I have a couple of comments.&lt;BR /&gt;First, as already suggested, I would not use sprintf in a interrupt handler. It just takes too long.&lt;BR /&gt;&lt;BR /&gt;The default sprintf is not reentrant. But if you really need it to be, then you can change it, there is a switch in libdef.h for this.&lt;BR /&gt;To use a reentrant sprintf: &lt;BR /&gt;- add lib\hc12c\src\printf.c to your project (before the ansi library in the link tab)&lt;BR /&gt;- add "-DLIBDEF_REENTRANT_PRINTF" to your compiler options&lt;BR /&gt;Recompile, this should give you a warning that the printf is taken out of your local object file and not out of the ansi lib.&lt;BR /&gt;&lt;BR /&gt;The warning can be ignored (or switched off) (the only way to get rid of the waring otherwise is to rebuild the ansi lib).&lt;BR /&gt;&lt;BR /&gt;About your concern of the buffer overflow, I have to agree. The printf %f format is generating a float number on the format "a.b", e.g. 1.2. &lt;BR /&gt;As the integral part is always printed, a float number of 1.0E100 will be writing more than 100 bytes, clearly overflowing your buffer.&lt;BR /&gt;&lt;BR /&gt;Possibilities:&lt;BR /&gt;- use %g instead of %f.&lt;BR /&gt;- do not use sprintf, but some other float output formatting function.&lt;BR /&gt;- use the (non ANSI) vsprintf function. See the sprintf implementation in printf.c how it does, then it is simple to detect a possible buffer overflow.&lt;BR /&gt;- do not print huge float numbers (also not very small, negative ones...)&lt;BR /&gt;&lt;BR /&gt;Daniel&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Mon, 14 Aug 2006 03:17:50 GMT</pubDate>
      <guid>https://community.nxp.com/t5/S12-MagniV-Microcontrollers/sprintf-concerns-floating-point/m-p/126415#M523</guid>
      <dc:creator>CompilerGuru</dc:creator>
      <dc:date>2006-08-14T03:17:50Z</dc:date>
    </item>
    <item>
      <title>Re: sprintf concerns (floating point)</title>
      <link>https://community.nxp.com/t5/S12-MagniV-Microcontrollers/sprintf-concerns-floating-point/m-p/126416#M524</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;DIV&gt;&lt;/DIV&gt;&lt;P&gt;Thanks for the ideas.&amp;nbsp; I'm considering range checking and staying with the %f format...something like:&lt;/P&gt;&lt;P&gt;void dig4_float_to_7seg(float val, unsigned char dp, unsigned char term,&amp;nbsp; unsigned char *buf)&lt;/P&gt;&lt;P&gt;&lt;SPAN class="text_smallest"&gt;Code:&lt;/SPAN&gt;&lt;/P&gt;&lt;SPAN class="msg_source_code"&gt;&lt;/SPAN&gt;&lt;PRE&gt;    volatile int len;    volatile int cnt;    char flt_buf[15] = {0};    /* Limit the float size.  This will help protect the buffer for %f format */    if ((val &amp;lt; -9999.5F) || (val &amp;gt; 9999.5F)) {        dig4_float_to_7seg_err(&amp;amp;buf[0]);        return;    }    /* No deeper than 1/100s resolution */    if ((val &amp;lt; 0.0F) &amp;amp;&amp;amp; (val &amp;gt; -0.01F)) {        dig4_float_to_7seg_err(&amp;amp;buf[0]);        return;    }    if ((val &amp;lt; 0.01F) &amp;amp;&amp;amp; (val &amp;gt; 0.0F)) {        dig4_float_to_7seg_err(&amp;amp;buf[0]);        return;    }    /* Load the float into a string.  For small numbers (near zero), %f will     * get the first significant digit for both .1 and .0 formats. */    __DI();    /* Showing tenths */    if (dp == 1U) {        cnt = sprintf(&amp;amp;flt_buf[0], "%-10.1f", val);        /* Override term! */        term = 0U;    }    /* No tenths */    else {        /*lint -e{559} */        cnt = sprintf(&amp;amp;flt_buf[0], "%-10.0f", val);    }    __EI();&lt;/PRE&gt;&lt;P&gt;&lt;/P&gt;&lt;BR /&gt;&amp;nbsp;&lt;BR /&gt;I experimented&amp;nbsp;with the %g using significnat digit counts; however, I'm trying to&amp;nbsp;write the&amp;nbsp;"driver" generic for ints and&amp;nbsp;floats for a 4-segment display with whole number and 1/10 display options.&amp;nbsp; I&amp;nbsp;failed to find a way to strip of the tenths (like %-10.0f does) for %g as it is based on significant digits.&amp;nbsp; The fact that I currently am leting sprintf handle negative numbers as well does complicate the range checking a bit as I have to allow zero AND watch out for small numbers and I guess I could pass a variable to indiate the sign (TBD).&amp;nbsp; &lt;P&gt;&lt;/P&gt;&lt;P&gt;I analyzed printf.c but I was hoping you could elaborate on the use of vsprintf and ensuring that the buffer does not overrun...&lt;/P&gt;&lt;P&gt;I envision soemthing like:&lt;SPAN class="msg_source_code"&gt;&lt;SPAN class="text_smallest"&gt;Code:&lt;/SPAN&gt;&lt;/SPAN&gt;&lt;/P&gt;&lt;PRE&gt;char buffer[15];int vspf(char *fmt, ...){   va_list argptr;   int cnt;   va_start(argptr, fmt);   cnt = vsprintf(buffer, fmt, argptr);   va_end(argptr);   return(cnt);}int main(void){   float fnumber = 90.0F;   vspf("%-10.1f", fnumber);   printf("%s\n", buffer);   return 0;}&lt;/PRE&gt;&lt;P&gt;&lt;BR /&gt;&amp;nbsp;&lt;BR /&gt;Exactly how do I ensure buffer is not overrun?&amp;nbsp; Also, just to clarify...based on your earlier comments there is no reason to protect sprintf with __DI()/__EI().&amp;nbsp; I never did use it in the isr I just wanted to understand its capabilities.&lt;/P&gt;&lt;P&gt;Thanks again!&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Mon, 14 Aug 2006 21:29:35 GMT</pubDate>
      <guid>https://community.nxp.com/t5/S12-MagniV-Microcontrollers/sprintf-concerns-floating-point/m-p/126416#M524</guid>
      <dc:creator>rhb3</dc:creator>
      <dc:date>2006-08-14T21:29:35Z</dc:date>
    </item>
    <item>
      <title>Re: sprintf concerns (floating point)</title>
      <link>https://community.nxp.com/t5/S12-MagniV-Microcontrollers/sprintf-concerns-floating-point/m-p/126417#M525</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;You don`t need to disable interrupts if you do not call ?printf from within an interrupt handler (and if you dont have a preemptative OS which runs multiple tasks using sprintf or ...)&lt;BR /&gt;&lt;BR /&gt;I did implement in the attachment a C99 like snprintf, it's almost the same as sprintf, but it takes as additional argument the size of the destination buffer.&lt;BR /&gt;You can directly use it, or you can extract the part you need.&lt;BR /&gt;When I look at the details of your problem, I wonder if you should use sprintf with a %f at all.&lt;BR /&gt;Basically you only have a very limited float range (-10000..+10000) and you only one one decimal digit after the dot.&lt;BR /&gt;To me, this sounds pretty easy to implement with integer (long) arithmetic only. So something like this (pseudocode, never run, never compiled :-):&lt;BR /&gt;&lt;BR /&gt;Bool neg= false;&lt;BR /&gt;if (x &amp;lt; 0.0f) { neg= true; x= -x; }&lt;BR /&gt;if (x &amp;gt; 10000.0f) { tooBig(); return; }&lt;BR /&gt;unsigned long val= (unsigned long)(x * 10.0f + 0.5f);&lt;BR /&gt;char buf[30]; // much more than needed :smileyhappy:&lt;BR /&gt;int pos=0;&lt;BR /&gt;if (neg) {&lt;BR /&gt;buf[0]= '-';&lt;BR /&gt;pos++;&lt;BR /&gt;}&lt;BR /&gt;pos += sprintf(buf+pos, "%lu", val / 10);&lt;BR /&gt;buf[pos]= '.';&lt;BR /&gt;pos++;&lt;BR /&gt;if (oneDigMore) {&lt;BR /&gt;buf[pos]= (char)((val % 10) + '0');&lt;BR /&gt;pos++;&lt;BR /&gt;}&lt;BR /&gt;buf[pos]= 0;&lt;BR /&gt;&lt;BR /&gt;Well, ok, this also needs some lines, but at least it is straight forward.&lt;BR /&gt;&lt;BR /&gt;Bye&lt;BR /&gt;&lt;BR /&gt;Daniel&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Wed, 16 Aug 2006 04:00:00 GMT</pubDate>
      <guid>https://community.nxp.com/t5/S12-MagniV-Microcontrollers/sprintf-concerns-floating-point/m-p/126417#M525</guid>
      <dc:creator>CompilerGuru</dc:creator>
      <dc:date>2006-08-16T04:00:00Z</dc:date>
    </item>
    <item>
      <title>Re: sprintf concerns (floating point)</title>
      <link>https://community.nxp.com/t5/S12-MagniV-Microcontrollers/sprintf-concerns-floating-point/m-p/126418#M526</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;Somehow the attachment did not get added, hope this works this time.&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Thu, 17 Aug 2006 04:14:52 GMT</pubDate>
      <guid>https://community.nxp.com/t5/S12-MagniV-Microcontrollers/sprintf-concerns-floating-point/m-p/126418#M526</guid>
      <dc:creator>CompilerGuru</dc:creator>
      <dc:date>2006-08-17T04:14:52Z</dc:date>
    </item>
  </channel>
</rss>

