
#include "appTask.h"
#include "uds.h"
#include "uds_user.h"
#include "stdint.h"
#include "drivers/flexcan/flexcan_drv.h"
#include "hwctrl.h"
#include "TLE94x1.h"
#include "string.h"
#include "SEGGER_RTT.h"
#include "MotorCtrl.h"
#include "canuser.h"

/*******************************************************************************
 * the defines
 ******************************************************************************/
#define UDS_RECV_BUF         (2056)
#define UDS_SEND_BUF         (512)

/* Indication value with boot loader request from asw */
#define ASW_BOOT_REQ_ACTIVE   (0x55AAAA55ul)




uint8_t udsSendBuf[UDS_SEND_BUF] = {0};
uint8_t udsRecvBuf[UDS_RECV_BUF] = {0};



UdsType                      udsObj;
int64_t Get_Cur_Time_Stamp(void);
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,
};


int64_t timer_1ms = 0;
volatile uint32_t gSystick1msEvent = 0, gSystick1msCnt = 0, gTestRunCnt = 0, gTestIoEn = 0, gSysTick1sCnt = 0;

#pragma location = ".bss.no_init"
static uint32_t sAswBoot_Req;

//
int64_t Get_Cur_Time_Stamp(void)
{
    return timer_1ms;
}

void Asw_SetBootloaderRequest(void)
{
    sAswBoot_Req = ASW_BOOT_REQ_ACTIVE;
}

void SysTick_Handler(void)
{
    gSystick1msEvent++;
    timer_1ms++;
    Uds_Tick(&udsObj);
}
void appTask(McuType *obj)
{
    if(gSystick1msEvent > 0u)
    {
        if(udsObj.session == UDS_SESSION_PROGRAMMING)
        {
            Asw_SetBootloaderRequest();
            ResetDrv_SoftwareResetModule(&obj->resetDrv, RESETDRV_SWRESET_SYS);
        }
        gSystick1msEvent--;
        gSystick1msCnt++;
        gSysTick1sCnt++;
        MsgTask(&udsObj);
        if (gSystick1msCnt % 10 == 0)
        {
            MotorCtrl_Maintask(obj);
        }

        if (gSystick1msCnt % 50 == 0)
        {
            SBC_WD_Trigger();
        }
        
        if(gSystick1msCnt % 200 == 0)
        {

            
        }
        if (gSystick1msCnt >= 10000)
        {
            gSystick1msCnt = 0;
            SEGGER_RTT_printf(0,"app running\n");
        }
        
    }
}

void appTaskInit(McuType *obj)
{
    /* UDS init */
    Uds_UserInit(&udsObj, &udsParam);

    MotorCtrl_Init(obj);
}












