• AVR Freaks

Hot!Removing SD-card Invokes error in system timer (Harmony)

Author
Kopper29
New Member
  • Total Posts : 4
  • Reward points : 0
  • Joined: 2019/02/04 04:54:05
  • Location: 0
  • Status: offline
2019/03/29 02:29:36 (permalink)
0

Removing SD-card Invokes error in system timer (Harmony)

 

Hallo
I’m working on a project involving reading data periodically with I2C and then saving this data to an SD card. (MPLAB X IDE5.10, Harmony 2.06)
Firstly a brief introduction to my problem: When removing the SD-card the system timer never return
SYS_TMR_DelayStatusGet(TimerDelayHandle) == true 
, and hence my code gets stuck at any delay. I do also have a
 SYS_TMR_CallbackPeriodic(1000, 0, &I2CTimerCallback); 
but this does not get affected at all.

 
Now more about my project:
The I2C reading is done periodically (1 Hz) and some of the peripherals on the I2C bus needs a time delay of about 1 ms between a ‘update registers message’ and a ‘read registers’ in order to read data correctly.
(I know the 1 ms delay is somewhat impossible to achieve with SYS_TMR_DelayMS() but the precision is not that important and I have kind of achieved it by setting delayMS to 2 ms, and it have run without problems so far – I have also tried setting the delay to 20 ms to ensure that this was not the problem).

My delay function is as follows, please note that the last else statement increments a counter which is set to 0 if the timer handle is invalid and a new delay is started. It is a kind of ugly error handler, I know.
 
 
 uint8_t I2C_Generic_Call_Delay(uint32_t DelayMS) {
    static SYS_TMR_HANDLE TimerDelayHandle = SYS_TMR_HANDLE_INVALID;
    static uint16_t NotDoneCount = 0;
    uint8_t DelayDone = false;
 
 
    if (TimerDelayHandle == SYS_TMR_HANDLE_INVALID) {
        //No delay running, make delay
        TimerDelayHandle = SYS_TMR_DelayMS(DelayMS);
        NotDoneCount = 0;
    } else if (SYS_TMR_DelayStatusGet(TimerDelayHandle)) {
        //Delay done
        TimerDelayHandle = SYS_TMR_HANDLE_INVALID;
        DelayDone = true;
    } else {
        NotDoneCount++;
        if (NotDoneCount > 1000 * DelayMS) //approx 5.8 ms*DelayMS
        {
            //Error, overtime!
            SYS_TMR_ObjectDelete(TimerDelayHandle);
            TimerDelayHandle = SYS_TMR_HANDLE_INVALID;
            DelayDone = true;
        }
    }
    return DelayDone;
}

The function is called repentantly until returing true.

 
I have got the SD-card up and running, based on the sdcard_fat_single_disk code, and I can write and read from files, make directories and so on. In my work to simplify the code and limit error possibilities I have disabled all operations to the SD card except the mounting. I have tried mounting both manually (like the sdcard_fat_single_disk example) and with automount feature and both ‘work’ but I still get the same error.
 
So, the Problem happen when I remove the SD card at a random time: it does not matter if the SD-card is mounted or mounted and unmounted, as long as it has been mounted once. From the scope I can see that the delay made by
 SYS_TMR_DelayMS(DelayMS); 
suddenly stop working. The normal delay seen on the scope is between 1 ms and 2 ms (which I find OK), but as soon as the SD card is removed (I can se the data transmission stops on the MISO when the SD card is removed), the next instance of the delay-period will have gone from 1-2 ms to 15-20 ms – this is indicating that my code is counting the NotDoneCount more than 1000 times and that is confirmed by debugging. For some reason the system timer never return
SYS_TMR_DelayStatusGet(TimerDelayHandle) == true 
, even though a valid timerhandle is provided – otherwise the NotDoneCount would be set to zero every time the function is called.
The
 SYS_TMR_CallbackPeriodic(1000, 0, &I2CTimerCallback); 
is not affected at all but is still running without any error.

I’ve been searching for an answer for hours but can seem to find any, I hope you can help me.
Regards Jakob
 
 
 
#1
Kopper29
New Member
  • Total Posts : 4
  • Reward points : 0
  • Joined: 2019/02/04 04:54:05
  • Location: 0
  • Status: offline
Re: Removing SD-card Invokes error in system timer (Harmony) 2019/04/02 23:28:53 (permalink)
5 (1)
Hello everybody,
I have located the error! The flaw lies in the drv_sdcard.c!
Problem is that the SD-card driver is also using timer handles – but it does not clear the handles when it is done with them!
This resulted in a situation where the SD-driver will remember values on the handles it has released – my timer delay would then pick one of these released handles, but the next time the code in the drv_sdcard.c is run through it tried to close this handle again (because it remember the value from last time it used it), and hence it closes my on-going timer-object and that is why my code gets stuck!

My solution was to add additional code after every statement in the drv_sdcard.c containing the:
 SYS_TMR_CallbackStop( dobj->cmdRespTmrHandle);
dobj->cmdRespTmrHandle = SYS_TMR_HANDLE_INVALID;  

and/or
 SYS_TMR_CallbackStop( dobj->timerHandle);
dobj-> timerHandle = SYS_TMR_HANDLE_INVALID;  

Making the handles invalid. Hence it will not remember the value of timer-handles it has already released!
I haven’t yet fully tested if this solution will create other problems but for now it works with my application!

I Hope this might be of help to anybody.
Regards Jakob
#2
Jump to:
© 2019 APG vNext Commercial Version 4.5