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

📄 usbwlnk.c

📁 DALLAS 1 Wire 总线 SDK 支持多种高级语言
💻 C
📖 第 1 页 / 共 2 页
字号:
					       &nOutput,
					       NULL))
      {
         // failure
         OWERROR(OWERROR_ADAPTER_ERROR);
         AdapterRecover(portnum);
         return USBLevel[portnum];
      }
      else
      {
         // success, read the result
         USBLevel[portnum] = new_level;
         return new_level;
      }
   }
   // unsupported
   else if (new_level != USBLevel[portnum])
   {
      OWERROR(OWERROR_FUNC_NOT_SUP);
      return USBLevel[portnum];
   }

   // success, return the current level 
   return USBLevel[portnum];
}

//--------------------------------------------------------------------------
// 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;
}

//--------------------------------------------------------------------------
//  Description:
//     Delay for at least 'len' ms
//
void msDelay(int len)
{
   Sleep(len);
}

//--------------------------------------------------------------------------
// Get the current millisecond tick count.  Does not have to represent
// an actual time, it just needs to be an incrementing timer.
//
long msGettick(void)
{
   return GetTickCount();
}

//--------------------------------------------------------------------------
// Send and recieve 8 bits of communication to the 1-Wire Net.   
// The parameter 'sendbyte' least significant 8 bits are used.  After the
// 8 bits are sent change the level of the 1-Wire net to strong pullup 
// power delivery.
//
// '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:  Byte read: byte read (or echo if write)
//           0: failure 
//
SMALLINT owTouchBytePower(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);

   // 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 touchbyte with the SPU immediatly after
   setup.RequestTypeReservedBits = 0x40;
   setup.Request = COMM_CMD;
   setup.Value = COMM_BYTE_IO | COMM_IM | COMM_SPU;
   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 FALSE;
   }
   else
   {
      // now strong pullup is enabled
      USBLevel[portnum] = MODE_STRONG5;
      // success, read the result
      nBytes = 1;
      if (DS2490Read(usbhnd[portnum], buf, &nBytes))
         return buf[0];
      else
      {
         OWERROR(OWERROR_ADAPTER_ERROR);
         AdapterRecover(portnum);
         return FALSE;
      }
   }
}

//--------------------------------------------------------------------------
// 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)
{
   return (owTouchBytePower(portnum,sendbyte) == sendbyte);
}

//--------------------------------------------------------------------------
// Read 8 bits of communication from the 1-Wire net and provide strong
// pullup power.  
//
// 'portnum'  - number 0 to MAX_PORTNUM-1.  This number was provided to
//              OpenCOM to indicate the port number.
//
// Returns:  byte read
//
SMALLINT owReadBytePower(int portnum)
{
   return owTouchBytePower(portnum,0xFF);
}

//--------------------------------------------------------------------------
// 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;
      }
   }
}

//--------------------------------------------------------------------------
// This procedure indicates wether the adapter can deliver power.
//
// 'portnum'  - number 0 to MAX_PORTNUM-1.  This number was provided to
//              OpenCOM to indicate the port number.
//
// Returns:  TRUE  because all userial adapters have over drive. 
//
SMALLINT owHasPowerDelivery(int portnum)
{
   return TRUE;
}

//--------------------------------------------------------------------------
// This procedure indicates wether the adapter can deliver power.
//
// 'portnum'  - number 0 to MAX_PORTNUM-1.  This number was provided to
//              OpenCOM to indicate the port number.
//
// Returns:  TRUE  because all userial adapters have over drive. 
//
SMALLINT owHasOverDrive(int portnum)
{
   return TRUE;
}

//--------------------------------------------------------------------------
// 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  program volatage available
//           FALSE program voltage not available  
//
SMALLINT owHasProgramPulse(int portnum)
{
   owTouchReset(portnum);
   return USBVpp[portnum];
}

//--------------------------------------------------------------------------
// Attempt to recover communication with the DS2490
//
// 'portnum'  - number 0 to MAX_PORTNUM-1.  This number was provided to
//              indicate the port number.
//
// Returns:  TRUE  DS2490 recover successfull
//           FALSE failed to recover
//
SMALLINT AdapterRecover(int portnum)
{
   // dectect DS2490 and set it up
   if (DS2490Detect(usbhnd[portnum]))
   {
      USBSpeed[portnum] = MODE_NORMAL; // current DS2490 1-Wire Net communication speed
      USBLevel[portnum] = MODE_NORMAL; // current DS2490 1-Wire Net level
      return TRUE;
   }
   else
   {
      OWERROR(OWERROR_SYSTEM_RESOURCE_INIT_FAILED);
      return FALSE;
   }
}

⌨️ 快捷键说明

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