|
|
View previous topic :: View next topic |
Author |
Message |
kumar
Joined: 16 Mar 2006 Posts: 4
|
Problem access 24lc256 from PIC 16F877 card (Silvercard) |
Posted: Thu Mar 16, 2006 3:29 pm |
|
|
I have assembled Silvercard (PIC 16F877 + 24LC256) but I'm unable to read/write to external EEPROM 24LC256. For silvercard schematic please check the site http://www.starwafer.com/pic16f877+24lc256eng.htm. The SDA of I2C is connected to RB4 & SCL of I2C is connected to RB5. I tried using the CCS driver 24256.c and changed the SDA and SCL to RB4 & RB5 of PIC respectively and no luck.
Can any one help me by providing sample code for I2C access for Silvercard. I'm desperately looking for solution. I'm using PCWH Compiler 3.245
:cry: |
|
|
PCM programmer
Joined: 06 Sep 2003 Posts: 21708
|
|
Posted: Thu Mar 16, 2006 4:10 pm |
|
|
1. Where are the pull-up resistors on that schematic ?
For i2c you need 4.7K pullups on the SDA and SCL lines.
2. Why do they call a PIC in a 28-pin package a "16F877" ?
It would be a 16F876. |
|
|
kumar
Joined: 16 Mar 2006 Posts: 4
|
|
Posted: Thu Mar 16, 2006 9:31 pm |
|
|
You are right it should be 16F876, but I have mapped the 28 PIN into 40 PIN 16F877 and I'm able to load and test the application on the PIC. The only problem is with I2C interface to 24LC256. I have also connected the SDA, SCL lines of I2C to PIN 37 & 38 of PIC respectively.
I'm newbie to PIC and I checked may sites for the silvercard schematic and nowhere they have connected Pullup resistors. Is it understood that Pull up resistors are required?.
Please send a sample code to write to 24LC256. |
|
|
Mark
Joined: 07 Sep 2003 Posts: 2838 Location: Atlanta, GA
|
|
Posted: Thu Mar 16, 2006 10:21 pm |
|
|
Are you trying to emulate what a card is?
PCM,
I have done this before with a 16F84A. The schematics show the eeproms on portb so he shouldn't have a problem if the pullups are turned on. |
|
|
kumar
Joined: 16 Mar 2006 Posts: 4
|
|
Posted: Fri Mar 17, 2006 10:31 am |
|
|
I'm trying to emulate a ISO 7816 Smart card. I'm able to program into the card and partly implemented ISO 7816 T=0 protocol functionality. I'm unable to create a filesystem due to I2C problem. |
|
|
Mark
Joined: 07 Sep 2003 Posts: 2838 Location: Atlanta, GA
|
|
Posted: Fri Mar 17, 2006 12:11 pm |
|
|
I see no code posted so we can comment on what else may be the problem. |
|
|
kumar
Joined: 16 Mar 2006 Posts: 4
|
|
Posted: Fri Mar 17, 2006 12:24 pm |
|
|
Source is here
----------------
I'm able to get ATR on inserting the card in the Smart Card reader. But when I try to call MF file creation CLA INS, TIMEOUT happens in read/write I2C EEPROM routing.
Processor.h
-------------
#include <16F877.h>
#fuses XT,NOWDT,NOPROTECT,NOLVP,NOBROWNOUT
#use delay(clock=3579545)
#byte W = 0x0000
#byte F = 0x0001
#byte PORTA = 0x05
#byte PORTB = 0x06
#byte PCLATH = 0x0A
#byte STATUS = 0x03
#byte RP0 = 0x05
#byte TRISA = 0x85
byte CONST C = 0x0000;
PCOS.H
---------
#include ".\16F877\processor.h"
#include <stdlib.h>
#include ".\HAL\IO\IO.c"
#include ".\HAL\IO\EEPROM.H"
#include ".\Commands\CommandProcessor.h"
CommandProcessor.h
-------------------------
#ifndef __COMMAND_PROCESSOR_H
#define __COMMAND_PROCESSOR_H
#include ".\Commands\Commands.h"
void processCommand(int CLA,int INS,int P1,int P2,int P3)
{
if(CLA == 0x00 && INS == 0x84)
{
commandGetChallenge(CLA,INS,P1,P2,P3);
return;
}
else if(CLA == 0x80 && INS == 0xE0)
{
if(P1 == 0x3F && P2 == 0x00) // Create MF
{
commandCreateMF(CLA,INS,P1,P2,P3);
return;
}
}
else
{
sendSW1SW2(0x6E,0x00);// Invalid CLA
}
}
#endif
Commands.h
----------------
#ifndef __COMMANDS_H
#define __COMMANDS_H
#include ".\HAL\IO\EEPROM.h"
#include ".\Support.h"
// Function Prototype
void commandGetChallenge(int CLA,int INS,int P1,int P2,int P3);
void sendSW1SW2(int SW1,int SW2);
void commandGetChallenge(int CLA,int INS,int P1,int P2,int P3)
{
int i;
int iRandom[16];
if(P3 < 1 || P3 > 16)
{
sendSW1SW2(0x67, 0x00); // Incorrect length
return;
}
if(isMFExists() == false)
{
sendSW1SW2(0x6A, 0x81); // Function not supported - MF not found
return;
}
for(i=0; i < P3; i++)
{
iRandom[i] = rand();
}
//SendByte(0x84);
for(i=0; i < P3; i++)
{
SendByte(iRandom[i]);
}
sendSW1SW2(0x90,0x00); // Success
}
// Format -
// CLA
// INS
// P1
// P2
// LC
// DATA - File Type, Create file Access rights, Erase File access rights
void commandCreateMF(int CLA,int INS,int P1,int P2,int P3)
{
int DATA[5];
int i;
byte byFreeSize[2];
if(P3 != 0x02)
{
sendSW1SW2(0x67,0x00); // Wrong Length
return;
}
if(isMFExists() == true)
{
sendSW1SW2(0x6A, 0x86); // Duplicate File - MF already exists
return;
}
// Receive rest of the data
for(i=0; i < P3; i++)
{
DATA[i] = receiveByte();
}
//SendByte(0xE0);
// Create MF
// Prepare MF Header
write_ext_eeprom(1, 0x03); // SCOS Identifier
write_ext_eeprom(2, 0x3F); // File ID High
write_ext_eeprom(3, 0x00); // File ID Low
write_ext_eeprom(4, 0x00); // 1st Child DF Ptr - High Byte
write_ext_eeprom(5, 0x00); // 1st Child DF Ptr - Low Byte
write_ext_eeprom(6, 0x00); // 1st EF Ptr - High Byte
write_ext_eeprom(7, 0x00); // 1st EF Ptr - Low Byte
write_ext_eeprom(8, 0x00); // MF Status 00 - Valid , 0B-Blocked
write_ext_eeprom(9, 0x00); // MF Status 00 - Valid , 0B-Blocked
write_ext_eeprom(10, DATA[0]); // Create File Access Rights
write_ext_eeprom(11, DATA[1]); // Erase File Access Rights
write_ext_eeprom(12, 0xFF); // MF File Size - High Byte - Mx of 64 KB is supported
write_ext_eeprom(13, 0xFF); // MF File Size - Low Byte
convertInt16ToString(32768-15, byFreeSize); // Calculate Freespace - 32 KB of data storage minus Length of the Header
write_ext_eeprom(14, byFreeSize[0]); // Free Card Space - High Byte - Mx of 64 KB is supported
write_ext_eeprom(15, byFreeSize[1]); // Free Card Space - Low Byte
// Send SW1 SW2
sendSW1SW2(0x90,0x00);
}
void sendSW1SW2(int SW1,int SW2)
{
sendByte(SW1);
sendByte(SW2);
}
#endif
EEPROM.H (Problamatic Code)
-----------------------------------
// Whenever I try to call read EEPROM or Write EEPROM function, Timeout happens.
#ifndef EEPROM_SDA
#define EEPROM_SDA PIN_B4
#define EEPROM_SCL PIN_B5
#use i2c(Master,Fast,sda=PIN_B4,scl=PIN_B5)
#define EEPROM_ADDRESS long int
#define EEPROM_SIZE 32768
void init_ext_eeprom()
{
output_float(EEPROM_SCL);
output_float(EEPROM_SDA);
}
void write_ext_eeprom(long int address, BYTE data)
{
short int status;
i2c_start();
i2c_write(0xa0);
i2c_write(address>>8);
i2c_write(address);
i2c_write(data);
i2c_stop();
i2c_start();
status=i2c_write(0xa0);
while(status==1)
{
i2c_start();
status=i2c_write(0xa0);
}
}
BYTE read_ext_eeprom(long int address) {
BYTE data;
i2c_start();
i2c_write(0xa0);
i2c_write(address>>8);
i2c_write(address);
i2c_start();
i2c_write(0xa1);
data=i2c_read(0);
i2c_stop();
return(data);
}
#endif
Support.h
------------
#ifndef __SUPPORT_H
#define _SUPPORT_H
boolean isMFExists()
{
if(read_ext_eeprom(1) == 0x03)
{
return true;
}
return false;
}
void convertInt16ToString (unsigned long iValue,char *pszString)
{
BYTE *byData;
byData = (BYTE *) &iValue;
memcpy(pszString,byData,2);
pszString[2] = '\0';
}
long int convertStringToInt16(char *pszString)
{
return *((unsigned long *)pszString);
}
#endif
IO.H (Serial IO Routine)
----------------------------
#include "IO.h"
void sendByte(int iSendData)
{
int iParity;
int iCtr;
int iPauseCtr;
#asm
movlw 0x32 ; Pause for 170 µs
call Pause ; call Pause
bcf PORTB,7 ; Send start bit in RB7
movlw 0x7F
tris PORTB ; TRIS PORB with 01111111 (0x7F)
clrf iParity ; Initialize Parity to 0
movlw 0x08
movwf iCtr ; Initialize 8 to counter to send 8 data bits
send_loop:
call Std_pause ; Pause for 93 µs (at 9600 bps)
rrf iSendData,f
rrf iSendData,W
andlw 0x80
xorwf iParity,f ; Calculate Parity
xorwf PORTB,W
xorwf PORTB,f
decfsz iCtr,f ; Decrement Data Bit counter
goto send_loop ; Loop
call Std_pause ; Pause
movf iParity,W ; Send Parity
xorwf PORTB,W
xorwf PORTB,f
call Std_pause
movlw 0xFF
tris PORTB
movlw 0x3E
goto Pause ; Pause 0x3E
Std_pause:
movlw 0x1A ; Standard Pause Value
Pause:
movwf iPauseCtr ; Initialize iPauseCounter with value of W Register
Pause_loop:
decfsz iPauseCtr,f ; Decrement Pause Counter
goto Pause_loop
retlw 0x00 ; Return after setting W Register to 0
#endasm
}
int receiveByte()
{
int iCtr;
int iPauseCtr;
int iReceiveData;
#asm
Receive:
btfsc PORTB,7
goto Receive
movlw 0x2D
call Pause
movlw 0x09
movwf iCtr
ReceiveLoop:
bcf STATUS,C
btfsc PORTB,7
bsf STATUS,C
rrf iReceiveData,f
call Std_pause
nop
nop
decfsz iCtr,f
goto ReceiveLoop
rlf iReceiveData,f
movlw 0x1D
call Pause
movf iReceiveData,W
#endasm
return iReceiveData;
#asm
Std_pause:
movlw 0x1A
Pause:
movwf iPauseCtr
Pause_loop:
decfsz iPauseCtr,f
goto Pause_loop
retlw 0x00
#endasm
}
MAIN SOURCE - PCOS.C
-----------------------------
include "PCOS.h"
void initialize()
{
init_ext_eeprom();
}
void main()
{
int CLA;
int INS;
int P1;
int P2;
int P3;
initialize();
// Send ATR 3B 69 00 00 26 02 04 A1 84 21 12 13 0A
sendByte(0x3B);
sendByte(0x69);
sendByte(0x00);
sendByte(0x00);
sendByte(0x26);
sendByte(0x02);
sendByte(0x04);
sendByte(0xA1);
sendByte(0x84);
sendByte(0x21);
sendByte(0x12);
sendByte(0x13);
sendByte(0x0B);
sendByte(0x90);
sendByte(0x00);
while(1)
{
CLA = receiveByte();
INS = receiveByte();
P1 = receiveByte();
P2 = receiveByte();
P3 = receiveByte();
processCommand(CLA,INS,P1,P2,P3);
}
} |
|
|
kumar
Joined: 16 Mar 2006 Posts: 4
|
|
Posted: Fri Mar 17, 2006 12:27 pm |
|
|
Source is here
----------------
I'm able to get ATR on inserting the card in the Smart Card reader. But when I try to call MF file creation CLA INS, TIMEOUT happens in read/write I2C EEPROM routing.
Processor.h
-------------
#include <16F877.h>
#fuses XT,NOWDT,NOPROTECT,NOLVP,NOBROWNOUT
#use delay(clock=3579545)
#byte W = 0x0000
#byte F = 0x0001
#byte PORTA = 0x05
#byte PORTB = 0x06
#byte PCLATH = 0x0A
#byte STATUS = 0x03
#byte RP0 = 0x05
#byte TRISA = 0x85
byte CONST C = 0x0000;
PCOS.H
---------
#include ".\16F877\processor.h"
#include <stdlib.h>
#include ".\HAL\IO\IO.c"
#include ".\HAL\IO\EEPROM.H"
#include ".\Commands\CommandProcessor.h"
CommandProcessor.h
-------------------------
#ifndef __COMMAND_PROCESSOR_H
#define __COMMAND_PROCESSOR_H
#include ".\Commands\Commands.h"
void processCommand(int CLA,int INS,int P1,int P2,int P3)
{
if(CLA == 0x00 && INS == 0x84)
{
commandGetChallenge(CLA,INS,P1,P2,P3);
return;
}
else if(CLA == 0x80 && INS == 0xE0)
{
if(P1 == 0x3F && P2 == 0x00) // Create MF
{
commandCreateMF(CLA,INS,P1,P2,P3);
return;
}
}
else
{
sendSW1SW2(0x6E,0x00);// Invalid CLA
}
}
#endif
Commands.h
----------------
#ifndef __COMMANDS_H
#define __COMMANDS_H
#include ".\HAL\IO\EEPROM.h"
#include ".\Support.h"
// Function Prototype
void commandGetChallenge(int CLA,int INS,int P1,int P2,int P3);
void sendSW1SW2(int SW1,int SW2);
void commandGetChallenge(int CLA,int INS,int P1,int P2,int P3)
{
int i;
int iRandom[16];
if(P3 < 1 || P3 > 16)
{
sendSW1SW2(0x67, 0x00); // Incorrect length
return;
}
if(isMFExists() == false) // PROBLEM HERE
{
sendSW1SW2(0x6A, 0x81); // Function not supported - MF not found
return;
}
for(i=0; i < P3; i++)
{
iRandom[i] = rand();
}
//SendByte(0x84);
for(i=0; i < P3; i++)
{
SendByte(iRandom[i]);
}
sendSW1SW2(0x90,0x00); // Success
}
// Format -
// CLA
// INS
// P1
// P2
// LC
// DATA - File Type, Create file Access rights, Erase File access rights
void commandCreateMF(int CLA,int INS,int P1,int P2,int P3)
{
int DATA[5];
int i;
byte byFreeSize[2];
if(P3 != 0x02)
{
sendSW1SW2(0x67,0x00); // Wrong Length
return;
}
if(isMFExists() == true)
{
sendSW1SW2(0x6A, 0x86); // Duplicate File - MF already exists
return;
}
// Receive rest of the data
for(i=0; i < P3; i++)
{
DATA[i] = receiveByte();
}
//SendByte(0xE0);
// Create MF
// Prepare MF Header - PROBLEM IN WRITE_EXT_EEPROM calls
write_ext_eeprom(1, 0x03); // SCOS Identifier
write_ext_eeprom(2, 0x3F); // File ID High
write_ext_eeprom(3, 0x00); // File ID Low
write_ext_eeprom(4, 0x00); // 1st Child DF Ptr - High Byte
write_ext_eeprom(5, 0x00); // 1st Child DF Ptr - Low Byte
write_ext_eeprom(6, 0x00); // 1st EF Ptr - High Byte
write_ext_eeprom(7, 0x00); // 1st EF Ptr - Low Byte
write_ext_eeprom(8, 0x00); // MF Status 00 - Valid , 0B-Blocked
write_ext_eeprom(9, 0x00); // MF Status 00 - Valid , 0B-Blocked
write_ext_eeprom(10, DATA[0]); // Create File Access Rights
write_ext_eeprom(11, DATA[1]); // Erase File Access Rights
write_ext_eeprom(12, 0xFF); // MF File Size - High Byte - Mx of 64 KB is supported
write_ext_eeprom(13, 0xFF); // MF File Size - Low Byte
convertInt16ToString(32768-15, byFreeSize); // Calculate Freespace - 32 KB of data storage minus Length of the Header
write_ext_eeprom(14, byFreeSize[0]); // Free Card Space - High Byte - Mx of 64 KB is supported
write_ext_eeprom(15, byFreeSize[1]); // Free Card Space - Low Byte
// Send SW1 SW2
sendSW1SW2(0x90,0x00);
}
void sendSW1SW2(int SW1,int SW2)
{
sendByte(SW1);
sendByte(SW2);
}
#endif
EEPROM.H (Problamatic Code)
-----------------------------------
// Whenever I try to call read EEPROM or Write EEPROM function, Timeout happens.
#ifndef EEPROM_SDA
#define EEPROM_SDA PIN_B4
#define EEPROM_SCL PIN_B5
#use i2c(Master,Fast,sda=PIN_B4,scl=PIN_B5)
#define EEPROM_ADDRESS long int
#define EEPROM_SIZE 32768
void init_ext_eeprom()
{
output_float(EEPROM_SCL);
output_float(EEPROM_SDA);
}
void write_ext_eeprom(long int address, BYTE data)
{
short int status;
i2c_start();
i2c_write(0xa0);
i2c_write(address>>8);
i2c_write(address);
i2c_write(data);
i2c_stop();
i2c_start();
status=i2c_write(0xa0);
while(status==1)
{
i2c_start();
status=i2c_write(0xa0);
}
}
BYTE read_ext_eeprom(long int address) {
BYTE data;
i2c_start();
i2c_write(0xa0);
i2c_write(address>>8);
i2c_write(address);
i2c_start();
i2c_write(0xa1);
data=i2c_read(0);
i2c_stop();
return(data);
}
#endif
Support.h
------------
#ifndef __SUPPORT_H
#define _SUPPORT_H
boolean isMFExists()
{
if(read_ext_eeprom(1) == 0x03)
{
return true;
}
return false;
}
void convertInt16ToString (unsigned long iValue,char *pszString)
{
BYTE *byData;
byData = (BYTE *) &iValue;
memcpy(pszString,byData,2);
pszString[2] = '\0';
}
long int convertStringToInt16(char *pszString)
{
return *((unsigned long *)pszString);
}
#endif
IO.H (Serial IO Routine)
----------------------------
#include "IO.h"
void sendByte(int iSendData)
{
int iParity;
int iCtr;
int iPauseCtr;
#asm
movlw 0x32 ; Pause for 170 µs
call Pause ; call Pause
bcf PORTB,7 ; Send start bit in RB7
movlw 0x7F
tris PORTB ; TRIS PORB with 01111111 (0x7F)
clrf iParity ; Initialize Parity to 0
movlw 0x08
movwf iCtr ; Initialize 8 to counter to send 8 data bits
send_loop:
call Std_pause ; Pause for 93 µs (at 9600 bps)
rrf iSendData,f
rrf iSendData,W
andlw 0x80
xorwf iParity,f ; Calculate Parity
xorwf PORTB,W
xorwf PORTB,f
decfsz iCtr,f ; Decrement Data Bit counter
goto send_loop ; Loop
call Std_pause ; Pause
movf iParity,W ; Send Parity
xorwf PORTB,W
xorwf PORTB,f
call Std_pause
movlw 0xFF
tris PORTB
movlw 0x3E
goto Pause ; Pause 0x3E
Std_pause:
movlw 0x1A ; Standard Pause Value
Pause:
movwf iPauseCtr ; Initialize iPauseCounter with value of W Register
Pause_loop:
decfsz iPauseCtr,f ; Decrement Pause Counter
goto Pause_loop
retlw 0x00 ; Return after setting W Register to 0
#endasm
}
int receiveByte()
{
int iCtr;
int iPauseCtr;
int iReceiveData;
#asm
Receive:
btfsc PORTB,7
goto Receive
movlw 0x2D
call Pause
movlw 0x09
movwf iCtr
ReceiveLoop:
bcf STATUS,C
btfsc PORTB,7
bsf STATUS,C
rrf iReceiveData,f
call Std_pause
nop
nop
decfsz iCtr,f
goto ReceiveLoop
rlf iReceiveData,f
movlw 0x1D
call Pause
movf iReceiveData,W
#endasm
return iReceiveData;
#asm
Std_pause:
movlw 0x1A
Pause:
movwf iPauseCtr
Pause_loop:
decfsz iPauseCtr,f
goto Pause_loop
retlw 0x00
#endasm
}
MAIN SOURCE - PCOS.C
-----------------------------
include "PCOS.h"
void initialize()
{
init_ext_eeprom();
}
void main()
{
int CLA;
int INS;
int P1;
int P2;
int P3;
initialize();
// Send ATR 3B 69 00 00 26 02 04 A1 84 21 12 13 0A
sendByte(0x3B);
sendByte(0x69);
sendByte(0x00);
sendByte(0x00);
sendByte(0x26);
sendByte(0x02);
sendByte(0x04);
sendByte(0xA1);
sendByte(0x84);
sendByte(0x21);
sendByte(0x12);
sendByte(0x13);
sendByte(0x0B);
sendByte(0x90);
sendByte(0x00);
while(1)
{
CLA = receiveByte();
INS = receiveByte();
P1 = receiveByte();
P2 = receiveByte();
P3 = receiveByte();
processCommand(CLA,INS,P1,P2,P3);
}
} |
|
|
|
|
You cannot post new topics in this forum You cannot reply to topics in this forum You cannot edit your posts in this forum You cannot delete your posts in this forum You cannot vote in polls in this forum
|
Powered by phpBB © 2001, 2005 phpBB Group
|