|
|
View previous topic :: View next topic |
Author |
Message |
ksp
Joined: 20 Jul 2007 Posts: 3
|
Complete Source Code |
Posted: Fri Jul 20, 2007 6:34 am |
|
|
Hi Miniman,
Can you please post the complete code for 18f452 and MMC.
Thanks,
KSP |
|
|
Miniman
Joined: 30 Apr 2007 Posts: 44
|
|
Posted: Mon Jul 23, 2007 2:34 am |
|
|
If you are going to use hardware SPI, then the only thing you should change is removing these lines:
Code: |
#locate FATTable = 0x0800
#locate gFiles = 0x0A00
#locate gFAT32Vars = 0x0E70
#locate DiskInfo = 0x0E90
|
if it still doesn't work, try changing this line(in MMC_Init): SSPCON1 = 0x30; to SSPCON1 = 0x32;
This will slow down the speed of the data transfer, since the MMC only allow max 400kHz upon init. after init you could change it back.
Hope it helps
(I'm working on getting this code a little bit "nicer" with better error handling and debugging and some functions for listing the files on the card (including long filenames), this code should be ready in a weak)
Last edited by Miniman on Wed Jul 25, 2007 10:28 am; edited 1 time in total |
|
|
ksp
Joined: 20 Jul 2007 Posts: 3
|
|
Posted: Tue Jul 24, 2007 7:23 am |
|
|
Hi
Is the file "my6720.h" works for PIC18F452 ? Or do i need to change any thing in "my6720.h". |
|
|
Miniman
Joined: 30 Apr 2007 Posts: 44
|
|
Posted: Tue Jul 24, 2007 12:49 pm |
|
|
ksp wrote: | Hi
Is the file "my6720.h" works for PIC18F452 ? Or do i need to change any thing in "my6720.h". |
Yes, it does work for your pic. I have just checked it, you can see for your self, compare the my6720.h with page 47 in your pic datasheet (http://ww1.microchip.com/downloads/en/DeviceDoc/39564c.pdf). |
|
|
Miniman
Joined: 30 Apr 2007 Posts: 44
|
|
|
Benjamin
Joined: 11 Jan 2006 Posts: 21 Location: Quebec (Canada)
|
|
Posted: Tue Aug 07, 2007 11:55 am |
|
|
Tomi,
There's something that I don't get in your code. You don't seem to bother about the number of sectors per cluster? (see Boot Sector variable SecPerClus) Your code is writen as if there was only one Sector per cluster. For instance, when reading a file and reaching the end of the first sector you jump to the next cluster, instead of the next sector.
What is it I'm not getting? |
|
|
Pret
Joined: 18 Jul 2006 Posts: 92 Location: Iasi, Romania
|
|
Posted: Wed Aug 08, 2007 3:56 am |
|
|
This is a constraint. There should be a single sector per cluster. This is configured in format command. Of course this should be checked in InitFat...... |
|
|
incubus22
Joined: 15 Nov 2007 Posts: 14
|
|
Posted: Wed Nov 21, 2007 8:31 pm |
|
|
Hello everyone!
How can I use the hardware SPI on my PIC18F4550? I've got Miniman's code to work on software SPI but not successfully on hardware SPI. |
|
|
info@ckmintech.com
Joined: 16 May 2006 Posts: 39
|
|
Posted: Thu Nov 22, 2007 12:03 am |
|
|
Any informatin on how to modify this code to work for SD card?
Rgds |
|
|
andreluizeng
Joined: 04 Apr 2006 Posts: 117 Location: Brasil
|
|
Posted: Mon Nov 26, 2007 4:59 am |
|
|
Hi, im trying to simulate this code on proteus VSM, but im having some problems.... the MMCInit function does not work (but i already fixed it)... and a INITFat function does not work too.
my question is:
where all those structs and variables are initizalized ?
in this piece of code
Code: | memcpy(&DiskInfo,gFiles[MAXFILES-1].IOpuffer,sizeof(DiskInfo));
|
DiskInfo struct is initialized with the data in IOPuffer, but IOpuffer are not initialized either.
Code: |
if (gFiles[MAXFILES-1].IOpuffer[0] != 0xEB)
{
gStartSectorL = gFiles[MAXFILES-1].IOpuffer[0x1C6];
gStartSectorH = gFiles[MAXFILES-1].IOpuffer[0x1C7];
gStartSectorHL = gFiles[MAXFILES-1].IOpuffer[0x1C8];
ReadSector (gFAT32Vars.gStartSector, gFiles[MAXFILES-1].IOpuffer);
}
|
the data in IOpuffer never will be 0xEB, insted it is 0xFF
when this calculus
Code: |
actsector = gFAT32Vars.gStartSector + DiskInfo.Reserved1;
|
or
Code: |
gFAT32Vars.gFirstDataSector = gFAT32Vars.gStartSector + DiskInfo.FATCopies * DiskInfo.hSectorsPerFat + DiskInfo.Reserved1 - 2;
|
is done:
we got a overflow in gFat32Vars.gFirstDataSector...
All those variables in that math have a 0xffff... value.
somebody have any sugestion ????
thanks in advance.
regards. _________________ Andre |
|
|
Tomi
Joined: 11 Aug 2005 Posts: 10
|
"bExtension" replaced by [spam] (????) |
Posted: Sat Mar 08, 2008 3:42 am |
|
|
Hi Everybody,
I don't know how but my original text "bExtension" is replaced by "[spam]"
Note that it is happened not only in the declaration part but inside of the code, too.
Of course that 3-char size array is the DOS File Extension (e.g. TXT DOC etc.) of the file.
Brg,
Tomi |
|
|
Tomi
Joined: 11 Aug 2005 Posts: 10
|
To "andreluizeng" |
Posted: Sat Mar 08, 2008 3:46 am |
|
|
Hi,
The structure is inited by a ReadSector call. So its original content is loaded from the MMC card itself (from the disk definition area) so I think emulating this part of code is not a really good idea (unless you have a full-featured MMC emulator, too).
Brg,
Tomi |
|
|
jamilsalem
Joined: 29 Sep 2008 Posts: 3 Location: Uberlandia-MG-Brasil
|
Problem using fat32 in 2GB SD/MMC |
Posted: Mon Sep 29, 2008 12:44 pm |
|
|
hi everbody, I having problems using fat32. I can't format SD card. When i try to format the system hang up. By the way, this system work with 1GB SD. I am using ccs v 4.74 fat library. Any one know how i can resolve this problem? what are the parameters i need to configure to format using dos/windows? Any one know another fat/mmc library that i can use. I will apreciate your help. Thanks. |
|
|
manish12
Joined: 18 Jan 2009 Posts: 15
|
modification for pic18f4620 |
Posted: Sun Jan 18, 2009 10:59 am |
|
|
I work on tomi code but not able to make compilable. What are the basic steps for compile it ?
I keep all files in one folder.
1] MyMMCFat32.h
2] MyMMCFat32.c
3] my6720.h
4] EX_MyFat32.c >> main
remove
.................
by
//...............
#include <18F6720.h> by #include <18F4620.h>
What else I can do for fix it ?
I am not so exp with ccs.
thank you |
|
|
manish12
Joined: 18 Jan 2009 Posts: 15
|
i compile it |
Posted: Sun Jan 18, 2009 12:48 pm |
|
|
i complie like this
my main.c file
Code: |
#define CardInserted pin_A4 // these pins are already defined in my main C file
#define REDLED pin_E7
#define GREENLED pin_E5
#define YELLOWLED pin_E6 // remove comments or comment out lines containing YELLOWLED refs in this file
#include "C:\Documents and Settings\manish\Desktop\mmc_new\proj1\main.h"
#include <string.h>
#include "my6720.h"
#include "MyMMCFat32.h"
#include "MyMMCFat32.c"
void main()
{
char f,v,msg[64],gfilename[32];
char gPrevCard,gActCard; // previous and actual card states (inserted, removed)
// InitClockInt(); // init the clock chip
// ReadClock(); // read the current time
if (!input(CardInserted))
{ // if MMC card is in place
do {
// output_high(REDLED);
v = MMCInit(); // init the card
delay_ms(50);
// output_low(REDLED);
delay_ms(50);
} while (!v);
// output_high(YELLOWLED);
InitFAT(); // init the file system
// output_low(YELLOWLED);
// output_low(REDLED);
}
//..................
strcpy(gfilename,"EVENTS.LOG");
f = fopen(gfilename,'a'); // open EVENTS.LOG for append
if (f < MAXFILES) {
sprintf(msg,"%04lu.%02u.%02u. %02u:%02u:%02u ",myrec.tm_year,myrec.tm_mon,myrec.tm_mday,myrec.tm_hour,myrec.tm_min,myrec.tm_sec);
fputstring(msg,f);
strcpy(msg,"System started\r\n");
fputstring(msg,f);
fclose(f);
}
while (1) {
restart_wdt();
// ResetPorts();
// DoIdle(); // Idle function incl. clock update
gActCard = input(CardInserted);
// if (gActCard)
// output_high(REDLED);
// else
// output_low(REDLED);
if (gActCard == 0 && gPrevCard != 0) { // card was pulled out then pushed back now
delay_ms(50);
do {
// output_high(REDLED);
v = MMCInit();
delay_ms(50);
// output_low(REDLED);
delay_ms(50);
} while (!v);
// output_high(YELLOWLED);
InitFAT();
// output_low(YELLOWLED);
// output_low(REDLED);
delay_ms(50);
strcpy(gfilename,"EVENTS.LOG");
f = fopen(gfilename,'a');
if (f < MAXFILES) {
sprintf(msg,"%04lu.%02u.%02u. %02u:%02u:%02u ",myrec.tm_year,myrec.tm_mon,myrec.tm_mday,myrec.tm_hour,myrec.tm_min,myrec.tm_sec);
fputstring(msg,f);
strcpy(msg,"Memory card replacement\r\n");
fputstring(msg,f);
fclose(f);
}
}
gPrevCard = gActCard;
}
}
|
mymmcfat32.h file
Code: |
#define MAXFILES 2
typedef struct _diskinforec
{
char hJumpCode[3];
char OEMName[8];
int16 hBytesPerSector;
char bSectorsPerCluster;
int16 Reserved1;
char FATCopies;
int16 hMaxRootEntries;
int16 hSectors;
char Descriptor;
int16 holdSectorsPerFat;
int16 hSectorsPerTrack;
int16 hNumberofHeads;
int32 hNumberofHidden;
int32 hNumberofSectors;
int32 hSectorsPerFat;
int16 hFlags;
int16 hFAT32Version;
int32 hRootStartCluster;
} diskinforec;
typedef struct _direntry
{
char sName[8];
char bExtension[3];
char bAttr;
char bReserved[8];
int16 hClusterH;
int16 hTime;
int16 hDate;
int16 hCluster;
int32 wSize;
} DIR;
typedef struct {
char IOpuffer[512];
DIR DirEntry;
int32 CurrentCluster;
int16 posinsector;
int32 position;
int32 dirSector;
int16 dirIdx;
char mode;
char Free;
} FILE;
typedef struct {
int32 MMCAddress;
int32 FATstartidx;
int32 gStartSector;
int32 gFirstDataSector;
int16 gDirEntryIdx;
int32 gDirEntrySector;
int16 gFirstEmptyDirEntry;
int32 gFirstDirEntryCluster;
} FAT32Vars;
char MMCInit();
void ReadSector(int32 sector, char *hova);
void WriteSector(int32 sector, char *honnan);
void InitFAT();
char FindDirEntry(char *fname, char f);
char fopen(char *fname, char mode);
void fclose(char f);
void fflush(char f);
char cwd(char *fname, char f);
void fputch(char be, char f);
char fgetch(char *ki, char f);
void fputstring(char *be, char f); // fputs is reserved in CCS C
int16 fread(char *buffer, int16 leng, char f);
void fwrite(char *buffer, int16 leng, char f);
char remove(char *fname);
char getfsize(char *fname, int32 *fsiz); |
mymmcfat32.c file
Code: |
int32 FATTable[128];
int32 gFirstEmptyCluster;
FAT32Vars gFAT32Vars;
diskinforec DiskInfo;
FILE gFiles[MAXFILES];
#byte MMCAddressL = gFAT32Vars
#byte MMCAddressH = gFAT32Vars+1
#byte MMCAddressHL = gFAT32Vars+2
#byte MMCAddressHH = gFAT32Vars+3
#byte gStartSectorL = gFAT32Vars+8
#byte gStartSectorH = gFAT32Vars+9
#byte gStartSectorHL = gFAT32Vars+10
#locate FATTable = 0x0800
#locate gFiles = 0x0A00
#locate gFAT32Vars = 0x0E70
#locate DiskInfo = 0x0E90
#use FAST_IO(C)
#define ChipSel pin_c2
#define ChipClk pin_c3
#define ChipDin pin_c5
typedef struct {
unsigned long tm_year;
char tm_mon;
char tm_day;
char tm_mday;
char tm_hour;
char tm_min;
char tm_sec;
} TimeRecord;
TimeRecord myrec; // this variable is updated in regular intervals in DoIdle()
char IsSelfDir(char *be)
{
if (be[0] == '.' && be[1] == '.') return 0xFF;
else return 0;
}
void MMCOut(char indata)
{
char i;
SSPBUF=indata;
while (!BF);
i = SSPBUF;
}
void MMC8Clock()
{
char i;
SSPBUF=0xFF;
while (!BF);
i = SSPBUF;
}
char MMCIn()
{
char i;
SSPBUF=0xFF;
while (!BF);
i = SSPBUF;
return i;
}
char MMCInit()
{
char response,iii,errcnt;
restart_wdt();
output_high(ChipSel);
output_high(ChipClk);
output_high(ChipDin);
bit_clear(SSPCON1,5);
SSPCON1 = 0x30; // modify this number to change SPI clock rate
SSPSTAT = 0;
iii = 10;
errcnt = 100;
do {
MMCOut(0xFF);
} while (--iii);
delay_us(1000);
output_low(ChipClk);
output_low(ChipSel);
MMCOut(0x40);
MMCOut(0x00);
MMCOut(0x00);
MMCOut(0x00);
MMCOut(0x00);
MMCOut(0x95);
MMC8Clock();
response = MMCIn();
output_high(ChipSel);
output_high(ChipClk);
do {
delay_us(1000);
output_low(ChipClk);
output_low(ChipSel);
MMCOut(0x41);
MMCOut(0x00);
MMCOut(0x00);
MMCOut(0x00);
MMCOut(0x00);
MMCOut(0x01);
MMC8Clock();
response = MMCIn();
output_high(ChipSel);
output_high(ChipClk);
MMC8Clock();
errcnt--;
} while (response && errcnt);
return errcnt;
}
int16 GetCurrentDOSDate()
{
int16 retval;
retval = myrec.tm_year - 1980;
retval <<= 9;
retval |= ((int16)myrec.tm_mon << 5);
retval |= (int16)myrec.tm_mday;
return retval;
}
int16 GetCurrentDOSTime()
{
int16 retval;
retval = myrec.tm_hour;
retval <<= 11;
retval |= ((int16)myrec.tm_min << 5);
retval |= (int16)myrec.tm_sec >> 1;
return retval;
}
void ReadSector(int32 sector, char *hova)
{
char errs,response;
char cnt2,cnt3;
#byte sectorL = sector
#byte sectorH = sector+1
#byte sectorHL = sector+2
if (input(CardInserted)) return;
Disable_interrupts(GLOBAL);
Restart_wdt();
MMCAddressL = 0;
MMCAddressH = sectorL;
MMCAddressHL = sectorH;
MMCAddressHH = sectorHL;
gFAT32Vars.MMCAddress <<= 1;
output_low(ChipClk);
output_low(ChipSel);
MMCOut(0x51);
MMCOut(MMCAddressHH);
MMCOut(MMCAddressHL);
MMCOut(MMCAddressH & 0xFE);
MMCOut(0);
MMCOut(0x01);
errs = 8;
do {
response = MMCIn();
} while (--errs && response==0xFF);
errs = 50;
do {
response = MMCIn();
if (response == 0xFE) break;
delay_ms(1);
} while (--errs);
*0xFE9 = (int16)hova;
cnt3 = 2;
cnt2 = 0;
do {
do {
SSPBUF=0xFF;
while (!BF);
*0xFEE = SSPBUF;
} while (--cnt2);
} while (--cnt3);
response = MMCIn();
response = MMCIn();
output_high(ChipSel);
output_high(ChipClk);
Enable_interrupts(GLOBAL);
}
void WriteSector(int32 sector, char *honnan)
{
char errs,response;
char cnt2,cnt3;
#byte sectorL = sector
#byte sectorH = sector+1
#byte sectorHL = sector+2
if (input(CardInserted)) return;
Disable_interrupts(GLOBAL);
Restart_wdt();
MMCAddressL = 0;
MMCAddressH = sectorL;
MMCAddressHL = sectorH;
MMCAddressHH = sectorHL;
gFAT32Vars.MMCAddress <<= 1;
response = 0;
output_low(ChipClk);
output_low(ChipSel);
MMCOut(0x58);
MMCOut(MMCAddressHH);
MMCOut(MMCAddressHL);
MMCOut(MMCAddressH & 0xFE);
MMCOut(0);
MMCOut(0x01);
MMC8Clock();
errs = 8;
do {
response = MMCIn();
} while (--errs && response==0xFF);
if (response) {
output_high(ChipSel);
output_high(ChipClk);
MMC8Clock();
Enable_interrupts(GLOBAL);
return;
}
MMC8Clock();
MMCOut(0xFE);
*0xFE9 = (int16)honnan;
cnt3 = 2;
cnt2 = 0;
do {
do {
SSPBUF=*0xFEE;
while (!BF);
} while (--cnt2);
} while (--cnt3);
MMCOut(0x00);
MMCOut(0x01);
response = MMCIn();
response ^= 0xE5;
if (response) {
goto endwr3;
}
do {
response = MMCIn();
} while (response == 0);
response = 0;
endwr3:
output_high(ChipSel);
output_high(ChipClk);
MMC8Clock();
Enable_interrupts(GLOBAL);
}
void InitFAT()
{
int32 actsector;
char i;
gFirstEmptyCluster = 0;
gFAT32Vars.gStartSector = 0;
ReadSector(gFAT32Vars.gStartSector,gFiles[MAXFILES-1].IOpuffer);
if (gFiles[MAXFILES-1].IOpuffer[0] != 0xEB) {
gStartSectorL = gFiles[MAXFILES-1].IOpuffer[0x1C6];
gStartSectorH = gFiles[MAXFILES-1].IOpuffer[0x1C7];
gStartSectorHL = gFiles[MAXFILES-1].IOpuffer[0x1C8];
ReadSector(gFAT32Vars.gStartSector,gFiles[MAXFILES-1].IOpuffer);
}
memcpy(&DiskInfo,gFiles[MAXFILES-1].IOpuffer,sizeof(DiskInfo));
actsector = gFAT32Vars.gStartSector+DiskInfo.Reserved1;
ReadSector(actsector,FATTable);
gFAT32Vars.FATstartidx = 0;
gFAT32Vars.gFirstDataSector = gFAT32Vars.gStartSector + DiskInfo.FATCopies*DiskInfo.hSectorsPerFat+DiskInfo.Reserved1 - 2;
for (i=0;i<MAXFILES;i++)
gFiles[i].Free = TRUE;
}
int32 GetNextCluster(int32 curcluster)
{
int32 actsector;
int32 clpage;
char clpos;
clpage = curcluster >> 7;
clpos = curcluster & 0x7F;
if (clpage != gFAT32Vars.FATstartidx) { // read in the requested page
actsector = gFAT32Vars.gStartSector+DiskInfo.Reserved1 + clpage;
ReadSector(actsector,FATTable);
gFAT32Vars.FATstartidx = clpage;
}
return (FATTable[clpos]);
}
void SetClusterEntry(int32 curcluster,int32 value)
{
int32 actsector;
int32 clpage;
char clpos;
clpage = curcluster >> 7;
clpos = curcluster & 0x7F;
actsector = gFAT32Vars.gStartSector+DiskInfo.Reserved1 + clpage;
if (clpage != gFAT32Vars.FATstartidx) {
ReadSector(actsector,FATTable);
gFAT32Vars.FATstartidx = clpage;
}
FATTable[clpos] = value;
WriteSector(actsector,FATTable);
actsector += DiskInfo.hSectorsPerFat;
WriteSector(actsector,FATTable);
}
void ClearClusterEntry(int32 curcluster)
{
int32 actsector;
int32 clpage;
char clpos;
clpage = curcluster >> 7;
clpos = curcluster & 0x7F;
if (clpage != gFAT32Vars.FATstartidx) {
actsector = gFAT32Vars.gStartSector+DiskInfo.Reserved1 + gFAT32Vars.FATstartidx;
WriteSector(actsector,FATTable);
actsector += DiskInfo.hSectorsPerFat;
WriteSector(actsector,FATTable);
actsector = gFAT32Vars.gStartSector+DiskInfo.Reserved1 + clpage;
ReadSector(actsector,FATTable);
gFAT32Vars.FATstartidx = clpage;
}
FATTable[clpos] = 0;
}
int32 FindFirstFreeCluster()
{
int32 i,st,actsector,retval;
char j;
st = gFirstEmptyCluster;
for (i=st;i<DiskInfo.hSectorsPerFat;i++) {
if (i != gFAT32Vars.FATstartidx) {
actsector = gFAT32Vars.gStartSector+DiskInfo.Reserved1 + i;
ReadSector(actsector,FATTable);
gFAT32Vars.FATstartidx = gFirstEmptyCluster = i;
}
for (j=0;j<128;j++)
if (FATTable[j] == 0) {
retval = i;
retval <<= 7;
retval |= j;
return retval;
}
}
return 0x0FFFFFFF;
}
void ConvertFilename(DIR *beDir,char *name)
{
char i,j,c;
j = 0;
name[0] = 0;
for (i=0;i<8;i++) {
c = beDir->sName[i];
if (c == ' ') break;
name[j++] = c;
}
for (i=0;i<3;i++) {
c = beDir-> bExtension[i];
if (c == ' ' || c == 0) break;
if (!i) name[j++] = '.';
name[j++] = c;
}
name[j++] = 0;
}
void GetDOSName(DIR *pDir, char *fname)
{
char i,j,leng,c,toext;
toext = FALSE;
j = 0;
leng = strlen(fname);
for (i=0;i<8;i++)
pDir->sName[i] = ' ';
for (i=0;i<3;i++)
pDir->bExtension[i] = ' ';
for (i=0;i<leng;i++) {
c = fname[i];
c = toupper(c);
if (c == '.') {
toext = TRUE;
continue;
}
if (toext) pDir->bExtension[j++] = c;
else pDir->sName[i] = c;
}
}
void ReadRootDirectory(char fil)
{
int32 actsector;
if (fil > (MAXFILES-1)) return;
actsector = gFAT32Vars.gStartSector + DiskInfo.FATCopies*DiskInfo.hSectorsPerFat+DiskInfo.Reserved1;
ReadSector(actsector,gFiles[fil].IOpuffer);
gFAT32Vars.gDirEntrySector = actsector;
gFiles[fil].dirSector = actsector;
gFiles[fil].dirIdx = 0;
memcpy(&(gFiles[fil].DirEntry),gFiles[fil].IOpuffer,32);
gFiles[fil].CurrentCluster = DiskInfo.hRootStartCluster;
}
char FindDirEntry(char *fname,char f)
{
DIR *pDir;
int16 i;
char filename[16];
int32 nextcluster,actsector;
if (f > (MAXFILES-1)) return FALSE;
gFAT32Vars.gFirstEmptyDirEntry = 0xFFFF;
gFAT32Vars.gFirstDirEntryCluster = 0x0FFFFFFF;
do {
pDir = (DIR*)(gFiles[f].IOpuffer);
for (i=0;i<16;i++) {
if ((pDir->sName[0] == 0xE5 || pDir->sName[0] == 0) && gFAT32Vars.gFirstEmptyDirEntry == 0xFFFF) { // store first free
gFAT32Vars.gFirstEmptyDirEntry = i;
gFAT32Vars.gFirstDirEntryCluster = gFiles[f].CurrentCluster;
}
if (pDir->sName[0] == 0) return FALSE;
ConvertFilename(pDir,filename);
if (!strcmp(filename,fname)) {
memcpy(&(gFiles[f].DirEntry),pDir,32);
gFiles[f].dirIdx = i;
gFAT32Vars.gDirEntryIdx = i;
return TRUE;
}
pDir++;
}
nextcluster = GetNextCluster(gFiles[f].CurrentCluster);
if (nextcluster != 0x0FFFFFFF && nextcluster != 0) {
actsector = nextcluster + gFAT32Vars.gFirstDataSector;
ReadSector(actsector,gFiles[f].IOpuffer);
gFAT32Vars.gDirEntrySector = actsector;
gFiles[f].dirSector = actsector;
gFiles[f].CurrentCluster = nextcluster;
}
} while (nextcluster != 0x0FFFFFFF && nextcluster != 0);
return FALSE;
}
// file I/O routines
char* TryFile(char *fname, char *f)
{
char i,leng;
char *filename;
*f = 0xFF;
for (i=0;i<MAXFILES;i++) {
if (gFiles[i].Free) {
*f = i;
break;
}
}
if (*f == 0xFF) return 0;
ReadRootDirectory(*f);
filename = fname;
leng = strlen(fname);
for (i=0;i<leng;i++) {
if (fname[i] == '/') {
fname[i] = 0;
if (!cwd(filename,*f)) {
gFiles[*f].Free = TRUE;
return 0;
}
filename = fname+i+1;
}
}
return filename;
}
char fcreate(char f,char *fname)
{
DIR *pDir;
int32 actsector,actcl;
int16 i;
if (f > (MAXFILES-1)) return FALSE;
if (gFAT32Vars.gFirstDirEntryCluster == 0x0FFFFFFF) {
// extend the directory file !!!
gFAT32Vars.gFirstDirEntryCluster = FindFirstFreeCluster();
gFAT32Vars.gFirstEmptyDirEntry = 0;
SetClusterEntry(gFiles[f].CurrentCluster,gFAT32Vars.gFirstDirEntryCluster);
SetClusterEntry(gFAT32Vars.gFirstDirEntryCluster,0x0FFFFFFF);
actsector = gFAT32Vars.gFirstDirEntryCluster + gFAT32Vars.gFirstDataSector;
for (i=0;i<512;i++)
gFiles[f].IOpuffer[i] = 0;
WriteSector(actsector,gFiles[f].IOpuffer);
}
actsector = gFAT32Vars.gFirstDirEntryCluster + gFAT32Vars.gFirstDataSector;
ReadSector(actsector,gFiles[f].IOpuffer);
pDir = (DIR*)(&(gFiles[f].IOpuffer[32*gFAT32Vars.gFirstEmptyDirEntry]));
gFiles[f].dirSector = actsector;
gFiles[f].dirIdx = gFAT32Vars.gFirstEmptyDirEntry;
GetDOSName(pDir,fname);
pDir->bAttr = 0;
actcl = FindFirstFreeCluster();
pDir->hCluster = actcl & 0xFFFF;
pDir->hClusterH = actcl >> 16;
SetClusterEntry(actcl,0x0FFFFFFF);
pDir->wSize = 0;
gFiles[f].position = 0;
pDir->hDate = GetCurrentDOSDate();
pDir->hTime = GetCurrentDOSTime();
WriteSector(actsector,gFiles[f].IOpuffer);
memcpy(&(gFiles[f].DirEntry),pDir,32);
return TRUE;
}
int32 ComposeCluster(char f)
{
int32 retval;
retval = gFiles[f].DirEntry.hClusterH;
retval <<= 16;
retval |= gFiles[f].DirEntry.hCluster;
return retval;
}
char fopen(char *fname, char mode)
{
char found;
char f;
int32 actsector,actcluster,nextcluster;
char *filename;
if (input(CardInserted)) return 0xFF;
// output_high(YELLOWLED);
filename = TryFile(fname,&f);
if (filename == 0) return 0xFF;
found = FALSE;
found = FindDirEntry(filename,f);
if (!found) {
if (mode == 'r') {
gFiles[f].Free = TRUE;
return 0xFF;
} else {
if (!fcreate(f,filename)) return 0xFF;
found = TRUE;
}
}
if (found) {
gFiles[f].Free = FALSE;
gFiles[f].mode = mode;
if (mode == 'a') {
gFiles[f].position = gFiles[f].DirEntry.wSize;
actcluster = ComposeCluster(f);
while (actcluster != 0x0FFFFFFF && nextcluster != 0) {
nextcluster = GetNextCluster(actcluster);
if (nextcluster == 0x0FFFFFFF || nextcluster == 0) break;
actcluster = nextcluster;
}
actsector = actcluster + gFAT32Vars.gFirstDataSector;
ReadSector(actsector,gFiles[f].IOpuffer);
gFiles[f].CurrentCluster = actcluster;
gFiles[f].posinsector = gFiles[f].position & 0x01FF;
if (gFiles[f].posinsector == 0 && gFiles[f].position != 0) gFiles[f].posinsector = 512;
} else {
gFiles[f].position = 0;
actsector = ComposeCluster(f);
actsector += gFAT32Vars.gFirstDataSector;
ReadSector(actsector,gFiles[f].IOpuffer);
gFiles[f].CurrentCluster = ComposeCluster(f);
gFiles[f].posinsector = 0;
}
}
return f;
}
void fclose(char f)
{
// output_low(YELLOWLED);
if (f > (MAXFILES-1)) return;
if ((gFiles[f].mode == 'a') || (gFiles[f].mode == 'w')) fflush(f);
gFiles[f].Free = TRUE;
}
void fflush(char f)
{
int32 actsector;
DIR *pDir;
if (f > (MAXFILES-1)) return;
actsector = gFiles[f].CurrentCluster + gFAT32Vars.gFirstDataSector;
WriteSector(actsector,gFiles[f].IOpuffer);
ReadSector(gFiles[f].dirSector,gFiles[f].IOpuffer);
pDir = (DIR*)(&(gFiles[f].IOpuffer[32*gFiles[f].dirIdx]));
if (gFiles[f].DirEntry.bAttr & 0x10) pDir->wSize = 0; // if it is a directory
else pDir->wSize = gFiles[f].position;
pDir->hDate = GetCurrentDOSDate();
pDir->hTime = GetCurrentDOSTime();
WriteSector(gFiles[f].dirSector,gFiles[f].IOpuffer);
ReadSector(actsector,gFiles[f].IOpuffer);
}
char cwd(char *fname, char f)
{
int32 actsector;
if (f > (MAXFILES-1)) return FALSE; // just in case of overaddressing
if (IsSelfDir(fname)) return TRUE; // already in Root dir
if (!FindDirEntry(fname,f)) return FALSE; // not found
actsector = ComposeCluster(f);
actsector += gFAT32Vars.gFirstDataSector; // read current dir
ReadSector(actsector,gFiles[f].IOpuffer);
gFAT32Vars.gDirEntrySector = actsector;
gFiles[f].dirSector = actsector;
gFiles[f].CurrentCluster = ComposeCluster(f);
return TRUE;
}
void fputch(char be, char f)
{
int32 nextcluster,actsector;
if (f > (MAXFILES-1)) return;
if (gFiles[f].posinsector == 512) {
actsector = gFiles[f].CurrentCluster + gFAT32Vars.gFirstDataSector;
WriteSector(actsector,gFiles[f].IOpuffer);
nextcluster = FindFirstFreeCluster();
if (nextcluster != 0x0FFFFFFF && nextcluster != 0) {
SetClusterEntry(gFiles[f].CurrentCluster,nextcluster);
SetClusterEntry(nextcluster,0x0FFFFFFF);
actsector = nextcluster + gFAT32Vars.gFirstDataSector;
ReadSector(actsector,gFiles[f].IOpuffer);
gFiles[f].CurrentCluster = nextcluster;
gFiles[f].posinsector = 0;
}
}
gFiles[f].IOpuffer[gFiles[f].posinsector] = be;
gFiles[f].posinsector++;
gFiles[f].position++;
}
void fputstring(char *be, char f)
{
int16 leng,i;
if (f > (MAXFILES-1)) return;
leng = strlen(be);
for (i=0;i<leng;i++)
fputch(be[i],f);
}
int16 fread(char *buffer, int16 leng, char f)
{
int16 i,retv;
char c,v;
if (f > (MAXFILES-1)) return 0;
retv = 0;
for (i=0;i<leng;i++) {
v = fgetch(&c,f);
if (v) {
buffer[i] = c;
retv++;
}
else break;
}
return retv;
}
void fwrite(char *buffer, int16 leng, char f)
{
int16 i;
if (f > (MAXFILES-1)) return;
for (i=0;i<leng;i++)
fputch(buffer[i],f);
}
char fgetch(char *ki,char f)
{
int32 nextcluster,actsector;
if (f > (MAXFILES-1)) return FALSE;
if (gFiles[f].position >= gFiles[f].DirEntry.wSize) return FALSE;
*ki = gFiles[f].IOpuffer[gFiles[f].posinsector];
gFiles[f].posinsector++;
gFiles[f].position++;
if (gFiles[f].posinsector == 512) {
nextcluster = GetNextCluster(gFiles[f].CurrentCluster);
if (nextcluster != 0x0FFFFFFF && nextcluster != 0) {
actsector = nextcluster + gFAT32Vars.gFirstDataSector;
ReadSector(actsector,gFiles[f].IOpuffer);
gFiles[f].CurrentCluster = nextcluster;
gFiles[f].posinsector = 0;
}
}
return TRUE;
}
char remove(char *fname)
{
char i,found;
char f;
DIR *pDir;
int32 nextcluster,currentcluster;
char *filename;
filename = TryFile(fname,&f);
if (filename == 0) return FALSE;
found = FindDirEntry(filename,f);
if (!found) {
gFiles[f].Free = TRUE;
return FALSE;
}
// output_high(YELLOWLED);
pDir = (DIR*)(&(gFiles[f].IOpuffer[32*gFAT32Vars.gDirEntryIdx]));
pDir->sName[0] = 0xE5;
for (i=1;i<8;i++)
pDir->sName[i] = ' ';
for (i=0;i<3;i++)
pDir->bExtension[i] = ' ';
WriteSector(gFAT32Vars.gDirEntrySector,gFiles[f].IOpuffer);
currentcluster = ComposeCluster(f);
while (currentcluster != 0x0FFFFFFF && nextcluster != 0) {
nextcluster = GetNextCluster(currentcluster);
ClearClusterEntry(currentcluster);
currentcluster = nextcluster;
}
ClearClusterEntry(currentcluster);
SetClusterEntry(currentcluster,0);
currentcluster = gFAT32Vars.gStartSector+DiskInfo.Reserved1 + gFAT32Vars.FATstartidx;
WriteSector(currentcluster,FATTable);
currentcluster += DiskInfo.hSectorsPerFat;
WriteSector(currentcluster,FATTable);
gFiles[f].Free = TRUE;
// output_low(YELLOWLED);
return TRUE;
}
char getfsize(char *fname, int32 *fsiz)
{
char found;
char f;
DIR *pDir;
char *filename;
filename = TryFile(fname,&f);
if (filename == 0) return FALSE;
found = FindDirEntry(filename,f);
if (!found) {
gFiles[f].Free = TRUE;
return FALSE;
}
pDir = (DIR*)(&(gFiles[f].IOpuffer[32*gFAT32Vars.gDirEntryIdx]));
gFiles[f].Free = TRUE;
*fsiz = pDir->wSize;
return TRUE;
} |
my6720.h file
Code: |
#byte STATUS = 0xFD8
//#bit bCARRY = STATUS.0
//#byte port_A = 0xF80
#byte port_B = 0xF81
#byte port_C = 0xF82
#byte port_D = 0xF83
#byte port_E = 0xF84
#byte port_F = 0xF85
#byte port_G = 0xF86
#byte tris_A = 0xF92
#byte tris_B = 0xF93
#byte tris_C = 0xF94
#byte tris_D = 0xF95
#byte tris_E = 0xF96
#byte tris_F = 0xF97
#byte tris_G = 0xF98
#byte PCON = 0xFD0
#byte bRCSTA = 0xFAB
#define bCREN 4
#define bOERR 2
#byte SSPADD = 0xFC8
#byte SSPCON1= 0xFC6
#byte SSPCON2= 0xFC5
#byte SSPBUF = 0xFC9
#byte SSPSTAT= 0xFC7
#byte PIR1 = 0xF9E
#byte PIE1 = 0xF9D
#byte PIR2 = 0xFA1
#bit SSPOV = SSPCON1.6
//#bit SEN = SSPCON2.0
//#bit RSEN = SSPCON2.1
//#bit PEN = SSPCON2.2
//#bit RCEN = SSPCON2.3
//#bit ACKEN = SSPCON2.4
//#bit ACKDT = SSPCON2.5
//#bit ACKSTAT= SSPCON2.6
//#bit SSPIF = PIR1.3
//#bit BCLIF = PIR2.3
#bit BF = SSPSTAT.0
|
now , i have to test it by hw .
i have mmc 32mb,sd 128mb,sd 256mb
for any correction ,check etc are wel-come
i dont know about the fat etc , i just want to make mmc <==> pic thats it. |
|
|
|
|
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
|