/***********************************************************************
 * Copyright (c)  2017 - 2022, Unicmicro Co.,Ltd .
 * All rights reserved.
 * Filename    : app.c
 * Description : app source file
 * Author(s)   : Dan
 * version     : V1.0
 * Modify date : 2022-08-10
 ***********************************************************************/
#include  "app.h"
#include  "config.h"
#include  "common.h"
#include  "uart0.h"
#include  "uart1.h"
#include  "uart2.h"
#include  "qspi.h"

uint32_t g_data_buf[512];

#ifdef UART0_COMM
uint8_t uart0_rx_flag = 0;
uint8_t uart0_rx_buf[64];
uint8_t uart0_rx_count = 0;
#endif

#ifdef UART1_COMM
uint8_t uart1_rx_flag = 0;
uint8_t uart1_rx_buf[64];
uint8_t uart1_rx_count = 0;
#endif

#ifdef UART2_COMM
uint8_t uart2_rx_flag = 0;
uint8_t uart2_rx_buf[64];
uint8_t uart2_rx_count = 0;
#endif

#ifdef UART0_COMM
/************************************************************************
 * function   : uart0_rec_pro
 * Description: uart0 rec pro 串口接收处理回调函数
 * input : none
 * return: none
 ************************************************************************/
void uart0_rec_pro(void)
{
	uart0_rx_buf[uart0_rx_count] = uart0_recv_byte();
	uart0_rx_count++;
	uart0_rx_flag = 1;	
}
#endif

#ifdef UART1_COMM
/************************************************************************
 * function   : uart1_rec_pro
 * Description: uart1 rec pro 串口接收处理回调函数
 * input : none
 * return: none
 ************************************************************************/
void uart1_rec_pro(void)
{
	uart1_rx_buf[uart1_rx_count] = uart1_recv_byte();
	uart1_rx_count++;
	uart1_rx_flag = 1;	
}
#endif

#ifdef UART2_COMM
/************************************************************************
 * function   : uart1_rec_pro
 * Description: uart1 rec pro 串口接收处理回调函数
 * input : none
 * return: none
 ************************************************************************/
void uart2_rec_pro(void)
{
	uart2_rx_buf[uart2_rx_count] = uart2_recv_byte();
	uart2_rx_count++;
	uart2_rx_flag = 1;	
}
#endif
/************************************************************************
 * function   : uart_init
 * Description: uart_init 串口初始化
 * input : none
 * return: none
 ************************************************************************/
void uart_init(void)
{
#ifdef  UART0_COMM     
	uart0_rx_flag = 0;
	uart0_init(PCLK, UART0_BAUD_RATE);
	uart0_irq_init(UART0_IRQ_ENABLE, uart0_rec_pro);
#endif

#ifdef  UART1_COMM	
	uart1_rx_flag = 0;	
	uart1_init(PCLK, UART1_BAUD_RATE);
	uart1_irq_init(UART1_IRQ_ENABLE, uart1_rec_pro);
#endif

#ifdef  UART2_COMM    
    uart2_rx_flag = 0;	
	uart2_init(PCLK, UART2_BAUD_RATE);
	uart2_irq_init(UART2_IRQ_ENABLE, uart2_rec_pro);
#endif
}

/************************************************************************
 * function   : qspi_flash_test
 * description: qspi_flash_test
 * input 	  : none
 * return	  : none
 ************************************************************************/
void qspi_flash_test(void)
{
	uint16_t device_id;
	uint32_t flash_id;
	uint8_t rx_buffer[10];
	uint8_t tx_buf[8] = {0xc0,0xc1,0xc2,0xc3,0xc4,0xc5,0xc6,0xc7};
	uint32_t i;

	device_id = flash_read_device_id();                           //获取设备 ID
	flash_id = flash_read_id();                                
	printfS("device id:0x%04x\n", device_id);
	printfS("flash id:0x%03x\n ", flash_id);
	
	erase_flash_sector(0x000000);                                 //擦除外部flash
	printfS("\npage erase end...\n");

#if QSPI_MODE == SIN_MODDE                                        //be defined in qspi.h
	flash_write_page(tx_buf, 0x000000, 8);                        //往flash写数据
	printfS("page write end...\n");
	flash_read_bytes(rx_buffer, 0x000000, 8);                     //往flash将写入的数据读出来，采用一线方式读取
    
#elif QSPI_MODE == DUAL_MODE                                      //be defined in qspi.h
	flash_write_page(tx_buf, 0x000000, 8);                        //往flash写数据
	printfS("page write end...\n");                               
	flash_2read_bytes(rx_buffer, 0x000000, 8);                    //往flash将写入的数据读出来，采用二线方式读取
    
#elif QSPI_MODE == QUAD_MODE                                      //be defined in qspi.h
	flash_4write_page(tx_buf, 0x000000, 8);                       //往flash写数据
	printfS("page write end...\n");                               
	flash_4read_bytes(rx_buffer, 0x000000, 8);                    //往flash将写入的数据读出来，采用四线方式读取
    
#endif
	for( i=0; i<8; i++)
	{
		printfS("rx_buffer[%d]:0x%02x\n", i, rx_buffer[i]);
	}
}

/************************************************************************
 * function   : soc test
 * description: soc test
 * input 	  : none
 * return	  : none
 ************************************************************************/
void soc_test(void)
{
    printfS("System run at %dHz\r\n", SystemCoreClock);
    
	qspi_init(QSPI_LINE_1, WORK_MODE_0,0x4);                       //qspi 初始化配置
    qspi_flash_test();                                            
}


