/********************************************************************************************/
/** <!-- Copyright (C) Alpine Electronics, Inc. All rights reserved. ------------------------>
 *
 *	\file			mydriver.h
 *	\date			2013/09/13
 *	\author			Liuyan[NEU]
 *	\model
 *	\description
 *	\version		0.000
 *
 *********************************************************************************************
 *	\par	Revision History:
 *	<!-----	No.		Date		Revised by		Details	------------------------------------->
 *			0001	2013/09/13	Liuyan[NEU]		All NEW
 ********************************************************************************************/

#ifndef __MYDRIVER_TYPE_H__
#define __MYDRIVER_TYPE_H__

/********************************************************************************************/
/*							Include File Section											*/
/********************************************************************************************/

/********************************************************************************************/
/*							Macro Definition Section										*/
/********************************************************************************************/
#ifndef Type_Char
#define NEUSOFT_SET_CAN
#define UARTBASE_GPS  UART3_BASE
#define UARTBASE_GPRS  UART1_BASE
#define UARTBASE_DEBUG  UART2_BASE
#endif

#define MYDRIVER_DEBUGMSG(cond, printf_exp)	((void)((cond)?(DebugAPIFunc printf_exp),1:0))

#define	MYDRIVER_RUN_LOG			(0)		/* view Run Log   */

#define DBG_LOG  UARTprintf


#define MD5_IN_USE
//#define MD5_DBG

/* for  GPS  */
#define GPS_STATUS_INIT				(0)
#define GPS_STATUS_VALID			(1)
#define GPS_STATUS_UNBELIEVABLE		(2)

#define GPS_LATITUDE_NORTH			(0)
#define GPS_LATITUDE_SOUTH			(1)

#define GPS_LONGITUDE_EAST			(0)
#define GPS_LONGITUDE_WEST			(1)

/* for GPRS */
#define WIRELESS_SIM_ST_INIT        (0)
#define WIRELESS_SIM_ST_READY   	(1)
#define WIRELESS_SIM_ST_ERROR 		(2)

/* No File FileSize */
#define GPRS_FTP_NO_FILE_SIZE_INIT	(0xFFFFFFFF)

#if 0
#define DBG_LOG_DRV  UARTprintf
#else
#define DBG_LOG_DRV(...)
#endif
#define CAN_INPUT  0
#define CAN_OUTPUT 1

#ifdef MD5_IN_USE
#define FILE_BLOCK_SIZE	512
#endif

extern Type_uByte CAN1ERRORSTATA;
extern Type_uByte CAN3ERRORSTATA;

/********************************************************************************************/
/*							Type Definition Section											*/
/********************************************************************************************/

/********************************************************************************************/
/*							Enumeration Type Definition Section								*/
/********************************************************************************************/


/********************************************************************************************/
/*							Structure/Union Type Definition Section							*/
/********************************************************************************************/
//Structure define for I2C and FRAM
typedef struct I2CDataBuff_ts
{
     Type_uByte ubSlaveAddress ;
     Type_uByte    ubSlaveAddressBitSize ;
     Type_uHWord  uhSubOffset ;
     Type_uByte   ubSubOffsetSize  ;
     const Type_uByte  *  ubData_p;
     Type_uHWord    uhDataByteSize ;

} I2CDataBuff_ts ;

/*GPS*****************************************************************/

typedef struct
{
    Type_uByte ubUpdated;  // this data has been read?
    Type_uByte ubUTCDateTime[6]; 
    Type_uByte ubStatus; 
    Type_uWord uwLatitude; 
    Type_uByte ubNS; 
    Type_uWord uwLongitude; 
    Type_uByte ubEW; 
    Type_uWord uwSpeed; 
    Type_uWord uwCourse; 
    Type_uByte ubMode;
}GPRMCbuffer_ts;


typedef struct
{
    Type_uByte ubSatelliteID;
    Type_uByte ubElevation;
    Type_uHWord uhwAzimuth;
    Type_uByte ubSNR;
}GPGSVbuffer_ts;

#ifdef MD5_IN_USE
typedef struct
{
    unsigned int count[2];
    unsigned int state[4];
    unsigned char buffer[64];   
}MD5_CTX;
#endif

/********************************************************************************************/
/*							Global Variable Definition Section								*/
/********************************************************************************************/

/********************************************************************************************/
/*							Static Variable Definition Section								*/
/********************************************************************************************/

/********************************************************************************************/
/*							Static Prototype Declaration Section							*/
/********************************************************************************************/
/********************************************************************************************/
/*							ROM Table Section												*/
/********************************************************************************************/

/********************************************************************************************/
/*                      	Table Include Section                                   		*/
/********************************************************************************************/


/********************************************************************************************/
/*							Function Declaration											*/
/********************************************************************************************/
extern void init_fram() ;
extern Type_uByte FRAMRead(Type_uByte *pucData,Type_uHWord uhOffset,Type_uHWord uhCount) ;
extern Type_uByte FRAMWrite(const Type_uByte *pucData,Type_uHWord uhOffset,Type_uHWord uhCount) ;
extern Type_uByte FRAMWriteByte(Type_uHWord usAddr, Type_uByte ucData);
extern Type_uByte wubI2CDrv_StandbyOut(void) ;
extern Type_uByte wubI2CDrv_StandbyIn(void) ;
extern void wvdI2CDrv_Reset(void) ;
extern void wvdI2CDrv_EnforceStandby(void) ;
extern void wvdI2CDrv_Callback(Type_uByte ucSts) ;
extern Type_uByte I2C_Read(I2CDataBuff_ts * astBuff_p) ;
extern Type_uByte I2C_Write(I2CDataBuff_ts * astBuff_p) ;


/**********************SDCARD***********************/
extern void init_sdcard() ;
extern void wvdSDCardApp_Callback(Type_uHWord auhCallback, Type_uByte aubProcRslt) ;
extern void wvdSDCardDrv_Callback(Type_uByte ucSts) ;

/*************************CAN***********************/
typedef struct CANBUSInfo{
	Type_uByte ubChannel ;
	Type_uByte ubDir ;
	Type_uByte ubType ;
	Type_uHWord uHWData ;
} CANBUSInfo;
extern void wvdCANApp_CallBack(Type_uByte aubCANChannel, Type_uByte CallBackType, Type_uHWord  Data) ;

extern void init_low_can( void ) ;
extern void init_high_can( void ) ;

extern void init_CAN0CAN1_REC(void) ;

//****************************************
#if defined INCLUDE_UBLOX_GPRS
extern void wvdWirelessDrv_Callback(Type_uByte isStandbyOut);
#endif
extern void wvdGPSDrv_Callback(Type_uByte isStandbyOut);

#if defined INCLUDE_UBLOX_GPRS
extern void wvd3GDrv_ModuleRead(void);
extern void wvdGPSDrv_ModuleRead(void);
extern void ublox_demo(void);
#endif
//****************************************
//****************************************

extern void UARTSend(const unsigned char *pucBuffer, unsigned long ulCount) ;

/********************************************************************************************/
/*							Extern Declaration												*/
/********************************************************************************************/

/*************************UBLOX***********************/
extern Type_uByte wubCANDrv_StandbyOut(void);
extern void init_sys(void);
extern void init_gpio(void);
extern void init_uart(void);
extern void cfg_interrupt(void);

/*GPRS*****************************************************************/
#if defined INCLUDE_UBLOX_GPRS
extern void wubATCheckSignal( void );
extern void wubATCheckSIM( void );
extern void wubATSetAPN( void );
extern void wubATActivePDP( void );
extern void wubATDeActivePDP( void );
extern void wubATCreatSocket( void );
extern void wubATDropSocket( void );
extern void wubATConnectSocket( void );
extern void wubATDisconnectSocket( void );
extern void wubATGpsActive( void  );
extern void wubATGpsDisable( void  );
extern void wubAT_IMEI( void  );
extern void wubAT_CCID( void  );
extern void wubATGetData(Type_uHWord len);
extern void wvd3GTOUART(unsigned char aubchar);
extern void wvd3GDrv_TxHandler(void);
extern Type_uByte wubWirelessDrv_Connect(Type_uByte * aubIP_p,Type_uByte  aubIPSize,Type_uHWord  auhPort_p);
extern Type_uByte wubWirelessDrv_Send(Type_uByte *aubData_p, Type_uHWord aubDataSize);
extern void wvdWirelessGetCCID(Type_uByte *aubDataBuffer_p);
extern void wvdWirelessGetIMEI(Type_uByte *aubDataBuffer_p);
extern Type_uByte wvdWirelessGetSIMSt(void);
extern Type_uByte wvdWirelessGetSignal(void);
extern Type_uByte wvdWirelessGetRegSt(void);
extern Type_uByte wvbInitUblox(void);
extern Type_uByte wvbInitGPS(void);
extern Type_uByte wubiGetWirelessDrvInitStatus(void);
#endif

extern void wvdGPSApp_GetGPRMCinfo(GPRMCbuffer_ts *astGPRMCBuff_p);
extern void wvdGPSApp_GetGPGSVinfo(GPGSVbuffer_ts astGPGSVBuff_p[4]);

extern void wvdGPSDrv_GGA_off(Type_uByte aubCommand);
extern void wvdGPSDrv_GLL_off(Type_uByte aubCommand);
extern void wvdGPSDrv_VTG_off(Type_uByte aubCommand);
extern void wvdGPSDrv_GSV_off(Type_uByte aubCommand);
extern void wvdGPSDrv_GSA_off(Type_uByte aubCommand);
extern void wvdGPSDrv_RMC_off(Type_uByte aubCommand);
extern Type_uByte wubGPSDrv_StandbyOut(void);
extern Type_uByte wubWirelessDrv_StandbyOut(void);
extern Type_uByte wvbWirelessDrv_GprsInit(void);
extern Type_uByte wubWirelessDrv_FTPStart(Type_uByte *aubFtpIp,Type_uByte aubFtpIpSize , Type_uHWord auhPort
        , Type_uByte *aubUserName , Type_uByte *aubPassWord ,Type_uByte *aubRemoteFileName);


extern void wvdWirelessSocketControlN11(Type_uHWord *auhwDataBuffer_p);
extern int Cmd_bootloader(int argc, char *argv[]);
extern void wvd3GDrv_ErrorClear(void);
extern void wvdGPSApp_GetState(Type_uByte *aubGPSstate);
extern void SysTickHandler(void);
extern Type_uByte wubWirelessDrv_Disconnect(void);


extern void cfg_can2_spi1(void);
extern void cfg_can3_spi3(void);
extern void init_can2(void);
extern void init_can3(void);
extern void Transmit_Data(unsigned long number, unsigned long TX_A[],unsigned long TX_B[],unsigned long TX_C[]);
extern void Transmit_Data_CAN3(unsigned long number, unsigned long TX_A[],unsigned long TX_B[],unsigned long TX_C[]);
extern void Read_RX(int number, unsigned long RX[]);
extern void Read_RX_CAN3(int number, unsigned long RX[]);
extern unsigned long Reg_Read(unsigned long address);
extern void Reg_BitModify(unsigned long address,unsigned long mask, unsigned long data);
extern unsigned long Reg_Read_CAN3(unsigned long address);
extern void Reg_BitModify_CAN3(unsigned long address,unsigned long mask, unsigned long data);
extern void wvdGPSApp_TimeValid(Type_uByte *aubState);
extern void cfg_PE6_interrupt();
#ifdef MD5_IN_USE
extern void MD5_Init(MD5_CTX *context);
extern void MD5_Update(MD5_CTX *context, unsigned char *input, unsigned int inputlen);
extern void MD5_Final(MD5_CTX *context, unsigned char digest[16]);
#endif

#endif

