/*******************************************************************************
* DISCLAIMER
* This software is supplied by Renesas Electronics Corporation and is only
* intended for use with Renesas products. No other uses are authorized. This
* software is owned by Renesas Electronics Corporation and is protected under
* all applicable laws, including copyright laws.
* THIS SOFTWARE IS PROVIDED "AS IS" AND RENESAS MAKES NO WARRANTIES REGARDING
* THIS SOFTWARE, WHETHER EXPRESS, IMPLIED OR STATUTORY, INCLUDING BUT NOT
* LIMITED TO WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE
* AND NON-INFRINGEMENT. ALL SUCH WARRANTIES ARE EXPRESSLY DISCLAIMED.
* TO THE MAXIMUM EXTENT PERMITTED NOT PROHIBITED BY LAW, NEITHER RENESAS
* ELECTRONICS CORPORATION NOR ANY OF ITS AFFILIATED COMPANIES SHALL BE LIABLE
* FOR ANY DIRECT, INDIRECT, SPECIAL, INCIDENTAL OR CONSEQUENTIAL DAMAGES FOR
* ANY REASON RELATED TO THIS SOFTWARE, EVEN IF RENESAS OR ITS AFFILIATES HAVE
* BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGES.
* Renesas reserves the right, without notice, to make changes to this software
* and to discontinue the availability of this software. By using this software,
* you agree to the additional terms and conditions found by accessing the
* following link:
* http://www.renesas.com/disclaimer *
* Copyright (C) 2014 Renesas Electronics Corporation
* and Renesas Solutions Corp. All rights reserved.
*******************************************************************************/
/*******************************************************************************
* File Name    : ov7740.c
* Version      : 1.00
* Device(s)    : Renesas
* Tool-Chain   : Renesas
* OS           : None
* H/W Platform : Independent
* Description  : This is ov7740 device control program code.
******************************************************************************/
/*****************************************************************************
* History : DD.MM.YYYY Version Description
*         : 01.09.2014 1.00    First Release
******************************************************************************/

/******************************************************************************
Includes <System Includes> , "Project Includes"
******************************************************************************/
#include "platform.h"
#include "r_cmt_rx_if.h"

#define  OV7740CONFIG_INITIAL
#include "ov7740config.h"

#define  OV7740_INITIAL
#include "ov7740.h"

#include "r_sci_iic_apl.h"

/******************************************************************************
Macro definitions
******************************************************************************/

/******************************************************************************
Typedef definitions
******************************************************************************/

/******************************************************************************
Exported global variables and functions (to be accessed by other files)
******************************************************************************/

/******************************************************************************
Private global variables and functions
******************************************************************************/
static  uint8_t  wait_flag = 0;           /* wait flag */

static uint8_t wr_ov7740reg( uint8_t reg_adr, uint8_t reg_data,  uint32_t size );
static uint8_t rd_ov7740reg( uint8_t reg_adr, uint8_t *reg_data, uint32_t size );

static void onshot_timer_int(void *pdata);
static void delay_ms( uint32_t time );

/***********************************************************************************************************************
* Function Name: wr_ov7740reg
* Description  : register read function
* Arguments    : uint8_t reg_adr     : sub address
                 uint8_t reg_data    : register setting data
                 uint32_t size       : data length
* Return Value : fixed : 1
***********************************************************************************************************************/
static uint8_t wr_ov7740reg( uint8_t reg_adr, uint8_t reg_data,  uint32_t size )
{
    simple_iic_return_t ret;
    simple_iic_info_t iic_info;
    uint8_t data = reg_data;

    iic_info.p_slv_adr = (IMG_SENSOR_SLV_ADDRESS >> 1);  /* Slave address           */
    iic_info.p_sub_adr = reg_adr;                        /* Sub address             */
    iic_info.p_data    = &data;                          /* Pointer for Data buffer */
    iic_info.cnt       = size;                           /* Data Counter            */

    ret = R_simple_iic_write(&iic_info);
    if ( SIMPLE_IIC_SUCCESS != ret )
    {
        /* do something */
    }

    return 1;
}

/***********************************************************************************************************************
* Function Name: rd_ov7740reg
* Description  : register read function
* Arguments    : uint8_t reg_adr     : sub address
                 uint8_t *reg_data   : pointer for data buffer
                 uint32_t size       : data length
* Return Value : fixed : 1
***********************************************************************************************************************/
static uint8_t rd_ov7740reg( uint8_t reg_adr, uint8_t *reg_data, uint32_t size )
{
    simple_iic_return_t ret;
    simple_iic_info_t iic_info;
    uint8_t data;

    iic_info.p_slv_adr = (IMG_SENSOR_SLV_ADDRESS >> 1);  /* Slave address           */
    iic_info.p_sub_adr = reg_adr;                        /* Sub address             */
    iic_info.p_data    = &data;                          /* Pointer for Data buffer */
    iic_info.cnt       = size;                           /* Data Counter            */

    ret = R_simple_iic_read(&iic_info);
    if ( SIMPLE_IIC_SUCCESS != ret )
    {
        /* do something */
    }

    *reg_data = data;

    return 1;
}

/***********************************************************************************************************************
* Function Name: R_ov7740_init
* Description  : Initialize OV7740 register
* Arguments    : None
* Return Value : fixed : 1
***********************************************************************************************************************/
uint8_t R_ov7740_init( void )
{
    uint8_t temp;
    uint32_t i;
    uint32_t err = 0;

    for ( i = 0; i < 0xFF; i++ )
    {
        if ( change_reg[i][0] == 0xFF )
        {
            break;
        }
        err = 0;

LBL_resetting:

        if ( 0 == wr_ov7740reg( change_reg[i][0], change_reg[i][1], 1 ) )
        {
            return 0;
        }
        delay_ms( 20 );
        if ( change_reg[i][0] == 0x12 )
        {
            continue;
        }
        temp = 0;
        rd_ov7740reg( change_reg[i][0], &temp, 1 );
        delay_ms( 20 );
        if ( temp != change_reg[i][1] )
        {
            err++;
            goto LBL_resetting;
        }
    }

    return 1;              // ok
}

/***********************************************************************************************************************
* Function Name:
* Description  : Timer control
* Arguments    : ---xs
* Return Value : ---
***********************************************************************************************************************/
static void onshot_timer_int(void *pdata)
{
    wait_flag = 0;
}

static void delay_ms( uint32_t time )
{
    uint32_t channel = 0xff;

    if (R_CMT_CreateOneShot( (time*1000), onshot_timer_int, &channel) != true)
    {
        while (1)
        {
            /* infinite loop(setting error) */
        }
    }
    wait_flag = 1;

    while ( 1 == wait_flag );
}

/******************************************************************************
End of file
******************************************************************************/
