
/* 
**
** routine to read one or more sectors from the compact flash card into
** XRAM memory at the specified buffer address.
**
*/

void CF_ReadSector(unsigned char xdata *buf, unsigned long lsn, unsigned char cnt)
{
    union LSN32 lsn_split;
    unsigned int i;

    lsn_split.l = lsn;

    CF_WaitReady();                                     /* wait for card to be ready */
    XBYTE[CF_SEC_CNT_REG] = cnt;
    XBYTE[CF_SEC_NUM_REG] = lsn_split.b[3];             /* (LBA 07-00) */
    XBYTE[CF_CYL_LOW_REG] = lsn_split.b[2];             /* (LBA 15-08) */
    XBYTE[CF_CYL_HIG_REG] = lsn_split.b[1];             /* (LBA 23-16) */
    XBYTE[CF_DR_HEAD_REG] = (lsn_split.b[0] & 0x0F) | 0xE0; /* 1 LBA 1 DRV0 (LBA 27-24) */
    XBYTE[CF_COMMAND_REG] = CF_CMD_READ_SEC;            /* send out the read command */

    while(cnt--)                                        /* loop to handle sectors */
    {
        CF_WaitReady();                                 /* wait for card to be ready */
        i = 512;                                        /* 512 bytes per sector */
        while(i--)
        {
            *buf++ = XBYTE[CF_DATA_REG];
        }
    }
}
