1-Wire implementation, using DS2480B controller interfaced with serial port, working example to read DS18B20, based on work already in progress / Dallas - Public domain code

Dependencies:   mbed

Committer:
pwheels
Date:
Thu Mar 24 17:21:29 2011 +0000
Revision:
0:1193dbfe28e2

        

Who changed what in which revision?

UserRevisionLine numberNew contents of line
pwheels 0:1193dbfe28e2 1 //---------------------------------------------------------------------------
pwheels 0:1193dbfe28e2 2 // Copyright (C) 2000v Dallas Semiconductor Corporation, All Rights Reserved.
pwheels 0:1193dbfe28e2 3 //
pwheels 0:1193dbfe28e2 4 // Permission is hereby granted, free of charge, to any person obtaining a
pwheels 0:1193dbfe28e2 5 // copy of this software and associated documentation files (the "Software"),
pwheels 0:1193dbfe28e2 6 // to deal in the Software without restriction, including without limitation
pwheels 0:1193dbfe28e2 7 // the rights to use, copy, modify, merge, publish, distribute, sublicense,
pwheels 0:1193dbfe28e2 8 // and/or sell copies of the Software, and to permit persons to whom the
pwheels 0:1193dbfe28e2 9 // Software is furnished to do so, subject to the following conditions:
pwheels 0:1193dbfe28e2 10 //
pwheels 0:1193dbfe28e2 11 // The above copyright notice and this permission notice shall be included
pwheels 0:1193dbfe28e2 12 // in all copies or substantial portions of the Software.
pwheels 0:1193dbfe28e2 13 //
pwheels 0:1193dbfe28e2 14 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
pwheels 0:1193dbfe28e2 15 // OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
pwheels 0:1193dbfe28e2 16 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
pwheels 0:1193dbfe28e2 17 // IN NO EVENT SHALL DALLAS SEMICONDUCTOR BE LIABLE FOR ANY CLAIM, DAMAGES
pwheels 0:1193dbfe28e2 18 // OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
pwheels 0:1193dbfe28e2 19 // ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
pwheels 0:1193dbfe28e2 20 // OTHER DEALINGS IN THE SOFTWARE.
pwheels 0:1193dbfe28e2 21 //
pwheels 0:1193dbfe28e2 22 // Except as contained in this notice, the name of Dallas Semiconductor
pwheels 0:1193dbfe28e2 23 // shall not be used except as stated in the Dallas Semiconductor
pwheels 0:1193dbfe28e2 24 // Branding Policy.
pwheels 0:1193dbfe28e2 25 //---------------------------------------------------------------------------
pwheels 0:1193dbfe28e2 26 //
pwheels 0:1193dbfe28e2 27 // ds2480ut.c - DS2480B utility functions.
pwheels 0:1193dbfe28e2 28 //
pwheels 0:1193dbfe28e2 29 // Version: 2.01
pwheels 0:1193dbfe28e2 30 //
pwheels 0:1193dbfe28e2 31 // History: 1.00 -> 1.01 Default PDSRC changed from 0.83 to 1.37V/us
pwheels 0:1193dbfe28e2 32 // in DS2480Detect. Changed to use msDelay instead
pwheels 0:1193dbfe28e2 33 // of Delay.
pwheels 0:1193dbfe28e2 34 // 1.01 -> 1.02 Changed global declarations from 'uchar' to 'int'.
pwheels 0:1193dbfe28e2 35 // Changed DSO/WORT from 7 to 10us in DS2480Detect.
pwheels 0:1193dbfe28e2 36 // 1.02 -> 1.03 Removed caps in #includes for Linux capatibility
pwheels 0:1193dbfe28e2 37 // 1.03 -> 2.00 Changed 'MLan' to 'ow'. Added support for
pwheels 0:1193dbfe28e2 38 // multiple ports. Changed W1LT to 8us.
pwheels 0:1193dbfe28e2 39 // 2.00 -> 2.01 Added error handling. Added circular-include check.
pwheels 0:1193dbfe28e2 40 // 2.01 -> 2.10 Added raw memory error handling and SMALLINT
pwheels 0:1193dbfe28e2 41 // 2.10 -> 3.00 Added memory bank functionality
pwheels 0:1193dbfe28e2 42 // Added file I/O operations
pwheels 0:1193dbfe28e2 43 // 3.00 -> 3.10 Modified as to properly work with mbed and send code
pwheels 0:1193dbfe28e2 44 // as defined by AN192 / DS2480B PDF's
pwheels 0:1193dbfe28e2 45 //
pwheels 0:1193dbfe28e2 46
pwheels 0:1193dbfe28e2 47 #include "ownet.h"
pwheels 0:1193dbfe28e2 48 #include "./Headers/ds2480.h"
pwheels 0:1193dbfe28e2 49
pwheels 0:1193dbfe28e2 50 // global DS2480B state
pwheels 0:1193dbfe28e2 51 SMALLINT ULevel[MAX_PORTNUM]; // current DS2480B 1-Wire Net level
pwheels 0:1193dbfe28e2 52 SMALLINT UBaud[MAX_PORTNUM]; // current DS2480B baud rate
pwheels 0:1193dbfe28e2 53 SMALLINT UMode[MAX_PORTNUM]; // current DS2480B command or data mode state
pwheels 0:1193dbfe28e2 54 SMALLINT USpeed[MAX_PORTNUM]; // current DS2480B 1-Wire Net communication speed
pwheels 0:1193dbfe28e2 55 SMALLINT UVersion[MAX_PORTNUM]; // current DS2480B version
pwheels 0:1193dbfe28e2 56
pwheels 0:1193dbfe28e2 57 //---------------------------------------------------------------------------
pwheels 0:1193dbfe28e2 58 // Attempt to resyc and detect a DS2480B
pwheels 0:1193dbfe28e2 59 //
pwheels 0:1193dbfe28e2 60 // 'portnum' - number 0 to MAX_PORTNUM-1. This number was provided to
pwheels 0:1193dbfe28e2 61 // OpenCOM to indicate the port number.
pwheels 0:1193dbfe28e2 62 //
pwheels 0:1193dbfe28e2 63 // Returns: TRUE - DS2480B detected successfully
pwheels 0:1193dbfe28e2 64 // FALSE - Could not detect DS2480B
pwheels 0:1193dbfe28e2 65 //
pwheels 0:1193dbfe28e2 66 SMALLINT DS2480Detect(int portnum)
pwheels 0:1193dbfe28e2 67 {
pwheels 0:1193dbfe28e2 68 uchar sendpacket[10],readbuffer[10];
pwheels 0:1193dbfe28e2 69 uchar sendlen=0;
pwheels 0:1193dbfe28e2 70
pwheels 0:1193dbfe28e2 71 // reset modes
pwheels 0:1193dbfe28e2 72 UMode[portnum] = MODSEL_COMMAND;
pwheels 0:1193dbfe28e2 73 /* has been changed, this is causing issue's when not sending RX invert using MBED
pwheels 0:1193dbfe28e2 74 UBaud[portnum] = PARMSET_INV_9600; */
pwheels 0:1193dbfe28e2 75 UBaud[portnum] = PARMSET_9600;
pwheels 0:1193dbfe28e2 76 USpeed[portnum] = SPEEDSEL_FLEX;
pwheels 0:1193dbfe28e2 77
pwheels 0:1193dbfe28e2 78 // set the baud rate to 9600
pwheels 0:1193dbfe28e2 79 SetBaudCOM(portnum,(uchar)UBaud[portnum]);
pwheels 0:1193dbfe28e2 80
pwheels 0:1193dbfe28e2 81 // send a break to reset the DS2480
pwheels 0:1193dbfe28e2 82 BreakCOM(portnum);
pwheels 0:1193dbfe28e2 83
pwheels 0:1193dbfe28e2 84 // delay to let line settle
pwheels 0:1193dbfe28e2 85 msDelay(2);
pwheels 0:1193dbfe28e2 86
pwheels 0:1193dbfe28e2 87 // flush the buffers
pwheels 0:1193dbfe28e2 88 FlushCOM(portnum);
pwheels 0:1193dbfe28e2 89
pwheels 0:1193dbfe28e2 90 // send the reset byte
pwheels 0:1193dbfe28e2 91 sendpacket[0] = 0xC1;
pwheels 0:1193dbfe28e2 92 if (WriteCOM(portnum,1,sendpacket) != 1)
pwheels 0:1193dbfe28e2 93 {
pwheels 0:1193dbfe28e2 94 OWERROR(OWERROR_WRITECOM_FAILED);
pwheels 0:1193dbfe28e2 95 return FALSE;
pwheels 0:1193dbfe28e2 96 }
pwheels 0:1193dbfe28e2 97
pwheels 0:1193dbfe28e2 98 // delay to let line settle
pwheels 0:1193dbfe28e2 99 msDelay(2);
pwheels 0:1193dbfe28e2 100
pwheels 0:1193dbfe28e2 101 /* has been removed as this is causing issue's when used with mbed
pwheels 0:1193dbfe28e2 102 // send the Baud Rate byte, which also inverts RX signal
pwheels 0:1193dbfe28e2 103 sendpacket[0] = 0x79;
pwheels 0:1193dbfe28e2 104 if (WriteCOM(portnum,1,sendpacket) != 1)
pwheels 0:1193dbfe28e2 105 {
pwheels 0:1193dbfe28e2 106 OWERROR(OWERROR_WRITECOM_FAILED);
pwheels 0:1193dbfe28e2 107 return FALSE;
pwheels 0:1193dbfe28e2 108 }
pwheels 0:1193dbfe28e2 109
pwheels 0:1193dbfe28e2 110 // delay to let line settle
pwheels 0:1193dbfe28e2 111 msDelay(4);
pwheels 0:1193dbfe28e2 112 */
pwheels 0:1193dbfe28e2 113 // set the FLEX configuration parameters
pwheels 0:1193dbfe28e2 114 // default PDSRC = 1.37Vus
pwheels 0:1193dbfe28e2 115 sendpacket[sendlen++] = CMD_CONFIG | PARMSEL_SLEW | PARMSET_Slew1p37Vus;
pwheels 0:1193dbfe28e2 116 // default W1LT = 10us
pwheels 0:1193dbfe28e2 117 sendpacket[sendlen++] = CMD_CONFIG | PARMSEL_WRITE1LOW | PARMSET_Write10us;
pwheels 0:1193dbfe28e2 118 // default DSO/WORT = 8us
pwheels 0:1193dbfe28e2 119 sendpacket[sendlen++] = CMD_CONFIG | PARMSEL_SAMPLEOFFSET | PARMSET_SampOff8us;
pwheels 0:1193dbfe28e2 120 // construct the command to read the baud rate (to test command block)
pwheels 0:1193dbfe28e2 121 sendpacket[sendlen++] = CMD_CONFIG | PARMSEL_PARMREAD | (PARMSEL_BAUDRATE >> 3);
pwheels 0:1193dbfe28e2 122 // also do 1 bit operation (to test 1-Wire block)
pwheels 0:1193dbfe28e2 123 sendpacket[sendlen++] = CMD_COMM | FUNCTSEL_BIT | UBaud[portnum] | BITPOL_ONE;
pwheels 0:1193dbfe28e2 124
pwheels 0:1193dbfe28e2 125 // flush the buffers
pwheels 0:1193dbfe28e2 126 FlushCOM(portnum);
pwheels 0:1193dbfe28e2 127 // send the packet
pwheels 0:1193dbfe28e2 128 if (WriteCOM(portnum,sendlen,sendpacket))
pwheels 0:1193dbfe28e2 129 {
pwheels 0:1193dbfe28e2 130 // read back the response
pwheels 0:1193dbfe28e2 131 if (ReadCOM(portnum,5,readbuffer) == 5)
pwheels 0:1193dbfe28e2 132 {
pwheels 0:1193dbfe28e2 133 // look at the baud rate and bit operation
pwheels 0:1193dbfe28e2 134 // to see if the response makes sense
pwheels 0:1193dbfe28e2 135 if (((readbuffer[3] & 0xF1) == 0x00) &&
pwheels 0:1193dbfe28e2 136 ((readbuffer[3] & 0x0E) == UBaud[portnum]) &&
pwheels 0:1193dbfe28e2 137 ((readbuffer[4] & 0xF0) == 0x90) &&
pwheels 0:1193dbfe28e2 138 ((readbuffer[4] & 0x0C) == UBaud[portnum]))
pwheels 0:1193dbfe28e2 139 return TRUE;
pwheels 0:1193dbfe28e2 140 else
pwheels 0:1193dbfe28e2 141 OWERROR(OWERROR_DS2480_BAD_RESPONSE);
pwheels 0:1193dbfe28e2 142 }
pwheels 0:1193dbfe28e2 143 else
pwheels 0:1193dbfe28e2 144 OWERROR(OWERROR_READCOM_FAILED);
pwheels 0:1193dbfe28e2 145 }
pwheels 0:1193dbfe28e2 146 else
pwheels 0:1193dbfe28e2 147 OWERROR(OWERROR_WRITECOM_FAILED);
pwheels 0:1193dbfe28e2 148
pwheels 0:1193dbfe28e2 149 return FALSE;
pwheels 0:1193dbfe28e2 150 }
pwheels 0:1193dbfe28e2 151
pwheels 0:1193dbfe28e2 152 //---------------------------------------------------------------------------
pwheels 0:1193dbfe28e2 153 // Change the DS2480B from the current baud rate to the new baud rate.
pwheels 0:1193dbfe28e2 154 //
pwheels 0:1193dbfe28e2 155 // 'portnum' - number 0 to MAX_PORTNUM-1. This number was provided to
pwheels 0:1193dbfe28e2 156 // OpenCOM to indicate the port number.
pwheels 0:1193dbfe28e2 157 // 'newbaud' - the new baud rate to change to, defined as:
pwheels 0:1193dbfe28e2 158 // PARMSET_9600 0x00
pwheels 0:1193dbfe28e2 159 // PARMSET_19200 0x02
pwheels 0:1193dbfe28e2 160 // PARMSET_57600 0x04
pwheels 0:1193dbfe28e2 161 // PARMSET_115200 0x06
pwheels 0:1193dbfe28e2 162 //
pwheels 0:1193dbfe28e2 163 // Returns: current DS2480B baud rate.
pwheels 0:1193dbfe28e2 164 //
pwheels 0:1193dbfe28e2 165 SMALLINT DS2480ChangeBaud(int portnum, uchar newbaud)
pwheels 0:1193dbfe28e2 166 {
pwheels 0:1193dbfe28e2 167 uchar rt=FALSE;
pwheels 0:1193dbfe28e2 168 uchar readbuffer[5],sendpacket[5],sendpacket2[5];
pwheels 0:1193dbfe28e2 169 uchar sendlen=0,sendlen2=0;
pwheels 0:1193dbfe28e2 170
pwheels 0:1193dbfe28e2 171 // see if diffenent then current baud rate
pwheels 0:1193dbfe28e2 172 if (UBaud[portnum] == newbaud)
pwheels 0:1193dbfe28e2 173 return UBaud[portnum];
pwheels 0:1193dbfe28e2 174 else
pwheels 0:1193dbfe28e2 175 {
pwheels 0:1193dbfe28e2 176 // build the command packet
pwheels 0:1193dbfe28e2 177 // check if correct mode
pwheels 0:1193dbfe28e2 178 if (UMode[portnum] != MODSEL_COMMAND)
pwheels 0:1193dbfe28e2 179 {
pwheels 0:1193dbfe28e2 180 UMode[portnum] = MODSEL_COMMAND;
pwheels 0:1193dbfe28e2 181 sendpacket[sendlen++] = MODE_COMMAND;
pwheels 0:1193dbfe28e2 182 }
pwheels 0:1193dbfe28e2 183 // build the command
pwheels 0:1193dbfe28e2 184 sendpacket[sendlen++] = CMD_CONFIG | PARMSEL_BAUDRATE | newbaud;
pwheels 0:1193dbfe28e2 185
pwheels 0:1193dbfe28e2 186 // flush the buffers
pwheels 0:1193dbfe28e2 187 FlushCOM(portnum);
pwheels 0:1193dbfe28e2 188
pwheels 0:1193dbfe28e2 189 // send the packet
pwheels 0:1193dbfe28e2 190 if (!WriteCOM(portnum,sendlen,sendpacket))
pwheels 0:1193dbfe28e2 191 {
pwheels 0:1193dbfe28e2 192 OWERROR(OWERROR_WRITECOM_FAILED);
pwheels 0:1193dbfe28e2 193 rt = FALSE;
pwheels 0:1193dbfe28e2 194 }
pwheels 0:1193dbfe28e2 195 else
pwheels 0:1193dbfe28e2 196 {
pwheels 0:1193dbfe28e2 197 // make sure buffer is flushed
pwheels 0:1193dbfe28e2 198 msDelay(5);
pwheels 0:1193dbfe28e2 199
pwheels 0:1193dbfe28e2 200 // change our baud rate
pwheels 0:1193dbfe28e2 201 SetBaudCOM(portnum,newbaud);
pwheels 0:1193dbfe28e2 202 UBaud[portnum] = newbaud;
pwheels 0:1193dbfe28e2 203
pwheels 0:1193dbfe28e2 204 // wait for things to settle
pwheels 0:1193dbfe28e2 205 msDelay(5);
pwheels 0:1193dbfe28e2 206
pwheels 0:1193dbfe28e2 207 // build a command packet to read back baud rate
pwheels 0:1193dbfe28e2 208 sendpacket2[sendlen2++] = CMD_CONFIG | PARMSEL_PARMREAD | (PARMSEL_BAUDRATE >> 3);
pwheels 0:1193dbfe28e2 209
pwheels 0:1193dbfe28e2 210 // flush the buffers
pwheels 0:1193dbfe28e2 211 FlushCOM(portnum);
pwheels 0:1193dbfe28e2 212
pwheels 0:1193dbfe28e2 213 // send the packet
pwheels 0:1193dbfe28e2 214 if (WriteCOM(portnum,sendlen2,sendpacket2))
pwheels 0:1193dbfe28e2 215 {
pwheels 0:1193dbfe28e2 216 // read back the 1 byte response
pwheels 0:1193dbfe28e2 217 if (ReadCOM(portnum,1,readbuffer) == 1)
pwheels 0:1193dbfe28e2 218 {
pwheels 0:1193dbfe28e2 219 // verify correct baud
pwheels 0:1193dbfe28e2 220 if (((readbuffer[0] & 0x0E) == (sendpacket[sendlen-1] & 0x0E)))
pwheels 0:1193dbfe28e2 221 rt = TRUE;
pwheels 0:1193dbfe28e2 222 else
pwheels 0:1193dbfe28e2 223 OWERROR(OWERROR_DS2480_WRONG_BAUD);
pwheels 0:1193dbfe28e2 224 }
pwheels 0:1193dbfe28e2 225 else
pwheels 0:1193dbfe28e2 226 OWERROR(OWERROR_READCOM_FAILED);
pwheels 0:1193dbfe28e2 227 }
pwheels 0:1193dbfe28e2 228 else
pwheels 0:1193dbfe28e2 229 OWERROR(OWERROR_WRITECOM_FAILED);
pwheels 0:1193dbfe28e2 230 }
pwheels 0:1193dbfe28e2 231 }
pwheels 0:1193dbfe28e2 232
pwheels 0:1193dbfe28e2 233 // if lost communication with DS2480 then reset
pwheels 0:1193dbfe28e2 234 if (rt != TRUE)
pwheels 0:1193dbfe28e2 235 DS2480Detect(portnum);
pwheels 0:1193dbfe28e2 236
pwheels 0:1193dbfe28e2 237 return UBaud[portnum];
pwheels 0:1193dbfe28e2 238 }