Beispiel #1
0
int Temp_DoRead(int iSensor)
{
	uchar send_block[30], lastcrc8;
	int send_cnt, tsht = 0, i, loop = 0;
//	LED_Set(6);
	if(iSensor >= NumDevices)
		return 0;
//	LED_Set(7);
	owSerialNum(PORTNUM, TempSensorSN[iSensor], FALSE);

	for (loop = 0; loop < 2; loop++) {
		// access the device
		if (owAccess(PORTNUM)) {
			// send the convert command and if nesessary start power delivery
			if (!owWriteByte(PORTNUM, 0x44))
					return 0;

			// access the device
			if (owAccess(PORTNUM)) {
				// create a block to send that reads the temperature
				// read scratchpad command
				send_cnt = 0;
				send_block[send_cnt++] = 0xBE;
				// now add the read bytes for data bytes and crc8
				for (i = 0; i < 9; i++)
					send_block[send_cnt++] = 0xFF;

				// now send the block
				if (owBlock(PORTNUM, FALSE, send_block, send_cnt)) {
					// initialize the CRC8
					setcrc8(PORTNUM, 0);
					// perform the CRC8 on the last 8 bytes of packet
					for (i = send_cnt - 9; i < send_cnt; i++)
						lastcrc8 = docrc8(PORTNUM, send_block[i]);

					// verify CRC8 is correct
					if (lastcrc8 == 0x00) {
						// calculate the high-res temperature
						tsht = send_block[2] << 8;
						tsht = tsht | send_block[1];
						if (tsht & 0x00001000)
							tsht = tsht | 0xffff0000;
						if(!(tsht == 1360 && LastTemperature[iSensor] == 0)){
							LastTemperature[iSensor] = tsht;
						}else{
							tsht = 0;
						}
						// success
						break;
					}
				}
			}
		}
	}
	return tsht;
}
Beispiel #2
0
int Copy2Mem43(int portnum, uchar *SerialNum)
{
	uchar rt=FALSE;
	uchar read_data;
	
	owSerialNum(portnum, SerialNum, FALSE);

	if(owAccess(portnum))
	{	
		if (!owWriteBytePower(portnum, COPY_SCRATCH_CMD)) 
			return FALSE;
		owLevel(portnum, MODE_NORMAL);
		owWriteBytePower(portnum, 0x00);	//write LSB of target addr
		owLevel(portnum, MODE_NORMAL);
		owWriteBytePower(portnum, 0x00);	//write MSB of target addr
		owLevel(portnum, MODE_NORMAL);
		owWriteBytePower(portnum, 0x1f);	//write E/S

		msDelay(500);
				
		
		owLevel(portnum,MODE_NORMAL);		
		read_data = owReadBytePower(portnum);
		
		if (read_data == 0xaa)			
			rt=TRUE;
	}

	return rt;
}
Beispiel #3
0
/**
 * Writes a new identifier and password to the secure subkey iButton
 *
 * portnum    the port number of the port being used for the 
 *            1-Wire Network.
 * key        number indicating the key to be read: 0, 1, or 2
 * oldName    identifier of the key used to confirm the correct
 *            key's password to be changed.  Must be exactly length 8.
 * newName    identifier to be used for the key with the new
 *            password.  Must be exactly length 8.
 * newPasswd  new password for destination subkey.  Must be
 *            exactly length 8.
 *
 * return     TRUE if the write was successful  
 */
SMALLINT writePassword(int portnum, int key, uchar *oldName, 
                       uchar *newName, uchar *newPasswd)
{
   uchar buffer[96];
   int   i;

   if(owAccess(portnum))
   {
      //confirm key names and passwd within legal parameters
      if (key > 0x03)
      {
         OWERROR(OWERROR_KEY_OUT_OF_RANGE);
         return FALSE;
      }

      buffer[0] = WRITE_PASSWORD_COMMAND;
      buffer[1] = (uchar) (key << 6);
      buffer[2] = (uchar) (~buffer[1]);

      //prepare buffer to receive 8 bytes of the identifier
      for(i = 3; i < 11; i++)
         buffer [i] = 0xFF;

      if(!owBlock(portnum,FALSE,buffer,11))
      {
         OWERROR(OWERROR_BLOCK_FAILED);
         return FALSE;
      }

      //prepare same subkey identifier for confirmation
      for(i=0;i<8;i++)
         buffer[i] = buffer[i+3];

      //prepare new subkey identifier
      for(i=0;i<8;i++)
         buffer[i+8] = newName[i];

      //prepare new password for writing
      for(i=0;i<8;i++)
         buffer[i+16] = newPasswd[i];

      //send command block
      if(!owBlock(portnum,FALSE,buffer,24))
      {
         OWERROR(OWERROR_BLOCK_FAILED);
         return FALSE;
      }

   }
   else
   {
      OWERROR(OWERROR_DEVICE_SELECT_FAIL);
      return FALSE;
   }

   return TRUE;
}
Beispiel #4
0
/**
 * Writes the data to the scratchpad from the given address.
 *
 * portnum  the port number of the port being used for the
 *          1-Wire network.
 * addr     address to begin writing.  Must be between
 *          0x00 and 0x3F.
 * data     data to write.
 * len      the length of the data to write
 *
 * return:  TRUE  if the write worked
 *          FALSE if there was an error in writing
 */
SMALLINT writeScratchpad (int portnum, int addr, uchar *data, int len)
{
   int   dataRoom,i;
   uchar buffer[96];

   if(owAccess(portnum))
   {
      //confirm that data will fit
      if (addr > 0x3F)
      {
         OWERROR(OWERROR_WRITE_OUT_OF_RANGE);
         return FALSE;
      }

      dataRoom = 0x3F - addr + 1;

      if (dataRoom < len)
      {
         OWERROR(OWERROR_DATA_TOO_LONG);
         return FALSE;
      }

      buffer[0] = ( uchar ) WRITE_SCRATCHPAD_COMMAND;
      buffer[1] = ( uchar ) (addr | 0xC0);
      buffer[2] = ( uchar ) (~buffer [1]);

      for(i=0;i<len;i++)
         buffer[i+3] = data[i];

      //send command block
      if((len+3) > 64)
      {
         if(!owBlock(portnum,FALSE,buffer,64))
         {
            OWERROR(OWERROR_BLOCK_FAILED);
            return FALSE;
         }
         else if(!owBlock(portnum,FALSE,&buffer[64],3))
         {
            OWERROR(OWERROR_BLOCK_FAILED);
            return FALSE;
         }
      }
      else if(!owBlock(portnum,FALSE,buffer,len+3))
      {
         OWERROR(OWERROR_BLOCK_FAILED);
         return FALSE;
      }
   }
   else
   {
      OWERROR(OWERROR_DEVICE_SELECT_FAIL);
      return FALSE;
   }

   return TRUE;
}
Beispiel #5
0
/*
  ds2338mem_rd

  Reading DS2438 memory

  returns one page of memory

  input parameters 
  portnum  the port number of the port being used for the
           1-Wire Network.
  SNum     the serial number for the part that the read is
           to be done on.
  pageno   the page number of memory to be read
 
 */
int 
ds2438mem_rd(int portnum, uchar *SNum, uchar *pagemem, uchar pageno, char *device) {
   int block_cnt;
   int i;
   ushort lastcrc8;

   owSerialNum(portnum,SNum,FALSE);

   block_cnt = 0;

   // Recall the Status/Configuration page
   // Recall command
   pagemem[block_cnt++] = 0xB8;
   // Page to Recall
   pagemem[block_cnt++] = pageno;
   owAccess(portnum);
   owBlock(portnum,FALSE,pagemem,block_cnt);     
   syslog(LOG_DEBUG, "ds2438mem_rd: recall memory (B8h %xh): %s\n", 
     pageno, ppagemem(pagemem));
   block_cnt = 0;
   // Read the Status/Configuration byte
   // Read scratchpad command
   pagemem[block_cnt++] = 0xBE;
   // Page for the Status/Configuration byte
   pagemem[block_cnt++] = pageno;
   for(i=0;i<9;i++)
     pagemem[block_cnt++] = 0xFF;
   owAccess(portnum);
   owBlock(portnum,FALSE,pagemem,block_cnt);     
   syslog(LOG_DEBUG,"ds2438mem_rd: read scratchpad (BEh %xh): %s \n", 
       pageno, ppagemem( pagemem));

   setcrc8(portnum,0);
   for(i=2;i<block_cnt;i++) {
     lastcrc8 = docrc8(portnum,pagemem[i]);
   }
   if(lastcrc8 != 0x00) {
     syslog(LOG_ALERT, "ds2438mem_rd: CRC error ");
     bitprint( lastcrc8, "lastcrc8");
     return 1;
   }

   return 0;
}
Beispiel #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;
}
Beispiel #7
0
/* -----------------------------------------------------------------------
   ----------------------------------------------------------------------- */
int get_ibl_type(int portnum, uchar page, int offset)
{
   uchar send_block[50];
   int send_cnt=0;
   int i;
   ushort lastcrc8=255;

   /* 01/08/2004 [bcl] DigiTemp does this before calling the function
    * owSerialNum(portnum,SNum,FALSE);
    */

   // Recall the Status/Configuration page
   // Recall command
   send_block[send_cnt++] = 0xB8;

   // Page to Recall
   send_block[send_cnt++] = page;

   if(!owBlock(portnum,FALSE,send_block,send_cnt))
      return FALSE;

   send_cnt = 0;

   if(owAccess(portnum))
   {
      // Read the Status/Configuration byte
      // Read scratchpad command
      send_block[send_cnt++] = 0xBE;

      // Page for the Status/Configuration byte
      send_block[send_cnt++] = page;

      for(i=0;i<9;i++)
         send_block[send_cnt++] = 0xFF;

      if(owBlock(portnum,FALSE,send_block,send_cnt))
      {
         setcrc8(portnum,0);

         for(i=2;i<send_cnt;i++)
            lastcrc8 = docrc8(portnum,send_block[i]);

         if(lastcrc8 != 0x00)
            return FALSE;
      } else {
         return FALSE;
      }

      // Return the requested byte
      return send_block[2+offset];
   }//Access

   return -1;
}
Beispiel #8
0
/**
 * Reads the subkey requested with the given key name and password.
 * Note that this method allows for reading from the subkey data
 * only which starts at address 0x10 within a key. It does not allow
 * reading from any earlier address within a key because the device
 * cannot be force to allow reading the password. This is why the
 * subkey number is or-ed with 0x10 in creating the address in bytes
 * 1 and 2 of the sendBlock.
 *
 * portnum    the port number of the port being used for the 
 *            1-Wire Network.
 * data       buffer of length 64 into which to write the data
 * key        number indicating the key to be read: 0, 1, or 2
 * passwd     password of destination subkey
 *
 * return:    TRUE  if reading the subkey was successful.
 */
SMALLINT readSubkey(int portnum, uchar *data, int key, uchar *passwd)
{
   uchar buffer[96];
   int i;

   if(owAccess(portnum))
   {
      //confirm key within legal parameters
      if (key > 0x03)
      {
         OWERROR(OWERROR_KEY_OUT_OF_RANGE);
         return FALSE;
      }

      buffer[0] = READ_SUBKEY_COMMAND;
      buffer[1] = (uchar) ((key << 6) | 0x10);
      buffer[2] = (uchar) (~buffer [1]);

      //prepare buffer to receive
      for (i = 3; i < 67; i++)
         buffer[i] = 0xFF;

      //insert password data
      for(i=0;i<8;i++)
         buffer[i+11] = passwd[i];

      //send command block
      if(!owBlock(portnum,FALSE,buffer,64))
      {
         OWERROR(OWERROR_BLOCK_FAILED);
         return FALSE;
      }
      else if(!owBlock(portnum,FALSE,&buffer[64],3))
      {
         OWERROR(OWERROR_BLOCK_FAILED);
         return FALSE;
      }

      //create block to send back
      for(i=0;i<64;i++)
         data[i] = buffer[i+3];
   }
   else
   {
      OWERROR(OWERROR_DEVICE_SELECT_FAIL);
      return FALSE;
   }

   return TRUE;
}
Beispiel #9
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;
}
Beispiel #10
0
/**
 * Writes the data from the scratchpad to the specified block or
 * blocks.  Note that the write will erase the data from the
 * scratchpad.
 *
 * portnum      the port number of the port being used for the 
 *              1-Wire Network.
 * key          subkey being written
 * passwd       password for the subkey being written
 * blockNum     number of the block to be copied (see page 7 of the
 *              DS1991 data sheet) block 0-7, or 8 to copy all 64 bytes.
 *
 * return       TRUE  if the copy scratchpad was successful.
 */
SMALLINT copyScratchpad(int portnum, int key, uchar *passwd, int blockNum)
{
   uchar buffer[96];
   int i;

   if(owAccess(portnum))
   {
      //confirm that input is OK
      if ((key < 0) || (key > 2))
      {
         OWERROR(OWERROR_KEY_OUT_OF_RANGE);
         return FALSE;
      }

      if ((blockNum < 0) || (blockNum > 8))
      {
         OWERROR(OWERROR_BLOCK_ID_OUT_OF_RANGE);
         return FALSE;
      }
   
      buffer[0] = COPY_SCRATCHPAD_COMMAND;
      buffer[1] = (uchar) (key << 6);
      buffer[2] = (uchar) (~buffer [1]);

      //set up block selector code
      for(i=0;i<8;i++)
         buffer[i+3] = pscodes[blockNum][i];

      //set up password
      for(i=0;i<8;i++)
         buffer[i+11] = passwd[i];

      //send command block
      if(!owBlock(portnum,FALSE,buffer,19))
      {
         OWERROR(OWERROR_BLOCK_FAILED);
         return FALSE;
      }
   }
   else
   {
      OWERROR(OWERROR_DEVICE_SELECT_FAIL);
      return FALSE;
   }

   return TRUE;
}
Beispiel #11
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;
}
Beispiel #12
0
/**
 * Reads the entire scratchpad.
 *
 * portnum  the port number of the port being used for the
 *          1-Wire Network
 * data     the data that was read from the scratchpad.
 *
 * return   TRUE  if the data was read without an error
 *
 */
SMALLINT readScratchpad(int portnum, uchar *data)
{
   uchar buffer[96];
   int i;

   if(owAccess(portnum))
   {
      buffer[0] = READ_SCRATCHPAD_COMMAND;
      buffer[1] = 0xC0;   //Starting address of scratchpad
      buffer[2] = 0x3F;

      for(i = 3; i < 67; i++)
         buffer[i] = 0xFF;

      //send command block
      if(!owBlock(portnum,FALSE,buffer,64))
      {
         OWERROR(OWERROR_BLOCK_FAILED);
         return FALSE;
      }
      else if(!owBlock(portnum,FALSE,&buffer[64],3))
      {
         OWERROR(OWERROR_BLOCK_FAILED);
         return FALSE;
      }

      for(i=0;i<64;i++)
         data[i] = buffer[i+3];
   }
   else
   {
      OWERROR(OWERROR_DEVICE_SELECT_FAIL);
      return FALSE;
   }

   return TRUE;
}
Beispiel #13
0
//----------------------------------------------------------------------
// Writes specific bits to the control register of the 1-Wire 
// clock.  Any of the 8 control register bits can be written.
//
// Note:  If specific bits are desired to be left alone, place values 
//        other than TRUE (1) or FALSE (0) in them (such as -99)
//        when calling the function.
//
// Parameters:
//  portnum      The port number of the port being used for the
//               1-Wire network.
//  SNum         The 1-Wire address of the device with which to communicate.
//  dsel         Delay select bit
//  stopstart    STOP/START (in Manual Mode).
//  automan      Automatic/Manual Mode.
//  osc          Oscillator enable (start the clock ticking...)
//  ro           Read only (during expiration: 1 for read-only, 0 for destruct).
//  wpc          Write-Protect cycle counter bit.
//  wpi          Write-Protect interval timer bit.
//  wpr          Write-Protect RTC and clock alarm bit.
//
// Returns:      TRUE  if the write worked.
//               FALSE if there was an error in writing the status register.
//
//
SMALLINT setControlRegister(int portnum, uchar * SNum, SMALLINT dsel, SMALLINT startstop, SMALLINT automan, SMALLINT osc, SMALLINT ro, SMALLINT wpc, SMALLINT wpi, SMALLINT wpr)
{
   uchar controlregister[2]; // to store both status and control bytes
   SMALLINT writetopart = FALSE;
   SMALLINT writeprotect = FALSE;
   int i = 0;
   controlregister[0] = 0;
   controlregister[1] = 0;

   // read 1 bytes from 1-Wire clock device (memory bank 2) 
   // starting at address 0x01.  This is the control register.
   if (!readNV(2, portnum, SNum, 0x00, FALSE, &controlregister[0], 2))
   {
	   return FALSE;
   }

   // if necessary, update control register
   if (((controlregister[1] & 0x80) > 0) && (dsel == FALSE)) // turn dsel to 0
   {
      // The dsel bit is in 7th bit position (MSb) of byte.
      // 'And'ing with 0x7F (127 decimal or 01111111 binary) will 
	   // guarantee a 0-bit in the position.
      controlregister[1] = controlregister[1] & 0x7F;
	   writetopart = TRUE;
   }
   if (((controlregister[1] & 0x80) == 0) && (dsel == TRUE)) // turn dsel to 1
   {
      // The dsel bit is in 7th bit position (MSb) of byte.
      // 'Or'ing with 0x80 (16) will guarantee a 1-bit in the position and 
      // turn the dsel bit to 1.
      controlregister[1] = controlregister[1] | 0x80;
	   writetopart = TRUE;
   }
   if (((controlregister[1] & 0x40) > 0) && (startstop == FALSE)) // turn startstop to 0
   {
      // The startstop bit is in the 6th bit position of the control register.
      // 'And'ing with 0xBF (191 decimal or 10111111 binary) will 
	   // guarantee a 0-bit in the position.
      controlregister[1] = controlregister[1] & 0xBF;
	   writetopart = TRUE;
   }
   if (((controlregister[1] & 0x40) == 0) && (startstop == TRUE)) // turn startstop to 1
   {
      // The startstop bit is in the 6th bit position of the control register.
      // 'Or'ing with 0x40 (64) will guarantee a 1-bit in the position.
      controlregister[1] = controlregister[1] | 0x40;
	   writetopart = TRUE;
   }
   if (((controlregister[1] & 0x20) > 0) && (automan == FALSE)) // turn automan to 0
   {
      // The automan bit is in the 5th bit position of the control register.
      // 'And'ing with 0xDF (223 decimal or 11011111 binary) will 
	   // guarantee a 0-bit in the position.
      controlregister[1] = controlregister[1] & 0xDF;
	   writetopart = TRUE;
   }
   if (((controlregister[1] & 0x20) == 0) && (automan == TRUE)) // turn automan to 1
   {
      // The automan bit is in the 5th bit position of the control register.
      // 'Or'ing with 0x20 (32) will guarantee a 1-bit in the position.
      controlregister[1] = controlregister[1] | 0x20;
	   writetopart = TRUE;
   }
   if (((controlregister[1] & 0x10) > 0) && (osc == FALSE)) // turn oscillator off
   {
      // The oscillator enable bit is in 4th bit position (16) of byte.
      // 'And'ing with 0xEF (239 decimal or 11101111 binary) will 
	   // guarantee a 0-bit in the position and turn the oscillator off.
      controlregister[1] = controlregister[1] & 0xEF;
	   writetopart = TRUE;
   }
   if (((controlregister[1] & 0x10) == 0) && (osc == TRUE)) // turn oscillator on
   {
      // The oscillator enable bit is in 4th bit position (16) of byte.
      // 'Or'ing with 0x10 (16) will guarantee a 1-bit in the position and 
      // turn the oscillator on.
      controlregister[1] = controlregister[1] | 0x10;
	   writetopart = TRUE;
   }
   if (((controlregister[1] & 0x08) > 0) && (ro == FALSE)) // turn ro to 0
   {
      // The ro bit is in the 3rd bit position of the control register.
      // 'And'ing with 0xF7 (247 decimal or 11110111 binary) will 
	   // guarantee a 0-bit in the position.
      controlregister[1] = controlregister[1] & 0xF7;
	   writetopart = TRUE;
   }
   if (((controlregister[1] & 0x08) == 0) && (ro == TRUE)) // turn ro to 1
   {
      // The ro bit is in the 3rd bit position of the control register.
      // 'Or'ing with 0x08 will guarantee a 1-bit in the position.
      controlregister[1] = controlregister[1] | 0x08;
	   writetopart = TRUE;
   }
   if (((controlregister[1] & 0x04) > 0) && (wpc == FALSE)) // turn wpc to 0
   {
      // The wpc bit is in the 2nd bit position of the control register.
      // 'And'ing with 0xFB (251 decimal or 11111011 binary) will 
	   // guarantee a 0-bit in the position.
      controlregister[1] = controlregister[1] & 0xFB;
	   writeprotect = TRUE;
	   writetopart = TRUE;
   }
   if (((controlregister[1] & 0x04) == 0) && (wpc == TRUE)) // turn wpc to 1
   {
      // The wpc bit is in the 2nd bit position of the control register.
      // 'Or'ing with 0x04 will guarantee a 1-bit in the position.
      controlregister[1] = controlregister[1] | 0x04;
	   writeprotect = TRUE;
	   writetopart = TRUE;
   }
   if (((controlregister[1] & 0x02) > 0) && (wpi == FALSE)) // turn wpi to 0
   {
      // The wpi bit is in the 1st bit position of the control register.
      // 'And'ing with 0xFD (253 decimal or 11111101 binary) will 
	   // guarantee a 0-bit in the position.
      controlregister[1] = controlregister[1] & 0xFD;
	   writeprotect = TRUE;
	   writetopart = TRUE;
   }
   if (((controlregister[1] & 0x02) == 0) && (wpi == TRUE)) // turn wpi to 1
   {
      // The wpi bit is in the 1st bit position of the control register.
      // 'Or'ing with 0x02 will guarantee a 1-bit in the position.
      controlregister[1] = controlregister[1] | 0x02;
	   writeprotect = TRUE;
	   writetopart = TRUE;
   }
   if (((controlregister[1] & 0x01) > 0) && (wpr == FALSE)) // turn wpr to 0
   {
      // The wpr bit is in the 0th bit position of the control register.
      // 'And'ing with 0xFE (254 decimal or 11111110 binary) will 
	   // guarantee a 0-bit in the position.
      controlregister[1] = controlregister[1] & 0xFE;
	   writeprotect = TRUE;
	   writetopart = TRUE;
   }
   if (((controlregister[1] & 0x01) == 0) && (wpr == TRUE)) // turn wpr to 1
   {
      // The wpr bit is in the 0th bit position of the control register.
      // 'Or'ing with 0x01 will guarantee a 1-bit in the position.
      controlregister[1] = controlregister[1] | 0x01;
	   writeprotect = TRUE;
	   writetopart = TRUE;
   }

   // write new control register (if any) to part.
   if (writetopart == TRUE)
   {
	   // if writeprotect is true, then copyscratchpad 3 times...
	   if (writeprotect == TRUE)
      {
         owSerialNum(portnum, SNum, 1);
		   // write to scratchpad
		   if (!writeScratchpd(portnum, 0x200, &controlregister[0], 2))
         {
		      return FALSE;
         }

         // copy the scratchpad 3 times to write protect
         for (i = 0; i < 3; i++)
         {
            if (!owAccess(portnum)) return FALSE;            // if Access is unsuccessful
               
            if(!((0x55 != (uchar)owTouchByte(portnum,0x55)) &&  // sent the copy command
                 (0x00 != (uchar)owTouchByte(portnum, 0x00)) && // write the target address 1
                 (0x02 != (uchar)owTouchByte(portnum, 0x02))))  // write the target address 2
            {
               return FALSE;
            }
			   if (i == 0)
            {
			      if(!owTouchByte(portnum,0x01))
                  return FALSE;
            }
			   else
            {
			      if(! owTouchByte(portnum, 0x81))  // The AA bit gets set on consecutive
				      return FALSE;                  // copyscratchpads (pg. 12 of datasheet)
            }
         }
      }
	   else
      {
         if (!writeNV(2, portnum, SNum, 0x00, &controlregister[0], 2))
         {
	         return FALSE;
         }
      }	 
   }
   return TRUE;
}
Beispiel #14
0
//---------------------------------------------------------------------------
// The main program that performs the operations on switches
//
int main(int argc, char **argv)
{
   short test;                     //info byte data
   short clear=0;                  //used to clear the button
   short done;                     //to tell when the user is done
   SwitchProps sw;                 //used to set Channel A and B
   uchar SwitchSN[MAXDEVICES][8];  //the serial number for the devices
   int num;                        //for the number of devices present
   int i,j,n,count,result;         //loop counters and indexes
   char out[140];                  //used for output of the info byte data
   int portnum=0;
   long select;                    //inputed number from user

   //----------------------------------------
   // Introduction header
   printf("\n/---------------------------------------------\n");
   printf("  Switch - V3.00\n"
          "  The following is a test to excersize the \n"
          "  setting of the state in a DS2406.\n");

   printf("  Press any CTRL-C to stop this program.\n\n");

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

   // attempt to acquire the 1-Wire Net
   if ((portnum = owAcquireEx(argv[1])) < 0)
   {
      OWERROR_DUMP(stdout);
      exit(1);
   }

   // success
   printf("Port opened: %s\n",argv[1]);

   // this is to get the number of the devices and the serial numbers
   num = FindDevices(portnum, &SwitchSN[0], SWITCH_FAMILY, MAXDEVICES);

   // setting up the first print out for the frist device
   owSerialNum(portnum, SwitchSN[0], FALSE);

   printf("\n");
   n=0;
   if(owAccess(portnum))
   {
      // loop while not done
      do
      {
         test = ReadSwitch12(portnum, clear);

         for(i=7; i>=0; i--)
            printf("%02X", SwitchSN[n][i]);

         printf("\n");

         count = SwitchStateToString12(test, out);
         printf("%s", out);

         // print menu
         select = 1;
         if (!EnterNum("\n\n(1) Display the switch Info\n"
              "(2) Clear activity Latches\n"
              "(3) Set Flip Flop(s) on switch\n"
              "(4) Select different device\n"
              "(5) Quit\n"
              "Select a Number", 1, &select, 1, 5))
              break;
         printf("\n\n");

         // do something from the menu selection
         clear = FALSE;
         switch(select)
         {
            case 1: // Display the switch Info
               done = FALSE;
               break;
            case 2: // Clear activity Latches
               clear = TRUE;
               done = FALSE;
               break;
            case 3: // Set Flip Flop(s) on switch
               select = 0;
               if (EnterNum("Channel A Flip Flop (1 set, 0 clear)",
                    1, &select, 0, 1))
               {
                  sw.Chan_A = (uchar)select;

                  if(test & 0x40)
                  {
                     if (EnterNum("Channel B Flip Flop (1 set, 0 clear)",
                          1, &select, 0, 1))
                     {
                        sw.Chan_B = (uchar)select;
                        done = FALSE;
                     }
                  }
                  else
                  {
                    sw.Chan_B = 0;
                    done = FALSE;
                  }
                  printf("\n");
               }

               // proceed to setting switch state if not done
               if (!done)
               {
                  // loop to attempt to set the switch (try up to 5 times)
                  count = 0;
                  do
                  {
                     result = SetSwitch12(portnum, SwitchSN[n], sw);

                     // if failed then delay to let things settle
                     if (!result)
                        msDelay(50);
                  }
                  while ((count++ < 5) && (result != TRUE));

                  // if could not set then show error
                  if (!result)
                     printf("Could not set Switch!\n");
               }
               break;
            case 4: // Switch Devices
               // print the device list
               for(j=0; j < num; j++)
               {
                  printf("%d  ", j+1);
                  for(i=7; i>=0; i--)
                  {
                     printf("%02X", SwitchSN[j][i]);
                  }
                  printf("\n");
               }
               printf("\n");

               // select the device
               select = 0;
               if (EnterNum("Select Device",1, &select, 1, num))
               {
                  n = (int)(select - 1);
                  owSerialNum(portnum, SwitchSN[n], FALSE);
                  done = FALSE;
               }
               break;

            case 5: // Done
               done = TRUE;
               break;
               default:
            break;
         }
      }
      while (!done);
   }
   //One Wire Access
   owRelease(portnum);
   printf("Closing port %s.\n", argv[1]);
   exit(0);

   return 0;
}
Beispiel #15
0
//----------------------------------------------------------------------
// Read the temperature of a DS1920/DS1820
//
// 'portnum'     - number 0 to MAX_PORTNUM-1.  This number was provided to
//                 OpenCOM to indicate the port number.
// 'SerialNum'   - Serial Number of DS1920/DS1820 to read temperature from
// 'Temp '       - pointer to variable where that temperature will be 
//                 returned
//
// Returns: TRUE(1)  temperature has been read and verified
//          FALSE(0) could not read the temperature, perhaps device is not
//                   in contact
//
int ReadTemperature(int portnum, uchar *SerialNum, float *Temp)
{
   int rt=FALSE;
   uchar send_block[30],lastcrc8;
   int send_cnt=0, tsht, i, loop=0;
   float tmp,cr,cpc;
   
   
   setcrc8(portnum,0);

   // set the device serial number to the counter device
   owSerialNum(portnum,SerialNum,FALSE);

   for (loop = 0; rt==FALSE && loop < 2; loop ++)
   {
      // access the device 
      if (owAccess(portnum))
      {
         // send the convert temperature command
         owTouchByte(portnum,0x44);

         // set the 1-Wire Net to strong pull-up
         if (owLevel(portnum,MODE_STRONG5) != MODE_STRONG5)
            return FALSE;
 
         // sleep for 1 second
         msDelay(1000);

         // turn off the 1-Wire Net strong pull-up
         if (owLevel(portnum,MODE_NORMAL) != MODE_NORMAL)
            return FALSE;

         // access the device 
         if (owAccess(portnum))
         {
            // create a block to send that reads the temperature
            // read scratchpad command
            send_block[send_cnt++] = 0xBE;
            // now add the read bytes for data bytes and crc8
            for (i = 0; i < 9; i++)
               send_block[send_cnt++] = 0xFF;

            // now send the block
            if (owBlock(portnum,FALSE,send_block,send_cnt))
            {
               // perform the CRC8 on the last 8 bytes of packet
               for (i = send_cnt - 9; i < send_cnt; i++)
                  lastcrc8 = docrc8(portnum,send_block[i]);

               // verify CRC8 is correct
               if (lastcrc8 == 0x00)
               {
                  // calculate the high-res temperature
                  tsht = send_block[1]/2;
                  if (send_block[2] & 0x01)
                     tsht |= -128;
                  tmp = (float)(tsht);
                  cr = send_block[7];
                  cpc = send_block[8];
                  if (((cpc - cr) == 1) && (loop == 0))
                     continue;
                  if (cpc == 0)
                     return FALSE;   
                  else
                     tmp = tmp - (float)0.25 + (cpc - cr)/cpc;
   
                  *Temp = tmp;
                  // success
                  rt = TRUE;
               }
            }
         }
      }
        
   }
   
 // return the result flag rt
      return rt;
      
}
Beispiel #16
0
//----------------------------------------------------------------------
//	SUBROUTINE - SetSwitch1F
//
//  This routine sets the main and auxilary on and off for DS2409.
//
// 'portnum'     - number 0 to MAX_PORTNUM-1.  This number is provided to
//                 indicate the symbolic port number.
// 'SerialNum'   - Serial Number of DS2409 to set the switch state
// 'Swtch'       - '0' Sets Main and Auxilary off
//                 '1' Sets Main on
//                 '2' Sets Auxilary on
//                 '3' Read Status Info Byte
//                 '4' Smart On Main
// 'NumExtra'    - The number of extra bytes for a command.
// 'InfoByte'    - Returns the info byte and other information depending
//                 on the command.  The InfoByte size changes depending on
//                 the NumExtra which is buffer length * (NumExtra + 1)
// 'rst'         - True then reset the search for devices
//                 False don't reset search
//
//  Returns: TRUE(1)  State of DS2409 set and verified
//           FALSE(0) could not set the DS2409, perhaps device is not
//                    in contact
//
int SetSwitch1F(int portnum, uchar *SerialNum, int Swtch, int NumExtra,
                    uchar *InfoByte, int rst)
{
   int send_cnt,i,cmd;
   uchar send_block[50];

   if(owAccess(portnum))
   {
      send_cnt = 0;
      // add the match command
      send_block[send_cnt++] = 0x55;

      for (i = 0; i < 8; i++)
         send_block[send_cnt++] = SerialNum[i];

      // the command
      switch(Swtch)
      {
         case 0: // All lines off
            send_block[send_cnt++] = 0x66;
            cmd = 0x66;
            break;

         case 1: // Direct on Main
            send_block[send_cnt++] = 0xA5;
            cmd = 0xA5;
            break;

         case 2: // Smart on Auxilary
            send_block[send_cnt++] = 0x33;
            cmd = 0x33;
            break;

         case 3: // Status Read/Write
            send_block[send_cnt++] = 0x5A;
            cmd = 0x5A;

            // bytes 0-2: don't care
            // bytes 3-4: write control 0 to change status
            // byte 5: 0 = auto-control, 1 = manual mode
            // byte 6: 0 = main, 1 = auxiliary
            // byte 7: value to be written to control output, manual mode only
            // 0x00 default value
            *InfoByte = 0x00;
            send_block[send_cnt++] = *InfoByte;
            break;

         case 4: // Smart on Main
            send_block[send_cnt++] = 0xCC;
            cmd = 0xCC;
            break;

         default:
            return FALSE;
      }

      // extra bytes and confirmation
      for(i=0; i<=NumExtra; i++)
         send_block[send_cnt++] = 0xFF;

      // send the command string
      if(owBlock(portnum,rst,send_block,send_cnt))
      {
         // returned information for the info byte and command
         for(i=0; i<=NumExtra; i++)
            *(InfoByte+(NumExtra-i)) = send_block[send_cnt - (i+2)];

         // Set because for the read/write command the confirmation
         // byte is the same as the status byte
         if (Swtch == 3)
            cmd = send_block[send_cnt - 2];

         if (send_block[send_cnt - 1] == cmd)
            return TRUE;
      }
   }

   return FALSE;
}
Beispiel #17
0
//----------------------------------------------------------------------
// Read the temperature of a DS18B20 (family code 0x28)
//
// 'portnum'     - number 0 to MAX_PORTNUM-1.  This number was provided to
//                 OpenCOM to indicate the port number.
// 'SerialNum'   - Serial Number of DS18B20 to read temperature from
// 'Temp '       - pointer to variable where that temperature will be
//                 returned
//
// Returns: TRUE(1)  temperature has been read and verified
//          FALSE(0) could not read the temperature, perhaps device is not
//                   in contact
//
int ReadTemperature28(int portnum, uchar *SerialNum, float *Temp)
{
   uchar rt=FALSE;
   uchar send_block[30],lastcrc8;
   int send_cnt, tsht, i, loop=0;
   int power;

   // set the device serial number to the counter device
   owSerialNum(portnum,SerialNum,FALSE);

   for (loop = 0; loop < 2; loop ++)
   {
      // check if the chip is connected to VDD
      if (owAccess(portnum))
      {
         owWriteByte(portnum,0xB4);
         power = owReadByte(portnum);
      } 

      // access the device
      if (owAccess(portnum))
      {
         // send the convert command and if nesessary start power delivery
         if (power) { 
            if (!owWriteBytePower(portnum,0x44))
               return FALSE;
         } else {
            if (!owWriteByte(portnum,0x44))
               return FALSE;
         }

         // sleep for 1 second
         msDelay(1000);

         // turn off the 1-Wire Net strong pull-up
         if (power) { 
            if (owLevel(portnum,MODE_NORMAL) != MODE_NORMAL)
               return FALSE;
         }

         // access the device
         if (owAccess(portnum))
         {
            // create a block to send that reads the temperature
            // read scratchpad command
            send_cnt = 0;
            send_block[send_cnt++] = 0xBE;
            // now add the read bytes for data bytes and crc8
            for (i = 0; i < 9; i++)
               send_block[send_cnt++] = 0xFF;

            // now send the block
            if (owBlock(portnum,FALSE,send_block,send_cnt))
            {
               // initialize the CRC8
               setcrc8(portnum,0);
               // perform the CRC8 on the last 8 bytes of packet
               for (i = send_cnt - 9; i < send_cnt; i++)
                  lastcrc8 = docrc8(portnum,send_block[i]);

               // verify CRC8 is correct
               if (lastcrc8 == 0x00)
               {
                  // calculate the high-res temperature
            	  tsht =        send_block[2] << 8;
            	  tsht = tsht | send_block[1];
            	  if (tsht & 0x00001000)
            		  tsht = tsht | 0xffff0000;
                  *Temp = ((float) tsht)/16;
                  // success
                  rt = TRUE;
                  break;
               }
            }
         }
      }

   }

   // return the result flag rt
   return rt;
}
Beispiel #18
0
//----------------------------------------------------------------------
//  Main Test
//
int main() //short argc, char **argv)
{
   int PortNum=1,rslt,i,j,testcnt=0,length;
   uchar TempSerialNum[8];
   uchar tran_buffer[2000], filename[10];
   char return_msg[128];
   int portnum=0;

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

   // attempt to acquire the 1-Wire Net
   if (!owAcquire(portnum, argv[1], return_msg))
   {  
      printf("%s",return_msg);
      exit(1);
   }

   // success
   printf("%s",return_msg);

   //----------------------------------------
   // Introduction
   printf("\n/---------------------------------------------\n");
    printf("  The following is a test excersize of the\n"
          "  1-Wire Net public domain library Version 2.00.\n\n"
          "  This test was run using with 2 DS1920's (DS1820),\n"
          "  1 DS1971 (DS2430), and 1 DS1996.\n\n");

   //----------------------------------------
   // First the devices on the 1-Wire Net
   printf("\n/---------------------------------------------\n");
   printf("TEST%d: Searching for devices on 1-Wire Net\n",testcnt++);

   // find the first device (all devices not just alarming)
   rslt = owFirst(portnum,TRUE, FALSE);
   while (rslt)
   {
      // print the Serial Number of the device just found
      PrintSerialNum(portnum);

      // find the next device
      rslt = owNext(portnum,TRUE, FALSE);
   }

   //----------------------------------------
   // now search for the part with a 0x0C family code (DS1996)
   printf("\n/---------------------------------------------\n");
   printf("TEST%d: Set to find first device with 0x0C family code\n",testcnt++);

   owFamilySearchSetup(portnum,0x0C);

   // find the first 0x0c device
   TempSerialNum[0]=0;
   while (TempSerialNum[0]!=0x0c && owNext(portnum,TRUE,FALSE)) {
     owSerialNum(portnum,TempSerialNum,TRUE);
   }
   printf("search result %d\n",TempSerialNum[0]==0x0c);

   // print the Serial Number of the device just found
   PrintSerialNum(portnum);
   
   //----------------------------------------
   // Access a device and read ram
   printf("\n/---------------------------------------------\n");
   printf("TEST%d: Access the current device and read ram\n",testcnt++);

   printf("owAccess %d\n",owAccess(portnum));

   printf("Read Ram 0xF0: %02X\n",owTouchByte(portnum,0xF0));
   printf("Address0 0x00: %02X\n",owTouchByte(portnum,0x00));
   printf("Address1 0x00: %02X\n",owTouchByte(portnum,0x00));

   printf("Page 0: ");
   for (i = 0; i < 32; i++)
      printf("%02X ",owTouchByte(portnum,0xFF));
   printf("\n");

   //----------------------------------------
   // Read ram with owBlock
   printf("\n/---------------------------------------------\n");
   printf("TEST%d: Read ram with owBlock\n",testcnt++);
   for (i = 0; i < 32; i++)
      tran_buffer[i] = 0xFF;

   printf("owBlock %d\n",owBlock(portnum,FALSE,tran_buffer,32));
   printf("Page 1: ");
   for (i = 0; i < 32; i++)
      printf("%02X ",tran_buffer[i]);
   printf("\n");

   //----------------------------------------
   // Write a packet in each page of DS1996
   printf("\n/---------------------------------------------\n");
   printf("TEST%d: Place the DS1996 into overdrive\n",testcnt++);
   printf("owOverdriveAccess %d\n",owOverdriveAccess(portnum));

   //----------------------------------------
   // Write 4 packets with owWritePacketStd 
   printf("\n/---------------------------------------------\n");
   printf("TEST%d: Write 4 packets with owWritePacketStd\n",testcnt++);
     
   for (j = 0; j < 4; j++)
   {
      for (i = 0; i < 29; i++)
	tran_buffer[i] = (uchar)i + j;

      printf("Write page %d: %d\n",j,owWritePacketStd(portnum,j,tran_buffer,29,FALSE,FALSE));

      for (i = 0; i < 29; i++)
         tran_buffer[i] = 0;
   
      length = owReadPacketStd(portnum,TRUE,j,tran_buffer);

      printf("Read page %d: %d\n",j,length);

      for (i = 0; i < length; i++)
         printf("%02X",tran_buffer[i]);
      printf("\n");
   }

   //----------------------------------------
   // Write a file to DS1996
   printf("\n/---------------------------------------------\n");
   printf("TEST%d: Format and write a file (in overdrive)\n",testcnt++);
   sprintf(filename,"DEMO");
   // set the data to write
   for (i = 0; i < 2000; i++)
      tran_buffer[i] = i % 255;
   printf("Format and write file DEMO.000 %d\n",
	  owFormatWriteFile(portnum,filename,2000,tran_buffer));

   // clear the buffer
   for (i = 0; i < 2000; i++)
      tran_buffer[i] = 0x55;
   printf("Read file DEMO.000 %d\n",owReadFile(portnum,filename,tran_buffer));
   // print the data result
   for (i = 0; i < 2000; i++)
   {
      if ((i % 0x20) == 0)
         printf("\n%03X    ",i);
      printf("%02X",tran_buffer[i]);
   }
   printf("\n");
  
   //----------------------------------------
   // Turn off overdrive
   printf("\n/---------------------------------------------\n");
   printf("TEST%d: Turn off overdrive\n",testcnt++);
   printf("Set 1-Wire Net speed to normal %d\n",owSpeed(portnum,MODE_NORMAL));

   //----------------------------------------
   // Verify a device
   printf("\n/---------------------------------------------\n");
   printf("TEST%d: Verify the current device\n",testcnt++);

   printf("owVerify (normal) %d\n",owVerify(portnum,FALSE));
   printf("owVerify (alarm)  %d\n",owVerify(portnum,TRUE));

   //----------------------------------------
   // Skip the first family code found
   printf("\n/---------------------------------------------\n");
   printf("TEST%d: Skip the first family code found\n",testcnt++);
   
   // find the next device
   printf("search result of owFirst %d\n",owFirst(portnum,TRUE, FALSE));

   // print the Serial Number of the device just found
   PrintSerialNum(portnum);

   // skip the first family type found
   owSkipFamily(portnum);
   printf("owSkipFamily called\n");

   // find the next device
   printf("search result of owNext %d\n",owNext(portnum,TRUE, FALSE));
   
   // print the Serial Number of the device just found
   PrintSerialNum(portnum);

   //----------------------------------------
   // Find first family code (DS1920) and read temperature
   printf("\n/---------------------------------------------\n");
   printf("TEST%d: Find first family code (DS1920) and read temperature\n",testcnt++);

   // find the next device
   printf("search result of owFirst %d\n",owFirst(portnum,TRUE, FALSE));

   // print the Serial Number of the device just found
   PrintSerialNum(portnum);

   // send the convert temperature command
   printf("Convert temperature command %02X\n",owTouchByte(portnum,0x44));

   // set the 1-Wire Net to strong pull-up
   printf("Set power delivery %d\n",owLevel(portnum,MODE_STRONG5));

   // sleep for 1 second
   msDelay(1000);

   // turn off the 1-Wire Net strong pull-up
   printf("Disable power delivery %d\n",owLevel(portnum,MODE_NORMAL));

   // read the DS1920 temperature value
   printf("Access the DS1920 %d\n",owAccess(portnum));
   tran_buffer[0] = 0xBE;
   tran_buffer[1] = 0xFF;
   tran_buffer[2] = 0xFF;
   printf("Block to read temperature %d\n",owBlock(portnum,FALSE,tran_buffer,3));
   // interpret the result
   printf("result: DS1920 temperature read: %d C\n", (tran_buffer[1] |
           ((int)tran_buffer[2] << 8)) / 2);
  
   //----------------------------------------
   //  Verify the current device, could also be alarming
   printf("\n/---------------------------------------------\n");
   printf("TEST%d: Verify the current device, could also be alarming\n",testcnt++);

   printf("owVerify (normal) %d\n",owVerify(portnum,FALSE));
   printf("owVerify (alarm)  %d\n",owVerify(portnum,TRUE));

   //----------------------------------------
   // Test setting the Serial Number with owSerialNum
   printf("\n/---------------------------------------------\n");
   printf("TEST%d: Test setting the Serial Number with owSerialNum\n",testcnt++);

   // set the Serial Num to 0 to 7
   for (i = 0; i < 8; i++)
      TempSerialNum[i] = (uchar)i;
   owSerialNum(portnum,TempSerialNum,FALSE);

   // read back the Serial Number 
   PrintSerialNum(portnum);

   //----------------------------------------
   //  Verify the current device (should fail, no such device)
   printf("\n/---------------------------------------------\n");
   printf("TEST%d: Verify the current device (should fail, no such device)\n",testcnt++);

   printf("owVerify (normal) %d\n",owVerify(portnum,FALSE));
   printf("owVerify (alarm)  %d\n",owVerify(portnum,TRUE));

   // release the 1-Wire Net
   owRelease(portnum,return_msg);
   printf("%s",return_msg);
   exit(0);

   return 0;
}
Beispiel #19
0
//-------------------------------------------------------------------------
// Select the current device and attempt overdrive if possible.  Usable
// for both DS1963S and DS1961S.
//
// 'portnum'     - number 0 to MAX_PORTNUM-1.  This number is provided to
//                 indicate the symbolic port number.
//
// Return: TRUE - device selected
//         FALSE - device not select
//
SMALLINT SelectSHA(int portnum)
{
   int rt,cnt=0;

#ifdef __MC68K__
   // Used in when overdrive isn't...Used owVerify for overdrive
   rt = owAccess(portnum);
#else
   //\\//\\//\\//\\//\\//\\//\\//\\//\\//
   #ifdef DEBUG_DUMP
      uchar ROM[8];
      int i, mcnt;
      char msg[255];

      owSerialNum(portnum,ROM, TRUE);
      mcnt = sprintf(msg,"\n  Device select ");
      for (i = 0; i < 8; i++)
         mcnt += sprintf(msg + mcnt, "%02X",ROM[i]);
      mcnt += sprintf(msg + mcnt,"\n");
      printf("%s",msg);
   #endif
   //\\//\\//\\//\\//\\//\\//\\//\\//\\//

   // loop to access the device and optionally do overdrive
   do
   {
      rt = owVerify(portnum,FALSE);

      // check not present
      if (rt != 1)
      {
         // if in overdrive, drop back
         if (in_overdrive[portnum&0x0FF])
         {
            // set to normal speed
            if(MODE_NORMAL == owSpeed(portnum,MODE_NORMAL))
            	in_overdrive[portnum&0x0FF] = FALSE;
         }
      }
      // present but not in overdrive
      else if (!in_overdrive[portnum&0x0FF])
      {
         // put all devices in overdrive
         if (owTouchReset(portnum))
         {
            if (owWriteByte(portnum,0x3C))
            {
               // set to overdrive speed
               if(MODE_OVERDRIVE == owSpeed(portnum,MODE_OVERDRIVE))
               		in_overdrive[portnum&0x0FF] = TRUE;
            }
         }

         rt = 0;
      }
      else
         break;
   }
   while ((rt != 1) && (cnt++ < 3));
#endif

   return rt;
}
Beispiel #20
0
/*

  SetupVsens

  setup DS2438 to read Vsens voltage difference

  enable IAD, CA and EE of status configuration register ( page <0h> byte <0h>)

  Vsens A/D conversion occurs with a frequency of 36.41 measurements/sec
  once IAD is enabled ( set to '1'). No special command necessary.
 
  input parameters
    portnum    port number
    SNum       serial number of DS2438
    device     device (USB DS2490 or serial DS9097U)

*/
int SetupVsens(int portnum, uchar *SNum, char *device)
{
   uchar datablock[50];
   uchar conf_reg = 0x00;
   int send_cnt = 0;
   int i;
   ushort lastcrc8;
   int busybyte;
   double ti, tf;
   struct timezone tz;
   struct timeval  tv; 

   gettimeofday( &tv, &tz);
   ti = tv.tv_sec+1.0e-6*tv.tv_usec;

   /* enable IAD, CA and EE of configuration byte */
   conf_reg |= IAD | CA | EE;

   owSerialNum(portnum,SNum,FALSE);
   // Recall the Status/Configuration page
   // Recall command
   datablock[send_cnt++] = 0xB8;

   // Page to Recall
   datablock[send_cnt++] = 0x00;

   if(!owBlock(portnum,FALSE,datablock,send_cnt))
      return FALSE;

   send_cnt = 0;

   
   if(owAccess(portnum))
   {
      // Read the Status/Configuration byte
      // Read scratchpad command
      datablock[send_cnt++] = 0xBE;

      // Page for the Status/Configuration byte
      datablock[send_cnt++] = 0x00;

      for(i=0;i<9;i++)
         datablock[send_cnt++] = 0xFF;

      if(owBlock(portnum,FALSE,datablock,send_cnt))
      {
         setcrc8(portnum,0);

         for(i=2;i<send_cnt;i++)
            lastcrc8 = docrc8(portnum,datablock[i]);

         if(lastcrc8 != 0x00)
            return FALSE;
      }//Block
      else
         return FALSE;

      if ( datablock[2] & conf_reg ) {
        syslog(LOG_DEBUG, "SetupVsens: IAD, CA and EE are set: return!\n");

	gettimeofday( &tv, &tz);
	tf = tv.tv_sec+1.0e-6*tv.tv_usec;
        syslog(LOG_DEBUG,
	       "SetupVsens: elapsed time: %f\n", tf -ti);

        return TRUE;
      } else {
	  syslog(LOG_DEBUG,
		 "SetupVsens: IAD, CA and EE are not set. Continue to setup\n");
      }

   }//Access

   if(owAccess(portnum))
   {
      send_cnt = 0;
      // Write the Status/Configuration byte
      // Write scratchpad command
      datablock[send_cnt++] = 0x4E;

      // Write page
      datablock[send_cnt++] = 0x00;

      // IAD, CA and EE set to "1"
      datablock[send_cnt++] |= conf_reg;

      // do not change the rest
      for(i=0;i<7;i++)
         datablock[send_cnt++] = datablock[i+3];

      if(owBlock(portnum,FALSE,datablock,send_cnt))
      {
         send_cnt = 0;

         if(owAccess(portnum))
         {
            // Copy the Status/Configuration byte
            // Copy scratchpad command
            datablock[send_cnt++] = 0x48;

            // Copy page
            datablock[send_cnt++] = 0x00;

            if(owBlock(portnum,FALSE,datablock,send_cnt))
            {
               busybyte = owReadByte(portnum);
         
               while(busybyte == 0)
                  busybyte = owReadByte(portnum);

	       gettimeofday( &tv, &tz);
	       tf = tv.tv_sec+1.0e-6*tv.tv_usec;
               syslog(LOG_DEBUG,
		      "SetupVsens: elapsed time: %f\n", tf -ti);
               return TRUE;
            }//Block
         }//Access
      }//Block

   }//Access

   return FALSE;
}
Beispiel #21
0
/**
 * Writes new data to the secure subkey
 *
 * portnum   the port number of the port being used for the 
 *           1-Wire Network.
 * key       number indicating the key to be written: 0, 1, or 2
 * addr      address to start writing at ( 0x00 to 0x3F )
 * passwd    passwird for the subkey
 * data      data to be written
 * len       the length of the data to be written
 *
 * return    TRUE if the write was successful
 */
SMALLINT writeSubkey (int portnum, int key, int addr, uchar *passwd,
                      uchar *data, int len)
{
   uchar buffer[96];
   int i;

   if(owAccess(portnum))
   {
      //confirm key names and passwd within legal parameters
      if (key > 0x03)
      {
         OWERROR(OWERROR_KEY_OUT_OF_RANGE);
         return FALSE;
      }

      if ((addr < 0x00) || (addr > 0x3F))
      {
         OWERROR(OWERROR_WRITE_OUT_OF_RANGE);
         return FALSE;
      }

      buffer[0] = WRITE_SUBKEY_COMMAND;
      buffer[1] = (uchar) ((key << 6) | addr);
      buffer[2] = (uchar) (~buffer [1]);

      //prepare buffer to receive 8 bytes of the identifier
      for(i = 3; i < 11; i++)
         buffer[i] = 0xFF;

      //prepare same subkey identifier for confirmation
      for(i=0;i<8;i++)
         buffer[i+11] = passwd[i];

      //prepare data to write
      for(i=0;i<len;i++)
         buffer[i+19] = data[i];
      
      //send command block
      if(len+19 > 64)
      {
         if(!owBlock(portnum,FALSE,&buffer[0],64))
         {
            OWERROR(OWERROR_BLOCK_FAILED);
            return FALSE;
         }
         else if(!owBlock(portnum,FALSE,&buffer[64],(len+19)-64))
         {
            OWERROR(OWERROR_BLOCK_FAILED);
            return FALSE;
         }
      }
      else
      {
         if(!owBlock(portnum,FALSE,buffer,len+19))
         {
            OWERROR(OWERROR_BLOCK_FAILED);
            return FALSE;
         }
      }
   }
   else
   {
      OWERROR(OWERROR_DEVICE_SELECT_FAIL);
      return FALSE;
   }

   return TRUE;
}
Beispiel #22
0
//----------------------------------------------------------------------
//  Use the script to perform a step and return.
//
int ThermoStep(int portnum, ThermoStateType *ThermoState, 
               ThermoScript *StateScript, int *SubStep, 
               int *Status, int *ErrorCount, char *msg)
{
   short  rslt;
   static int read_page_num, read_pages, write_addr, write_len;
   static uchar *read_buf, *write_buf;
   static uchar tbuf[5];

   ErrorCount; // hush the compiler

   // do the current step
   switch (StateScript->Step)
   {
      // the operation is complete      
      case ST_FINISH:
         sprintf(msg,"Operation complete");
         *Status = STATUS_COMPLETE;
         break;      

      // read the mission status page
      case ST_READ_STATUS:
         read_page_num = STATUS_PAGE;
         read_pages = 1;
         read_buf = ThermoState->MissStat.status_raw;
         sprintf(msg,"Ready to read status page %d",
                      read_page_num);
         *Status = STATUS_STEP_COMPLETE;
         break;      

      // set up to read the alarm registers
      case ST_READ_ALARM:
         read_page_num = 17;
         read_pages = 3;
         read_buf = ThermoState->AlarmData.alarm_raw;
         sprintf(msg,"Ready to read alarm pages %d to %d",
                      read_page_num, read_page_num + read_pages - 1);
         *Status = STATUS_STEP_COMPLETE;
         break;
         
      // set up to read the histogram data
      case ST_READ_HIST:
         read_page_num = 64;
         read_pages = 4;
         read_buf = ThermoState->HistData.hist_raw;
         sprintf(msg,"Ready to read histogram pages %d to %d",
                      read_page_num, read_page_num + read_pages - 1);
         *Status = STATUS_STEP_COMPLETE;
         break;

      // set up to read the log data
      case ST_READ_LOG:
         read_page_num = 128;
         read_pages = 64;
         read_buf = ThermoState->LogData.log_raw;
         sprintf(msg,"Ready to read log pages %d to %d",
                      read_page_num, read_page_num + read_pages - 1);
         *Status = STATUS_STEP_COMPLETE;
         break;

      // read the specified pages
      case ST_READ_PAGES:
         // check for last page
         if (*SubStep == 0)
            // set the sub-step to the current page being read
            *SubStep =  read_page_num;
         // read the status page
         rslt = ReadPages(portnum, read_page_num, read_pages, SubStep, read_buf);
         if (rslt == FALSE)
         {
            sprintf(msg,"Thermochron not on 1-Wire Net");
            *Status = STATUS_INPROGRESS;
         }
         else 
         {
            sprintf(msg,"Pages read from Thermochron");
            *Status = STATUS_STEP_COMPLETE;
         }
         break;      

      // setup the clear memory
      case ST_CLEAR_SETUP:
         // create a small buff to write to start the clear memory
         tbuf[0] = 0x40;
         write_buf = &tbuf[0];
         write_len = 1;
         write_addr = 0x20E;
         sprintf(msg,"Write to setup clear memory");
         *Status = STATUS_STEP_COMPLETE;
         break;

      // clear the memory
      case ST_CLEAR_MEM:
         // set the clear memory command (not check return because verify)        
         owAccess(portnum);
         owWriteByte(portnum,0x3C);
         msDelay(3);
         owTouchReset(portnum);
         sprintf(msg,"Clear memory command sent");
         *Status = STATUS_STEP_COMPLETE;
         break;

      // clear the memory
      case ST_CLEAR_VERIFY:
         // look at the memory clear bit
         if ((ThermoState->MissStat.status_raw[0x14] & 0x40) == 0x40)
         {
            sprintf(msg,"Memory is clear");
            *Status = STATUS_STEP_COMPLETE;
            break;
         }
         else
         {
            sprintf(msg,"Memory did NOT clear");
            *Status = STATUS_ERROR_TRANSIENT;
            break;
         }
         break;      

      // setup write time, clock alarm, control, trips
      case ST_WRITE_TIME:
         // create the write buffer
         FormatMission(&ThermoState->MissStat);
         write_buf = &ThermoState->MissStat.status_raw[0x00];
         write_len = 13;
         write_addr = 0x200;
         sprintf(msg,"Write time, clock alarm, and trips setup");
         *Status = STATUS_STEP_COMPLETE;
         break;

      // write the control, mission delay and clear flags
      case ST_WRITE_CONTROL:
         write_buf = &ThermoState->MissStat.status_raw[0x0E];
         write_len = 7;
         write_addr = 0x20E;
         sprintf(msg,"Write control, mission delay, clear flags setup");
         *Status = STATUS_STEP_COMPLETE;
         break;

      case ST_WRITE_RATE:
         write_buf = &ThermoState->MissStat.status_raw[0x0D];
         write_len = 1;
         write_addr = 0x20D;
         sprintf(msg,"Write sample rate setup");
         *Status = STATUS_STEP_COMPLETE;
         break;

      // write the specified memory location 
      case ST_WRITE_MEM:
         if (WriteMemory(portnum, write_buf, write_len, write_addr))
         {
            sprintf(msg,"Memory written to Thermochron");
            *Status = STATUS_STEP_COMPLETE;
         }
         else
         {
            sprintf(msg,"Thermochron not on 1-Wire Net");
            *Status = STATUS_INPROGRESS;
         }
      default:
           break;
   }

   return *Status;
}