Ejemplo n.º 1
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;
}
Ejemplo n.º 2
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;
}
Ejemplo n.º 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;
}
Ejemplo n.º 4
0
/* 0 = success, -1 = error */
int8_t get_persistent_mac(uint8_t portnum, uint8_t * mac)
{
	uint8_t read_buffer[32];
	uint8_t i;
	int8_t out;

	out = -1;

	if (devsnum == 0)
		return out;

	for (i = 0; i < devsnum; ++i) {
//#if DEBUG_PMAC
		mprintf("Found device: "
			"%02x:%02x:%02x:%02x:%02x:%02x:%02x:%02x\n",
		FamilySN[i][7], FamilySN[i][6], FamilySN[i][5], FamilySN[i][4],
		FamilySN[i][3], FamilySN[i][2], FamilySN[i][1], FamilySN[i][0]);
//#endif

		/* If there is a temperature sensor, use it for the low three MAC values */
		if (FamilySN[i][0] == 0x28 || FamilySN[i][0] == 0x42) {
			mac[3] = FamilySN[i][3];
			mac[4] = FamilySN[i][2];
			mac[5] = FamilySN[i][1];
			out = 0;
#if DEBUG_PMAC
			mprintf("Using temperature ID for MAC\n");
#endif
		}

		/* If there is an EEPROM, read page 0 for the MAC */
		if (FamilySN[i][0] == 0x43) {
			owLevel(portnum, MODE_NORMAL);
			if (ReadMem43(portnum, FamilySN[i], EEPROM_MAC_PAGE,
				      read_buffer) == TRUE) {
				if (read_buffer[0] == 0 && read_buffer[1] == 0
				    && read_buffer[2] == 0) {
					/* Skip the EEPROM since it has not been programmed! */
#if DEBUG_PMAC
					mprintf("EEPROM has not been "
						"programmed with a MAC\n");
#endif
				} else {
					memcpy(mac, read_buffer, 6);
					out = 0;
#if DEBUG_PMAC
					mprintf("Using EEPROM page: "
						"%02x:%02x:%02x:%02x:%02x:%02x\n",
					     mac[0], mac[1], mac[2], mac[3],
					     mac[4], mac[5]);
#endif
				}
			}
		}
	}

	return out;
}
Ejemplo n.º 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;
}
Ejemplo n.º 6
0
//--------------------------------------------------------------------------
// Send 1 bit of communication to the 1-Wire Net and verify that the
// response matches the 'applyPowerResponse' bit and apply power delivery
// to the 1-Wire net.  Note that some implementations may apply the power
// first and then turn it off if the response is incorrect.
//
// 'portnum'  - number 0 to MAX_PORTNUM-1.  This number was provided to
//              OpenCOM to indicate the port number.
// 'applyPowerResponse' - 1 bit response to check, if correct then start
//                        power delivery 
//
// Returns:  TRUE: bit written and response correct, strong pullup now on
//           FALSE: response incorrect
//
SMALLINT owReadBitPower(int portnum, SMALLINT applyPowerResponse)
{
   if(owTouchBit(portnum,0x01) != applyPowerResponse)
      return FALSE;

   if(owLevel(portnum,MODE_STRONG5) != MODE_STRONG5)
      return FALSE;

   return TRUE;
}
Ejemplo n.º 7
0
//--------------------------------------------------------------------------
// Send 8 bits of communication to the 1-Wire Net and verify that the
// 8 bits read from the 1-Wire Net is the same (write operation).  
// The parameter 'sendbyte' least significant 8 bits are used.  After the
// 8 bits are sent change the level of the 1-Wire net.
//
// 'portnum'  - number 0 to MAX_PORTNUM-1.  This number was provided to
//              OpenCOM to indicate the port number.
// 'sendbyte' - 8 bits to send (least significant byte)
//
// Returns:  TRUE: bytes written and echo was the same
//           FALSE: echo was not the same 
//
SMALLINT owWriteBytePower(int portnum, SMALLINT sendbyte)
{
   if(owTouchByte(portnum,sendbyte) != sendbyte)
      return FALSE;

   if(owLevel(portnum,MODE_STRONG5) != MODE_STRONG5)
      return FALSE;

   return TRUE;
}
Ejemplo n.º 8
0
//--------------------------------------------------------------------------
// Reset all of the devices on the 1-Wire Net and return the result.
//
// 'portnum'     - number 0 to MAX_PORTNUM-1.  This number is provided to
//                 indicate the symbolic port number.
//
// Returns: TRUE(1):  presense pulse(s) detected, device(s) reset
//          FALSE(0): no presense pulses detected
//
SMALLINT owTouchReset(int portnum)
{
   SETUP_PACKET setup;
   ULONG nOutput = 0;
   SMALLINT present,vpp;

   // make sure strong pullup is not on
   if (USBLevel[portnum] == MODE_STRONG5)
      owLevel(portnum, MODE_NORMAL);

   // construct command
   setup.RequestTypeReservedBits = 0x40;
   setup.Request = COMM_CMD;
   setup.Value = COMM_1_WIRE_RESET | COMM_F | COMM_IM | COMM_SE;
   setup.Index = (USBSpeed[portnum] == MODE_OVERDRIVE) ?
                  ONEWIREBUSSPEED_OVERDRIVE : ONEWIREBUSSPEED_FLEXIBLE;
   setup.Length = 0;
   setup.DataOut = FALSE;
   // call the driver
   if (!DeviceIoControl(usbhnd[portnum],
					    DS2490_IOCTL_VENDOR,
					    &setup,
					    sizeof(SETUP_PACKET),
					    NULL,
					    0,
					    &nOutput,
					    NULL))
   {
      // failure
      OWERROR(OWERROR_RESET_FAILED);
      AdapterRecover(portnum);
      return FALSE;
   }
   else
   {
      // extra delay for alarming DS1994/DS2404 complience
      if ((FAMILY_CODE_04_ALARM_TOUCHRESET_COMPLIANCE) && (USBSpeed[portnum] != MODE_OVERDRIVE))
         Sleep(5);

      // success, check for shorts
      if (DS2490ShortCheck(usbhnd[portnum], &present,&vpp))
      {
         USBVpp[portnum] = vpp;     
         return present;
      }
      else
      {
         OWERROR(OWERROR_OW_SHORTED);
         // short occuring
         msDelay(300);
         AdapterRecover(portnum);
         return FALSE;
      }
   }
}
Ejemplo n.º 9
0
//----------------------------------------------------------------------
// Perform a overdrive MATCH command to select the 1-Wire device with
// the address in the ID data register.
//
// 'portnum'     - number 0 to MAX_PORTNUM-1.  This number is provided to
//                 indicate the symbolic port number.
//
// Returns:  TRUE: If the device is present on the 1-Wire Net and
//                 can do overdrive then the device is selected.
//           FALSE: Device is not present or not capable of overdrive.
//
//  *Note: This function could be converted to send DS2480
//         commands in one packet.
//
BYTE owOverdriveAccess(BYTE portnum)
{
   BYTE sendpacket[8];
   BYTE i, bad_echo = FALSE;

   owSetCurrentPort(portnum);

   // make sure normal level
   owLevel(owCurrentPortnum, MODE_NORMAL);

   // force to normal communication speed
   owSpeed(owCurrentPortnum, MODE_NORMAL);

   // call the 1-Wire Net reset function
   if(owTouchReset(owCurrentPortnum)) {
     // send the match command 0x69
     if(owWriteByte(owCurrentPortnum, 0x69)) {
       // switch to overdrive communication speed
       owSpeed(owCurrentPortnum, MODE_OVERDRIVE);

       // create a buffer to use with block function
       // Serial Number
       for(i = 0; i < 8; i++) {
         sendpacket[i] = owNetCurrent.SerialNum[i];
       }
       
       // send/recieve the transfer buffer
       if(owBlock(owCurrentPortnum, FALSE, sendpacket,8)) {
         // verify that the echo of the writes was correct
         for(i = 0; i < 8; i++) {
           if(sendpacket[i] != owNetCurrent.SerialNum[i]) {
             bad_echo = TRUE;
           }
         }
         // if echo ok then success
         if(!bad_echo) {
           return TRUE;
         } else {
           OWERROR(OWERROR_WRITE_VERIFY_FAILED);
         }
       } else {
         OWERROR(OWERROR_BLOCK_FAILED);
       }
     } else {
       OWERROR(OWERROR_WRITE_BYTE_FAILED);
     }
   } else {
     OWERROR(OWERROR_NO_DEVICES_ON_NET);
   }

   // failure, force back to normal communication speed
   owSpeed(owCurrentPortnum, MODE_NORMAL);

   return FALSE;
}
Ejemplo n.º 10
0
Archivo: owllu.c Proyecto: bcl/digitemp
//--------------------------------------------------------------------------
// This procedure creates a fixed 480 microseconds 12 volt pulse
// on the 1-Wire Net for programming EPROM iButtons.
//
// 'portnum'  - number 0 to MAX_PORTNUM-1.  This number was provided to
//              OpenCOM to indicate the port number.
//
// Returns:  TRUE  successful
//           FALSE program voltage not available
//
SMALLINT owProgramPulse(int portnum)
{
   uchar sendpacket[10],readbuffer[10];
   uchar sendlen=0;

   // check if programming voltage available
   if (!ProgramAvailable[portnum])
      return FALSE;

   // make sure normal level
   owLevel(portnum,MODE_NORMAL);

   // check if correct mode
   if (UMode[portnum] != MODSEL_COMMAND)
   {
      UMode[portnum] = MODSEL_COMMAND;
      sendpacket[sendlen++] = MODE_COMMAND;
   }

   // set the SPUD time value
   sendpacket[sendlen++] = CMD_CONFIG | PARMSEL_12VPULSE | PARMSET_512us;

   // pulse command
   sendpacket[sendlen++] = CMD_COMM | FUNCTSEL_CHMOD | BITPOL_12V | SPEEDSEL_PULSE;

   // flush the buffers
   FlushCOM(portnum);

   // send the packet
   if (WriteCOM(portnum,sendlen,sendpacket))
   {
      // read back the 2 byte response
      if (ReadCOM(portnum,2,readbuffer) == 2)
      {
         // check response byte
         if (((readbuffer[0] | CMD_CONFIG) ==
                (CMD_CONFIG | PARMSEL_12VPULSE | PARMSET_512us)) &&
             ((readbuffer[1] & 0xFC) ==
                (0xFC & (CMD_COMM | FUNCTSEL_CHMOD | BITPOL_12V | SPEEDSEL_PULSE))))
            return TRUE;
      }
      else
         OWERROR(OWERROR_READCOM_FAILED);
   }
   else
      OWERROR(OWERROR_WRITECOM_FAILED);

   // an error occurred so re-sync with DS2480
   DS2480Detect(portnum);

   return FALSE;
}
Ejemplo n.º 11
0
Archivo: owllu.c Proyecto: bcl/digitemp
//--------------------------------------------------------------------------
// Reset all of the devices on the 1-Wire Net and return the result.
//
// 'portnum'    - number 0 to MAX_PORTNUM-1.  This number was provided to
//                OpenCOM to indicate the port number.
//
// Returns: TRUE(1):  presense pulse(s) detected, device(s) reset
//          FALSE(0): no presense pulses detected
//
// WARNING: This routine will not function correctly on some
//          Alarm reset types of the DS1994/DS1427/DS2404 with
//          Rev 1,2, and 3 of the DS2480/DS2480B.
//
SMALLINT owTouchReset(int portnum)
{
   uchar readbuffer[10],sendpacket[10];
   uchar sendlen=0;

   // make sure normal level
   owLevel(portnum,MODE_NORMAL);

   // check if correct mode
   if (UMode[portnum] != MODSEL_COMMAND)
   {
      UMode[portnum] = MODSEL_COMMAND;
      sendpacket[sendlen++] = MODE_COMMAND;
   }

   // construct the command
   sendpacket[sendlen++] = (uchar)(CMD_COMM | FUNCTSEL_RESET | USpeed[portnum]);

   // flush the buffers
   FlushCOM(portnum);

   // send the packet
   if (WriteCOM(portnum,sendlen,sendpacket))
   {
      // read back the 1 byte response
      if (ReadCOM(portnum,1,readbuffer) == 1)
      {
         // make sure this byte looks like a reset byte
         if (((readbuffer[0] & RB_RESET_MASK) == RB_PRESENCE) ||
             ((readbuffer[0] & RB_RESET_MASK) == RB_ALARMPRESENCE))
         {
            // check if programming voltage available
            ProgramAvailable[portnum] = ((readbuffer[0] & 0x20) == 0x20);
            return TRUE;
         }
         else
            OWERROR(OWERROR_RESET_FAILED);

      }
      else
         OWERROR(OWERROR_READCOM_FAILED);
   }
   else
      OWERROR(OWERROR_WRITECOM_FAILED);

   // an error occurred so re-sync with DS2480
   DS2480Detect(portnum);

   return FALSE;
}
Ejemplo n.º 12
0
//--------------------------------------------------------------------------
// Read 8 bits of communication to the 1-Wire Net and then turn on
// power delivery.
//
// 'portnum'  - number 0 to MAX_PORTNUM-1.  This number was provided to
//              OpenCOM to indicate the port number.
//
// Returns:  byte read
//           FALSE: power delivery failed
//
SMALLINT owReadBytePower(int portnum)
{
   SMALLINT getbyte;

   if (!owHasPowerDelivery(portnum))
      return FALSE;

   getbyte = owTouchByte(portnum,0xFF);

   if (owLevel(portnum,MODE_STRONG5) != MODE_STRONG5)
      return FALSE;

   return getbyte;
}
Ejemplo n.º 13
0
//--------------------------------------------------------------------------
// Send 8 bits of communication to the 1-Wire Net and verify that the
// 8 bits read from the 1-Wire Net is the same (write operation).
// The parameter 'sendbyte' least significant 8 bits are used.
//
// 'portnum'    - number 0 to MAX_PORTNUM-1.  This number is provided to
//                indicate the symbolic port number.
// 'sendbyte'   - 8 bits to send (least significant byte)
//
// Returns:  TRUE: bytes written and echo was the same
//           FALSE: echo was not the same
//
SMALLINT owTouchByte(int portnum, SMALLINT sendbyte)
{
   SETUP_PACKET setup;
   ULONG nOutput = 0;
   WORD nBytes;   
   BYTE buf[2];

   // make sure strong pullup is not on
   if (USBLevel[portnum] == MODE_STRONG5)
      owLevel(portnum, MODE_NORMAL);

   // set to do touchbyte
   setup.RequestTypeReservedBits = 0x40;
   setup.Request = COMM_CMD;
   setup.Value = COMM_BYTE_IO | COMM_IM;
   setup.Index = sendbyte & 0xFF;  
   setup.Length = 0;
   setup.DataOut = FALSE;

   // call the driver
   if (!DeviceIoControl(usbhnd[portnum],
					    DS2490_IOCTL_VENDOR,
					    &setup,
					    sizeof(SETUP_PACKET),
					    NULL,
					    0,
					    &nOutput,
					    NULL))
   {
      // failure
      OWERROR(OWERROR_ADAPTER_ERROR);
      AdapterRecover(portnum);
      return 0;
   }
   else
   {
      // success, read the result
      nBytes = 1;
      if (DS2490Read(usbhnd[portnum], buf, &nBytes))
         return buf[0];
      else
      {
         OWERROR(OWERROR_ADAPTER_ERROR);
         AdapterRecover(portnum);
         return 0;
      }
   }
}
Ejemplo n.º 14
0
Archivo: owllu.c Proyecto: bcl/digitemp
//--------------------------------------------------------------------------
// Send 1 bit of communication to the 1-Wire Net and return the
// result 1 bit read from the 1-Wire Net.  The parameter 'sendbit'
// least significant bit is used and the least significant bit
// of the result is the return bit.
//
// 'portnum' - number 0 to MAX_PORTNUM-1.  This number was provided to
//             OpenCOM to indicate the port number.
// 'sendbit' - the least significant bit is the bit to send
//
// Returns: 0:   0 bit read from sendbit
//          1:   1 bit read from sendbit
//
SMALLINT owTouchBit(int portnum, SMALLINT sendbit)
{
   uchar readbuffer[10],sendpacket[10];
   uchar sendlen=0;

   // make sure normal level
   owLevel(portnum,MODE_NORMAL);

   // check if correct mode
   if (UMode[portnum] != MODSEL_COMMAND)
   {
      UMode[portnum] = MODSEL_COMMAND;
      sendpacket[sendlen++] = MODE_COMMAND;
   }

   // construct the command
   sendpacket[sendlen] = (sendbit != 0) ? BITPOL_ONE : BITPOL_ZERO;
   sendpacket[sendlen++] |= CMD_COMM | FUNCTSEL_BIT | USpeed[portnum];

   // flush the buffers
   FlushCOM(portnum);

   // send the packet
   if (WriteCOM(portnum,sendlen,sendpacket))
   {
      // read back the response
      if (ReadCOM(portnum,1,readbuffer) == 1)
      {
         // interpret the response
         if (((readbuffer[0] & 0xE0) == 0x80) &&
             ((readbuffer[0] & RB_BIT_MASK) == RB_BIT_ONE))
            return 1;
         else
            return 0;
      }
      else
         OWERROR(OWERROR_READCOM_FAILED);
   }
   else
      OWERROR(OWERROR_WRITECOM_FAILED);

   // an error occurred so re-sync with DS2480
   DS2480Detect(portnum);

   return 0;
}
Ejemplo n.º 15
0
Archivo: owllu.c Proyecto: bcl/digitemp
//--------------------------------------------------------------------------
// Send 8 bits of communication to the 1-Wire Net and return the
// result 8 bits read from the 1-Wire Net.  The parameter 'sendbyte'
// least significant 8 bits are used and the least significant 8 bits
// of the result is the return byte.
//
// 'portnum'  - number 0 to MAX_PORTNUM-1.  This number was provided to
//              OpenCOM to indicate the port number.
// 'sendbyte' - 8 bits to send (least significant byte)
//
// Returns:  8 bits read from sendbyte
//
SMALLINT owTouchByte(int portnum, SMALLINT sendbyte)
{
   uchar readbuffer[10],sendpacket[10];
   uchar sendlen=0;

   // make sure normal level
   owLevel(portnum,MODE_NORMAL);

   // check if correct mode
   if (UMode[portnum] != MODSEL_DATA)
   {
      UMode[portnum] = MODSEL_DATA;
      sendpacket[sendlen++] = MODE_DATA;
   }

   // add the byte to send
   sendpacket[sendlen++] = (uchar)sendbyte;

   // check for duplication of data that looks like COMMAND mode
   if (sendbyte ==(SMALLINT)MODE_COMMAND)
      sendpacket[sendlen++] = (uchar)sendbyte;

   // flush the buffers
   FlushCOM(portnum);

   // send the packet
   if (WriteCOM(portnum,sendlen,sendpacket))
   {
      // read back the 1 byte response
      if (ReadCOM(portnum,1,readbuffer) == 1)
      {
          // return the response
          return (int)readbuffer[0];
      }
      else
         OWERROR(OWERROR_READCOM_FAILED);
   }
   else
      OWERROR(OWERROR_WRITECOM_FAILED);

   // an error occurred so re-sync with DS2480
   DS2480Detect(portnum);

   return 0;
}
Ejemplo n.º 16
0
//--------------------------------------------------------------------------
// This procedure creates a fixed 480 microseconds 12 volt pulse
// on the 1-Wire Net for programming EPROM iButtons.
//
// 'portnum'     - number 0 to MAX_PORTNUM-1.  This number is provided to
//                 indicate the symbolic port number.
//
// Returns:  TRUE  successful
//           FALSE program voltage not available
//
SMALLINT owProgramPulse(int portnum)
{
   SETUP_PACKET setup;
   ULONG nOutput = 0;

   // check if Vpp available
   if (!USBVpp[portnum])
      return FALSE;

   // make sure strong pullup is not on
   if (USBLevel[portnum] == MODE_STRONG5)
      owLevel(portnum, MODE_NORMAL);

   // send the pulse (already enabled)
   setup.RequestTypeReservedBits = 0x40;
   setup.Request = COMM_CMD;
   setup.Value = COMM_PULSE | COMM_IM | COMM_TYPE;
   setup.Index = 0;
   setup.Length = 0;
   setup.DataOut = FALSE;
   // call the driver
   if (!DeviceIoControl(usbhnd[portnum],
					    DS2490_IOCTL_VENDOR,
					    &setup,
					    sizeof(SETUP_PACKET),
					    NULL,
					    0,
					    &nOutput,
					    NULL))
   {
      // failure
      OWERROR(OWERROR_ADAPTER_ERROR);
      AdapterRecover(portnum);
      return FALSE;
   }
   else
      return TRUE;
}
Ejemplo n.º 17
0
/* 0 = success, -1 = error */
int8_t set_persistent_mac(uint8_t portnum, uint8_t * mac)
{
	uint8_t FamilySN[1][8];
	uint8_t write_buffer[32];

	// Find the device (only the first one, we won't write MAC to all EEPROMs out there, right?)
	if (FindDevices(portnum, &FamilySN[0], 0x43, 1) == 0)
		return -1;

	memset(write_buffer, 0, sizeof(write_buffer));
	memcpy(write_buffer, mac, 6);

#if DEBUG_PMAC
	mprintf("Writing to EEPROM\n");
#endif

	/* Write the last EEPROM with the MAC */
	owLevel(portnum, MODE_NORMAL);
	if (Write43(portnum, FamilySN[0], EEPROM_MAC_PAGE, write_buffer) ==
	    TRUE)
		return 0;

	return -1;
}
Ejemplo n.º 18
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;
      
}
Ejemplo n.º 19
0
//----------------------------------------------------------------------
//  Main AlarmSearch Test
//
void main(void)
{
  BYTE portnum;

  //----------------------------------------
  ADCON1      = 0x07;     // PortB digital

  bTRD6       = 1;        // RD6 = Serial Input
  bTRD7       = 0;        // RD7 = Serial Output
  bLD7        = 1;

  printf("\nEnumeration (AlarmSearch) test\n%");

// Initialise the OneWire nets and the temperature devices
  for(portnum = 0; portnum < MAX_PORTNUM; portnum++) {
    // attempt to acquire the 1-Wire Nets
    if (!owAcquire(portnum,NULL)) {
      printf("Acquire failed for OneWire net %d.\n%", portnum);
    } else {
      // Reset the devices
      owTouchReset(portnum);
      // Address all devices on this net
      owWriteByte(portnum, OW_SKIPROM);
      // send the WriteScratchpad command to set AlarmTemps
      owWriteByte(portnum, OW_WRITESCRATCHPAD);
      owWriteByte(portnum, 19);  // Valid RoomTemperature
      owWriteByte(portnum, 21);  //   19 <= Temp < 21
    }
  }
  
  while(1) {
    // Do a temp conversion on all DS18S20's on all nets simultanously
    for(portnum = 0; portnum < MAX_PORTNUM; portnum++) {
      // Reset the devices
      owTouchReset(portnum);
      // Address all devices on this net
      owWriteByte(portnum, OW_SKIPROM);
      // send the convert command and start power delivery
      if(!owWriteBytePower(portnum, OW_CONVERTT)) {
        printf("\nNo Strong PullUp on net %d. Trying without SPU.\n%", portnum);
        owWriteByte(portnum, OW_CONVERTT);
      }
    }

    // Conversion takes 750 milliseconds max. Safer is 751ms
    Wait(751);
    
    // turn off the 1-Wire Net strong pull-up
    for(portnum = 0; portnum < MAX_PORTNUM; portnum++) {
      owLevel(portnum, MODE_NORMAL);
    }
    
    for(portnum = 0; portnum < MAX_PORTNUM; portnum++) {
      // Find the device(s) in AlarmState
      printf("\nEnumeration for net %d.\n%", portnum);
      if(!owFirst(portnum, TRUE, TRUE)) {
        printf("No devices in AlarmState found on this net.\n%");
      } else {
        do {
          printf("Device in AlarmState found: 0x%02X%02X%02X%02X%02X%02X%02X%02X\n%",
                 owNetCurrent.SerialNum[7],
                 owNetCurrent.SerialNum[6],
                 owNetCurrent.SerialNum[5],
                 owNetCurrent.SerialNum[4],
                 owNetCurrent.SerialNum[3],
                 owNetCurrent.SerialNum[2],
                 owNetCurrent.SerialNum[1],
                 owNetCurrent.SerialNum[0]);
        } while(owNext(portnum, TRUE, TRUE));
      }
    }   
     
    Wait(10);  // Just a convenient way to set a breakpoint
  }
}
Ejemplo n.º 20
0
//----------------------------------------------------------------------
// Read the temperature of a DS18B20
//
// '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 ReadTemperature28(int portnum, uchar *SerialNum, float *Temp)
{
   uchar rt=FALSE;
   uchar send_block[30],lastcrc8;
   int send_cnt, tsht, i, loop=0;
   float tmp,cr,cpc;

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

   for (loop = 0; loop < 2; loop ++)
   {
      // access the device
      if (owAccess(portnum))
      {
         // send the convert command and start power delivery
         if (!owWriteBytePower(portnum,0x44))
            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_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[1] >> 4) + ((send_block[2] & 0x07) << 4);
                  if (send_block[2] & 0x80)
                     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;

	  	  // TODO: Check config register

		  if(send_block[1] & 0x01) {
			tmp += 0.0625f;
                  }
		  else if(send_block[1] & 0x02) {
			tmp += 0.125f;
                  }
		  else if(send_block[1] & 0x04) {
			tmp += 0.25f;
                  }
		  else if(send_block[1] & 0x08) {
			tmp += 0.5f;
                  }

                  *Temp = tmp;
                  // success
                  rt = TRUE;
                  break;
               }
            }
         }
      }

   }
Ejemplo n.º 21
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;
}
Ejemplo n.º 22
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;
}
Ejemplo n.º 23
0
//--------------------------------------------------------------------------
// Read 1 bit of communication from the 1-Wire net and verify that the
// response matches the 'applyPowerResponse' bit and apply power delivery
// to the 1-Wire net.  Note that some implementations may apply the power
// first and then turn it off if the response is incorrect.
//
// 'portnum'  - number 0 to MAX_PORTNUM-1.  This number was provided to
//              OpenCOM to indicate the port number.
// 'applyPowerResponse' - 1 bit response to check, if correct then start
//                        power delivery 
//
// Returns:  TRUE: bit written and response correct, strong pullup now on
//           FALSE: response incorrect
//
SMALLINT owReadBitPower(int portnum, SMALLINT applyPowerResponse)
{
   SETUP_PACKET setup;
   ULONG nOutput = 0;
   WORD nBytes;   
   BYTE buf[2];

   // make sure strong pullup is not on
   if (USBLevel[portnum] == MODE_STRONG5)
      owLevel(portnum, MODE_NORMAL);

   // enable the strong pullup pulse
   setup.RequestTypeReservedBits = 0x40;
   setup.Request = MODE_CMD;
   setup.Value = MOD_PULSE_EN;
   setup.Index = ENABLEPULSE_SPUE;
   setup.Length = 0x00;
   setup.DataOut = FALSE;
   // call the driver
   if (!DeviceIoControl(usbhnd[portnum],
					    DS2490_IOCTL_VENDOR,
					    &setup,
					    sizeof(SETUP_PACKET),
					    NULL,
					    0,
					    &nOutput,
					    NULL))
   {
      // failure
      OWERROR(OWERROR_ADAPTER_ERROR);
      AdapterRecover(portnum);
      return FALSE;
   }

   // set to do touchbit
   setup.RequestTypeReservedBits = 0x40;
   setup.Request = COMM_CMD;
   setup.Value = COMM_BIT_IO | COMM_IM | COMM_SPU | COMM_D;
   setup.Index = 0;  
   setup.Length = 0;
   setup.DataOut = FALSE;

   // call the driver
   if (!DeviceIoControl(usbhnd[portnum],
					    DS2490_IOCTL_VENDOR,
					    &setup,
					    sizeof(SETUP_PACKET),
					    NULL,
					    0,
					    &nOutput,
					    NULL))
   {
      // failure
      OWERROR(OWERROR_ADAPTER_ERROR);
      AdapterRecover(portnum);
      return FALSE;
   }
   else
   {
      // now strong pullup is enabled
      USBLevel[portnum] = MODE_STRONG5;
      // success, read the result
      nBytes = 1;
      if (DS2490Read(usbhnd[portnum], buf, &nBytes))
      {
         // check response 
         if (buf[0] != applyPowerResponse)
         {
            owLevel(portnum, MODE_NORMAL);
            return FALSE;
         }
         else
            return TRUE;
      }
      else
      {
         OWERROR(OWERROR_ADAPTER_ERROR);
         AdapterRecover(portnum);
         return FALSE;
      }
   }
}
Ejemplo n.º 24
0
void scu_init()
{
  	uchar FamilySN[MAXDEVICES][8];
	int current_temp;
	int c_frac;
  	int i = 0;
  	int j = 0;
	int cnt = 0;
  	int NumDevices = 0;
  	SMALLINT didRead = 0;
	uchar read_buffer[32];
	uchar write_buffer[32];

	owInit();
	uart_init();
	uart_write_string("SCU\n");
 
  	//use port number for 1-wire
  	uchar portnum = ONEWIRE_PORT;
     j = 0;
     // Find the device(s)
     NumDevices  = 0;
     NumDevices += FindDevices(portnum, &FamilySN[NumDevices], 0x42, MAXDEVICES-NumDevices);
     NumDevices += FindDevices(portnum, &FamilySN[NumDevices], 0x20, MAXDEVICES-NumDevices);
     NumDevices += FindDevices(portnum, &FamilySN[NumDevices], 0x43, MAXDEVICES-NumDevices);
     if (NumDevices)
     {
        mprintf("\r\n");

        // read the temperature and print serial number and temperature
        for (i = NumDevices; i; i--)
        {
           mprintf("(%d) ", j++);
           DisplaySerialNum(FamilySN[i-1]);
           if (FamilySN[i-1][0] == 0x43) {
//		if(!Write43(portnum, FamilySN[i-1], write_buffer))
//			mprintf("write failed!\n");
	  	owLevel(portnum, MODE_NORMAL); 
		if (ReadMem43(portnum, FamilySN[i-1], read_buffer))
		{
			for(cnt = 0; cnt < 32; cnt++)
			{
				mprintf("read_buffer[%x]: %x\n",cnt, read_buffer[cnt]);
			}
		}
		continue;
	   }

	   if (FamilySN[i-1][0] == 0x42) {
             didRead = ReadTemperature42(portnum, FamilySN[i-1],&current_temp,&c_frac);
	   }
           if (didRead)
           {
             	mprintf(" %d",current_temp);
		if (c_frac)
			mprintf(".5");
		else
			mprintf(".0");
		mprintf(" deegree celsius\r\n");
	   }
           else
           {
             mprintf("  Convert failed.  Device is");
             if(!owVerify(portnum, FALSE))
                mprintf(" not");
             mprintf(" present.\r\n");
#ifdef SOCKIT_OWM_ERR_ENABLE
             while(owHasErrors())
                 mprintf("  - Error %d\r\n", owGetErrorNum());
#endif
           }
 
        }
     }
     else
        mprintf("No temperature devices found!\r\n");
	
}
Ejemplo n.º 25
0
//----------------------------------------------------------------------
//  Family 0x10 Demo
//
void main(void)
{
  int   temp;
  int   sign;
  BYTE  i;

  //----------------------------------------
  ADCON1      = 0x07;     // PortB digital

  bTRD6       = 1;        // RD6 = Serial Input
  bTRD7       = 0;        // RD7 = Serial Output
  bLD7        = 1;

  printf("\nFamily 0x10 Demo\n%");

////////////////////////////////////////////////////////////
// Initialise the OneWire network (bus)
  // attempt to acquire the 1-Wire Nets
  if (!owAcquire(0, NULL)) {
    printf("Acquire failed.\n%");
    while(1)
      ;
  }
  
////////////////////////////////////////////////////////////
// Let's set the alarmtemps of all devices on the bus.
// To demonstrate this, we write a low alarmtemp of 19
// and a high alarmtemp of 21 to all devices.
// Finally we copy the alarmtemps to non-volatile EEPROM.
  printf("\nStart - Set AlarmTemperatures.\n%");
  // Reset the devices
  owTouchReset(0);
  // Address all devices on the net
  owWriteByte(0, OW_SKIPROM);
  // Use the WriteScratchpad command to set AlarmTemps
  owWriteByte(0, OW_WRITESCRATCHPAD);
  owWriteByte(0, 19);  // Valid RoomTemperature
  owWriteByte(0, 21);  //   19 <= Temp < 21
  printf("End   - Set AlarmTemperatures.\n%");
  // Set alarmtemps done
    
  // Copy the temps to EEPROM
  printf("\nStart - Write AlarmTemperatures to EEPROM.\n%");
  // Reset the devices
  owTouchReset(0);
  // Address all devices on the net
  owWriteByte(0, OW_SKIPROM);
  // send the CopyScratchpad command to copy the AlarmTemps to EEPROM
  // If the bus supports Strong PullUp, we use it (because
  // there might be parasite powered devices on this bus).
  if(owHasPowerDelivery(0)) {
    printf("The bus has a Strong PullUp\n%");
    owWriteBytePower(0, OW_COPYSCRATCHPAD);
    // Eeprom update takes 10 milliseconds max. 11ms is safer
    Wait(11);
    // turn off the strong pull-up
    owLevel(0, MODE_NORMAL);
  } else {
    printf("The bus has NO Strong PullUp\n%");
    owWriteByte(0, OW_COPYSCRATCHPAD);
    // Eeprom update takes 10 milliseconds max. 11ms is safer
    Wait(11);
  }
  printf("End   - Write AlarmTemperatures to EEPROM.\n%");
  // Copy the temps to EEPROM done
  
  while(1) {
////////////////////////////////////////////////////////////
    // We do a temp conversion on all DS18S20's
    printf("\nStart - TemperatureConversions\n%");
    // Reset the devices
    owTouchReset(0);
    // Address all devices on this net
    owWriteByte(0, OW_SKIPROM);
    // Send the convert command and start power delivery if available.
    // Note that we use a different way here to determine
    // Strong PullUp capability.
    if(owWriteBytePower(0, OW_CONVERTT)) {
      printf("We are using a Strong PullUp.\n%");
    } else {
      printf("No Strong PullUp configured.\n%");
      owWriteByte(0, OW_CONVERTT);
    }

    // Coversion takes 750 milliseconds max. 751ms is safer
    Wait(751);
    // turn off the 1-Wire Net strong pull-up
    owLevel(0, MODE_NORMAL);
    printf("End - TemperatureConversions\n%");

////////////////////////////////////////////////////////////
    // We will now demonstrate an alarmsearch
    printf("\nStart - Find devices in alarmstate\n%");
    // Find the devices with temp < 19 or temp >= 21
    if(!owFirst(0, TRUE, TRUE)) {
      printf("No devices in AlarmState found.\n%");
    } else {
      do {
        printf("Device in AlarmState found: 0x%02X%02X%02X%02X%02X%02X%02X%02X\n%",
               owNetCurrent.SerialNum[7],
               owNetCurrent.SerialNum[6],
               owNetCurrent.SerialNum[5],
               owNetCurrent.SerialNum[4],
               owNetCurrent.SerialNum[3],
               owNetCurrent.SerialNum[2],
               owNetCurrent.SerialNum[1],
               owNetCurrent.SerialNum[0]);
      } while(owNext(0, TRUE, TRUE));
    }
    printf("End   - Find devices in alarmstate\n%");

////////////////////////////////////////////////////////////
    // We will now demonstrate a normal search. For
    // every device found, it's temperature is retrieved.
    printf("\nStart - Find devices and get their temperatures.\n%");
    if(!owFirst(0, TRUE, FALSE)) {
      printf("No devices found.\n%");
    } else {
      do {
        printf("Device found: 0x%02X%02X%02X%02X%02X%02X%02X%02X%",
               owNetCurrent.SerialNum[7],
               owNetCurrent.SerialNum[6],
               owNetCurrent.SerialNum[5],
               owNetCurrent.SerialNum[4],
               owNetCurrent.SerialNum[3],
               owNetCurrent.SerialNum[2],
               owNetCurrent.SerialNum[1],
               owNetCurrent.SerialNum[0]);
        // Init the crc
        setcrc8(0, 0);
        // Read the device's memory
        owWriteByte(0, OW_READSCRATCHPAD);
        for(i = 0; i < SCRATCHPAD_SIZE; i++) {
          ScratchPad[i] = owReadByte(0);
          docrc8(0, ScratchPad[i]);
        }
        // Check the CRC
        if(owNetCurrent.utilcrc8 != 0) {
          printf(", crc is NOT OK.\n%");          
        } else {
          // We don't want to use float's
          temp = ((int)ScratchPad[SCRATCHPAD_TEMPERATUREMSB] << 8) |
                  (int)ScratchPad[SCRATCHPAD_TEMPERATURELSB];
          if(temp < 0) {
            temp = -temp;
            sign = '-';
          } else if(temp == 0) {
            sign = ' ';
          } else {
            sign = '+';
          }
          printf(", Temperature is %c%d.%d degrees\n%",
                 sign,
                 (temp >> 1),
                 (temp & 0x01) ? 5 : 0);
        }
      } while(owNext(0, TRUE, FALSE));
    }   
    printf("End   - Find devices and get their temperatures.\n%");
     
////////////////////////////////////////////////////////////
    Wait(1);  // Just a convenient way to set a breakpoint
  }
}