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

📄 psgdrv.c

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


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

/*
Name:    PSWaitStatusByteByByte
Desc:    Interrupt action for RX interrupt when a Status Byte, SW1 is expected
         See ISO/IEC 7816-3 8.2.2.
         Once correctly received, the interrupt handler is changed to expect
         the second status byte.
Params:  None
Returns: Nothing
Caveats:
*/

static void PSWaitStatusByteByByte(void)
   {
   UINT8 RXByte, RXAck;

   RXByte = PSGet();
      if (RXByte!=0x60) /* Its a NULL byte - must be patient */
      {                                                    
      RXAck = RXByte&0xF0;
      if (RXAck==0x60||RXAck==0x90)
         {
         PSStatusBuffer.SW1 = RXByte;
		 PSIntAction = PSWaitSW2;
#if SIM_DEBUG == SIM_DEBUG_MAX
         //PS Debug...
         OldIntAction_Dbg = NewIntAction_Dbg;
         NewIntAction_Dbg = _PSWaitSW2;
         //End PS Debug
#endif
         }
      /* else it is a mystery byte - ignore it ?????? */
      }
   }


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

/*
Name:    PSWaitSW2
Desc:    Interrupt action for RX interrupt when the second Status Byte, SW2
         is expected.  See ISO/IEC 7816-3 8.2.2.
         Once correctly received, the command sequence is complete and
         interrupt handler is changed to the Idle interrupt handler.
Params:  None
Returns: Nothing
Caveats:
*/

static void PSWaitSW2(void)
   {                                /* Received bad SIM status */
/*
   if (PSBadSIMStatus)              
      { PSStatusBuffer.SW2 = PSGet(); }
   else   
      { PSStatusBuffer.SW2 = *SIMbuffPtr; }
*/
   //GSMcprintf( MEMPutChar, "PSWaitSW2\n" );   
   PSStatusBuffer.SW2=PSGet();
   PSReturnToIdle(PSNormalCompletion);
   }

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

/*
Name:    PSInitChar
Desc:    Interrupt action for RX interrupt when the Initial Character is
         expected during Answer to Reset.  See ISO/IEC 7816-3 6.1.4.1.
         Once correctly received, the interrupt action is changed to expect
         The T0 Interface Character.
Params:  None
Returns: Nothing
Caveats:
*/

static void PSInitChar(void)
   {                                                                    
   UINT8 RXByte;

   RXByte = PSGet();
   if (RXByte==0x03 || RXByte==0x3F)  /* What you get with inverse when direct is set */
      {
      PSSetInverse();
//    PSRemainingWorkTime = 2;      //to speed up inverse SIM detection
//    2 is too fast -> slowing it to 50 frames
      PSRemainingWorkTime = 50;
      }
   else if (RXByte==0x3B)
      {
      PSSetDirect();
      }
   else
      return; /* Something screwy - ignore it */
#if defined(HW_SIM) 
    if ( *(UINT16 *)SIM_STAT_REG & SIM_ME_RX_FAIL)    /* Parity error - ignore it */
      {
      PSFoundInitChar = FALSE;    
      PSIntAction = PSInitChar;
#if SIM_DEBUG == SIM_DEBUG_MAX   
      //PS Debug...
      OldIntAction_Dbg = NewIntAction_Dbg;
      NewIntAction_Dbg = _PSInitChar;
      //End PS Debug
#endif
        
      }
    else                                                 
      {
      PSFoundInitChar = TRUE;
      PSIntAction = PST0Char;
#if SIM_DEBUG == SIM_DEBUG_MAX  
      //PS Debug...
      OldIntAction_Dbg = NewIntAction_Dbg;
      NewIntAction_Dbg = _PST0Char;
      //End PS Debug
#endif
       
      }
#else
   PSFoundInitChar = TRUE;
   PSIntAction = PST0Char;
#if SIM_DEBUG == SIM_DEBUG_MAX
   //PS Debug...
   OldIntAction_Dbg = NewIntAction_Dbg;
   NewIntAction_Dbg = _PST0Char;
   //End PS Debug
#endif
#endif
   PSErrAction = PSParityErr;
#if SIM_DEBUG == SIM_DEBUG_MAX
   //PS Debug...
   OldErrAction_Dbg = NewIntAction_Dbg;
   NewErrAction_Dbg = _PSParityErr;
   //End PS Debug
#endif

   PSDisableRxFailInterrupt(); /* to deal with inverse mode SIM */
   }

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

/*
Name:    PST0Char
Desc:    Interrupt action for RX interrupt when the Interface Character T0 is
         expected during Answer to Reset.  See ISO/IEC 7816-3 6.1.4.1.
         Once correctly received, the interrupt action is changed to expect
         subsequent interface characters, as determined by the helper procedure
         DetermineNextResetAnswer.
Params:  None
Returns: Nothing
Caveats: Note that this routine sets up PSTByteFlags, PSNoHistChars and
         PSTByteMask to deal with the first block of Interface characters,
         TA1..TD1, so that DetermineNextResetAnswer can predict what the SIM
         is going to send.
*/

static void PST0Char(void)
   {
   PSTByteFlags = PSGet();
   PSNoHistChars = PSTByteFlags&0x0F;
   PSTByteMask = 0x10;
   PSCheckSum ^= PSTByteFlags;

   DetermineNextResetAnswer();
   }

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

/*
Name:    PSTABCiChar
Desc:    Interrupt action for RX interrupt when the Interface Characters TAi,
         TBi or TCi is expected during Answer to Reset.
         See ISO/IEC 7816-3 6.1.4.1.
         Once correctly received, the interrupt action is changed to expect
         subsequent interface characters, as determined by the helper procedure
         DetermineNextResetAnswer.
Params:  None
Returns: Nothing
Caveats: Note that only the first two groups of interface characters are
         stored for protocol T=0.  These are the only ones rquired for a GSM
         SIM.
*/

static void PSTABCiChar(void)
   {
   UINT8 RXByte;
   RXByte = PSGet();
   PSCheckSum ^= RXByte;

   if (PSCurrProtType==0&&PSTi<2) /* We are only interested in TA1..TC2
                                     for Protocol T=0 */
      *PSTBytePtr = RXByte;
   DetermineNextResetAnswer();
   }

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

/*
Name:    PSTDiChar
Desc:    Interrupt action for RX interrupt when the Interface Character TDi is
         expected during Answer to Reset.  See ISO/IEC 7816-3 6.1.4.1.
         Once correctly received, the interrupt action is changed to expect
         subsequent interface characters, as determined by the helper procedure
         DetermineNextResetAnswer.
Params:  None
Returns: Nothing
Caveats: Note that this routine resets up PSTByteFlags, PSCurrProtType and
         PSTByteMask to deal with the subsequent block of Interface characters,
         TAi..TDi, so that DetermineNextResetAnswer can predict what the SIM
         is going to send.
*/

static void PSTDiChar(void)
   {
   PSTByteFlags = PSGet();
   PSCheckSum ^= PSTByteFlags;

   if (PSTi==0) /* TD1 is present - cannot assume T=0 is available */
      PSCardProtocols = 0;
   PSCurrProtType = PSTByteFlags&0x0F;
   PSCardProtocols |= 1<<PSCurrProtType;
   PSTByteMask = 0x10;
   PSTi++;
   DetermineNextResetAnswer();
   }

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

/*
Name:    PSHistChar
Desc:    Interrupt action for RX interrupt when an Historical Character Ti is
         expected during Answer to Reset.  See ISO/IEC 7816-3 6.1.4.1.
         Once all the Historical Characters have been received, the interrupt
         action is changed to expect the Checksum TCK or return to the idle
         state, as determined by the detection of a card supporting protocols
         other than T=0.
Params:  None
Returns: Nothing
Caveats: Note that the Historical Characters are ignored for GSM operation.
*/

static void PSHistChar(void)
   {
   UINT8 RXByte;
   RXByte = PSGet();
   PSCheckSum ^= RXByte;
   PSNoHistChars--;
   if (PSNoHistChars==0)
      {
      if (PSCardProtocols>1)            /* Protocol other than T=0 */
         {
         PSIntAction = PSWaitTCK;       /* Expect a checksum byte */
#if SIM_DEBUG == SIM_DEBUG_MAX
         //PS Debug...
         OldIntAction_Dbg = NewIntAction_Dbg;
         NewIntAction_Dbg = _PSWaitTCK;
         //End PS Debug
#endif
		 }
      else                              /* Answer to reset is complete */
         {
         PSCheckSum = 0;                /* In this case SIM doesn't send one */
         PSReturnToIdle(PSNormalCompletion);
         }
      }
   }

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

/*
Name:    PSWaitTCK
Desc:    Interrupt action for RX interrupt when a ChackSum Character TCK is
         expected during Answer to Reset.  See ISO/IEC 7816-3 6.1.4.1.
         Having received this character, the interrupt action returns to idle.
Params:  None
Returns: Nothing
Caveats:
*/

static void PSWaitTCK(void)
   {
   UINT8 RXByte;
   RXByte = PSGet();
   PSCheckSum ^= RXByte;

   PSReturnToIdle(PSNormalCompletion);
   }

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

/*
Name:    PSSendPTS0
Desc:    Interrupt action for TX interrupt to send The PTS0 character when
         Protocol Type Selection (PTS) must be performed.  See ISO/IEC 7816-3
         7.1.  Note that the Startup procedure for PTS will send the PTSS
         character.
         Having sent the PTS0 character, the interrupt action changes to send
         the PCK character on the next interrupt.
Params:  None
Returns: Nothing
Caveats: Note that for the current versions of GSM SIMs only PTS0=0 may be sent.
         See GSM 11.11 5.2.
*/

static void PSSendPTS0(void)
   {
   // Don't do a PTS1 if it needs to do a default speed PTS or
   // if extra guard time is requested by the SIM
    currentPTSState = PTS_DEFAULT_RETRY_STARTED;
   if (currentPTSState == PTS_DEFAULT_RETRY_STARTED)
      {
      // Make sure TA1 is set back to the default
      PSIFChars[0] = 0x11;
      PSSend(0x00);
      PSIntAction = PSSendPCK;
#if SIM_DEBUG == SIM_DEBUG_MAX
      //PS Debug...
      OldIntAction_Dbg = NewIntAction_Dbg;
      NewIntAction_Dbg = _PSSendPCK;
      //End PS Debug
#endif
      }
   else
   	  {
      // Turn on the TA1 bit for the F and D values
      PSSend(0x10);    
      PSIntAction = PSSendPTS1;
#if SIM_DEBUG == SIM_DEBUG_MAX
      //PS Debug...
      OldIntAction_Dbg = NewIntAction_Dbg;
      NewIntAction_Dbg = _PSSendPTS1;
      //End PS Debug
#endif
      }
   }


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

/*
Name:    PSSendPTS1
Desc:    Interrupt action for TX interrupt to send The PTS1 character when
         Protocol Type Selection (PTS) must be performed.  See ISO/IEC 7816-3
         7.1.  Note that the Startup procedure for PTS will send the PTSS
         character.
         Having sent the PTS1 character, the interrupt action changes to send
         the PCK character on the next interrupt.
Params:  None
Returns: Nothing
Caveats: Note that for the current versions of GSM SIMs only PTS0=0 may be sent.
         See GSM 11.11 5.2.
*/

static void PSSendPTS1(void)
   {
   // Send whatever F/D value the SIM is capable of - we could handle it!
   PSSend(PSIFChars[0]);
   PSIntAction = PSSendPCK;
#if SIM_DEBUG == SIM_DEBUG_MAX
   //PS Debug...
   OldIntAction_Dbg = NewIntAction_Dbg;
   NewIntAction_Dbg = _PSSendPCK;
   //End PS Debug
#endif
   }

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

/*
Name:    PSSendPCK
Desc:    Interrupt action for TX interrupt to send The PCK character when
         Protocol Type Selection (PTS) must be performed.  See ISO/IEC 7816-3
         7.1, 7.2.  Note that the preceding PTS0 is sent byte the interrupt
         action PSTXPTS0.
         Having sent the PCK character, the interrupt action changes to setting
         the UART mode to Input to receive the PTS acknowledgement.
Params:  None
Returns: Nothing
Caveats: Note that for the current versions of GSM SIMs only PTS0=0 may be sent.
         As a result this checksum is ALWAYS 0xFF! See GSM 11.11 5.2.
*/

static void PSSendPCK(void)
   {
   UINT

⌨️ 快捷键说明

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