/***********************************************************************
 * 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  "spi.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;

volatile uint8_t spi_rx_data;
volatile uint8_t spi_rx_flag = 0;

uint8_t xdata rx_buf[256];
uint8_t xdata tx_buf[256];
/************************************************************************
 * 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 spi_irq_func(void)
{
	spi_rx_data = spi_receive_byte();
	spi_rx_flag = 1;
}

void spi_test(void)
{
		uint16_t xdata i;

		for(i=0;i<LEN;i++)
		{
			tx_buf[i]= i+0xA0;
		}		
		
		printfS("SPI Slave test start!! \r\n");
		spi_slave_init(WORK_MODE_0);

#ifdef SPI_IRQ_ENABLE									//中断方式，在app.h中开启,从机中断接收注意SPI通讯速率快慢
		printfS("SPI Slave RXNE irq test start!! \r\n");
		spi_irq_enable(SPI_RXNE_IE,spi_irq_func);	
		while(1)
		{
					
			for(i=0;i<LEN;i++)
			{		
					spi_send_byte(tx_buf[i]);	
					while(!spi_rx_flag);
					rx_buf[i] = spi_rx_data;
					spi_rx_flag = 0;		
			
			}
			printfB8(rx_buf,LEN);	

		}
		
		
#else												//查询方式	
		while(1)
		{
			for(i=0;i<LEN;i++)
			{		
				spi_send_byte(tx_buf[i]);						 
				rx_buf[i] = spi_receive_byte();	
					
			}
			printfB8(rx_buf,LEN);	
		}

#endif
					
}

