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

📄 mmc.lst

📁 C8051F120与串口、并口、D12、USB、LCD应用
💻 LST
📖 第 1 页 / 共 2 页
字号:
C51 COMPILER V7.01  MMC                                                                    06/30/2007 11:42:51 PAGE 1   


C51 COMPILER V7.01, COMPILATION OF MODULE MMC
OBJECT MODULE PLACED IN MMC.OBJ
COMPILER INVOKED BY: C:\Keil\C51\BIN\C51.EXE MMC.c LARGE BROWSE DEBUG OBJECTEXTEND

stmt level    source

   1          #include <string.h>
   2          #include <stdio.h>
   3          #include "uart.h"
   4          #include "menu.h"
   5          #include "Lcd.h"
   6          #include "system.h"
   7          #include "mmc.h"
   8          #include "usbtest.h"
   9          
  10          char FILENO=0;
  11          unsigned long WCnt=0;
  12          unsigned long RCnt=0;
  13          char Found;                                                                     //在目录中找到的文件或目录个数
  14          unsigned long DCounter;                                         //查找目录时字节计数器
  15          FILESTRUCT  File[13];                                           //保存查找到的文件或目录信息
  16          FILESTRUCT CurF;                                                        //存放当前目录
  17          unsigned char MMCDataBuf[513];                          //存放MMC一个块的数据
  18          unsigned char MMCUSBBuf[513];                           //用作与PC通过USB交换数据
  19          char PATH[40];
  20          char DEPTH;
  21          
  22          unsigned long SecPClu=0;                                        //每簇所含块数
  23          unsigned long BytePClu=0;                                       //每簇所含字节数
  24          unsigned long DIR_ORI=0;                                        //根目录起始点  (根目录共占32个Sector,16kB)
  25          unsigned long DIR_END=0;                                        //根目录结束点  (紧随其后是第二簇,没有第零,一簇)
  26          unsigned long DIR_LEN=0;                                        //根目长度
  27          unsigned long CLU_ORI=0;                                        //用户区起始地址,假设存在的第零簇的位置
  28          unsigned long FAT1_ORI=0;                                       //FAT表1起始地址
  29          unsigned long FAT2_ORI=0;                                       //FAT表2起始地址
  30          
  31          
  32          unsigned char code FileIcon[]={"砬"};
  33          unsigned char code FoldIcon[]={"苈"};
  34          
  35          void SYS_INIT()
  36          {       
  37   1              SFRPAGE=0x0f;           //c8051f120系列中要访问P4-P7必须设置SFRPAGE=0x0F
  38   1      
  39   1              XBR2|=0x40;                     //交叉开关使能
  40   1              XBR0|=0x06;                     //SPI,UART0使能(在C8051F0XX系列中时SMbBus使能,即0x03)*/
  41   1              P0MDOUT|=0x14;          //p0.2:SPI-SCK为PUSH-PULL,p0.4:SPI-MOSI 设为PUSH-PULL fpage
  42   1              P7MDOUT|=0x60;      //P7.5:CS设置为PUSH-PULL,P7.6:PWR设为PUSH-PULL,f page
  43   1      
  44   1              // SPI 0 PAGE
  45   1              SFRPAGE=0x00;
  46   1              SPIEN=0;                        //turn off SPIEN before setting phase and polarity
  47   1              SPI0CFG |= 0x70;                //设置时钟相位和极性,SPI设置为主模式
  48   1              SPI0CN |= 0x01;        //SSPI使能!transmit buffer empty!由于使三线所以设置NSSnMD为00
  49   1              SPI0CKR = 0x00;       //SPI时钟速率
  50   1      
  51   1              //skip p1.1 pin because spi set to 3 lines mode
  52   1      //      XBR0 |= 0x40;
  53   1      //      P1MDIN &= 0xFD;
  54   1      
  55   1      
C51 COMPILER V7.01  MMC                                                                    06/30/2007 11:42:51 PAGE 2   

  56   1              SFRPAGE=0x0f;
  57   1              MMC_power=0;            //打开电源
  58   1      }
  59          
  60          //////////////////////////////////////////////////////////////////////////////////////////////////
  61          //                                                              SPI底层单字节数据收发                                                                                   //
  62          //////////////////////////////////////////////////////////////////////////////////////////////////
  63          unsigned char SPI_SEND(unsigned char Bdata)                     //与MMC卡交换数据函数
  64          {       
  65   1              SFRPAGE=0x00;
  66   1              SPI0DAT=Bdata;
  67   1              while(!SPIF);                                   //当SPI中断为到,即数据为传输完时则循环等待.                            
  68   1              SPIF=0;                                                 //由于硬件中断后没有清零,所以需要手动清零
  69   1              return SPI0DAT;
  70   1      }
  71          
  72          //////////////////////////////////////////////////////////////////////////////////////////////////
  73          //                                                      MMC卡SPI模式命令层                                                                                                      //
  74          //////////////////////////////////////////////////////////////////////////////////////////////////
  75          unsigned char MMC_SEND_CMD(unsigned char index,argument1,argument2,argument3,argument4)
  76          {
  77   1      
  78   1      /*
  79   1        每一个MMC命令均为48bits,所以发送6个字节的数据
  80   1        而默认的情况下:MMC的命令格式如下:
  81   1      
  82   1        位    47              46              [45:40]         [39:8]          [7:1]           0
  83   1        值    0               1                       X                 X                       X                     1 
  84   1        宽度  1       1           6         32                  7                     1
  85   1        功能  起始位 传输位    命令值       参数       CRC7      中止位
  86   1      
  87   1         SPI模式下CRC校验默认为关闭,相应的位可为任意值.               
  88   1      */
  89   1              signed char temp;
  90   1              //以下的6个语句用来reset MMC to SPI mode
  91   1              SPI_SEND(index|0x40);           //01xx xxxx 
  92   1              SPI_SEND(argument1);
  93   1              SPI_SEND(argument2);
  94   1              SPI_SEND(argument3);
  95   1              SPI_SEND(argument4);            //32bit参数zz
  96   1              SPI_SEND(0x95);                         //crc校验位,进入spi模式后默认关闭,值可以任意
  97   1      
  98   1              SPI_SEND(0xff);                         
  99   1              temp=SPI_SEND(0xff);
 100   1              if ((temp-0x80)>=0)
 101   1              {               //0b1xxxxxxx=err        
 102   2                      temp=0xff;
 103   2                      while (SPI_SEND(0xff)==0); 
 104   2              }
 105   1              else 
 106   1              if (index&0xbf==13)
 107   1              {                                                                       //如果是读状态命令
 108   2                      temp=SPI_SEND(0xff);                    //read STATUS
 109   2                      while (SPI_SEND(0xff)==0); 
 110   2              }                                                                                                        
             -                                                                                              
 111   1              else
 112   1              {                                                               //是其他命令
 113   2                      while (SPI_SEND(0xff)==0); 
 114   2              }
 115   1              return temp; 
 116   1      }
C51 COMPILER V7.01  MMC                                                                    06/30/2007 11:42:51 PAGE 3   

 117          
 118          unsigned char MMC_SPI_INIT(void)
 119          {
 120   1              unsigned char counter,temp;
 121   1              temp=0xff;
 122   1              SFRPAGE=0x0F;
 123   1              MMC_CS=0;                       
 124   1              //延时
 125   1              for(counter=0;counter<10;counter++)
 126   1              SPI_SEND(0xff);
 127   1              delay(500);
 128   1      
 129   1              temp=MMC_SEND_CMD(MMC_GO_IDLE_STATE,0,0,0,0);   //进入SPI模式
 130   1              for(counter=0;counter<=80;counter++)                    //延时80周期
 131   1              {
 132   2                      temp=MMC_SEND_CMD(MMC_SEND_OP_COND,0,0,0,0);    
 133   2                      if(temp==0)     break;
 134   2              }
 135   1              if(counter==81) return KO;
 136   1              return OK;
 137   1              MMC_CS=1;
 138   1      }
 139          
 140          
 141          unsigned char MMC_block_read(unsigned char *buf,unsigned long Addr32)   //读取Addr32所在的SECTOR到Buf
 142          {
 143   1              int counter=0;
 144   1              unsigned char temp;
 145   1              unsigned char P[3];
 146   1              P[2]=(unsigned char)(Addr32>>8)&0xFE;
 147   1              P[1]=(unsigned char)(Addr32>>16);
 148   1              P[0]=(unsigned char)(Addr32>>32);
 149   1              MMC_CS=0;
 150   1              temp=MMC_SEND_CMD(MMC_READ_SINGLE_BLOCK,P[0],P[1],P[2],0);      //发送单块读命令
 151   1              while(temp!=0xfe && counter<255)                                        //等待返回0xFE起始字节
 152   1              {
 153   2                      temp=SPI_SEND(0xff);
 154   2                      counter++;
 155   2              }
 156   1              if(counter==255)
 157   1              {
 158   2                      return KO;
 159   2              }
 160   1              for(counter=0;counter<512;counter++)
 161   1              {
 162   2                      buf[counter]=SPI_SEND(0xff);
 163   2              }
 164   1              SPI_SEND(0xff);
 165   1              SPI_SEND(0xff);
 166   1              MMC_CS=1;
 167   1              return OK;
 168   1      }
 169          
 170          unsigned char MMC_block_write(unsigned char *buf,unsigned long Addr32)//将512B的Buf写入到Addr32所在的SECTO
             -R
 171          {
 172   1              int counter;
 173   1              unsigned char temp;
 174   1              unsigned char P[3];

⌨️ 快捷键说明

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