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

📄 disk3.lst

📁 用于Slave I2C得读写,此代码针对特定得芯片,但对其他得芯片也有借鉴作用.
💻 LST
📖 第 1 页 / 共 5 页
字号:
 842   1              memcpy(package+5,nId,3);  
 843   1         else
 844   1              memcpy(package+5,t8ID,3);  
 845   1              
 846   1      
 847   1         memcpy(package+8,KEY,24);
 848   1         Reverse(package+8,8);
 849   1         Reverse(package+16,8);
 850   1         Reverse(package+24,8); 
 851   1      
 852   1         DoXX(package,package, 8+24, 1); 
 853   1         GetSn(package,8+24);
 854   1         
 855   1          return(DoComunication(package, 8+24+2, 1));
C51 COMPILER V7.02b   DISK3                                                                03/16/2006 13:52:41 PAGE 15  

 856   1       }
 857          
 858          /*
 859          **************************************************
 860          * prototype :   BOOL WriteCustomIdCmd(BYTE * KEY,BYTE len,BYTE Nvmoffset)
 861          * Des: send "Write Custom Id" command on SPI or I2C bus
 862          * here "CId" is the poniter of len bytes array.
 863          * "Nvmoffset" is the offset in NVM 
 864          * If comunication succeed,it return true;
 865          * If input parametre is error,it return false and errcode is 1.
 866          *If comunication is not success,it return false and errcode is 2.
 867          * Use GetLastError to determine. 
 868          ***************************************************
 869          */
 870          BOOL WriteCustomIdCmd(BYTE * CId,BYTE len,BYTE Nvmoffset)   //add
 871          {               
 872   1          if(CId==NULL||len>8||((Nvmoffset<0x20)||(Nvmoffset>0x7f)))   //add 12.09
 873   1          {
 874   2                    errcode=PRAMWRONG;
 875   2                      return false;
 876   2              }
 877   1         
 878   1         memset(package,0,8);
 879   1         package[0]=0xc2;
 880   1         package[1]=MCU_SN;
 881   1         package[2]=RN;
 882   1         package[6]=Nvmoffset;
 883   1         package[7]=len;
 884   1        
 885   1         memcpy(package+8,CId,len);  
 886   1      
 887   1         DoXX(package, package, 8+ 8, 1);
 888   1      
 889   1         GetSn(package,8+8);
 890   1         
 891   1         return(DoComunication(package,8+8+2,1));
 892   1      }
 893           /*
 894          ***************************************************
 895          *   check whether the receive package is
 896          *   "Write key ready" command 
 897          ***************************************************
 898          */
 899          
 900           BOOL IsWriteKeyReadyCmd(){
 901   1          if(package[0]==0xd1)
 902   1              return true;
 903   1          else
 904   1              return false;
 905   1       }
 906          
 907           /*
 908          ***************************************************
 909          *   check whether the receive package is
 910          *   "Write key not ready" command 
 911          ***************************************************
 912          */
 913          
 914           BOOL IsWriteKeyNotReadyCmd(){
 915   1          if(package[0]==0xd2)
 916   1              return true;
 917   1          else
C51 COMPILER V7.02b   DISK3                                                                03/16/2006 13:52:41 PAGE 16  

 918   1              return false;
 919   1       }
 920          
 921           /*
 922          ***************************************************
 923          *   check whether the receive package is
 924          *   "Write CustomId ready" command 
 925          ***************************************************
 926          */
 927          
 928           BOOL IsWriteCustomIdReadyCmd(){
 929   1          if(package[0]==0xd3)
 930   1              return true;
 931   1          else
 932   1              return false;
 933   1       }
 934          
 935           /*
 936          ***************************************************
 937          *   check whether the receive package is
 938          *   "Write CustomId not ready" command 
 939          ***************************************************
 940          */
 941          
 942           BOOL IsWriteCustomIdNotReadyCmd(){
 943   1          if(package[0]==0xd4)
 944   1              return true;
 945   1          else
 946   1              return false;
 947   1       }
 948          /*
 949          **************************************************
 950          * prototype :   BOOL ResetCmd()
 951          * Des: send "Reset" command on SPI or I2C bus
 952          * If comunication succeed,it return true;
 953          *If comunication is not success,it return false and errcode is 2.
 954          * Use GetLastError to determine. 
 955          ***************************************************
 956          */
 957           BOOL  ResetCmd(){              //
 958   1         return(GetStatus(0xe1));
 959   1              }       
 960          
 961          
 962          /*
 963          **************************************************
 964          * prototype :   BOOL PowerDownCmd()
 965          * Des: send "Power down" command on SPI or I2C bus
 966          * If comunication succeed,it return true;
 967          *If comunication is not success,it return false and errcode is 2.
 968          * Use GetLastError to determine. 
 969          ***************************************************
 970          */
 971           BOOL  PowerDownCmd() {
 972   1         return(GetStatus(0xf1));
 973   1       }
 974          
 975          
 976          /*
 977          *********************************************************
 978                          flow command class interface
 979          *********************************************************
C51 COMPILER V7.02b   DISK3                                                                03/16/2006 13:52:41 PAGE 17  

 980          */
 981          
 982          /*
 983          **************************************************
 984          * prototype :   BOOL WriteCustomIdOpr(BYTE * KEY,BYTE len,BYTE Nvmoffset)
 985          * Des: send "Write Custom Id" command on SPI or I2C bus
 986          * here "CId" is the poniter of len bytes array.
 987          * "Nvmoffset" is the offset in NVM 
 988          * If Receive state is ready,it return true;
 989          * If Receive state is not ready,it return false and errcode is 0.
 990          *If Receive state is neither above,it return false and errcode is not 0.
 991          * Use GetLastError to determine. 
 992          ***************************************************
 993          */
 994          
 995          #define WriteCustomIdCmdErr  0x21
 996          #define GetCustomIdWriteStatusCmdErr 0x22
 997          #define ReceiveFromT8Err  0x23 
 998          
 999          extern     void  Delay4_2ms(BYTE nCnt);
1000           BOOL WriteCustomIdOpr(BYTE * CId,BYTE len,BYTE Nvmoffset)    //add
1001           {
1002   1         BYTE   readcnt=1;
1003   1         BOOL  b_retry; 
1004   1      
1005   1         if( len+Nvmoffset>0x80)
1006   1         {
1007   2                    errcode=PRAMWRONG;
1008   2                      return false;
1009   2           }  
1010   1      
1011   1        
1012   1                 while(len>=8)
1013   1                 {
1014   2                         if(!WriteCustomIdCmd(CId,8,Nvmoffset))
1015   2                         {
1016   3                             errcode=WriteCustomIdCmdErr;
1017   3                              return false;
1018   3                              }
1019   2                         
1020   2                         CId+=8;
1021   2                         Nvmoffset+=8;
1022   2                         len-=8;      
1023   2                         MCU_SN++;
1024   2                         Delay4_2ms(8*3);
1025   2                 }
1026   1      
1027   1                 if(len>0)
1028   1                 {
1029   2                         if(!WriteCustomIdCmd(CId,len,Nvmoffset))
1030   2                         {
1031   3                             errcode=WriteCustomIdCmdErr;
1032   3                              return false;
1033   3                              }           
1034   2                         MCU_SN++;
1035   2                         Delay4_2ms(len*3);
1036   2                 }
1037   1                 
1038   1                 while(!CommMode)
1039   1                 {
1040   2                    // Delay_0_5us(10);       
1041   2                     if(!GetCustomIdWriteStatusCmd())
C51 COMPILER V7.02b   DISK3                                                                03/16/2006 13:52:41 PAGE 18  

1042   2                     {
1043   3                        errcode=GetCustomIdWriteStatusCmdErr;
1044   3                        goto  WriteCustomIdExit;
1045   3                     }
1046   2                         
1047   2                     //Delay_0_5us(10);       
1048   2                     b_retry=!GetReply(10);
1049   2                        
1050   2                     b_retry=(b_retry||(!IsWriteCustomIdReadyCmd()&&(!IsWriteCustomIdNotReadyCmd())));        
1051   2                      b_retry=b_retry&&(readcnt<3);           
1052   2                    if(b_retry)
1053   2                           readcnt++; 
1054   2                    else
1055   2                          break;        
1056   2                 }
1057   1      
1058   1                 if(CommMode)
1059   1                 {
1060   2                      GetCustomIdWriteStatusCmd();
1061   2                      while(1)
1062   2                      {
1063   3                             b_retry=!GetReply(10);             
1064   3                             b_retry=(b_retry||(!IsWriteCustomIdReadyCmd()&&(!IsWriteCustomIdNotReadyCmd())));        
1065   3                              b_retry=b_retry&&(readcnt<3);           
1066   3                            if(b_retry)
1067   3                                   readcnt++; 
1068   3                            else
1069   3                                  break;        
1070   3                      
1071   3                      }
1072   2                 }
1073   1                 
1074   1                   if(errcode!=0)          //=2 ,3
1075   1                      goto  WriteCustomIdExit;
1076   1                              
1077   1                  if(IsWriteCustomIdReadyCmd())
1078   1                  {
1079   2                       return true;   
1080   2                      }
1081   1                  if(IsWriteCustomIdNotReadyCmd())
1082   1                   { 
1083   2                       MCU_SN=package[1];
1084   2                       return false;
1085   2                      }
1086   1                  else
1087   1                           errcode=CMDERR;
1088   1                   
1089   1                WriteCustomIdExit:
1090   1                                      MCU_SN--;
1091   1                    if(MCU_SN==0xff)
1092   1                                SN_High--;
1093   1                       return false;
1094   1       
1095   1       }
1096          
1097          #define ChallengeCustomIdCmdErr      0x21
1098          #define GetCustomIdResponseCmdErr 0x22
1099          
1100          BOOL ChallengeCustomIdOpr(BYTE * CId,BYTE len,BYTE Nvmoffset )  //add
1101          {
1102   1         BYTE  readcnt=1;
1103   1         BOOL  b_retry;
C51 COMPILER V7.02b   DISK3                                                                03/16/2006 13:52:41 PAGE 19  

1104   1         BYTE  from[8];
1105   1      
1106   1        BYTE * iCId=CId;
1107   1        BYTE  lenbak=len;
1108   1        
1109   1         if( len+Nvmoffset>0x80)
1110   1         {
1111   2                    errcode=PRAMWRONG;
1112   2                      return false;
1113   2           }  
1114   1                      
1115   1                if(lenbak>=8)
1116   1                {
1117   2                         if(!ChallengeCustomIdCmd(iCId,8,Nvmoffset)) 
1118   2                         {
1119   3                               errcode=ChallengeCustomIdCmdErr;
1120   3                               return false;
1121   3                         }
1122   2                         iCId+=8;
1123   2                         Nvmoffset+=8;
1124   2                         lenbak=8;    
1125   2                         MCU_SN++;

⌨️ 快捷键说明

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