Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
owllu.cpp@0:70c3bea805ee, 2010-11-30 (annotated)
- Committer:
- svlach
- Date:
- Tue Nov 30 22:11:18 2010 +0000
- Revision:
- 0:70c3bea805ee
- Child:
- 1:dcf2f8359398
Who changed what in which revision?
| User | Revision | Line number | New contents of line |
|---|---|---|---|
| svlach | 0:70c3bea805ee | 1 | //--------------------------------------------------------------------------- |
| svlach | 0:70c3bea805ee | 2 | // Copyright (C) 2000 Dallas Semiconductor Corporation, All Rights Reserved. |
| svlach | 0:70c3bea805ee | 3 | // |
| svlach | 0:70c3bea805ee | 4 | // Permission is hereby granted, free of charge, to any person obtaining a |
| svlach | 0:70c3bea805ee | 5 | // copy of this software and associated documentation files (the "Software"), |
| svlach | 0:70c3bea805ee | 6 | // to deal in the Software without restriction, including without limitation |
| svlach | 0:70c3bea805ee | 7 | // the rights to use, copy, modify, merge, publish, distribute, sublicense, |
| svlach | 0:70c3bea805ee | 8 | // and/or sell copies of the Software, and to permit persons to whom the |
| svlach | 0:70c3bea805ee | 9 | // Software is furnished to do so, subject to the following conditions: |
| svlach | 0:70c3bea805ee | 10 | // |
| svlach | 0:70c3bea805ee | 11 | // The above copyright notice and this permission notice shall be included |
| svlach | 0:70c3bea805ee | 12 | // in all copies or substantial portions of the Software. |
| svlach | 0:70c3bea805ee | 13 | // |
| svlach | 0:70c3bea805ee | 14 | // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS |
| svlach | 0:70c3bea805ee | 15 | // OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF |
| svlach | 0:70c3bea805ee | 16 | // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. |
| svlach | 0:70c3bea805ee | 17 | // IN NO EVENT SHALL DALLAS SEMICONDUCTOR BE LIABLE FOR ANY CLAIM, DAMAGES |
| svlach | 0:70c3bea805ee | 18 | // OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, |
| svlach | 0:70c3bea805ee | 19 | // ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR |
| svlach | 0:70c3bea805ee | 20 | // OTHER DEALINGS IN THE SOFTWARE. |
| svlach | 0:70c3bea805ee | 21 | // |
| svlach | 0:70c3bea805ee | 22 | // Except as contained in this notice, the name of Dallas Semiconductor |
| svlach | 0:70c3bea805ee | 23 | // shall not be used except as stated in the Dallas Semiconductor |
| svlach | 0:70c3bea805ee | 24 | // Branding Policy. |
| svlach | 0:70c3bea805ee | 25 | //--------------------------------------------------------------------------- |
| svlach | 0:70c3bea805ee | 26 | // |
| svlach | 0:70c3bea805ee | 27 | // owLLU.C - Link Layer 1-Wire Net functions using the DS2480/DS2480B (U) |
| svlach | 0:70c3bea805ee | 28 | // serial interface chip. |
| svlach | 0:70c3bea805ee | 29 | // |
| svlach | 0:70c3bea805ee | 30 | // Version: 3.00 |
| svlach | 0:70c3bea805ee | 31 | // |
| svlach | 0:70c3bea805ee | 32 | // History: 1.00 -> 1.01 DS2480 version number now ignored in |
| svlach | 0:70c3bea805ee | 33 | // owTouchReset. |
| svlach | 0:70c3bea805ee | 34 | // 1.02 -> 1.03 Removed caps in #includes for Linux capatibility |
| svlach | 0:70c3bea805ee | 35 | // Removed #include <windows.h> |
| svlach | 0:70c3bea805ee | 36 | // Add #include "ownet.h" to define TRUE,FALSE |
| svlach | 0:70c3bea805ee | 37 | // 1.03 -> 2.00 Changed 'MLan' to 'ow'. Added support for |
| svlach | 0:70c3bea805ee | 38 | // multiple ports. |
| svlach | 0:70c3bea805ee | 39 | // 2.00 -> 2.01 Added error handling. Added circular-include check. |
| svlach | 0:70c3bea805ee | 40 | // 2.01 -> 2.10 Added raw memory error handling and SMALLINT |
| svlach | 0:70c3bea805ee | 41 | // 2.10 -> 3.00 Added memory bank functionality |
| svlach | 0:70c3bea805ee | 42 | // Added file I/O operations |
| svlach | 0:70c3bea805ee | 43 | // Added owReadBitPower and owWriteBytePower |
| svlach | 0:70c3bea805ee | 44 | // Added support for THE LINK |
| svlach | 0:70c3bea805ee | 45 | // Updated owLevel to match AN192 |
| svlach | 0:70c3bea805ee | 46 | // |
| svlach | 0:70c3bea805ee | 47 | |
| svlach | 0:70c3bea805ee | 48 | #include "./Headers/ownet.h" |
| svlach | 0:70c3bea805ee | 49 | #include "./Headers/ds2480.h" |
| svlach | 0:70c3bea805ee | 50 | |
| svlach | 0:70c3bea805ee | 51 | int dodebug=0; |
| svlach | 0:70c3bea805ee | 52 | |
| svlach | 0:70c3bea805ee | 53 | // external globals |
| svlach | 0:70c3bea805ee | 54 | extern SMALLINT ULevel[MAX_PORTNUM]; // current DS2480B 1-Wire Net level |
| svlach | 0:70c3bea805ee | 55 | extern SMALLINT UBaud[MAX_PORTNUM]; // current DS2480B baud rate |
| svlach | 0:70c3bea805ee | 56 | extern SMALLINT UMode[MAX_PORTNUM]; // current DS2480B command or data mode state |
| svlach | 0:70c3bea805ee | 57 | extern SMALLINT USpeed[MAX_PORTNUM]; // current DS2480B 1-Wire Net communication speed |
| svlach | 0:70c3bea805ee | 58 | extern SMALLINT UVersion[MAX_PORTNUM]; // current DS2480B version |
| svlach | 0:70c3bea805ee | 59 | |
| svlach | 0:70c3bea805ee | 60 | // new global for DS1994/DS2404/DS1427. If TRUE, puts a delay in owTouchReset to compensate for alarming clocks. |
| svlach | 0:70c3bea805ee | 61 | SMALLINT FAMILY_CODE_04_ALARM_TOUCHRESET_COMPLIANCE = FALSE; // default owTouchReset to quickest response. |
| svlach | 0:70c3bea805ee | 62 | |
| svlach | 0:70c3bea805ee | 63 | // local varable flag, true if program voltage available |
| svlach | 0:70c3bea805ee | 64 | static SMALLINT ProgramAvailable[MAX_PORTNUM]; |
| svlach | 0:70c3bea805ee | 65 | |
| svlach | 0:70c3bea805ee | 66 | //-------------------------------------------------------------------------- |
| svlach | 0:70c3bea805ee | 67 | // Reset all of the devices on the 1-Wire Net and return the result. |
| svlach | 0:70c3bea805ee | 68 | // |
| svlach | 0:70c3bea805ee | 69 | // 'portnum' - number 0 to MAX_PORTNUM-1. This number was provided to |
| svlach | 0:70c3bea805ee | 70 | // OpenCOM to indicate the port number. |
| svlach | 0:70c3bea805ee | 71 | // |
| svlach | 0:70c3bea805ee | 72 | // Returns: TRUE(1): presense pulse(s) detected, device(s) reset |
| svlach | 0:70c3bea805ee | 73 | // FALSE(0): no presense pulses detected |
| svlach | 0:70c3bea805ee | 74 | // |
| svlach | 0:70c3bea805ee | 75 | // WARNING: Without setting the above global (FAMILY_CODE_04_ALARM_TOUCHRESET_COMPLIANCE) |
| svlach | 0:70c3bea805ee | 76 | // to TRUE, this routine will not function correctly on some |
| svlach | 0:70c3bea805ee | 77 | // Alarm reset types of the DS1994/DS1427/DS2404 with |
| svlach | 0:70c3bea805ee | 78 | // Rev 1,2, and 3 of the DS2480/DS2480B. |
| svlach | 0:70c3bea805ee | 79 | // |
| svlach | 0:70c3bea805ee | 80 | // |
| svlach | 0:70c3bea805ee | 81 | SMALLINT owTouchReset(int portnum) |
| svlach | 0:70c3bea805ee | 82 | { |
| svlach | 0:70c3bea805ee | 83 | uchar readbuffer[10],sendpacket[10]; |
| svlach | 0:70c3bea805ee | 84 | uchar sendlen=0; |
| svlach | 0:70c3bea805ee | 85 | |
| svlach | 0:70c3bea805ee | 86 | if (dodebug) |
| svlach | 0:70c3bea805ee | 87 | printf("\nRST ");//?????????????? |
| svlach | 0:70c3bea805ee | 88 | |
| svlach | 0:70c3bea805ee | 89 | // make sure normal level |
| svlach | 0:70c3bea805ee | 90 | owLevel(portnum,MODE_NORMAL); |
| svlach | 0:70c3bea805ee | 91 | |
| svlach | 0:70c3bea805ee | 92 | // check if correct mode |
| svlach | 0:70c3bea805ee | 93 | if (UMode[portnum] != MODSEL_COMMAND) |
| svlach | 0:70c3bea805ee | 94 | { |
| svlach | 0:70c3bea805ee | 95 | UMode[portnum] = MODSEL_COMMAND; |
| svlach | 0:70c3bea805ee | 96 | sendpacket[sendlen++] = MODE_COMMAND; |
| svlach | 0:70c3bea805ee | 97 | } |
| svlach | 0:70c3bea805ee | 98 | |
| svlach | 0:70c3bea805ee | 99 | // construct the command |
| svlach | 0:70c3bea805ee | 100 | sendpacket[sendlen++] = (uchar)(CMD_COMM | FUNCTSEL_RESET | USpeed[portnum]); |
| svlach | 0:70c3bea805ee | 101 | |
| svlach | 0:70c3bea805ee | 102 | // flush the buffers |
| svlach | 0:70c3bea805ee | 103 | FlushCOM(portnum); |
| svlach | 0:70c3bea805ee | 104 | |
| svlach | 0:70c3bea805ee | 105 | // send the packet |
| svlach | 0:70c3bea805ee | 106 | if (WriteCOM(portnum,sendlen,sendpacket)) |
| svlach | 0:70c3bea805ee | 107 | { |
| svlach | 0:70c3bea805ee | 108 | // read back the 1 byte response |
| svlach | 0:70c3bea805ee | 109 | if (ReadCOM(portnum,1,readbuffer) == 1) |
| svlach | 0:70c3bea805ee | 110 | { |
| svlach | 0:70c3bea805ee | 111 | // make sure this byte looks like a reset byte |
| svlach | 0:70c3bea805ee | 112 | if (((readbuffer[0] & RB_RESET_MASK) == RB_PRESENCE) || |
| svlach | 0:70c3bea805ee | 113 | ((readbuffer[0] & RB_RESET_MASK) == RB_ALARMPRESENCE)) |
| svlach | 0:70c3bea805ee | 114 | { |
| svlach | 0:70c3bea805ee | 115 | // check if programming voltage available |
| svlach | 0:70c3bea805ee | 116 | ProgramAvailable[portnum] = ((readbuffer[0] & 0x20) == 0x20); |
| svlach | 0:70c3bea805ee | 117 | UVersion[portnum] = (readbuffer[0] & VERSION_MASK); |
| svlach | 0:70c3bea805ee | 118 | |
| svlach | 0:70c3bea805ee | 119 | // only check for alarm pulse if DS2404 present and not using THE LINK |
| svlach | 0:70c3bea805ee | 120 | if ((FAMILY_CODE_04_ALARM_TOUCHRESET_COMPLIANCE) && |
| svlach | 0:70c3bea805ee | 121 | (UVersion[portnum] != VER_LINK)) |
| svlach | 0:70c3bea805ee | 122 | { |
| svlach | 0:70c3bea805ee | 123 | msDelay(5); // delay 5 ms to give DS1994 enough time |
| svlach | 0:70c3bea805ee | 124 | FlushCOM(portnum); |
| svlach | 0:70c3bea805ee | 125 | } |
| svlach | 0:70c3bea805ee | 126 | return TRUE; |
| svlach | 0:70c3bea805ee | 127 | } |
| svlach | 0:70c3bea805ee | 128 | else |
| svlach | 0:70c3bea805ee | 129 | OWERROR(OWERROR_RESET_FAILED); |
| svlach | 0:70c3bea805ee | 130 | |
| svlach | 0:70c3bea805ee | 131 | } |
| svlach | 0:70c3bea805ee | 132 | else |
| svlach | 0:70c3bea805ee | 133 | OWERROR(OWERROR_READCOM_FAILED); |
| svlach | 0:70c3bea805ee | 134 | } |
| svlach | 0:70c3bea805ee | 135 | else |
| svlach | 0:70c3bea805ee | 136 | OWERROR(OWERROR_WRITECOM_FAILED); |
| svlach | 0:70c3bea805ee | 137 | |
| svlach | 0:70c3bea805ee | 138 | // an error occured so re-sync with DS2480 |
| svlach | 0:70c3bea805ee | 139 | DS2480Detect(portnum); |
| svlach | 0:70c3bea805ee | 140 | |
| svlach | 0:70c3bea805ee | 141 | return FALSE; |
| svlach | 0:70c3bea805ee | 142 | } |
| svlach | 0:70c3bea805ee | 143 | |
| svlach | 0:70c3bea805ee | 144 | //-------------------------------------------------------------------------- |
| svlach | 0:70c3bea805ee | 145 | // Send 1 bit of communication to the 1-Wire Net and return the |
| svlach | 0:70c3bea805ee | 146 | // result 1 bit read from the 1-Wire Net. The parameter 'sendbit' |
| svlach | 0:70c3bea805ee | 147 | // least significant bit is used and the least significant bit |
| svlach | 0:70c3bea805ee | 148 | // of the result is the return bit. |
| svlach | 0:70c3bea805ee | 149 | // |
| svlach | 0:70c3bea805ee | 150 | // 'portnum' - number 0 to MAX_PORTNUM-1. This number was provided to |
| svlach | 0:70c3bea805ee | 151 | // OpenCOM to indicate the port number. |
| svlach | 0:70c3bea805ee | 152 | // 'sendbit' - the least significant bit is the bit to send |
| svlach | 0:70c3bea805ee | 153 | // |
| svlach | 0:70c3bea805ee | 154 | // Returns: 0: 0 bit read from sendbit |
| svlach | 0:70c3bea805ee | 155 | // 1: 1 bit read from sendbit |
| svlach | 0:70c3bea805ee | 156 | // |
| svlach | 0:70c3bea805ee | 157 | SMALLINT owTouchBit(int portnum, SMALLINT sendbit) |
| svlach | 0:70c3bea805ee | 158 | { |
| svlach | 0:70c3bea805ee | 159 | uchar readbuffer[10],sendpacket[10]; |
| svlach | 0:70c3bea805ee | 160 | uchar sendlen=0; |
| svlach | 0:70c3bea805ee | 161 | |
| svlach | 0:70c3bea805ee | 162 | // make sure normal level |
| svlach | 0:70c3bea805ee | 163 | owLevel(portnum,MODE_NORMAL); |
| svlach | 0:70c3bea805ee | 164 | |
| svlach | 0:70c3bea805ee | 165 | // check if correct mode |
| svlach | 0:70c3bea805ee | 166 | if (UMode[portnum] != MODSEL_COMMAND) |
| svlach | 0:70c3bea805ee | 167 | { |
| svlach | 0:70c3bea805ee | 168 | UMode[portnum] = MODSEL_COMMAND; |
| svlach | 0:70c3bea805ee | 169 | sendpacket[sendlen++] = MODE_COMMAND; |
| svlach | 0:70c3bea805ee | 170 | } |
| svlach | 0:70c3bea805ee | 171 | |
| svlach | 0:70c3bea805ee | 172 | // construct the command |
| svlach | 0:70c3bea805ee | 173 | sendpacket[sendlen] = (sendbit != 0) ? BITPOL_ONE : BITPOL_ZERO; |
| svlach | 0:70c3bea805ee | 174 | sendpacket[sendlen++] |= CMD_COMM | FUNCTSEL_BIT | USpeed[portnum]; |
| svlach | 0:70c3bea805ee | 175 | |
| svlach | 0:70c3bea805ee | 176 | // flush the buffers |
| svlach | 0:70c3bea805ee | 177 | FlushCOM(portnum); |
| svlach | 0:70c3bea805ee | 178 | |
| svlach | 0:70c3bea805ee | 179 | // send the packet |
| svlach | 0:70c3bea805ee | 180 | if (WriteCOM(portnum,sendlen,sendpacket)) |
| svlach | 0:70c3bea805ee | 181 | { |
| svlach | 0:70c3bea805ee | 182 | // read back the response |
| svlach | 0:70c3bea805ee | 183 | if (ReadCOM(portnum,1,readbuffer) == 1) |
| svlach | 0:70c3bea805ee | 184 | { |
| svlach | 0:70c3bea805ee | 185 | // interpret the response |
| svlach | 0:70c3bea805ee | 186 | if (((readbuffer[0] & 0xE0) == 0x80) && |
| svlach | 0:70c3bea805ee | 187 | ((readbuffer[0] & RB_BIT_MASK) == RB_BIT_ONE)) |
| svlach | 0:70c3bea805ee | 188 | return 1; |
| svlach | 0:70c3bea805ee | 189 | else |
| svlach | 0:70c3bea805ee | 190 | return 0; |
| svlach | 0:70c3bea805ee | 191 | } |
| svlach | 0:70c3bea805ee | 192 | else |
| svlach | 0:70c3bea805ee | 193 | OWERROR(OWERROR_READCOM_FAILED); |
| svlach | 0:70c3bea805ee | 194 | } |
| svlach | 0:70c3bea805ee | 195 | else |
| svlach | 0:70c3bea805ee | 196 | OWERROR(OWERROR_WRITECOM_FAILED); |
| svlach | 0:70c3bea805ee | 197 | |
| svlach | 0:70c3bea805ee | 198 | // an error occured so re-sync with DS2480 |
| svlach | 0:70c3bea805ee | 199 | DS2480Detect(portnum); |
| svlach | 0:70c3bea805ee | 200 | |
| svlach | 0:70c3bea805ee | 201 | return 0; |
| svlach | 0:70c3bea805ee | 202 | } |
| svlach | 0:70c3bea805ee | 203 | |
| svlach | 0:70c3bea805ee | 204 | //-------------------------------------------------------------------------- |
| svlach | 0:70c3bea805ee | 205 | // Send 8 bits of communication to the 1-Wire Net and verify that the |
| svlach | 0:70c3bea805ee | 206 | // 8 bits read from the 1-Wire Net is the same (write operation). |
| svlach | 0:70c3bea805ee | 207 | // The parameter 'sendbyte' least significant 8 bits are used. |
| svlach | 0:70c3bea805ee | 208 | // |
| svlach | 0:70c3bea805ee | 209 | // 'portnum' - number 0 to MAX_PORTNUM-1. This number was provided to |
| svlach | 0:70c3bea805ee | 210 | // OpenCOM to indicate the port number. |
| svlach | 0:70c3bea805ee | 211 | // 'sendbyte' - 8 bits to send (least significant byte) |
| svlach | 0:70c3bea805ee | 212 | // |
| svlach | 0:70c3bea805ee | 213 | // Returns: TRUE: bytes written and echo was the same |
| svlach | 0:70c3bea805ee | 214 | // FALSE: echo was not the same |
| svlach | 0:70c3bea805ee | 215 | // |
| svlach | 0:70c3bea805ee | 216 | SMALLINT owWriteByte(int portnum, SMALLINT sendbyte) |
| svlach | 0:70c3bea805ee | 217 | { |
| svlach | 0:70c3bea805ee | 218 | return (owTouchByte(portnum,sendbyte) == (0xff & sendbyte)) ? TRUE : FALSE; |
| svlach | 0:70c3bea805ee | 219 | } |
| svlach | 0:70c3bea805ee | 220 | |
| svlach | 0:70c3bea805ee | 221 | |
| svlach | 0:70c3bea805ee | 222 | //-------------------------------------------------------------------------- |
| svlach | 0:70c3bea805ee | 223 | // Send 8 bits of read communication to the 1-Wire Net and and return the |
| svlach | 0:70c3bea805ee | 224 | // result 8 bits read from the 1-Wire Net. |
| svlach | 0:70c3bea805ee | 225 | // |
| svlach | 0:70c3bea805ee | 226 | // 'portnum' - number 0 to MAX_PORTNUM-1. This number was provided to |
| svlach | 0:70c3bea805ee | 227 | // OpenCOM to indicate the port number. |
| svlach | 0:70c3bea805ee | 228 | // |
| svlach | 0:70c3bea805ee | 229 | // Returns: 8 bits read from 1-Wire Net |
| svlach | 0:70c3bea805ee | 230 | // |
| svlach | 0:70c3bea805ee | 231 | SMALLINT owReadByte(int portnum) |
| svlach | 0:70c3bea805ee | 232 | { |
| svlach | 0:70c3bea805ee | 233 | return owTouchByte(portnum,(SMALLINT)0xFF); |
| svlach | 0:70c3bea805ee | 234 | } |
| svlach | 0:70c3bea805ee | 235 | |
| svlach | 0:70c3bea805ee | 236 | //-------------------------------------------------------------------------- |
| svlach | 0:70c3bea805ee | 237 | // Send 8 bits of communication to the 1-Wire Net and return the |
| svlach | 0:70c3bea805ee | 238 | // result 8 bits read from the 1-Wire Net. The parameter 'sendbyte' |
| svlach | 0:70c3bea805ee | 239 | // least significant 8 bits are used and the least significant 8 bits |
| svlach | 0:70c3bea805ee | 240 | // of the result is the return byte. |
| svlach | 0:70c3bea805ee | 241 | // |
| svlach | 0:70c3bea805ee | 242 | // 'portnum' - number 0 to MAX_PORTNUM-1. This number was provided to |
| svlach | 0:70c3bea805ee | 243 | // OpenCOM to indicate the port number. |
| svlach | 0:70c3bea805ee | 244 | // 'sendbyte' - 8 bits to send (least significant byte) |
| svlach | 0:70c3bea805ee | 245 | // |
| svlach | 0:70c3bea805ee | 246 | // Returns: 8 bits read from sendbyte |
| svlach | 0:70c3bea805ee | 247 | // |
| svlach | 0:70c3bea805ee | 248 | SMALLINT owTouchByte(int portnum, SMALLINT sendbyte) |
| svlach | 0:70c3bea805ee | 249 | { |
| svlach | 0:70c3bea805ee | 250 | uchar readbuffer[10],sendpacket[10]; |
| svlach | 0:70c3bea805ee | 251 | uchar sendlen=0; |
| svlach | 0:70c3bea805ee | 252 | |
| svlach | 0:70c3bea805ee | 253 | // make sure normal level |
| svlach | 0:70c3bea805ee | 254 | owLevel(portnum,MODE_NORMAL); |
| svlach | 0:70c3bea805ee | 255 | |
| svlach | 0:70c3bea805ee | 256 | // check if correct mode |
| svlach | 0:70c3bea805ee | 257 | if (UMode[portnum] != MODSEL_DATA) |
| svlach | 0:70c3bea805ee | 258 | { |
| svlach | 0:70c3bea805ee | 259 | UMode[portnum] = MODSEL_DATA; |
| svlach | 0:70c3bea805ee | 260 | sendpacket[sendlen++] = MODE_DATA; |
| svlach | 0:70c3bea805ee | 261 | } |
| svlach | 0:70c3bea805ee | 262 | |
| svlach | 0:70c3bea805ee | 263 | // add the byte to send |
| svlach | 0:70c3bea805ee | 264 | sendpacket[sendlen++] = (uchar)sendbyte; |
| svlach | 0:70c3bea805ee | 265 | |
| svlach | 0:70c3bea805ee | 266 | // check for duplication of data that looks like COMMAND mode |
| svlach | 0:70c3bea805ee | 267 | if (sendbyte ==(SMALLINT)MODE_COMMAND) |
| svlach | 0:70c3bea805ee | 268 | sendpacket[sendlen++] = (uchar)sendbyte; |
| svlach | 0:70c3bea805ee | 269 | |
| svlach | 0:70c3bea805ee | 270 | // flush the buffers |
| svlach | 0:70c3bea805ee | 271 | FlushCOM(portnum); |
| svlach | 0:70c3bea805ee | 272 | |
| svlach | 0:70c3bea805ee | 273 | // send the packet |
| svlach | 0:70c3bea805ee | 274 | if (WriteCOM(portnum,sendlen,sendpacket)) |
| svlach | 0:70c3bea805ee | 275 | { |
| svlach | 0:70c3bea805ee | 276 | // read back the 1 byte response |
| svlach | 0:70c3bea805ee | 277 | if (ReadCOM(portnum,1,readbuffer) == 1) |
| svlach | 0:70c3bea805ee | 278 | { |
| svlach | 0:70c3bea805ee | 279 | if (dodebug) |
| svlach | 0:70c3bea805ee | 280 | printf("%02X ",readbuffer[0]);//?????????????? |
| svlach | 0:70c3bea805ee | 281 | |
| svlach | 0:70c3bea805ee | 282 | // return the response |
| svlach | 0:70c3bea805ee | 283 | return (int)readbuffer[0]; |
| svlach | 0:70c3bea805ee | 284 | } |
| svlach | 0:70c3bea805ee | 285 | else |
| svlach | 0:70c3bea805ee | 286 | OWERROR(OWERROR_READCOM_FAILED); |
| svlach | 0:70c3bea805ee | 287 | } |
| svlach | 0:70c3bea805ee | 288 | else |
| svlach | 0:70c3bea805ee | 289 | OWERROR(OWERROR_WRITECOM_FAILED); |
| svlach | 0:70c3bea805ee | 290 | |
| svlach | 0:70c3bea805ee | 291 | // an error occured so re-sync with DS2480 |
| svlach | 0:70c3bea805ee | 292 | DS2480Detect(portnum); |
| svlach | 0:70c3bea805ee | 293 | |
| svlach | 0:70c3bea805ee | 294 | return 0; |
| svlach | 0:70c3bea805ee | 295 | } |
| svlach | 0:70c3bea805ee | 296 | |
| svlach | 0:70c3bea805ee | 297 | //-------------------------------------------------------------------------- |
| svlach | 0:70c3bea805ee | 298 | // Set the 1-Wire Net communucation speed. |
| svlach | 0:70c3bea805ee | 299 | // |
| svlach | 0:70c3bea805ee | 300 | // 'portnum' - number 0 to MAX_PORTNUM-1. This number was provided to |
| svlach | 0:70c3bea805ee | 301 | // OpenCOM to indicate the port number. |
| svlach | 0:70c3bea805ee | 302 | // 'new_speed' - new speed defined as |
| svlach | 0:70c3bea805ee | 303 | // MODE_NORMAL 0x00 |
| svlach | 0:70c3bea805ee | 304 | // MODE_OVERDRIVE 0x01 |
| svlach | 0:70c3bea805ee | 305 | // |
| svlach | 0:70c3bea805ee | 306 | // Returns: current 1-Wire Net speed |
| svlach | 0:70c3bea805ee | 307 | // |
| svlach | 0:70c3bea805ee | 308 | SMALLINT owSpeed(int portnum, SMALLINT new_speed) |
| svlach | 0:70c3bea805ee | 309 | { |
| svlach | 0:70c3bea805ee | 310 | uchar sendpacket[5]; |
| svlach | 0:70c3bea805ee | 311 | uchar sendlen=0; |
| svlach | 0:70c3bea805ee | 312 | uchar rt = FALSE; |
| svlach | 0:70c3bea805ee | 313 | |
| svlach | 0:70c3bea805ee | 314 | // check if change from current mode |
| svlach | 0:70c3bea805ee | 315 | if (((new_speed == MODE_OVERDRIVE) && |
| svlach | 0:70c3bea805ee | 316 | (USpeed[portnum] != SPEEDSEL_OD)) || |
| svlach | 0:70c3bea805ee | 317 | ((new_speed == MODE_NORMAL) && |
| svlach | 0:70c3bea805ee | 318 | (USpeed[portnum] != SPEEDSEL_FLEX))) |
| svlach | 0:70c3bea805ee | 319 | { |
| svlach | 0:70c3bea805ee | 320 | if (new_speed == MODE_OVERDRIVE) |
| svlach | 0:70c3bea805ee | 321 | { |
| svlach | 0:70c3bea805ee | 322 | // check for unsupported mode in THE LINK |
| svlach | 0:70c3bea805ee | 323 | if (UVersion[portnum] == VER_LINK) |
| svlach | 0:70c3bea805ee | 324 | { |
| svlach | 0:70c3bea805ee | 325 | OWERROR(OWERROR_FUNC_NOT_SUP); |
| svlach | 0:70c3bea805ee | 326 | // if overdrive then switch to higher baud |
| svlach | 0:70c3bea805ee | 327 | } |
| svlach | 0:70c3bea805ee | 328 | else |
| svlach | 0:70c3bea805ee | 329 | { |
| svlach | 0:70c3bea805ee | 330 | if (DS2480ChangeBaud(portnum,MAX_BAUD) == MAX_BAUD) |
| svlach | 0:70c3bea805ee | 331 | { |
| svlach | 0:70c3bea805ee | 332 | USpeed[portnum] = SPEEDSEL_OD; |
| svlach | 0:70c3bea805ee | 333 | rt = TRUE; |
| svlach | 0:70c3bea805ee | 334 | } |
| svlach | 0:70c3bea805ee | 335 | } |
| svlach | 0:70c3bea805ee | 336 | } |
| svlach | 0:70c3bea805ee | 337 | else if (new_speed == MODE_NORMAL) |
| svlach | 0:70c3bea805ee | 338 | { |
| svlach | 0:70c3bea805ee | 339 | // else normal so set to 9600 baud |
| svlach | 0:70c3bea805ee | 340 | if (DS2480ChangeBaud(portnum,PARMSET_9600) == PARMSET_9600) |
| svlach | 0:70c3bea805ee | 341 | { |
| svlach | 0:70c3bea805ee | 342 | USpeed[portnum] = SPEEDSEL_FLEX; |
| svlach | 0:70c3bea805ee | 343 | rt = TRUE; |
| svlach | 0:70c3bea805ee | 344 | } |
| svlach | 0:70c3bea805ee | 345 | |
| svlach | 0:70c3bea805ee | 346 | } |
| svlach | 0:70c3bea805ee | 347 | |
| svlach | 0:70c3bea805ee | 348 | // if baud rate is set correctly then change DS2480 speed |
| svlach | 0:70c3bea805ee | 349 | if (rt) |
| svlach | 0:70c3bea805ee | 350 | { |
| svlach | 0:70c3bea805ee | 351 | // check if correct mode |
| svlach | 0:70c3bea805ee | 352 | if (UMode[portnum] != MODSEL_COMMAND) |
| svlach | 0:70c3bea805ee | 353 | { |
| svlach | 0:70c3bea805ee | 354 | UMode[portnum] = MODSEL_COMMAND; |
| svlach | 0:70c3bea805ee | 355 | sendpacket[sendlen++] = MODE_COMMAND; |
| svlach | 0:70c3bea805ee | 356 | } |
| svlach | 0:70c3bea805ee | 357 | |
| svlach | 0:70c3bea805ee | 358 | // proceed to set the DS2480 communication speed |
| svlach | 0:70c3bea805ee | 359 | sendpacket[sendlen++] = CMD_COMM | FUNCTSEL_SEARCHOFF | USpeed[portnum]; |
| svlach | 0:70c3bea805ee | 360 | |
| svlach | 0:70c3bea805ee | 361 | // send the packet |
| svlach | 0:70c3bea805ee | 362 | if (!WriteCOM(portnum,sendlen,sendpacket)) |
| svlach | 0:70c3bea805ee | 363 | { |
| svlach | 0:70c3bea805ee | 364 | OWERROR(OWERROR_WRITECOM_FAILED); |
| svlach | 0:70c3bea805ee | 365 | rt = FALSE; |
| svlach | 0:70c3bea805ee | 366 | // lost communication with DS2480 then reset |
| svlach | 0:70c3bea805ee | 367 | DS2480Detect(portnum); |
| svlach | 0:70c3bea805ee | 368 | } |
| svlach | 0:70c3bea805ee | 369 | } |
| svlach | 0:70c3bea805ee | 370 | } |
| svlach | 0:70c3bea805ee | 371 | |
| svlach | 0:70c3bea805ee | 372 | // return the current speed |
| svlach | 0:70c3bea805ee | 373 | return (USpeed[portnum] == SPEEDSEL_OD) ? MODE_OVERDRIVE : MODE_NORMAL; |
| svlach | 0:70c3bea805ee | 374 | } |
| svlach | 0:70c3bea805ee | 375 | |
| svlach | 0:70c3bea805ee | 376 | //-------------------------------------------------------------------------- |
| svlach | 0:70c3bea805ee | 377 | // Set the 1-Wire Net line level. The values for new_level are |
| svlach | 0:70c3bea805ee | 378 | // as follows: |
| svlach | 0:70c3bea805ee | 379 | // |
| svlach | 0:70c3bea805ee | 380 | // 'portnum' - number 0 to MAX_PORTNUM-1. This number was provided to |
| svlach | 0:70c3bea805ee | 381 | // OpenCOM to indicate the port number. |
| svlach | 0:70c3bea805ee | 382 | // 'new_level' - new level defined as |
| svlach | 0:70c3bea805ee | 383 | // MODE_NORMAL 0x00 |
| svlach | 0:70c3bea805ee | 384 | // MODE_STRONG5 0x02 |
| svlach | 0:70c3bea805ee | 385 | // MODE_PROGRAM 0x04 |
| svlach | 0:70c3bea805ee | 386 | // MODE_BREAK 0x08 (not supported) |
| svlach | 0:70c3bea805ee | 387 | // |
| svlach | 0:70c3bea805ee | 388 | // Returns: current 1-Wire Net level |
| svlach | 0:70c3bea805ee | 389 | // |
| svlach | 0:70c3bea805ee | 390 | SMALLINT owLevel(int portnum, SMALLINT new_level) |
| svlach | 0:70c3bea805ee | 391 | { |
| svlach | 0:70c3bea805ee | 392 | uchar sendpacket[10],readbuffer[10]; |
| svlach | 0:70c3bea805ee | 393 | uchar sendlen=0; |
| svlach | 0:70c3bea805ee | 394 | uchar rt=FALSE; |
| svlach | 0:70c3bea805ee | 395 | |
| svlach | 0:70c3bea805ee | 396 | // check if need to change level |
| svlach | 0:70c3bea805ee | 397 | if (new_level != ULevel[portnum]) |
| svlach | 0:70c3bea805ee | 398 | { |
| svlach | 0:70c3bea805ee | 399 | // check if correct mode |
| svlach | 0:70c3bea805ee | 400 | if (UMode[portnum] != MODSEL_COMMAND) |
| svlach | 0:70c3bea805ee | 401 | { |
| svlach | 0:70c3bea805ee | 402 | UMode[portnum] = MODSEL_COMMAND; |
| svlach | 0:70c3bea805ee | 403 | sendpacket[sendlen++] = MODE_COMMAND; |
| svlach | 0:70c3bea805ee | 404 | } |
| svlach | 0:70c3bea805ee | 405 | |
| svlach | 0:70c3bea805ee | 406 | // check if just putting back to normal |
| svlach | 0:70c3bea805ee | 407 | if (new_level == MODE_NORMAL) |
| svlach | 0:70c3bea805ee | 408 | { |
| svlach | 0:70c3bea805ee | 409 | // stop pulse command |
| svlach | 0:70c3bea805ee | 410 | sendpacket[sendlen++] = MODE_STOP_PULSE; |
| svlach | 0:70c3bea805ee | 411 | |
| svlach | 0:70c3bea805ee | 412 | // add the command to begin the pulse WITHOUT prime |
| svlach | 0:70c3bea805ee | 413 | sendpacket[sendlen++] = CMD_COMM | FUNCTSEL_CHMOD | SPEEDSEL_PULSE | BITPOL_5V | PRIME5V_FALSE; |
| svlach | 0:70c3bea805ee | 414 | |
| svlach | 0:70c3bea805ee | 415 | // stop pulse command |
| svlach | 0:70c3bea805ee | 416 | sendpacket[sendlen++] = MODE_STOP_PULSE; |
| svlach | 0:70c3bea805ee | 417 | |
| svlach | 0:70c3bea805ee | 418 | // flush the buffers |
| svlach | 0:70c3bea805ee | 419 | FlushCOM(portnum); |
| svlach | 0:70c3bea805ee | 420 | |
| svlach | 0:70c3bea805ee | 421 | // send the packet |
| svlach | 0:70c3bea805ee | 422 | if (WriteCOM(portnum,sendlen,sendpacket)) |
| svlach | 0:70c3bea805ee | 423 | { |
| svlach | 0:70c3bea805ee | 424 | // read back the 2 byte response |
| svlach | 0:70c3bea805ee | 425 | if (ReadCOM(portnum,2,readbuffer) == 2) |
| svlach | 0:70c3bea805ee | 426 | { |
| svlach | 0:70c3bea805ee | 427 | // check response byte |
| svlach | 0:70c3bea805ee | 428 | if (((readbuffer[0] & 0xE0) == 0xE0) && |
| svlach | 0:70c3bea805ee | 429 | ((readbuffer[1] & 0xE0) == 0xE0)) |
| svlach | 0:70c3bea805ee | 430 | { |
| svlach | 0:70c3bea805ee | 431 | rt = TRUE; |
| svlach | 0:70c3bea805ee | 432 | ULevel[portnum] = MODE_NORMAL; |
| svlach | 0:70c3bea805ee | 433 | } |
| svlach | 0:70c3bea805ee | 434 | } |
| svlach | 0:70c3bea805ee | 435 | else |
| svlach | 0:70c3bea805ee | 436 | OWERROR(OWERROR_READCOM_FAILED); |
| svlach | 0:70c3bea805ee | 437 | } |
| svlach | 0:70c3bea805ee | 438 | else |
| svlach | 0:70c3bea805ee | 439 | OWERROR(OWERROR_WRITECOM_FAILED); |
| svlach | 0:70c3bea805ee | 440 | } |
| svlach | 0:70c3bea805ee | 441 | // set new level |
| svlach | 0:70c3bea805ee | 442 | else |
| svlach | 0:70c3bea805ee | 443 | { |
| svlach | 0:70c3bea805ee | 444 | // strong 5 volts |
| svlach | 0:70c3bea805ee | 445 | if (new_level == MODE_STRONG5) |
| svlach | 0:70c3bea805ee | 446 | { |
| svlach | 0:70c3bea805ee | 447 | // set the SPUD time value |
| svlach | 0:70c3bea805ee | 448 | sendpacket[sendlen++] = CMD_CONFIG | PARMSEL_5VPULSE | PARMSET_infinite; |
| svlach | 0:70c3bea805ee | 449 | // add the command to begin the pulse |
| svlach | 0:70c3bea805ee | 450 | sendpacket[sendlen++] = CMD_COMM | FUNCTSEL_CHMOD | SPEEDSEL_PULSE | BITPOL_5V; |
| svlach | 0:70c3bea805ee | 451 | } |
| svlach | 0:70c3bea805ee | 452 | // 12 volts |
| svlach | 0:70c3bea805ee | 453 | else if (new_level == MODE_PROGRAM) |
| svlach | 0:70c3bea805ee | 454 | { |
| svlach | 0:70c3bea805ee | 455 | // check if programming voltage available |
| svlach | 0:70c3bea805ee | 456 | if (!ProgramAvailable[portnum]) |
| svlach | 0:70c3bea805ee | 457 | return MODE_NORMAL; |
| svlach | 0:70c3bea805ee | 458 | |
| svlach | 0:70c3bea805ee | 459 | // set the PPD time value |
| svlach | 0:70c3bea805ee | 460 | sendpacket[sendlen++] = CMD_CONFIG | PARMSEL_12VPULSE | PARMSET_infinite; |
| svlach | 0:70c3bea805ee | 461 | // add the command to begin the pulse |
| svlach | 0:70c3bea805ee | 462 | sendpacket[sendlen++] = CMD_COMM | FUNCTSEL_CHMOD | SPEEDSEL_PULSE | BITPOL_12V; |
| svlach | 0:70c3bea805ee | 463 | } |
| svlach | 0:70c3bea805ee | 464 | |
| svlach | 0:70c3bea805ee | 465 | // flush the buffers |
| svlach | 0:70c3bea805ee | 466 | FlushCOM(portnum); |
| svlach | 0:70c3bea805ee | 467 | |
| svlach | 0:70c3bea805ee | 468 | // send the packet |
| svlach | 0:70c3bea805ee | 469 | if (WriteCOM(portnum,sendlen,sendpacket)) |
| svlach | 0:70c3bea805ee | 470 | { |
| svlach | 0:70c3bea805ee | 471 | // read back the 1 byte response from setting time limit |
| svlach | 0:70c3bea805ee | 472 | if (ReadCOM(portnum,1,readbuffer) == 1) |
| svlach | 0:70c3bea805ee | 473 | { |
| svlach | 0:70c3bea805ee | 474 | // check response byte |
| svlach | 0:70c3bea805ee | 475 | if ((readbuffer[0] & 0x81) == 0) |
| svlach | 0:70c3bea805ee | 476 | { |
| svlach | 0:70c3bea805ee | 477 | ULevel[portnum] = new_level; |
| svlach | 0:70c3bea805ee | 478 | rt = TRUE; |
| svlach | 0:70c3bea805ee | 479 | } |
| svlach | 0:70c3bea805ee | 480 | } |
| svlach | 0:70c3bea805ee | 481 | else |
| svlach | 0:70c3bea805ee | 482 | OWERROR(OWERROR_READCOM_FAILED); |
| svlach | 0:70c3bea805ee | 483 | } |
| svlach | 0:70c3bea805ee | 484 | else |
| svlach | 0:70c3bea805ee | 485 | OWERROR(OWERROR_WRITECOM_FAILED); |
| svlach | 0:70c3bea805ee | 486 | } |
| svlach | 0:70c3bea805ee | 487 | |
| svlach | 0:70c3bea805ee | 488 | // if lost communication with DS2480 then reset |
| svlach | 0:70c3bea805ee | 489 | if (rt != TRUE) |
| svlach | 0:70c3bea805ee | 490 | DS2480Detect(portnum); |
| svlach | 0:70c3bea805ee | 491 | } |
| svlach | 0:70c3bea805ee | 492 | |
| svlach | 0:70c3bea805ee | 493 | // return the current level |
| svlach | 0:70c3bea805ee | 494 | return ULevel[portnum]; |
| svlach | 0:70c3bea805ee | 495 | } |
| svlach | 0:70c3bea805ee | 496 | |
| svlach | 0:70c3bea805ee | 497 | //-------------------------------------------------------------------------- |
| svlach | 0:70c3bea805ee | 498 | // This procedure creates a fixed 480 microseconds 12 volt pulse |
| svlach | 0:70c3bea805ee | 499 | // on the 1-Wire Net for programming EPROM iButtons. |
| svlach | 0:70c3bea805ee | 500 | // |
| svlach | 0:70c3bea805ee | 501 | // 'portnum' - number 0 to MAX_PORTNUM-1. This number was provided to |
| svlach | 0:70c3bea805ee | 502 | // OpenCOM to indicate the port number. |
| svlach | 0:70c3bea805ee | 503 | // |
| svlach | 0:70c3bea805ee | 504 | // Returns: TRUE successful |
| svlach | 0:70c3bea805ee | 505 | // FALSE program voltage not available |
| svlach | 0:70c3bea805ee | 506 | // |
| svlach | 0:70c3bea805ee | 507 | SMALLINT owProgramPulse(int portnum) |
| svlach | 0:70c3bea805ee | 508 | { |
| svlach | 0:70c3bea805ee | 509 | uchar sendpacket[10],readbuffer[10]; |
| svlach | 0:70c3bea805ee | 510 | uchar sendlen=0; |
| svlach | 0:70c3bea805ee | 511 | |
| svlach | 0:70c3bea805ee | 512 | // check if programming voltage available |
| svlach | 0:70c3bea805ee | 513 | if (!ProgramAvailable[portnum]) |
| svlach | 0:70c3bea805ee | 514 | return FALSE; |
| svlach | 0:70c3bea805ee | 515 | |
| svlach | 0:70c3bea805ee | 516 | // make sure normal level |
| svlach | 0:70c3bea805ee | 517 | owLevel(portnum,MODE_NORMAL); |
| svlach | 0:70c3bea805ee | 518 | |
| svlach | 0:70c3bea805ee | 519 | // check if correct mode |
| svlach | 0:70c3bea805ee | 520 | if (UMode[portnum] != MODSEL_COMMAND) |
| svlach | 0:70c3bea805ee | 521 | { |
| svlach | 0:70c3bea805ee | 522 | UMode[portnum] = MODSEL_COMMAND; |
| svlach | 0:70c3bea805ee | 523 | sendpacket[sendlen++] = MODE_COMMAND; |
| svlach | 0:70c3bea805ee | 524 | } |
| svlach | 0:70c3bea805ee | 525 | |
| svlach | 0:70c3bea805ee | 526 | // set the SPUD time value |
| svlach | 0:70c3bea805ee | 527 | sendpacket[sendlen++] = CMD_CONFIG | PARMSEL_12VPULSE | PARMSET_512us; |
| svlach | 0:70c3bea805ee | 528 | |
| svlach | 0:70c3bea805ee | 529 | // pulse command |
| svlach | 0:70c3bea805ee | 530 | sendpacket[sendlen++] = CMD_COMM | FUNCTSEL_CHMOD | BITPOL_12V | SPEEDSEL_PULSE; |
| svlach | 0:70c3bea805ee | 531 | |
| svlach | 0:70c3bea805ee | 532 | // flush the buffers |
| svlach | 0:70c3bea805ee | 533 | FlushCOM(portnum); |
| svlach | 0:70c3bea805ee | 534 | |
| svlach | 0:70c3bea805ee | 535 | // send the packet |
| svlach | 0:70c3bea805ee | 536 | if (WriteCOM(portnum,sendlen,sendpacket)) |
| svlach | 0:70c3bea805ee | 537 | { |
| svlach | 0:70c3bea805ee | 538 | // read back the 2 byte response |
| svlach | 0:70c3bea805ee | 539 | if (ReadCOM(portnum,2,readbuffer) == 2) |
| svlach | 0:70c3bea805ee | 540 | { |
| svlach | 0:70c3bea805ee | 541 | // check response byte |
| svlach | 0:70c3bea805ee | 542 | if (((readbuffer[0] | CMD_CONFIG) == |
| svlach | 0:70c3bea805ee | 543 | (CMD_CONFIG | PARMSEL_12VPULSE | PARMSET_512us)) && |
| svlach | 0:70c3bea805ee | 544 | ((readbuffer[1] & 0xFC) == |
| svlach | 0:70c3bea805ee | 545 | (0xFC & (CMD_COMM | FUNCTSEL_CHMOD | BITPOL_12V | SPEEDSEL_PULSE)))) |
| svlach | 0:70c3bea805ee | 546 | return TRUE; |
| svlach | 0:70c3bea805ee | 547 | } |
| svlach | 0:70c3bea805ee | 548 | else |
| svlach | 0:70c3bea805ee | 549 | OWERROR(OWERROR_READCOM_FAILED); |
| svlach | 0:70c3bea805ee | 550 | } |
| svlach | 0:70c3bea805ee | 551 | else |
| svlach | 0:70c3bea805ee | 552 | OWERROR(OWERROR_WRITECOM_FAILED); |
| svlach | 0:70c3bea805ee | 553 | |
| svlach | 0:70c3bea805ee | 554 | // an error occured so re-sync with DS2480 |
| svlach | 0:70c3bea805ee | 555 | DS2480Detect(portnum); |
| svlach | 0:70c3bea805ee | 556 | |
| svlach | 0:70c3bea805ee | 557 | return FALSE; |
| svlach | 0:70c3bea805ee | 558 | } |
| svlach | 0:70c3bea805ee | 559 | |
| svlach | 0:70c3bea805ee | 560 | //-------------------------------------------------------------------------- |
| svlach | 0:70c3bea805ee | 561 | // Send 8 bits of communication to the 1-Wire Net and verify that the |
| svlach | 0:70c3bea805ee | 562 | // 8 bits read from the 1-Wire Net is the same (write operation). |
| svlach | 0:70c3bea805ee | 563 | // The parameter 'sendbyte' least significant 8 bits are used. After the |
| svlach | 0:70c3bea805ee | 564 | // 8 bits are sent change the level of the 1-Wire net. |
| svlach | 0:70c3bea805ee | 565 | // |
| svlach | 0:70c3bea805ee | 566 | // 'portnum' - number 0 to MAX_PORTNUM-1. This number was provided to |
| svlach | 0:70c3bea805ee | 567 | // OpenCOM to indicate the port number. |
| svlach | 0:70c3bea805ee | 568 | // 'sendbyte' - 8 bits to send (least significant bit) |
| svlach | 0:70c3bea805ee | 569 | // |
| svlach | 0:70c3bea805ee | 570 | // Returns: TRUE: bytes written and echo was the same, strong pullup now on |
| svlach | 0:70c3bea805ee | 571 | // FALSE: echo was not the same |
| svlach | 0:70c3bea805ee | 572 | // |
| svlach | 0:70c3bea805ee | 573 | SMALLINT owWriteBytePower(int portnum, SMALLINT sendbyte) |
| svlach | 0:70c3bea805ee | 574 | { |
| svlach | 0:70c3bea805ee | 575 | uchar sendpacket[10],readbuffer[10]; |
| svlach | 0:70c3bea805ee | 576 | uchar sendlen=0; |
| svlach | 0:70c3bea805ee | 577 | uchar rt=FALSE; |
| svlach | 0:70c3bea805ee | 578 | uchar i, temp_byte; |
| svlach | 0:70c3bea805ee | 579 | |
| svlach | 0:70c3bea805ee | 580 | if (dodebug) |
| svlach | 0:70c3bea805ee | 581 | printf("P%02X ",sendbyte);//?????????????? |
| svlach | 0:70c3bea805ee | 582 | |
| svlach | 0:70c3bea805ee | 583 | // check if correct mode |
| svlach | 0:70c3bea805ee | 584 | if (UMode[portnum] != MODSEL_COMMAND) |
| svlach | 0:70c3bea805ee | 585 | { |
| svlach | 0:70c3bea805ee | 586 | UMode[portnum] = MODSEL_COMMAND; |
| svlach | 0:70c3bea805ee | 587 | sendpacket[sendlen++] = MODE_COMMAND; |
| svlach | 0:70c3bea805ee | 588 | } |
| svlach | 0:70c3bea805ee | 589 | |
| svlach | 0:70c3bea805ee | 590 | // set the SPUD time value |
| svlach | 0:70c3bea805ee | 591 | sendpacket[sendlen++] = CMD_CONFIG | PARMSEL_5VPULSE | PARMSET_infinite; |
| svlach | 0:70c3bea805ee | 592 | |
| svlach | 0:70c3bea805ee | 593 | // construct the stream to include 8 bit commands with the last one |
| svlach | 0:70c3bea805ee | 594 | // enabling the strong-pullup |
| svlach | 0:70c3bea805ee | 595 | temp_byte = sendbyte; |
| svlach | 0:70c3bea805ee | 596 | for (i = 0; i < 8; i++) |
| svlach | 0:70c3bea805ee | 597 | { |
| svlach | 0:70c3bea805ee | 598 | sendpacket[sendlen++] = ((temp_byte & 0x01) ? BITPOL_ONE : BITPOL_ZERO) |
| svlach | 0:70c3bea805ee | 599 | | CMD_COMM | FUNCTSEL_BIT | USpeed[portnum] | |
| svlach | 0:70c3bea805ee | 600 | ((i == 7) ? PRIME5V_TRUE : PRIME5V_FALSE); |
| svlach | 0:70c3bea805ee | 601 | temp_byte >>= 1; |
| svlach | 0:70c3bea805ee | 602 | } |
| svlach | 0:70c3bea805ee | 603 | |
| svlach | 0:70c3bea805ee | 604 | // flush the buffers |
| svlach | 0:70c3bea805ee | 605 | FlushCOM(portnum); |
| svlach | 0:70c3bea805ee | 606 | |
| svlach | 0:70c3bea805ee | 607 | // send the packet |
| svlach | 0:70c3bea805ee | 608 | if (WriteCOM(portnum,sendlen,sendpacket)) |
| svlach | 0:70c3bea805ee | 609 | { |
| svlach | 0:70c3bea805ee | 610 | // read back the 9 byte response from setting time limit |
| svlach | 0:70c3bea805ee | 611 | if (ReadCOM(portnum,9,readbuffer) == 9) |
| svlach | 0:70c3bea805ee | 612 | { |
| svlach | 0:70c3bea805ee | 613 | // check response |
| svlach | 0:70c3bea805ee | 614 | if ((readbuffer[0] & 0x81) == 0) |
| svlach | 0:70c3bea805ee | 615 | { |
| svlach | 0:70c3bea805ee | 616 | // indicate the port is now at power delivery |
| svlach | 0:70c3bea805ee | 617 | ULevel[portnum] = MODE_STRONG5; |
| svlach | 0:70c3bea805ee | 618 | |
| svlach | 0:70c3bea805ee | 619 | // reconstruct the echo byte |
| svlach | 0:70c3bea805ee | 620 | temp_byte = 0; |
| svlach | 0:70c3bea805ee | 621 | for (i = 0; i < 8; i++) |
| svlach | 0:70c3bea805ee | 622 | { |
| svlach | 0:70c3bea805ee | 623 | temp_byte >>= 1; |
| svlach | 0:70c3bea805ee | 624 | temp_byte |= (readbuffer[i + 1] & 0x01) ? 0x80 : 0; |
| svlach | 0:70c3bea805ee | 625 | } |
| svlach | 0:70c3bea805ee | 626 | |
| svlach | 0:70c3bea805ee | 627 | if (temp_byte == sendbyte) |
| svlach | 0:70c3bea805ee | 628 | rt = TRUE; |
| svlach | 0:70c3bea805ee | 629 | } |
| svlach | 0:70c3bea805ee | 630 | } |
| svlach | 0:70c3bea805ee | 631 | else |
| svlach | 0:70c3bea805ee | 632 | OWERROR(OWERROR_READCOM_FAILED); |
| svlach | 0:70c3bea805ee | 633 | } |
| svlach | 0:70c3bea805ee | 634 | else |
| svlach | 0:70c3bea805ee | 635 | OWERROR(OWERROR_WRITECOM_FAILED); |
| svlach | 0:70c3bea805ee | 636 | |
| svlach | 0:70c3bea805ee | 637 | // if lost communication with DS2480 then reset |
| svlach | 0:70c3bea805ee | 638 | if (rt != TRUE) |
| svlach | 0:70c3bea805ee | 639 | DS2480Detect(portnum); |
| svlach | 0:70c3bea805ee | 640 | |
| svlach | 0:70c3bea805ee | 641 | return rt; |
| svlach | 0:70c3bea805ee | 642 | } |
| svlach | 0:70c3bea805ee | 643 | |
| svlach | 0:70c3bea805ee | 644 | //-------------------------------------------------------------------------- |
| svlach | 0:70c3bea805ee | 645 | // Send 8 bits of communication to the 1-Wire Net and verify that the |
| svlach | 0:70c3bea805ee | 646 | // 8 bits read from the 1-Wire Net is the same (write operation). |
| svlach | 0:70c3bea805ee | 647 | // The parameter 'sendbyte' least significant 8 bits are used. After the |
| svlach | 0:70c3bea805ee | 648 | // 8 bits are sent change the level of the 1-Wire net. |
| svlach | 0:70c3bea805ee | 649 | // |
| svlach | 0:70c3bea805ee | 650 | // 'portnum' - number 0 to MAX_PORTNUM-1. This number was provided to |
| svlach | 0:70c3bea805ee | 651 | // OpenCOM to indicate the port number. |
| svlach | 0:70c3bea805ee | 652 | // 'sendbyte' - 8 bits to send (least significant bit) |
| svlach | 0:70c3bea805ee | 653 | // |
| svlach | 0:70c3bea805ee | 654 | // Returns: TRUE: bytes written and echo was the same, strong pullup now on |
| svlach | 0:70c3bea805ee | 655 | // FALSE: echo was not the same |
| svlach | 0:70c3bea805ee | 656 | // |
| svlach | 0:70c3bea805ee | 657 | SMALLINT owReadBytePower(int portnum) |
| svlach | 0:70c3bea805ee | 658 | { |
| svlach | 0:70c3bea805ee | 659 | uchar sendpacket[10],readbuffer[10]; |
| svlach | 0:70c3bea805ee | 660 | uchar sendlen=0; |
| svlach | 0:70c3bea805ee | 661 | uchar rt=FALSE; |
| svlach | 0:70c3bea805ee | 662 | uchar i, temp_byte; |
| svlach | 0:70c3bea805ee | 663 | |
| svlach | 0:70c3bea805ee | 664 | // check if correct mode |
| svlach | 0:70c3bea805ee | 665 | if (UMode[portnum] != MODSEL_COMMAND) |
| svlach | 0:70c3bea805ee | 666 | { |
| svlach | 0:70c3bea805ee | 667 | UMode[portnum] = MODSEL_COMMAND; |
| svlach | 0:70c3bea805ee | 668 | sendpacket[sendlen++] = MODE_COMMAND; |
| svlach | 0:70c3bea805ee | 669 | } |
| svlach | 0:70c3bea805ee | 670 | |
| svlach | 0:70c3bea805ee | 671 | // set the SPUD time value |
| svlach | 0:70c3bea805ee | 672 | sendpacket[sendlen++] = CMD_CONFIG | PARMSEL_5VPULSE | PARMSET_infinite; |
| svlach | 0:70c3bea805ee | 673 | |
| svlach | 0:70c3bea805ee | 674 | // construct the stream to include 8 bit commands with the last one |
| svlach | 0:70c3bea805ee | 675 | // enabling the strong-pullup |
| svlach | 0:70c3bea805ee | 676 | temp_byte = 0xFF; |
| svlach | 0:70c3bea805ee | 677 | for (i = 0; i < 8; i++) |
| svlach | 0:70c3bea805ee | 678 | { |
| svlach | 0:70c3bea805ee | 679 | sendpacket[sendlen++] = ((temp_byte & 0x01) ? BITPOL_ONE : BITPOL_ZERO) |
| svlach | 0:70c3bea805ee | 680 | | CMD_COMM | FUNCTSEL_BIT | USpeed[portnum] | |
| svlach | 0:70c3bea805ee | 681 | ((i == 7) ? PRIME5V_TRUE : PRIME5V_FALSE); |
| svlach | 0:70c3bea805ee | 682 | temp_byte >>= 1; |
| svlach | 0:70c3bea805ee | 683 | } |
| svlach | 0:70c3bea805ee | 684 | |
| svlach | 0:70c3bea805ee | 685 | // flush the buffers |
| svlach | 0:70c3bea805ee | 686 | FlushCOM(portnum); |
| svlach | 0:70c3bea805ee | 687 | |
| svlach | 0:70c3bea805ee | 688 | // send the packet |
| svlach | 0:70c3bea805ee | 689 | if (WriteCOM(portnum,sendlen,sendpacket)) |
| svlach | 0:70c3bea805ee | 690 | { |
| svlach | 0:70c3bea805ee | 691 | // read back the 9 byte response from setting time limit |
| svlach | 0:70c3bea805ee | 692 | if (ReadCOM(portnum,9,readbuffer) == 9) |
| svlach | 0:70c3bea805ee | 693 | { |
| svlach | 0:70c3bea805ee | 694 | // check response |
| svlach | 0:70c3bea805ee | 695 | if ((readbuffer[0] & 0x81) == 0) |
| svlach | 0:70c3bea805ee | 696 | { |
| svlach | 0:70c3bea805ee | 697 | // indicate the port is now at power delivery |
| svlach | 0:70c3bea805ee | 698 | ULevel[portnum] = MODE_STRONG5; |
| svlach | 0:70c3bea805ee | 699 | |
| svlach | 0:70c3bea805ee | 700 | // reconstruct the return byte |
| svlach | 0:70c3bea805ee | 701 | temp_byte = 0; |
| svlach | 0:70c3bea805ee | 702 | for (i = 0; i < 8; i++) |
| svlach | 0:70c3bea805ee | 703 | { |
| svlach | 0:70c3bea805ee | 704 | temp_byte >>= 1; |
| svlach | 0:70c3bea805ee | 705 | temp_byte |= (readbuffer[i + 1] & 0x01) ? 0x80 : 0; |
| svlach | 0:70c3bea805ee | 706 | } |
| svlach | 0:70c3bea805ee | 707 | |
| svlach | 0:70c3bea805ee | 708 | rt = TRUE; |
| svlach | 0:70c3bea805ee | 709 | } |
| svlach | 0:70c3bea805ee | 710 | } |
| svlach | 0:70c3bea805ee | 711 | else |
| svlach | 0:70c3bea805ee | 712 | OWERROR(OWERROR_READCOM_FAILED); |
| svlach | 0:70c3bea805ee | 713 | } |
| svlach | 0:70c3bea805ee | 714 | else |
| svlach | 0:70c3bea805ee | 715 | OWERROR(OWERROR_WRITECOM_FAILED); |
| svlach | 0:70c3bea805ee | 716 | |
| svlach | 0:70c3bea805ee | 717 | // if lost communication with DS2480 then reset |
| svlach | 0:70c3bea805ee | 718 | if (rt != TRUE) |
| svlach | 0:70c3bea805ee | 719 | DS2480Detect(portnum); |
| svlach | 0:70c3bea805ee | 720 | |
| svlach | 0:70c3bea805ee | 721 | if (dodebug) |
| svlach | 0:70c3bea805ee | 722 | printf("PFF%02X ",temp_byte);//?????????????? |
| svlach | 0:70c3bea805ee | 723 | |
| svlach | 0:70c3bea805ee | 724 | return temp_byte; |
| svlach | 0:70c3bea805ee | 725 | } |
| svlach | 0:70c3bea805ee | 726 | |
| svlach | 0:70c3bea805ee | 727 | |
| svlach | 0:70c3bea805ee | 728 | //-------------------------------------------------------------------------- |
| svlach | 0:70c3bea805ee | 729 | // Send 1 bit of communication to the 1-Wire Net and verify that the |
| svlach | 0:70c3bea805ee | 730 | // response matches the 'applyPowerResponse' bit and apply power delivery |
| svlach | 0:70c3bea805ee | 731 | // to the 1-Wire net. Note that some implementations may apply the power |
| svlach | 0:70c3bea805ee | 732 | // first and then turn it off if the response is incorrect. |
| svlach | 0:70c3bea805ee | 733 | // |
| svlach | 0:70c3bea805ee | 734 | // 'portnum' - number 0 to MAX_PORTNUM-1. This number was provided to |
| svlach | 0:70c3bea805ee | 735 | // OpenCOM to indicate the port number. |
| svlach | 0:70c3bea805ee | 736 | // 'applyPowerResponse' - 1 bit response to check, if correct then start |
| svlach | 0:70c3bea805ee | 737 | // power delivery |
| svlach | 0:70c3bea805ee | 738 | // |
| svlach | 0:70c3bea805ee | 739 | // Returns: TRUE: bit written and response correct, strong pullup now on |
| svlach | 0:70c3bea805ee | 740 | // FALSE: response incorrect |
| svlach | 0:70c3bea805ee | 741 | // |
| svlach | 0:70c3bea805ee | 742 | SMALLINT owReadBitPower(int portnum, SMALLINT applyPowerResponse) |
| svlach | 0:70c3bea805ee | 743 | { |
| svlach | 0:70c3bea805ee | 744 | uchar sendpacket[3],readbuffer[3]; |
| svlach | 0:70c3bea805ee | 745 | uchar sendlen=0; |
| svlach | 0:70c3bea805ee | 746 | uchar rt=FALSE; |
| svlach | 0:70c3bea805ee | 747 | |
| svlach | 0:70c3bea805ee | 748 | // check if correct mode |
| svlach | 0:70c3bea805ee | 749 | if (UMode[portnum] != MODSEL_COMMAND) |
| svlach | 0:70c3bea805ee | 750 | { |
| svlach | 0:70c3bea805ee | 751 | UMode[portnum] = MODSEL_COMMAND; |
| svlach | 0:70c3bea805ee | 752 | sendpacket[sendlen++] = MODE_COMMAND; |
| svlach | 0:70c3bea805ee | 753 | } |
| svlach | 0:70c3bea805ee | 754 | |
| svlach | 0:70c3bea805ee | 755 | // set the SPUD time value |
| svlach | 0:70c3bea805ee | 756 | sendpacket[sendlen++] = CMD_CONFIG | PARMSEL_5VPULSE | PARMSET_infinite; |
| svlach | 0:70c3bea805ee | 757 | |
| svlach | 0:70c3bea805ee | 758 | // enabling the strong-pullup after bit |
| svlach | 0:70c3bea805ee | 759 | sendpacket[sendlen++] = BITPOL_ONE |
| svlach | 0:70c3bea805ee | 760 | | CMD_COMM | FUNCTSEL_BIT | USpeed[portnum] | |
| svlach | 0:70c3bea805ee | 761 | PRIME5V_TRUE; |
| svlach | 0:70c3bea805ee | 762 | // flush the buffers |
| svlach | 0:70c3bea805ee | 763 | FlushCOM(portnum); |
| svlach | 0:70c3bea805ee | 764 | |
| svlach | 0:70c3bea805ee | 765 | // send the packet |
| svlach | 0:70c3bea805ee | 766 | if (WriteCOM(portnum,sendlen,sendpacket)) |
| svlach | 0:70c3bea805ee | 767 | { |
| svlach | 0:70c3bea805ee | 768 | // read back the 2 byte response from setting time limit |
| svlach | 0:70c3bea805ee | 769 | if (ReadCOM(portnum,2,readbuffer) == 2) |
| svlach | 0:70c3bea805ee | 770 | { |
| svlach | 0:70c3bea805ee | 771 | // check response to duration set |
| svlach | 0:70c3bea805ee | 772 | if ((readbuffer[0] & 0x81) == 0) |
| svlach | 0:70c3bea805ee | 773 | { |
| svlach | 0:70c3bea805ee | 774 | // indicate the port is now at power delivery |
| svlach | 0:70c3bea805ee | 775 | ULevel[portnum] = MODE_STRONG5; |
| svlach | 0:70c3bea805ee | 776 | |
| svlach | 0:70c3bea805ee | 777 | // check the response bit |
| svlach | 0:70c3bea805ee | 778 | if ((readbuffer[1] & 0x01) == applyPowerResponse) |
| svlach | 0:70c3bea805ee | 779 | rt = TRUE; |
| svlach | 0:70c3bea805ee | 780 | else |
| svlach | 0:70c3bea805ee | 781 | owLevel(portnum,MODE_NORMAL); |
| svlach | 0:70c3bea805ee | 782 | |
| svlach | 0:70c3bea805ee | 783 | return rt; |
| svlach | 0:70c3bea805ee | 784 | } |
| svlach | 0:70c3bea805ee | 785 | } |
| svlach | 0:70c3bea805ee | 786 | else |
| svlach | 0:70c3bea805ee | 787 | OWERROR(OWERROR_READCOM_FAILED); |
| svlach | 0:70c3bea805ee | 788 | } |
| svlach | 0:70c3bea805ee | 789 | else |
| svlach | 0:70c3bea805ee | 790 | OWERROR(OWERROR_WRITECOM_FAILED); |
| svlach | 0:70c3bea805ee | 791 | |
| svlach | 0:70c3bea805ee | 792 | // if lost communication with DS2480 then reset |
| svlach | 0:70c3bea805ee | 793 | if (rt != TRUE) |
| svlach | 0:70c3bea805ee | 794 | DS2480Detect(portnum); |
| svlach | 0:70c3bea805ee | 795 | |
| svlach | 0:70c3bea805ee | 796 | return rt; |
| svlach | 0:70c3bea805ee | 797 | } |
| svlach | 0:70c3bea805ee | 798 | |
| svlach | 0:70c3bea805ee | 799 | |
| svlach | 0:70c3bea805ee | 800 | //-------------------------------------------------------------------------- |
| svlach | 0:70c3bea805ee | 801 | // This procedure indicates whether the adapter can deliver power. |
| svlach | 0:70c3bea805ee | 802 | // |
| svlach | 0:70c3bea805ee | 803 | // 'portnum' - number 0 to MAX_PORTNUM-1. This number was provided to |
| svlach | 0:70c3bea805ee | 804 | // OpenCOM to indicate the port number. |
| svlach | 0:70c3bea805ee | 805 | // |
| svlach | 0:70c3bea805ee | 806 | // Returns: TRUE because all userial adapters have over drive. |
| svlach | 0:70c3bea805ee | 807 | // |
| svlach | 0:70c3bea805ee | 808 | SMALLINT owHasPowerDelivery(int portnum) |
| svlach | 0:70c3bea805ee | 809 | { |
| svlach | 0:70c3bea805ee | 810 | return TRUE; |
| svlach | 0:70c3bea805ee | 811 | } |
| svlach | 0:70c3bea805ee | 812 | |
| svlach | 0:70c3bea805ee | 813 | //-------------------------------------------------------------------------- |
| svlach | 0:70c3bea805ee | 814 | // This procedure indicates wether the adapter can deliver power. |
| svlach | 0:70c3bea805ee | 815 | // |
| svlach | 0:70c3bea805ee | 816 | // 'portnum' - number 0 to MAX_PORTNUM-1. This number was provided to |
| svlach | 0:70c3bea805ee | 817 | // OpenCOM to indicate the port number. |
| svlach | 0:70c3bea805ee | 818 | // |
| svlach | 0:70c3bea805ee | 819 | // Returns: TRUE because all userial adapters have over drive. |
| svlach | 0:70c3bea805ee | 820 | // |
| svlach | 0:70c3bea805ee | 821 | SMALLINT owHasOverDrive(int portnum) |
| svlach | 0:70c3bea805ee | 822 | { |
| svlach | 0:70c3bea805ee | 823 | return TRUE; |
| svlach | 0:70c3bea805ee | 824 | } |
| svlach | 0:70c3bea805ee | 825 | //-------------------------------------------------------------------------- |
| svlach | 0:70c3bea805ee | 826 | // This procedure creates a fixed 480 microseconds 12 volt pulse |
| svlach | 0:70c3bea805ee | 827 | // on the 1-Wire Net for programming EPROM iButtons. |
| svlach | 0:70c3bea805ee | 828 | // |
| svlach | 0:70c3bea805ee | 829 | // 'portnum' - number 0 to MAX_PORTNUM-1. This number was provided to |
| svlach | 0:70c3bea805ee | 830 | // OpenCOM to indicate the port number. |
| svlach | 0:70c3bea805ee | 831 | // |
| svlach | 0:70c3bea805ee | 832 | // Returns: TRUE program volatage available |
| svlach | 0:70c3bea805ee | 833 | // FALSE program voltage not available |
| svlach | 0:70c3bea805ee | 834 | SMALLINT owHasProgramPulse(int portnum) |
| svlach | 0:70c3bea805ee | 835 | { |
| svlach | 0:70c3bea805ee | 836 | return ProgramAvailable[portnum]; |
| svlach | 0:70c3bea805ee | 837 | } |
| svlach | 0:70c3bea805ee | 838 |