/***********************************************************************************************************************
* 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 WARRANTIESREGARDING 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) 2012, 2015 Renesas Electronics Corporation. All rights reserved.
***********************************************************************************************************************/

/***********************************************************************************************************************
* File Name    : r_main.c
* Version      : CodeGenerator for RL78/F13 V2.02.01.03 [30 Jan 2015]
* Device(s)    : R5F10BLG
* Tool-Chain   : CA78K0R
* Description  : This file implements main function.
* Creation Date: 2017/7/4
***********************************************************************************************************************/

/***********************************************************************************************************************
Pragma directive
***********************************************************************************************************************/
/* Start user code for pragma. Do not edit comment generated here */
/* End user code. Do not edit comment generated here */

/***********************************************************************************************************************
Includes
***********************************************************************************************************************/
#include "boot_macrodriver.h"
#include "boot_cgc.h"
#include "boot_port.h"
#include "boot_timer.h"
#include "boot_wdt.h"
/* Start user code for include. Do not edit comment generated here */
#include "r_rl78_can_drv.h"
#include "r_rl78_can_sfr.h"
#include "RL78_RCAN.h"
#include "boot_can_user.h"
#include "api.h"
#include "Dcm_extern.h"
#include "pfdl_user.h"
#include "CanTp.h"
#include "ftable.h"
#include "boot_adc.h"
#include "Dcm_App.h"
/* End user code. Do not edit comment generated here */
#include "boot_userdefine.h"
#include "CanTp_Cfg.h"

/***********************************************************************************************************************
Global variables and functions
***********************************************************************************************************************/
/* Start user code for global. Do not edit comment generated here */
#pragma section @@DATA KAM_SEG
	KamData_t KamData;
#pragma section @@DATA @@DATA

#pragma section @@DATA FslSeg
uint8_t fsl_code_buf[0x480];
#pragma section @@DATA @@DATA

uint16 ADval_Power;
uint16 AdcSample(uint8 ch) ;
void AdScan(void);

/* End user code. Do not edit comment generated here */
void Boot_MAIN_UserInit(void);
void IGN_Scan(void);
extern unsigned char flag_100ms;
unsigned char time,IGN_State;
#define CAN_STB P1.2
/***********************************************************************************************************************
* Function Name: main
* Description  : This function implements main function.
* Arguments    : None
* Return Value : None
***********************************************************************************************************************/
void boot_main(void)
{	    
    uint8 i;
    Boot_MAIN_UserInit();
    /* Start user code. Do not edit comment generated here */
    Boot_TAU0_Channel4_Start();    
    R_ADC_Start();
    R_ADC_Set_OperationOn();
    //P3.0 = 1;  // 开启AD_Bat
    CAN_STB = 0;
    if(KamData.boot_enable != RPRGRM_REQ_VALID)
	{
		if((GetAppValid() == APP_VALID)&&(GetAppEndValid() == APP_VALID))
		{
			DI();
			Boot_TAU0_Channel4_Stop();
			KamData.boot_enable = 0;
			KamData.RunAir = RUN_IN_APP;
			while(1)
			{
				((__far void(*)(void))(*(__far unsigned long *)(FLASH_TABLE)))();
			}
		}
		else
		{
			KamData.RunAir = RUN_IN_BOOT;
			InitCanTp_Parameter();
			InitDcm_Parameter();
			Boot_CAN_Pin_init();
			CanUserInit();
			BootDefaultSessionDeal(); 
		}
	}
	else
	{	
		KamData.RunAir = RUN_IN_BOOT;
		InitCanTp_Parameter();
		InitDcm_Parameter();
		Boot_CAN_Pin_init();
		CanUserInit();
		
		BootProgramSessionDeal();  
		
	}
#if 0 	
	if(KamData.boot_enable == RPRGRM_REQ_VALID)
	{
		SetCanTp_TxBuffer(KamData.RspData[0],&KamData.RspData[1]);
	}
#endif
	if(KamData.AckReq == ACK_REQ)
	{
		TransmitCAN_Message(CANTP_RESP_CANID,8,&KamData.RspData[0]);
		KamData.AckReq = 0;
	}
    while (1U)
    {
        Boot_WDT_Restart();
		MngDcm_1ms_Sever();
		i++;
		if(i>100)
		{
		    AdScan();
		    IGN_Scan();
		    i=0;
		}
    }
	
    /* End user code. Do not edit comment generated here */
}

/***********************************************************************************************************************
* Function Name: Boot_MAIN_UserInit
* Description  : This function adds user code before implementing main function.
* Arguments    : None
* Return Value : None
***********************************************************************************************************************/
void Boot_MAIN_UserInit(void)
{
    /* Start user code. Do not edit comment generated here */
	KamData.RstSrc = RESF;
	if(KamData.RstSrc != WDT_RST)
	{
		KamData.boot_enable = 0;
		KamData.RunAir = RUN_IN_BOOT;
		KamData.AckReq = 0;
	}    
    EI();
    /* End user code. Do not edit comment generated here */
}

/* Start user code for adding. Do not edit comment generated here */
void Boot_CAN_Pin_init(void)
{
    PM1.0 = 0;
    P1.0 = 1;
    PM1.1 = 1;

    //PM1.3 = 0;//OUTPUT
    CAN_STB = 0;
}

uint16 AdcSample(uint8 ch) 
{ 
	uint16 u2Temp;
	ADS = ch; //模拟通道输入寄存器
	ADCS = 1;  // A/D转换运行的控制，1为允许转换运行
	while(!ADIF);  //中断请求标志
	u2Temp = ADCR>>6;  //10 位 A/D 转换结果寄存器
	ADCS = 0;
	ADIF = 0;
	return  u2Temp;
 }

void AdScan(void)
{
    static uint8 FilterCounter;
    static uint16 AD_temp[5];
    uint8 i;
    uint16 AD_cal=0;
    AD_temp[FilterCounter] = AdcSample(6);
    FilterCounter++;
    if(FilterCounter >= 5)FilterCounter=0;
    for(i=0;i<5;i++)
    {
        AD_cal += AD_temp[i];
    }
    //ADval_Power = AD_cal/5;
    ADval_Power = 350;
}


void IGN_Scan(void)
{
    /*
    if(P1.7 == 1)
    {
        IGN_State = 0;
    }
    else
    {
        IGN_State = 1;
    }
    */
    IGN_State = 1;
}

/* End user code. Do not edit comment generated here */
