#include <stdint.h>
#include "OV7670/ov7725_7670.h"
#include "system.h"
#include "OV7670/sccb.h"
#include "unistd.h"
#include "sys/alt_stdio.h"
#include "altera_avalon_uart_regs.h"
#include "altera_avalon_pio_regs.h"

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

	CMOS_MODEL = OV7xxx_Init();
	if(CMOS_MODEL == OV7725)
	{
		alt_putstr("CMOS Init OK,The Model is OV7725!\n");
	}else if(CMOS_MODEL == OV7670){
		alt_putstr("CMOS Init OK,The Model is OV7670!\n");
	}
	else
	{
		alt_putstr("Can't recognize the Model!\n");
		return 0;
	}
	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.(For OV7670)\n");
	alt_putstr("note:0x42 is the id of CMOS, 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] == SCCB_ID)	//Ҫ7725в
 			{
 				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;
 		}
 		else if(error == 1){
 			alt_putstr("CMD ERROR!!!\n");
 			error = 0;
 		}

	}

	return 0;
}

