- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
I'm working on a PsoC5lp board with PsoC creator 4.4 . My project is fairly due to the driver for my I2C display that my school is making me use. I set the heap size to 200 bytes. I set "use newlib-nano" to true. I set "newlib-nano float formatting" to true ( -u _printf_float). I also set link time optimization to true and and optimization level to size because of the aforementioned driver.
When I use sprintf on any non-zero float, the stringbuffer contains the following three values in place of the float:
0x00 '\000'
0x2E '.'
0x80 '\200'
The code I use is fairly simple and equivalent to:
char stringbuffer[20];
sprintf(stringbuffer, "(%.1f, %.1f)", 1.1, 1.1);
What is going wrong? I saw on stackoverflow that there might be a bug in the linker script... but that goes way above my head.
https://stackoverflow.com/questions/28746062/snprintf-prints-garbage-floats-with-newlib-nano
Solved! Go to Solution.
- Labels:
-
PSoC 5 Device Programming
-
PSoC 5LP
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
Hi,
I think that 200 bytes for heap is not enough.
I tested with heap = 200 bytes and got
Then I changed the heap = 0x200 and got
If your program is bigger then you may need more.
I used CY8CKIT-059
schematic
pins
main.c
====================
#include "project.h"
#include "string.h"
#define STR_BUF_LEN 32
char str[STR_BUF_LEN+1] ;
int main(void)
{
char stringbuffer[20];
float f = 0.0 ;
CyGlobalIntEnable; /* Enable global interrupts. */
UART_Start() ;
UART_PutString("\x1b[2J\x1b[;H") ;
UART_PutString("UART Float Test\n\r") ;
sprintf(stringbuffer, "(%.1f, %.1f)", 1.1, 1.1);
UART_PutString(stringbuffer) ;
UART_PutString("\n\r") ;
for(;;)
{
snprintf(str, STR_BUF_LEN, "(%.1f, %.1f)\n\r", f, 1.1) ;
f += 0.1 ;
UART_PutString(str) ;
CyDelay(1000) ;
}
}
====================
moto
(Edited) Some typos fixed.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
Hi,
I think that 200 bytes for heap is not enough.
I tested with heap = 200 bytes and got
Then I changed the heap = 0x200 and got
If your program is bigger then you may need more.
I used CY8CKIT-059
schematic
pins
main.c
====================
#include "project.h"
#include "string.h"
#define STR_BUF_LEN 32
char str[STR_BUF_LEN+1] ;
int main(void)
{
char stringbuffer[20];
float f = 0.0 ;
CyGlobalIntEnable; /* Enable global interrupts. */
UART_Start() ;
UART_PutString("\x1b[2J\x1b[;H") ;
UART_PutString("UART Float Test\n\r") ;
sprintf(stringbuffer, "(%.1f, %.1f)", 1.1, 1.1);
UART_PutString(stringbuffer) ;
UART_PutString("\n\r") ;
for(;;)
{
snprintf(str, STR_BUF_LEN, "(%.1f, %.1f)\n\r", f, 1.1) ;
f += 0.1 ;
UART_PutString(str) ;
CyDelay(1000) ;
}
}
====================
moto
(Edited) Some typos fixed.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
kova,
moto-san is correct about increasing the heap. You may also need to make sure the stack many need to be increased.
It appears you got the floating point library setting correct.
Len
"Engineering is an Art. The Art of Compromise."