#include <stdint.h>
#include "OV7670/ov7670.h"
#include "system.h"

#include "OV7670/sccb.h"
#include "unistd.h"
#include "sys/alt_stdio.h"
#include "altera_avalon_uart_regs.h"

int main(void)
{
	uint8_t *data;
	uint32_t i;
	uint8_t error = 1;

	OV7670_Init();
	alt_putstr("OV7670 Init OK!\n");
	alt_putstr("You can send a register address to modify the value in it through the serial port\n");
	alt_putstr("For Example: Send 0x42 0x1e 0x01 in hex format, You can mirror of the image in vertical direction\n");
	alt_putstr("note:0x42 is the id of ov7670, 0x1e is one of the register address, 0x01 is the new value\n");

 	while(1)
	{
 		if(IORD_ALTERA_AVALON_UART_STATUS(UART_0_BASE) & ALTERA_AVALON_UART_STATUS_RRDY_MSK)	//յ
 		{
 			data[0] = IORD_ALTERA_AVALON_UART_RXDATA(UART_0_BASE);	//յ
 			if(data[0] == OV7670_ADDR)	//Ҫ7670в
 			{
 				i = 100000;
 				while(!(IORD_ALTERA_AVALON_UART_STATUS(UART_0_BASE) & ALTERA_AVALON_UART_STATUS_RRDY_MSK) | (!i))//ȴյݻ߳ʱ
 				{
 					i--;
 				}
 				if(IORD_ALTERA_AVALON_UART_STATUS(UART_0_BASE) & ALTERA_AVALON_UART_STATUS_RRDY_MSK)//յ
 				{
 					data[1] = IORD_ALTERA_AVALON_UART_RXDATA(UART_0_BASE);	//յ
 					i = 100000;
					while(!(IORD_ALTERA_AVALON_UART_STATUS(UART_0_BASE) & ALTERA_AVALON_UART_STATUS_RRDY_MSK) | (!i))//ȴյݻ߳ʱ
					{
						i--;
					}
					if(IORD_ALTERA_AVALON_UART_STATUS(UART_0_BASE) & ALTERA_AVALON_UART_STATUS_RRDY_MSK)//յ
					{
						data[2] = IORD_ALTERA_AVALON_UART_RXDATA(UART_0_BASE);	//յ
						error = 0;
					}
					else
					error = 1;
 				}
 				else
 					error = 1;
 			}
 			else
 				error = 1;
 		}

 		if(!error)
 		{
 			SCCB_WR_Reg(data[1],data[2]);
 			error = 1;
 		}
	}

	return 0;
}

