/*******************************************************************************
* 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    : r_sci_iic_apl.c
* Version      : 1.00
* Device(s)    : Renesas
* Tool-Chain   : Renesas
* OS           : None
* H/W Platform : Independent
* Description  : This is simpleIIC application 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_sci_iic_rx_if.h"

#define SCI_IIC_INITIAL
#include    "r_sci_iic_apl.h"

/******************************************************************************
Macro definitions
******************************************************************************/
#define END_FLAG_ON    1
#define END_FLAG_OFF   0

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

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

/******************************************************************************
Private global variables and functions
******************************************************************************/
static  uint16_t  g_tx_endflag = END_FLAG_OFF;           /* send end flag */
static  uint16_t  g_rx_endflag = END_FLAG_OFF;           /* recieve end flag */

static void sci_iic_callback_transmitend(void);
static void sci_iic_callback_receiveend(void);


/***********************************************************************************************************************
* Function Name: R_simple_iic_init
* Description  : initialize
* Arguments    : none
* Return Value : none
***********************************************************************************************************************/
void R_simple_iic_init( void )
{
    sci_iic_info_t     iic_info;

    iic_info.dev_sts = 0;                 /* Device status flag */
    iic_info.ch_no   = SCI_IIC_CH_NUM;    /* Channel No.        */

    R_SCI_IIC_Open( &iic_info);
}

/***********************************************************************************************************************
* Function Name: R_simple_iic_write
* Description  : transmission start processing
* Arguments    : IIC Information
* Return Value : SIMPLE_IIC_SUCCESS                    ; Successful operation
*              ; SIMPLE_IIC_ERR_OTHER                  ;  error
***********************************************************************************************************************/
simple_iic_return_t R_simple_iic_write( simple_iic_info_t* info )
{
    simple_iic_return_t ret = SIMPLE_IIC_SUCCESS;
    sci_iic_info_t     iic_info;

    g_tx_endflag = END_FLAG_OFF;

    iic_info.dev_sts      = 0;                                /* Device status flag */
    iic_info.ch_no        = SCI_IIC_CH_NUM;                   /* Channel No.        */
    iic_info.callbackfunc = &sci_iic_callback_transmitend;    /* Callback function  */
    iic_info.cnt1st       = 1;                                /* 1st Data Counter   */

    iic_info.p_slv_adr    = &(info->p_slv_adr);               /* Slave address           */
    iic_info.p_data1st    = &(info->p_sub_adr);               /* Sub address             */
    iic_info.p_data2nd    = info->p_data;                     /* Pointer for Data buffer */
    iic_info.cnt2nd       = (uint32_t)info->cnt;              /* Data Counter            */

    if ( R_SCI_IIC_MasterSend(&iic_info) != SIMPLE_IIC_SUCCESS )
    {
        /* do something */
    }

    while ( END_FLAG_OFF == g_tx_endflag );

    return ret;
}

/***********************************************************************************************************************
* Function Name: R_simple_iic_read
* Description  : reception start processing
* Arguments    : IIC Information
* Return Value : SIMPLE_IIC_SUCCESS                    ; Successful operation
*              ; SIMPLE_IIC_ERR_OTHER                  ; error
***********************************************************************************************************************/
simple_iic_return_t R_simple_iic_read( simple_iic_info_t* info )
{
    // don't select combination format(ov7670 read NG)
    simple_iic_return_t ret = SIMPLE_IIC_SUCCESS;
    sci_iic_info_t     iic_info;
    simple_iic_info_t  snd_info;
    uint8_t dummy;

    snd_info.p_slv_adr    = info->p_slv_adr;                   /* Slave address           */
    snd_info.p_sub_adr    = info->p_sub_adr;                   /* Sub address             */
    snd_info.p_data       = &dummy;                            /* Pointer for Data buffer */
    snd_info.cnt          = 0;                                 /* Data Counter            */
    ret = R_simple_iic_write(&snd_info);
    if ( SIMPLE_IIC_SUCCESS != ret )
    {
        /* do something */
    }

    g_rx_endflag = END_FLAG_OFF;

    iic_info.dev_sts      = 0;                               /* Device status flag */
    iic_info.ch_no        = SCI_IIC_CH_NUM;                  /* Channel No.        */
    iic_info.callbackfunc = &sci_iic_callback_receiveend;    /* Callback function  */
    iic_info.cnt1st       = 0;                               /* 1st Data Counter   */  // set receive mode

    iic_info.p_slv_adr    = &(info->p_slv_adr);              /* Slave address           */
    iic_info.p_data1st    = &(info->p_sub_adr);              /* Sub address             */
    iic_info.p_data2nd    = info->p_data;                    /* Pointer for Data buffer */
    iic_info.cnt2nd       = (uint32_t)info->cnt;             /* Data Counter            */

    if ( R_SCI_IIC_MasterReceive(&iic_info) != SIMPLE_IIC_SUCCESS )
    {
        /* do something */
    }

    while ( END_FLAG_OFF == g_rx_endflag );


    return ret;
}

/***********************************************************************************************************************
* Function Name: sci_iic_callback_transmitend
* Description  : This function is a callback function when SCI-IIC finishes transmission.
* Arguments    : None
* Return Value : None
***********************************************************************************************************************/
static void sci_iic_callback_transmitend(void)
{
    sci_iic_return_t ret;
    sci_iic_mcu_status_t iic_status;
    sci_iic_info_t iic_info_ch;

    iic_info_ch.ch_no = SCI_IIC_CH_NUM;
    ret = R_SCI_IIC_GetStatus(&iic_info_ch, &iic_status);
    if (SCI_IIC_SUCCESS != ret)
    {
        /* Call error processing for the R_SCI_IIC_GetStatus()function*/
    }
    else
    {
        if (1 == iic_status.BIT.NACK)
        {
            /* Processing when a NACK is detected
               by verifying the iic_status flag. */
        }
    }

    g_tx_endflag = END_FLAG_ON;
}

/***********************************************************************************************************************
* Function Name: sci_iic_callback_receiveend
* Description  : This function is a callback function when SCI-IIC finishes reception.
* Arguments    : None
* Return Value : None
***********************************************************************************************************************/
static void sci_iic_callback_receiveend(void)
{
    sci_iic_return_t ret;
    sci_iic_mcu_status_t iic_status;
    sci_iic_info_t iic_info_ch;

    iic_info_ch.ch_no = SCI_IIC_CH_NUM;
    ret = R_SCI_IIC_GetStatus(&iic_info_ch, &iic_status);
    if (SCI_IIC_SUCCESS != ret)
    {
        /* Call error processing for the R_SCI_IIC_GetStatus()function*/
    }

    g_rx_endflag = END_FLAG_ON;
}

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