//  Copyright (C) 2016, mituhiromatuura all right reserved.

#include <kernel.h>
#include "iodefine.h"

#include "camera.h"
#include "ov7670.h"
#include "i2c.h"

#define WAIT 9

static
void write_array(unsigned char array[][2])
{
	int n;
	for(n = 0; 0xff != array[n][0]; n++)
	{
		i2c_send_start(I2C_SCCB_CH, 0x42);
		i2c_send_restart(I2C_SCCB_CH, array[n][0]);
		i2c_send_stop(I2C_SCCB_CH, array[n][1]);

		dly_tsk(WAIT);
	}
	return;
}

void init_camera(void)
{
	init_i2c(I2C_SCCB_CH, I2C_SCCB_PORT);

	write_array(ov7670_default_regs);
	write_array(ov7670_fmt_rgb565);

	i2c_send_start(I2C_SCCB_CH, 0x42);
	i2c_send_restart(I2C_SCCB_CH, 0x15);
	i2c_send_stop(I2C_SCCB_CH, 0x20);

	dly_tsk(WAIT);

	i2c_send_start(I2C_SCCB_CH, 0x42);
	i2c_send_restart(I2C_SCCB_CH, 0x12);
	i2c_send_stop(I2C_SCCB_CH, 0x14);

	dly_tsk(WAIT);

	i2c_send_start(I2C_SCCB_CH, 0x42);
	i2c_send_restart(I2C_SCCB_CH, 0x40);
	i2c_send_stop(I2C_SCCB_CH, 0xd0);

	return;
}
