// Built with BFTC Rev. 1.0.0, mar 19 agosto 2014 09:46:37

 

/*

    Copyright 2014 Digital Technology Art SRL

    This file is part of Blackfin Toolchain (BFTC) project.

 

    BFTC is free software: you can redistribute it and/or modify

    it under the terms of the GNU General Public License as published by

    the Free Software Foundation, either version 3 of the License, or

    (at your option) any later version.

 

    BFTC is distributed in the hope that it will be useful,

    but WITHOUT ANY WARRANTY; without even the implied warranty of

    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the

    GNU General Public License for more details.

 

    You should have received a copy of the GNU General Public License

    along with Nome-Programma.  If not, see <http://www.gnu.org/licenses/>.

*/

 

#include "main.h"

 

#define ST st = Get_TimerCounter(TIMER_0)

#define ET et = Get_TimerCounter(TIMER_0)

 

#define TIMEBASE 0xFFFFFFFF

 

void display(const char * header, unsigned int et, unsigned int st)

{

    double tx;

    if(st > et) tx = TIMEBASE - st + et;

    else tx = et - st;

    printf("%s Test, Elapsed time: %10.3f ms\n", header, tx / 100000.0);

}

 

#define NSECT   128 // 128 * 512 = 65536

int main(void)

{

    unsigned long   st, et;

    int             sector, i, er;

    struct SD_Info  info;

    unsigned char   buf[512];

   

    Set_PLL(16, 4);     // CORE: 25MHz * 16 = 400MHz, SCLK: 400MHz / 4 = 100MHz

    Set_Port();         // Set the port according to project set

    Set_Uart0(115200);  // printf is redirected to UART0

   

    while(1)            // Main loop

    {

        printf("Press any key to start the test\n");

        GetChar_Uart0();

        SD_Power(true);

        DelayUs(100000);    // 100 ms power up

        er = SD_Init();

        if(er == SD_STS_OK)

        {

            printf("MMC/SD Init OK\n");

            SD_GetInfo(&info);

            printf("Manufacturer        = %d\n",            (int) info.manufacturer);

            printf("OEM                 = %c%c%c\n",        (int) info.oem[0], (int) info.oem[1], (int) info.oem[2]);

            printf("Product             = %c%c%c%c%c%c\n",  (int) info.product[0], (int) info.product[1], (int) info.product[2], (int) info.product[3], (int) info.product[4], (int) info.product[5]);

            printf("Revision            = %d\n",            (int) info.revision);

            printf("Serial              = %u\n",            info.serial);

            printf("Manufacturing year  = %d\n",            2000 + info.manufacturing_year);

            printf("Manufacturing month = %d\n",            (int) info.manufacturing_month);

            printf("Capacity            = %u Kbyte\n",      (int) (info.capacity / 1024));

            Set_TimerCounter(TIMER_0);

            Set_TimerEnable(TIMER_0, true);

           

            printf("Writing %d sectors ... ", NSECT);

            for(i = 0; i < 512; i++)

                buf[i] = i;

            ST;

            for(sector = 0; sector < NSECT; sector++)

                SD_Write(sector, buf, 512);

            ET; display("Writing", et, st);

           

            printf("Reading %d sectors ... ", NSECT);

            ST;

            for(sector = 0; sector < NSECT; sector++)

                SD_Read(sector, buf, 512);

            ET; display("Reading", et, st);

           

            // Verify last read

            for(i = 0; i < 512; i++)

                if(buf[i] != (i & 0xFF))

                    break;

            if(i < 512)

                printf("Compare error at %d\n", i);

            else

                printf("Done, test OK\n");

            printf("Test finished. Remove SD\n");

           

            // Turn off

            SD_Power(false);

        }

        else

        {

            switch(er)

            {

                case SD_STS_NOCARD: printf("MMC/SD not inserted\n");

                                    break;

                default:            printf("SD/MMC Unknown error [%d]!\n", er);

            }

            SD_Power(false);

            Set_SPI(SPI_PP);

            printf("PB port status %02x\n", Get_Controls());

        }

    }

    return 0;

}