I am using MQX for KSDK 1.3.0 on a K64F processor. I am using the MFS file system and writing to an SD card. I noticed a post mentioning there is a 32-bit / 64-bit issue and designed a simple test to evaluate the changes. (See here) However, during this testing I noticed that writing to the SD card will fail because of a timeout. I traced this timeout into esdhc.c and found it is the NIO_ETIMEDOUT error. It seems to occur randomly.
My test code for the issue is the following:
1. Create a new text file.
2. Write 1 KB of data into the file.
3. After 1024 Writes (1 MB), close the file.
4. Open the same file, seek to the end.
5. Perform steps 2 - 4 until I write 1 GB to the file.
6. After 1 GB has been written, close the current file and create a new one. Repeat steps 2-4 until 8 files have been created. (We are using a 16 GB card)
The error occurs when I write to the file. The section of code it fails on is the following in esdhc.c (~line 1795):
/* Wait for transfer to complete. Timeout depends on number of blocks. */
if (_lwevent_wait_ticks(&esdhc_device_ptr->LWEVENT, (ESDHC_LWEVENT_TRANSFER_DONE | ESDHC_LWEVENT_TRANSFER_ERROR), FALSE, /*(esdhc_device_ptr->BUFFERED_CMD.BLOCKS > 1)? */ ESDHC_CMD12_TICK_TIMEOUT /*: ESDHC_CMD_TICK_TIMEOUT*/) != MQX_OK)
*error = NIO_ETIMEDOUT;
The event timeout value used is ESDHC_CMD12_TICK_TIMEOUT. I found the following definition in esdhc_prv.h:
#define ESDHC_CMD_TICK_TIMEOUT 20 // 40ms?
#define ESDHC_CMD12_TICK_TIMEOUT 200 //500ms
#define ESDHC_TRANSFER_TIMEOUT_MS 750 //750ms
I am confused on the comments of these defines. A tick timeout of 200 would only be 500 ms if the tick time was set to 2.5 ms. I think the default tick time for the K64F processor is 5 ms. So 200 * 5 = 1 second timeout. I have re-defined the tick time for my application to be 1 ms. So the timeout value is actually 200 ms.
The data is setup to transfer via ADMA a couple of lines before we wait for it to complete. I added some code in this area to provide feedback on the amount of time it takes to transfer the data to the SD card. I found that ~ every 400 writes to the SD card it would take > 10 ms for the write to complete. All other transfers took less than 10 ms.
However, occasionally it takes longer than 200 ms to complete the transfer, and the write fails. I have seen this happen when writing the 87th MB to the file all the way up to the 500th MB. I have yet to be able to complete all writes to the first file without this timeout occurring.
How can there be so much variability in the time it takes to transfer the data?
How was the value of the CMD12 tick timeout assigned?
What should the timeout value, in milliseconds, be for the CMD12 and CMD timeouts?