cancel
Showing results for 
Search instead for 
Did you mean: 

SD read/write with DMA mode working only in debug mode

Poonam Deoghare
Associate III
Posted on May 29, 2018 at 13:55

Hi All,

I am working on STM32F413 Disco kit and trying to read/write into SD card using DMA mode.

My requirement is -   i have to call single function which will read/write into SD card in 1ms timer callback function.

It is working fine in debug mode but not in normal mode.

Is there any delay issue ? Because when we run in debug mode, automatically delay is getting added.

Our project requirement is very rigid that we should not use HAL_Delay().

Please provide your suggestions.

My code:-

/********************************************/

void HAL_TIM_PeriodElapsedCallback(TIM_HandleTypeDef *htim)

{    // TIM1 is configured for 1ms

        if(htim->Instance == TIM1)    {

        SDtest();     

    }

}

/********************************************/

void SDtest(void){

      LastWriteAddr = SD_WriteBlocks_DMA(Tx,TEMP_RAW_DATA_START_ADDR,1);

        LastReadAddr = SD_ReadBlocks_DMA(Rx,TEMP_RAW_DATA_START_ADDR,1);

}

/********************************************/

/*** Function To write into SD card via DMA***/

uint32_t SD_WriteBlocks_DMA(uint8_t *pData, uint32_t WriteAddr, uint32_t NumOfBlocks)

{

    if(SDOperationState == SDWriteOperation)

    {

        printf('\n SD_WriteBlocks_DMA SDWriteOperation\n');

        SDState = SDCardAvailable;

        return 0;

    }   

    if(SDState == SDCardAvailable)

    {    

        printf('\n SD_WriteBlocks_DMA\n');

        HAL_SD_WriteBlocks_DMA(&hsd, (uint8_t *)pData, WriteAddr, NumOfBlocks);

        SDState = SDCardBusy;

        return 0;

  }

  if(txDone == true)

  {

        WriteAddr += EACH_BLOCK_SIZE;

        SDOperationState = SDWriteOperation;

        printf('\n SD_WriteBlocks_DMA txDone\n');

        txDone = false;

        return WriteAddr;

  }    

}

/********************************************/

/*** Function To read from SD card via DMA***/

uint32_t SD_ReadBlocks_DMA(uint8_t *pData, uint32_t ReadAddr, uint32_t NumOfBlocks)

{  

    if(SDOperationState == SDReadOperation)

    {

        SDState = SDCardAvailable;

        return 0;

    }

    if(SDState == SDCardAvailable)

    {    

        HAL_SD_ReadBlocks_DMA(&hsd, (uint8_t *)pData, ReadAddr, NumOfBlocks);

        SDState = SDCardBusy;

        return 0;

    }

  if(rxDone == true)

  {

        ReadAddr += EACH_BLOCK_SIZE;

        SDOperationState = SDReadOperation;        

        printf('SD card Read Done \n');

        printf('Rx is %s \n',Rx);

        rxDone = false;

        return ReadAddr ;

  }

}

/********************************************/

enum SDStateEnum{

    SDCardAvailable,

    SDCardBusy

};

enum SDOperationStateEnum{

    SDIdle,

    SDWriteOperation,

    SDReadOperation

};

enum SDStateEnum SDState = SDCardAvailable;

enum SDOperationStateEnum SDOperationState = SDIdle;

/********************************************/

Thanks,

Poonam

16 REPLIES 16
Posted on June 05, 2018 at 09:37

which flags ?

The object of the forum is to have a easy path to a solution.

If its ok with you, the Solution is ?

Poonam Deoghare
Associate III
Posted on June 11, 2018 at 12:17

Hi ,

I have one new requirement as follow.

I need to find the bad sector of SD card and I should not write/ read into those?

Is there any API to find these Bad sector?

OR I have to manually do this?

Also is there any API to format the SD card?

And Regarding class speed

Class 10 :- will this type of card work with STM32F23 MCU?

Thanks,

Poonam

Posted on June 11, 2018 at 13:50

Who creates these requirements? Ask that person what they expect...

The card is going to hide a lot of defects from you, and will have a number of spare blocks internally to manage that. If you want to find them you're going to have to scan the media looking for hard errors, or there might be some specific CMD the card supports to report the defect list, or manage it. With SCSI devices these where MODE PAGES as I recall. Review the published SDMMC specification.

FatFs provides f_mkfs() to write initial structures to the media.

Not familiar with the STM32F23, the code provided by ST should work for all SD, SDHC and SDXC cards to Class 10 and beyond, and north of 400 GB in capacity. Not sure you're going to hit Class 10 numbers unless you recode the driver and optimize how you stream data to the card.

Tips, Buy me a coffee, or three.. PayPal Venmo
Up vote any posts that you find helpful, it shows what's working..
Poonam Deoghare
Associate III
Posted on June 15, 2018 at 19:13

Hi,

I will find the bad sector by implementing following.

Write fixed 512 byte and read it back . If getting the exact same then make that block as good sector.

I am trying to get the card info using HAL_SD_GetCardInfo(), but for all the cards following info is constant.

cart type = 1

card version =1

class = 5B5 

Could you please help me in this?

Posted on June 15, 2018 at 19:29

>>Write fixed 512 byte and read it back . If getting the exact same then make that block as good sector.

I don't think this is an effective manner to achieve the stated goal, there are several layers of CRC/ECC and block management between you and the flash array to learn anything useful. Like I said earlier, there are likely card level methods that will expose lower levels of functionality.

If you proceed on the current route, you should do blocks of at least 32KB, doing interaction at a sector level will be excruciating slow. In terms of media/file-system integrity I would write massive pseudo-random sequences across the media, this will expose cross-linking, or mirroring of data in multiple places.

>>Could you please help me in this?

I'm not looking to perform work assignments in an unpaid capacity.

Tips, Buy me a coffee, or three.. PayPal Venmo
Up vote any posts that you find helpful, it shows what's working..
Posted on June 15, 2018 at 20:10

Never mind Clive.

Thanks anyways.

Regards,

Poonam

Posted on June 16, 2018 at 01:54

Just trying to scope expectations. ie Help with directions, not Help moving the couch, especially when no beer and pizza involved.

You should really talk this through with colleagues and supervisors, doing a read-write-read-write a sector at a time will take forever, and is more likely to destroy the card than demonstrate integrity. The media is designed to stream, you can't afford to write multiple patterns, and the failure of the array is likely to be pattern sensitive (holds some things, won't hold other, dependent on neighbours), and hard to observe from the outside. You want to write multi GB patterns, ones that don't repeat at sector/block boundaries, then read back and validate.

I asked who defined the requirements, I didn't get an answer, and my concern here is a lack of real grasp of what can and can't be observed externally. The cards are designed to mask failure. You get a check-box item on a sell sheet, but it is entirely useless. The people involved here need to get a much better grasp of how NAND memory functions, and how SD/MMC devices function internally.

Tips, Buy me a coffee, or three.. PayPal Venmo
Up vote any posts that you find helpful, it shows what's working..