This discussion has been locked.
You can no longer post new replies to this discussion. If you have a question you can start a new discussion

sprintf causing a hardfault exception

Had to add some data logging in a hurry to diagnose an issue.

A function is passed a pointer to data that is then used to assemble a readable string that's then stored on a SD card

the RTOS task that does this has a 8K stack and typically runs at 10% loading up to 80%
(Crude observations of the debug options window)

The first version of the code version cause a Hardfault exception at the sprintf
instruction.

I do not use the micro Lib and have included retarget.c in the project


unit8_t baBuf[100];
/*******************************
*       Create the Data
********************************/
sprintf((char *)&baBuf[0],"%08d,%08d,%02d,%02d,%02d,%02d,%02d,%02d,%04d,%04d\r\n",
     psLIN->lMillsecTimePerDay,
     psLIN->lDataId,
      psLIN->bData1,psLIN->bData2,psLIN->bData3,psLIN->bData4,
        psLIN->bData5,psLIN->bData6,psLIN->wData1,psLIN->wData2);


The second version of the code works as expected


unit8_t baBuf[100];
/*******************************
*       Create the Data
********************************/
sprintf((char *)&baBuf[0],"%08d,",psLIN->lMillsecTimePerDay);
wDataLength = strlen((const char *)&baBuf[0]);

sprintf((char *)&baBuf[wDataLength],"%08d,%02d,%02d,%02d,%02d,%02d,%02d,%04d,%04d\r\n",
     psLIN->lDataId,
      psLIN->bData1,psLIN->bData2,psLIN->bData3,psLIN->bData4,
        psLIN->bData5,psLIN->bData6,psLIN->wData1,psLIN->wData2);

Any one have an view on why the first version of the code should be failing ?
or is it a case of Please read the manual

thanks Dan

Parents
  • But if the problem can be recreated in a small test-application with ample stack space, then this is a project that should be handed to Keil support. The code generator should not have any issues with sending 10 or 20 or 50 parameters to sprintf(). And sprintf() itself should use teh formatting parameters to iterate through the parameter values one by one.

    So the only real limitation should be that the output buffer is large enough to hold the produced string.

Reply
  • But if the problem can be recreated in a small test-application with ample stack space, then this is a project that should be handed to Keil support. The code generator should not have any issues with sending 10 or 20 or 50 parameters to sprintf(). And sprintf() itself should use teh formatting parameters to iterate through the parameter values one by one.

    So the only real limitation should be that the output buffer is large enough to hold the produced string.

Children
No data