///**--------------File Info---------------------------------------------------------------------------------
//** File name:         interrupt.c
//** modified Date:         2013-03-12
//** Last Version:      V1.00
//** Descriptions:
//**
//*********************************************************************************************************/
////===================================================
////
////===================================================
//#include "Example_Flash2802x_API.h"
#include "CommunicateWithESC.h"
//#include "ESC_data_information.h"
//#include "Flash2802x_API_Library.h"
//#include "DSP2802x_Device.h"
#include "stdint.h"
//#include "aes256.h"
#include "string.h"
#include "device.h"

#define FRAME_DEVICE_USING                      FRAME_DEVICE_TYPE_ESC_DRV
unsigned short int pBody_Length = 0;

unsigned char  CrcCheckTemp_U8[2];
uint16_t CrcCheckTemp_U16 = 0;
UPGRADE_CTRL   UpgradeCtrl;
SYSTEM_INFOMATION_STRUCT pSysInfo;
uint16_t Temp32 = 0;
unsigned char cpuLow,cpuHigh;
uint16_t Temp32_CRC = 0;
 //===================================================================================

unsigned char aes_cbc_ini[16] = {"newbeeing.com.cn"};
unsigned char aes_key[32] = {"aaaabbbbccccddddeeeeffffgggghhhh"};
//============================dp=================================
const unsigned char AES_Temp[48] ={120,212,222,164,60,244,182,27,193,74,187,241,151,1,55,160,12,10,223,94,253,49,148,145,144,48,39,67,219,26,127,228,5,68,9,62,91,78,234,198,80,88,143,44,249,195,63,142 };


//aes256_context aes_ctx;

const unsigned char ESCType_flash[item_AREA_BYTES]     ={"XR_FOC_ESC_V5   "};
const unsigned char HardwareVer_flash[item_AREA_BYTES] ={"HW590_YT1_V1.1  "};
const unsigned char SoftwareVer_flash[item_AREA_BYTES] ={"    04.0.00     "};
unsigned char Null_flash[item_AREA_BYTES]              ={"    04.0.00     "};       // size: 0x10 byte


extern uint16_t                Buffer[WORDS_IN_FLASH_BUFFER];
extern unsigned long           boot_time_out;
extern unsigned char           boot_time_out_en;

unsigned short int TraPackageNo = 0;   //
unsigned short int RecPackageNo = 0;   //

unsigned short int th_curve_add = 0;   //
unsigned char item_add      = 0;   //
unsigned char default_DATA  = 0;    //

unsigned char TraData[TRA_INFORMATION_NO]; //

unsigned char TraDataNo = 0;       //
unsigned char BackupTraDataNo = 0;  //

unsigned char PackageIndex = 0;                 //
unsigned char PageIndex = 0;                    //

unsigned char ProgrameCardState;            //
unsigned char ConfigureRecState = REC_IDLE; //
unsigned char ReadConfDataMode = 0;  //

unsigned char debug_Counter;
unsigned char ProgrameCardRecDataFlag;
unsigned short int wflash_ok;
unsigned short int first_time;

signed int TimeOutFlag;
unsigned short int Tim_OutCon;
unsigned short int bRecESCTimeout;

unsigned long          rx_count;            //
unsigned char          Uart_time_out;       //
unsigned char          Uart_time_out_en;    //
unsigned char          Uart_st;             //

unsigned long           boot_time_out;
unsigned long           power_sw_time;
unsigned char           boot_time_out_en;
unsigned char           updata_over;

 _CommunicateData      CommunicateData; //

//extern   FLASH_ST      FlashStatus;
extern SECTOR        Sector[4];
extern   unsigned long boot_time_out;
extern   unsigned char updata_over;

extern void setmodeSci(unsigned char mode);



uint16_t Monitor[10] = {0,0,0,0,0,0,0,0,0,0};

//==============================================================================================================================


unsigned char CRCH[] =
{
        0x00,0xC1,0x81,0x40,0x01,0xC0,0x80,0x41,0x01,0xC0,0x80,0x41,0x00,0xC1,0x81,
        0x40,0x01,0xC0,0x80,0x41,0x00,0xC1,0x81,0x40,0x00,0xC1,0x81,0x40,0x01,0xC0,
        0x80,0x41,0x01,0xC0,0x80,0x41,0x00,0xC1,0x81,0x40,0x00,0xC1,0x81,0x40,0x01,
        0xC0,0x80,0x41,0x00,0xC1,0x81,0x40,0x01,0xC0,0x80,0x41,0x01,0xC0,0x80,0x41,
        0x00,0xC1,0x81,0x40,0x01,0xC0,0x80,0x41,0x00,0xC1,0x81,0x40,0x00,0xC1,0x81,
        0x40,0x01,0xC0,0x80,0x41,0x00,0xC1,0x81,0x40,0x01,0xC0,0x80,0x41,0x01,0xC0,
        0x80,0x41,0x00,0xC1,0x81,0x40,0x00,0xC1,0x81,0x40,0x01,0xC0,0x80,0x41,0x01,
        0xC0,0x80,0x41,0x00,0xC1,0x81,0x40,0x01,0xC0,0x80,0x41,0x00,0xC1,0x81,0x40,
        0x00,0xC1,0x81,0x40,0x01,0xC0,0x80,0x41,0x01,0xC0,0x80,0x41,0x00,0xC1,0x81,
        0x40,0x00,0xC1,0x81,0x40,0x01,0xC0,0x80,0x41,0x00,0xC1,0x81,0x40,0x01,0xC0,
        0x80,0x41,0x01,0xC0,0x80,0x41,0x00,0xC1,0x81,0x40,0x00,0xC1,0x81,0x40,0x01,
        0xC0,0x80,0x41,0x01,0xC0,0x80,0x41,0x00,0xC1,0x81,0x40,0x01,0xC0,0x80,0x41,
        0x00,0xC1,0x81,0x40,0x00,0xC1,0x81,0x40,0x01,0xC0,0x80,0x41,0x00,0xC1,0x81,
        0x40,0x01,0xC0,0x80,0x41,0x01,0xC0,0x80,0x41,0x00,0xC1,0x81,0x40,0x01,0xC0,
        0x80,0x41,0x00,0xC1,0x81,0x40,0x00,0xC1,0x81,0x40,0x01,0xC0,0x80,0x41,0x01,
        0xC0,0x80,0x41,0x00,0xC1,0x81,0x40,0x00,0xC1,0x81,0x40,0x01,0xC0,0x80,0x41,
        0x00,0xC1,0x81,0x40,0x01,0xC0,0x80,0x41,0x01,0xC0,0x80,0x41,0x00,0xC1,0x81,
        0x40
};

unsigned char CRCL[] =
{
        0x00,0xC0,0xC1,0x01,0xC3,0x03,0x02,0xC2,0xC6,0x06,0x07,0xC7,0x05,0xC5,0xC4,
        0x04,0xCC,0x0C,0x0D,0xCD,0x0F,0xCF,0xCE,0x0E,0x0A,0xCA,0xCB,0x0B,0xC9,0x09,
        0x08,0xC8,0xD8,0x18,0x19,0xD9,0x1B,0xDB,0xDA,0x1A,0x1E,0xDE,0xDF,0x1F,0xDD,
        0x1D,0x1C,0xDC,0x14,0xD4,0xD5,0x15,0xD7,0x17,0x16,0xD6,0xD2,0x12,0x13,0xD3,
        0x11,0xD1,0xD0,0x10,0xF0,0x30,0x31,0xF1,0x33,0xF3,0xF2,0x32,0x36,0xF6,0xF7,
        0x37,0xF5,0x35,0x34,0xF4,0x3C,0xFC,0xFD,0x3D,0xFF,0x3F,0x3E,0xFE,0xFA,0x3A,
        0x3B,0xFB,0x39,0xF9,0xF8,0x38,0x28,0xE8,0xE9,0x29,0xEB,0x2B,0x2A,0xEA,0xEE,
        0x2E,0x2F,0xEF,0x2D,0xED,0xEC,0x2C,0xE4,0x24,0x25,0xE5,0x27,0xE7,0xE6,0x26,
        0x22,0xE2,0xE3,0x23,0xE1,0x21,0x20,0xE0,0xA0,0x60,0x61,0xA1,0x63,0xA3,0xA2,
        0x62,0x66,0xA6,0xA7,0x67,0xA5,0x65,0x64,0xA4,0x6C,0xAC,0xAD,0x6D,0xAF,0x6F,
        0x6E,0xAE,0xAA,0x6A,0x6B,0xAB,0x69,0xA9,0xA8,0x68,0x78,0xB8,0xB9,0x79,0xBB,
        0x7B,0x7A,0xBA,0xBE,0x7E,0x7F,0xBF,0x7D,0xBD,0xBC,0x7C,0xB4,0x74,0x75,0xB5,
        0x77,0xB7,0xB6,0x76,0x72,0xB2,0xB3,0x73,0xB1,0x71,0x70,0xB0,0x50,0x90,0x91,
        0x51,0x93,0x53,0x52,0x92,0x96,0x56,0x57,0x97,0x55,0x95,0x94,0x54,0x9C,0x5C,
        0x5D,0x9D,0x5F,0x9F,0x9E,0x5E,0x5A,0x9A,0x9B,0x5B,0x99,0x59,0x58,0x98,0x88,
        0x48,0x49,0x89,0x4B,0x8B,0x8A,0x4A,0x4E,0x8E,0x8F,0x4F,0x8D,0x4D,0x4C,0x8C,
        0x44,0x84,0x85,0x45,0x87,0x47,0x46,0x86,0x82,0x42,0x43,0x83,0x41,0x81,0x80,
        0x40
};

unsigned char Read_First_From_Flash(void)
{
    unsigned char  res=0;
    uint32_t *Parameter_temp, Parameter_Temp32,Address = 0;
    Address = 0x0008FFF5;
    while(Address < 0x00090000)
    {
       Parameter_Temp32 = Address;
       Parameter_temp   = (uint32_t *)Parameter_Temp32;
       Temp32 = *Parameter_temp;
       if(Temp32 == 0xffff)
       {
          res = 1;
        }
       else
       {
          res = 0;
        }
       Address = Address + 1;
     }
    return res;
}


uint16_t  CalculateCRC16( unsigned char const *msgPtr, uint32_t msgLen)
{
     unsigned char crcHigh = 0xFF;
     unsigned char crcLow = 0xFF;
     unsigned char index;

     while (msgLen--)
     {
         index = crcLow ^ (*(msgPtr++));
         crcLow = crcHigh ^ CRCH[index];
         crcHigh = CRCL[index];
     }
     return (uint16_t)((uint16_t)(crcHigh<<8) | crcLow);
}

unsigned char crcHigh = 0xFF;
unsigned char crcLow = 0xFF;
unsigned char index;


uint16_t  CalculateCRC16_CPU(uint32_t const *msgPtr, uint32_t msgLen)
{
     crcHigh = 0xFF;
     crcLow = 0xFF;

     while (msgLen--)
     {
         cpuLow = (*(msgPtr))&0x00ff;
         cpuHigh = ((*(msgPtr))&0xff00)>>8;

         index = crcLow ^ cpuLow;
         crcLow = crcHigh ^ CRCH[index];
         crcHigh = CRCL[index];

         index = crcLow ^ cpuHigh;
         crcLow = crcHigh ^ CRCH[index];
         crcHigh = CRCL[index];

         msgPtr++;
     }
     return (uint32_t)((uint32_t)(crcHigh<<8) | crcLow);
}



unsigned char SystemApplicationChecking(void)
{
    uint16_t    CrcCheckValue=0;
    pSysInfo.ImageInfoRO                 = (FLASH_IMGINFO_STRUCT*)SYSTEM_CHECK_ADDRESS;

    if(pSysInfo.ImageInfoRO->ComImageCreatTimeYear==0xFFFF)                   return 2;
    if(((pSysInfo.ImageInfoRO->ComImageCreatTimeMonthDay)&0xFF00)>0x1F00)     return 3;
    if(((pSysInfo.ImageInfoRO->ComImageCreatTimeMonthDay)&0x00FF)>0x000C)     return 4;
    if(((pSysInfo.ImageInfoRO->ComImageCreatTimeHourMin)&0xFF00)>0x3C00)      return 5;
    if(((pSysInfo.ImageInfoRO->ComImageCreatTimeHourMin)&0x00FF)>0x0018)      return 6;

    CrcCheckValue = CalculateCRC16_CPU((uint32_t *)FLASH_START_ADDR,0x5ff8);

    if(CrcCheckValue==pSysInfo.ImageInfoRO->ComImageCheckCrcDecode)
        return 0;
    else
        return 0xFF;
}



void  Flash_Erase_Full(void)
{
    SysCtl_serviceWatchdog();
    DINT;
//    Example_EraseAppSector();
    EINT;

//    aes256_init(&aes_ctx,(void *)aes_cbc_ini,(void *)aes_key);  //
}


void UpgradeRequest(unsigned char MsgType, unsigned char DeviceType)
{

    UpgradeCtrl.Tx.pHeader->Statr       = FRAME_HEADER_START;
    UpgradeCtrl.Tx.pHeader->Identify    = FRAME_FLAG_REQUEST|FRAME_FLAG_NO_ERROR|FRAME_FLAG_SHORT_PACKET|DeviceType;
    UpgradeCtrl.Tx.pHeader->IdentifyNot = ~UpgradeCtrl.Tx.pHeader->Identify;
    UpgradeCtrl.Tx.pHeader->MsgType     = MsgType;
    UpgradeCtrl.Tx.pHeader->ErrorCode   = FRAME_ERR_NONE;

    CrcCheckTemp_U16                    = CalculateCRC16(TraData,5);

    TraData[5] = CrcCheckTemp_U16 & 0x00ff;
    TraData[6] = (CrcCheckTemp_U16 & 0xff00)>>8;
    Tra_ESC(TraData,7);
}



void UpgradeAnalyse(unsigned char const * Src, uint16_t Len)
{
    UpgradeCtrl.Rx.pHeader = (UPGRADE_PACKET_HEADER_STRUCT*)Src;
}



//====================================================================================
//Function  :void ConnectESCReadInf(void)
//Parameter
//          :None
//Return
//          :None
//Version   £ºV1.0       2013.03.02
//Remark
//===================================================================================
void Connect_ESC_Send_Inf(void)
{
    ProgrameCardState = 1;

    if(ProgrameCardRecDataFlag)           //
    {
        ProgrameCardRecDataFlag = 0;

        UpgradeAnalyse(CommunicateData.Buffer,rx_count);        //
    }
}



//============================================================================
//TraESC
// Return Value : None
// Parameters   :
// 1) uint uNumBytes
// 2) unsigned char *pData
//============================================================================
void Tra_ESC(unsigned char *pData,unsigned char uNumBytes)
{

    SysCtl_serviceWatchdog();
    Uart_st                     = 0;
    setmodeSci(0);              //

    SCI_writeCharArray(SCIA_BASE, (uint16_t*)pData, uNumBytes);

    while(!((HWREGH(SCIA_BASE + SCI_O_CTL2) & SCI_CTL2_TXRDY) ==
            SCI_CTL2_TXRDY))
    {
                //
    }

    while(!((HWREGH(SCIA_BASE + SCI_O_CTL2) & SCI_CTL2_TXEMPTY) ==
            SCI_CTL2_TXEMPTY))
    {
                //
    }

    setmodeSci(1);
    SysCtl_serviceWatchdog();

    if((SCI_getRxStatus(SCIA_BASE) & SCI_RXSTATUS_ERROR) == SCI_RXSTATUS_ERROR)    // rx err flag
    {
        SCI_performSoftwareReset(SCIA_BASE);        // reset sci

    }
}
























