VOC Optimation
[owSlave2.git] / tools_cmd / rwOW / tmextran.c
index 31d2cf7..4a4b926 100644 (file)
-//---------------------------------------------------------------------------
-// Copyright (C) 1999 Dallas Semiconductor Corporation, All Rights Reserved.
-//
-// Permission is hereby granted, free of charge, to any person obtaining a
-// copy of this software and associated documentation files (the "Software"),
-// to deal in the Software without restriction, including without limitation
-// the rights to use, copy, modify, merge, publish, distribute, sublicense,
-// and/or sell copies of the Software, and to permit persons to whom the
-// Software is furnished to do so, subject to the following conditions:
-//
-// The above copyright notice and this permission notice shall be included
-// in all copies or substantial portions of the Software.
-//
-// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
-// OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
-// MERCHANTABILITY,  FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
-// IN NO EVENT SHALL DALLAS SEMICONDUCTOR BE LIABLE FOR ANY CLAIM, DAMAGES
-// OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
-// ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
-// OTHER DEALINGS IN THE SOFTWARE.
-//
-// Except as contained in this notice, the name of Dallas Semiconductor
-// shall not be used except as stated in the Dallas Semiconductor
-// Branding Policy.
-//---------------------------------------------------------------------------
-//
-//  tmexnet.C - Wrapper class to hook 1-Wire Public Domain API to TMEX API
-//              for transport functions.
-//
-//  Version: 3.00
-//
-
-#include "ownet.h"
-#include <windows.h>
-
-// external TMEX variables
-extern long SessionHandle[MAX_PORTNUM];
-extern short far pascal TMBlockIO(long, uchar *, short);
-extern short far pascal TMBlockStream(long, uchar *, short);
-
-//--------------------------------------------------------------------------
-// The 'owBlock' transfers a block of data to and from the
-// 1-Wire Net with an optional reset at the begining of communication.
-// The result is returned in the same buffer.
-//
-// 'do_reset' - cause a owTouchReset to occure at the begining of
-//              communication TRUE(1) or not FALSE(0)
-// 'tran_buf' - pointer to a block of unsigned
-//              chars of length 'TranferLength' that will be sent
-//              to the 1-Wire Net
-// 'tran_len' - length in bytes to transfer
-// Supported devices: all
-//
-// Returns:   TRUE (1) : The optional reset returned a valid
-//                       presence (do_reset == TRUE) or there
-//                       was no reset required.
-//            FALSE (0): The reset did not return a valid prsence
-//                       (do_reset == TRUE).
-//
-//  The maximum tran_len is 64
-//
-SMALLINT owBlock(int portnum, SMALLINT do_reset, uchar *tran_buf, SMALLINT tran_len)
-{
-   short rslt;
-
-   // check for a block too big
-   if (tran_len > 192)
-   {
-      OWERROR(OWERROR_BLOCK_TOO_BIG);
-      return FALSE;
-   }
-
-   // check if need to do a owTouchReset first
-   if (do_reset)
-      rslt = TMBlockIO(SessionHandle[portnum], tran_buf, (short)tran_len);
-   else
-      rslt = TMBlockStream(SessionHandle[portnum], tran_buf, (short)tran_len);
-
-   return (rslt == tran_len);
-}
-
-//--------------------------------------------------------------------------
-// Write a byte to an EPROM 1-Wire device.
-//
-// Supported devices: crc_type=0(CRC8)
-//                        DS1982
-//                    crc_type=1(CRC16)
-//                        DS1985, DS1986, DS2407
-//
-// 'portnum'    - number 0 to MAX_PORTNUM-1.  This number is provided to
-//                indicate the symbolic port number.
-// 'write_byte' - byte to program
-// 'addr'       - address of byte to program
-// 'write_cmd'  - command used to write (0x0F reg mem, 0x55 status)
-// 'crc_type'   - CRC used (0 CRC8, 1 CRC16)
-// 'do_access'  - Flag to access device for each byte
-//                (0 skip access, 1 do the access)
-//                WARNING, only use do_access=0 if programing the NEXT
-//                byte immediatly after the previous byte.
-//
-// Returns: >=0   success, this is the resulting byte from the program
-//                effort
-//          -1    error, device not connected or program pulse voltage
-//                not available
-//
-SMALLINT owProgramByte(int portnum, SMALLINT write_byte, int addr, SMALLINT write_cmd,
-                       SMALLINT crc_type, SMALLINT do_access)
-{
-   ushort lastcrc16;
-   uchar lastcrc8;
-
-   // optionally access the device
-   if (do_access)
-   {
-      if (!owAccess(portnum))
-      {
-         OWERROR(OWERROR_ACCESS_FAILED);
-         return -1;
-      }
-
-      // send the write command
-      if (!owWriteByte(portnum,write_cmd))
-      {
-         OWERROR(OWERROR_WRITE_BYTE_FAILED);
-         return -1;
-      }
-
-      // send the address
-      if (!owWriteByte(portnum,addr & 0xFF) || !owWriteByte(portnum,addr >> 8))
-      {
-         OWERROR(OWERROR_WRITE_BYTE_FAILED);
-         return -1;
-      }
-   }
-
-   // send the data to write
-   if (!owWriteByte(portnum,write_byte))
-   {
-      OWERROR(OWERROR_WRITE_BYTE_FAILED);
-      return -1;
-   }
-
-   // read the CRC
-   if (crc_type == 0)
-   {
-      // calculate CRC8
-      if (do_access)
-      {
-         setcrc8(portnum,0);
-         docrc8(portnum,(uchar)write_cmd);
-         docrc8(portnum,(uchar)(addr & 0xFF));
-         docrc8(portnum,(uchar)(addr >> 8));
-      }
-      else
-         setcrc8(portnum,(uchar)(addr & 0xFF));
-
-      docrc8(portnum,(uchar)write_byte);
-      // read and calculate the read crc
-      lastcrc8 = docrc8(portnum,(uchar)owReadByte(portnum));
-      // crc should now be 0x00
-      if (lastcrc8 != 0)
-      {
-         OWERROR(OWERROR_CRC_FAILED);
-         return -1;
-      }
-   }
-   else
-   {
-      // CRC16
-      if (do_access)
-      {
-         setcrc16(portnum,0);
-         docrc16(portnum,(ushort)write_cmd);
-         docrc16(portnum,(ushort)(addr & 0xFF));
-         docrc16(portnum,(ushort)(addr >> 8));
-      }
-      else
-         setcrc16(portnum,(ushort)addr);
-      docrc16(portnum,(ushort)write_byte);
-      // read and calculate the read crc
-      docrc16(portnum,(ushort)owReadByte(portnum));
-      lastcrc16 = docrc16(portnum,(ushort)owReadByte(portnum));
-      // crc should now be 0xB001
-      if (lastcrc16 != 0xB001)
-         return -1;
-   }
-
-   // send the program pulse
-   if (!owProgramPulse(portnum))
-   {
-      OWERROR(OWERROR_PROGRAM_PULSE_FAILED);
-      return -1;
-   }
-
-   // read back and return the resulting byte
-   return owReadByte(portnum);
-}
-
-
+//---------------------------------------------------------------------------\r
+// Copyright (C) 1999 Dallas Semiconductor Corporation, All Rights Reserved.\r
+//\r
+// Permission is hereby granted, free of charge, to any person obtaining a\r
+// copy of this software and associated documentation files (the "Software"),\r
+// to deal in the Software without restriction, including without limitation\r
+// the rights to use, copy, modify, merge, publish, distribute, sublicense,\r
+// and/or sell copies of the Software, and to permit persons to whom the\r
+// Software is furnished to do so, subject to the following conditions:\r
+//\r
+// The above copyright notice and this permission notice shall be included\r
+// in all copies or substantial portions of the Software.\r
+//\r
+// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS\r
+// OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF\r
+// MERCHANTABILITY,  FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.\r
+// IN NO EVENT SHALL DALLAS SEMICONDUCTOR BE LIABLE FOR ANY CLAIM, DAMAGES\r
+// OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,\r
+// ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR\r
+// OTHER DEALINGS IN THE SOFTWARE.\r
+//\r
+// Except as contained in this notice, the name of Dallas Semiconductor\r
+// shall not be used except as stated in the Dallas Semiconductor\r
+// Branding Policy.\r
+//---------------------------------------------------------------------------\r
+//\r
+//  tmexnet.C - Wrapper class to hook 1-Wire Public Domain API to TMEX API\r
+//              for transport functions.\r
+//\r
+//  Version: 3.00\r
+//\r
+\r
+#include "ownet.h"\r
+#include <windows.h>\r
+\r
+// external TMEX variables\r
+extern long SessionHandle[MAX_PORTNUM];\r
+extern short far pascal TMBlockIO(long, uchar *, short);\r
+extern short far pascal TMBlockStream(long, uchar *, short);\r
+\r
+//--------------------------------------------------------------------------\r
+// The 'owBlock' transfers a block of data to and from the\r
+// 1-Wire Net with an optional reset at the begining of communication.\r
+// The result is returned in the same buffer.\r
+//\r
+// 'do_reset' - cause a owTouchReset to occure at the begining of\r
+//              communication TRUE(1) or not FALSE(0)\r
+// 'tran_buf' - pointer to a block of unsigned\r
+//              chars of length 'TranferLength' that will be sent\r
+//              to the 1-Wire Net\r
+// 'tran_len' - length in bytes to transfer\r
+// Supported devices: all\r
+//\r
+// Returns:   TRUE (1) : The optional reset returned a valid\r
+//                       presence (do_reset == TRUE) or there\r
+//                       was no reset required.\r
+//            FALSE (0): The reset did not return a valid prsence\r
+//                       (do_reset == TRUE).\r
+//\r
+//  The maximum tran_len is 64\r
+//\r
+SMALLINT owBlock(int portnum, SMALLINT do_reset, uchar *tran_buf, SMALLINT tran_len)\r
+{\r
+   short rslt;\r
+\r
+   // check for a block too big\r
+   if (tran_len > 192)\r
+   {\r
+      OWERROR(OWERROR_BLOCK_TOO_BIG);\r
+      return FALSE;\r
+   }\r
+\r
+   // check if need to do a owTouchReset first\r
+   if (do_reset)\r
+      rslt = TMBlockIO(SessionHandle[portnum], tran_buf, (short)tran_len);\r
+   else\r
+      rslt = TMBlockStream(SessionHandle[portnum], tran_buf, (short)tran_len);\r
+\r
+   return (rslt == tran_len);\r
+}\r
+\r
+//--------------------------------------------------------------------------\r
+// Write a byte to an EPROM 1-Wire device.\r
+//\r
+// Supported devices: crc_type=0(CRC8)\r
+//                        DS1982\r
+//                    crc_type=1(CRC16)\r
+//                        DS1985, DS1986, DS2407\r
+//\r
+// 'portnum'    - number 0 to MAX_PORTNUM-1.  This number is provided to\r
+//                indicate the symbolic port number.\r
+// 'write_byte' - byte to program\r
+// 'addr'       - address of byte to program\r
+// 'write_cmd'  - command used to write (0x0F reg mem, 0x55 status)\r
+// 'crc_type'   - CRC used (0 CRC8, 1 CRC16)\r
+// 'do_access'  - Flag to access device for each byte\r
+//                (0 skip access, 1 do the access)\r
+//                WARNING, only use do_access=0 if programing the NEXT\r
+//                byte immediatly after the previous byte.\r
+//\r
+// Returns: >=0   success, this is the resulting byte from the program\r
+//                effort\r
+//          -1    error, device not connected or program pulse voltage\r
+//                not available\r
+//\r
+SMALLINT owProgramByte(int portnum, SMALLINT write_byte, int addr, SMALLINT write_cmd,\r
+                       SMALLINT crc_type, SMALLINT do_access)\r
+{\r
+   ushort lastcrc16;\r
+   uchar lastcrc8;\r
+\r
+   // optionally access the device\r
+   if (do_access)\r
+   {\r
+      if (!owAccess(portnum))\r
+      {\r
+         OWERROR(OWERROR_ACCESS_FAILED);\r
+         return -1;\r
+      }\r
+\r
+      // send the write command\r
+      if (!owWriteByte(portnum,write_cmd))\r
+      {\r
+         OWERROR(OWERROR_WRITE_BYTE_FAILED);\r
+         return -1;\r
+      }\r
+\r
+      // send the address\r
+      if (!owWriteByte(portnum,addr & 0xFF) || !owWriteByte(portnum,addr >> 8))\r
+      {\r
+         OWERROR(OWERROR_WRITE_BYTE_FAILED);\r
+         return -1;\r
+      }\r
+   }\r
+\r
+   // send the data to write\r
+   if (!owWriteByte(portnum,write_byte))\r
+   {\r
+      OWERROR(OWERROR_WRITE_BYTE_FAILED);\r
+      return -1;\r
+   }\r
+\r
+   // read the CRC\r
+   if (crc_type == 0)\r
+   {\r
+      // calculate CRC8\r
+      if (do_access)\r
+      {\r
+         setcrc8(portnum,0);\r
+         docrc8(portnum,(uchar)write_cmd);\r
+         docrc8(portnum,(uchar)(addr & 0xFF));\r
+         docrc8(portnum,(uchar)(addr >> 8));\r
+      }\r
+      else\r
+         setcrc8(portnum,(uchar)(addr & 0xFF));\r
+\r
+      docrc8(portnum,(uchar)write_byte);\r
+      // read and calculate the read crc\r
+      lastcrc8 = docrc8(portnum,(uchar)owReadByte(portnum));\r
+      // crc should now be 0x00\r
+      if (lastcrc8 != 0)\r
+      {\r
+         OWERROR(OWERROR_CRC_FAILED);\r
+         return -1;\r
+      }\r
+   }\r
+   else\r
+   {\r
+      // CRC16\r
+      if (do_access)\r
+      {\r
+         setcrc16(portnum,0);\r
+         docrc16(portnum,(ushort)write_cmd);\r
+         docrc16(portnum,(ushort)(addr & 0xFF));\r
+         docrc16(portnum,(ushort)(addr >> 8));\r
+      }\r
+      else\r
+         setcrc16(portnum,(ushort)addr);\r
+      docrc16(portnum,(ushort)write_byte);\r
+      // read and calculate the read crc\r
+      docrc16(portnum,(ushort)owReadByte(portnum));\r
+      lastcrc16 = docrc16(portnum,(ushort)owReadByte(portnum));\r
+      // crc should now be 0xB001\r
+      if (lastcrc16 != 0xB001)\r
+         return -1;\r
+   }\r
+\r
+   // send the program pulse\r
+   if (!owProgramPulse(portnum))\r
+   {\r
+      OWERROR(OWERROR_PROGRAM_PULSE_FAILED);\r
+      return -1;\r
+   }\r
+\r
+   // read back and return the resulting byte\r
+   return owReadByte(portnum);\r
+}\r
+\r
+\r