Problems with CF card!


Closed Thread
Results 1 to 8 of 8
  1. #1
    Join Date
    Oct 2004
    Location
    Zagreb, Croatia
    Posts
    27

    Default Problems with CF card!

    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!
    Code:
    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

  2. #2
    Join Date
    Aug 2004
    Posts
    64


    Did you find this post helpful? Yes | No

    Default Cf Read

    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

  3. #3
    Join Date
    Oct 2004
    Location
    Zagreb, Croatia
    Posts
    27


    Did you find this post helpful? Yes | No

    Question Still confused

    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?

  4. #4
    Join Date
    Aug 2004
    Posts
    64


    Did you find this post helpful? Yes | No

    Default Cf Read

    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

  5. #5
    Join Date
    Oct 2004
    Location
    Zagreb, Croatia
    Posts
    27


    Did you find this post helpful? Yes | No

    Smile It works!

    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!

  6. #6
    Join Date
    Sep 2003
    Location
    INDIA
    Posts
    161


    Did you find this post helpful? Yes | No

    Default Thank U.

    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

  7. #7
    Join Date
    Sep 2004
    Location
    montreal, canada
    Posts
    6,898


    Did you find this post helpful? Yes | No

    Default

    I'll receive EasyPIC4 in few days, if there's few problem i could also give a try.
    Steve

    It's not a bug, it's a random feature.
    There's no problem, only learning opportunities.

  8. #8
    Join Date
    Sep 2003
    Location
    INDIA
    Posts
    161


    Did you find this post helpful? Yes | No

    Default

    Great,

    Looking forward to that.

    Wonder what's the difference between EasyPIC3 and EasyPIC4.

    Overall nice product for a good price.

    regards

Similar Threads

  1. TTL magnetic card reader interface ith PC
    By attabros in forum Schematics
    Replies: 3
    Last Post: - 24th February 2016, 07:04
  2. PIC plus sound card?
    By TonyA in forum mel PIC BASIC Pro
    Replies: 5
    Last Post: - 27th May 2006, 16:40
  3. Mag Card Reader
    By Melanie in forum mel PIC BASIC Pro
    Replies: 9
    Last Post: - 4th February 2006, 17:29
  4. Smart card reader with PIC16F84A
    By bangunprayogi in forum Serial
    Replies: 0
    Last Post: - 12th August 2005, 10:36
  5. Reading Magnetic Card on Pic16f876
    By Finn in forum mel PIC BASIC Pro
    Replies: 7
    Last Post: - 4th February 2004, 23:36

Members who have read this thread : 0

You do not have permission to view the list of names.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts