Skip to main content
anbeyon
Associate II
October 22, 2009
Question

RL-FLASHFS- fwrite() HardFault

  • October 22, 2009
  • 7 replies
  • 1463 views
Posted on October 22, 2009 at 10:01

RL-FLASHFS- fwrite() HardFault

    This topic has been closed for replies.

    7 replies

    anbeyon
    anbeyonAuthor
    Associate II
    May 17, 2011
    Posted on May 17, 2011 at 13:27

    Environment:

    Processor : STM32F103RCT6

    Software : Realview MDK-ARM 3.08

    Middleware : RL-ARM (RTX RTOS), RL-FLASHFS (using SDIO)

    Hi everyone,

    I am hoping somone might be able to point me in the right direction.

    I have a problem!

    Within the application I am developing I have a task that receives mailbox messages. These message contain data that is to be logged to micro-sd card that is connected via SDIO.

    However, I have a problem and am struggling to work out what is going wrong !

    The code below is the function that is called when a record is to be added to the log file.

    The code runs quite happily for a sometime but suddenly stops running. If

    I stop the code running I can see that it is looping around a never-ending while loop in the HardFault interrupt handler.

    I can see from the value of variable ul_Task_Debug_Count that it fails at the first fwrite() call. (highlighted below with // **** FAILS HERE ****

    comment)

    I know this because ul_Task_Debug_Count = 0x50000000.

    Just to be clear - it does not fail every time. Just after some extended running. (A few minutes).

    I am wondering if you might have some thoughts/suggestions/ideas etc... I can try to investigate what might be happening?

    I problem always occurs at the same place in the code. It doesn't seem to matter if I use fwrite() or fputc().

    If anyone can think of anything I can try I'd be really greatful.

    void AS_Logger_LogEvent(LOG_MESSAGE *Message) {

    char FileName[MAX_FILENAME_SIZE];

    FILE *f;

    unsigned char uc_ChkSum = 0;

    unsigned short us_RecNum = 0;

    ul_Task_Debug_Count = 0x10000000;

    // get filename

    if (AS_Logger_GetFileName(Message, FileName))

    {

    ul_Task_Debug_Count = 0x20000000;

    f = fopen (FileName, ''a''); // open file for

    append - add new record to end

    ul_Task_Debug_Count = 0x30000000;

    if (f)

    {

    // write record to disk

    ul_Task_Debug_Count = 0x40000000;

    // recnum

    us_RecNum = AS_Logger_GetNextRecordNumber(Message);

    ul_Task_Debug_Count = 0x50000000;

    fwrite (&us_RecNum, sizeof (us_RecNum), 1, f); // **** FAILS HERE ****

    ul_Task_Debug_Count = 0x60000000;

    // ErrType

    fwrite (&Message->uc_ErrorType, sizeof(Message->uc_ErrorType), 1, f);

    ul_Task_Debug_Count = 0x70000000;

    // ErrCode

    fwrite (&Message->uc_ErrorCode, sizeof(Message->uc_ErrorCode), 1, f);

    ul_Task_Debug_Count = 0x80000000;

    // Timestamp

    fwrite (&Message->ul_TimeStamp, sizeof(Message->ul_TimeStamp), 1, f);

    ul_Task_Debug_Count = 0x90000000;

    // Size

    fwrite (&Message->uc_Size, sizeof(Message->uc_Size), 1, f);

    ul_Task_Debug_Count = 0xa0000000;

    // data

    if (Message->uc_Size) // data to write ?

    {

    ul_Task_Debug_Count = 0xb0000000;

    fwrite (Message->uc_Data, 1, Message->uc_Size, f);

    }

    ul_Task_Debug_Count = 0xc0000000;

    // chksum

    uc_ChkSum = AS_Logger_GetRecordChkSum(us_RecNum,

    Message);

    fwrite (&uc_ChkSum, sizeof(uc_ChkSum), 1, f);

    ul_Task_Debug_Count = 0xd0000000;

    // close it

    fclose (f);

    ul_Task_Debug_Count = 0xe0000000;

    }

    else

    {

    // failed to open file - do something

    ul_Task_Debug_Count = 0xf0000000;

    }

    }

    }

    tomas23
    Visitor II
    May 17, 2011
    Posted on May 17, 2011 at 13:27

    Hi, Hard Fault is only collector exception. Enable Bus fault, Memory fault and Usage fault exceptions, give them high priority and when you fall in them, read NVIC error register (type of error, address of instruction).

    From the first insight, I'd suspect bad pointer operation or bad/small stack settings, check in some RTOS viewer.

    Please check in documentation whether the SDIO middleware isn't reentrant and whether you don't need to guard the access with some mutex.

    anbeyon
    anbeyonAuthor
    Associate II
    May 17, 2011
    Posted on May 17, 2011 at 13:27

    Sorry for re-posting

    Attached code as a file so that it is easier to read !!

    Incidentally If I only perform the open/close but not write operation I do not get the problem.

    This is the only taks that performs File IO.

    Anbeyon

    ccowdery9
    Associate III
    May 17, 2011
    Posted on May 17, 2011 at 13:27

    My interpretation (which has fixed a few of my bugs) is that it's fundamentally when the processor tries to read / write to a memory location that doesn't exist. It could have jumped there (stack overflow), or be trying to access data there (pointer issues).

    Chris.

    anbeyon
    anbeyonAuthor
    Associate II
    May 17, 2011
    Posted on May 17, 2011 at 13:27

    Thanks for the reply edison.

    When you say you suspect a pointer issue. I don;t think it can be since if I replace the fwrite(....) with fputc('A',f) it still happens!

    I will check the re-entrancy of the SDIO.

    Thanks again

    ccowdery9
    Associate III
    May 17, 2011
    Posted on May 17, 2011 at 13:27

    I don't. I use 'crash & burn' technique. I don't use a debugger at all.

    I use printf() statements through my code until I find out where the crash is caused.

    I guess you could write a stack backtrace routine which runs as the HardFault handler, but I've never bothered. All the info is on the stack though......

    Chris.

    anbeyon
    anbeyonAuthor
    Associate II
    May 17, 2011
    Posted on May 17, 2011 at 13:27

    But once you are in the HArd Fault hander how do you work backwards to know where the problem cam from ?