Tuesday 8 January 2013

Disk IO functions : MMC SD Card interfacing

Disk IO functions : MMC SD Card interfacing

►Disk related functions

The only file you need to modify is diskio.c, and the functions within it. This module is the interface for Chan’s library to your SD card. If the functions within this file are written as wrappers to your fundamental functions, Chan’s library will work without any problems.

The functions in this module are as follows: disk_initialize, disk_status, disk_read, disk_write, and disk_ioctl. Also note that the only feature within disk_ioctl that is used is CTRL_SYNC. These functions, along with all of the previous functions, will do the job.

CODE:
DSTATUS disk_initialize( BYTE drv )
{
   /* Supports only single drive */
   if( drv != 0)
      return STA_NOINIT;
 
   /* if initialization succeeds... */
   if( !SD_Init() )
   {
      /* Clear STA_NOINIT */
      Stat &= ~STA_NOINIT;
   }
 
   /* return current status */
   return( Stat );
}

DSTATUS disk_status( BYTE drv   )
{
   /* Supports only single drive */
   if( drv != 0)
      return STA_NOINIT;

   /* return current status */
   return( Stat );
}

DRESULT disk_read ( BYTE drv, BYTE *buff, DWORD sector, BYTE count )
{
   /* Supports only single drive and must have a size of 1 sector */
   if( drv || !count || (count>1) )
      return( RES_PARERR );

   /* if we haven't initialized the card yet... */
   if( Stat & STA_NOINIT )
      return( RES_NOTRDY );
 
   /* Single block read */
   if( SD_ReadSector( sector, buff ) )
      return( RES_ERROR );
 
   /* return successful result: OK */
   return( RES_OK );
}

#if _READONLY == 0
DRESULT disk_write( BYTE drv, const BYTE *buff, DWORD sector, BYTE count )
{
   /* Supports only single drive and must have a size of 1 sector */
   if( drv || !count || (count>1) )
      return( RES_PARERR );

   /* if we haven't initialized the card yet... */
   if( Stat & STA_NOINIT )
      return( RES_NOTRDY );
   
   /* Single block write */
   if( SD_WriteSector( sector, buff ) )
      return( RES_ERROR );
   
   /* return successful result: OK */
   return( RES_OK );
}
#endif // _READONLY

DRESULT disk_ioctl ( BYTE drv, BYTE ctrl, void *buff )
{
   DRESULT res;
   BYTE  *ptr = buff;

   /* Supports only single drive */
   if( drv != 0)
      return RES_PARERR;
 
   /* if we haven't initialized the card yet... */
   if( Stat & STA_NOINIT )
      return RES_NOTRDY;
 
   res = RES_ERROR;
 
   switch( ctrl )
   {
      /* Flush dirty buffer if present */
      case CTRL_SYNC :
         SPI_EnableCS();
         if( SD_WaitForReady() == 0xFF )
            res = RES_OK;
         break;
 
      default:
         res = RES_PARERR;
         break;
   }
 
   SPI_DisableCS();
   SPI_Byte( 0xFF );
   return res;
}
 

No comments:

Post a Comment