#include "HW_Config.h"
#include "uart_memory_drv.h"
#include "CnCpp.h"

#define MEMORY_UART 	5

unsigned char memory_uart_tx[32];

unsigned int memory_is_busy(void)//1:is busy 0:no busy
{
	if(!get_uart_tx_complete_delay_flag(MEMORY_UART))return 1;
	
	return 0;
}

void set_memory_tx_data(unsigned char cmd, unsigned char * pdata, unsigned int len)
{
	unsigned int i = 0;
	
	if(len > 32)return;
	
	memory_uart_tx[0] = MEMORY_UART_HEAD;
	memory_uart_tx[1] = 0x55;
	memory_uart_tx[2] = cmd;
	memory_uart_tx[3] = len;
	for(i=0;i<len;i++){
		memory_uart_tx[4+i] = (*pdata);
		pdata++;
	}
	memory_uart_tx[len + 4] = sum8_data(memory_uart_tx, (len + 4));
	memory_uart_tx[len + 5] = MEMORY_UART_END;
	
	uart_tx_over(MEMORY_UART, memory_uart_tx, (len + 6));
}

//memory brd
void uart5_rx_task(void)
{
	uart_info_8bit_struct * p_uart_info;
	p_uart_info = &uart_info_8bit[MEMORY_UART];
	
	if(p_uart_info->rx_complete){
		p_uart_info->rx_complete = 0;
		if((p_uart_info->rx_len <= UART_RX_BUF_MAX)&&(p_uart_info->rx_len >= 6)){
			if(((p_uart_info->rx_len - 6) == p_uart_info->rx_buf[3])&&(0xAA == p_uart_info->rx_buf[1])&&\
					(MEMORY_UART_HEAD == p_uart_info->rx_buf[0])&&(MEMORY_UART_END == p_uart_info->rx_buf[p_uart_info->rx_len-1])){
				if(0 == (check_sum8(p_uart_info->rx_buf, (p_uart_info->rx_len-2)))){
					LoadExRecordViaMemory(&(p_uart_info->rx_buf[2]));
				}
			}
		}
	}
}

void uart_memory_task(void)
{
	uart5_rx_task();
}


