Skip to main content
con3
Senior
November 29, 2017
Solved

SD_Read bug?

  • November 29, 2017
  • 14 replies
  • 4014 views
Posted on November 29, 2017 at 14:53

Good day everyone,

I'm not sure if this is a bug or I'm looking at this incorrectly, but It seems to be a bug.

While calling the f_mount function there is a point where SD_read is called.

DRESULT SD_read(BYTE lun, BYTE *buff, DWORD sector, UINT count)

{

DRESULT res = RES_ERROR;

ReadStatus = 0;

uint32_t timeout;

if (ENABLE_SD_DMA_CACHE_MAINTENANCE == 1)

uint32_t alignedAddr;

endif

if(BSP_SD_ReadBlocks_DMA((uint32_t*)buff,

(uint32_t) (sector),

count) == MSD_OK)

{

/* Wait that the reading process is completed or a timeout occurs */

timeout = HAL_GetTick();

while((ReadStatus == 0) && ((HAL_GetTick() - timeout) < SD_TIMEOUT))

{

}

/* incase of a timeout return error */

if (ReadStatus == 0)

{

res = RES_ERROR;

}

else

{

ReadStatus = 0;

timeout = HAL_GetTick();

while((HAL_GetTick() - timeout) < SD_TIMEOUT)

{

if (BSP_SD_GetCardState() == SD_TRANSFER_OK)

{

res = RES_OK;

if (ENABLE_SD_DMA_CACHE_MAINTENANCE == 1)

/*

the SCB_InvalidateDCache_by_Addr() requires a 32-Bit aligned address,

adjust the address and the D-Cache size to invalidate accordingly.

*/

alignedAddr = (uint32_t)buff & ~3;

SCB_InvalidateDCache_by_Addr((uint32_t*)alignedAddr, count*BLOCKSIZE + ((uint32_t)buff - alignedAddr));

endif

break;

}

}

}

}

return res;

}

After this call my code enters

f(BSP_SD_ReadBlocks_DMA((uint32_t*)buff,(uint32_t) (sector),count) == MSD_OK)

and passes it successfully, after which it gets stuck in the while loop.

while((ReadStatus == 0) && ((HAL_GetTick() - timeout) < SD_TIMEOUT))

{

}

After this the problem occurs and that is:

if (ReadStatus == 0)

{

res = RES_ERROR;

}

the function always gets stuck here no matter what. So i went and looked at all the calls for ReadStatus in my project and this popped up:

0690X00000604DJQAY.jpg

From here it seems there's one callback that will ensure the SD_Read passes and thats BSP_SD_ReadCpltCallback. Although this is never called anywhere in the project and therefore SD_Read always fails. Therefore it always remain 0 and the LL read function fails. This is cube generated.

Is this a mistake from my side or CubeMX??

I'm using the latest cube(V 4.0 and the latest Hal(1.8.0) for the stm32F7

f_mount-sdcard sdcard cubemx-project stm32f7-hal stm32f7

This topic has been closed for replies.
Best answer by Imen.D
Posted on November 29, 2017 at 16:21

Hello

Reishauer.Ian

,

Iwill raise this issue internally forcheck and investigation.

Best Regards

Imen

14 replies

Imen.DBest answer
ST Technical Moderator
November 29, 2017
Posted on November 29, 2017 at 16:21

Hello

Reishauer.Ian

,

Iwill raise this issue internally forcheck and investigation.

Best Regards

Imen

In order to give better visibility on the answered topics, please click on 'Best answer' on the reply which solved your issue or answered your question. Thanks
con3
con3Author
Senior
November 29, 2017
Posted on November 29, 2017 at 16:34

Thank you, the write function seems fine as it internally sets the write flag to 1, it looks like this was just forgotten in the read function

greg239955_stm1
Associate II
November 30, 2017
Posted on November 30, 2017 at 23:52

Same issue here.

Furthermore, the Cube generated sd_diskio.c doesn't call BSP_SD_Init() during SD_initialize().

con3
con3Author
Senior
December 2, 2017
Posted on December 02, 2017 at 00:47

Same issue, after I implemented the fix to SD_initialize, as well as SD_read and SD_write I can successfully mount and open files. F_write still fails which seems very odd(cause I can create files), but trying to debug it. Once I figure out why its bugging out I should be able to completely use the FATFS system.

moien mo
Visitor II
May 15, 2018
Posted on May 15, 2018 at 10:27

hi piet t how do you fix 

SD_initialize to mount and open file?

franckngatcha
Associate II
December 2, 2017
Posted on December 02, 2017 at 13:00

same issue with 

V 4.22.1 and the latest Hal(1.8.0) for the stm32F7

Amel NASRI
ST Technical Moderator
December 6, 2017
Posted on December 06, 2017 at 17:16

Hi

Reishauer.Ian

‌,

It seems you identified root cause of your issue as stated in

https://community.st.com/0D50X00009XkXg5SAF

.

For others having similar issues, please do the following:

1) Re-defineSD_initialize as following (in case code is generated with CubeMX 4.23):

Depending on you application requirement:

FreeRTOS not used:

In the generated file 'your_project/src/sd_diskio.c', re-define 'SD_initialize' as following:

DSTATUS SD_initialize(BYTE lun)

{

if(BSP_SD_Init() == MSD_OK)

{

Stat = SD_CheckStatus(lun);

}

return Stat;

}

FreeRTOS used:

In the generated file 'your_project/src/sd_diskio.c', re-define 'SD_initialize' as following:

DSTATUS SD_initialize(BYTE lun)

{

Stat = STA_NOINIT;

/*

* check that the kernel has been started before continuing

* as the osMessage API will fail otherwise

*/

if(osKernelRunning())

{

if(BSP_SD_Init() == MSD_OK)

{

Stat = SD_CheckStatus(lun);

}

/*

* if the SD is correctly initialized, create the operation queue

*/

if (Stat != STA_NOINIT)

{

osMessageQDef(SD_Queue, QUEUE_SIZE, uint16_t);

SDQueueID = osMessageCreate (osMessageQ(SD_Queue), NULL);

}

}

return Stat;

}

2) Apply the fix suggested by

Reishauer.Ian

‌ in the other discussions.

If you still face issues, please let us know about them.

-Amel

To give better visibility on the answered topics, please click on "Best Answer" on the reply which solved your issue or answered your question.
con3
con3Author
Senior
December 6, 2017
Posted on December 06, 2017 at 17:52

Hi Amel, Both posts I Made are issues.

The one being the flag never being set (ReadStatus =1) and the state of the sdcard that is never returned to Ready.

I hope these help anyone that has a similar issue.

-Piet

Amel NASRI
ST Technical Moderator
December 8, 2017
Posted on December 08, 2017 at 11:42

Ok

Reishauer.Ian

‌,

As already said by

DAHMEN.IMEN

‌, the case is already raised internally.

Similar issue is already discussed in

https://community.st.com/0D50X00009XkWyLSAV

and a solution is suggeste there.

You can test it and let us know if it is working for you.

-Amel

To give better visibility on the answered topics, please click on "Best Answer" on the reply which solved your issue or answered your question.