예제 #1
0
//----------------------------------------------------------------------
// Perform a scratchpad match using provided data. Fixed data length
// of 20 bytes.
//
// 'portnum'     - number 0 to MAX_PORTNUM-1.  This number is provided to
//                 indicate the symbolic port number.
// 'data'        - data to use in match scratch operation
// 'resume'      - if true, device access is resumed using the RESUME
//                 ROM command (0xA5).  Otherwise, a a MATCH ROM is
//                 used along with the device's entire address number.
//
// Return: TRUE - valid match
//         FALSE - no match or device not present
//
SMALLINT MatchScratchpadSHA18(int portnum, uchar* data, SMALLINT resume)
{
   short send_cnt=0;
   uchar send_block[50];
   int i;
   ushort lastcrc16;

   if(!resume)
   {
      // access the device
      OWASSERT( SelectSHA(portnum), OWERROR_ACCESS_FAILED, FALSE );
   }
   else
   {
      // transmit RESUME command
      send_block[send_cnt++] = ROM_CMD_RESUME;
   }

   setcrc16(portnum,0);
   // match scratchpad command
   send_block[send_cnt] = CMD_MATCH_SCRATCHPAD;
   lastcrc16 = docrc16(portnum,send_block[send_cnt++]);

   // send 20 data bytes
   for (i = 0; i < 20; i++)
   {
      send_block[send_cnt] = data[i];
      lastcrc16 = docrc16(portnum,send_block[send_cnt++]);
   }
   // send two crc bytes and verification byte
   //for (i = 0; i < 3; i++)
   //   send_block[send_cnt++] = 0xFF;
   memset(&send_block[send_cnt], 0x0FF, 3);
   send_cnt += 3;

   // now send the block
   OWASSERT( owBlock(portnum,resume,send_block,send_cnt),
             OWERROR_BLOCK_FAILED, FALSE );

   //\\//\\//\\//\\//\\//\\//\\//\\//\\//
   #ifdef DEBUG_DUMP
      debugout(send_block,send_cnt);
   #endif
   //\\//\\//\\//\\//\\//\\//\\//\\//\\//

   // check the CRC
   for (i = (send_cnt - 3); i < (send_cnt - 1); i++)
      lastcrc16 = docrc16(portnum,send_block[i]);

   // verify CRC16 is correct
   OWASSERT( lastcrc16==0xB001, OWERROR_CRC_FAILED, FALSE );

   // check verification
   if(send_block[send_cnt - 1] != (uchar)0xFF)
      return TRUE;
   else
      return FALSE;
}
예제 #2
0
//----------------------------------------------------------------------
// Read the counter on a specified page of a DS2423.
//
// 'portnum'  - number 0 to MAX_PORTNUM-1.  This number is provided to
//              indicate the symbolic port number.
// 'SerialNum'   - Serial Number of DS2423 that contains the counter
//                 to be read
// 'CounterPage' - page number that the counter is associated with
// 'Count'       - pointer to variable where that count will be returned
//
// Returns: TRUE(1)  counter has been read and verified
//          FALSE(0) could not read the counter, perhaps device is not
//                   in contact
//
SMALLINT ReadCounter(int portnum, int CounterPage,  unsigned long *Count)
{
   uchar rt=FALSE;
   uchar send_block[30];
   uchar send_cnt=0, i;
   int address;
   ushort lastcrc16;

   setcrc16(portnum,0);

   // set the device serial number to the counter device
/* 2/12/2003 [bcl] DigiTemp does this before calling the routine */
/*   owSerialNum(portnum,SerialNum,FALSE); */

   // access the device
   if (owAccess(portnum))
   {
      // create a block to send that reads the counter
      // read memory and counter command
      send_block[send_cnt++] = 0xA5;
      docrc16(portnum,0xA5);
      // address of last data byte before counter
      address = (CounterPage << 5) + 31;  // (1.02)
      send_block[send_cnt++] = (uchar)(address & 0xFF);
      docrc16(portnum,(ushort)(address & 0xFF));
      send_block[send_cnt++] = (uchar)(address >> 8);
      docrc16(portnum,(ushort)(address >> 8));
      // now add the read bytes for data byte,counter,zero bits, crc16
      for (i = 0; i < 11; i++)
         send_block[send_cnt++] = 0xFF;

      // now send the block
      if (owBlock(portnum,FALSE,send_block,send_cnt))
      {
         // perform the CRC16 on the last 11 bytes of packet
         for (i = send_cnt - 11; i < send_cnt; i++)
            lastcrc16 = docrc16(portnum,send_block[i]);

         // verify CRC16 is correct
         if (lastcrc16 == 0xB001)
         {
            // success
            rt = TRUE;
            // extract the counter value
            *Count = 0;
            for (i = send_cnt - 7; i >= send_cnt - 10; i--)
            {
               *Count <<= 8;
               *Count |= send_block[i];
            }
         }
      }
   }

   // return the result flag rt
   return rt;
}
예제 #3
0
int Write43(int portnum, uchar *SerialNum, uchar *page_buffer)
{
	uchar rt=FALSE;
	ushort lastcrc16;
	int  i;

	owSerialNum(portnum, SerialNum, FALSE);

	if(owAccess(portnum))
	{
		mprintf(" Writing Scratchpad...\n");
		if (!owWriteBytePower(portnum, WRITE_SCRATCH_CMD)) 
			return FALSE;
		setcrc16(portnum, 0);
		docrc16(portnum,(ushort)WRITE_SCRATCH_CMD);
		
		owLevel(portnum, MODE_NORMAL);
		owWriteBytePower(portnum, 0x00);	//write LSB of target addr
		docrc16(portnum,(ushort)0x00);
		owLevel(portnum, MODE_NORMAL);
		owWriteBytePower(portnum, 0x00);	//write MSB of target addr
		docrc16(portnum,(ushort)0x00);


		for(i = 0; i < 32; i++)			//write 32 data bytes to scratchpad 
		{	
			owLevel(portnum,MODE_NORMAL);
			owWriteBytePower(portnum, i);
			lastcrc16 = docrc16(portnum,i);

		}
		for(i = 0; i < 2; i++)			//read two bytes CRC16
		{
			owLevel(portnum,MODE_NORMAL);
			lastcrc16 = docrc16(portnum,(ushort)owReadBytePower(portnum));
		}
		mprintf(" CRC16: %x\n", lastcrc16);
		if(lastcrc16 == 0xb001)
		{
			//copy to mem
			owLevel(portnum, MODE_NORMAL);
			if(Copy2Mem43(portnum, SerialNum))
				rt=TRUE;
	
		}
	}

	return rt;
}
예제 #4
0
//----------------------------------------------------------------------
// Read the scratchpad with CRC16 verification for DS1963S.
//
// 'portnum'     - number 0 to MAX_PORTNUM-1.  This number is provided to
//                 indicate the symbolic port number.
// 'address'     - pointer to address that is read from scratchpad
// 'es'          - pointer to offset byte read from scratchpad
// 'data'        - pointer data buffer read from scratchpad
// 'resume'      - if true, device access is resumed using the RESUME
//                 ROM command (0xA5).  Otherwise, a a MATCH ROM is
//                 used along with the device's entire address number.
//
// Return: TRUE - scratch read, address, es, and data returned
//         FALSE - error reading scratch, device not present
//
//
SMALLINT ReadScratchpadSHA18(int portnum, int* address, uchar* es,
                           uchar* data, SMALLINT resume)
{
   short send_cnt=0;
   uchar send_block[40];
   SMALLINT i;
   ushort lastcrc16;

   if(!resume)
   {
      // access the device
      OWASSERT( SelectSHA(portnum), OWERROR_ACCESS_FAILED, FALSE );
   }
   else
   {
      // transmit RESUME command
      send_block[send_cnt++] = ROM_CMD_RESUME;
      resume = 1; // for addition later
   }

   // read scratchpad command
   send_block[send_cnt++] = CMD_READ_SCRATCHPAD;

   // now add the read bytes for data bytes and crc16
   //for (i = 0; i < 37; i++)
   //   send_block[send_cnt++] = 0xFF;
   memset(&send_block[send_cnt], 0x0FF, 37);
   send_cnt += 37;

   // now send the block
   OWASSERT( owBlock(portnum,resume,send_block,send_cnt),
             OWERROR_BLOCK_FAILED, FALSE );

   //\\//\\//\\//\\//\\//\\//\\//\\//\\//
   #ifdef DEBUG_DUMP
      debugout(send_block,send_cnt);
   #endif
   //\\//\\//\\//\\//\\//\\//\\//\\//\\//

   // calculate CRC16 of result
   setcrc16(portnum,0);
   for (i = resume; i < send_cnt ; i++)
      lastcrc16 = docrc16(portnum,send_block[i]);

   // verify CRC16 is correct
   OWASSERT( lastcrc16==0xB001, OWERROR_CRC_FAILED, FALSE );

   // copy data to return buffers
   if(address)
      *address = (send_block[2+resume] << 8) | send_block[1+resume];
   if(es)
      *es = send_block[3+resume];

   memcpy(data, &send_block[4+resume], 32);

   // success
   return TRUE;
}
예제 #5
0
// routine for reading a memory page of a DS28EC20P EEPROM
// expects 32 byte deep buffer
int ReadMem43(int portnum, uchar *SerialNum, uchar *page_buffer)
{
	uchar rt=FALSE;
	ushort lastcrc16; 
	int i;
	uchar read_data;
	
	owSerialNum(portnum, SerialNum, FALSE);

	if(owAccess(portnum))
	{	
		if (!owWriteBytePower(portnum, E_READ_MEM_CMD)) 
			return FALSE;

		setcrc16(portnum, 0);				//init crc
		docrc16(portnum,(ushort)E_READ_MEM_CMD);
		
		owLevel(portnum, MODE_NORMAL);
		owWriteBytePower(portnum, 0x00);	//write LSB of target addr
		docrc16(portnum,(ushort)0x00);
		owLevel(portnum, MODE_NORMAL);
		owWriteBytePower(portnum, 0x00);	//write MSB of target addr
		docrc16(portnum,(ushort)0x00);
		
		for(i = 0; i < 32; i++) 
		{	
			owLevel(portnum,MODE_NORMAL);
			page_buffer[i] = owReadBytePower(portnum);
			lastcrc16 = docrc16(portnum, page_buffer[i]);
		}
		
		for(i = 0; i < 2; i++)
		{
			owLevel(portnum,MODE_NORMAL);		
			read_data = owReadBytePower(portnum);
			lastcrc16 = docrc16(portnum, read_data);
		}	
		if (lastcrc16 == 0xb001)			
			rt=TRUE;
	}

	return rt;
}
예제 #6
0
// routine for reading the scratchpad of a DS28EC20P EEPROM
// 80 pages of 32byte
// 32byte scratchpad
// expects 32 byte deep buffer
int ReadScratch43(int portnum, uchar *SerialNum, uchar *page_buffer)
{
	uchar rt=FALSE;
	ushort lastcrc16; 
	int i;
	ushort target_addr = 0;
	uchar read_data;
	
	owSerialNum(portnum, SerialNum, FALSE);

	if(owAccess(portnum))
	{	
		mprintf(" Reading Scratchpad...\n");
		if (!owWriteBytePower(portnum, READ_SCRATCH_CMD)) 
			return FALSE;

		setcrc16(portnum, 0);				//init crc
		docrc16(portnum,(ushort)READ_SCRATCH_CMD);
		
					
		
		owLevel(portnum,MODE_NORMAL);		//read 2 byte address and 1 byte status
		read_data = owReadBytePower(portnum);
		lastcrc16 = docrc16(portnum, read_data);
		target_addr = read_data;
 
		owLevel(portnum,MODE_NORMAL);		
		read_data = owReadBytePower(portnum);
		lastcrc16 = docrc16(portnum, read_data);
		target_addr |= read_data << 8;

		owLevel(portnum,MODE_NORMAL);		
		read_data = owReadBytePower(portnum);
		lastcrc16 = docrc16(portnum, read_data);

		mprintf("E/S: 0x%x\n", read_data);
		for(i = 0; i < 32; i++) 
		{	
			owLevel(portnum,MODE_NORMAL);
			page_buffer[i] = owReadBytePower(portnum);
			lastcrc16 = docrc16(portnum, page_buffer[i]);
		}
		
		for(i = 0; i < 2; i++)
		{
			owLevel(portnum,MODE_NORMAL);		
			read_data = owReadBytePower(portnum);
			lastcrc16 = docrc16(portnum, read_data);
		}	
		if (lastcrc16 == 0xb001)			
			rt=TRUE;
	}

	return rt;
}
예제 #7
0
//----------------------------------------------------------------------
//  This is the Main routine for swtmain1c
//
int main(short argc, char **argv)
{
   char msg[200];
   uchar data[256];
   int portnum = 0;
   int n=0;
   int addr = 0;
   int len;
   uchar es = 0x00;
   uchar address[2];
   ushort i;
   uchar sn[8],state[3], reg[3], send_block[37];
   uchar family[2][8];
   int done=FALSE; 
   int channel=0,send_cnt=0;
   SMALLINT alternate=TRUE, alt=FALSE;
   ushort lastcrc16;
   uchar latch, set;
   uchar check;

   // check for required port name
   if (argc != 2)
   {
      sprintf(msg,"1-Wire Net name required on command line!\n"
                  " (example: \"COM1\" (Win32 DS2480),\"/dev/cua0\" "
                  "(Linux DS2480),\"1\" (Win32 TMEX)\n");
      printf("%s",msg);
      return 0;
   }

   if((portnum = owAcquireEx(argv[1])) < 0)
   {
      printf("Did not Acquire port.\n",1);
      exit(1);
   }
   else
   {
      if(FindDevices(portnum,&family[0],0x1C,1))
      {
         for(i=0;i<8;i++)
            sn[i] = family[0][i];

         printf("device found: ");
         for(i=0;i<8;i++)
            printf("%02X ",sn[i]);
         printf("\n");

         do
         {
            printf("PICK AN OPERATION:\n\n");
            printf("(1)  Read Channel state\n");             // Gives channel information

            printf("(2)  Set Channel On/Off\n");             // Sets channel

            printf("(3)  Read Channel Mask\n");              // Read Selection Mask

            printf("(4)  Set Channel mask\n");               // Sets channel mask

            printf("(5)  Read Channel Polarity\n");          // Read Polarity Selection

            printf("(6)  Set Channel polarity\n");           // sets channel polarity

            printf("(7)  Read Control/Status Register\n");   // Read Control/Status Reg

            printf("(8)  Set Reset Mode On/Off\n");          // Set Reset Mode

            printf("(9)  Clear Power on Reset\n");           // Clear Power on reset

            printf("(10) Get VCC state\n");                  // Get VCC state

            printf("(11) Set OR conditional search\n");      // or condition search

            printf("(12) Set AND conditional search\n");     // and condition search

            printf("(13) Write Scratchpad\n");               // write scratchpad command

            printf("(14) Read Scratchpad\n");                // read scratchpad command

            printf("(15) Copy Scratchpad\n");                // copy scratchpad command

            printf("(16) Read Memory\n");                    // read memory

            printf("(17) PIO access read with CRC confirmation\n");  // access read

            printf("(18) LED test\n");                       // LED test

            printf("(19) QUIT\n");

            scanf("%d",&n);
            if(n == 19)
            {
               n = 0;        //used to finish off the loop
               done = TRUE;
               break;
            }

            switch(n)
            {
               case 1:  // Channel Info
                  printf("\nEnter the channel\n");
                  scanf("%d",&channel);

                  if(readSwitch1C(portnum,&sn[0],&state[0]))
                  {
                     printf("The channel is ");
                     if(getLatchState1C(channel,&state[0]))
                        printf("on.\n");
                     else
                        printf("off\n");
                     printf("The Level is ");
                     if(getLevel1C(channel,&state[0]))
                        printf("high.\n");
                     else
                        printf("low.\n");
                     if(getSensedActivity1C(channel,&state[0]))
                        printf("Activity was detected on the channel.\n\n");
                     else
                        printf("No activity was detected on the channel.\n\n");
                  }
                  else
                     OWERROR_DUMP(stdout);

                  break;

               case 2:  // Sets channel
                  printf("\nEnter the channel\n");
                  scanf("%d",&channel);
                  printf("Turn channel off enter 0, on 1.\n");
                  scanf("%d",&set);

                  if(setLatchState1C(portnum,&sn[0],channel,set))
                  {
                     printf("Latch was set ");
                     if(set)
                        printf("on.\n");
                     else
                        printf("off.\n");
                  }
                  else
                     OWERROR_DUMP(stdout);

                  break;

               case 3:  // Read Selection Mask
                  printf("\nEnter the channel\n");
                  scanf("%d",&channel);

                  if(readRegister1C(portnum,&sn[0],&reg[0]))
                  {
                     printf("register is %02X %02X %02X\n",reg[0],reg[1],reg[2]);
                     printf("The Selection Mask for channel %d is ",channel);

                     latch = (uchar) (0x01 << channel);
                    
                     if((reg[0] & latch) == latch)
                        printf("set.\n\n");
                     else
                        printf("not set.\n\n");
                  }
                  else
                     OWERROR_DUMP(stdout);

                  break;

               case 4:  // Sets channel mask
                  printf("\nEnter the channel\n");
                  scanf("%d",&channel);
                  printf("Turn channel mask off enter 0, on 1.\n");
                  scanf("%d",&set);

                  if(setChannelMask1C(portnum,&sn[0],channel,set))
                  {
                     printf("The mask for channel %d was set ",channel);
                     if(set)
                        printf("on.\n\n");
                     else
                        printf("off.\n\n");
                  }
                  else
                     OWERROR_DUMP(stdout);

                  break;

               case 5:  // Read Polarity Selection 
                  printf("\nEnter the channel\n");
                  scanf("%d",&channel);

                  printf("The Polarity for channel %d is ",channel);

                  if(getChannelPolarity1C(portnum,&sn[0],channel))
                     printf("set.\n\n");
                  else
                     printf("not set.\n\n");

                  break;

               case 6:  // sets channel polarity
                  printf("\nEnter the channel\n");
                  scanf("%d",&channel);
                  printf("Turn channel polarity off enter 0, on 1.\n");
                  scanf("%d",&set);

                  if(setChannelPolarity1C(portnum,&sn[0],channel,set))
                  {
                     printf("The polarity for channel %d was set ",channel);
                     if(set)
                        printf("on.\n\n");
                     else
                        printf("off.\n\n");
                  }
                  else
                     OWERROR_DUMP(stdout);

                  break;

               case 7:  // Read Control/Status Reg
                  if(readRegister1C(portnum,&sn[0],&reg[0]))
                     printf("The Constrol/Status register is as following in hex %02X\n\n",
                             reg[2]);
                  else
                     OWERROR_DUMP(stdout);

                  break;

               case 8:  // Set Reset Mode
                  printf("Turn reset mode off enter 0, on 1.\n");
                  scanf("%d",&set);

                  if(setResetMode1C(portnum,&sn[0],set))
                  {
                     printf("Reset Mode was turned ");
                     if(set)
                        printf("on.\n\n");
                     else
                        printf("off.\n\n");
                  }
                  else
                     OWERROR_DUMP(stdout);

                  break;

               case 9:  // Clear Power on reset
                  if(clearPowerOnReset1C(portnum,&sn[0]))
                     printf("Power on reset was cleared.\n\n");
                  else
                     OWERROR_DUMP(stdout);

                  break;

               case 10:  // Get VCC state
                  if(readRegister1C(portnum,&sn[0],&reg[0]))
                  {
                     printf("VCC state register is %02X\n",reg[2]);
                     if(getVCC1C(&reg[0]))
                        printf("VCC is powered.\n\n");
                     else
                        printf("VCC is grounded.\n\n");
                  }
                  else
                     OWERROR_DUMP(stdout);

                  break;

               case 11:  // or condition search
                  if(orConditionalSearch1C(portnum,&sn[0]))
                     printf("OR condition search was set.\n\n");
                  else
                     OWERROR_DUMP(stdout);

                  break;

               case 12:  // and condition search
                  if(andConditionalSearch1C(portnum,&sn[0]))
                     printf("AND condition search was set.\n\n");
                  else
                     OWERROR_DUMP(stdout);

                  break;

               case 13:  // write scratchpad
                  printf("Enter the address to start writing: ");

                  addr = getNumber(0, 550);

            		if(menuSelect(&sn[0]) == MODE_TEXT)
       					len = getData(data,MAX_LEN,MODE_TEXT);
        				else
        					len = getData(data,MAX_LEN,MODE_HEX);

                  if(!writeScratch1C(portnum,&sn[0],addr,len,&data[0]))
                     OWERROR_DUMP(stdout);
                  break;

               case 14:  // read scratchpad
                  if(!readScratch1C(portnum,&sn[0],&len,&es,&address[0],&data[0]))
                  {
                     printf("error\n");
                     OWERROR_DUMP(stdout);
                  }
                  else
                  {
                     printf("Address bytes:   %02X %02X\n",address[0],address[1]);
                     printf("ES byte:         %02X\n",es);
                     printf("Length:          %d\n",len);
                     printf("Scratchpad data: ");
                     for(i=0;i<len;i++)
                        printf("%02X ",data[i]);
                     printf("\n");
                  }

                  break;

               case 15:  // copy scratchpad
                  if(!copyScratch1C(portnum,&sn[0]))
                  {
                     OWERROR_DUMP(stdout);
                  }
                  else
                  {
                     printf("Copy Scratchpad Complete.\n");
                  }

                  break;

               case 16:  // read memory
                  printf("Enter the address to start reading: ");
                  addr = getNumber(0, 550);

                  printf("Enter the length you want to read: ");
                  len = getNumber(0,256);

                  if(!read1C(portnum,&sn[0],addr,len,&data[0]))
                  {
                     OWERROR_DUMP(stdout);
                  }
                  else
                  {
                     for(i=0;i<len;i++)
                        printf("%02X ",data[i]);
                     printf("\n");
                  }

                  break;

               case 17:  // and condition search
                  if (!owTouchReset(portnum))
                     OWERROR_DUMP(stdout);

                  if(!owWriteByte(portnum,0xCC))
                     printf("skip rom error.\n");

                  owWriteByte(portnum,0xF5);
                  for(i=0;i<34;i++)
                     send_block[send_cnt++] = 0xFF;
                  
                  if(!owBlock(portnum,FALSE,&send_block[0],send_cnt))
                  {
                     OWERROR(OWERROR_BLOCK_FAILED);
                     return FALSE;
                  }

                  setcrc16(portnum,0);
                  lastcrc16 = docrc16(portnum,0xF5);
                  for(i=0;i<34;i++)
                     lastcrc16 = docrc16(portnum,send_block[i]);
                  if(lastcrc16 != 0xB001)
                     printf("CRC didn't match.\n");


                  printf("read data: ");
                  for(i=0;i<34;i++)
                     printf("%02X ",send_block[i]);
                  printf("\n");

                  send_cnt = 0;
                  for(i=0;i<34;i++)
                     send_block[send_cnt++] = 0xFF;
                  
                  if(!owBlock(portnum,FALSE,&send_block[0],send_cnt))
                  {
                     OWERROR(OWERROR_BLOCK_FAILED);
                     return FALSE;
                  }

                  setcrc16(portnum,0);
                  for(i=0;i<34;i++)
                     lastcrc16 = docrc16(portnum,send_block[i]);
                  if(lastcrc16 != 0xB001)
                     printf("CRC2 didn't match.\n");


                  printf("read data2: ");
                  for(i=0;i<34;i++)
                     printf("%02X ",send_block[i]);
                  printf("\n");

                  send_cnt = 0;
                  for(i=0;i<34;i++)
                     send_block[send_cnt++] = 0xFF;
                  
                  if(!owBlock(portnum,FALSE,&send_block[0],send_cnt))
                  {
                     OWERROR(OWERROR_BLOCK_FAILED);
                     return FALSE;
                  }

                  setcrc16(portnum,0);
                  for(i=0;i<34;i++)
                     lastcrc16 = docrc16(portnum,send_block[i]);
                  if(lastcrc16 != 0xB001)
                     printf("CRC3 didn't match.\n");


                  printf("read data3: ");
                  for(i=0;i<34;i++)
                     printf("%02X ",send_block[i]);
                  printf("\n");

                  break;

               case 18:  // LED test
                  printf("\nEnter the channel to turn the LED on.\n");
                  scanf("%d",&channel);
                  printf("Turn reset mode off enter 1, on 0.\n");
                  scanf("%d",&set);
                  printf("Alternate on and off 0=No, 1=Yes.\n");
                  scanf("%d",&alternate);

                  if (!owTouchReset(portnum))
                     OWERROR_DUMP(stdout);

                  if(!owWriteByte(portnum,0xCC))
                     printf("skip rom error.\n");

                  owWriteByte(portnum,0x5A);

                  for(i=0;i<256;i++)
                  {
                     if(channel == 0)
                     {
                        if(set == 0)
                        {
                           if(!alternate)
                           {
                              if(!owWriteByte(portnum,0xFE))
                                 printf("write byte error.\n");
                              if(!owWriteByte(portnum,0x01))
                                 printf("write byte error.\n");
                              check = 0xFE;
                           }
                           else
                           {
                              if(alt)
                              {
                                 if(!owWriteByte(portnum,0xFE))
                                    printf("write byte error.\n");
                                 if(!owWriteByte(portnum,0x01))
                                    printf("write byte error.\n");
                                 check = 0xFE;
                                 alt = FALSE;
                              }
                              else
                              {
                                 if(!owWriteByte(portnum,0xFF))
                                    printf("write byte error.\n");
                                 if(!owWriteByte(portnum,0x00))
                                    printf("write byte error.\n");
                                 check = 0xFF;
                                 alt = TRUE;
                              }
                           }
                        }
                        else
                        {
                           if(!alternate)
                           {
                              if(!owWriteByte(portnum,0xFF))
                                 printf("write byte error.\n");
                              if(!owWriteByte(portnum,0x00))
                                 printf("write byte error.\n");
                              check = 0xFF;
                           }
                           else
                           {
                              if(alt)
                              {
                                 if(!owWriteByte(portnum,0xFE))
                                    printf("write byte error.\n");
                                 if(!owWriteByte(portnum,0x01))
                                    printf("write byte error.\n");
                                 check = 0xFE;
                                 alt = FALSE;
                              }
                              else
                              {
                                 if(!owWriteByte(portnum,0xFF))
                                    printf("write byte error.\n");
                                 if(!owWriteByte(portnum,0x00))
                                    printf("write byte error.\n");
                                 check = 0xFF;
                                 alt = TRUE;
                              }
                           }
                        }
                     }
                     else
                     {
                        if(set == 0)
                        {
                           if(!alternate)
                           {
                              if(!owWriteByte(portnum,0xFD))
                                 printf("write byte error.\n");
                              if(!owWriteByte(portnum,0x02))
                                 printf("write byte error.\n");
                              check = 0xFD;
                           }
                           else
                           {
                              if(alt)
                              {
                                 if(!owWriteByte(portnum,0xFD))
                                    printf("write byte error.\n");
                                 if(!owWriteByte(portnum,0x02))
                                    printf("write byte error.\n");
                                 check = 0xFD;
                                 alt = FALSE;
                              }
                              else
                              {
                                 if(!owWriteByte(portnum,0xFF))
                                    printf("write byte error.\n");
                                 if(!owWriteByte(portnum,0x00))
                                    printf("write byte error.\n");
                                 check = 0xFF;
                                 alt = TRUE;
                              }
                           }
                        }
                        else
                        {
                           if(!alternate)
                           {
                              if(!owWriteByte(portnum,0xFF))
                                 printf("write byte error.\n");
                              if(!owWriteByte(portnum,0x00))
                                 printf("write byte error.\n");
                              check = 0xFF;
                           }
                           else
                           {
                              if(alt)
                              {
                                 if(!owWriteByte(portnum,0xFD))
                                    printf("write byte error.\n");
                                 if(!owWriteByte(portnum,0x02))
                                    printf("write byte error.\n");
                                 check = 0xFD;
                                 alt = FALSE;
                              }
                              else
                              {
                                 if(!owWriteByte(portnum,0xFF))
                                    printf("write byte error.\n");
                                 if(!owWriteByte(portnum,0x00))
                                    printf("write byte error.\n");
                                 check = 0xFF;
                                 alt = TRUE;
                              }
                           }
                        }
                     }

                     send_block[0] = (uchar)owReadByte(portnum);

                     send_block[1] = (uchar)owReadByte(portnum);

                     if((send_block[0] != 0xAA) && (send_block[1] != check))
                        printf("confirmation byte was %02X and read back was %02X\n",
                                 send_block[0],send_block[1]);
                  }

               default:
                  break;
            }

         }while(!done);

      }
      else
         printf("DS28E04 not found on One Wire Network\n");

      owRelease(portnum);
   }

   return 1;
}
예제 #8
0
//----------------------------------------------------------------------
// Copies hidden scratchpad data into specified secret.  Resume command
// is used by default.
//
// 'portnum'      - number 0 to MAX_PORTNUM-1.  This number is provided to
//                  indicate the symbolic port number.
// 'secretnum'    - secret number to replace with scratchpad data.
//
// Return: TRUE - copy secret succeeded
//         FALSE - error or device not present
//
SMALLINT CopySecretSHA18(int portnum, SMALLINT secretnum)
{
   // change number of verification bytes if in overdrive
   SMALLINT num_verf = (in_overdrive[portnum&0x0FF]) ? 10 : 2;

   // each page has 4 secrets, so look at 2 LS bits to
   // determine offset in the page.
   SMALLINT secret_offset = (secretnum&3) << 3;

   // secrets 0-3 are stored starting at address 0200h
   // and 4-7 are stored starting at address 0220h.
   int address = (secretnum<4 ? 0x0200 : 0x0220)
                 + secret_offset;

   SMALLINT length = 32 - secret_offset;
   SMALLINT send_cnt = 0, i;
   uchar send_block[38];
   ushort lastcrc16;

   //Since other functions must be called before this one
   //that are communicating with the button, resume is assumed.
   send_block[send_cnt++] = ROM_CMD_RESUME;
   send_block[send_cnt++] = CMD_WRITE_SCRATCHPAD;
   send_block[send_cnt++] = (uchar)address;
   send_block[send_cnt++] = (uchar)(address>>8);

   //for(i=0; i<length+2; i++)
   //   send_block[send_cnt++] = (uchar)0x0FF;
   memset(&send_block[send_cnt], 0x0FF, 2+length);
   send_cnt += 2+length;


   // now send the block
   OWASSERT( owBlock(portnum,TRUE,send_block,send_cnt),
             OWERROR_BLOCK_FAILED, FALSE );

   //\\//\\//\\//\\//\\//\\//\\//\\//\\//
   #ifdef DEBUG_DUMP
      debugout(send_block,send_cnt);
   #endif
   //\\//\\//\\//\\//\\//\\//\\//\\//\\//

   // calculate CRC16 of result
   setcrc16(portnum,0);
   for (i = 1; i < send_cnt ; i++)
      lastcrc16 = docrc16(portnum,send_block[i]);

   // verify CRC16 is correct
   OWASSERT( lastcrc16==0xB001, OWERROR_CRC_FAILED, FALSE );

   // Now read TA1/TA2 and ES, but not rest of data;
   send_cnt = 1;
   send_block[send_cnt++] = CMD_READ_SCRATCHPAD;

   //for(i=0; i<3; i++)
   //   send_block[send_cnt++] = (uchar)0x0FF;
   memset(&send_block[send_cnt], 0x0FF, 3);
   send_cnt += 3;

   // now send the block to get TA1/TA2 and ES
   OWASSERT( owBlock(portnum,TRUE,send_block,send_cnt),
             OWERROR_BLOCK_FAILED, FALSE );

   //\\//\\//\\//\\//\\//\\//\\//\\//\\//
   #ifdef DEBUG_DUMP
      debugout(send_block,send_cnt);
   #endif
   //\\//\\//\\//\\//\\//\\//\\//\\//\\//

   // Use the same buffer to call copyScratchpad with proper
   // authorization bytes.
   send_block[1] = CMD_COPY_SCRATCHPAD;

   //for(i=0; i<num_verf; i++)
   //   send_block[send_cnt++] = (uchar)0x0FF;
   memset(&send_block[send_cnt], 0x0FF, num_verf);
   send_cnt += num_verf;

   // now send the block
   OWASSERT( owBlock(portnum,TRUE,send_block,send_cnt),
             OWERROR_BLOCK_FAILED, FALSE );

   //\\//\\//\\//\\//\\//\\//\\//\\//\\//
   #ifdef DEBUG_DUMP
      debugout(send_block,send_cnt);
   #endif
   //\\//\\//\\//\\//\\//\\//\\//\\//\\//

   // check verification
   OWASSERT( ((send_block[send_cnt-1] & 0xF0) == 0x50) ||
             ((send_block[send_cnt-1] & 0xF0) == 0xA0),
             OWERROR_NO_COMPLETION_BYTE, FALSE );

   return TRUE;
}
예제 #9
0
//----------------------------------------------------------------------
// Compute sha command based on control_byte and page address.
//
// 'portnum'      - number 0 to MAX_PORTNUM-1.  This number is provided to
//                  indicate the symbolic port number.
// 'control_byte' - control byte used in sha operation
// 'address'      - address used in compute sha operation
// 'resume'       - if true, device access is resumed using the RESUME
//                  ROM command (0xA5).  Otherwise, a a MATCH ROM is
//                  used along with the device's entire address number.
//
// Return: TRUE - compute sha finished
//         FALSE - CRC error, device not present
//
SMALLINT SHAFunction18(int portnum, uchar control_byte,
                    int address, SMALLINT resume)
{
   short send_cnt=0;
   uchar send_block[18];
   int i,num_verf;
   ushort lastcrc16;

   if(!resume)
   {
      // access the device
      OWASSERT( SelectSHA(portnum), OWERROR_ACCESS_FAILED, FALSE );
   }
   else
   {
      // transmit RESUME command
      send_block[send_cnt++] = ROM_CMD_RESUME;
   }

   setcrc16(portnum,0);
   // change number of verification bytes if in overdrive
   num_verf = (in_overdrive[portnum&0x0FF]) ? 10 : 2;

   // Compute SHA Command
   send_block[send_cnt] = CMD_COMPUTE_SHA;
   lastcrc16 = docrc16(portnum,send_block[send_cnt++]);
   // address 1
   send_block[send_cnt] = (uchar)(address & 0xFF);
   lastcrc16 = docrc16(portnum,send_block[send_cnt++]);
   // address 2
   send_block[send_cnt] = (uchar)((address >> 8) & 0xFF);
   lastcrc16 = docrc16(portnum,send_block[send_cnt++]);
   // control byte
   send_block[send_cnt] = control_byte;
   lastcrc16 = docrc16(portnum,send_block[send_cnt++]);

   // now read bytes crc16, and verification
   //for (i = 0; i < 2 + num_verf; i++)
   //   send_block[send_cnt++] = 0xFF;
   memset(&send_block[send_cnt], 0x0FF, 2+num_verf);
   send_cnt += 2+num_verf;

   // now send the block
   OWASSERT( owBlock(portnum,resume,send_block,send_cnt),
             OWERROR_BLOCK_FAILED, FALSE );

   //\\//\\//\\//\\//\\//\\//\\//\\//\\//
   #ifdef DEBUG_DUMP
      debugout(send_block,send_cnt);
   #endif
   //\\//\\//\\//\\//\\//\\//\\//\\//\\//

   // check the CRC
   for (i = resume?5:4; i < (send_cnt - num_verf); i++)
      lastcrc16 = docrc16(portnum,send_block[i]);

   // verify CRC16 is correct
   OWASSERT( lastcrc16==0xB001, OWERROR_CRC_FAILED, FALSE );

   // check verification
   OWASSERT( ((send_block[send_cnt-1] & 0xF0) == 0x50) ||
             ((send_block[send_cnt-1] & 0xF0) == 0xA0),
             OWERROR_NO_COMPLETION_BYTE, FALSE );

   return TRUE;
}
예제 #10
0
//----------------------------------------------------------------------
// Read Authenticated Page for DS1963S.
//
// 'portnum'     - number 0 to MAX_PORTNUM-1.  This number is provided to
//                 indicate the symbolic port number.
// 'pagenum'     - page number to do a read authenticate
// 'data'        - buffer to read into from page
// 'sign'        - buffer for storing sha computation
// 'resume'      - if true, device access is resumed using the RESUME
//                 ROM command (0xA5).  Otherwise, a a MATCH ROM is
//                 used along with the device's entire address number.
//
// Return: Value of write cycle counter for the page
//         -1 for error
//
int ReadAuthPageSHA18(int portnum, SMALLINT pagenum, uchar* data,
                    uchar* sign, SMALLINT resume)
{
   short send_cnt=0;
   uchar send_block[56];
   short num_verf;
   SMALLINT i;
   ushort lastcrc16;
   int address = pagenum*32, wcc = -1;

   if(!resume)
   {
      // access the device
      OWASSERT( SelectSHA(portnum), OWERROR_ACCESS_FAILED, -1 );
   }
   else
   {
      // transmit RESUME command
      send_block[send_cnt++] = ROM_CMD_RESUME;
   }

   //seed the crc
   setcrc16(portnum,0);

   // change number of verification bytes if in overdrive
   num_verf = (in_overdrive[portnum&0x0FF]) ? 10 : 2;

   // create the send block
   // Read Authenticated Page command
   send_block[send_cnt] = CMD_READ_AUTH_PAGE;
   lastcrc16 = docrc16(portnum, send_block[send_cnt++]);

   // TA1
   send_block[send_cnt] = (uchar)(address & 0xFF);
   lastcrc16 = docrc16(portnum,send_block[send_cnt++]);

   // TA2
   send_block[send_cnt] = (uchar)((address >> 8) & 0xFF);
   lastcrc16 = docrc16(portnum,send_block[send_cnt++]);

   // now add the read bytes for data bytes, counter, and crc16, verification
   //for (i = 0; i < (42 + num_verf); i++)
   //   send_block[send_cnt++] = 0xFF;
   memset(&send_block[send_cnt], 0x0FF, 42+num_verf);
   send_cnt += 42+num_verf;

   // now send the block
   OWASSERT( owBlock(portnum,resume,send_block,send_cnt),
             OWERROR_BLOCK_FAILED, -1 );

   //\\//\\//\\//\\//\\//\\//\\//\\//\\//
   #ifdef DEBUG_DUMP
      debugout(send_block,send_cnt);
   #endif
   //\\//\\//\\//\\//\\//\\//\\//\\//\\//

   // check the CRC
   for (i = resume?4:3; i < (send_cnt - num_verf); i++)
      lastcrc16 = docrc16(portnum,send_block[i]);

   // verify CRC16 is correct
   OWASSERT( lastcrc16==0xB001, OWERROR_CRC_FAILED, -1);

   // check verification
   OWASSERT( ((send_block[send_cnt-1] & 0xF0) == 0x50) ||
             ((send_block[send_cnt-1] & 0xF0) == 0xA0),
             OWERROR_NO_COMPLETION_BYTE, -1);

   // transfer results
   //cnt = 0;
   //for (i = send_cnt - 42 - num_verf; i < (send_cnt - 10 - num_verf); i++)
   //   data[cnt++] = send_block[i];
   memcpy(data, &send_block[send_cnt-42-num_verf], 32);

   wcc = BytesToInt(&send_block[send_cnt-10-num_verf], 4);

   if(sign!=NULL)
   {
      OWASSERT( ReadScratchpadSHA18(portnum, 0, 0, send_block, TRUE),
                OWERROR_READ_SCRATCHPAD_FAILED, FALSE );

      //for(i=0; i<20; i++)
      //   sign[i] = send_block[i+8];
      memcpy(sign, &send_block[8], 20);
   }

   return wcc;
}
예제 #11
0
//----------------------------------------------------------------------
// Write the scratchpad with CRC16 verification for DS1963S.  The data
// is padded until the offset is 0x1F so that the CRC16 is retrieved.
//
// 'portnum'     - number 0 to MAX_PORTNUM-1.  This number is provided to
//                 indicate the symbolic port number.
// 'address'     - address to write data to
// 'data'        - data to write
// 'data_len'    - number of bytes of data to write
// 'resume'      - if true, device access is resumed using the RESUME
//                 ROM command (0xA5).  Otherwise, a a MATCH ROM is
//                 used along with the device's entire address number.
//
// Return: TRUE - write to scratch verified
//         FALSE - error writing scratch, device not present, or HIDE
//                 flag is in incorrect state for address being written.
//
SMALLINT WriteScratchpadSHA18(int portnum, int address, uchar *data,
                              SMALLINT data_len, SMALLINT resume)
{
   uchar send_block[50];
   short send_cnt=0,i;
   ushort lastcrc16;

   if(!resume)
   {
      // access the device
      OWASSERT( SelectSHA(portnum), OWERROR_ACCESS_FAILED, FALSE );
   }
   else
   {
      // transmit RESUME command
      send_block[send_cnt++] = ROM_CMD_RESUME;
   }

   setcrc16(portnum,0);
   // write scratchpad command
   send_block[send_cnt] = CMD_WRITE_SCRATCHPAD;
   lastcrc16 = docrc16(portnum,send_block[send_cnt++]);
   // address 1
   send_block[send_cnt] = (uchar)(address & 0xFF);
   lastcrc16 = docrc16(portnum,send_block[send_cnt++]);
   // address 2
   send_block[send_cnt] = (uchar)((address >> 8) & 0xFF);
   lastcrc16 = docrc16(portnum,send_block[send_cnt++]);
   // data
   for (i = 0; i < data_len; i++)
   {
      send_block[send_cnt] = data[i];
      lastcrc16 = docrc16(portnum,send_block[send_cnt++]);
   }
   // pad if needed
   for (i = 0; i < (0x1F - ((address + data_len - 1) & 0x1F)); i++)
   {
      send_block[send_cnt] = 0xFF;
      lastcrc16 = docrc16(portnum,send_block[send_cnt++]);
   }
   // CRC16
   send_block[send_cnt++] = 0xFF;
   send_block[send_cnt++] = 0xFF;

   // now send the block
   OWASSERT( owBlock(portnum,resume,send_block,send_cnt),
             OWERROR_BLOCK_FAILED, FALSE );

   //\\//\\//\\//\\//\\//\\//\\//\\//\\//
   #ifdef DEBUG_DUMP
      debugout(send_block,send_cnt);
   #endif
   //\\//\\//\\//\\//\\//\\//\\//\\//\\//

   // perform CRC16 of last 2 byte in packet
   for (i = send_cnt - 2; i < send_cnt; i++)
      lastcrc16 = docrc16(portnum,send_block[i]);

   // verify CRC16 is correct
   OWASSERT( lastcrc16==0xB001, OWERROR_CRC_FAILED, FALSE );

   // success
   return TRUE;
}
예제 #12
0
//-------------------------------------------------------------------------
// Updates service data on a user token.  This includes signing the
// data if the part is a DS1963S.
//
// 'copr'      - Structure for holding coprocessor information.
// 'user'      - Structure for holding user token information.
//
// Return: If TRUE, update succeeded.
//         If FALSE, an error occurred.
//
SMALLINT UpdateServiceData(SHACopr* copr, SHAUser* user)
{
   ushort crc16, i;
   uchar scratchpad[32];

   if(user->devAN[0] == 0x18)
   {
      DebitFile* accountFile = (DebitFile*)user->accountFile;

      // make sure length is right.
      accountFile->fileLength = 29;

      // update transaction ID
      // doesn't matter what it is, just needs to change
      accountFile->transID[0] += 1;
      if(accountFile->transID[0]==0)
        accountFile->transID[1] += 1;

      // conversion factor - 2 data bytes
      accountFile->convFactor[0] = (uchar)0x8B;
      accountFile->convFactor[1] = (uchar)0x48;

      // clear out the old signature and CRC
      memcpy(accountFile->signature, copr->initSignature, 20);
      memset(accountFile->crc16, 0x00, 2);

      // reset data type code
      accountFile->dataTypeCode = 1;

      //file doesn't continue on another page
      accountFile->contPtr = 0;

      // ---  Set up the scratchpad for signing
      memset(scratchpad, 0x00, 32);
      // the write cycle counter +1 (since we are about to write this to it)
      if(user->writeCycleCounter>0)
         IntToBytes(&scratchpad[8], 4, user->writeCycleCounter+1);
      else
         // user doesn't have write cycle counter (DS1961S)
         memset(&scratchpad[8], 0x0FF, 4);
      // the pagenumber
      scratchpad[12] = (uchar)user->accountPageNumber;
      // and 7 bytes of the address of current device
      memcpy(&scratchpad[13], user->devAN, 7);
      // the coprocessor's signing challenge
      memcpy(&scratchpad[20], copr->signChlg, 3);

      OWASSERT( CreateDataSignatureVM(copr, sign_secret,
                                      user->accountFile,
                                      scratchpad,
                                      accountFile->signature,
                                      TRUE),
                OWERROR_SIGN_SERVICE_DATA_FAILED, FALSE );

      //add the crc at the end of the data.
      setcrc16(user->portnum, user->accountPageNumber);
      for (i = 0; i < 30; i++)
         crc16 = docrc16(user->portnum,user->accountFile[i]);
      crc16 = ~crc16;
      accountFile->crc16[0] = (uchar)crc16;
      accountFile->crc16[1] = (uchar)(crc16>>8);
   }
예제 #13
0
//----------------------------------------------------------------------
//  Read a specified number of pages in overdrive
//
// 'portnum'  - number 0 to MAX_PORTNUM-1.  This number is provided to
//              indicate the symbolic port number.
//
int ReadPages(int portnum, int start_pg, int num_pgs, int *last_pg, uchar *finalbuf)
{
   int skip_overaccess = 0, skip_access = 0;
   uchar pkt[60];
   int len,i;
   uchar  SerialNumber[8];
   ushort lastcrc16;

   // read the rom number 
   owSerialNum(portnum,SerialNumber,TRUE);

   // verify device is in overdrive
   if (current_speed[portnum] == MODE_OVERDRIVE)
   {
      if (owVerify(portnum,FALSE)) 
         skip_overaccess = 1;
   }

   if (!skip_overaccess)
   {
      if (owOverdriveAccess(portnum))
         current_speed[portnum] = MODE_OVERDRIVE;
      else
         current_speed[portnum] = MODE_NORMAL;
   }

   // loop while there is pages to read
   do
   {
      // create a packet to read a page
      len = 0;
      setcrc16(portnum,0);
      // optional skip access on subsequent pages 
      if (!skip_access)
      {  
         // match
         pkt[len++] = 0x55; 
         // rom number
         for (i = 0; i < 8; i++)
            pkt[len++] = SerialNumber[i];
         // read memory with crc command 
         pkt[len] = 0xA5; 
         docrc16(portnum,pkt[len++]);         
         // address
         pkt[len] = (uchar)((*last_pg << 5) & 0xFF);
         docrc16(portnum,pkt[len++]);         
         pkt[len] = (uchar)(*last_pg >> 3); 
         docrc16(portnum,pkt[len++]);         
      }

      // set 32 reads for data and 2 for crc
      for (i = 0; i < 34; i++)
         pkt[len++] = 0xFF; 
         
      // send the bytes
      if (owBlock(portnum,!skip_access,pkt,len))
      {
         // calucate the CRC over the last 34 bytes
         for (i = 0; i < 34; i++)
            lastcrc16 = docrc16(portnum,pkt[len - 34 + i]);

         // check crc
         if (lastcrc16 == 0xB001)
         {
            // copy the data into the buffer
#ifdef LetsCrashTheCompiler
	   for (i = 0; i < 32; i++)
	     finalbuf[i + (*last_pg - start_pg) * 32] = pkt[len - 34 + i];
#endif
	   {
	     ushort k;
	     for (i = 0; i < 32; i++) {
	       k=i + (*last_pg - start_pg) * 32;
	       finalbuf[k] = pkt[len - 34 + i];
	     }
	   }
            // change number of pages 
            *last_pg = *last_pg + 1;

            // now skip access 
            skip_access = TRUE;
         }
         else
            return FALSE;
      }
      else
         return FALSE;
   }