Пример #1
0
AtaError mmcConfigFs(char *fileName) {
    Uint16 index;
    AtaError ata_error;
    unsigned int diskType;

    ata_error = ATA_ERROR_NONE;

    for (index = 0; index < CSL_MMCSD_ATA_BUF_SIZE; index++) {
        gMmcWriteBuf[index] = 0x4142;
        AtaWrBuf[index] = 0x4344;
        gMmcReadBuf[index] = 0x0;
    }

    /* Call init function initialize ATA state structure */
    gpstrAtaDrive->AtaInitAtaMediaState = (AtaError (*)(void *)) MMC_initState;
    gpstrAtaMMCState->hMmcSd = mmcsdHandle;
    gpstrAtaDrive->pAtaMediaState = gpstrAtaMMCState;
    gpstrAtaDrive->AtaInitAtaMediaState(gpstrAtaDrive);

    /* Set the temp write buffer */
    gpstrAtaDrive->_AtaWriteBuffer = AtaWrBuf;

    diskType = CSL_MMCSD_ATAFS_DISKTYPE;
    /* Call ATA_systemInit() to intialize some values whcih are
     used by chk_mmc() function */
    ata_error = ATA_systemInit(gpstrAtaDrive, diskType);

    chk_mmc(gpstrAtaDrive, &diskType);
    if (diskType != CSL_MMCSD_ATAFS_DISKTYPE) {
        ata_error = ATA_systemInit(gpstrAtaDrive, diskType);
        if (ata_error != ATA_ERROR_NONE) {
            printf("ATA_systemInit Failed\n");
            printf("Format the SD card\n");
            return (ata_error);
        }
    }

    printf("\nATA File System Initialization successful\n");

    /* Find the first file available */
    ata_error = ATA_fileInit(gpstrAtaDrive, pAtaFile);
    if (ata_error) {
        printf("ATA_fileInit error (0x%x)\n", ata_error);
        return (ata_error);
    }

    ata_error = ATA_setLongDirectoryName(pAtaFile, fileName);
    if (ata_error != ATA_ERROR_NONE) {
        printf("ATA_setLongDirectoryName Failed\n");
        return (ata_error);
    }

    ata_error = ATA_createDirectoryLong(pAtaFile, fileName);
    if (ata_error != ATA_ERROR_NONE) {
        printf("ATA_createDirectoryLong Failed\n");
        return (ata_error);
    }

    ata_error = ATA_cdRoot(pAtaFile);
    if (ata_error != ATA_ERROR_NONE) {
        printf("ATA_cdRoot Failed\n");
        return (ata_error);
    }

    do {
        ata_error = ATA_findNext(pAtaFile);
        if (ata_error != ATA_ERROR_NONE) {
            printf("Cannot find created directory\n");
            return (ata_error);
        }

        ata_error = ATA_getLongName(pAtaFile, ataFileName, 0, 10);
        if (ata_error != ATA_ERROR_NONE) {
            printf("ATA_getLongName failed\n");
            return (ata_error);
        }
    } while (strcmp(ataFileName, fileName) != 0);

    ata_error = ATA_cd(pAtaFile);
    if (ata_error != ATA_ERROR_NONE) {
        printf("ATA_cdRoot Failed\n");
        return (ata_error);
    }

    /* Set the file name */
    ATA_setFileName(pAtaFile, fileName, "txt");

    ata_error = ATA_create(pAtaFile);
    if (ata_error != ATA_ERROR_NONE) {
        printf("ATA_create Failed\n");
        return (ata_error);
    } else {
        printf("\nFile Creation on SD card is Successful\n");
    }

    /* Write data to the file */
    ata_error = ATA_write(pAtaFile, gMmcWriteBuf, CSL_MMCSD_ATA_BUF_SIZE);
    if (ata_error != ATA_ERROR_NONE) {
        printf("ATA_write Failed\n");
        return (ata_error);
    } else {
        printf("\nWriting Data to the file on SD card successful\n");
    }

    /* Reset the file pointer to the beginning */
    ATA_seek(pAtaFile, 0);

    /* Read the data from the file in little endian mode */
    ata_error = ATA_readLittleEndian(pAtaFile, gMmcReadBuf,
                                     CSL_MMCSD_ATA_BUF_SIZE);
    if (ata_error != ATA_ERROR_NONE) {
        printf("ATA_readLittleEndian Failed\n");
        return (ata_error);
    } else {
        printf("\nReading Data from the file on SD card successful\n");
    }

    /* Close the file */
    ata_error = ATA_close(pAtaFile);
    if (ata_error != ATA_ERROR_NONE) {
        printf("ATA_close Failed\n");
        return (ata_error);
    }

    /* Compare the data read and data written */
    for (index = 0; index < CSL_MMCSD_ATA_BUF_SIZE; index++) {
        if (gMmcWriteBuf[index] != gMmcReadBuf[index]) {
            ata_error = 1;
            printf("\nMMCSD Read and Write Buffers do not Match\n");
            break;
        }
    }

    if (ata_error == 0) {
        printf("\nMMCSD Read and Write Buffers Match\n");
    }

    return (ata_error);
}
/**
 *  \brief  Function to configure file system and perform
 *          read/write operations
 *
 *  \param  fileName - Name of the file to be created
 *
 *  \return Test result
 */
AtaError mmcConfigFs(char    *fileName)
{
    Uint16        index;
    AtaError      ata_error;
	unsigned int diskType;

    ata_error = ATA_ERROR_NONE;

    for(index = 0; index < CSL_MMCSD_ATA_BUF_SIZE; index++)
    {
        gMmcWriteBuf[index] = 0x4142;
		AtaWrBuf[index] = 0x4344;
        gMmcReadBuf[index]  = 0x0;
    }

	/* Call init function initialize ATA state structure */
    gpstrAtaDrive->AtaInitAtaMediaState = (AtaError (*)(void *))MMC_initState;
	gpstrAtaMMCState->hMmcSd = mmcsdHandle;
    gpstrAtaDrive->pAtaMediaState = gpstrAtaMMCState;
    gpstrAtaDrive->AtaInitAtaMediaState(gpstrAtaDrive);

	/* For partitioned disk, 'diskType' should be 0
	   and for unpartiotioned disk, it should be 1
	 */
	 /* chk_mmc() function is used to check the partition type of
	    SD card.
	    ATA_systemInit() function needs to be called
	    with 'diskType' set to 0 before calling chk_mmc().
	    chk_mmc() function will check whether the disk is partitioned
	    or unpartitioned. If disk is not partitioned it will change the
	    'diskType' value to 1 otherwise it will not change the diskType value.
		After calling chk_mmc() if 'diskType' is not '0' , It means that
		the SD card is not partitioned and ATA_systemInit() needs to be
		called with 'diskType' value modified by chk_mmc() function */

	diskType = CSL_MMCSD_ATAFS_DISKTYPE;
	/* Call ATA_systemInit() to intialize some values whcih are
	  used by chk_mmc() function */
	ata_error = ATA_systemInit(gpstrAtaDrive, diskType);

	chk_mmc(gpstrAtaDrive, &diskType);
	if(diskType != CSL_MMCSD_ATAFS_DISKTYPE)
	{
		ata_error = ATA_systemInit(gpstrAtaDrive, diskType);
	    if(ata_error != ATA_ERROR_NONE)
	    {
			printf("ATA_systemInit Failed\n");
			printf("Format the SD card\n");
	        return(ata_error);
	    }
	}

	printf("\nATA File System Initialization successful\n");

	/* Find the first file available */
    ata_error =  ATA_fileInit(gpstrAtaDrive, pAtaFile);
    if(ata_error) {
        printf("ATA_fileInit error (0x%x)\n", ata_error);
        return(ata_error);
    }

	/* Set the temp write buffer */
    pAtaFile->pDrive->_AtaWriteBuffer = AtaWrBuf;

	/* Set the file name */
	ATA_setFileName(pAtaFile, fileName, "txt");

    ata_error = ATA_create(pAtaFile);
    if(ata_error != ATA_ERROR_NONE)
    {
    	printf("ATA_create Failed\n");
        return(ata_error);
    }
    else
    {
		printf("\nFile Creation on SD card is Successful\n");
	}

	/* Write data to the file */
    ata_error = ATA_write(pAtaFile, gMmcWriteBuf, CSL_MMCSD_ATA_BUF_SIZE);
    if(ata_error != ATA_ERROR_NONE)
    {
		printf("ATA_write Failed\n");
        return(ata_error);
    }
    else
    {
		printf("\nWriting Data to the file on SD card successful\n");
	}

	/* Reset the file pointer to the beginning */
	ATA_seek (pAtaFile, 0);

	/* Read the data from the file in little endian mode */
    ata_error = ATA_readLittleEndian(pAtaFile,gMmcReadBuf, CSL_MMCSD_ATA_BUF_SIZE);
    if(ata_error != ATA_ERROR_NONE)
    {
		printf("ATA_readLittleEndian Failed\n");
        return(ata_error);
    }
    else
    {
		printf("\nReading Data from the file on SD card successful\n");
	}

	/* Close the file */
    ata_error = ATA_close(pAtaFile);
    if(ata_error != ATA_ERROR_NONE)
    {
		printf("ATA_close Failed\n");
        return(ata_error);
    }

	/* Compare the data read and data written */
    for(index = 0; index < CSL_MMCSD_ATA_BUF_SIZE; index++)
    {
        if(gMmcWriteBuf[index] != gMmcReadBuf[index])
        {
            ata_error = 1;
            printf("\nMMCSD Read and Write Buffers do not Match\n");
            break;
        }
    }

    if(ata_error == 0)
    {
        printf("\nMMCSD Read and Write Buffers Match\n");
    }

    return(ata_error);
}