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

📄 sd.lst

📁 Philips LPC2138 Demo Application with Keil C
💻 LST
📖 第 1 页 / 共 5 页
字号:
ARM COMPILER V2.42,  sd                                                                    27/03/06  10:45:52  PAGE 1   


ARM COMPILER V2.42, COMPILATION OF MODULE sd
OBJECT MODULE PLACED IN .\obj\sd.obj
COMPILER INVOKED BY: C:\Keil\ARM\BIN\CA.exe src\sd.c THUMB DEBUG PRINT(.\LST\SD.LST) TABS(4) OBJECT(.\obj\sd.obj) 

stmt  level    source

    1          /*****************************************************************************\
    2          *              efs - General purpose Embedded Filesystem library              *
    3          *          --------------------- -----------------------------------          *
    4          *                                                                             *
    5          * Filename : sd.c                                                             *
    6          * Revision : Initial developement                                             *
    7          * Description : This file contains the functions needed to use efs for        *
    8          *               accessing files on an SD-card.                                *
    9          *                                                                             *
   10          * This library is free software; you can redistribute it and/or               *
   11          * modify it under the terms of the GNU Lesser General Public                  *
   12          * License as published by the Free Software Foundation; either                *
   13          * version 2.1 of the License, or (at your option) any later version.          *
   14          *                                                                             *
   15          * This library is distributed in the hope that it will be useful,             *
   16          * but WITHOUT ANY WARRANTY; without even the implied warranty of              *
   17          * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU           *
   18          * Lesser General Public License for more details.                             *
   19          *                                                                             *
   20          *                                                    (c)2005 Michael De Nil   *
   21          *                                                    (c)2005 Lennart Yseboodt *
   22          \*****************************************************************************/
   23          
   24          /*****************************************************************************/
   25          #include "sd.h"
   26          /*****************************************************************************/
   27          
   28          esint8 sd_Init(hwInterface *iface)
   29          {
   30   1          esint16 i;
   31   1          euint8 resp;
   32   1          
   33   1          /* Try to send reset command up to 100 times */
   34   1          i=100;
   35   1          do{
   36   2              sd_Command(iface,0, 0, 0);
   37   2              resp=sd_Resp8b(iface);
   38   2          }
   39   1          while(resp!=1 && i--);
   40   1          
   41   1          if(resp!=1){
   42   2              if(resp==0xff){
   43   3                  return(-1);
   44   3              }
   45   2              else{
   46   3                  sd_Resp8bError(iface,resp);
   47   3                  return(-2);
   48   3              }
   49   2          }
   50   1      
   51   1          /* Wait till card is ready initialising (returns 0 on CMD1) */
   52   1          /* Try up to 32000 times. */
   53   1          i=32000;
   54   1          do{
   55   2              sd_Command(iface,1, 0, 0);
   56   2              
   57   2              resp=sd_Resp8b(iface);
   58   2              if(resp!=0)
   59   2                  sd_Resp8bError(iface,resp);
ARM COMPILER V2.42,  sd                                                                    27/03/06  10:45:52  PAGE 2   

   60   2          }
   61   1          while(resp==1 && i--);
   62   1          
   63   1          if(resp!=0){
   64   2              sd_Resp8bError(iface,resp);
   65   2              return(-3);
   66   2          }
   67   1          
   68   1          return(0);
   69   1      }
   70          /*****************************************************************************/
   71          
   72          void sd_Command(hwInterface *iface,euint8 cmd, euint16 paramx, euint16 paramy)
   73          {
   74   1          if_spiSend(iface,0xff);
   75   1      
   76   1          if_spiSend(iface,0x40 | cmd);
   77   1          if_spiSend(iface,(euint8) (paramx >> 8)); /* MSB of parameter x */
   78   1          if_spiSend(iface,(euint8) (paramx)); /* LSB of parameter x */
   79   1          if_spiSend(iface,(euint8) (paramy >> 8)); /* MSB of parameter y */
   80   1          if_spiSend(iface,(euint8) (paramy)); /* LSB of parameter y */
   81   1      
   82   1          if_spiSend(iface,0x95); /* Checksum (should be only valid for first command (0) */
   83   1      
   84   1          if_spiSend(iface,0xff); /* eat empty command - response */
   85   1      }
   86          /*****************************************************************************/
   87          
   88          euint8 sd_Resp8b(hwInterface *iface)
   89          {
   90   1          euint8 i;
   91   1          euint8 resp;
   92   1          
   93   1          /* Respone will come after 1 - 8 pings */
   94   1          for(i=0;i<8;i++){
   95   2              resp = if_spiSend(iface,0xff);
   96   2              if(resp != 0xff)
   97   2                  return(resp);
   98   2          }
   99   1              
  100   1          return(resp);
  101   1      }
  102          /*****************************************************************************/
  103          
  104          euint16 sd_Resp16b(hwInterface *iface)
  105          {
  106   1          euint16 resp;
  107   1          
  108   1          resp = ( sd_Resp8b(iface) << 8 ) & 0xff00;
  109   1          resp |= if_spiSend(iface,0xff);
  110   1          
  111   1          return(resp);
  112   1      }
  113          /*****************************************************************************/
  114          
  115          void sd_Resp8bError(hwInterface *iface,euint8 value)
  116          {
  117   1          switch(value)
  118   1          {
  119   2              case 0x40:
  120   2                  DBG((TXT("Argument out of bounds.\n")));
  121   2                  break;
  122   2              case 0x20:
  123   2                  DBG((TXT("Address out of bounds.\n")));
  124   2                  break;
  125   2              case 0x10:
ARM COMPILER V2.42,  sd                                                                    27/03/06  10:45:52  PAGE 3   

  126   2                  DBG((TXT("Error during erase sequence.\n")));
  127   2                  break;
  128   2              case 0x08:
  129   2                  DBG((TXT("CRC failed.\n")));
  130   2                  break;
  131   2              case 0x04:
  132   2                  DBG((TXT("Illegal command.\n")));
  133   2                  break;
  134   2              case 0x02:
  135   2                  DBG((TXT("Erase reset (see SanDisk docs p5-13).\n")));
  136   2                  break;
  137   2              case 0x01:
  138   2                  DBG((TXT("Card is initialising.\n")));
  139   2                  break;
  140   2              default:
  141   2                  DBG((TXT("Unknown error 0x%x (see SanDisk docs p5-13).\n"),value));
  142   2                  break;
  143   2          }
  144   1      }
*** WARNING C47 IN LINE 115 OF SRC\SD.C: 'iface': unreferenced parameter
  145          /*****************************************************************************/
  146          
  147          esint8 sd_State(hwInterface *iface)
  148          {
  149   1          eint16 value;
  150   1          
  151   1          sd_Command(iface,13, 0, 0);
  152   1          value=sd_Resp16b(iface);
  153   1      
  154   1          switch(value)
  155   1          {
  156   2              case 0x000:
  157   2                  return(1);
  158   2                  break;
  159   2              case 0x0001:
  160   2                  DBG((TXT("Card is Locked.\n")));
  161   2                  break;
  162   2              case 0x0002:
  163   2                  DBG((TXT("WP Erase Skip, Lock/Unlock Cmd Failed.\n")));
  164   2                  break;
  165   2              case 0x0004:
  166   2                  DBG((TXT("General / Unknown error -- card broken?.\n")));
  167   2                  break;
  168   2              case 0x0008:
  169   2                  DBG((TXT("Internal card controller error.\n")));
  170   2                  break;
  171   2              case 0x0010:
  172   2                  DBG((TXT("Card internal ECC was applied, but failed to correct the data.\n")));
  173   2                  break;
  174   2              case 0x0020:
  175   2                  DBG((TXT("Write protect violation.\n")));
  176   2                  break;
  177   2              case 0x0040:
  178   2                  DBG((TXT("An invalid selection, sectors for erase.\n")));
  179   2                  break;
  180   2              case 0x0080:
  181   2                  DBG((TXT("Out of Range, CSD_Overwrite.\n")));
  182   2                  break;
  183   2              default:
  184   2                  if(value>0x00FF)
  185   2                      sd_Resp8bError(iface,(euint8) (value>>8));
  186   2                  else
  187   2                      DBG((TXT("Unknown error: 0x%x (see SanDisk docs p5-14).\n"),value));
  188   2                  break;
  189   2          }
  190   1          return(-1);
ARM COMPILER V2.42,  sd                                                                    27/03/06  10:45:52  PAGE 4   

  191   1      }
  192          /*****************************************************************************/
  193          
  194          /* ****************************************************************************
  195           * WAIT ?? -- FIXME
  196           * CMDWRITE
  197           * WAIT
  198           * CARD RESP
  199           * WAIT
  200           * DATA BLOCK OUT
  201           *      START BLOCK
  202           *      DATA
  203           *      CHKS (2B)
  204           * BUSY...
  205           */
  206          
  207          esint8 sd_writeSector(hwInterface *iface,euint32 address, euint8* buf)
  208          {
  209   1          euint32 place;
  210   1          euint16 i;
  211   1          euint16 t=0;
  212   1          
  213   1          /*DBG((TXT("Trying to write %u to sector %u.\n"),(void *)&buf,address));*/
  214   1          place=512*address;
  215   1          sd_Command(iface,CMDWRITE, (euint16) (place >> 16), (euint16) place);
  216   1      
  217   1          sd_Resp8b(iface); /* Card response */
  218   1      
  219   1          if_spiSend(iface,0xfe); /* Start block */
  220   1          for(i=0;i<512;i++) 
  221   1              if_spiSend(iface,buf[i]); /* Send data */
  222   1          if_spiSend(iface,0xff); /* Checksum part 1 */
  223   1          if_spiSend(iface,0xff); /* Checksum part 2 */
  224   1      
  225   1          if_spiSend(iface,0xff);
  226   1      
  227   1          while(if_spiSend(iface,0xff)!=0xff){
  228   2              t++;
  229   2              /* Removed NOP */
  230   2          }
  231   1          /*DBG((TXT("Nopp'ed %u times.\n"),t));*/
  232   1      
  233   1          return(0);
  234   1      }
  235          /*****************************************************************************/
  236          
  237          /* ****************************************************************************
  238           * WAIT ?? -- FIXME
  239           * CMDCMD
  240           * WAIT
  241           * CARD RESP
  242           * WAIT
  243           * DATA BLOCK IN
  244           *      START BLOCK
  245           *      DATA
  246           *      CHKS (2B)
  247           */
  248          
  249          esint8 sd_readSector(hwInterface *iface,euint32 address, euint8* buf, euint16 len)
  250          {
  251   1          euint8 cardresp;
  252   1          euint8 firstblock;
  253   1          euint8 c;
  254   1          euint16 fb_timeout=0xffff;
  255   1          euint32 i;
  256   1          euint32 place;
ARM COMPILER V2.42,  sd                                                                    27/03/06  10:45:52  PAGE 5   

  257   1      
  258   1          /*DBG((TXT("sd_readSector::Trying to read sector %u and store it at %p.\n"),address,&buf[0]));*/
  259   1          place=512*address;
  260   1          sd_Command(iface,CMDREAD, (euint16) (place >> 16), (euint16) place);
  261   1          
  262   1          cardresp=sd_Resp8b(iface); /* Card response */ 
  263   1      
  264   1          /* Wait for startblock */
  265   1          do

⌨️ 快捷键说明

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