FX3S: FW hangs if we remove the SD card while writing

Tip / Sign in to post questions, reply, level up, and achieve exciting badges. Know more

cross mob
vim_2255711
Level 2
Level 2
5 sign-ins 5 replies posted First question asked

Dear Cypress,

Write data to Storage Port(through FatFs):

I was running the cyfx3_fatfs  example code on FX3S board, where am using write command

FX3S: `write` sample

Above command works fine and able to write raw data to that sample file and by defaults, it writes 20MB data, next what I did was executed the same command

FX3S: write sample1

this time while writing the data I just removed the SD card and it hangs.
Would like to know do we get any error from f_write?
if we are removed the SD card and it is blocking in f_write.

Thanks,

Vijay

0 Likes
1 Solution

Hi Vijay,

Then the control is not returning from for{} loop inside the f_write function.

This function (f_write) is from the FATFS File system and it doesn't have the provision to handle the case when the SD card is removed.

You can try modifying thef_write function by checking the status of the disk (in the starting / add in the condition part of the for{} loop inside f_write function) using this function disk_status. If the function return 0x02 which is STA_NODISK then the for{} can be exited using break.

or the other way can be when CY_U3P_SIB_EVENT_REMOVE occurs a global variable can be be used to indicate the status of the SD card. If the value of the variable changes when CY_U3P_SIB_EVENT_REMOVE occurs then the for loop can be exited

Please let me know if this helps

Regards,

Rashi

Regards,
Rashi

View solution in original post

0 Likes
11 Replies
Rashi_Vatsa
Moderator
Moderator
Moderator
5 likes given 500 solutions authored 1000 replies posted

Hello Vijay,

From the above description, this is my understanding. Please let me know if this is correct

- For the first case (sample) the SD  card is not removed

- For the second case (sample1) the SD card is removed when the write to the SD card is ongoing

- Please let me know if the data written in both these cases is the same?

Testing: (Please remove the SD card while doing the tests mentioned  below )

- From the FATFs firmware. Please let me know the error code value. and return value of  f_write function. Is the control going into to "if" condition

uint8_t var =0;

rc = f_write(&Fil[Port], ptr_arg2, n_bytes, &bw);

                if (rc)

                {       

                    err_code = (uint16_t)rc + 48;

                    if (err_code > 57)

                    {

                        err_code = err_code + 7;

                    }

                    err = (uint8_t *) &err_code;   

                    CyFxUartSendBuffer(error, 10);

                    CyFxUartSendBuffer(err, 1);

                   var++;

                }

-Also, let me know if the control is exiting the if condition.. You can check this by incrementing a dummy variable at the end of the if condition as shown above. Later in the firmware, the value of this variable can be tracked to know if the API's  CyFxUartSendBuffe and  CyFxUartSendBuffe are executed successfully.

If possible please share the art logs while doing these tests or while trying to write to SD card when SD card is not mounted.

Regards,

Rashi

Regards,
Rashi
0 Likes

Yes, your understanding is correct,  data is the same in both the case, but am not sure how much it will help us with your tests in solving the problem. Because fw hangs once you remove the sd card.

1) I can do those tests and let you know
2) The last point you mentioned without mounting and SD card write, sounds good, will do this and update.

0 Likes

Hello Vijay,

The tests I have asked for will help to know the return value of the f_write function when the SD card is not mounted which will be a similar case as to when you remove the SD card while writing to the card.

Regards,

Rashi

Regards,
Rashi
0 Likes
lock attach
Attachments are accessible only for community members.

Hi Rashi,
Did a couple of tests as you mentioned
1) When the SD card is mounted and try to write the data SD card and remove the SD card while writing, fw hangs and never comes out for f_write. No error message I have seen, I added all your code. In this case, it is neither going to if and not exiting from if, it just hangs.

2) commented the mount part of the code and try to write on SD card, got the error message

FX3S $ write no_mount1

write no_mount1

ERROR CODE:ÔC..

Attached the logs for reference.

0 Likes

Hello Vijay,

When the SD card is mounted and try to write the data SD card and remove the SD card while writing, fw hangs and never comes out for f_write. No error message I have seen, I added all your code.In this case, it is neither going to if and not exiting from if, it just hangs.

>> To confirm whether the f_write function returns or not (when the SD card is removed/ the device freezes)/ Is any code after f_write executed when you remove SD card?

OR can you call CyU3PDeviceReset(CyFalse)just after the f_write call and check if the device resets or its doesn't return from the f_write function. Also, confirm that the control enters the f_write function before the SD card is removed. Please let me know the results.

>>

rc = f_write(&Fil[Port], ptr_arg2, n_bytes, &bw);

CyU3PDeviceReset(CyFalse);

-  From the debug logs that you have shared, I see a print "Removed" at the end . Is it added by you in the firmware. Please let me know where is this print added in the firmware

commented the mount part of the code and try to write on SD card, got the error message

>> From not mounting the SD card i meant not inserting the SD card physically and trying the tests.

Regards,

Rashi

Regards,
Rashi
0 Likes
lock attach
Attachments are accessible only for community members.

Hi Rashi, thanks for Quick reply, I inlined the comments below
1) To confirm whether the f_write function returns or not (when the SD card is removed/ the device freezes)
/ Is any code after f_write executed when you remove SD card?

--> No major Code runs after that

2) can you call CyU3PDeviceReset(CyFalse)just after the f_write call and check if the device resets or its doesn't return from the f_write function. Also, confirm that the control enters the f_write function before the SD card is removed. Please let me know the results.

--> if I put CyU3PDeviceReset after f_write, for first f_write itself it resets right and no use of removing the SD card.

3) From the debug logs that you have shared, I see a print "Removed" at the end . Is it added by you in the firmware. Please let me know where is this print added in the firmware

-->  in cyfx3s_blkdrvr.c

CyFxFatfsAppSibCallback (

        uint8_t            portId,

        CyU3PSibEventType  evt,

        CyU3PReturnStatus_t status)

{                    

    if (evt == CY_U3P_SIB_EVENT_XFER_CPLT)

    {        

        CyU3PEventSet (&glStorDriverEvent, CY_FX_FATFS_SIB_DONE_EVENT, CYU3P_EVENT_OR);

    }                    

    if(evt == CY_U3P_SIB_EVENT_INSERT)

    {        

                          

        uint8_t Error[9] = "INSERTED\n";

            CyFxUartSendBuffer(Error, 9);

        //CyU3PDebugPrint(4,"INSERTED\n");

    }

    if(evt == CY_U3P_SIB_EVENT_REMOVE)

    {

        uint8_t Error[8] = "REMOVED\n";

            CyFxUartSendBuffer(Error, 8);

        //CyU3PDebugPrint(4,"CARD REMOVED\n");

    }

}

4) From not mounting the SD card i meant not inserting the SD card physically and trying the tests.

-->

FX3S $

FX3S $ write cypptest

write cypptest

ERROR CODE:Ô1..

FX3S $ ls

ls

ERROR CODE:Ô1..

Please find the firmware attached to this.
Am suspecting problem in FATFS, any thoughts on that side and it is easily reproducible.

0 Likes

Hello Vijay,

We also suspect that the program control is stuck in the f_write function and does not come out of it. To confirm that we asked you to add Device Reset API just after the f_write function call.

We are trying to reproduce the issue at our end but it is taking some time meanwhile can you try at your end and let us know the results. Instead of CyU3PDeviceReset (CyFalse) can you just add some debug prints after the f_write function so that we can confirm if the f_write returns (when the SD Card is removed) or freezes inside the f_write function call.

rc = f_write(&Fil[Port], ptr_arg2, n_bytes, &bw);

CyFXUartPrint (0, "Returned from F_WRITE with return value = %d \r",rc);

if(rc)

{

....

}

Please share the debug prints after this test

Regards,

Rashi

Regards,
Rashi
0 Likes
lock attach
Attachments are accessible only for community members.

Hello Rashi,

Please find the attached logs, this contains the debug prints which you have requested.

-Regards

Vijay

0 Likes

Hi Vijay,

From the logs, I observed that no UART prints are seen after the SD Card is removed i.e. f_write function doesn't return the control. Is that right?

Regards,

Rashi

Regards,
Rashi
0 Likes

Yes Rashi, you are right.

Regards

Vijay

0 Likes

Hi Vijay,

Then the control is not returning from for{} loop inside the f_write function.

This function (f_write) is from the FATFS File system and it doesn't have the provision to handle the case when the SD card is removed.

You can try modifying thef_write function by checking the status of the disk (in the starting / add in the condition part of the for{} loop inside f_write function) using this function disk_status. If the function return 0x02 which is STA_NODISK then the for{} can be exited using break.

or the other way can be when CY_U3P_SIB_EVENT_REMOVE occurs a global variable can be be used to indicate the status of the SD card. If the value of the variable changes when CY_U3P_SIB_EVENT_REMOVE occurs then the for loop can be exited

Please let me know if this helps

Regards,

Rashi

Regards,
Rashi
0 Likes