AnsweredAssumed Answered

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

Question asked by Poonam Deoghare on May 29, 2018
Latest reply on Jun 16, 2018 by Clive Two.Zero

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

Outcomes