This thread has been locked.

If you have a related question, please click the "Ask a related question" button in the top right corner. The newly created question will be automatically linked to this question.

TM4C129ENCPDT: uController will hang on sprintf statment

Part Number: TM4C129ENCPDT

Hi,

I am trying to run a function that is otherwise tested and work very well. However if I run the function on uController. The uController will hang on the sprintf statement.

This functions is actually combining two arrays such that one array is holding a string and another array is holding HEX characters. This function works very well if I simulate on my linux machine.

char Generate_Hostname(char *header, unsigned char *MacAddr, char *GeneratedHostName)
{
    char hex_tmp[50];
    unsigned char i;
    strcpy(GeneratedHostName, header); //Copy Header of the hostname i.e. BECMS to GeneratedHostName
    for (i = 0; i < 4; i++){sprintf(&hex_tmp[i*2],"%02x", *(MacAddr+i+2));} //Convert MacAddr numbers starting from MAC_Addr[2] upto MAC_Addr[5]
    strcat(GeneratedHostName,hex_tmp);

    return 1;
}

one can try by simply copying and pasting the following code to see quickly the function itself is working
#include<stdio.h>
#include<string.h>

#define HostNameHeader "TI_Device_"


char Generate_Hostname(char *header, unsigned char *MacAddr, char *hostname)
{
    char hex_tmp[20];
    unsigned char i;
    strcpy(hostname, header);
    for (i = 0; i < 4; i++){sprintf(&hex_tmp[i*2],"%02x", *(MacAddr+i+2));}
    strcat(hostname,hex_tmp);
}

int main(void)
{
    
char hostname[50];
unsigned char  MAC_Addr[6]={0xE0,0xC0,0x01,0x00,0xAF,0x60};

Generate_Hostname(HostNameHeader,&MAC_Addr[0],hostname);
printf("\n%s",hostname);

return 0;
}

.





  • Sahil said:

    Part Number: TM4C129ENCPDT

    Hi,

    I am trying to run a function that is otherwise tested and work very well. However if I run the function on uController. The uController will hang on the sprintf statement.

    This functions is actually combining two arrays such that one array is holding a string and another array is holding HEX characters. This function works very well if I simulate on my linux machine.

    Presumably you are linking in a library which implements sprintf() in some useful manner?

    There's more to it than just #include <stdio.h>! You're not running Linux -- you are running "Bare metal" on a microcontroller with no operating system.

    All of that said -- TI provides a utility library called the "Micro Standard library" which implements simplified versions of some of those functions.  It's part of the TivaWare installation and the particular function you want is called usprintf(). So look in the utils directory of your firmware development kit installation for ustdlib.c and ustdlib.h and add them to your project.

  • I am using sprintf for other purposes and it just work fine.

    Let me try usprintf() and will be reporting back.

  • Sahil said:

    I am using sprintf for other purposes and it just work fine.

    Define "other purposes."
    Is this your first embedded-controller design?
  • Hi,

      Perhaps you don't have enough stack or some other memory issue. These standard C library functions (e.g. sprintf) takes up a lot of memory space to execute. In any case, I will suggest you use the usprintf that is provided by the TivaWare library. They can be found in <TivaWare_Installation>/utils/ustdio.c. Refer to the TivaWare Utility User's Guide. Below is an excerpt of usprintf().