/*******************************************************************************
 * the includes
 ******************************************************************************/
#include "appTask.h"
#include "canuser.h"
#include "uds_user.h"
#include "TLE94x1.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_SEND_BUF         (512)
#define UDS_RECV_BUF         (2056)


/*******************************************************************************
 * the typedefs
 ******************************************************************************/

/*******************************************************************************
 * 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;
uint32_t rollingcounter=0;


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,
};





/*******************************************************************************
 * the functions
 ******************************************************************************/



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);
    
}

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

        gSystick1msEvent--;
        gSystick1msCnt++;
        gSysTick1sCnt++;
        CANMsgTask();
        if (gSystick1msCnt % 5 == 0)
        {

        }
        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",rollingcounter++,ret);
        }
        

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

        
    }
}
