/*******************************************************************************
* 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    : main.c
* Version      : 1.00
* Device(s)    : Renesas R5F564MxxDxx
* Tool-Chain   : Renesas e2 studio v3.0.1.09 or later
*              : C/C++ Compiler Package for RX Family V2.02.00 or later
* OS           : None
* H/W Platform : Renesas Starter Kit+ RX64M
* Description  : This is the main tutorial code.
******************************************************************************/
/*****************************************************************************
* History : DD.MM.YYYY Version Description
*         : 04.08.2014 1.00    First Release
******************************************************************************/

/******************************************************************************
Includes   <System Includes> , "Project Includes"
******************************************************************************/
#include "platform.h"
#include "r_t4_itcpip.h"
#include "r_ether_rx_if.h"
#include "r_t4_http_server_rx_if.h"
#include "r_dmaca_rx_if.h"
#include "r_pdc_apl.h"
#include "r_dmac_apl.h"
#include "ov7740.h"
#include "r_jpeg_enc_apl.h"
#include "r_sci_iic_apl.h"

/******************************************************************************
Section    <Section Definition> , "Project Sections"
******************************************************************************/

/******************************************************************************
External variables and functions
******************************************************************************/

/******************************************************************************
Private global variables and functions
******************************************************************************/
#pragma section _TCPIP_USER_BUFFER
UW tcpudp_work[ 10228 / 4]; // calculated by W tcpudp_get_ramsize( void )
#pragma section

static uint8_t pdc_capt_retry_flag = 0;

/******************************************************************************
Function Name   : main
Description     : sample main
Arguments       : none
Return value    : none
******************************************************************************/
void main(void)
{
    ER   ercd;
    W   ramsize;
    dmaca_return_t     ret_dmac;
#ifndef JPEG_AREA_MULTI
    dmaca_stat_t       stat_dmac;
#endif // JPEG_AREA_MULTI

    // IIC initialize
    R_simple_iic_init();

    // dma initialize
    R_dmac_init();

    /* initialize step1of pdc */
    R_pdc_init();

    // jpeg compressor initialize
    R_jpeg_compress_init();

    /* Initialize the Ethernet driver */
    R_ETHER_Initial();

    /* start LAN controller */
    ercd = lan_open();
    if (ercd != E_OK)
    {
        for ( ;; );
    }

    /* Get the size of the work area used by the T4 (RAM size). */
    ramsize = tcpudp_get_ramsize();
    if (ramsize > (sizeof(tcpudp_work)))
    {
        /* Then reserve as much memory array for the work area as the size
                 indicated by the returned value. */
        for ( ;; );
    }

    /* Initialize the TCP/IP */
    ercd = tcpudp_open(tcpudp_work);
    if (ercd != E_OK)
    {
        for ( ;; );
    }

    /* OV7740 Initialization */
    while ( 1 != R_ov7740_init() )
    {
        /* do nothing */
    }

    ret_dmac = R_DMACA_Open(DMACA_CH0);
    if (DMACA_SUCCESS != ret_dmac)
    {
        return;
    }
    R_dmac_ch0_init( 0 );              // DMAC0 initialize

    /* initialize step2 of pdc */  // need to pclk from ov7740
    R_pdc_create();

#ifndef JPEG_AREA_MULTI
    ret_dmac = R_DMACA_Open(DMACA_CH1);
    R_dmac_ch1_init( 0 );           // DMAC1 initialize
#endif// !JPEG_AREA_MULTI

    // PDC start
    R_pdc_receive_again(1);


    /* Sample code main loop */
    LED1 = LED_ON;
    while ( 1 )
    {
        /* Ethernet driver link up processing */
        R_ETHER_LinkProcess(0);

        /* HTTPD wake-up */
        R_httpd();

        if ( pdc_capt_retry_flag == 1 )
        {
            pdc_capt_retry_flag = 0;
#ifndef JPEG_AREA_MULTI
            R_DMACA_Control(DMACA_CH1, DMACA_CMD_SOFT_REQ_NOT_CLR_REQ, &stat_dmac);
#endif // !JPEG_AREA_MULTI
            R_pdc_receive_again( 1 );
        }
    }
}

/******************************************************************************
End of function main
******************************************************************************/


/******************************************************************************
Function Name   : R_req_capture_retry
Description     : pdc restart timming control
Arguments       : none
Return value    : none
******************************************************************************/
void R_req_capture_retry( uint8_t retry )
{
    R_BSP_InterruptsDisable();
    pdc_capt_retry_flag = retry;
    R_BSP_InterruptsEnable();
}

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