CCS C Software and Maintenance Offers
FAQFAQ   FAQForum Help   FAQOfficial CCS Support   SearchSearch  RegisterRegister 

ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

CCS does not monitor this forum on a regular basis.

Please do not post bug reports on this forum. Send them to CCS Technical Support

SD Card / FAT offset question

 
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion
View previous topic :: View next topic  
Author Message
matt1971



Joined: 26 Oct 2008
Posts: 15

View user's profile Send private message

SD Card / FAT offset question
PostPosted: Fri Oct 31, 2008 8:40 pm     Reply with quote

I have an SD card, formatted FAT16 on a PC. I'm just doing some tests on read/writes to it and one of the tests is to find the volume name on the card.

When I search for the volume name ('MYDATA') on the PC with winhex, it comes back at offset 249856. Yet when I sequentially search the SD card on the pic, it comes back at offset 377344.

Should the two offsets be the same, or am I missing something?
asmallri



Joined: 12 Aug 2004
Posts: 1634
Location: Perth, Australia

View user's profile Send private message Send e-mail Visit poster's website

PostPosted: Fri Oct 31, 2008 9:39 pm     Reply with quote

The keyword here is offset. Offset with respect to what?

The PIC may well be offset from physical sector 0 (i.e the real sector) and the other could be an offset from the partition or from the end of the directory structure.
_________________
Regards, Andrew

http://www.brushelectronics.com/software
Home of Ethernet, SD card and Encrypted Serial Bootloaders for PICs!!
matt1971



Joined: 26 Oct 2008
Posts: 15

View user's profile Send private message

PostPosted: Sat Nov 01, 2008 4:49 am     Reply with quote

Well, if I read from the SD card using mmcsd_read_data, my offset is an int32 based value, starting at 0. When I use this function and read the first 5 bytes I get :

EB 58 90 4D 53 44 4F 53 35

When I look at the first 5 bytes in hexedit, I get :

EB 3C 90 4D 53 44 4F 53 35

So I assume the "offset" in both cases is the same, since the data is more or less the same. But doing this experiment shows that the values I read are not exactly the same, which points to some sort of problem with mmcsd.c.

My test program is :

Code:

#include <18F4685.h>
#fuses NOWDT, HS, NOPROTECT
#use delay(clock=20M)

#use rs232(baud=9600, UART1)

//media library, a compatable media library is required for FAT.
#use fast_io(c)
#define MMCSD_PIN_SCL     PIN_C3 //o
#define MMCSD_PIN_SDI     PIN_C4 //i org PIN_C4
#define MMCSD_PIN_SDO     PIN_C5 //o org PIN_C5
#define MMCSD_PIN_SELECT  PIN_C2 //o  org PIN_C2
#include <mmcsd.c>

#include "lcd_driver.h"

void main(void)
{
   int32 i = 0;
   char data;

   // Initialise LCD
   lcd_init();
   
   // Initialise the SD lib
   mmcsd_init();
 
   printf(lcd_putc, "Data : ");
   for (i=0; i < 10; i++) {
      mmcsd_read_byte(i, &data);
      printf(lcd_putc, "%X,", data);
   }   

   while(1);
}


This is for a PIC18F4685, CCS 4.057. I know my compiler is quite old now, have there been significant changes to mmcsd.c between .057 and .082 ?
crystal_lattice



Joined: 13 Jun 2006
Posts: 164

View user's profile Send private message

PostPosted: Sat Nov 01, 2008 6:57 am     Reply with quote

The PIC will tell you the the physical address but Winhex will give you the logical address (not very logical... i know Confused ) I use a program called FlexHEX www.heaventools.com it has various options/modes in which you can view/load a drive/partition.
asmallri



Joined: 12 Aug 2004
Posts: 1634
Location: Perth, Australia

View user's profile Send private message Send e-mail Visit poster's website

PostPosted: Sat Nov 01, 2008 11:05 am     Reply with quote

crystal_lattice wrote:
The PIC will tell you the the physical address but win hex will give you the logical address (not very logical... i know Confused ) i use a program called flex


This is what I was trying to tell Matt - obviously not as clearly as you did.
_________________
Regards, Andrew

http://www.brushelectronics.com/software
Home of Ethernet, SD card and Encrypted Serial Bootloaders for PICs!!
matt1971



Joined: 26 Oct 2008
Posts: 15

View user's profile Send private message

PostPosted: Sat Nov 01, 2008 2:18 pm     Reply with quote

Thanks for both your help! I'll investigate further. I still dont think the mmcsd.c driver is working properly though.
ckielstra



Joined: 18 Mar 2004
Posts: 3680
Location: The Netherlands

View user's profile Send private message

PostPosted: Sat Nov 01, 2008 8:10 pm     Reply with quote

I never tested the mmcsd.c driver, but in v4.057 it contains a bug when it configures the SPI bus for mode 2 while the SD cards only support mode 0 or 3.

Change in mmcsd.c
Code:
#use spi(MASTER, DI=MMCSD_PIN_SDI, DO=MMCSD_PIN_SDO, CLK=MMCSD_PIN_SCL, BITS=8, MSB_FIRST, IDLE=1, stream=mmcsd_spi)
to
Code:
#use spi(MASTER, DI=MMCSD_PIN_SDI, DO=MMCSD_PIN_SDO, CLK=MMCSD_PIN_SCL, mode=0, stream=mmcsd_spi, FORCE_HW)
matt1971



Joined: 26 Oct 2008
Posts: 15

View user's profile Send private message

PostPosted: Sun Nov 02, 2008 6:26 am     Reply with quote

I've tried a number of different #USE_SPI settings and none seem to alter that I dont read the correct values from the SD Card. the "FORCE_HW" isnt supported in 4.057

I'm guessing the only way forward on this is to buy a maintenance upgrade to the latest compiler ?
matt1971



Joined: 26 Oct 2008
Posts: 15

View user's profile Send private message

PostPosted: Sun Nov 02, 2008 6:46 am     Reply with quote

Having done further tests on this using flexhex, it seems that mmcd.c library looks only at the "Physical" layer (obvious really), and that the "real" boot sector needs to be read from the "logical layer"

So the obvious question now is how to read the logical layer, and not the physical one?

What ultimately concerns me though, is that I'm trying to read/write a FAT drive. Looking at the fat.c driver, it's using the same mmcd.c byte read functions I am, so the fat_init function will NEVER get the correct values it needs. This may explain why so many folds are having problems with the fat.c driver ?
Display posts from previous:   
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion All times are GMT - 6 Hours
Page 1 of 1

 
Jump to:  
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