/*******************************************************************************
 * the includes
 ******************************************************************************/
#include "appTask.h"
#include "canuser.h"
#include "uds_user.h"
#include "TLE94x1.h"
#include "SEGGER_RTT.h"
#include "hod.h"
#include "ad_button1.h"
/*******************************************************************************
 * the defines
 ******************************************************************************/
/* Indication value with boot loader request from asw */
#define ASW_BOOT_REQ_ACTIVE   (0x55AAAA55ul)
/* Asw code head id to show asw is not empty */
#define ASW_HEAD_MASK         (0xAABBCCDDul)

#define ASW_VECTOR_START_ADDR 0xc000ul

#define UDS_RECV_BUF         (1026)
#define UDS_SEND_BUF         (128)

#define heat_level_low 80
#define heat_level_mid 85
#define heat_level_high 90

/*******************************************************************************
 * the globals
 ******************************************************************************/
#pragma location = ".bss.no_init"
static uint32_t sAswBoot_Req = 0;

McuType                      mcu;
volatile uint32_t            gSystick1msEvent = 0, gSystick1msCnt = 0, gTestRunCnt = 0, gTestIoEn = 0, gSysTick1sCnt = 0;

int64_t                      timer_1ms                = 0;
uint8_t                      udsSendBuf[UDS_SEND_BUF] = {0};
uint8_t                      udsRecvBuf[UDS_RECV_BUF] = {0};
UdsType                      udsObj;

Uds_ParamsType udsParam = {
    .isotpParams.framePadding = true,
    .isotpParams.blockSize    = 0,
    .isotpParams.recvPhysId   = UDS_PHYS_RECV_MSG_ID,
    .isotpParams.recvFuncId   = UDS_FUNC_RECV_MSG_ID,
    .isotpParams.sendid       = UDS_PHYS_RESP_MSG_ID,
    .isotpParams.sendBuf      = udsSendBuf,
    .isotpParams.sendBufSize  = UDS_SEND_BUF,
    .isotpParams.recvBuf      = udsRecvBuf,
    .isotpParams.recvBufSize  = UDS_RECV_BUF,
    .isotpParams.debug        = NULL,
    .isotpParams.sendCanMsg   = FlexCanBoot_TxMessage,
    .isotpParams.getTimeMs    = Get_Cur_Time_Stamp,
    .p2Server_ms              = 50,
    .p2xServer_10ms           = 500,
    .s3Server_ms              = 5000,
};


//DMK-RP-77C
volatile uint16_t adc_conv_val  = 0;
volatile uint32_t adc_conv_cnt  = 0;


volatile int16_t 	rawdata  = 0;
volatile int16_t 	basedata = 0;
volatile int16_t 	diffdata = 0;
volatile uint16_t 	tempdata = 0; /* adc raw val, not temp. */

const hod_gpio_t hod_high_side1_pin = {&mcu.ptc, 10};	//pwm p1  		
const hod_gpio_t hod_high_side2_pin = {&mcu.ptc, 11};	//pwm p2  		
const hod_gpio_t hod_low_side_pin   = {&mcu.ptc,  9};	//pwm n2  		
const hod_gpio_t hod_heat_shd_pin   = {&mcu.pta,  0};	//shd-en-pmos 	
const hod_gpio_t hod_sensor_pin     = {&mcu.ptc, 13};	//hod pc13 		
const hod_gpio_t hod_shd_pin        = {&mcu.ptc, 12};	//shd			

uint8_t heat_min=heat_level_low;

uint16_t tempdata1 = 0; /* adc raw val, not temp. */
uint32_t temp_adc=0;
uint8_t heating  = 0;
uint8_t heat_level  = 0;
volatile uint8_t handing  = 0;

uint32_t hod_heating_duty = 0; /* control heating duty, 10 ~ 99. */


/*******************************************************************************
 * the functions
 ******************************************************************************/
void heat_deal(void);



void Asw_SetBootloaderRequest(void)
{
    sAswBoot_Req = ASW_BOOT_REQ_ACTIVE;
}

int64_t Get_Cur_Time_Stamp(void)
{
    return timer_1ms;
}

void SysTick_Handler(void)
{
    gSystick1msEvent++;
    timer_1ms++;
    Uds_Tick(&udsObj);
}

void appTaskInit(void)
{
    /* UDS init */
    Uds_UserInit(&udsObj, &udsParam);
    while(!hod_init(timer_1ms)) {};
}

void appTask(void)
{
    
    if(gSystick1msEvent > 0u)
    {
        if(udsObj.session == UDS_SESSION_PROGRAMMING)
        {
            Asw_SetBootloaderRequest();
            ResetDrv_SoftwareResetModule(&mcu.resetDrv, RESETDRV_SWRESET_SYS);
        }

        gSystick1msEvent--;
        gSystick1msCnt++;
        gSysTick1sCnt++;
        
        if (gSystick1msCnt % 5 == 0)
        {
            CAN_MsgUpdate();
        }
        if (gSystick1msCnt % 50 == 0)
        {
            SBC_WD_Trigger();//喂狗
            
        }
        if (gSystick1msCnt % 1000 == 0)
        {
            //TxTestMsg(NULL);
            uint8_t ret = SBC_Read_Command(SBC_M_S_CTRL);//sbc测试
            //SEGGER_RTT_printf(0,"%06d : M_S_CTRL = 0x%x\n",0,ret);
        }
        

        if (gSystick1msCnt >= 10000)
        {
            gSystick1msCnt = 0;
            
        }

        //1ms task
        CANMsgTask();
        hod_task(timer_1ms);
        heat_deal();
    }
}

void hod_upload_info_cb(int16_t raw, int16_t base, int16_t diff)
{
    //rawdata  = raw;
    //basedata = base;
    //diffdata = diff;

    mfs_canmatrix_tx.MFS_HOD.MFS_HOD_RAW = raw;
    mfs_canmatrix_tx.MFS_HOD.MFS_HOD_baseline = base;
    mfs_canmatrix_tx.MFS_HOD.MFS_HOD_DIFF = diff;
}

void hod_touch_on_cb()
{
    handing = 1;
    mfs_canmatrix_tx.MFS_HOD.MFS_HOD_HANDING = 1;
}

void hod_touch_off_cb()
{
    handing = 0;
    mfs_canmatrix_tx.MFS_HOD.MFS_HOD_HANDING = 0;
}

void heat_deal(void)
{
	switch (heat_level)
	{
		case 1:
			heat_min=heat_level_low;
			break;
		case 2:
			heat_min=heat_level_mid;
			break;
		case 4:
			heat_min=heat_level_high;
			break;
		default:
			heat_min=heat_level_low;
			break;
	}
	
	tempdata < heat_min ? (hod_heating_duty = 80) : (hod_heating_duty = 20);
}


/* adc control, get adc raw data. */

void ADC_Handler()
{
    adc_conv_val += AdcDrv_GetRn(&mcu.adcDrv, 0);
    adc_conv_cnt++;

    if (adc_conv_cnt == 64)
    {
        tempdata1 = (adc_conv_val >> 8) & 0xFF;
        temp_adc = ((uint32_t)adc_conv_val)*4700;
		temp_adc /= (65536-adc_conv_val);
        tempdata = GetTempture(temp_adc);
        adc_conv_val  = 0;
        adc_conv_cnt  = 0;
    }
	
	for(uint8_t i=1;i<4;i++){
		adcRawValue[i] = AdcDrv_GetRn(&mcu.adcDrv, i);
    }
}
