1-Wire® library for mbed. Complete 1-Wire library that supports our silicon masters along with a bit-bang master on the MAX32600MBED platform with one common interface for mbed. Slave support has also been included and more slaves will be added as time permits.

Dependents:   MAXREFDES131_Qt_Demo MAX32630FTHR_iButton_uSD_Logger MAX32630FTHR_DS18B20_uSD_Logger MAXREFDES130_131_Demo ... more

Superseded by MaximInterface.

Committer:
IanBenzMaxim
Date:
Fri Aug 05 15:06:47 2016 -0500
Revision:
109:5c9180b4be25
Parent:
104:3f48daed532b
Child:
112:82eb520a644b
Begin cleanup of DS2480B. Removed receive buffer and some unused functionality.

Who changed what in which revision?

UserRevisionLine numberNew contents of line
j3 3:644fc630f958 1 /******************************************************************//**
j3 3:644fc630f958 2 * Copyright (C) 2016 Maxim Integrated Products, Inc., All Rights Reserved.
j3 3:644fc630f958 3 *
j3 3:644fc630f958 4 * Permission is hereby granted, free of charge, to any person obtaining a
j3 3:644fc630f958 5 * copy of this software and associated documentation files (the "Software"),
j3 3:644fc630f958 6 * to deal in the Software without restriction, including without limitation
j3 3:644fc630f958 7 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
j3 3:644fc630f958 8 * and/or sell copies of the Software, and to permit persons to whom the
j3 3:644fc630f958 9 * Software is furnished to do so, subject to the following conditions:
j3 3:644fc630f958 10 *
j3 3:644fc630f958 11 * The above copyright notice and this permission notice shall be included
j3 3:644fc630f958 12 * in all copies or substantial portions of the Software.
j3 3:644fc630f958 13 *
j3 3:644fc630f958 14 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
j3 3:644fc630f958 15 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
j3 3:644fc630f958 16 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
j3 3:644fc630f958 17 * IN NO EVENT SHALL MAXIM INTEGRATED BE LIABLE FOR ANY CLAIM, DAMAGES
j3 3:644fc630f958 18 * OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
j3 3:644fc630f958 19 * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
j3 3:644fc630f958 20 * OTHER DEALINGS IN THE SOFTWARE.
j3 3:644fc630f958 21 *
j3 3:644fc630f958 22 * Except as contained in this notice, the name of Maxim Integrated
j3 3:644fc630f958 23 * Products, Inc. shall not be used except as stated in the Maxim Integrated
j3 3:644fc630f958 24 * Products, Inc. Branding Policy.
j3 3:644fc630f958 25 *
j3 3:644fc630f958 26 * The mere transfer of this software does not imply any licenses
j3 3:644fc630f958 27 * of trade secrets, proprietary technology, copyrights, patents,
j3 3:644fc630f958 28 * trademarks, maskwork rights, or any other form of intellectual
j3 3:644fc630f958 29 * property whatsoever. Maxim Integrated Products, Inc. retains all
j3 3:644fc630f958 30 * ownership rights.
j3 3:644fc630f958 31 **********************************************************************/
j3 3:644fc630f958 32
j3 104:3f48daed532b 33 #include "Masters/DS2480B/DS2480B.h"
IanBenzMaxim 73:2cecc1372acc 34 #include "Serial.h"
IanBenzMaxim 73:2cecc1372acc 35 #include "Timer.h"
IanBenzMaxim 73:2cecc1372acc 36 #include "wait_api.h"
j3 63:422be898443a 37
j3 63:422be898443a 38 // Mode Commands
j3 63:422be898443a 39 #define MODE_DATA 0xE1
j3 63:422be898443a 40 #define MODE_COMMAND 0xE3
j3 63:422be898443a 41 #define MODE_STOP_PULSE 0xF1
j3 63:422be898443a 42
j3 63:422be898443a 43 // Return byte value
j3 63:422be898443a 44 #define RB_CHIPID_MASK 0x1C
j3 63:422be898443a 45 #define RB_RESET_MASK 0x03
j3 63:422be898443a 46 #define RB_1WIRESHORT 0x00
j3 63:422be898443a 47 #define RB_PRESENCE 0x01
j3 63:422be898443a 48 #define RB_ALARMPRESENCE 0x02
j3 63:422be898443a 49 #define RB_NOPRESENCE 0x03
j3 63:422be898443a 50
j3 63:422be898443a 51 #define RB_BIT_MASK 0x03
j3 63:422be898443a 52 #define RB_BIT_ONE 0x03
j3 63:422be898443a 53 #define RB_BIT_ZERO 0x00
j3 63:422be898443a 54
j3 63:422be898443a 55 // Masks for all bit ranges
j3 63:422be898443a 56 #define CMD_MASK 0x80
j3 63:422be898443a 57 #define FUNCTSEL_MASK 0x60
j3 63:422be898443a 58 #define BITPOL_MASK 0x10
j3 63:422be898443a 59 #define SPEEDSEL_MASK 0x0C
j3 63:422be898443a 60 #define MODSEL_MASK 0x02
j3 63:422be898443a 61 #define PARMSEL_MASK 0x70
j3 63:422be898443a 62 #define PARMSET_MASK 0x0E
j3 63:422be898443a 63
j3 63:422be898443a 64 // Command or config bit
j3 63:422be898443a 65 #define CMD_COMM 0x81
j3 63:422be898443a 66 #define CMD_CONFIG 0x01
j3 63:422be898443a 67
j3 63:422be898443a 68 // Function select bits
j3 63:422be898443a 69 #define FUNCTSEL_BIT 0x00
j3 63:422be898443a 70 #define FUNCTSEL_SEARCHON 0x30
j3 63:422be898443a 71 #define FUNCTSEL_SEARCHOFF 0x20
j3 63:422be898443a 72 #define FUNCTSEL_RESET 0x40
j3 63:422be898443a 73 #define FUNCTSEL_CHMOD 0x60
j3 63:422be898443a 74
j3 63:422be898443a 75 // Bit polarity/Pulse voltage bits
j3 63:422be898443a 76 #define BITPOL_ONE 0x10
j3 63:422be898443a 77 #define BITPOL_ZERO 0x00
j3 63:422be898443a 78 #define BITPOL_5V 0x00
j3 63:422be898443a 79 #define BITPOL_12V 0x10
j3 63:422be898443a 80
j3 63:422be898443a 81 // One Wire speed bits
j3 63:422be898443a 82 #define SPEEDSEL_STD 0x00
j3 63:422be898443a 83 #define SPEEDSEL_FLEX 0x04
j3 63:422be898443a 84 #define SPEEDSEL_OD 0x08
j3 63:422be898443a 85 #define SPEEDSEL_PULSE 0x0C
j3 63:422be898443a 86
j3 63:422be898443a 87 // Data/Command mode select bits
j3 63:422be898443a 88 #define MODSEL_DATA 0x00
j3 63:422be898443a 89 #define MODSEL_COMMAND 0x02
j3 63:422be898443a 90
j3 63:422be898443a 91 // 5V Follow Pulse select bits
j3 63:422be898443a 92 #define PRIME5V_TRUE 0x02
j3 63:422be898443a 93 #define PRIME5V_FALSE 0x00
j3 63:422be898443a 94
j3 63:422be898443a 95 // Parameter select bits
j3 63:422be898443a 96 #define PARMSEL_PARMREAD 0x00
j3 63:422be898443a 97 #define PARMSEL_SLEW 0x10
j3 63:422be898443a 98 #define PARMSEL_12VPULSE 0x20
j3 63:422be898443a 99 #define PARMSEL_5VPULSE 0x30
j3 63:422be898443a 100 #define PARMSEL_WRITE1LOW 0x40
j3 63:422be898443a 101 #define PARMSEL_SAMPLEOFFSET 0x50
j3 63:422be898443a 102 #define PARMSEL_ACTIVEPULLUPTIME 0x60
j3 63:422be898443a 103 #define PARMSEL_BAUDRATE 0x70
j3 63:422be898443a 104
j3 63:422be898443a 105 // Pull down slew rate.
j3 63:422be898443a 106 #define PARMSET_Slew15Vus 0x00
j3 63:422be898443a 107 #define PARMSET_Slew2p2Vus 0x02
j3 63:422be898443a 108 #define PARMSET_Slew1p65Vus 0x04
j3 63:422be898443a 109 #define PARMSET_Slew1p37Vus 0x06
j3 63:422be898443a 110 #define PARMSET_Slew1p1Vus 0x08
j3 63:422be898443a 111 #define PARMSET_Slew0p83Vus 0x0A
j3 63:422be898443a 112 #define PARMSET_Slew0p7Vus 0x0C
j3 63:422be898443a 113 #define PARMSET_Slew0p55Vus 0x0E
j3 63:422be898443a 114
j3 63:422be898443a 115 // 12V programming pulse time table
j3 63:422be898443a 116 #define PARMSET_32us 0x00
j3 63:422be898443a 117 #define PARMSET_64us 0x02
j3 63:422be898443a 118 #define PARMSET_128us 0x04
j3 63:422be898443a 119 #define PARMSET_256us 0x06
j3 63:422be898443a 120 #define PARMSET_512us 0x08
j3 63:422be898443a 121 #define PARMSET_1024us 0x0A
j3 63:422be898443a 122 #define PARMSET_2048us 0x0C
j3 63:422be898443a 123 #define PARMSET_infinite 0x0E
j3 63:422be898443a 124
j3 63:422be898443a 125 // 5V strong pull up pulse time table
j3 63:422be898443a 126 #define PARMSET_16p4ms 0x00
j3 63:422be898443a 127 #define PARMSET_65p5ms 0x02
j3 63:422be898443a 128 #define PARMSET_131ms 0x04
j3 63:422be898443a 129 #define PARMSET_262ms 0x06
j3 63:422be898443a 130 #define PARMSET_524ms 0x08
j3 63:422be898443a 131 #define PARMSET_1p05s 0x0A
j3 63:422be898443a 132 #define PARMSET_dynamic 0x0C
j3 63:422be898443a 133 #define PARMSET_infinite 0x0E
j3 63:422be898443a 134
j3 63:422be898443a 135 // Write 1 low time
j3 63:422be898443a 136 #define PARMSET_Write8us 0x00
j3 63:422be898443a 137 #define PARMSET_Write9us 0x02
j3 63:422be898443a 138 #define PARMSET_Write10us 0x04
j3 63:422be898443a 139 #define PARMSET_Write11us 0x06
j3 63:422be898443a 140 #define PARMSET_Write12us 0x08
j3 63:422be898443a 141 #define PARMSET_Write13us 0x0A
j3 63:422be898443a 142 #define PARMSET_Write14us 0x0C
j3 63:422be898443a 143 #define PARMSET_Write15us 0x0E
j3 63:422be898443a 144
j3 63:422be898443a 145 // Data sample offset and Write 0 recovery time
j3 63:422be898443a 146 #define PARMSET_SampOff3us 0x00
j3 63:422be898443a 147 #define PARMSET_SampOff4us 0x02
j3 63:422be898443a 148 #define PARMSET_SampOff5us 0x04
j3 63:422be898443a 149 #define PARMSET_SampOff6us 0x06
j3 63:422be898443a 150 #define PARMSET_SampOff7us 0x08
j3 63:422be898443a 151 #define PARMSET_SampOff8us 0x0A
j3 63:422be898443a 152 #define PARMSET_SampOff9us 0x0C
j3 63:422be898443a 153 #define PARMSET_SampOff10us 0x0E
j3 63:422be898443a 154
j3 63:422be898443a 155 // Active pull up on time
j3 63:422be898443a 156 #define PARMSET_PullUp0p0us 0x00
j3 63:422be898443a 157 #define PARMSET_PullUp0p5us 0x02
j3 63:422be898443a 158 #define PARMSET_PullUp1p0us 0x04
j3 63:422be898443a 159 #define PARMSET_PullUp1p5us 0x06
j3 63:422be898443a 160 #define PARMSET_PullUp2p0us 0x08
j3 63:422be898443a 161 #define PARMSET_PullUp2p5us 0x0A
j3 63:422be898443a 162 #define PARMSET_PullUp3p0us 0x0C
j3 63:422be898443a 163 #define PARMSET_PullUp3p5us 0x0E
j3 63:422be898443a 164
j3 63:422be898443a 165 // DS2480B program voltage available
j3 63:422be898443a 166 #define DS2480BPROG_MASK 0x20
j3 63:422be898443a 167
IanBenzMaxim 76:84e6c4994e29 168 using OneWire::DS2480B;
IanBenzMaxim 76:84e6c4994e29 169 using OneWire::OneWireMaster;
j3 3:644fc630f958 170
IanBenzMaxim 109:5c9180b4be25 171 static uint32_t calculateBitTimeout(DS2480B::BaudRate baud)
IanBenzMaxim 109:5c9180b4be25 172 {
IanBenzMaxim 109:5c9180b4be25 173 // Calculate bit timeout in microsecond.
IanBenzMaxim 109:5c9180b4be25 174 // 10x the time needed to transmit or receive.
IanBenzMaxim 109:5c9180b4be25 175 // Double for 115200 due to timer inaccuracies.
IanBenzMaxim 109:5c9180b4be25 176
IanBenzMaxim 109:5c9180b4be25 177 uint32_t timeout = 1000000 * 10;
IanBenzMaxim 109:5c9180b4be25 178
IanBenzMaxim 109:5c9180b4be25 179 switch (baud)
IanBenzMaxim 109:5c9180b4be25 180 {
IanBenzMaxim 109:5c9180b4be25 181 case DS2480B::Baud115200bps:
IanBenzMaxim 109:5c9180b4be25 182 timeout = (timeout * 2) / 115200;
IanBenzMaxim 109:5c9180b4be25 183 break;
j3 3:644fc630f958 184
IanBenzMaxim 109:5c9180b4be25 185 case DS2480B::Baud57600bps:
IanBenzMaxim 109:5c9180b4be25 186 timeout /= 57600;
IanBenzMaxim 109:5c9180b4be25 187 break;
IanBenzMaxim 109:5c9180b4be25 188
IanBenzMaxim 109:5c9180b4be25 189 case DS2480B::Baud19200bps:
IanBenzMaxim 109:5c9180b4be25 190 timeout /= 19200;
IanBenzMaxim 109:5c9180b4be25 191 break;
IanBenzMaxim 109:5c9180b4be25 192
IanBenzMaxim 109:5c9180b4be25 193 case DS2480B::Baud9600bps:
IanBenzMaxim 109:5c9180b4be25 194 default:
IanBenzMaxim 109:5c9180b4be25 195 timeout /= 9600;
IanBenzMaxim 109:5c9180b4be25 196 break;
j3 3:644fc630f958 197 }
IanBenzMaxim 109:5c9180b4be25 198
IanBenzMaxim 109:5c9180b4be25 199 return timeout;
j3 3:644fc630f958 200 }
j3 3:644fc630f958 201
IanBenzMaxim 109:5c9180b4be25 202 DS2480B::DS2480B(PinName tx, PinName rx)
IanBenzMaxim 109:5c9180b4be25 203 : serial(tx, rx)
j3 63:422be898443a 204 {
IanBenzMaxim 109:5c9180b4be25 205
j3 63:422be898443a 206 }
j3 63:422be898443a 207
IanBenzMaxim 75:8b627804927c 208 OneWireMaster::CmdResult DS2480B::OWInitMaster()
j3 14:7b2886a50321 209 {
IanBenzMaxim 109:5c9180b4be25 210 return detect();
j3 14:7b2886a50321 211 }
j3 14:7b2886a50321 212
IanBenzMaxim 75:8b627804927c 213 OneWireMaster::CmdResult DS2480B::OWReset()
j3 3:644fc630f958 214 {
j3 63:422be898443a 215 OneWireMaster::CmdResult result;
j3 63:422be898443a 216
IanBenzMaxim 74:23be10c32fa3 217 uint8_t readbuffer[10], sendpacket[10];
IanBenzMaxim 74:23be10c32fa3 218 uint8_t sendlen = 0;
j3 63:422be898443a 219
j3 63:422be898443a 220 // make sure normal level
IanBenzMaxim 75:8b627804927c 221 result = OWSetLevel(OneWireMaster::NormalLevel);
IanBenzMaxim 74:23be10c32fa3 222 if (result == OneWireMaster::Success)
j3 63:422be898443a 223 {
j3 63:422be898443a 224 // check for correct mode
IanBenzMaxim 109:5c9180b4be25 225 if (mode != MODSEL_COMMAND)
j3 63:422be898443a 226 {
IanBenzMaxim 109:5c9180b4be25 227 mode = MODSEL_COMMAND;
j3 63:422be898443a 228 sendpacket[sendlen++] = MODE_COMMAND;
j3 63:422be898443a 229 }
j3 63:422be898443a 230
j3 63:422be898443a 231 // construct the command
IanBenzMaxim 109:5c9180b4be25 232 sendpacket[sendlen++] = (uint8_t)(CMD_COMM | FUNCTSEL_RESET | speed);
IanBenzMaxim 74:23be10c32fa3 233
IanBenzMaxim 109:5c9180b4be25 234 // flush the buffers
IanBenzMaxim 109:5c9180b4be25 235 flushCom();
j3 63:422be898443a 236
j3 63:422be898443a 237 // send the packet
IanBenzMaxim 109:5c9180b4be25 238 result = writeCom(sendlen, sendpacket);
IanBenzMaxim 74:23be10c32fa3 239 if (result == OneWireMaster::Success)
j3 63:422be898443a 240 {
j3 63:422be898443a 241 // read back the 1 byte response
IanBenzMaxim 109:5c9180b4be25 242 result = readCom(1, readbuffer);
IanBenzMaxim 74:23be10c32fa3 243 if (result == OneWireMaster::Success)
j3 63:422be898443a 244 {
j3 63:422be898443a 245 // make sure this byte looks like a reset byte
IanBenzMaxim 74:23be10c32fa3 246 if (((readbuffer[0] & RB_RESET_MASK) == RB_PRESENCE) || ((readbuffer[0] & RB_RESET_MASK) == RB_ALARMPRESENCE))
j3 63:422be898443a 247 {
j3 63:422be898443a 248 result = OneWireMaster::Success;
j3 63:422be898443a 249 }
j3 63:422be898443a 250 else
j3 63:422be898443a 251 {
j3 63:422be898443a 252 result = OneWireMaster::OperationFailure;
j3 63:422be898443a 253 }
j3 63:422be898443a 254 }
j3 63:422be898443a 255 }
j3 63:422be898443a 256 }
j3 63:422be898443a 257
j3 17:b646b1e3970b 258 return result;
j3 3:644fc630f958 259 }
j3 3:644fc630f958 260
IanBenzMaxim 75:8b627804927c 261 OneWireMaster::CmdResult DS2480B::OWTouchBitSetLevel(uint8_t & sendRecvBit, OWLevel afterLevel)
j3 3:644fc630f958 262 {
j3 23:e8e403d61359 263 OneWireMaster::CmdResult result = OneWireMaster::OperationFailure;
j3 63:422be898443a 264
IanBenzMaxim 74:23be10c32fa3 265 uint8_t readbuffer[10], sendpacket[10];
IanBenzMaxim 74:23be10c32fa3 266 uint8_t sendlen = 0;
j3 63:422be898443a 267
j3 63:422be898443a 268 // make sure normal level
IanBenzMaxim 75:8b627804927c 269 OWSetLevel(OneWireMaster::NormalLevel);
j3 63:422be898443a 270
j3 63:422be898443a 271 // check for correct mode
IanBenzMaxim 109:5c9180b4be25 272 if (mode != MODSEL_COMMAND)
j3 63:422be898443a 273 {
IanBenzMaxim 109:5c9180b4be25 274 mode = MODSEL_COMMAND;
j3 63:422be898443a 275 sendpacket[sendlen++] = MODE_COMMAND;
j3 63:422be898443a 276 }
j3 63:422be898443a 277
j3 63:422be898443a 278 // construct the command
IanBenzMaxim 75:8b627804927c 279 sendpacket[sendlen] = (sendRecvBit != 0) ? BITPOL_ONE : BITPOL_ZERO;
IanBenzMaxim 109:5c9180b4be25 280 sendpacket[sendlen++] |= CMD_COMM | FUNCTSEL_BIT | speed;
j3 63:422be898443a 281
j3 63:422be898443a 282 // flush the buffers
IanBenzMaxim 109:5c9180b4be25 283 flushCom();
j3 63:422be898443a 284
j3 63:422be898443a 285 // send the packet
IanBenzMaxim 109:5c9180b4be25 286 result = writeCom(sendlen, sendpacket);
IanBenzMaxim 74:23be10c32fa3 287 if (result == OneWireMaster::Success)
j3 63:422be898443a 288 {
j3 63:422be898443a 289 // read back the response
IanBenzMaxim 109:5c9180b4be25 290 result = readCom(1, readbuffer);
IanBenzMaxim 74:23be10c32fa3 291 if (result == OneWireMaster::Success)
j3 63:422be898443a 292 {
j3 63:422be898443a 293 // interpret the response
j3 63:422be898443a 294 if (((readbuffer[0] & 0xE0) == 0x80) && ((readbuffer[0] & RB_BIT_MASK) == RB_BIT_ONE))
j3 63:422be898443a 295 {
IanBenzMaxim 75:8b627804927c 296 sendRecvBit = 1;
j3 63:422be898443a 297 result = OneWireMaster::Success;
j3 63:422be898443a 298 }
j3 63:422be898443a 299 else
j3 63:422be898443a 300 {
IanBenzMaxim 75:8b627804927c 301 sendRecvBit = 0;
j3 63:422be898443a 302 result = OneWireMaster::Success;
j3 63:422be898443a 303 }
j3 63:422be898443a 304 }
j3 63:422be898443a 305 else
j3 63:422be898443a 306 {
j3 63:422be898443a 307 result = OneWireMaster::CommunicationReadError;
j3 63:422be898443a 308 }
j3 63:422be898443a 309 }
j3 63:422be898443a 310 else
j3 63:422be898443a 311 {
j3 63:422be898443a 312 result = OneWireMaster::CommunicationWriteError;
j3 63:422be898443a 313 }
j3 63:422be898443a 314
IanBenzMaxim 109:5c9180b4be25 315 if (result == OneWireMaster::Success)
j3 63:422be898443a 316 {
IanBenzMaxim 75:8b627804927c 317 result = OWSetLevel(afterLevel);
j3 63:422be898443a 318 }
j3 63:422be898443a 319
j3 17:b646b1e3970b 320 return result;
j3 3:644fc630f958 321 }
j3 3:644fc630f958 322
IanBenzMaxim 75:8b627804927c 323 OneWireMaster::CmdResult DS2480B::OWWriteByteSetLevel(uint8_t sendByte, OWLevel afterLevel)
IanBenzMaxim 26:a361e3f42ba5 324 {
IanBenzMaxim 26:a361e3f42ba5 325 OneWireMaster::CmdResult result = OneWireMaster::OperationFailure;
j3 63:422be898443a 326
IanBenzMaxim 74:23be10c32fa3 327 uint8_t readbuffer[10], sendpacket[10];
IanBenzMaxim 74:23be10c32fa3 328 uint8_t sendlen = 0;
j3 63:422be898443a 329
j3 63:422be898443a 330 // make sure normal level
IanBenzMaxim 75:8b627804927c 331 OWSetLevel(OneWireMaster::NormalLevel);
j3 63:422be898443a 332
j3 63:422be898443a 333 // check for correct mode
IanBenzMaxim 109:5c9180b4be25 334 if (mode != MODSEL_DATA)
j3 63:422be898443a 335 {
IanBenzMaxim 109:5c9180b4be25 336 mode = MODSEL_DATA;
j3 63:422be898443a 337 sendpacket[sendlen++] = MODE_DATA;
j3 63:422be898443a 338 }
j3 63:422be898443a 339
j3 63:422be898443a 340 // add the byte to send
IanBenzMaxim 75:8b627804927c 341 sendpacket[sendlen++] = sendByte;
j3 63:422be898443a 342
j3 63:422be898443a 343 // check for duplication of data that looks like COMMAND mode
IanBenzMaxim 75:8b627804927c 344 if (sendByte == MODE_COMMAND)
j3 63:422be898443a 345 {
IanBenzMaxim 75:8b627804927c 346 sendpacket[sendlen++] = sendByte;
j3 63:422be898443a 347 }
j3 63:422be898443a 348
j3 63:422be898443a 349 // flush the buffers
IanBenzMaxim 109:5c9180b4be25 350 flushCom();
j3 63:422be898443a 351
j3 63:422be898443a 352 // send the packet
IanBenzMaxim 109:5c9180b4be25 353 result = writeCom(sendlen, sendpacket);
IanBenzMaxim 74:23be10c32fa3 354 if (result == OneWireMaster::Success)
j3 63:422be898443a 355 {
j3 63:422be898443a 356 // read back the 1 byte response
IanBenzMaxim 109:5c9180b4be25 357 result = readCom(1, readbuffer);
IanBenzMaxim 75:8b627804927c 358 if ((result == OneWireMaster::Success) && (readbuffer[0] == sendByte))
j3 63:422be898443a 359 {
j3 63:422be898443a 360 result = OneWireMaster::Success;
j3 63:422be898443a 361 }
j3 63:422be898443a 362 else
j3 63:422be898443a 363 {
j3 63:422be898443a 364 result = OneWireMaster::CommunicationReadError;
j3 63:422be898443a 365 }
j3 63:422be898443a 366 }
j3 63:422be898443a 367 else
j3 63:422be898443a 368 {
j3 63:422be898443a 369 result = OneWireMaster::CommunicationWriteError;
j3 63:422be898443a 370 }
j3 63:422be898443a 371
IanBenzMaxim 109:5c9180b4be25 372 if (result == OneWireMaster::Success)
j3 63:422be898443a 373 {
IanBenzMaxim 75:8b627804927c 374 result = OWSetLevel(afterLevel);
j3 63:422be898443a 375 }
j3 63:422be898443a 376
IanBenzMaxim 26:a361e3f42ba5 377 return result;
IanBenzMaxim 26:a361e3f42ba5 378 }
IanBenzMaxim 26:a361e3f42ba5 379
IanBenzMaxim 75:8b627804927c 380 OneWireMaster::CmdResult DS2480B::OWReadByteSetLevel(uint8_t & recvByte, OWLevel afterLevel)
j3 3:644fc630f958 381 {
j3 23:e8e403d61359 382 OneWireMaster::CmdResult result = OneWireMaster::OperationFailure;
j3 63:422be898443a 383
IanBenzMaxim 74:23be10c32fa3 384 uint8_t readbuffer[10], sendpacket[10];
IanBenzMaxim 74:23be10c32fa3 385 uint8_t sendlen = 0;
j3 63:422be898443a 386
j3 63:422be898443a 387 // make sure normal level
IanBenzMaxim 75:8b627804927c 388 OWSetLevel(OneWireMaster::NormalLevel);
j3 63:422be898443a 389
j3 63:422be898443a 390 // check for correct mode
IanBenzMaxim 109:5c9180b4be25 391 if (mode != MODSEL_DATA)
j3 63:422be898443a 392 {
IanBenzMaxim 109:5c9180b4be25 393 mode = MODSEL_DATA;
j3 63:422be898443a 394 sendpacket[sendlen++] = MODE_DATA;
j3 63:422be898443a 395 }
j3 63:422be898443a 396
j3 63:422be898443a 397 // add the byte to send
j3 63:422be898443a 398 sendpacket[sendlen++] = 0xFF;
j3 63:422be898443a 399
j3 63:422be898443a 400 // flush the buffers
IanBenzMaxim 109:5c9180b4be25 401 flushCom();
j3 63:422be898443a 402
j3 63:422be898443a 403 // send the packet
IanBenzMaxim 109:5c9180b4be25 404 result = writeCom(sendlen, sendpacket);
IanBenzMaxim 74:23be10c32fa3 405 if (result == OneWireMaster::Success)
j3 63:422be898443a 406 {
j3 63:422be898443a 407 // read back the 1 byte response
IanBenzMaxim 109:5c9180b4be25 408 result = readCom(1, readbuffer);
IanBenzMaxim 74:23be10c32fa3 409 if (result == OneWireMaster::Success)
j3 63:422be898443a 410 {
IanBenzMaxim 75:8b627804927c 411 recvByte = readbuffer[0];
j3 63:422be898443a 412 result = OneWireMaster::Success;
j3 63:422be898443a 413 }
j3 63:422be898443a 414 else
j3 63:422be898443a 415 {
j3 63:422be898443a 416 result = OneWireMaster::CommunicationReadError;
j3 63:422be898443a 417 }
j3 63:422be898443a 418 }
j3 63:422be898443a 419 else
j3 63:422be898443a 420 {
j3 63:422be898443a 421 result = OneWireMaster::CommunicationWriteError;
j3 63:422be898443a 422 }
j3 63:422be898443a 423
j3 63:422be898443a 424 // an error occured so re-sync with DS2480B
IanBenzMaxim 109:5c9180b4be25 425 if (result == OneWireMaster::Success)
j3 63:422be898443a 426 {
IanBenzMaxim 75:8b627804927c 427 result = OWSetLevel(afterLevel);
j3 63:422be898443a 428 }
j3 63:422be898443a 429
j3 17:b646b1e3970b 430 return result;
j3 3:644fc630f958 431 }
j3 3:644fc630f958 432
IanBenzMaxim 75:8b627804927c 433 OneWireMaster::CmdResult DS2480B::OWSetSpeed(OWSpeed newSpeed)
j3 17:b646b1e3970b 434 {
IanBenzMaxim 109:5c9180b4be25 435 OneWireMaster::CmdResult result = OneWireMaster::OperationFailure;
j3 96:9b7b63c0105e 436
j3 96:9b7b63c0105e 437 uint8_t sendpacket[5];
j3 96:9b7b63c0105e 438 uint8_t sendlen = 0;
IanBenzMaxim 74:23be10c32fa3 439
j3 96:9b7b63c0105e 440 // check if change from current mode
IanBenzMaxim 109:5c9180b4be25 441 if (((newSpeed == OneWireMaster::OverdriveSpeed) && (speed != SPEEDSEL_OD)) ||
IanBenzMaxim 109:5c9180b4be25 442 ((newSpeed == OneWireMaster::StandardSpeed) && (speed != SPEEDSEL_STD)))
j3 96:9b7b63c0105e 443 {
j3 96:9b7b63c0105e 444 if (newSpeed == OneWireMaster::OverdriveSpeed)
j3 96:9b7b63c0105e 445 {
IanBenzMaxim 109:5c9180b4be25 446 result = changeBaud(Baud115200bps);
j3 96:9b7b63c0105e 447 if (result == OneWireMaster::Success)
j3 96:9b7b63c0105e 448 {
IanBenzMaxim 109:5c9180b4be25 449 speed = SPEEDSEL_OD;
j3 96:9b7b63c0105e 450 }
j3 96:9b7b63c0105e 451 }
j3 96:9b7b63c0105e 452 else if (newSpeed == OneWireMaster::StandardSpeed)
j3 96:9b7b63c0105e 453 {
IanBenzMaxim 109:5c9180b4be25 454 result = changeBaud(Baud9600bps);
j3 96:9b7b63c0105e 455 if (result == OneWireMaster::Success)
j3 96:9b7b63c0105e 456 {
IanBenzMaxim 109:5c9180b4be25 457 speed = SPEEDSEL_STD;
j3 96:9b7b63c0105e 458 }
j3 96:9b7b63c0105e 459 }
j3 96:9b7b63c0105e 460
j3 96:9b7b63c0105e 461 // if baud rate is set correctly then change DS2480 speed
j3 96:9b7b63c0105e 462 if (result == OneWireMaster::Success)
j3 96:9b7b63c0105e 463 {
j3 96:9b7b63c0105e 464 // check if correct mode
IanBenzMaxim 109:5c9180b4be25 465 if (mode != MODSEL_COMMAND)
j3 96:9b7b63c0105e 466 {
IanBenzMaxim 109:5c9180b4be25 467 mode = MODSEL_COMMAND;
j3 96:9b7b63c0105e 468 sendpacket[sendlen++] = MODE_COMMAND;
j3 96:9b7b63c0105e 469 }
j3 96:9b7b63c0105e 470
j3 96:9b7b63c0105e 471 // proceed to set the DS2480 communication speed
IanBenzMaxim 109:5c9180b4be25 472 sendpacket[sendlen++] = CMD_COMM | FUNCTSEL_SEARCHOFF | speed;
j3 96:9b7b63c0105e 473
j3 96:9b7b63c0105e 474 // send the packet
IanBenzMaxim 109:5c9180b4be25 475 result = writeCom(sendlen,sendpacket);
j3 96:9b7b63c0105e 476 }
j3 96:9b7b63c0105e 477 }
j3 96:9b7b63c0105e 478
j3 17:b646b1e3970b 479 return result;
j3 3:644fc630f958 480 }
j3 3:644fc630f958 481
IanBenzMaxim 75:8b627804927c 482 OneWireMaster::CmdResult DS2480B::OWSetLevel(OWLevel newLevel)
j3 3:644fc630f958 483 {
j3 63:422be898443a 484 OneWireMaster::CmdResult result = OneWireMaster::Success;
j3 63:422be898443a 485
IanBenzMaxim 74:23be10c32fa3 486 uint8_t sendpacket[10], readbuffer[10];
IanBenzMaxim 74:23be10c32fa3 487 uint8_t sendlen = 0;
j3 63:422be898443a 488
j3 63:422be898443a 489 // check if need to change level
IanBenzMaxim 109:5c9180b4be25 490 if (newLevel != level)
j3 63:422be898443a 491 {
j3 63:422be898443a 492 // check for correct mode
IanBenzMaxim 109:5c9180b4be25 493 if (mode != MODSEL_COMMAND)
j3 63:422be898443a 494 {
IanBenzMaxim 109:5c9180b4be25 495 mode = MODSEL_COMMAND;
j3 63:422be898443a 496 sendpacket[sendlen++] = MODE_COMMAND;
j3 63:422be898443a 497 }
IanBenzMaxim 74:23be10c32fa3 498
j3 63:422be898443a 499 // check if just putting back to normal
IanBenzMaxim 75:8b627804927c 500 if (newLevel == OneWireMaster::NormalLevel)
j3 63:422be898443a 501 {
j3 63:422be898443a 502 // stop pulse command
j3 63:422be898443a 503 sendpacket[sendlen++] = MODE_STOP_PULSE;
j3 63:422be898443a 504
j3 63:422be898443a 505 // add the command to begin the pulse WITHOUT prime
j3 63:422be898443a 506 sendpacket[sendlen++] = CMD_COMM | FUNCTSEL_CHMOD | SPEEDSEL_PULSE | BITPOL_5V | PRIME5V_FALSE;
j3 63:422be898443a 507
j3 63:422be898443a 508 // stop pulse command
j3 63:422be898443a 509 sendpacket[sendlen++] = MODE_STOP_PULSE;
IanBenzMaxim 74:23be10c32fa3 510
IanBenzMaxim 109:5c9180b4be25 511 // flush the buffers
IanBenzMaxim 109:5c9180b4be25 512 flushCom();
j3 63:422be898443a 513
j3 63:422be898443a 514 // send the packet
IanBenzMaxim 109:5c9180b4be25 515 result = writeCom(sendlen, sendpacket);
IanBenzMaxim 74:23be10c32fa3 516 if (result == OneWireMaster::Success)
j3 63:422be898443a 517 {
j3 63:422be898443a 518 // read back the 1 byte response
IanBenzMaxim 109:5c9180b4be25 519 result = readCom(2, readbuffer);
IanBenzMaxim 74:23be10c32fa3 520 if (result == OneWireMaster::Success)
j3 63:422be898443a 521 {
j3 63:422be898443a 522 // check response byte
IanBenzMaxim 74:23be10c32fa3 523 if (((readbuffer[0] & 0xE0) == 0xE0) && ((readbuffer[1] & 0xE0) == 0xE0))
j3 63:422be898443a 524 {
IanBenzMaxim 109:5c9180b4be25 525 level = OneWireMaster::NormalLevel;
j3 63:422be898443a 526 }
j3 63:422be898443a 527 else
j3 63:422be898443a 528 {
j3 63:422be898443a 529 result = OneWireMaster::OperationFailure;
j3 63:422be898443a 530 }
j3 63:422be898443a 531 }
j3 63:422be898443a 532 }
j3 63:422be898443a 533 }
j3 63:422be898443a 534 // set new level
IanBenzMaxim 74:23be10c32fa3 535 else
j3 63:422be898443a 536 {
j3 63:422be898443a 537 // set the SPUD time value
j3 63:422be898443a 538 sendpacket[sendlen++] = CMD_CONFIG | PARMSEL_5VPULSE | PARMSET_infinite;
j3 63:422be898443a 539 // add the command to begin the pulse
j3 63:422be898443a 540 sendpacket[sendlen++] = CMD_COMM | FUNCTSEL_CHMOD | SPEEDSEL_PULSE | BITPOL_5V;
IanBenzMaxim 74:23be10c32fa3 541
IanBenzMaxim 109:5c9180b4be25 542 // flush the buffers
IanBenzMaxim 109:5c9180b4be25 543 flushCom();
j3 63:422be898443a 544
j3 63:422be898443a 545 // send the packet
IanBenzMaxim 109:5c9180b4be25 546 result = writeCom(sendlen, sendpacket);
IanBenzMaxim 74:23be10c32fa3 547 if (result == OneWireMaster::Success)
j3 63:422be898443a 548 {
j3 63:422be898443a 549 // read back the 1 byte response from setting time limit
IanBenzMaxim 109:5c9180b4be25 550 result = readCom(1, readbuffer);
IanBenzMaxim 74:23be10c32fa3 551 if (result == OneWireMaster::Success)
j3 63:422be898443a 552 {
j3 63:422be898443a 553 // check response byte
IanBenzMaxim 74:23be10c32fa3 554 if ((readbuffer[0] & 0x81) == 0)
j3 63:422be898443a 555 {
IanBenzMaxim 109:5c9180b4be25 556 level = newLevel;
j3 63:422be898443a 557 }
j3 63:422be898443a 558 else
j3 63:422be898443a 559 {
j3 63:422be898443a 560 result = OneWireMaster::OperationFailure;
j3 63:422be898443a 561 }
j3 63:422be898443a 562 }
j3 63:422be898443a 563 }
j3 63:422be898443a 564 }
j3 63:422be898443a 565 }
j3 63:422be898443a 566
j3 63:422be898443a 567 return result;
j3 63:422be898443a 568 }
j3 63:422be898443a 569
IanBenzMaxim 109:5c9180b4be25 570 OneWireMaster::CmdResult DS2480B::detect()
j3 63:422be898443a 571 {
j3 63:422be898443a 572 OneWireMaster::CmdResult result;
j3 63:422be898443a 573
IanBenzMaxim 74:23be10c32fa3 574 uint8_t sendpacket[10], readbuffer[10];
IanBenzMaxim 74:23be10c32fa3 575 uint8_t sendlen = 0;
j3 63:422be898443a 576
j3 63:422be898443a 577 // reset modes
IanBenzMaxim 109:5c9180b4be25 578 level = OneWireMaster::NormalLevel;
IanBenzMaxim 109:5c9180b4be25 579 mode = MODSEL_COMMAND;
IanBenzMaxim 109:5c9180b4be25 580 baud = Baud9600bps;
IanBenzMaxim 109:5c9180b4be25 581 speed = SPEEDSEL_FLEX;
j3 63:422be898443a 582
j3 63:422be898443a 583 // set the baud rate to 9600
IanBenzMaxim 109:5c9180b4be25 584 setComBaud(baud);
j3 63:422be898443a 585
j3 63:422be898443a 586 // send a break to reset the DS2480B
IanBenzMaxim 109:5c9180b4be25 587 breakCom();
j3 63:422be898443a 588
j3 63:422be898443a 589 // delay to let line settle
j3 63:422be898443a 590 wait_ms(2);
IanBenzMaxim 74:23be10c32fa3 591
IanBenzMaxim 109:5c9180b4be25 592 // flush the buffers
IanBenzMaxim 109:5c9180b4be25 593 flushCom();
j3 63:422be898443a 594
j3 63:422be898443a 595 // send the timing byte
j3 63:422be898443a 596 sendpacket[0] = 0xC1;
IanBenzMaxim 109:5c9180b4be25 597 result = writeCom(1, sendpacket);
IanBenzMaxim 74:23be10c32fa3 598 if (result == OneWireMaster::Success)
j3 63:422be898443a 599 {
j3 63:422be898443a 600 // delay to let line settle
j3 63:422be898443a 601 wait_ms(2);
j3 63:422be898443a 602
j3 63:422be898443a 603 // set the FLEX configuration parameters
j3 63:422be898443a 604 // default PDSRC = 1.37Vus
j3 63:422be898443a 605 sendpacket[sendlen++] = CMD_CONFIG | PARMSEL_SLEW | PARMSET_Slew1p37Vus;
j3 63:422be898443a 606 // default W1LT = 10us
j3 63:422be898443a 607 sendpacket[sendlen++] = CMD_CONFIG | PARMSEL_WRITE1LOW | PARMSET_Write10us;
j3 63:422be898443a 608 // default DSO/WORT = 8us
j3 63:422be898443a 609 sendpacket[sendlen++] = CMD_CONFIG | PARMSEL_SAMPLEOFFSET | PARMSET_SampOff8us;
j3 63:422be898443a 610
j3 63:422be898443a 611 // construct the command to read the baud rate (to test command block)
j3 63:422be898443a 612 sendpacket[sendlen++] = CMD_CONFIG | PARMSEL_PARMREAD | (PARMSEL_BAUDRATE >> 3);
j3 63:422be898443a 613
j3 63:422be898443a 614 // also do 1 bit operation (to test 1-Wire block)
IanBenzMaxim 109:5c9180b4be25 615 sendpacket[sendlen++] = CMD_COMM | FUNCTSEL_BIT | baud | BITPOL_ONE;
IanBenzMaxim 74:23be10c32fa3 616
IanBenzMaxim 109:5c9180b4be25 617 // flush the buffers
IanBenzMaxim 109:5c9180b4be25 618 flushCom();
j3 63:422be898443a 619
j3 63:422be898443a 620 // send the packet
IanBenzMaxim 109:5c9180b4be25 621 result = writeCom(sendlen, sendpacket);
IanBenzMaxim 74:23be10c32fa3 622 if (result == OneWireMaster::Success)
j3 63:422be898443a 623 {
j3 63:422be898443a 624 // read back the response
IanBenzMaxim 109:5c9180b4be25 625 result = readCom(5, readbuffer);
IanBenzMaxim 74:23be10c32fa3 626 if (result == OneWireMaster::Success)
j3 63:422be898443a 627 {
j3 63:422be898443a 628 // look at the baud rate and bit operation
j3 63:422be898443a 629 // to see if the response makes sense
IanBenzMaxim 109:5c9180b4be25 630 if (((readbuffer[3] & 0xF1) == 0x00) && ((readbuffer[3] & 0x0E) == baud) && ((readbuffer[4] & 0xF0) == 0x90) && ((readbuffer[4] & 0x0C) == baud))
j3 63:422be898443a 631 {
j3 63:422be898443a 632 result = OneWireMaster::Success;
j3 63:422be898443a 633 }
j3 63:422be898443a 634 else
j3 63:422be898443a 635 {
j3 63:422be898443a 636 result = OneWireMaster::OperationFailure;
j3 63:422be898443a 637 }
j3 63:422be898443a 638 }
j3 63:422be898443a 639 }
j3 63:422be898443a 640 }
IanBenzMaxim 74:23be10c32fa3 641
j3 63:422be898443a 642 return result;
j3 63:422be898443a 643 }
j3 63:422be898443a 644
IanBenzMaxim 109:5c9180b4be25 645 OneWireMaster::CmdResult DS2480B::changeBaud(BaudRate newBaud)
j3 63:422be898443a 646 {
j3 65:a28ac52ca127 647 OneWireMaster::CmdResult result = OneWireMaster::Success;
j3 65:a28ac52ca127 648
IanBenzMaxim 74:23be10c32fa3 649 uint8_t readbuffer[5], sendpacket[5], sendpacket2[5];
IanBenzMaxim 74:23be10c32fa3 650 uint8_t sendlen = 0, sendlen2 = 0;
j3 65:a28ac52ca127 651
j3 65:a28ac52ca127 652 //see if diffenent then current baud rate
IanBenzMaxim 109:5c9180b4be25 653 if (baud != newBaud)
j3 65:a28ac52ca127 654 {
j3 65:a28ac52ca127 655 // build the command packet
j3 65:a28ac52ca127 656 // check for correct mode
IanBenzMaxim 109:5c9180b4be25 657 if (mode != MODSEL_COMMAND)
j3 65:a28ac52ca127 658 {
IanBenzMaxim 109:5c9180b4be25 659 mode = MODSEL_COMMAND;
j3 65:a28ac52ca127 660 sendpacket[sendlen++] = MODE_COMMAND;
j3 65:a28ac52ca127 661 }
j3 65:a28ac52ca127 662 // build the command
IanBenzMaxim 75:8b627804927c 663 sendpacket[sendlen++] = CMD_CONFIG | PARMSEL_BAUDRATE | newBaud;
j3 63:422be898443a 664
j3 65:a28ac52ca127 665 // flush the buffers
IanBenzMaxim 109:5c9180b4be25 666 flushCom();
j3 65:a28ac52ca127 667
j3 65:a28ac52ca127 668 // send the packet
IanBenzMaxim 109:5c9180b4be25 669 result = writeCom(sendlen, sendpacket);
IanBenzMaxim 74:23be10c32fa3 670 if (result == OneWireMaster::Success)
j3 65:a28ac52ca127 671 {
j3 65:a28ac52ca127 672 // make sure buffer is flushed
j3 65:a28ac52ca127 673 wait_ms(5);
j3 63:422be898443a 674
j3 65:a28ac52ca127 675 // change our baud rate
IanBenzMaxim 109:5c9180b4be25 676 setComBaud(newBaud);
IanBenzMaxim 109:5c9180b4be25 677 baud = newBaud;
j3 65:a28ac52ca127 678
j3 65:a28ac52ca127 679 // wait for things to settle
j3 65:a28ac52ca127 680 wait_ms(5);
j3 65:a28ac52ca127 681
j3 65:a28ac52ca127 682 // build a command packet to read back baud rate
j3 65:a28ac52ca127 683 sendpacket2[sendlen2++] = CMD_CONFIG | PARMSEL_PARMREAD | (PARMSEL_BAUDRATE >> 3);
j3 65:a28ac52ca127 684
j3 65:a28ac52ca127 685 // flush the buffers
IanBenzMaxim 109:5c9180b4be25 686 flushCom();
j3 63:422be898443a 687
j3 65:a28ac52ca127 688 // send the packet
IanBenzMaxim 109:5c9180b4be25 689 result = writeCom(sendlen2, sendpacket2);
IanBenzMaxim 74:23be10c32fa3 690 if (result == OneWireMaster::Success)
j3 63:422be898443a 691 {
j3 65:a28ac52ca127 692 // read back the 1 byte response
IanBenzMaxim 109:5c9180b4be25 693 result = readCom(1, readbuffer);
IanBenzMaxim 74:23be10c32fa3 694 if (result == OneWireMaster::Success)
j3 65:a28ac52ca127 695 {
j3 65:a28ac52ca127 696 // verify correct baud
IanBenzMaxim 74:23be10c32fa3 697 if ((readbuffer[0] & 0x0E) == (sendpacket[sendlen - 1] & 0x0E))
j3 65:a28ac52ca127 698 {
j3 65:a28ac52ca127 699 result = OneWireMaster::Success;
j3 65:a28ac52ca127 700 }
j3 65:a28ac52ca127 701 else
j3 65:a28ac52ca127 702 {
IanBenzMaxim 74:23be10c32fa3 703 result = OneWireMaster::OperationFailure;
j3 65:a28ac52ca127 704 }
j3 65:a28ac52ca127 705 }
j3 65:a28ac52ca127 706 else
j3 65:a28ac52ca127 707 {
j3 65:a28ac52ca127 708 result = OneWireMaster::CommunicationReadError;
j3 65:a28ac52ca127 709 }
j3 63:422be898443a 710 }
j3 65:a28ac52ca127 711 else
j3 65:a28ac52ca127 712 {
j3 65:a28ac52ca127 713 result = OneWireMaster::CommunicationWriteError;
j3 65:a28ac52ca127 714 }
j3 65:a28ac52ca127 715 }
j3 65:a28ac52ca127 716 else
j3 65:a28ac52ca127 717 {
j3 65:a28ac52ca127 718 result = OneWireMaster::CommunicationWriteError;
j3 65:a28ac52ca127 719 }
j3 65:a28ac52ca127 720 }
IanBenzMaxim 74:23be10c32fa3 721
IanBenzMaxim 109:5c9180b4be25 722 return result;
IanBenzMaxim 109:5c9180b4be25 723 }
IanBenzMaxim 109:5c9180b4be25 724
IanBenzMaxim 109:5c9180b4be25 725 OneWireMaster::CmdResult DS2480B::writeCom(size_t outlen, uint8_t *outbuf)
IanBenzMaxim 109:5c9180b4be25 726 {
IanBenzMaxim 109:5c9180b4be25 727 OneWireMaster::CmdResult result = OneWireMaster::OperationFailure;
IanBenzMaxim 109:5c9180b4be25 728
IanBenzMaxim 109:5c9180b4be25 729 mbed::Timer timer;
IanBenzMaxim 109:5c9180b4be25 730 uint32_t timeout = calculateBitTimeout(baud) * outlen;
IanBenzMaxim 109:5c9180b4be25 731 uint8_t idx = 0;
IanBenzMaxim 109:5c9180b4be25 732
IanBenzMaxim 109:5c9180b4be25 733 timer.start();
IanBenzMaxim 109:5c9180b4be25 734 do
j3 65:a28ac52ca127 735 {
IanBenzMaxim 109:5c9180b4be25 736 while ((idx < outlen) && serial.writeable())
IanBenzMaxim 109:5c9180b4be25 737 {
IanBenzMaxim 109:5c9180b4be25 738 serial.putc(outbuf[idx++]);
IanBenzMaxim 109:5c9180b4be25 739 }
IanBenzMaxim 109:5c9180b4be25 740
IanBenzMaxim 109:5c9180b4be25 741 } while ((idx < outlen) && (timer.read_us() < timeout));
IanBenzMaxim 109:5c9180b4be25 742
IanBenzMaxim 109:5c9180b4be25 743 if (idx == outlen)
IanBenzMaxim 109:5c9180b4be25 744 {
IanBenzMaxim 109:5c9180b4be25 745 result = OneWireMaster::Success;
IanBenzMaxim 109:5c9180b4be25 746 }
IanBenzMaxim 109:5c9180b4be25 747 else
IanBenzMaxim 109:5c9180b4be25 748 {
IanBenzMaxim 109:5c9180b4be25 749 result = OneWireMaster::TimeoutError;
j3 65:a28ac52ca127 750 }
IanBenzMaxim 74:23be10c32fa3 751
j3 65:a28ac52ca127 752 return result;
j3 63:422be898443a 753 }
j3 63:422be898443a 754
IanBenzMaxim 109:5c9180b4be25 755 OneWireMaster::CmdResult DS2480B::readCom(size_t inlen, uint8_t *inbuf)
j3 63:422be898443a 756 {
IanBenzMaxim 109:5c9180b4be25 757 OneWireMaster::CmdResult result;
IanBenzMaxim 109:5c9180b4be25 758 mbed::Timer timer;
IanBenzMaxim 109:5c9180b4be25 759 uint32_t num_bytes_read = 0;
IanBenzMaxim 109:5c9180b4be25 760 uint32_t timeout = calculateBitTimeout(baud) * inlen;
IanBenzMaxim 74:23be10c32fa3 761
IanBenzMaxim 109:5c9180b4be25 762 timer.start();
j3 63:422be898443a 763 do
j3 63:422be898443a 764 {
IanBenzMaxim 109:5c9180b4be25 765 while ((num_bytes_read < inlen) && serial.readable())
j3 63:422be898443a 766 {
IanBenzMaxim 109:5c9180b4be25 767 inbuf[num_bytes_read++] = serial.getc();
j3 63:422be898443a 768 }
IanBenzMaxim 109:5c9180b4be25 769 } while ((num_bytes_read < inlen) && (timer.read_us() < timeout));
IanBenzMaxim 109:5c9180b4be25 770 timer.stop();
IanBenzMaxim 74:23be10c32fa3 771
IanBenzMaxim 109:5c9180b4be25 772 if (num_bytes_read == inlen)
j3 63:422be898443a 773 {
IanBenzMaxim 109:5c9180b4be25 774 result = OneWireMaster::Success;
j3 63:422be898443a 775 }
j3 63:422be898443a 776 else
j3 63:422be898443a 777 {
IanBenzMaxim 109:5c9180b4be25 778 result = OneWireMaster::TimeoutError;
j3 63:422be898443a 779 }
IanBenzMaxim 74:23be10c32fa3 780
j3 63:422be898443a 781 return result;
j3 63:422be898443a 782 }
j3 63:422be898443a 783
IanBenzMaxim 109:5c9180b4be25 784 void DS2480B::breakCom()
j3 63:422be898443a 785 {
IanBenzMaxim 109:5c9180b4be25 786 // Switch to lower baud rate to ensure break is longer than 2 ms.
IanBenzMaxim 109:5c9180b4be25 787 serial.baud(4800);
IanBenzMaxim 109:5c9180b4be25 788 serial.send_break();
IanBenzMaxim 109:5c9180b4be25 789 setComBaud(baud);
j3 63:422be898443a 790 }
j3 63:422be898443a 791
IanBenzMaxim 109:5c9180b4be25 792 void DS2480B::flushCom()
j3 63:422be898443a 793 {
IanBenzMaxim 109:5c9180b4be25 794 // Clear receive buffer.
IanBenzMaxim 109:5c9180b4be25 795 while (serial.readable())
j3 63:422be898443a 796 {
IanBenzMaxim 109:5c9180b4be25 797 serial.getc();
j3 63:422be898443a 798 }
IanBenzMaxim 74:23be10c32fa3 799
IanBenzMaxim 109:5c9180b4be25 800 /* No apparent way to clear transmit buffer.
IanBenzMaxim 109:5c9180b4be25 801 * From the example in AN192 (http://pdfserv.maximintegrated.com/en/an/AN192.pdf),
IanBenzMaxim 109:5c9180b4be25 802 * the data shouldn't be sent, just aborted and the buffer cleaned out.
IanBenzMaxim 109:5c9180b4be25 803 * Below is what was used in AN192 example code using windows drivers:
IanBenzMaxim 109:5c9180b4be25 804 * PurgeComm(ComID, PURGE_TXABORT | PURGE_RXABORT | PURGE_TXCLEAR | PURGE_RXCLEAR );
IanBenzMaxim 109:5c9180b4be25 805 */
j3 63:422be898443a 806 }
j3 63:422be898443a 807
IanBenzMaxim 109:5c9180b4be25 808 void DS2480B::setComBaud(BaudRate new_baud)
j3 63:422be898443a 809 {
IanBenzMaxim 74:23be10c32fa3 810 switch (new_baud)
j3 63:422be898443a 811 {
IanBenzMaxim 109:5c9180b4be25 812 case Baud115200bps:
IanBenzMaxim 109:5c9180b4be25 813 serial.baud(115200);
IanBenzMaxim 74:23be10c32fa3 814 break;
IanBenzMaxim 74:23be10c32fa3 815
IanBenzMaxim 109:5c9180b4be25 816 case Baud57600bps:
IanBenzMaxim 109:5c9180b4be25 817 serial.baud(57600);
IanBenzMaxim 74:23be10c32fa3 818 break;
IanBenzMaxim 74:23be10c32fa3 819
IanBenzMaxim 109:5c9180b4be25 820 case Baud19200bps:
IanBenzMaxim 109:5c9180b4be25 821 serial.baud(19200);
IanBenzMaxim 74:23be10c32fa3 822 break;
IanBenzMaxim 74:23be10c32fa3 823
IanBenzMaxim 109:5c9180b4be25 824 case Baud9600bps:
IanBenzMaxim 74:23be10c32fa3 825 default:
IanBenzMaxim 109:5c9180b4be25 826 serial.baud(9600);
IanBenzMaxim 74:23be10c32fa3 827 break;
j3 63:422be898443a 828 }
j3 63:422be898443a 829 }