/*==============================================================================
 *   Copyright (c) Texas Instruments Inc 2002, 2003, 2004
 *
 *   Use of this software is controlled by the terms and conditions found in the
 *   license agreement under which this software has been supplied.
 *==============================================================================
 */

/**=============================================================================
 *   @file  Emif_example.c
 *
 *
 *   @desc  Example of EMIF module of CSL
 *
 *==============================================================================
 *   @n <b> Test Description </b>
 *   @n The example shows how to test the EMIF.
 *      1. Initilizing the EMIF Hardware with required values for SDRAM
 *      2. Write a particular pattern of data to SDRAM
 *      3. Read verify the pattern is succefully written to SDRAM
 *==============================================================================
 *
 *   <b> Test Procedure </b>
 *   @verbatim
 *      1. Configure the CCS setup to work with the emulator being used.
 *         Refer CCS manual for setup configuration
 *      2. Load required GEL file depending on the board used
 *      3. Launch CCS window
 *      4. Open project emif_example.pjt
 *      5. Build the project and load the .out file of the project.
 *      6. The test can be executed from the main().
 *
 *   @endverbatim
 *
 *
 *==============================================================================
 */

/*==============================================================================
 *  Revision History
 *  ===============
 *  12-may-2005 Ganesh K  File created.
 *
 *==============================================================================
 */

#include "Emif_example.h"

/* Variable to count the test failures */
Uint32                   emifTestFail = EMIF_TEST_PASSED;

/* Emif Hardware setup structure */
CSL_EmifHwSetup          hwSetup;

/* Handle for the module */
CSL_EmifHandle           hEmif = NULL;

/*
 *==============================================================================
 *  @func   main
 *
 *  @desc
 *     This is the main routine for the file.
 *
 *  @arg
 *      NONE
 *
 *  @return
 *      NONE
 *==============================================================================
*/
void
main (
    void
)
{
    /* Perform the test */
    emif_csl_example_test ();

    /* Check for errors in test */
    if (emifTestFail == EMIF_TEST_PASSED) {
        printf ("\nEmif CSL Example Test PASSED\n");
    }
    else {
        printf ("\nEmif CSL Example Test FAILED\n");
    }
}

/*
 *==============================================================================
 *  @func   emif_csl_example_test
 *
 *  @desc
 *     This is the test routine which SDRAM write, read and verify data.
 *
 *  @arg
 *      NONE
 *
 *  @return
 *      NONE
 *==============================================================================
*/
void
emif_csl_example_test (
    void
)
{
    CSL_EmifObj              emifObj;
    CSL_Status               status;
    Uint32                  *arg;
    Uint8                    pattern;
    Uint32                   i, j, k;

    CSL_sysInit();

    /* Initialize the emif CSL module */
    status = CSL_emifInit (NULL);
    if (status != CSL_SOK) {
        printf ("EMIF: Initialization... Failed.\n");
        printf ("\tReason: CSL_emifInit failed [Status: %d] \n ", status);
        emifTestFail++;
        return;
    }
    else {
        printf ("EMIF: Initialization... Passed.\n");
        //printf ("\tReason: CSL_emifInit failed [Status: %d] \n ", status);
    }

    /* Open the emif CSL module */
    hEmif = CSL_emifOpen (&emifObj, CSL_EMIF, NULL, &status);
    if ((hEmif == NULL) || (status != CSL_SOK)) {
        printf ("EMIF: Open invalid instance... Failed\n");
        printf ("\tReason: CSL_emifOpen failed [Status: %d] \n ", status);
        emifTestFail++;
        return;
    }
    else {
        printf ("EMIF: Invalid instance... Passed.\n");
    }

    /* Do the required setup for SDRAM. Refer SDRAM data sheet for values */
    /* Setting SDRAM timimng register SDTIMR */
    hwSetup.emClkCycles.tras = EMIF_TEST_EM_CLK_CYCLES_3;
    hwSetup.emClkCycles.trc = EMIF_TEST_EM_CLK_CYCLES_5;
    hwSetup.emClkCycles.trcd = EMIF_TEST_EM_CLK_CYCLES_1;
    hwSetup.emClkCycles.trfc = EMIF_TEST_EM_CLK_CYCLES_6;
    hwSetup.emClkCycles.trp = EMIF_TEST_EM_CLK_CYCLES_1;
    hwSetup.emClkCycles.trrd = EMIF_TEST_EM_CLK_CYCLES_1;
    hwSetup.emClkCycles.twr = EMIF_TEST_EM_CLK_CYCLES_1;
    /* Setting required refresh rate */
    hwSetup.refreshRate = EMIF_TEST_SDRAM_REFRESH;
    hwSetup.sdramPageSize = CSL_EMIF_512WORD_9COL_ADDR;
    hwSetup.sdramIntBankSetup = CSL_EMIF_4_SDRAM_BANKS;
    hwSetup.sdramCasLaten = CSL_EMIF_CAS_LATENCY_SET_TO_2;
    hwSetup.narrowMode = CSL_EMIF_32BIT_USAGE;
    hwSetup.sdramSelfRefreshEn = CSL_EMIF_SELF_REFRESH_DISABLE;
    /* Setting values in SDRAM Self reresh exit timing register SDSRETR */
    hwSetup.txs = EMIF_TEST_ECLKOUT_CYCLES_6;

    /* Setup EMIF hardware parameters */
    status = CSL_emifHwSetup (hEmif, &hwSetup);
    if (status != CSL_SOK) {
        printf ("EMIF: Hardware setup... Failed.\n");
        emif_error_exit ();
        emifTestFail++;
    }
    else {
        printf ("EMIF: Hardware setup... Passed.\n");
    }

    /*
     * Write a Pattern to SDRAM. The pattern written consist of clock of
     * pattern written to SDRAM. The pattern is nth block starts with value
     * 'n' and ends with value 'n-1'. for eg block0 start with 0x00 and ends
     * with 0xFF. Block 0xF starts with 0xF ends with 0xE
     *
     */

    arg = (Uint32 *) EMIF_TEST_SDRAM_START_ADDR;

    for (j = 0; j <= 15; j++) {
        pattern = j;
        for (i = 0; i <= 50; i++) {
            *arg = pattern++;
            arg++;
        }
    }

    k = EMIF_TEST_PASSED;

    /* Read and verify Pattern From SDRAM */
    arg = (Uint32 *) EMIF_TEST_SDRAM_START_ADDR;

    for (j = 0; j <= 15; j++) {
        pattern = j;
        for (i = 0; i <= 50; i++) {
            if (*arg != pattern++) {
                /* If pattern doesnt match set error */
                k = 1;
                break;
            }
            arg++;
        }
        /* On error exit */
        if (k != EMIF_TEST_PASSED) {
            break;
        }
    }

    /* Print the test result */
    if (k != EMIF_TEST_PASSED) {
        emifTestFail++;
        printf ("EMIF:SDRAM Read verify ......Failed\n");
    }
    else {
        printf ("EMIF:SDRAM Read verify ......Passed\n");
    }

    /* Check closing the module */
    status = CSL_emifClose (hEmif);
    if (status != CSL_SOK) {
        emifTestFail++;
        printf ("EMIF: Close Emif instance... Failed\n");
    }
    else {
        printf ("EMIF: Close Emif instance... Passed\n");
    }

    return;
}

/*
 * =============================================================================
 *  @func   emif_error_exit
 *
 *  @desc
 *     This calls the module close function in case of error
 *
 *  @arg
 *      NONE
 *
 *  @return
 *      NONE
 * =============================================================================
 */
void
emif_error_exit (
    void
)
{
    CSL_Status               status;

    /* Check closing the module */
    status = CSL_emifClose (hEmif);
    if (status != CSL_SOK) {
        emifTestFail++;
        printf ("EMIF: Close Emif instance... Failed\n");
    }
    else {
        printf ("EMIF: Close Emif instance... Passed\n");
    }
}