►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.

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;
}



Tutorial Index
Interface to Chan’s Library of functions SD Card Initialization
Target development platform Reading and Writing a single sector
Setting up the SPI port during startup.A51 Working with diskio.c
Global type definitions and variables Pulling it all together
Basic SPI function Index Page

Downloads

Comments

Bobbyerilar
Thu Mar 28 2024, 08:08 am
pb58
Thu Mar 28 2024, 05:54 am
Clarazkafup
Thu Mar 28 2024, 02:24 am
Walterkic
Thu Mar 28 2024, 01:19 am
Davidusawn
Wed Mar 27 2024, 08:30 pm
Richardsop
Tue Mar 26 2024, 10:33 pm
Stevencog
Tue Mar 26 2024, 04:26 pm
Bernardwarge
Tue Mar 26 2024, 11:15 am