tom
- 2nd April 2006, 10:53
Hello PIC friends!
I have Compact Flash modul from Mikroelektronika connected to PIC 16F877 with 20MHz xtal.
I use 64 Mb Lexar's CF card. I want to read four bytes from the begining of boot sector
with following code but I get wrong data. The first three bytes should be $EB, $??, $90
or $E9, $??, $?? (BS_jmpBoot) - when I read card with card reader and Winhex I get
$EB, $3E, $90 but with my code I don't get this values. Any ideas?
Thanks!
define LOADER_USED 1
define OSC 20
define DEBUG_REG PORTC
define DEBUG_BIT 6
define DEBUG_BAUD 9600
define DEBUG_MODE 0
symbol CF_DATA = PORTD
data_hi var byte
data_lo var byte
read_1 var byte
read_2 var byte
'------------------------------registers--------------------------
DATA_REG con $00
ERROR_REG con $01
FEATURES_REG con $01
SEC_CNT_REG con $02
SEC_NUM_REG con $03
CYL_LO_REG con $04
CYL_HI_REG con $05
HEAD_REG con $06
STATUS_REG con $07
COMMAND_REG con $07
'----------------------------------------------------------------
'----------------------------pins--------------------------------
symbol oe = PORTB.5
symbol WE = PORTB.6
symbol A0 = PORTB.0
symbol A1 = PORTB.1
symbol A2 = PORTB.2
symbol CD1 = PORTB.4
symbol RDY_BSY=PORTB.7
'-----------------------------commands----------------------------
IDENTIFY con $EC
WRITE_SEC con $30
READ_SEC con $20
'-----------------------------------------------------------------
TRISB=%10010000 'RB4(-CD1) and RB7(RDY/BSY) input pins
TRISD=$00 'DATA port
ADCON1=7
ADCON0=0
Main:
pause 5000
low PORTC.0
OE=1 : WE=1
A0=0 : A1=0 : A2=0
CHK_CARD:
if CD1=1 then CHK_CARD
high PORTC.0
debug 66
pause 5000
debug 65
'----------------------------reading sector 0--------------------------
CF_DATA=$E0 '1110 0000
A2=1 : A1=1 : A0=0 'address 6, LBA 27-24
gosub CF_WRITE
CF_DATA=$00
A2=1 : A1=0 : A0=1 'address 5, LBA 16-23
gosub CF_WRITE
CF_DATA=$00
A2=1 : A1=0 : A0=0 'address 4, LBA 15-8
gosub CF_WRITE
CF_DATA=$00
A2=0 : A1=1 : A0=1 'address 3, sector number, LBA 7-0
gosub CF_WRITE
CF_DATA=$01
A2=0 : A1=1 : A0=0 'address 2, sector count register
gosub CF_WRITE
CF_DATA=READ_SEC
A2=1 : A1=1 : A0=1 'address 7, command registar
gosub CF_WRITE
A2=0 : A1=0 : A0=0 'data registar
gosub CF_READ
read_1=data_hi
read_2=data_lo
debug read_1,read_2
A2=0 : A1=0 : A0=0 'data registar
gosub CF_READ
read_1=data_hi
read_2=data_lo
debug read_1,read_2
CF_WRITE:
gosub CHECK_READY
low PORTC.0
@ nop
WE=0
@ nop
@ nop
@ nop
WE=1
@ nop
return
CF_READ:
gosub CHECK_READY
TRISD=%11111111 'DATA port is now input
@ nop
OE=0
@ nop
@ nop
@ nop
data_lo=CF_DATA
@ nop
OE=1
@ nop
@ nop
gosub CHECK_READY
@ nop
OE=0
@ nop
@ nop
@ nop
data_hi=CF_DATA
@ nop
OE=1
@ nop
@ nop
TRISD=%00000000
return
CHECK_READY:
cekaj:
if RDY_BSY=0 then cekaj
return
end
I have Compact Flash modul from Mikroelektronika connected to PIC 16F877 with 20MHz xtal.
I use 64 Mb Lexar's CF card. I want to read four bytes from the begining of boot sector
with following code but I get wrong data. The first three bytes should be $EB, $??, $90
or $E9, $??, $?? (BS_jmpBoot) - when I read card with card reader and Winhex I get
$EB, $3E, $90 but with my code I don't get this values. Any ideas?
Thanks!
define LOADER_USED 1
define OSC 20
define DEBUG_REG PORTC
define DEBUG_BIT 6
define DEBUG_BAUD 9600
define DEBUG_MODE 0
symbol CF_DATA = PORTD
data_hi var byte
data_lo var byte
read_1 var byte
read_2 var byte
'------------------------------registers--------------------------
DATA_REG con $00
ERROR_REG con $01
FEATURES_REG con $01
SEC_CNT_REG con $02
SEC_NUM_REG con $03
CYL_LO_REG con $04
CYL_HI_REG con $05
HEAD_REG con $06
STATUS_REG con $07
COMMAND_REG con $07
'----------------------------------------------------------------
'----------------------------pins--------------------------------
symbol oe = PORTB.5
symbol WE = PORTB.6
symbol A0 = PORTB.0
symbol A1 = PORTB.1
symbol A2 = PORTB.2
symbol CD1 = PORTB.4
symbol RDY_BSY=PORTB.7
'-----------------------------commands----------------------------
IDENTIFY con $EC
WRITE_SEC con $30
READ_SEC con $20
'-----------------------------------------------------------------
TRISB=%10010000 'RB4(-CD1) and RB7(RDY/BSY) input pins
TRISD=$00 'DATA port
ADCON1=7
ADCON0=0
Main:
pause 5000
low PORTC.0
OE=1 : WE=1
A0=0 : A1=0 : A2=0
CHK_CARD:
if CD1=1 then CHK_CARD
high PORTC.0
debug 66
pause 5000
debug 65
'----------------------------reading sector 0--------------------------
CF_DATA=$E0 '1110 0000
A2=1 : A1=1 : A0=0 'address 6, LBA 27-24
gosub CF_WRITE
CF_DATA=$00
A2=1 : A1=0 : A0=1 'address 5, LBA 16-23
gosub CF_WRITE
CF_DATA=$00
A2=1 : A1=0 : A0=0 'address 4, LBA 15-8
gosub CF_WRITE
CF_DATA=$00
A2=0 : A1=1 : A0=1 'address 3, sector number, LBA 7-0
gosub CF_WRITE
CF_DATA=$01
A2=0 : A1=1 : A0=0 'address 2, sector count register
gosub CF_WRITE
CF_DATA=READ_SEC
A2=1 : A1=1 : A0=1 'address 7, command registar
gosub CF_WRITE
A2=0 : A1=0 : A0=0 'data registar
gosub CF_READ
read_1=data_hi
read_2=data_lo
debug read_1,read_2
A2=0 : A1=0 : A0=0 'data registar
gosub CF_READ
read_1=data_hi
read_2=data_lo
debug read_1,read_2
CF_WRITE:
gosub CHECK_READY
low PORTC.0
@ nop
WE=0
@ nop
@ nop
@ nop
WE=1
@ nop
return
CF_READ:
gosub CHECK_READY
TRISD=%11111111 'DATA port is now input
@ nop
OE=0
@ nop
@ nop
@ nop
data_lo=CF_DATA
@ nop
OE=1
@ nop
@ nop
gosub CHECK_READY
@ nop
OE=0
@ nop
@ nop
@ nop
data_hi=CF_DATA
@ nop
OE=1
@ nop
@ nop
TRISD=%00000000
return
CHECK_READY:
cekaj:
if RDY_BSY=0 then cekaj
return
end