Pages: [1]   Go Down
Author Topic: Rewrite  (Read 424 times)
0 Members and 1 Guest are viewing this topic.
0
Offline Offline
Newbie
*
Karma: 0
Posts: 16
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Hi

I bougth a smartcardreader and some cards, but i dont know how to program it... I have a arduino duemilanove, but i found some code to another microcontroller, is there someone there can help me rewrite it to "arduino language"?

Code:
' SC4442_Demo.bs2

' {$STAMP BS2}
' {$PBASIC 2.5}

' This program demonstrates the basics of reading and writing data to the
' IS23SC4442 smart card.  Since this card has a Programmable Security Code
' (PSC), the user is required to enter a password before writing data to
' the card.

' This program simply declares the password as a constant (default $FFFFFF)
' and when 'authenticate' is selected, it compares that password to the one
' on this card.  This MUST be selected before the user is able to write
' data to the card.

' -----[ I/O Definitions ]-------------------------------------------------

IO                 PIN    3                   ' Serial Data IO
CD                 PIN    2                   ' Card Detect
CLK                PIN    1                   ' Serial Data Clock
RST                PIN    0                   ' Reset

' -----[ Constants ]-------------------------------------------------------

ReadMain           CON    $30                 ' Read main memory
ReadProtection     CON    $34                 ' Read protection bit
ReadSecurity       CON    $31                 ' Read PSC
UpdateMain         CON    $38                 ' Update main memory
UpdateSecurity     CON    $39                 ' Update PSC
UpdateProtection   CON    $3C                 ' Update Protection bits
CompareData        CON    $33                 ' Compare Verification Data

' Programmable Security Code - REMEMBER TO CHANGE IF PASSWORD IS UPDATED!!
Password1          CON    $FF                 ' 1st byte of PSC
Password2          CON    $FF                 ' 2nd byte of PSC
Password3          CON    $FF                 ' 3rd byte of PSC

' -----[ Variables ]-------------------------------------------------------

index              VAR    Byte                ' Variable space for FOR...NEXT loop
command            VAR    Byte                ' Command format
dat                VAR    Byte                ' Variable space for data
address            VAR    Byte                ' Address location in memory

PSW1               VAR    Byte                ' Variable space for PSC comparison
PSW2               VAR    Byte
PSW3               VAR    Byte

temp               VAR    Byte
errCounter         VAR    Byte                ' Error counter for PSC
choice             VAR    Nib                 ' Main menu selection
keyboard           VAR    Bit                 ' Input from keyboard

' -----[ Initialization ]--------------------------------------------------
' Declares PSC and waits for a smart card to be inserted
Program_Start:
  DEBUG CLS

  DO                                             ' Check for smart card,
    DEBUG HOME, "No card inserted!"              ' loop until card detected
  LOOP UNTIL(CD = 1)

  GOSUB Reset                                    ' Reset smart card

' -----[ Program Code ]----------------------------------------------------
Main:
  DO
    DEBUG HOME, "Remember to authenticate before writing data to the card!", CR,
          CR, "Select:", CR,
          "  1) Read Data from the card", CR,
          "  2) Authenticate", CR,
          "  3) Write Data to the card", CR, CR,
          "Selection: ", CLREOL
    DEBUGIN DEC Choice

    SELECT Choice
      CASE 1: GOSUB Read_Data_From_Card
      CASE 2: GOSUB Authenticate
      CASE 3: GOSUB Write_Data_To_Card
    ENDSELECT
  LOOP

' -----[ Subroutine - Read_Data_From_Card ]--------------------------------
' Reads all data from memory
Read_Data_From_Card:
  ' Display memory map heading
  DEBUG HOME, 0, 13, REP "-"\36, "[   DATA   ]", REP "-"\36,
        CR, "Addr.  *0   *1   *2   *3   *4   *5   *6   *7   *8   *9   *A   *B   *C   *D   *E   *F",
        CR, REP "_"\84

  address = 0                                    ' Set address location to 0
  command = ReadMain                             ' Set command format to read main memory
  GOSUB Read_Main                                ' Call the Read_Main subroutine

  DEBUG CR, REP "_"\84, CR                       ' Finish memory map "box"
  DEBUG "Press any key to return to menu..."
  DEBUGIN keyboard                               ' Receive input from user
  DEBUG CLS
RETURN

' -----[ Subroutine - Authenticate ]---------------------------------------
' Compares password delcared in 'Constants' to PSC in memory
Authenticate:
  DEBUG CLS, "Authenticating...", CR
  GOSUB Compare
  SELECT errCounter
    CASE %000: DEBUG HOME, "Card is locked.", CLREOL, CR, CLREOL
    CASE %001: DEBUG HOME, "Invalid PSC, one try remaining."
    CASE %011: DEBUG HOME, "Invalid PSC, two tries remaining."
    CASE %111: DEBUG HOME, "PSC verified, you may now write data to the card."
  ENDSELECT

  DEBUG CR, CR, "Press any key to return to menu..."
  DEBUGIN keyboard                               ' Receive input from user
  DEBUG CLS
RETURN

' -----[ Subroutine - Write_Data_To_Card ]---------------------------------
' Writes data to specified address location
Write_Data_To_Card:
  DEBUG CLS, "Write Data to Address", CR, REP "-"\25, CR, CR,
        "Input address location (HEX): ", CLREOL
  DEBUGIN HEX address                            ' Receive address location from user
  DEBUG "Input data (HEX): ", CLREOL
  DEBUGIN HEX Dat                                ' Receive data from user

  command = UpdateMain                           ' Set command format to update main memory
  GOSUB Update_Main                              ' Call the Update_Main subroutine
  GOSUB Read_Data_From_Card                      ' Display memory map for verification
RETURN


' -----[ Subroutine - Read_Main ]------------------------------------------
' Call I2C protocol to read main memory
Read_Main:
  GOSUB Send_Command
    FOR index = 0 TO 255
      IF (index//16 = 0) THEN DEBUG CR, HEX1 index/16, "*  |"
      GOSUB Recieve_Byte
      DEBUG "  ", HEX2 Temp, " "
    NEXT
RETURN

' -----[ Subroutine - Update_Main ]----------------------------------------
' Calls I2C protocol to update main memory
Update_Main:
  GOSUB Send_Command
  GOSUB Process
RETURN

Logged

0
Offline Offline
Newbie
*
Karma: 0
Posts: 16
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Code:
' -----[ Subroutine - Compare ]--------------------------------------------
' Calls I2C protocol to compare PSC
Compare:
  command = ReadSecurity                        ' Set command format to read protection memory
  address = 0                                   ' Reset address to 0
  dat = 0                                       ' Reset dat to 0
  GOSUB Read_Security                           ' Call Read_Security subroutine

  command = UpdateSecurity                      ' Updates error counter if PSC is invalid
  address = 0
   IF(errCounter = %111) THEN dat = %011
   IF(errCounter = $011) THEN dat = $001
   IF(errCounter = $001) THEN dat = $000
  GOSUB Send_Command
  GOSUB Process

  Command = CompareData                         ' Set command format to compare data

  Address = $01                                 ' Compare 1st byte of PSC
  Dat = PassWord1
  GOSUB Send_Command
  GOSUB Process

  Address = $02                                 ' Compare 2nd byte of PSC
  Dat = PassWord2
  GOSUB Send_Command
  GOSUB Process

  Address = $03                                 ' Compare 3rd byte of PSC
  Dat = PassWord3
  GOSUB Send_Command
  GOSUB Process

'   Command=UpdateSecurity                                ' Erase Error Counter
'   Address=$00
'   Dat=$07                                               ' If password match then the counter restore
'   GOSUB Send_Command
'   GOSUB Process
RETURN

' -----[ Subroutine - Read_Security ]--------------------------------------
' Collects 3 byte PSC from protection memory
Read_Security:
   GOSUB Send_Command                            ' Call Send_Command subroutine
   GOSUB Recieve_Byte                            ' Retrieve error counter value &
   errCounter = Temp                             ' store in errCounter variable
   GOSUB Recieve_Byte                            ' Retrieve 1st byte of PSC &
   PSW1 = Temp                                   ' store in PSW1 variable
   GOSUB Recieve_Byte                            ' Retrieve 2nd byte of PSC &
   PSW2 = Temp                                   ' store in PSW2 variable
   GOSUB Recieve_Byte                            ' Retrieve 3rd byte of PSC &
   PSW3 = Temp                                   ' store in PSW3 variable
RETURN

' -----[ Subroutine - DeviceStart ]----------------------------------------
DeviceStart:                                     ' I2C start bit sequence
  INPUT IO
  INPUT CLK
  LOW   IO
RETURN

' -----[ Subroutine - DeviceStop ]-----------------------------------------
DeviceStop:                                      ' I2C stop bit sequence
  LOW   IO
  INPUT CLK
  INPUT IO
RETURN

' -----[ Subroutine - Send_Command ]---------------------------------------
Send_Command:                                    ' Send command format to smart card
  GOSUB DeviceStart
  SHIFTOUT IO, CLK, LSBFIRST, [Command\8, Address\8, Dat\8]
  GOSUB DeviceStop
RETURN

' -----[ Subroutine - Receive_Byte ]---------------------------------------
Recieve_Byte:                                    ' I2C receive data from smart card
  SHIFTIN IO, CLK, LSBPRE, [Temp\8]
RETURN

' -----[ Subroutine - Process ]--------------------------------------------
Process:
  DO
    HIGH CLK
    LOW  CLK
  LOOP UNTIL(IO = 1)
RETURN

' -----[ Subroutine - Reset ]----------------------------------------------
' Resets internal address counter to zero
Reset:
  LOW   CLK
  HIGH  RST
  HIGH  CLK
  LOW   CLK
  LOW   RST
  FOR index = 0 TO 3
    GOSUB Recieve_Byte
  NEXT
RETURN
Logged

Global Moderator
Netherlands
Offline Offline
Shannon Member
*****
Karma: 212
Posts: 13531
In theory there is no difference between theory and practice, however in practice there are many...
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
I bougth a smartcardreader and some cards, but i dont know how to program it.
Do you have a datasheet / url ?

Logged

Rob Tillaart

Nederlandse sectie - http://arduino.cc/forum/index.php/board,77.0.html -
(Please do not PM for private consultancy)

Massachusetts, USA
Offline Offline
Tesla Member
***
Karma: 201
Posts: 8699
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

This is what the 'Reset' subroutine would look like re-written for Arduino.   Perhaps you can do the rest of the translation for yourself.

Code:
#define IO_PIN 2     // IO                 PIN    3                   ' Serial Data IO
#define CD_PIN 3    //CD                 PIN    2                   ' Card Detect
#define CLK_PIN 4  //CLK                PIN    1                   ' Serial Data Clock
#define RST_PIN 5  //RST                PIN    0                   ' Reset


void Reset()   // Reset:
    {
    digitalWrite(CLK_PIN, LOW);     //  LOW   CLK
    digitalWrite(RST_PIN, HIGH);     //  HIGH  RST
    digitalWrite(CLK_PIN, HIGH);     //   HIGH  CLK
    digitalWrite(CLK_PIN, LOW);     //    LOW   CLK
    digitalWrite(RST_PIN, LOW);     //    LOW   RST
    for (int index = 0; index < 4; index++)  // FOR index = 0 TO 3
        {
        Receive_Byte();    // GOSUB Recieve_Byte
        }  // NEXT
     }    // RETURN
Logged

Send Bitcoin tips to: 1L3CTDoTgrXNA5WyF77uWqt4gUdye9mezN
Send Litecoin tips to : LVtpaq6JgJAZwvnVq3ftVeHafWkcpmuR1e

Pages: [1]   Go Up
Jump to: