Hi Everyone
I am hoping someone might be able provide me with some inspiration or something !!
I have a problem within the application I am developing and am struggling to work out what is going wrong !
Development environment details!
1. Processor STM32F103RCT6 2. Keil ARM-MDK, RealView etc... 3. Running RL-ARM (RTOS) with RL-FLASHFS configured to run using SDIO connected micro sd-card.
There are a number for tasks with in my system. One receives mail box messages. The messages contain data that is to be logged to files on the micro-SD card.
The code that performs the logging is below. It is self contained. That is to say it performs the Open, write to disk and Close operations.
The code runs quite happily for sometime but then suddenly stops. If I then stop the code running, it is stuck in never ending loop in the HardFault interrupt handler. (That is shown below as well)
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. (although the example shown uses fwrite() it does exactly the same even if I just use a plain and simple fputc(‘A’,f) instead.
Just to be clear â€" it does not fail every time. Just after some extended running.
Any ideas you might have for things to try mill be welcome!!
LOGGING ROUTINE !!
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; } } }
HARD FAULT HANDLER
void AS_CTL_HardFault_Handler(void) __irq { BACKUP->ul_Hardfault_Count++; BACKUP->ul_Task_Debug_Count_Lo = ul_Task_Debug_Count & 0xFFFF; BACKUP->ul_Task_Debug_Count_Hi = ((ul_Task_Debug_Count >> 16 ) & 0xFFFF); while (1); }
Thanks in advance
did you try to lock access to the SD-card with a mutex? I think Keil claim FalshFS is task safe, but I'm not sure that is always the case...
Do you use Keil provided card hardware driver or did you write your own?
Hi Thanks for the replies,
I'll investigate the use of a Mutex further. I did see something on the forum that mentioned this but currently I am not accessing the SD card from more than one task.
The Driver is from Keil. It is all part of demo code for the MCBSTM32
Thanks
I suggest you first try Keil example SD_File and see if you have problems, if not then you can try turning off your possibly problematic code until you pinpoint the code that is making problems.
Hi Milorad Thanks for the reply !!
I have already tried isolating parts of my code.
I have discovered that it always fails when performing fwrite() or fputc().
If I just open and close the file but do not perform the write operations it does not fail.
Thanks again
Can you try Keil provided SD_File example and see if fwrite produces the problem there also?
As a test it might also be worth seeing if reads work?
I had the same problem. It was solved if I increased the heap to 0x400.
Hi Markus
Thanks for the reply. I had already done that.
I think my problem is that some other resource is shared in some way.
Something like DMA I think.
Anbeyon