#include "my_includes.h"
#include <stdio.h>
#include <stdlib.h>
#include "LCD.h"
#include "LED.h"
#include "FatFileSystem.h"
#include "SDCardDriver.h"
#include "UART.h"



void welcome_led(void){
    int i;
    for(i=0;i<5;i++){
        LED_AllOn();
        usleep(100*1000);
        LED_AllOff();
        usleep(100*1000);
    }
    LED_AllOn();
}

bool test_lcd(void){
    bool bSuccess = FALSE;
    FILE *fp;
    fp = fopen(LCD_NAME, "w");
    if (!fp)
        printf("[LCD] open %s fail\r\n", LCD_NAME);
/*    
    if (LCD_Open()){
        LCD_TextOut("DE2-70\nTest\n");
        LCD_Close();
        bSuccess = TRUE;
    }else{
        printf("[LCD] test fail\r\n");
    }
*/    
    return bSuccess;

}

bool test_sdcard(void){
    bool bSuccess = FALSE;
    if (Fat_Mount(FAT_SD_CARD)){
        FAT_FILE_HANDLE hFile;
        bSuccess = Fat_FileOpen(&hFile, "test.txt");
        if (bSuccess){
            Fat_FileClose(&hFile);
            //printf("[SD]find file test.txt\r\n");
        }else{    
            //printf("[SD]cannot file test.txt\r\n");
        }   
        Fat_Unmount();
    }else{
        //printf("[SD]sdcard test fail\r\n");
    }
    printf("sdcard test %s\r\n", bSuccess?"success":"fail");
    return bSuccess;
}

bool test_ssram(void){
    bool bSuccess = TRUE;
    volatile alt_u8 *ssram = (alt_u8 *)SSRAM_BASE;
    alt_u32 time_start, time_elapsed;
    time_start = alt_nticks();
    alt_u8 data;
    const alt_u32 len = SSRAM_SPAN; 
    
    int i;
    data = 0;
    for(i=0;i<len;i++){
        *(ssram + i) = data;
        data++;
        if (data == 252)
            data = 0;
    }    
    alt_dcache_flush_all(); // flush sram cache becuase sram will be used by tv-decoder controller.    
         
    data = 0;         
    for(i=0;i<len && bSuccess;i++){
        if (*(ssram + i) != data){
            bSuccess = FALSE;
            printf("ssram compare fail at index %d\r\n", i);
        }            
        data++;
        if (data == 252)
            data = 0;
            
    }        
    time_elapsed = alt_nticks() - time_start;
    printf("ssram test %s, elapsed %ld nticks\r\n", bSuccess?"success":"fail",time_elapsed);
    return bSuccess;
}

bool test_sdram(void){  // OK
    bool bSuccess = TRUE;
    volatile alt_u8 *ssram = (alt_u8 *)SDRAM_8M_BASE;
    alt_u32 time_start, time_elapsed;
    time_start = alt_nticks();
    alt_u8 data;
    const alt_u32 len = SDRAM_8M_SPAN; 
    
    int i;
    data = 0;
    for(i=0;i<len;i++){
        *(ssram + i) = data;
        data++;
        if (data == 252)
            data = 0;
    }      
    alt_dcache_flush_all(); // flush sram cache becuase sram will be used by tv-decoder controller.  
         
    data = 0;         
    for(i=0;i<len && bSuccess;i++){
        if (*(ssram + i) != data){
            bSuccess = FALSE;
            printf("sdram compare fail at index %d\r\n", i);
        }            
        data++;
        if (data == 252)
            data = 0;
            
    }        
    time_elapsed = alt_nticks() - time_start;
    printf("sdram test %s, elapsed %ld nticks\r\n", bSuccess?"success":"fail",time_elapsed);
    return bSuccess;
}

bool test_uart(void){
    bool bSuccess = FALSE;
    if (UART_Open()){
        UART_WriteString("Hello from UART\r\n");
        UART_Close();
        printf("[UART]test success\r\n");
    }else{
        printf("[UART]test fail\r\n");
    }
    return bSuccess;
}



int main()
{
 //   DEBUG_Enable();
    printf("\r\n===== DE2-70 production test [3] =====\r\n");
    welcome_led();
    test_lcd();
    test_sdcard();
    test_uart();
    test_sdram();
    test_ssram();
    while(1){
        welcome_led();
    }        
    return 0;
}
