/***********************************************************************
 * Copyright (c)  2017 - 2020, Unicmicro Co.,Ltd .
 * All rights reserved.
 * Filename    : app.c
 * Description : app driver source file
 * Author(s)   : wanyi
 * version     : V1.0
 * Modify date : 2020-07-27
 ***********************************************************************/
#include  "system_um800x.h"
#include  "app.h"
#include  "config.h"
#include  "common.h"
#include  "uart0.h"
#include  "eflash.h"

volatile uint8_t rx_flag ;
volatile uint8_t uart0_rx_buf[32];
volatile uint8_t uart0_tx_buf[32];
volatile uint16_t rx_count = 0;
volatile uint16_t tx_count = 0;
/************************************************************************
 * function   : uart0_rec_pro
 * Description: uart0_rec_pro 串口接收处理回调函数
 * input : none
 * return: none
 ************************************************************************/
void uart0_rec_pro(void)
{
	uart0_rx_buf[rx_count++]= uart0_recv_byte();
	rx_flag = 1;	
}

/************************************************************************
 * function   : uart_init
 * Description: uart_init 串口初始化
 * input : none
 * return: none
 ************************************************************************/
void uart_init(void)
{
	rx_flag = 0;
	uart0_init(UART0_BAUD_RATE);
    uart0_irq_init(UART0_IRQ_ENABLE,uart0_rec_pro);
}


void eflash_page_check_8bits(uint16_t base_addr,uint8_t w_val, uint8_t page)
{
    uint16_t i;
    
    for(i = 0; i < PAGE_SIZE; i++)
    {
        eflash_write_byte(base_addr+page*PAGE_SIZE+i,w_val);
    }
    
    for(i = 0; i < PAGE_SIZE; i++)
    {
        if(eflash_read_byte(base_addr+page*PAGE_SIZE+i) != w_val)
        {
            printf("Error: 8bit Read not equal write![0x%x]\n",(base_addr+page*PAGE_SIZE+i));
            break;
        }
    }

}
void eflash_page_check_bytes(uint16_t base_addr,uint8_t w_val, uint32_t length)
{
    uint32_t i;
 
    uint8_t  write_data[DATA_SIZE]={0};
    uint8_t  read_data[DATA_SIZE]={0};
    
    //多字节读写测试
    eflash_erase_page(base_addr);								//页擦除

    for(i = 0; i < length; i++)
    {
        if(eflash_read_byte(base_addr+i) != 0xff)
        {
                printf("Error: erase fail![0x%lx]\n",(base_addr+i));
                break;
        }
    }	
    
    for(i=0;i<length;i++)
    {
        write_data[i] = w_val;                          
    }
    
    eflash_write_bytes(base_addr, write_data, length);                       //多字节写数据
        
        
   if(length == eflash_read_bytes(base_addr, read_data, length))             //多字节读    
   {
        for(i=0;i<length;i++)
        {
            if(read_data[i] != w_val)
            {
                printf("Error: multibyte Read not equal Write![0x%x]\n",(base_addr+i));
                break;
            } 
//            printf("read_data:[0x%bx]\n",read_data[i]);
        }          
   }
        
    
}

void eflash_page_check_byte(uint16_t base_addr, uint8_t start_page, uint8_t num_page)
{
    uint16_t i;
    uint8_t page;

    //单字节读写测试

    for(page = start_page; page < num_page; page++)
    {
        eflash_erase_page(base_addr + page * PAGE_SIZE);								//页擦除
        for(i = 0; i < PAGE_SIZE; i++)
        {
            if(eflash_read_byte(base_addr+page*PAGE_SIZE+i) != 0xff)
            {
                    printf("Error: erase fail![0x%lx]\n",(base_addr+page*PAGE_SIZE+i));
                    break;
            }
        }	
        eflash_page_check_8bits(base_addr,0x55,page);                                   //单字节读写
    }


}

void eflash_sn_test(void)
{
	uint8_t chip_sn[16];
	uint8_t chip_UID[8];
    
	printfS("--------read chip sn for 16 bytes-------\n");
    read_sequence(chip_sn);   
	printfS("SN(16bytes): \n");
	printfB8(chip_sn,16);
	printfS("--------end-------\n\n");

	
	printfS("--------read chip UID for 8 bytes-------\n");
    read_UID(chip_UID);
	printfS("UID(8bytes): \n");
	printfB8(chip_UID,8);
	printfS("--------end-------\n\n");


}

void eflash_test(void)
{
	eflash_init(SystemCoreClock);

	/********MAIN区擦读写测试*********/
    //单字节读写测试
	printfS("0x1800- 0x2000 write and read by byte test start\r\n");
	eflash_page_check_byte(0x1800,0,1);
   	printfS("0x1800- 0x2000 write and read by byte test end\r\n"); 
    
    //多字节读写测试
    printfS("0x1800 write and read by multi bytes test start\r\n");
    eflash_page_check_bytes(0x1800,0xa5,DATA_SIZE);                                          //多字节读写
	printfS("0x1800 write and read by multi bytes test end\r\n");

    //SN(16 Bytes or 8 Bytes)
	eflash_sn_test();
    
	 
}		
