I'm using C2000 compiler 6.0.1 with CCSv4. I've written a printf-like function to help me print formatted debugging data to a serial port. I'm making use of the vsprintf() function but it seems to be broken whenever I use it with a non-empty argument list. The function code is as follows:
unsigned int Uart::send(const char* data, ...)
{
unsigned int count;
va_list args;
int stringLimit = MAX_TX_SIZE;
// Truncate format string
strncpy(formattedString, data, stringLimit);
// Execute formatting
va_start(args, data);
count = vsprintf(formattedString, formattedString, args);
va_end(args);
return send(formattedString, count);
} // end send
I've ensured that my heap and stack are both allocated and each is 4KB in size. If I make a call such as: myUart->send("Hello world"); the function works fine. If, however, I call it as myUart->send("Value is %d", someInt); the program will crash. Using the debugger, I've determined that this is because after the system returns from vsprintf, the formattedString variable points to a location in Flash which contains what is certainly not a null-terminated string. I have also ensured that my build settings are such that full printf support is included in my executable.
Does anyone know what might be going wrong? Also if you know of some vsnprintf() implementation that TI provides, that would be great :)
Thanks!