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

📄 user_ds2431.cpp

📁 最最常用的加密芯片DS2413的windowsce下驱动
💻 CPP
字号:
#ifdef TMEX

/* Local Function Prototypes */
unsigned char _fastcall Open_Com_Session(void);
short _fastcall FindDevice(void);
short _fastcall FindFirstFamily(short);
uchar _fastcall dowcrc(uchar);
ushort _fastcall docrc16(ushort);
void _fastcall ROMIDtoString(char *, short);
void _fastcall ByteToString(uchar, char *);

int _fastcall WritePages(uchar *);
int _fastcall CopyScratch(short);
int _fastcall WriteScratch(short, uchar *);
int _fastcall ReadScratch(short, uchar *);

int _fastcall ReadMemory(uchar *);

/*----------------------------------------------------------------------
 * Find a valid 1-wire adapter: COM, USB, Parallel
 *
 */ 
unsigned char _fastcall Open_Com_Session(void)
{
   unsigned char result=FALSE;
   unsigned char type[4]={5,1,6,2};    //1-Wire adapter type
   short i,j;
     /* attempt to get a session */
     for(j=0;j<4;j++)
     {
       PortType=type[j];
       for( i=1;i<=3;i++)
       {
         PortNum=i;
         SHandle = TMExtendedStartSession(PortNum,PortType,NULL);
         if (SHandle > 0)
         {
         /* reset the session prompt */
            result = TMSetup(SHandle);
            if( result==1 )
            {
              SetupDone = true;
              break;
            }
            else  TMEndSession(SHandle);
         }
       }
       if( i!=4 ) break;
      }
      return result;
}
/*----------------------------------------------------------------------
 * Find all of the components in a weather station:
 *  - DS18B20
 *  - DS2408
 */                                                          
short _fastcall FindDevice(void)
{    
   short i,j,desired_types[] = { DS2431_FAM, DS2401_FAM,DS2502_FAM };
   char temp_zstr[256];

   /* loop to find each type */
   for (i = 0; i < 1; i++)
   {
      if (FindFirstFamily(desired_types[i]))
      {
         /* copy the rom number */
         DeviceROM[i][0] = 0;
         TMRom(SHandle,state_buffer,&DeviceROM[i][0]);
      }
      else
      {
 	return FALSE;
       }
    }

   /* found all three types */
   return TRUE;
}

/*----------------------------------------------------------------------
 * Find the first device with a particular family
 */
short _fastcall FindFirstFamily(short family)
{
//   short ROM[9];
   short i;
   uchar rd_buf[50];
    for(i=0;i<15360;i++) state_buffer[i]=0;
   /* set up to find the first device with family 'family */
   if (TMFamilySearchSetup(SHandle,&state_buffer,family) == 1)
   { 
      /* get first device in list with the specified family */
      if (TMNext(SHandle,state_buffer) == 1)
      {
         /* read the rom number */
         ROM[0] = 0;
         TMRom(SHandle,state_buffer,&ROM[0]);
         
         /* check if correct type */
         if ((family & 0x7F) == (ROM[0] & 0x7F)) return TRUE;
      
      }
   }
     
   /* failed to find device of that type */
   return FALSE;
}


//--------------------------------------------------------------------------
// Write specified number of pages
//    
// Returns: TRUE (1) success
//          FALSE (0) failed
//
//-------
int _fastcall WritePages(unsigned char *pbuf)
{
  short i,j;
  unsigned char TA1, TA2, ES;
  uchar   dbuf[160];

  TMRom(SHandle,state_buffer,&DeviceROM[DS2431_ORDER][0]);
  for(i=0;i<16;i++)
  {

   /* access the device */
   if (TMAccess(SHandle,&state_buffer) != 1)
      return FALSE;

   /* send the write scratchpad command */
   TA1=(i*32)%256; TA2=(i*32)/256;          	//Control register address starting at 0x0201
   TMTouchByte(SHandle,0x0f);          //write scratchpad command
   TMTouchByte(SHandle,TA1);          //TA1 address
   TMTouchByte(SHandle,TA2);          //TA2 address
   for(j=0;j<32;j++)  TMTouchByte(SHandle,pbuf[i*32+j]);  //write memory data

//reset the 1-wire bus and access the DS1994 again
 if (TMAccess(SHandle,&state_buffer) != 1)   return FALSE;
   ES=31;
   TMTouchByte(SHandle,0xaa);          //read scratchpad command
   if( TMTouchByte(SHandle,0xff)!=TA1) return false;
   if( TMTouchByte(SHandle,0xff)!=TA2) return false;
   if( TMTouchByte(SHandle,0xff)!=ES) return false;
   for( j=0;j<32;j++)
   {
        if( TMTouchByte(SHandle,0xff)!=pbuf[i*32+j] ) return false;
   }

//reset the 1-wire bus and access the DS1994 again
 if (TMAccess(SHandle,&state_buffer) != 1)   return FALSE; 
   TMTouchByte(SHandle,0x55);          //copy scratchpad command
   TMTouchByte(SHandle,TA1);          //TA1 address
   TMTouchByte(SHandle,TA2);          //TA2 address
   TMTouchByte(SHandle,ES);          //E/S register
   TMTouchByte(SHandle,0xff);	     //dull read operation
   if( TMTouchByte(SHandle,0xff)!=0x00) return false; 
  }
  return true;

}
//--------------------------------------------------------------------------
// Read scratch
//    
// Returns: TRUE (1) success
//          FALSE (0) failed
//
int _fastcall ReadScratch(short TargetAddress, uchar *buf)
{
  short i;
  uchar ch, pbuf[20]; 
  TMRom(SHandle,state_buffer,&DeviceROM[DS2431_ORDER][0]);
//reset the 1-wire bus and access the DS2431 again
  if (TMAccess(SHandle,&state_buffer) != 1)   return FALSE; 
   TMTouchByte(SHandle,0xaa);          //read scratchpad command
   if( TMTouchByte(SHandle,0xff)!=(TargetAddress%256) ) return false;  //target address LSB
   if( TMTouchByte(SHandle,0xff)!=0x00) return false;			//target address MSB
   if( TMTouchByte(SHandle,0xff)!=0x07) return false;			//E/S byte
   for( i=0;i<8;i++)
   {
        if( TMTouchByte(SHandle,0xff)!=buf[i] ) return false;
   }
//read two bytes of crc16
   TMTouchByte(SHandle,0xff);
   TMTouchByte(SHandle,0xff);
   return true;
}



//--------------------------------------------------------------------------
// Write scratch
//    
// Returns: TRUE (1) success
//          FALSE (0) failed
//
int _fastcall WriteScratch(short TargetAddress, uchar *buf)
{
  short i,cnt=0;
  uchar ch, pbuf[20];
  TMRom(SHandle,state_buffer,&DeviceROM[DS2431_ORDER][0]);
//reset the 1-wire bus and access the DS2431 again
  if (TMAccess(SHandle,&state_buffer) != 1)   return FALSE;
// construct a packet to send
  pbuf[cnt++] = 0x0F; // write scratch command
  pbuf[cnt++] = TargetAddress%256; // address LSB
  pbuf[cnt++] = 0x00; 		   // address MSB

// data to write
  for (i = 0; i < 8; i++) pbuf[cnt++] = (uchar)(buf[i]);

// run the CRC over this part
  CRC16 = 0;
  for (i = 0; i < cnt; i++) docrc16(pbuf[i]);

// for reading crc bytes
  pbuf[cnt++] = 0xFF;
  pbuf[cnt++] = 0xFF;

// perform the block writing and reading 2 bytes of CRC16
  if (TMBlockStream(SHandle,pbuf,(short)(cnt)) != (short)(cnt)) return FALSE;

// check CRC
  for (i = cnt - 2; i < cnt; i++) docrc16(pbuf[i]);

// return result of inverted CRC
   return (CRC16 == 0xB001); //not 0 because that the calculating result is CRC16 and the reading result is inverted CRC16
}


//--------------------------------------------------------------------------
// Read memory (no checking)
//
// Returns: TRUE (1) success
//          FALSE (0) failed
//
int _fastcall ReadMemory(uchar *buf)
{
  short i;
  TMRom(SHandle,state_buffer,&DeviceROM[DS2431_ORDER][0]);
//reset the 1-wire bus and access the DS2431 again
 if (TMAccess(SHandle,&state_buffer) != 1)   return FALSE; 
   TMTouchByte(SHandle,0xf0);          //Read memory command
   TMTouchByte(SHandle,0x00);          //TA1 address
   TMTouchByte(SHandle,0x00);          //TA2 address
   for( i=0;i<0x90;i++) buf[i]=TMTouchByte(SHandle,0xff);
   return true;
}
//--------------------------------------------------------------------------
// Copy scratch
//
// Returns: TRUE (1) success
//          FALSE (0) failed

int _fastcall CopyScratch(short TargetAddr)
{
  short i;
  uchar ch;
  TMRom(SHandle,state_buffer,&DeviceROM[DS2431_ORDER][0]);
//reset the 1-wire bus and access the DS2431 again
  if (TMAccess(SHandle,&state_buffer) != 1)   return FALSE;
  TMTouchByte(SHandle,0x55);     // write scratch command
  TMTouchByte(SHandle, TargetAddr%256 );     // write target address LSB
  TMTouchByte(SHandle, 0x00 );     // write target address MSB
// prepare the strong pullup after next TMTouchByte
  TMOneWireLevel(SHandle,LEVEL_SET,LEVEL_STRONG_PULLUP,PRIMED_BYTE);
  TMTouchByte(SHandle, 0x07 );     // write E/S byte
  // sleep for a 13 ms
  Sleep(13);
  // disable the strong pull-up
  TMOneWireLevel(SHandle,LEVEL_SET,LEVEL_NORMAL,PRIMED_NONE);

  // do one more byte to see if it is complete
  ch = (uchar)TMTouchByte(SHandle,0xFF);

  // return true if it is toggling
  return ((ch == 0xAA) || (ch == 0x55));
}





/*--------------------------------------------------------------------------
 * Update the Dallas Semiconductor One Wire CRC (CRC8) from the global
 * variable CRC8 and the argument.  Return the updated CRC8.
 */
uchar dscrc_table[] = {
        0, 94,188,226, 97, 63,221,131,194,156,126, 32,163,253, 31, 65,
      157,195, 33,127,252,162, 64, 30, 95,  1,227,189, 62, 96,130,220,
       35,125,159,193, 66, 28,254,160,225,191, 93,  3,128,222, 60, 98,
      190,224,  2, 92,223,129, 99, 61,124, 34,192,158, 29, 67,161,255,
       70, 24,250,164, 39,121,155,197,132,218, 56,102,229,187, 89,  7,
      219,133,103, 57,186,228,  6, 88, 25, 71,165,251,120, 38,196,154,
      101, 59,217,135,  4, 90,184,230,167,249, 27, 69,198,152,122, 36,
      248,166, 68, 26,153,199, 37,123, 58,100,134,216, 91,  5,231,185,
      140,210, 48,110,237,179, 81, 15, 78, 16,242,172, 47,113,147,205,
       17, 79,173,243,112, 46,204,146,211,141,111, 49,178,236, 14, 80,
      175,241, 19, 77,206,144,114, 44,109, 51,209,143, 12, 82,176,238,
       50,108,142,208, 83, 13,239,177,240,174, 76, 18,145,207, 45,115,
      202,148,118, 40,171,245, 23, 73,  8, 86,180,234,105, 55,213,139,
       87,  9,235,181, 54,104,138,212,149,203, 41,119,244,170, 72, 22,
      233,183, 85, 11,136,214, 52,106, 43,117,151,201, 74, 20,246,168,
      116, 42,200,150, 21, 75,169,247,182,232, 10, 84,215,137,107, 53};
//--------
uchar _fastcall dowcrc(uchar x)
{
   CRC8 = dscrc_table[CRC8 ^ x];
   return CRC8;
}


/*--------------------------------------------------------------------------
 * Calculate a new CRC16 from the input data shorteger.  Return the current
 * CRC16 and also update the global variable CRC16.
 */
static short oddparity[16] = { 0, 1, 1, 0, 1, 0, 0, 1, 1, 0, 0, 1, 0, 1, 1, 0 };

ushort _fastcall docrc16(ushort data)
{
   data = (data ^ (CRC16 & 0xff)) & 0xff;
   CRC16 >>= 8;

   if (oddparity[data & 0xf] ^ oddparity[data >> 4])
     CRC16 ^= 0xc001;

   data <<= 6;
   CRC16   ^= data;
   data <<= 1;
   CRC16   ^= data;

   return CRC16;
}
//---------------------------------
//convert ROM ID to AnsiString
void _fastcall ROMIDtoString(char *str, short device)
{
  short k;
  for (k=0;k<8;k++)
  {
    sprintf(str+2*k+k/4,"%2.2X",DeviceROM[device][7-k]);
    if( k==3) sprintf(str+2*k+2,"-");
  }
}
//---------------------------------
//convert Byte to AnsiString  with displaying each bit
void _fastcall ByteToString(uchar byte, char *str)
{
   uchar i;
   uchar t[8]={0x80,0x40,0x20,0x10,0x08,0x04,0x02,0x01};
   sprintf(str,"0x%2.2X=",byte);
   for(i=0;i<8;i++)
   {
      if( (byte&t[i]) ) sprintf(str+strlen(str), "%c", '1');
      else   sprintf(str+strlen(str), "%c", '0');
      if(i==3)  sprintf(str+strlen(str), "%c", '-');
   }
}

 

//---------------------------------

#else
extern unsigned char _fastcall Open_Com_Session(void);
extern short _fastcall FindDevice(void);
extern short _fastcall FindFirstFamily(short);
extern uchar _fastcall dowcrc(uchar);
extern ushort _fastcall docrc16(ushort);
extern void _fastcall ROMIDtoString(char *, short);
extern void _fastcall ByteToString(uchar, char *);

extern int _fastcall WritePages(uchar *);
extern int _fastcall CopyScratch(short);
extern int _fastcall WriteScratch(short, uchar *);
extern int _fastcall ReadScratch(short, uchar *);

extern int _fastcall ReadMemory(uchar *);

#endif

⌨️ 快捷键说明

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