⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 commands.c

📁 MMC interface and FAT File system
💻 C
字号:
//#########################################################################
// File: commands.c
//
// Serial commands input
//
//#########################################################################
// Last change: 10.05.2003
//#########################################################################
// holger.klabunde@t-online.de
// http://home.t-online.de/home/holger.klabunde/homepage.htm
//#########################################################################
// Compiler: AVR-GCC 3.2
//#########################################################################
#include <string.h>

#include "protos.h"
#include "commands.h"
#include "serial.h"
#include "dos.h"
#include "lcd.h"
#include "mydefs.h"

unsigned long atoul(char *s);
void FillSectorBuffer(unsigned char *buf);

//########################################################
unsigned char WaitCommand(void)
//########################################################
{
 char c, s[2];
 unsigned long tmp,count;
 unsigned int i;
 
 s[1]=0;
 strcpy(line,"\0");

 do
  {
   c=ser_getc();
//   if(echo) ser_putc(c);  //echo back
   if(c==0x0A) c=0x0D;
   if(c!=0x0D && c!=0x08)
    { s[0]=c;
      strcat(line,s);
    }
   if(c==0x08 && strlen(line)>0) line[strlen(line)-1]=0; //DEL
  }while(c!=0x0D); 

 if(line[0]==0) return NOCOMM;

 if(strlen(line)==1) //short commands without parameters
  {
   if(line[0]=='h') return HELP;
   if(line[0]=='r') return ROOT;
   if(line[0]=='i') return INFO;
  }

 if(strlen(line)==2) //short commands without parameters
  {
   if(strcmp(line,"ls")==0) return LIST_DIR;
   if(strcmp(line,"ds")==0) return DUMP_SECTOR;
   if(strcmp(line,"e0")==0) { echo=0; return NOCOMM; }
   if(strcmp(line,"e1")==0) { echo=1; puts("Echo on\n"); return NOCOMM; }
   if(strcmp(line,"b0")==0) { bmode=0; puts("Binary off\n"); return NOCOMM; }
   if(strcmp(line,"b1")==0) { bmode=1; return NOCOMM; }
   if(strcmp(line,"l0")==0) { lo=0; return NOCOMM; }
   if(strcmp(line,"l1")==0) { lo=1; return NOCOMM; }

   if(strcmp(line,"nc")==0)
    {
     CurrentCluster=GetNextClusterNumber(CurrentCluster);
     if(echo) printf("Cluster: %lu 0x%08lx\n",CurrentCluster,CurrentCluster);
     return NOCOMM;
    } 

   if(strcmp(line,"fc")==0)
    {
     tmp=FindFreeCluster(CurrentCluster);
     if(echo) printf("Free Cluster: %lu 0x%08lx\n",tmp,tmp);
     return NOCOMM;
    }
  }

 if(strlen(line)==3) //short commands without parameters
  {
   if(strcmp(line,"ds+")==0)
    {
     if(CurrentSector<(maxsect-1)) CurrentSector++;
     return DUMP_SECTOR;
    } 

   if(strcmp(line,"ds-")==0)
    {
     if(CurrentSector>0) CurrentSector--;
     return DUMP_SECTOR;
    } 
  }

 if(strncmp(line,"cd",2)==0)
  {
   ChangeDir(&line[2]);
   return NOCOMM;
  }

 if(strncmp(line,"ds",2)==0)
  {
   tmp=atoul(&line[2]);
   if(tmp>=maxsect)
    {
     printf("Sector number %lu too big\n",tmp);
     return NOCOMM;
    } 
   CurrentSector=tmp;
   return DUMP_SECTOR;
  }

 if(strncmp(line,"cs",2)==0)
  {
   tmp=atoul(&line[2]);
   if(tmp<=maxcluster)
    {
     CurrentCluster=tmp;
     CurrentSector=GetFirstSectorOfCluster(tmp);

     if(CurrentCluster==0) //FAT12/16 Rootdir
      {
       CurrentSector=FirstRootSector;
      }
    } 
   return CLUSTER_TO_SECTOR;
  }

 if(strncmp(line,"df",2)==0)
  {
   if(FindName(&line[2]))
    {
     ReadFileRaw(FileFirstCluster);
    } 
   return NOCOMM;
  }

 if(strncmp(line,"ws",2)==0)
  {
   tmp=atoul(&line[2]);
   FillSectorBuffer(inbuff); //read over serial
   CFWriteSector(tmp,inbuff);
   return NOCOMM;
  }

 if(strncmp(line,"ts",2)==0)
  {
   tmp=atoul(&line[2]);

   while(tmp-- > 0) CFReadSector(0,inbuff);
   
   ser_putc(EOT);
   return NOCOMM;
  }

 if(strncmp(line,"cc",2)==0)
  {
   count=0;
   tmp=atoul(&line[2]);
   printf("%lu\n",tmp);
   count++;
   while(tmp<endofclusterchain)
    {
     tmp=GetNextClusterNumber(tmp);
     if(tmp<endofclusterchain)
      { printf("%lu\n",tmp);
        count++;
      }  
    }
   printf("Clusters %lu Space %lu\n",count,count*secPerCluster*BYTE_PER_SEC);
     
   ser_putc(EOT);
   return NOCOMM;
  }

 if(strncmp(line,"rf",2)==0)
  {
   if(fopen(&line[2],F_READ))
    {
//     while(fread(&c,1)) ser_putc(c);
//     while(fread(&c,1));
//     while((count=fread(line,40))); //read 40 bytes into buffer

     while((count=fread(line,40))) //read 40 bytes into buffer
      {
       for(i=0; i<count; i++)
        {
         ser_putc(line[i]);
        }
      }

     fclose();
    }

   return NOCOMM;
  }

 if(strncmp(line,"fo",2)==0)
  {
   if(fopen(&line[2],F_WRITE))
    {
     for(i=0; i<10000; i++)
      {
       if(fwrite("Hello World\n",12)!=12) break;
      }
      
     fclose();
    } 
   ser_putc(EOT); 
   return NOCOMM;
  }

 puts("Unknown Command\n");   

 return NOCOMM;
}

//########################################################
void HelpScreen(void)
//########################################################
{
 puts("\nCommand List:\n");
 puts("b0,b1    send sectors binary b1=on b0=off\n");
 puts("e0,e1    echo e1=on e0=off\n");
 puts("h        show this help\n");
 puts("i        read drive parameters and show drive informations\n");
 puts("ds       dump current sector\n");
 puts("ds+,ds-  dump next/previous sector\n");
 puts("ds[nr]   dump sector[nr]\n");
 puts("fc       find next free cluster\n");
 puts("nc       next cluster in chain\n");
 puts("cc[nr]   send cluster chain for cluster [nr]\n");
 puts("cs[nr]   set cluster and current sector to beginning of cluster[nr]\n");
 puts("ts[nr]   test read sector0 for [nr]times to RAM\n");
 puts("ws[nr]   write sector[nr]\n");
 puts("r        change to root directory and show it\n");
 puts("ls       list current directory\n");
 puts("cd[name] change dir [name]\n");
 puts("df[name] download file [name] with ReadFileRaw()\n");
 puts("rf[name] download file [name] with fread()\n");
 puts("fo[name] open file [name] and write/append 120000 Bytes\n");
 puts("l0,l1    show long filename entrys l1=on l0=off\n");

 ser_putc(EOT);
}

//########################################################
void FillSectorBuffer(unsigned char *buf)
//########################################################
{
 unsigned int i;

 for(i=0; i<BYTE_PER_SEC; i++)
  {
   buf[i]=ser_getc();
  }
}

//########################################################
//Very small atol(). only for decimal numbers. no hex
unsigned long atoul(char *s)
//########################################################
{
 unsigned char c;
 unsigned long tmp;
 
 tmp=0;

 while((c=*s++))
  {
   if(c>='0' && c<='9')
    {
     tmp*=10;
     tmp+=c-'0';
    }
  }
 
 return tmp;
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -