#include "main.h"
#include "HW_config.h"
#include "user_eeprom.h"
#include "user_norflash.h"
#include "uart_printer_drv.h"

unsigned char tx_text_buf[32];
unsigned char rx_text_buf[32];

unsigned int hw_test_mode = 0;

static void wait(void)
{
	__nop();__nop();__nop();__nop();
	__nop();__nop();__nop();__nop();
	__nop();__nop();__nop();__nop();
	__nop();__nop();__nop();__nop();
}

unsigned int hw_test_init(void)
{
	unsigned int i = 0;
	
	for(i=0;i<32;i+=2){
		tx_text_buf[i] = 0x55;
		tx_text_buf[i+1] = 0xAA;
		
		rx_text_buf[i] = 0;
		rx_text_buf[i+1] = 0;
	}
}

unsigned int hw_test_deinit(void)
{
	unsigned int i = 0;
	
	for(i=0;i<32;i++){
		tx_text_buf[i] = 0;
		rx_text_buf[i] = 0;
	}
}

unsigned int hw_uart_test_all(void)//test uart1-7
{
	unsigned int res = 0;
	unsigned int i = 0;
	unsigned char data[4] = {0};
	unsigned int test_tick = 0;
	
	HAL_Delay(50);
	
	while(0 == uart_info_8bit[1].tx_complete);
	while(0 == uart_info_8bit[2].tx_complete);
	while(0 == uart_info_8bit[3].tx_complete);
	while(0 == uart_info_8bit[4].tx_complete);
	while(0 == uart_info_8bit[5].tx_complete);
	while(0 == uart_info_8bit[6].tx_complete);
	while(0 == uart_info_8bit[7].tx_complete);
	
	//UART1
	LL_USART_DisableIT_RXNE(USART1);
	LL_USART_ClearFlag_IDLE(USART1);
	LL_USART_DisableIT_IDLE(USART1);
	while(LL_USART_IsActiveFlag_RXNE(USART1)){
		data[0] = LL_USART_ReceiveData8(USART1);
	}
	
	//UART2
	LL_USART_DisableIT_RXNE(USART2);
	LL_USART_ClearFlag_IDLE(USART2);
	LL_USART_DisableIT_IDLE(USART2);
	while(LL_USART_IsActiveFlag_RXNE(USART2)){
		data[0] = LL_USART_ReceiveData8(USART2);
	}
	
	//UART3
	LL_USART_DisableIT_RXNE(USART3);
	LL_USART_ClearFlag_IDLE(USART3);
	LL_USART_DisableIT_IDLE(USART3);
	while(LL_USART_IsActiveFlag_RXNE(USART3)){
		data[0] = LL_USART_ReceiveData8(USART3);
	}
	
	//UART4
	LL_USART_DisableIT_RXNE(UART4);
	LL_USART_ClearFlag_IDLE(UART4);
	LL_USART_DisableIT_IDLE(UART4);
	while(LL_USART_IsActiveFlag_RXNE(UART4)){
		data[0] = LL_USART_ReceiveData8(UART4);
	}
	
	//UART5
	LL_USART_DisableIT_RXNE(UART5);
	LL_USART_ClearFlag_IDLE(UART5);
	LL_USART_DisableIT_IDLE(UART5);
	while(LL_USART_IsActiveFlag_RXNE(UART5)){
		data[0] = LL_USART_ReceiveData8(UART5);
	}
	
	//UART6
	LL_USART_DisableIT_RXNE(USART6);
	LL_USART_ClearFlag_IDLE(USART6);
	LL_USART_DisableIT_IDLE(USART6);
	while(LL_USART_IsActiveFlag_RXNE(USART6)){
		data[0] = LL_USART_ReceiveData8(USART6);
	}
	
	//UART7
	LL_USART_DisableIT_RXNE(UART7);
	LL_USART_ClearFlag_IDLE(UART7);
	LL_USART_DisableIT_IDLE(UART7);
	while(LL_USART_IsActiveFlag_RXNE(UART7)){
		data[0] = LL_USART_ReceiveData8(UART7);
	}
	
	while(test_tick <= 7){
		test_tick++;
		//--------------------------uart1------------------------------
		if(1 == test_tick){
			rx_text_buf[0] = 0;
			rx_text_buf[1] = 0;
			rx_text_buf[2] = 0;
			wait();
			USART1->TDR = 0xAA;
			USART1->TDR = 0x66;
			USART1->TDR = 0x55;
			i=0;
			HAL_Delay(5);
			while(LL_USART_IsActiveFlag_RXNE(USART1)){
				rx_text_buf[i] = LL_USART_ReceiveData8(USART1);
				i++;
				if(i>3)i=3;
			}
			if((0xAA != rx_text_buf[0])||(0x66 != rx_text_buf[1])||(0x55 != rx_text_buf[2])){
				res = 1;
				test_tick = 10;
			}
		}else
		
		//--------------------------uart2------------------------------
		if(2 == test_tick){
			rx_text_buf[0] = 0;
			rx_text_buf[1] = 0;
			rx_text_buf[2] = 0;
			wait();
			USART2->TDR = 0xAA;
			USART2->TDR = 0x66;
			USART2->TDR = 0x55;
			i=0;
			HAL_Delay(5);
			while(LL_USART_IsActiveFlag_RXNE(USART2)){
				rx_text_buf[i] = LL_USART_ReceiveData8(USART2);
				i++;
				if(i>3)i=3;
			}
			if((0xAA != rx_text_buf[0])||(0x66 != rx_text_buf[1])||(0x55 != rx_text_buf[2])){
				res = 2;
				test_tick = 10;
			}
		}else
		
		//--------------------------uart3------------------------------
		if(3 == test_tick){
			rx_text_buf[0] = 0;
			rx_text_buf[1] = 0;
			rx_text_buf[2] = 0;
			wait();
			USART3->TDR = 0xAA;
			USART3->TDR = 0x66;
			USART3->TDR = 0x55;
			i=0;
			HAL_Delay(5);
			while(LL_USART_IsActiveFlag_RXNE(USART3)){
				rx_text_buf[i] = LL_USART_ReceiveData8(USART3);
				i++;
				if(i>3)i=3;
			}
			if((0xAA != rx_text_buf[0])||(0x66 != rx_text_buf[1])||(0x55 != rx_text_buf[2])){
				res = 3;
				test_tick = 10;
			}
		}else
		
		//--------------------------uart4------------------------------
		if(4 == test_tick){
			rx_text_buf[0] = 0;
			rx_text_buf[1] = 0;
			rx_text_buf[2] = 0;
			wait();
			UART4->TDR = 0xAA;
			UART4->TDR = 0x66;
			UART4->TDR = 0x55;
			i=0;
			HAL_Delay(5);
			while(LL_USART_IsActiveFlag_RXNE(UART4)){
				rx_text_buf[i] = LL_USART_ReceiveData8(UART4);
				i++;
				if(i>3)i=3;
			}
			if((0xAA != rx_text_buf[0])||(0x66 != rx_text_buf[1])||(0x55 != rx_text_buf[2])){
				res = 4;
				test_tick = 10;
			}
		}else
		
		//--------------------------uart5------------------------------
		if(5 == test_tick){
			rx_text_buf[0] = 0;
			rx_text_buf[1] = 0;
			rx_text_buf[2] = 0;
			wait();
			UART5->TDR = 0xAA;
			UART5->TDR = 0x66;
			UART5->TDR = 0x55;
			i=0;
			HAL_Delay(5);
			while(LL_USART_IsActiveFlag_RXNE(UART5)){
				rx_text_buf[i] = LL_USART_ReceiveData8(UART5);
				i++;
				if(i>3)i=3;
			}
			if((0xAA != rx_text_buf[0])||(0x66 != rx_text_buf[1])||(0x55 != rx_text_buf[2])){
				res = 5;
				test_tick = 10;
			}
		}else
		
		//--------------------------uart6------------------------------
		if(6 == test_tick){
			rx_text_buf[0] = 0;
			rx_text_buf[1] = 0;
			rx_text_buf[2] = 0;
			wait();
			USART6->TDR = 0xAA;
			USART6->TDR = 0x66;
			USART6->TDR = 0x55;
			i=0;
			HAL_Delay(5);
			while(LL_USART_IsActiveFlag_RXNE(USART6)){
				rx_text_buf[i] = LL_USART_ReceiveData8(USART6);
				i++;
				if(i>3)i=3;
			}
			if((0xAA != rx_text_buf[0])||(0x66 != rx_text_buf[1])||(0x55 != rx_text_buf[2])){
				res = 6;
				test_tick = 10;
			}
		}else
		
		//--------------------------uart7------------------------------
		if(7 == test_tick){
			rx_text_buf[0] = 0;
			rx_text_buf[1] = 0;
			rx_text_buf[2] = 0;
			wait();
			UART7->TDR = 0xAA;
			UART7->TDR = 0x66;
			UART7->TDR = 0x55;
			i=0;
			HAL_Delay(5);
			while(LL_USART_IsActiveFlag_RXNE(UART7)){
				rx_text_buf[i] = LL_USART_ReceiveData8(UART7);
				i++;
				if(i>3)i=3;
			}
			if((0xAA != rx_text_buf[0])||(0x66 != rx_text_buf[1])||(0x55 != rx_text_buf[2])){
				res = 7;
				test_tick = 10;
			}
		}
	}
	//----------------------------------end--------------------------------------------------------
	
	//UART1
	LL_USART_EnableIT_RXNE(USART1);
	LL_USART_ClearFlag_IDLE(USART1);
	LL_USART_EnableIT_IDLE(USART1);
	uart_info_8bit[1].tx_complete = 1;
	
	//UART2
	LL_USART_EnableIT_RXNE(USART2);
	LL_USART_ClearFlag_IDLE(USART2);
	//LL_USART_EnableIT_IDLE(USART2);
	uart_info_8bit[2].tx_complete = 1;
	
	//UART3
	LL_USART_EnableIT_RXNE(USART3);
	LL_USART_ClearFlag_IDLE(USART3);
	LL_USART_EnableIT_IDLE(USART3);
	uart_info_8bit[3].tx_complete = 1;
	
	//UART4
	LL_USART_EnableIT_RXNE(UART4);
	LL_USART_ClearFlag_IDLE(UART4);
	//LL_USART_EnableIT_IDLE(UART4);
	uart_info_8bit[4].tx_complete = 1;
	
	//UART5
	LL_USART_EnableIT_RXNE(UART5);
	LL_USART_ClearFlag_IDLE(UART5);
	LL_USART_EnableIT_IDLE(UART5);
	uart_info_8bit[5].tx_complete = 1;
	
	//UART6
	LL_USART_EnableIT_RXNE(USART6);
	uart_info_8bit[6].tx_complete = 1;
	
	//UART7
	LL_USART_EnableIT_RXNE(UART7);
	LL_USART_ClearFlag_IDLE(UART7);
	LL_USART_EnableIT_IDLE(UART7);
	uart_info_8bit[7].tx_complete = 1;
	
	return res;
}

unsigned int hw_uart_test(void)//test uart1-5  7
{
	unsigned int res = 0;
	unsigned int i = 0;
	unsigned char data[4] = {0};
	unsigned int test_tick = 0;
	
	HAL_Delay(50);
	
	while(0 == uart_info_8bit[1].tx_complete);
	while(0 == uart_info_8bit[2].tx_complete);
	while(0 == uart_info_8bit[3].tx_complete);
	while(0 == uart_info_8bit[4].tx_complete);
	while(0 == uart_info_8bit[5].tx_complete);
	while(0 == uart_info_8bit[7].tx_complete);
	
	//UART1
	LL_USART_DisableIT_RXNE(USART1);
	LL_USART_ClearFlag_IDLE(USART1);
	LL_USART_DisableIT_IDLE(USART1);
	while(LL_USART_IsActiveFlag_RXNE(USART1)){
		data[0] = LL_USART_ReceiveData8(USART1);
	}
	
	//UART2
	LL_USART_DisableIT_RXNE(USART2);
	LL_USART_ClearFlag_IDLE(USART2);
	LL_USART_DisableIT_IDLE(USART2);
	while(LL_USART_IsActiveFlag_RXNE(USART2)){
		data[0] = LL_USART_ReceiveData8(USART2);
	}
	
	//UART3
	LL_USART_DisableIT_RXNE(USART3);
	LL_USART_ClearFlag_IDLE(USART3);
	LL_USART_DisableIT_IDLE(USART3);
	while(LL_USART_IsActiveFlag_RXNE(USART3)){
		data[0] = LL_USART_ReceiveData8(USART3);
	}
	
	//UART4
	LL_USART_DisableIT_RXNE(UART4);
	LL_USART_ClearFlag_IDLE(UART4);
	LL_USART_DisableIT_IDLE(UART4);
	while(LL_USART_IsActiveFlag_RXNE(UART4)){
		data[0] = LL_USART_ReceiveData8(UART4);
	}
	
	//UART5
	LL_USART_DisableIT_RXNE(UART5);
	LL_USART_ClearFlag_IDLE(UART5);
	LL_USART_DisableIT_IDLE(UART5);
	while(LL_USART_IsActiveFlag_RXNE(UART5)){
		data[0] = LL_USART_ReceiveData8(UART5);
	}
	
	//UART7
	LL_USART_DisableIT_RXNE(UART7);
	LL_USART_ClearFlag_IDLE(UART7);
	LL_USART_DisableIT_IDLE(UART7);
	while(LL_USART_IsActiveFlag_RXNE(UART7)){
		data[0] = LL_USART_ReceiveData8(UART7);
	}
	
	while(test_tick <= 7){
		test_tick++;
		//--------------------------uart1------------------------------
		if(1 == test_tick){
			rx_text_buf[0] = 0;
			rx_text_buf[1] = 0;
			rx_text_buf[2] = 0;
			wait();
			USART1->TDR = 0xAA;
			USART1->TDR = 0x66;
			USART1->TDR = 0x55;
			i=0;
			HAL_Delay(5);
			while(LL_USART_IsActiveFlag_RXNE(USART1)){
				rx_text_buf[i] = LL_USART_ReceiveData8(USART1);
				i++;
				if(i>3)i=3;
			}
			if((0xAA != rx_text_buf[0])||(0x66 != rx_text_buf[1])||(0x55 != rx_text_buf[2])){
				res = 1;
				test_tick = 10;
			}
		}else
		
		//--------------------------uart2------------------------------
		if(2 == test_tick){
			rx_text_buf[0] = 0;
			rx_text_buf[1] = 0;
			rx_text_buf[2] = 0;
			wait();
			USART2->TDR = 0xAA;
			USART2->TDR = 0x66;
			USART2->TDR = 0x55;
			i=0;
			HAL_Delay(5);
			while(LL_USART_IsActiveFlag_RXNE(USART2)){
				rx_text_buf[i] = LL_USART_ReceiveData8(USART2);
				i++;
				if(i>3)i=3;
			}
			if((0xAA != rx_text_buf[0])||(0x66 != rx_text_buf[1])||(0x55 != rx_text_buf[2])){
				res = 2;
				test_tick = 10;
			}
		}else
		
		//--------------------------uart3------------------------------
		if(3 == test_tick){
			rx_text_buf[0] = 0;
			rx_text_buf[1] = 0;
			rx_text_buf[2] = 0;
			wait();
			USART3->TDR = 0xAA;
			USART3->TDR = 0x66;
			USART3->TDR = 0x55;
			i=0;
			HAL_Delay(5);
			while(LL_USART_IsActiveFlag_RXNE(USART3)){
				rx_text_buf[i] = LL_USART_ReceiveData8(USART3);
				i++;
				if(i>3)i=3;
			}
			if((0xAA != rx_text_buf[0])||(0x66 != rx_text_buf[1])||(0x55 != rx_text_buf[2])){
				res = 3;
				test_tick = 10;
			}
		}else
		
		//--------------------------uart4------------------------------
		if(4 == test_tick){
			rx_text_buf[0] = 0;
			rx_text_buf[1] = 0;
			rx_text_buf[2] = 0;
			wait();
			UART4->TDR = 0xAA;
			UART4->TDR = 0x66;
			UART4->TDR = 0x55;
			i=0;
			HAL_Delay(5);
			while(LL_USART_IsActiveFlag_RXNE(UART4)){
				rx_text_buf[i] = LL_USART_ReceiveData8(UART4);
				i++;
				if(i>3)i=3;
			}
			if((0xAA != rx_text_buf[0])||(0x66 != rx_text_buf[1])||(0x55 != rx_text_buf[2])){
				res = 4;
				test_tick = 10;
			}
		}else
		
		//--------------------------uart5------------------------------
		if(5 == test_tick){
			rx_text_buf[0] = 0;
			rx_text_buf[1] = 0;
			rx_text_buf[2] = 0;
			wait();
			UART5->TDR = 0xAA;
			UART5->TDR = 0x66;
			UART5->TDR = 0x55;
			i=0;
			HAL_Delay(5);
			while(LL_USART_IsActiveFlag_RXNE(UART5)){
				rx_text_buf[i] = LL_USART_ReceiveData8(UART5);
				i++;
				if(i>3)i=3;
			}
			if((0xAA != rx_text_buf[0])||(0x66 != rx_text_buf[1])||(0x55 != rx_text_buf[2])){
				res = 5;
				test_tick = 10;
			}
		}else
		
		//--------------------------uart7------------------------------
		if(7 == test_tick){
			rx_text_buf[0] = 0;
			rx_text_buf[1] = 0;
			rx_text_buf[2] = 0;
			wait();
			UART7->TDR = 0xAA;
			UART7->TDR = 0x66;
			UART7->TDR = 0x55;
			i=0;
			HAL_Delay(5);
			while(LL_USART_IsActiveFlag_RXNE(UART7)){
				rx_text_buf[i] = LL_USART_ReceiveData8(UART7);
				i++;
				if(i>3)i=3;
			}
			if((0xAA != rx_text_buf[0])||(0x66 != rx_text_buf[1])||(0x55 != rx_text_buf[2])){
				res = 7;
				test_tick = 10;
			}
		}
	}
	//----------------------------------end--------------------------------------------------------
	
	//UART1
	LL_USART_EnableIT_RXNE(USART1);
	LL_USART_ClearFlag_IDLE(USART1);
	LL_USART_EnableIT_IDLE(USART1);
	uart_info_8bit[1].tx_complete = 1;
	
	//UART2
	LL_USART_EnableIT_RXNE(USART2);
	LL_USART_ClearFlag_IDLE(USART2);
	//LL_USART_EnableIT_IDLE(USART2);
	uart_info_8bit[2].tx_complete = 1;
	
	//UART3
	LL_USART_EnableIT_RXNE(USART3);
	LL_USART_ClearFlag_IDLE(USART3);
	LL_USART_EnableIT_IDLE(USART3);
	uart_info_8bit[3].tx_complete = 1;
	
	//UART4
	LL_USART_EnableIT_RXNE(UART4);
	LL_USART_ClearFlag_IDLE(UART4);
	//LL_USART_EnableIT_IDLE(UART4);
	uart_info_8bit[4].tx_complete = 1;
	
	//UART5
	LL_USART_EnableIT_RXNE(UART5);
	LL_USART_ClearFlag_IDLE(UART5);
	LL_USART_EnableIT_IDLE(UART5);
	uart_info_8bit[5].tx_complete = 1;
	
	//UART7
	LL_USART_EnableIT_RXNE(UART7);
	LL_USART_ClearFlag_IDLE(UART7);
	LL_USART_EnableIT_IDLE(UART7);
	uart_info_8bit[7].tx_complete = 1;
	
	return res;
}

unsigned int hw_eeprom_test(void)
{
	unsigned int i = 0;
	unsigned int addr = 0x1505;
	
	unsigned int res = 0;
	
	eeprom_write_byte_via_page(0, addr, &tx_text_buf[0], 16);
	HAL_Delay(5);
	
	eeprom_write_byte_via_page(1, addr, &tx_text_buf[1], 16);
	HAL_Delay(5);
	
	eeprom_read_byte(0, addr, rx_text_buf, 16);
	for(i=0;i<16;i+=2){
		if(0x55 != rx_text_buf[i])res = 1;
		if(0xAA != rx_text_buf[i+1])res = 1;
	}
	
	eeprom_read_byte(1, addr, rx_text_buf, 16);
	for(i=0;i<16;i+=2){
		if(0xAA != rx_text_buf[i])res = 1;
		if(0x55 != rx_text_buf[i+1])res = 1;
	}
	
	return res;
}
	
unsigned int hw_nor_flash_test(void)
{
	unsigned int i = 0;
	unsigned int nor_sate = 0;
	unsigned int addr = 0x1000;
	
	unsigned int res = 0;
	
	//------------------------pcs 0---------------------------------------
	nor_erase_sector_4k(0,addr);
	do{
		HAL_Delay(5);
		nor_sate = nor_get_busy_state(0);
	}while(nor_sate);
	
	nor_write_byte_via_page(0,addr,&tx_text_buf[0],16);
	do{
		HAL_Delay(5);
		nor_sate = nor_get_busy_state(0);
	}while(nor_sate);
	
	nor_read_byte(0,addr,rx_text_buf,16);
	for(i=0;i<16;i+=2){
		if(0x55 != rx_text_buf[i])res = 1;
		if(0xAA != rx_text_buf[i+1])res = 1;
	}
	
	//------------------------pcs 1---------------------------------------
	nor_erase_sector_4k(1,addr);
	do{
		HAL_Delay(5);
		nor_sate = nor_get_busy_state(1);
	}while(nor_sate);
	
	nor_write_byte_via_page(1,addr,&tx_text_buf[1],16);
	do{
		HAL_Delay(5);
		nor_sate = nor_get_busy_state(1);
	}while(nor_sate);
	
	nor_read_byte(1,addr,rx_text_buf,16);
	for(i=0;i<16;i+=2){
		if(0xAA != rx_text_buf[i])res = 1;
		if(0x55 != rx_text_buf[i+1])res = 1;
	}
	
	return res;
}
	
unsigned int hw_sdram_test(void)
{
	volatile unsigned int sdram_addr = 0xC0000000;
	volatile unsigned int i = 0;
	//volatile unsigned int value = 0;
	volatile unsigned int res = 0;
	unsigned int x = 0,y = 0;
	
	res = 0;
	/*
	for(i=0;i<0x04000000;i+=4){
		sdram_addr = 0xC0000000 + i;
		
		*(volatile unsigned int*)sdram_addr = 0x55555555;
		wait();wait();
		//value = *(volatile unsigned int*)sdram_addr;
		if((*(volatile unsigned int*)sdram_addr) != 0x55555555){
			res = 1;
			break;
		}
		
		*(volatile unsigned int*)sdram_addr = 0xAAAAAAAA;
		wait();wait();
		//value = *(volatile unsigned int*)sdram_addr;
		if((*(volatile unsigned int*)sdram_addr) != 0xAAAAAAAA){
			res = 1;
			break;
		}
	}
	*/
	HAL_Delay(1);
	for(i=0;i<0x04000000;i+=4){
		sdram_addr = 0xC0000000 + i;
		
		*(volatile unsigned int*)sdram_addr = 0x55555555;
		wait();
	}
	LL_IWDG_ReloadCounter(IWDG1);
	HAL_Delay(3);
	for(i=0;i<0x04000000;i+=4){
		sdram_addr = 0xC0000000 + i;
		if((*(volatile unsigned int*)sdram_addr) != 0x55555555){
			res = 1;
			break;
		}
	}
	
	
	if(0 == res){
		HAL_Delay(3);
		for(i=0;i<0x04000000;i+=4){
			sdram_addr = 0xC0000000 + i;
			
			*(volatile unsigned int*)sdram_addr = 0xAAAAAAAA;
			wait();
		}
		LL_IWDG_ReloadCounter(IWDG1);
		HAL_Delay(3);
		for(i=0;i<0x04000000;i+=4){
			sdram_addr = 0xC0000000 + i;
			if((*(volatile unsigned int*)sdram_addr) != 0xAAAAAAAA){
				res = 1;
				break;
			}
		}
	}
	
	sdram_addr = 0xC0000000 + 480*800*4;
	for(y=0;y<480;y++){
		for(x=0;x<800;x++){
			*(volatile unsigned int*)sdram_addr = 0;
			sdram_addr+=4;
		}
	}
	
	return res;
}

void hw_lcd_test(void)
{
	unsigned int addr = 0xC0000000;
	unsigned int res = 0;
	unsigned int value = 0;
	unsigned int i = 0,x = 0,y = 0;
	
	//----------------------------R-------------------------------
	for(y=0;y<480;y++){
		addr = 0xC0000000 + y*800*4;
		for(x=0;x<100;x++){
			*(unsigned int*)addr = 0xFFFF0000;
			addr+=4;
		}
	}
	for(y=0;y<480;y++){
		addr = 0xC0000000 + 100*4 + y*800*4;
		for(x=0;x<100;x++){
			*(unsigned int*)addr = 0xFFE00000;
			addr+=4;
		}
	}
	for(y=0;y<480;y++){
		addr = 0xC0000000 + 200*4 + y*800*4;
		for(x=0;x<100;x++){
			*(unsigned int*)addr = 0xFFC00000;
			addr+=4;
		}
	}
	for(y=0;y<480;y++){
		addr = 0xC0000000 + 300*4 + y*800*4;
		for(x=0;x<100;x++){
			*(unsigned int*)addr = 0xFFA00000;
			addr+=4;
		}
	}
	for(y=0;y<480;y++){
		addr = 0xC0000000 + 400*4 + y*800*4;
		for(x=0;x<100;x++){
			*(unsigned int*)addr = 0xFF800000;
			addr+=4;
		}
	}
	for(y=0;y<480;y++){
		addr = 0xC0000000 + 500*4 + y*800*4;
		for(x=0;x<100;x++){
			*(unsigned int*)addr = 0xFF600000;
			addr+=4;
		}
	}
	for(y=0;y<480;y++){
		addr = 0xC0000000 + 600*4 + y*800*4;
		for(x=0;x<100;x++){
			*(unsigned int*)addr = 0xFF400000;
			addr+=4;
		}
	}
	for(y=0;y<480;y++){
		addr = 0xC0000000 + 700*4 + y*800*4;
		for(x=0;x<100;x++){
			*(unsigned int*)addr = 0xFF200000;
			addr+=4;
		}
	}
	HAL_Delay(1500);
	
	//----------------------------G-------------------------------
	for(y=0;y<480;y++){
		addr = 0xC0000000 + y*800*4;
		for(x=0;x<100;x++){
			*(unsigned int*)addr = 0xFF00FF00;
			addr+=4;
		}
	}
	for(y=0;y<480;y++){
		addr = 0xC0000000 + 100*4 + y*800*4;
		for(x=0;x<100;x++){
			*(unsigned int*)addr = 0xFF00E000;
			addr+=4;
		}
	}
	for(y=0;y<480;y++){
		addr = 0xC0000000 + 200*4 + y*800*4;
		for(x=0;x<100;x++){
			*(unsigned int*)addr = 0xFF00C000;
			addr+=4;
		}
	}
	for(y=0;y<480;y++){
		addr = 0xC0000000 + 300*4 + y*800*4;
		for(x=0;x<100;x++){
			*(unsigned int*)addr = 0xFF00A000;
			addr+=4;
		}
	}
	for(y=0;y<480;y++){
		addr = 0xC0000000 + 400*4 + y*800*4;
		for(x=0;x<100;x++){
			*(unsigned int*)addr = 0xFF008000;
			addr+=4;
		}
	}
	for(y=0;y<480;y++){
		addr = 0xC0000000 + 500*4 + y*800*4;
		for(x=0;x<100;x++){
			*(unsigned int*)addr = 0xFF006000;
			addr+=4;
		}
	}
	for(y=0;y<480;y++){
		addr = 0xC0000000 + 600*4 + y*800*4;
		for(x=0;x<100;x++){
			*(unsigned int*)addr = 0xFF004000;
			addr+=4;
		}
	}
	for(y=0;y<480;y++){
		addr = 0xC0000000 + 700*4 + y*800*4;
		for(x=0;x<100;x++){
			*(unsigned int*)addr = 0xFF002000;
			addr+=4;
		}
	}
	HAL_Delay(1500);
	
	//----------------------------B-------------------------------
	for(y=0;y<480;y++){
		addr = 0xC0000000 + y*800*4;
		for(x=0;x<100;x++){
			*(unsigned int*)addr = 0xFF0000FF;
			addr+=4;
		}
	}
	for(y=0;y<480;y++){
		addr = 0xC0000000 + 100*4 + y*800*4;
		for(x=0;x<100;x++){
			*(unsigned int*)addr = 0xFF0000E0;
			addr+=4;
		}
	}
	for(y=0;y<480;y++){
		addr = 0xC0000000 + 200*4 + y*800*4;
		for(x=0;x<100;x++){
			*(unsigned int*)addr = 0xFF0000C0;
			addr+=4;
		}
	}
	for(y=0;y<480;y++){
		addr = 0xC0000000 + 300*4 + y*800*4;
		for(x=0;x<100;x++){
			*(unsigned int*)addr = 0xFF0000A0;
			addr+=4;
		}
	}
	for(y=0;y<480;y++){
		addr = 0xC0000000 + 400*4 + y*800*4;
		for(x=0;x<100;x++){
			*(unsigned int*)addr = 0xFF000080;
			addr+=4;
		}
	}
	for(y=0;y<480;y++){
		addr = 0xC0000000 + 500*4 + y*800*4;
		for(x=0;x<100;x++){
			*(unsigned int*)addr = 0xFF000060;
			addr+=4;
		}
	}
	for(y=0;y<480;y++){
		addr = 0xC0000000 + 600*4 + y*800*4;
		for(x=0;x<100;x++){
			*(unsigned int*)addr = 0xFF000040;
			addr+=4;
		}
	}
	for(y=0;y<480;y++){
		addr = 0xC0000000 + 700*4 + y*800*4;
		for(x=0;x<100;x++){
			*(unsigned int*)addr = 0xFF000020;
			addr+=4;
		}
	}
	HAL_Delay(1500);
	
	//----------------------------R-------------------------------
	addr = 0xC0000000;
	for(y=0;y<60;y++){
		for(x=0;x<800;x++){
			*(unsigned int*)addr = 0xFF200000;
			addr+=4;
		}
	}
	addr = 0xC0000000 + (60*800*4);
	for(y=0;y<60;y++){
		for(x=0;x<800;x++){
			*(unsigned int*)addr = 0xFF400000;
			addr+=4;
		}
	}
	addr = 0xC0000000 + (120*800*4);
	for(y=0;y<60;y++){
		for(x=0;x<800;x++){
			*(unsigned int*)addr = 0xFF600000;
			addr+=4;
		}
	}
	addr = 0xC0000000 + (180*800*4);
	for(y=0;y<60;y++){
		for(x=0;x<800;x++){
			*(unsigned int*)addr = 0xFF800000;
			addr+=4;
		}
	}
	addr = 0xC0000000 + (240*800*4);
	for(y=0;y<60;y++){
		for(x=0;x<800;x++){
			*(unsigned int*)addr = 0xFFA00000;
			addr+=4;
		}
	}
	addr = 0xC0000000 + (300*800*4);
	for(y=0;y<60;y++){
		for(x=0;x<800;x++){
			*(unsigned int*)addr = 0xFFC00000;
			addr+=4;
		}
	}
	addr = 0xC0000000 + (360*800*4);
	for(y=0;y<60;y++){
		for(x=0;x<800;x++){
			*(unsigned int*)addr = 0xFFE00000;
			addr+=4;
		}
	}
	addr = 0xC0000000 + (420*800*4);
	for(y=0;y<60;y++){
		for(x=0;x<800;x++){
			*(unsigned int*)addr = 0xFFFF0000;
			addr+=4;
		}
	}
	HAL_Delay(1500);
	
	//----------------------------G-------------------------------
	addr = 0xC0000000;
	for(y=0;y<60;y++){
		for(x=0;x<800;x++){
			*(unsigned int*)addr = 0xFF002000;
			addr+=4;
		}
	}
	addr = 0xC0000000 + (60*800*4);
	for(y=0;y<60;y++){
		for(x=0;x<800;x++){
			*(unsigned int*)addr = 0xFF004000;
			addr+=4;
		}
	}
	addr = 0xC0000000 + (120*800*4);
	for(y=0;y<60;y++){
		for(x=0;x<800;x++){
			*(unsigned int*)addr = 0xFF006000;
			addr+=4;
		}
	}
	addr = 0xC0000000 + (180*800*4);
	for(y=0;y<60;y++){
		for(x=0;x<800;x++){
			*(unsigned int*)addr = 0xFF008000;
			addr+=4;
		}
	}
	addr = 0xC0000000 + (240*800*4);
	for(y=0;y<60;y++){
		for(x=0;x<800;x++){
			*(unsigned int*)addr = 0xFF00A000;
			addr+=4;
		}
	}
	addr = 0xC0000000 + (300*800*4);
	for(y=0;y<60;y++){
		for(x=0;x<800;x++){
			*(unsigned int*)addr = 0xFF00C000;
			addr+=4;
		}
	}
	addr = 0xC0000000 + (360*800*4);
	for(y=0;y<60;y++){
		for(x=0;x<800;x++){
			*(unsigned int*)addr = 0xFF00E000;
			addr+=4;
		}
	}
	addr = 0xC0000000 + (420*800*4);
	for(y=0;y<60;y++){
		for(x=0;x<800;x++){
			*(unsigned int*)addr = 0xFF00FF00;
			addr+=4;
		}
	}
	HAL_Delay(1500);
	
	//----------------------------B-------------------------------
	addr = 0xC0000000;
	for(y=0;y<60;y++){
		for(x=0;x<800;x++){
			*(unsigned int*)addr = 0xFF000020;
			addr+=4;
		}
	}
	addr = 0xC0000000 + (60*800*4);
	for(y=0;y<60;y++){
		for(x=0;x<800;x++){
			*(unsigned int*)addr = 0xFF000040;
			addr+=4;
		}
	}
	addr = 0xC0000000 + (120*800*4);
	for(y=0;y<60;y++){
		for(x=0;x<800;x++){
			*(unsigned int*)addr = 0xFF000060;
			addr+=4;
		}
	}
	addr = 0xC0000000 + (180*800*4);
	for(y=0;y<60;y++){
		for(x=0;x<800;x++){
			*(unsigned int*)addr = 0xFF000080;
			addr+=4;
		}
	}
	addr = 0xC0000000 + (240*800*4);
	for(y=0;y<60;y++){
		for(x=0;x<800;x++){
			*(unsigned int*)addr = 0xFF0000A0;
			addr+=4;
		}
	}
	addr = 0xC0000000 + (300*800*4);
	for(y=0;y<60;y++){
		for(x=0;x<800;x++){
			*(unsigned int*)addr = 0xFF0000C0;
			addr+=4;
		}
	}
	addr = 0xC0000000 + (360*800*4);
	for(y=0;y<60;y++){
		for(x=0;x<800;x++){
			*(unsigned int*)addr = 0xFF0000E0;
			addr+=4;
		}
	}
	addr = 0xC0000000 + (420*800*4);
	for(y=0;y<60;y++){
		for(x=0;x<800;x++){
			*(unsigned int*)addr = 0xFF0000FF;
			addr+=4;
		}
	}
	HAL_Delay(1500);
}

void hw_print_test(void)
{
	HAL_Delay(500);
	print_text("\r\n\r\n\r\n\r\n");
	HAL_Delay(500);
	print_text("\r\n--------------------------------\r\n");
	HAL_Delay(500);
	print_text("\r\n----------´òÓ¡²âÊÔÒ³------------\r\n");
	HAL_Delay(500);
	print_text("\r\n--------------------------------\r\n");
	HAL_Delay(500);
}

void hw_relay_test(void)
{
	LL_TIM_OC_SetCompareCH1(TIM2,255);//RELAY1_ON()
	LL_TIM_OC_SetCompareCH1(TIM12,255);//RELAY2_ON()
	HAL_Delay(3000);
	LL_TIM_OC_SetCompareCH1(TIM2,0);//RELAY1_OFF()
	LL_TIM_OC_SetCompareCH1(TIM12,0);//RELAY2_OFF()
	HAL_Delay(1000);
}
