⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 psgdrv.c

📁 SIM卡的驱动程序,有利于开发一些与SIM相通的一些工具,或者手机软件SIM驱动的开发
💻 C
📖 第 1 页 / 共 5 页
字号:
  PSInc,
  PSTPrf,
  PSEnv,
  PSFtch,
  PSTRsp,
  PSBad
  } XXXX;



/*---------------------------- Special Globals: --------------------------*/
extern CHAR SIMBuffer[257]; 
extern UINT16 GetAPDULength(UINT8 paramP3);
extern void SIResetSupplyVoltageIndicator(void);
UINT8 CCPAR0;
UINT8 CCPAR1;
UINT8 CCPAR2;
UINT8 CCRBR;
UINT8 CCTBR;
UINT8 CCINT;

PS_COMMAND PSCommandBuffer;        /* Interface buffer for SIM Command */
PS_STATUS  PSStatusBuffer;         /* Interface buffer to return SIM status */
UINT8      *PSDataBuffer;          /* Pointer to buffer for data to be
                                      transferred to or from the SIM */
BOOLEAN    DMABufferHasData;
void       (*PSIntAction)(void);   /* Current SIM action for a successful
                                      byte transfer */
void       (*PSErrAction)(void);   /* Current SIM action for a erroneous
                                      byte transfer.  Note assumes that
                                      normally byte will be automatically
                                      repeated under HW control! */
UINT16  PSRemainingWorkTime;        /* Remaining polling periods before
                                      timeout waiting for SIM to respond */
UINT16  PSWorkWaitingTime;          /* Value of the work waiting time in
                                      polling periods */

BOOL PSRxDMAEnabled =FALSE;  /* To keep track if whem SIM receive DMA is active */

                                      
INT16  PSTransferRemainingRetries; /* Count of remaining errouneous byte
                                      transfers before driver should give up
                                      trying to talk to SIM */

// This is to keep track of the current PTS state
PS_PTS_STATE currentPTSState = PTS_NOT_STARTED;

// This flag is used to keep track of Enhanced Clocking Mode retry attempts
PS_ENH_RETRY_STATE enhancedModeRetryState = PS_ENH_RETRY_INITIALIZED;

// GUARD PERIOD value used for Tx
UINT32 PSGuardPeriod = PS_DEFAULT_GUARD_PERIOD;

UINT8 *SIMbuffPtr;
SIM_DMA_CONFIG_STRUCT *SIMConfigDataPtr;
SIM_DMA_CONFIG_STRUCT SIMConfigData;

#if SIM_DEBUG == SIM_DEBUG_MAX
BOOL HighSpeedClockEnabled_Dbg = FALSE;
BOOL DisableClockHigh_Dbg = FALSE;
PS_ACTION_DBG OldIntAction_Dbg = _NotAssignedYet;
PS_ACTION_DBG NewIntAction_Dbg = _NotAssignedYet;
PS_ACTION_DBG OldErrAction_Dbg = _NotAssignedYet;
PS_ACTION_DBG NewErrAction_Dbg = _NotAssignedYet;
PS_IH_DBG IntHandler_Dbg = _SimResetTimerAIH;
UINT8 DefectSIMScenario_Dbg = 0;
UINT8 PSStateScenario_Dbg = 0;
PS_STATE OldPSState_Dbg = PSNoMess;
UINT8 PSDeactivateSIM_Dbg = 0;
PS_COMPLETION_CODE PSCompletionCode_Dbg = PSNormalCompletion;
#elif SIM_DEBUG == SIM_DEBUG_MIN
UINT8 DefectSIMScenario_Dbg = 0;
PS_STATE OldPSState_Dbg = PSNoMess;
#endif

SIM_CLOCK_SPEED PSClockSpeed;       // Flag that indicates the clock speed of SIM.

/*----------------------- Local Function Prototypes: ---------------------*/

void DeadLoop( void );                     // PMIC access error, stop
static void PSStartSIMResetSequence(void);
static void PSNullCompleter(PS_COMPLETION_CODE CompletionCode);
static void PSCommandCompleter(PS_COMPLETION_CODE CompletionCode);
static void PSStartCommandCompleter(PS_COMPLETION_CODE CompletionCode);
static void PSStartResetCompleter(PS_COMPLETION_CODE CompletionCode);
static void PSResetLoCompleter(PS_COMPLETION_CODE CompletionCode);
static void PSResetHiCompleter(PS_COMPLETION_CODE CompletionCode);
static void PSPTSCompleter(PS_COMPLETION_CODE CompletionCode);
static void PSIdleCompleter(PS_COMPLETION_CODE CompletionCode);
//added by gary for phase 2
#if PS_CLOCK_START_DELAY>0
static void PSResetStartClockCompleter( PS_COMPLETION_CODE CompletionCode );
#endif
//end of added by gary for phase 2

#ifdef PSDisableClockHigh
static void PSStopClockHighCompleter(PS_COMPLETION_CODE CompletionCode);
#endif
#ifdef PSDisableClockLow
static void PSStopClockLowCompleter(PS_COMPLETION_CODE CompletionCode);
#endif

static void PSIdle(void);
void PSSendINS(void);
static void PSSendP1(void);
static void PSSendP2(void);
static void PSSendP3(void);
static void PSTXOneByte(void);
static void PSWaitProcByte(void);
static void PSRXOneByte(void);
static void PSRXData(void);
static void PSTXData(void);
static void PSWaitStatus(void);
static void PSWaitStatusByteByByte(void);
static void PSWaitSW2(void);
static void PSInitChar(void);
static void PST0Char(void);
static void PSTABCiChar(void);
static void PSTDiChar(void);
static void PSHistChar(void);
static void PSWaitTCK(void);
static void PSSendPTS0(void);
static void PSSendPTS1(void);
static void PSSendPCK(void);
static void PSSendPTSDone(void);
static void PSWaitPTSSAck(void);
static void PSWaitPTS0Ack(void);
static void PSWaitPTS1Ack(void);
static void PSWaitPCKAck(void);
static void PSParityErr(void);
static void EnableDMAWaitStatus(void); 
static void ClampVSIM(BOOL state);
static void PSSetSIMVoltage(UINT8 simvcc);
static void PSSetGuardPeriod( UINT32 period );

/*---------------------------- Local Statics: ----------------------------*/

/* The C166 locator renders the whole stack unusable, if this variable
   isn't defined as "far" */

static UINT16 PSInitRemainingRetries;  /* Count of remaining errouneous byte
                                          transfers before driver should give
                                          up waiting for an initial character
                                          on ATR */

//end of added by gary for phase 2

static const PS_STATE CompletionCodeToState[7] =
                   { PSSIM, PSSIM, PSSIM, PSNoSIM, PSDefectSIM, PSDefectSIM, PSDefectSIM };
              /* Look up table to map SIM completion codes to the SIM State */
static void (*PSCompleter)(PS_COMPLETION_CODE CompletionCode) = PSNullCompleter;
                                      /* Pointer to current completer routine */
#if SIM_DEBUG == SIM_DEBUG_NONE
static PS_COMPLETION_CODE PSCompletionCode;  /* Completion code of current SIM operation */
#else
PS_COMPLETION_CODE PSCompletionCode;  /* Completion code of current SIM operation */
#endif
INT16 PSDataCount;                /* Count of remaining Data Bytes to be
                                         transferred */
static UINT16 PSCardProtocols;        /* Bit map of Protocols supported by
                                         SIM */
static INT16 PSResetRemainingRetries; /* Count of remaining retries to
                                         Reset SIM */
static UINT8 PSTByteMask;             /* Bit map mask for Protocol types
                                         supported by SIM */
static UINT8 PSTByteFlags;            /* Flags for the protocol characters
                                         received from SIM during answer to
                                         reset.  Determines which characters
                                         will be transmitted */
static UINT8 PSNoHistChars;           /* Number of Historical Characters
                                         to expect during answer to reset */
static UINT8 PSCurrProtType;          /* Current Protocol Type that SIM
                                         claims to support during answer
                                         to reset */
static UINT8 PSTi;                    /* Counter for currently expected
                                         interface character during answer to
                                         reset */
static UINT8 PSCheckSum;              /* Current checksum of bytes received
                                         during answer to reset */
static UINT8 PSDirection;             /* Direction of data transfer of current
                                         SIM command */
static UINT8 *PSTBytePtr;             /* Pointer into PSIFChars array to where
                                         current IF char should be stored */
static UINT8 PSIFChars[6];            /* Array to store the Interface
                                         characters useful to the GSM
                                         application that have been received
                                         during the answer to reset */
static BOOLEAN PSFoundInitChar;       /* Flag to indicate SIM has started
                                         answering the reset */
static BOOLEAN PSEverFoundInitChar;   /* Flag to indicate the SIM started
                                         answering the reset in some reset
                                         attempt */
// Also used by psintrpt.c
BOOLEAN PSClockStopped;               /* Flag to indicate that the SIM driver
                                         has stopped the SIM clock */
static BOOLEAN PSSocketClosed;        /* Flag to determine if the SIM Socket
                                         os closed or not */
static PS_STATE PSState;              /* The current State of the SIM driver */

static UINT8 PSCommandRetryCount=0;            /* Retry Counter for incompleted SIM commanad
                                                  will be set to PS_COMMAND_RETRY_COUNT.
                                                  Must initialy be zero in order to pass FTA 27.17.1.5.1*/

static const UINT8 UniqueLookUp[128] =
   {
/*          X0      X2      X4      X6      X8      XA      XC     XE   */
/* 0X */  PSBad,  PSBad,  PSInv,  PSBad,  PSBad,  PSBad,  PSBad,  PSBad,
/* 1X */ PSTPrf, PSFtch, PSTRsp,  PSBad,  PSBad,  PSBad,  PSBad,  PSBad,
/* 2X */  PSVer,  PSBad,  PSChg,  PSDis,  PSEna,  PSBad, PSUnbl,  PSBad,
/* 3X */  PSBad,  PSInc,  PSBad,  PSBad,  PSBad,  PSBad,  PSBad,  PSBad,
/* 4X */  PSBad,  PSBad,  PSReH,  PSBad,  PSBad,  PSBad,  PSBad,  PSBad,
/* 5X */  PSBad,  PSBad,  PSBad,  PSBad,  PSBad,  PSBad,  PSBad,  PSBad,
/* 6X */  PSBad,  PSBad,  PSBad,  PSBad,  PSBad,  PSBad,  PSBad,  PSBad,
/* 7X */  PSBad,  PSBad,  PSBad,  PSBad,  PSBad,  PSBad,  PSBad,  PSBad,
/* 8X */  PSBad,  PSBad,  PSBad,  PSBad,  PSRun,  PSBad,  PSBad,  PSBad,
/* 9X */  PSBad,  PSBad,  PSBad,  PSBad,  PSBad,  PSBad,  PSBad,  PSBad,
/* AX */  PSBad, PSSeek,  PSSel,  PSBad,  PSBad,  PSBad,  PSBad,  PSBad,
/* BX */ PSRBin, PSRRec,  PSBad,  PSBad,  PSBad,  PSBad,  PSBad,  PSBad,
/* CX */ PSGRes,  PSEnv,  PSBad,  PSBad,  PSBad,  PSBad,  PSBad,  PSBad,
/* DX */  PSBad,  PSBad,  PSBad, PSWBin,  PSBad,  PSBad, PSWRec,  PSBad,
/* EX */  PSBad,  PSBad,  PSBad,  PSBad,  PSBad,  PSBad,  PSBad,  PSBad,
/* FX */  PSBad,  PSSta,  PSBad,  PSBad,  PSBad,  PSSlp,  PSBad,  PSBad
   };

static BOOLEAN PSBadSIMStatus;       /* Flag to indicate bad SIM status */

/*---------------------------- Local Functions: --------------------------*/


/*---------------------------- Utility Functions: --------------------------

The PS Driver Utility functions are static helper functions used by the
SIM driver code
*/

//***************************************************************************
//
// Name:    PSSetClockSpeed
// Desc:    Helper function to set up the SIM block clock speed based on the 
//        	parameter specified in pubparam.c
// Params:  None
// Returns: None
// Caveats:
//***************************************************************************
void  PSSetClockSpeed()
{
	switch (PSClockSpeed)
	{
		case SIM_CLOCK_3_9Mhz :
			*(UINT16 *)SIM_CTRL_HI_REG &= ~(SIM_CLK_DIV);
#if SIM_DEBUG == SIM_DEBUG_MAX
			HighSpeedClockEnabled_Dbg = TRUE;
#endif  
			break;

		case SIM_CLOCK_1_95Mhz :
		default:				// errror case, set to 1.95Mhz and let it run
			*(UINT16 *)SIM_CTRL_HI_REG |= SIM_CLK_DIV ;
#if SIM_DEBUG == SIM_DEBUG_MAX
			HighSpeedClockEnabled_Dbg = FALSE;
#endif  
			break;
	}
}


/*------------------------------------------------------------------------*/

/*
Name:    PSAcceptSIM
Desc:    Helper function to set up the SIM driver state following acceptance
         of a valid SIM.  Having accepted the SIM the SIM driver can accept
         and process SIM Commands.
Params:  None
Returns: Nothing
Caveats:
*/

static void PSAcceptSIM(void)
   {
   PSCompleter = PSIdleCompleter;
   PSState = PSSIM;
#if SIM_DEBUG == SIM_DEBUG_MAX
   PSStateScenario_Dbg = 1;
#endif
   }

/*------------------------------------------------------------------------*/

/*
Name:    PSDeactivateSIM
Desc:    Helper function to return the SIM driver to the idle state following
         removal of the SIM, detection of an invalid SIM, or simply as part
         of the power down deactivation sequence.
Params:  PS_STATE SIMState - The new SIM driver state.
Returns: Nothing
Caveats:
*/

static void PSDeactivateSIM(PS_STATE SIMState)
   {

   //PSChangeIdleToDeact();
   PSDoShutDown();
   PSIntAction = PSIdle;
   PSErrAction = PSIdle;
   PSCompleter = PSIdleCompleter;
//changed by gary for phase 2
#if PS_CLOCK_START_DELAY>0
   PSRemainingWorkTime = PS_CLOCK_START_DELAY;
   PSCompletionCode = PSBusy;
#else
   PSCompletionCode = PSNormalCompletion;
#endif
//end of changed by gary for phase 2
   PSState = SIMState;
#if SIM_DEBUG == SIM_DEBUG_MAX
   PSStateScenario_Dbg = 2;
#endif

#if SIM_DEBUG == SIM_DEBUG_MAX
   //PS Debug...
   OldIntAction_Dbg = NewIntAction_Dbg;
   NewIntAction_Dbg	= _PSIdle;
   OldErrAction_Dbg	= NewErrAction_Dbg;
   NewErrAction_Dbg	= _PSIdle;
   //End PS Debug
#endif
   }

/*------------------------------------------------------------------------*/

/*
Name:    PSDoPTSIfNecessary(void)
Desc:    Routine to check the results of the 'answer to reset', and determine
         if the Protocol Type Selection procedure needs to be performed.
         As a result of performing the analysis of the answer to reset, the
         SIM driver may reject the SIM.  Reasons for rejecting a SIM are
         specified in GSM rec. 11.10.
         The Routine also sets the value of PSWorkWaitingTime, which is used
         as a timeout value for SIM responses.
Params:  None
Returns:
Caveats: This code is specific to GSM SIMs!
*/

static void PSDoPTSIfNecessary(void)
   {
   /* Set up WorkWaitingTime */
   if (PSIFChars[5]==0) 
      PSIFChars[5] = PS_DEFAULT_WI;
   PSWorkWaitingTime = MSECS(PSIFChars[5]*PS_WORK_WAIT_FACTOR);


   if (((PSIFChars[1]&0x1F)!=0&&(PSIFChars[1]&0x1F)!=5)||PSIFChars[4]!=50)
      /* The card required non-standard programming voltages */
      {
      PSStartSIMResetSequence();   
      //      PSDeactivateSIM(PSDefectSIM);
      }
   else if (PSIFChars[2]!=0&&PSIFChars[2]!=255) /* TC1 not valid */
      {
      PSStartSIMResetSequence();   
      // PSDeactivateSIM(PSDefectSIM);
      }
   else if ((PSCardProtocols&1)==0) /* The card does not support T=0 */
      {
      PSStartSIMResetSequence();   
      //PSDeactivateSIM(PSDefectSIM);
      }
   else if ((PSCardProtocols>1|| /* The card supports protocols other than T=0 */
//            Need to also check if TA1 != 0x01
//            PSIFChars[0]!=0x11||
            ((PSIFChars[0]!=0x11)&&(PSIFChars[0]!=0x01))||
            PSIFChars[2]==255) && (currentPTSState != PTS_DEFAULT_RETRY_STARTED))
      {                         /* Must perform protocol type selection */
      // This makes the ME initiate PPS procedure using default values
      // after two failed PPS attempts.
      // Originally, this variable is for three consecutive wrong ATRs
      PSResetRemainingRetries = PS_RESET_ALLOWED_RETRIES;
      PSChangeIdletoTX();
      PSSend(0xFF);
      PSCompletionCode = PSBusy;
      PSCompleter = PSPTSCompleter;
      PSIntAction = PSSendPTS0;
   
 //   Move to the next PTS state
      if (PSIFChars[0] == 0x94)
         (UINT8)currentPTSState++;  

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -