PDA

View Full Version : Problems with CF card!



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

Ruben Pena
- 3rd April 2006, 18:04
Tom:
May be your card has a partition in Sector 0. If so, at the address $01C6 is a value of the sector where the Boot sector is.This value multiply by $0200, gives you the offset address where the "EB 90.." are located.
The PC and Winhex make this offset when you are reading the "logical " drive.
Greetings...
Ruben de la Pena

tom
- 4th April 2006, 16:17
Thanks for reply Ruben
but I still don't understand something.
you are talking about partition table which starts at 446. On offset 8 from 446 which is
$01C6 that you mentioned it should be value (4 bytes) of relative offset to partition
in sectors(LBA) but on that address I have four byte's value: 64 69 61 2E what is
2E 61 69 64 due to little endian system what is 778 135 908 decimal?!

Am I missing something?

Ruben Pena
- 4th April 2006, 17:31
Tom:
You know that the first byte in the sector 0 MUST be a $EB. OK ?.
Well... if you read the sector 0 ,address 0 and do not found the $EB
then you read the addresses $01C7 (high byte) and $01C6 (low byte).
If the value in those addresses is $0040 then you multiply that value by
$0200 = $8000. That is the absolute address where the Boot sector is located, (Sector $40, in that case.).
Then you can read in that sector , verify the byte 0 as the $EB and read
all needed parameters, keeping in mind that your card has a offset of
$8000 bytes.
I hope that helps...
Ruben de la Pena

tom
- 5th April 2006, 15:37
It works!
My boot sector starts at LBA=32 because my card has 32 hidden sectors!
Well, I have first step on thousand miles trip!
Ruben, Thanks again for help!

charudatt
- 15th November 2006, 06:33
Hello TOM,

I am also using the same setup, MikroElectronika EasyPIC3 Board and their CF adpater Module and I am looking for some help in reading and writing using PBP.

I would try your code on this board, but if there's a problem , can you help me.

Thank you in advance.

regards

Charudatt

mister_e
- 15th November 2006, 14:03
I'll receive EasyPIC4 in few days, if there's few problem i could also give a try.

charudatt
- 16th November 2006, 06:48
Great,

Looking forward to that.

Wonder what's the difference between EasyPIC3 and EasyPIC4.

Overall nice product for a good price.

regards