Implementation of 1-Wire with added Alarm Search Functionality

Dependents:   Max32630_One_Wire_Interface

Committer:
j3
Date:
Tue Aug 02 18:21:09 2016 +0000
Revision:
104:3f48daed532b
Parent:
96:9b7b63c0105e
Child:
109:5c9180b4be25
removed "OneWire" from absolute paths

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 #define WRITE_FUNCTION 0
j3 63:422be898443a 39 #define READ_FUNCTION 1
j3 63:422be898443a 40
j3 63:422be898443a 41 // Mode Commands
j3 63:422be898443a 42 #define MODE_DATA 0xE1
j3 63:422be898443a 43 #define MODE_COMMAND 0xE3
j3 63:422be898443a 44 #define MODE_STOP_PULSE 0xF1
j3 63:422be898443a 45
j3 63:422be898443a 46 // Return byte value
j3 63:422be898443a 47 #define RB_CHIPID_MASK 0x1C
j3 63:422be898443a 48 #define RB_RESET_MASK 0x03
j3 63:422be898443a 49 #define RB_1WIRESHORT 0x00
j3 63:422be898443a 50 #define RB_PRESENCE 0x01
j3 63:422be898443a 51 #define RB_ALARMPRESENCE 0x02
j3 63:422be898443a 52 #define RB_NOPRESENCE 0x03
j3 63:422be898443a 53
j3 63:422be898443a 54 #define RB_BIT_MASK 0x03
j3 63:422be898443a 55 #define RB_BIT_ONE 0x03
j3 63:422be898443a 56 #define RB_BIT_ZERO 0x00
j3 63:422be898443a 57
j3 63:422be898443a 58 // Masks for all bit ranges
j3 63:422be898443a 59 #define CMD_MASK 0x80
j3 63:422be898443a 60 #define FUNCTSEL_MASK 0x60
j3 63:422be898443a 61 #define BITPOL_MASK 0x10
j3 63:422be898443a 62 #define SPEEDSEL_MASK 0x0C
j3 63:422be898443a 63 #define MODSEL_MASK 0x02
j3 63:422be898443a 64 #define PARMSEL_MASK 0x70
j3 63:422be898443a 65 #define PARMSET_MASK 0x0E
j3 63:422be898443a 66
j3 63:422be898443a 67 // Command or config bit
j3 63:422be898443a 68 #define CMD_COMM 0x81
j3 63:422be898443a 69 #define CMD_CONFIG 0x01
j3 63:422be898443a 70
j3 63:422be898443a 71 // Function select bits
j3 63:422be898443a 72 #define FUNCTSEL_BIT 0x00
j3 63:422be898443a 73 #define FUNCTSEL_SEARCHON 0x30
j3 63:422be898443a 74 #define FUNCTSEL_SEARCHOFF 0x20
j3 63:422be898443a 75 #define FUNCTSEL_RESET 0x40
j3 63:422be898443a 76 #define FUNCTSEL_CHMOD 0x60
j3 63:422be898443a 77
j3 63:422be898443a 78 // Bit polarity/Pulse voltage bits
j3 63:422be898443a 79 #define BITPOL_ONE 0x10
j3 63:422be898443a 80 #define BITPOL_ZERO 0x00
j3 63:422be898443a 81 #define BITPOL_5V 0x00
j3 63:422be898443a 82 #define BITPOL_12V 0x10
j3 63:422be898443a 83
j3 63:422be898443a 84 // One Wire speed bits
j3 63:422be898443a 85 #define SPEEDSEL_STD 0x00
j3 63:422be898443a 86 #define SPEEDSEL_FLEX 0x04
j3 63:422be898443a 87 #define SPEEDSEL_OD 0x08
j3 63:422be898443a 88 #define SPEEDSEL_PULSE 0x0C
j3 63:422be898443a 89
j3 63:422be898443a 90 // Data/Command mode select bits
j3 63:422be898443a 91 #define MODSEL_DATA 0x00
j3 63:422be898443a 92 #define MODSEL_COMMAND 0x02
j3 63:422be898443a 93
j3 63:422be898443a 94 // 5V Follow Pulse select bits
j3 63:422be898443a 95 #define PRIME5V_TRUE 0x02
j3 63:422be898443a 96 #define PRIME5V_FALSE 0x00
j3 63:422be898443a 97
j3 63:422be898443a 98 // Parameter select bits
j3 63:422be898443a 99 #define PARMSEL_PARMREAD 0x00
j3 63:422be898443a 100 #define PARMSEL_SLEW 0x10
j3 63:422be898443a 101 #define PARMSEL_12VPULSE 0x20
j3 63:422be898443a 102 #define PARMSEL_5VPULSE 0x30
j3 63:422be898443a 103 #define PARMSEL_WRITE1LOW 0x40
j3 63:422be898443a 104 #define PARMSEL_SAMPLEOFFSET 0x50
j3 63:422be898443a 105 #define PARMSEL_ACTIVEPULLUPTIME 0x60
j3 63:422be898443a 106 #define PARMSEL_BAUDRATE 0x70
j3 63:422be898443a 107
j3 63:422be898443a 108 // Pull down slew rate.
j3 63:422be898443a 109 #define PARMSET_Slew15Vus 0x00
j3 63:422be898443a 110 #define PARMSET_Slew2p2Vus 0x02
j3 63:422be898443a 111 #define PARMSET_Slew1p65Vus 0x04
j3 63:422be898443a 112 #define PARMSET_Slew1p37Vus 0x06
j3 63:422be898443a 113 #define PARMSET_Slew1p1Vus 0x08
j3 63:422be898443a 114 #define PARMSET_Slew0p83Vus 0x0A
j3 63:422be898443a 115 #define PARMSET_Slew0p7Vus 0x0C
j3 63:422be898443a 116 #define PARMSET_Slew0p55Vus 0x0E
j3 63:422be898443a 117
j3 63:422be898443a 118 // 12V programming pulse time table
j3 63:422be898443a 119 #define PARMSET_32us 0x00
j3 63:422be898443a 120 #define PARMSET_64us 0x02
j3 63:422be898443a 121 #define PARMSET_128us 0x04
j3 63:422be898443a 122 #define PARMSET_256us 0x06
j3 63:422be898443a 123 #define PARMSET_512us 0x08
j3 63:422be898443a 124 #define PARMSET_1024us 0x0A
j3 63:422be898443a 125 #define PARMSET_2048us 0x0C
j3 63:422be898443a 126 #define PARMSET_infinite 0x0E
j3 63:422be898443a 127
j3 63:422be898443a 128 // 5V strong pull up pulse time table
j3 63:422be898443a 129 #define PARMSET_16p4ms 0x00
j3 63:422be898443a 130 #define PARMSET_65p5ms 0x02
j3 63:422be898443a 131 #define PARMSET_131ms 0x04
j3 63:422be898443a 132 #define PARMSET_262ms 0x06
j3 63:422be898443a 133 #define PARMSET_524ms 0x08
j3 63:422be898443a 134 #define PARMSET_1p05s 0x0A
j3 63:422be898443a 135 #define PARMSET_dynamic 0x0C
j3 63:422be898443a 136 #define PARMSET_infinite 0x0E
j3 63:422be898443a 137
j3 63:422be898443a 138 // Write 1 low time
j3 63:422be898443a 139 #define PARMSET_Write8us 0x00
j3 63:422be898443a 140 #define PARMSET_Write9us 0x02
j3 63:422be898443a 141 #define PARMSET_Write10us 0x04
j3 63:422be898443a 142 #define PARMSET_Write11us 0x06
j3 63:422be898443a 143 #define PARMSET_Write12us 0x08
j3 63:422be898443a 144 #define PARMSET_Write13us 0x0A
j3 63:422be898443a 145 #define PARMSET_Write14us 0x0C
j3 63:422be898443a 146 #define PARMSET_Write15us 0x0E
j3 63:422be898443a 147
j3 63:422be898443a 148 // Data sample offset and Write 0 recovery time
j3 63:422be898443a 149 #define PARMSET_SampOff3us 0x00
j3 63:422be898443a 150 #define PARMSET_SampOff4us 0x02
j3 63:422be898443a 151 #define PARMSET_SampOff5us 0x04
j3 63:422be898443a 152 #define PARMSET_SampOff6us 0x06
j3 63:422be898443a 153 #define PARMSET_SampOff7us 0x08
j3 63:422be898443a 154 #define PARMSET_SampOff8us 0x0A
j3 63:422be898443a 155 #define PARMSET_SampOff9us 0x0C
j3 63:422be898443a 156 #define PARMSET_SampOff10us 0x0E
j3 63:422be898443a 157
j3 63:422be898443a 158 // Active pull up on time
j3 63:422be898443a 159 #define PARMSET_PullUp0p0us 0x00
j3 63:422be898443a 160 #define PARMSET_PullUp0p5us 0x02
j3 63:422be898443a 161 #define PARMSET_PullUp1p0us 0x04
j3 63:422be898443a 162 #define PARMSET_PullUp1p5us 0x06
j3 63:422be898443a 163 #define PARMSET_PullUp2p0us 0x08
j3 63:422be898443a 164 #define PARMSET_PullUp2p5us 0x0A
j3 63:422be898443a 165 #define PARMSET_PullUp3p0us 0x0C
j3 63:422be898443a 166 #define PARMSET_PullUp3p5us 0x0E
j3 63:422be898443a 167
j3 63:422be898443a 168 // Baud rate bits
j3 63:422be898443a 169 #define PARMSET_9600 0x00
j3 63:422be898443a 170 #define PARMSET_19200 0x02
j3 63:422be898443a 171 #define PARMSET_57600 0x04
j3 63:422be898443a 172 #define PARMSET_115200 0x06
j3 63:422be898443a 173
j3 63:422be898443a 174 // DS2480B program voltage available
j3 63:422be898443a 175 #define DS2480BPROG_MASK 0x20
j3 63:422be898443a 176
j3 63:422be898443a 177 // mode bit flags
j3 63:422be898443a 178 #define MODE_NORMAL 0x00
j3 63:422be898443a 179 #define MODE_OVERDRIVE 0x01
j3 63:422be898443a 180 #define MODE_STRONG5 0x02
j3 63:422be898443a 181 #define MODE_PROGRAM 0x04
j3 63:422be898443a 182 #define MODE_BREAK 0x08
j3 63:422be898443a 183
j3 63:422be898443a 184 #define MAX_BAUD PARMSET_115200
j3 63:422be898443a 185
IanBenzMaxim 76:84e6c4994e29 186 using OneWire::DS2480B;
IanBenzMaxim 76:84e6c4994e29 187 using OneWire::OneWireMaster;
j3 3:644fc630f958 188
j3 3:644fc630f958 189 //*********************************************************************
IanBenzMaxim 73:2cecc1372acc 190 DS2480B::DS2480B(mbed::Serial &p_serial)
IanBenzMaxim 74:23be10c32fa3 191 :_p_serial(&p_serial), _serial_owner(false)
j3 3:644fc630f958 192 {
j3 3:644fc630f958 193 }
j3 3:644fc630f958 194
j3 3:644fc630f958 195
j3 3:644fc630f958 196 //*********************************************************************
IanBenzMaxim 73:2cecc1372acc 197 DS2480B::~DS2480B()
j3 3:644fc630f958 198 {
IanBenzMaxim 74:23be10c32fa3 199 if (_serial_owner)
j3 3:644fc630f958 200 {
j3 3:644fc630f958 201 delete _p_serial;
j3 3:644fc630f958 202 }
j3 3:644fc630f958 203 }
j3 3:644fc630f958 204
j3 3:644fc630f958 205
j3 3:644fc630f958 206 //*********************************************************************
IanBenzMaxim 73:2cecc1372acc 207 void DS2480B::rx_callback(void)
j3 63:422be898443a 208 {
IanBenzMaxim 74:23be10c32fa3 209 while (_p_serial->readable())
j3 65:a28ac52ca127 210 {
j3 65:a28ac52ca127 211 rx_buffer.buff[rx_buffer.w_idx++] = _p_serial->getc();
j3 65:a28ac52ca127 212 rx_buffer.rx_bytes_available++;
j3 65:a28ac52ca127 213 }
IanBenzMaxim 74:23be10c32fa3 214
IanBenzMaxim 74:23be10c32fa3 215 if (rx_buffer.w_idx == rx_buffer.r_idx)
j3 63:422be898443a 216 {
j3 63:422be898443a 217 rx_buffer.wrap_error = true;
j3 63:422be898443a 218 }
j3 63:422be898443a 219 }
j3 63:422be898443a 220
j3 63:422be898443a 221
j3 63:422be898443a 222 //*********************************************************************
IanBenzMaxim 75:8b627804927c 223 OneWireMaster::CmdResult DS2480B::OWInitMaster()
j3 14:7b2886a50321 224 {
IanBenzMaxim 73:2cecc1372acc 225 _p_serial->attach(this, &DS2480B::rx_callback, mbed::Serial::RxIrq);
IanBenzMaxim 74:23be10c32fa3 226
j3 63:422be898443a 227 rx_buffer.w_idx = 0;
j3 63:422be898443a 228 rx_buffer.r_idx = 0;
j3 65:a28ac52ca127 229 rx_buffer.rx_bytes_available = 0;
j3 63:422be898443a 230 rx_buffer.wrap_error = false;
IanBenzMaxim 74:23be10c32fa3 231
IanBenzMaxim 75:8b627804927c 232 _ULevel = OneWireMaster::NormalLevel;
IanBenzMaxim 75:8b627804927c 233 _UBaud = Bps9600;
j3 63:422be898443a 234 _UMode = MODSEL_COMMAND;
IanBenzMaxim 75:8b627804927c 235 _USpeed = OneWireMaster::StandardSpeed;
IanBenzMaxim 74:23be10c32fa3 236
j3 63:422be898443a 237 return DS2480B_Detect();
j3 14:7b2886a50321 238 }
j3 14:7b2886a50321 239
j3 14:7b2886a50321 240
j3 14:7b2886a50321 241 //*********************************************************************
IanBenzMaxim 75:8b627804927c 242 OneWireMaster::CmdResult DS2480B::OWReset()
j3 3:644fc630f958 243 {
j3 63:422be898443a 244 OneWireMaster::CmdResult result;
j3 63:422be898443a 245
IanBenzMaxim 74:23be10c32fa3 246 uint8_t readbuffer[10], sendpacket[10];
IanBenzMaxim 74:23be10c32fa3 247 uint8_t sendlen = 0;
j3 63:422be898443a 248
j3 63:422be898443a 249 // make sure normal level
IanBenzMaxim 75:8b627804927c 250 result = OWSetLevel(OneWireMaster::NormalLevel);
IanBenzMaxim 74:23be10c32fa3 251 if (result == OneWireMaster::Success)
j3 63:422be898443a 252 {
j3 63:422be898443a 253 // check for correct mode
IanBenzMaxim 74:23be10c32fa3 254 if (_UMode != MODSEL_COMMAND)
j3 63:422be898443a 255 {
j3 63:422be898443a 256 _UMode = MODSEL_COMMAND;
j3 63:422be898443a 257 sendpacket[sendlen++] = MODE_COMMAND;
j3 63:422be898443a 258 }
j3 63:422be898443a 259
j3 63:422be898443a 260 // construct the command
j3 63:422be898443a 261 sendpacket[sendlen++] = (uint8_t)(CMD_COMM | FUNCTSEL_RESET | _USpeed);
IanBenzMaxim 74:23be10c32fa3 262
j3 63:422be898443a 263 FlushCOM();
j3 63:422be898443a 264
j3 63:422be898443a 265 // send the packet
IanBenzMaxim 74:23be10c32fa3 266 result = WriteCOM(sendlen, sendpacket);
IanBenzMaxim 74:23be10c32fa3 267 if (result == OneWireMaster::Success)
j3 63:422be898443a 268 {
j3 63:422be898443a 269 // read back the 1 byte response
IanBenzMaxim 74:23be10c32fa3 270 result = ReadCOM(1, readbuffer);
IanBenzMaxim 74:23be10c32fa3 271 if (result == OneWireMaster::Success)
j3 63:422be898443a 272 {
j3 63:422be898443a 273 // make sure this byte looks like a reset byte
IanBenzMaxim 74:23be10c32fa3 274 if (((readbuffer[0] & RB_RESET_MASK) == RB_PRESENCE) || ((readbuffer[0] & RB_RESET_MASK) == RB_ALARMPRESENCE))
j3 63:422be898443a 275 {
j3 63:422be898443a 276 result = OneWireMaster::Success;
j3 63:422be898443a 277 }
j3 63:422be898443a 278 else
j3 63:422be898443a 279 {
j3 63:422be898443a 280 result = OneWireMaster::OperationFailure;
j3 63:422be898443a 281 }
j3 63:422be898443a 282 }
j3 63:422be898443a 283 }
IanBenzMaxim 74:23be10c32fa3 284
IanBenzMaxim 74:23be10c32fa3 285 if (result != OneWireMaster::Success)
j3 63:422be898443a 286 {
j3 63:422be898443a 287 // an error occured so re-sync with DS2480B
j3 63:422be898443a 288 DS2480B_Detect();
j3 63:422be898443a 289 }
j3 63:422be898443a 290 }
j3 63:422be898443a 291
j3 17:b646b1e3970b 292 return result;
j3 3:644fc630f958 293 }
j3 3:644fc630f958 294
j3 3:644fc630f958 295
j3 63:422be898443a 296 //*********************************************************************
IanBenzMaxim 75:8b627804927c 297 OneWireMaster::CmdResult DS2480B::OWTouchBitSetLevel(uint8_t & sendRecvBit, OWLevel afterLevel)
j3 3:644fc630f958 298 {
j3 23:e8e403d61359 299 OneWireMaster::CmdResult result = OneWireMaster::OperationFailure;
j3 63:422be898443a 300
IanBenzMaxim 74:23be10c32fa3 301 uint8_t readbuffer[10], sendpacket[10];
IanBenzMaxim 74:23be10c32fa3 302 uint8_t sendlen = 0;
j3 63:422be898443a 303
j3 63:422be898443a 304 // make sure normal level
IanBenzMaxim 75:8b627804927c 305 OWSetLevel(OneWireMaster::NormalLevel);
j3 63:422be898443a 306
j3 63:422be898443a 307 // check for correct mode
IanBenzMaxim 74:23be10c32fa3 308 if (_UMode != MODSEL_COMMAND)
j3 63:422be898443a 309 {
j3 63:422be898443a 310 _UMode = MODSEL_COMMAND;
j3 63:422be898443a 311 sendpacket[sendlen++] = MODE_COMMAND;
j3 63:422be898443a 312 }
j3 63:422be898443a 313
j3 63:422be898443a 314 // construct the command
IanBenzMaxim 75:8b627804927c 315 sendpacket[sendlen] = (sendRecvBit != 0) ? BITPOL_ONE : BITPOL_ZERO;
j3 63:422be898443a 316 sendpacket[sendlen++] |= CMD_COMM | FUNCTSEL_BIT | _USpeed;
j3 63:422be898443a 317
j3 63:422be898443a 318 // flush the buffers
j3 63:422be898443a 319 FlushCOM();
j3 63:422be898443a 320
j3 63:422be898443a 321 // send the packet
IanBenzMaxim 74:23be10c32fa3 322 result = WriteCOM(sendlen, sendpacket);
IanBenzMaxim 74:23be10c32fa3 323 if (result == OneWireMaster::Success)
j3 63:422be898443a 324 {
j3 63:422be898443a 325 // read back the response
IanBenzMaxim 74:23be10c32fa3 326 result = ReadCOM(1, readbuffer);
IanBenzMaxim 74:23be10c32fa3 327 if (result == OneWireMaster::Success)
j3 63:422be898443a 328 {
j3 63:422be898443a 329 // interpret the response
j3 63:422be898443a 330 if (((readbuffer[0] & 0xE0) == 0x80) && ((readbuffer[0] & RB_BIT_MASK) == RB_BIT_ONE))
j3 63:422be898443a 331 {
IanBenzMaxim 75:8b627804927c 332 sendRecvBit = 1;
j3 63:422be898443a 333 result = OneWireMaster::Success;
j3 63:422be898443a 334 }
j3 63:422be898443a 335 else
j3 63:422be898443a 336 {
IanBenzMaxim 75:8b627804927c 337 sendRecvBit = 0;
j3 63:422be898443a 338 result = OneWireMaster::Success;
j3 63:422be898443a 339 }
j3 63:422be898443a 340 }
j3 63:422be898443a 341 else
j3 63:422be898443a 342 {
j3 63:422be898443a 343 result = OneWireMaster::CommunicationReadError;
j3 63:422be898443a 344 }
j3 63:422be898443a 345 }
j3 63:422be898443a 346 else
j3 63:422be898443a 347 {
j3 63:422be898443a 348 result = OneWireMaster::CommunicationWriteError;
j3 63:422be898443a 349 }
j3 63:422be898443a 350
j3 63:422be898443a 351 // an error occured so re-sync with DS2480B
IanBenzMaxim 74:23be10c32fa3 352 if (result != OneWireMaster::Success)
j3 63:422be898443a 353 {
j3 63:422be898443a 354 DS2480B_Detect();
j3 63:422be898443a 355 }
j3 63:422be898443a 356 else
j3 63:422be898443a 357 {
IanBenzMaxim 75:8b627804927c 358 result = OWSetLevel(afterLevel);
j3 63:422be898443a 359 }
j3 63:422be898443a 360
j3 17:b646b1e3970b 361 return result;
j3 3:644fc630f958 362 }
j3 3:644fc630f958 363
j3 3:644fc630f958 364
j3 3:644fc630f958 365 //*********************************************************************
IanBenzMaxim 75:8b627804927c 366 OneWireMaster::CmdResult DS2480B::OWWriteByteSetLevel(uint8_t sendByte, OWLevel afterLevel)
IanBenzMaxim 26:a361e3f42ba5 367 {
IanBenzMaxim 26:a361e3f42ba5 368 OneWireMaster::CmdResult result = OneWireMaster::OperationFailure;
j3 63:422be898443a 369
IanBenzMaxim 74:23be10c32fa3 370 uint8_t readbuffer[10], sendpacket[10];
IanBenzMaxim 74:23be10c32fa3 371 uint8_t sendlen = 0;
j3 63:422be898443a 372
j3 63:422be898443a 373 // make sure normal level
IanBenzMaxim 75:8b627804927c 374 OWSetLevel(OneWireMaster::NormalLevel);
j3 63:422be898443a 375
j3 63:422be898443a 376 // check for correct mode
IanBenzMaxim 74:23be10c32fa3 377 if (_UMode != MODSEL_DATA)
j3 63:422be898443a 378 {
j3 63:422be898443a 379 _UMode = MODSEL_DATA;
j3 63:422be898443a 380 sendpacket[sendlen++] = MODE_DATA;
j3 63:422be898443a 381 }
j3 63:422be898443a 382
j3 63:422be898443a 383 // add the byte to send
IanBenzMaxim 75:8b627804927c 384 sendpacket[sendlen++] = sendByte;
j3 63:422be898443a 385
j3 63:422be898443a 386 // check for duplication of data that looks like COMMAND mode
IanBenzMaxim 75:8b627804927c 387 if (sendByte == MODE_COMMAND)
j3 63:422be898443a 388 {
IanBenzMaxim 75:8b627804927c 389 sendpacket[sendlen++] = sendByte;
j3 63:422be898443a 390 }
j3 63:422be898443a 391
j3 63:422be898443a 392 // flush the buffers
j3 63:422be898443a 393 FlushCOM();
j3 63:422be898443a 394
j3 63:422be898443a 395 // send the packet
IanBenzMaxim 74:23be10c32fa3 396 result = WriteCOM(sendlen, sendpacket);
IanBenzMaxim 74:23be10c32fa3 397 if (result == OneWireMaster::Success)
j3 63:422be898443a 398 {
j3 63:422be898443a 399 // read back the 1 byte response
IanBenzMaxim 74:23be10c32fa3 400 result = ReadCOM(1, readbuffer);
IanBenzMaxim 75:8b627804927c 401 if ((result == OneWireMaster::Success) && (readbuffer[0] == sendByte))
j3 63:422be898443a 402 {
j3 63:422be898443a 403 result = OneWireMaster::Success;
j3 63:422be898443a 404 }
j3 63:422be898443a 405 else
j3 63:422be898443a 406 {
j3 63:422be898443a 407 result = OneWireMaster::CommunicationReadError;
j3 63:422be898443a 408 }
j3 63:422be898443a 409 }
j3 63:422be898443a 410 else
j3 63:422be898443a 411 {
j3 63:422be898443a 412 result = OneWireMaster::CommunicationWriteError;
j3 63:422be898443a 413 }
j3 63:422be898443a 414
j3 63:422be898443a 415 // an error occured so re-sync with DS2480B
IanBenzMaxim 74:23be10c32fa3 416 if (result != OneWireMaster::Success)
j3 63:422be898443a 417 {
j3 63:422be898443a 418 DS2480B_Detect();
j3 63:422be898443a 419 }
j3 63:422be898443a 420 else
j3 63:422be898443a 421 {
IanBenzMaxim 75:8b627804927c 422 result = OWSetLevel(afterLevel);
j3 63:422be898443a 423 }
j3 63:422be898443a 424
IanBenzMaxim 26:a361e3f42ba5 425 return result;
IanBenzMaxim 26:a361e3f42ba5 426 }
IanBenzMaxim 26:a361e3f42ba5 427
IanBenzMaxim 26:a361e3f42ba5 428
IanBenzMaxim 26:a361e3f42ba5 429 //*********************************************************************
IanBenzMaxim 75:8b627804927c 430 OneWireMaster::CmdResult DS2480B::OWReadByteSetLevel(uint8_t & recvByte, OWLevel afterLevel)
j3 3:644fc630f958 431 {
j3 23:e8e403d61359 432 OneWireMaster::CmdResult result = OneWireMaster::OperationFailure;
j3 63:422be898443a 433
IanBenzMaxim 74:23be10c32fa3 434 uint8_t readbuffer[10], sendpacket[10];
IanBenzMaxim 74:23be10c32fa3 435 uint8_t sendlen = 0;
j3 63:422be898443a 436
j3 63:422be898443a 437 // make sure normal level
IanBenzMaxim 75:8b627804927c 438 OWSetLevel(OneWireMaster::NormalLevel);
j3 63:422be898443a 439
j3 63:422be898443a 440 // check for correct mode
IanBenzMaxim 74:23be10c32fa3 441 if (_UMode != MODSEL_DATA)
j3 63:422be898443a 442 {
j3 63:422be898443a 443 _UMode = MODSEL_DATA;
j3 63:422be898443a 444 sendpacket[sendlen++] = MODE_DATA;
j3 63:422be898443a 445 }
j3 63:422be898443a 446
j3 63:422be898443a 447 // add the byte to send
j3 63:422be898443a 448 sendpacket[sendlen++] = 0xFF;
j3 63:422be898443a 449
j3 63:422be898443a 450 // flush the buffers
j3 63:422be898443a 451 FlushCOM();
j3 63:422be898443a 452
j3 63:422be898443a 453 // send the packet
IanBenzMaxim 74:23be10c32fa3 454 result = WriteCOM(sendlen, sendpacket);
IanBenzMaxim 74:23be10c32fa3 455 if (result == OneWireMaster::Success)
j3 63:422be898443a 456 {
j3 63:422be898443a 457 // read back the 1 byte response
IanBenzMaxim 74:23be10c32fa3 458 result = ReadCOM(1, readbuffer);
IanBenzMaxim 74:23be10c32fa3 459 if (result == OneWireMaster::Success)
j3 63:422be898443a 460 {
IanBenzMaxim 75:8b627804927c 461 recvByte = readbuffer[0];
j3 63:422be898443a 462 result = OneWireMaster::Success;
j3 63:422be898443a 463 }
j3 63:422be898443a 464 else
j3 63:422be898443a 465 {
j3 63:422be898443a 466 result = OneWireMaster::CommunicationReadError;
j3 63:422be898443a 467 }
j3 63:422be898443a 468 }
j3 63:422be898443a 469 else
j3 63:422be898443a 470 {
j3 63:422be898443a 471 result = OneWireMaster::CommunicationWriteError;
j3 63:422be898443a 472 }
j3 63:422be898443a 473
j3 63:422be898443a 474 // an error occured so re-sync with DS2480B
IanBenzMaxim 74:23be10c32fa3 475 if (result != OneWireMaster::Success)
j3 63:422be898443a 476 {
j3 63:422be898443a 477 DS2480B_Detect();
j3 63:422be898443a 478 }
j3 63:422be898443a 479 else
j3 63:422be898443a 480 {
IanBenzMaxim 75:8b627804927c 481 result = OWSetLevel(afterLevel);
j3 63:422be898443a 482 }
j3 63:422be898443a 483
j3 17:b646b1e3970b 484 return result;
j3 3:644fc630f958 485 }
j3 3:644fc630f958 486
j3 3:644fc630f958 487
j3 3:644fc630f958 488 //*********************************************************************
IanBenzMaxim 75:8b627804927c 489 OneWireMaster::CmdResult DS2480B::OWSetSpeed(OWSpeed newSpeed)
j3 17:b646b1e3970b 490 {
j3 96:9b7b63c0105e 491 OneWireMaster::CmdResult result = OneWireMaster::OperationFailure; //uchar rt = FALSE;
j3 96:9b7b63c0105e 492
j3 96:9b7b63c0105e 493 uint8_t sendpacket[5];
j3 96:9b7b63c0105e 494 uint8_t sendlen = 0;
IanBenzMaxim 74:23be10c32fa3 495
j3 96:9b7b63c0105e 496 // check if change from current mode
j3 96:9b7b63c0105e 497 if (((newSpeed == OneWireMaster::OverdriveSpeed) && (_USpeed != OneWireMaster::OverdriveSpeed)) ||
j3 96:9b7b63c0105e 498 ((newSpeed == OneWireMaster::StandardSpeed) && (_USpeed != OneWireMaster::StandardSpeed)))
j3 96:9b7b63c0105e 499 {
j3 96:9b7b63c0105e 500 if (newSpeed == OneWireMaster::OverdriveSpeed)
j3 96:9b7b63c0105e 501 {
j3 96:9b7b63c0105e 502 result = DS2480B_ChangeBaud(Bps115200);
j3 96:9b7b63c0105e 503 if (result == OneWireMaster::Success)
j3 96:9b7b63c0105e 504 {
j3 96:9b7b63c0105e 505 _USpeed = SPEEDSEL_OD;
j3 96:9b7b63c0105e 506 }
j3 96:9b7b63c0105e 507 }
j3 96:9b7b63c0105e 508 else if (newSpeed == OneWireMaster::StandardSpeed)
j3 96:9b7b63c0105e 509 {
j3 96:9b7b63c0105e 510 result = DS2480B_ChangeBaud(Bps9600);
j3 96:9b7b63c0105e 511 if (result == OneWireMaster::Success)
j3 96:9b7b63c0105e 512 {
j3 96:9b7b63c0105e 513 _USpeed = SPEEDSEL_STD;
j3 96:9b7b63c0105e 514 }
j3 96:9b7b63c0105e 515 }
j3 96:9b7b63c0105e 516
j3 96:9b7b63c0105e 517 // if baud rate is set correctly then change DS2480 speed
j3 96:9b7b63c0105e 518 if (result == OneWireMaster::Success)
j3 96:9b7b63c0105e 519 {
j3 96:9b7b63c0105e 520 // check if correct mode
j3 96:9b7b63c0105e 521 if (_UMode != MODSEL_COMMAND)
j3 96:9b7b63c0105e 522 {
j3 96:9b7b63c0105e 523 _UMode = MODSEL_COMMAND;
j3 96:9b7b63c0105e 524 sendpacket[sendlen++] = MODE_COMMAND;
j3 96:9b7b63c0105e 525 }
j3 96:9b7b63c0105e 526
j3 96:9b7b63c0105e 527 // proceed to set the DS2480 communication speed
j3 96:9b7b63c0105e 528 sendpacket[sendlen++] = CMD_COMM | FUNCTSEL_SEARCHOFF | _USpeed;
j3 96:9b7b63c0105e 529
j3 96:9b7b63c0105e 530 // send the packet
j3 96:9b7b63c0105e 531 result = WriteCOM(sendlen,sendpacket);
j3 96:9b7b63c0105e 532 if (result != OneWireMaster::Success)
j3 96:9b7b63c0105e 533 {
j3 96:9b7b63c0105e 534 // lost communication with DS2480 then reset
j3 96:9b7b63c0105e 535 DS2480B_Detect();
j3 96:9b7b63c0105e 536 }
j3 96:9b7b63c0105e 537 }
j3 96:9b7b63c0105e 538 }
j3 96:9b7b63c0105e 539
IanBenzMaxim 74:23be10c32fa3 540
j3 17:b646b1e3970b 541 return result;
j3 3:644fc630f958 542 }
j3 3:644fc630f958 543
j3 3:644fc630f958 544
j3 3:644fc630f958 545 //*********************************************************************
IanBenzMaxim 75:8b627804927c 546 OneWireMaster::CmdResult DS2480B::OWSetLevel(OWLevel newLevel)
j3 3:644fc630f958 547 {
j3 63:422be898443a 548 OneWireMaster::CmdResult result = OneWireMaster::Success;
j3 63:422be898443a 549
IanBenzMaxim 74:23be10c32fa3 550 uint8_t sendpacket[10], readbuffer[10];
IanBenzMaxim 74:23be10c32fa3 551 uint8_t sendlen = 0;
j3 63:422be898443a 552
j3 63:422be898443a 553 // check if need to change level
IanBenzMaxim 75:8b627804927c 554 if (newLevel != _ULevel)
j3 63:422be898443a 555 {
j3 63:422be898443a 556 // check for correct mode
IanBenzMaxim 74:23be10c32fa3 557 if (_UMode != MODSEL_COMMAND)
j3 63:422be898443a 558 {
j3 63:422be898443a 559 _UMode = MODSEL_COMMAND;
j3 63:422be898443a 560 sendpacket[sendlen++] = MODE_COMMAND;
j3 63:422be898443a 561 }
IanBenzMaxim 74:23be10c32fa3 562
j3 63:422be898443a 563 // check if just putting back to normal
IanBenzMaxim 75:8b627804927c 564 if (newLevel == OneWireMaster::NormalLevel)
j3 63:422be898443a 565 {
j3 63:422be898443a 566 // stop pulse command
j3 63:422be898443a 567 sendpacket[sendlen++] = MODE_STOP_PULSE;
j3 63:422be898443a 568
j3 63:422be898443a 569 // add the command to begin the pulse WITHOUT prime
j3 63:422be898443a 570 sendpacket[sendlen++] = CMD_COMM | FUNCTSEL_CHMOD | SPEEDSEL_PULSE | BITPOL_5V | PRIME5V_FALSE;
j3 63:422be898443a 571
j3 63:422be898443a 572 // stop pulse command
j3 63:422be898443a 573 sendpacket[sendlen++] = MODE_STOP_PULSE;
IanBenzMaxim 74:23be10c32fa3 574
j3 63:422be898443a 575 FlushCOM();
j3 63:422be898443a 576
j3 63:422be898443a 577 // send the packet
IanBenzMaxim 74:23be10c32fa3 578 result = WriteCOM(sendlen, sendpacket);
IanBenzMaxim 74:23be10c32fa3 579 if (result == OneWireMaster::Success)
j3 63:422be898443a 580 {
j3 63:422be898443a 581 // read back the 1 byte response
IanBenzMaxim 74:23be10c32fa3 582 result = ReadCOM(2, readbuffer);
IanBenzMaxim 74:23be10c32fa3 583 if (result == OneWireMaster::Success)
j3 63:422be898443a 584 {
j3 63:422be898443a 585 // check response byte
IanBenzMaxim 74:23be10c32fa3 586 if (((readbuffer[0] & 0xE0) == 0xE0) && ((readbuffer[1] & 0xE0) == 0xE0))
j3 63:422be898443a 587 {
IanBenzMaxim 75:8b627804927c 588 _ULevel = OneWireMaster::NormalLevel;
j3 63:422be898443a 589 }
j3 63:422be898443a 590 else
j3 63:422be898443a 591 {
j3 63:422be898443a 592 result = OneWireMaster::OperationFailure;
j3 63:422be898443a 593 }
j3 63:422be898443a 594 }
j3 63:422be898443a 595 }
j3 63:422be898443a 596 }
j3 63:422be898443a 597 // set new level
IanBenzMaxim 74:23be10c32fa3 598 else
j3 63:422be898443a 599 {
j3 63:422be898443a 600 // set the SPUD time value
j3 63:422be898443a 601 sendpacket[sendlen++] = CMD_CONFIG | PARMSEL_5VPULSE | PARMSET_infinite;
j3 63:422be898443a 602 // add the command to begin the pulse
j3 63:422be898443a 603 sendpacket[sendlen++] = CMD_COMM | FUNCTSEL_CHMOD | SPEEDSEL_PULSE | BITPOL_5V;
IanBenzMaxim 74:23be10c32fa3 604
j3 63:422be898443a 605 FlushCOM();
j3 63:422be898443a 606
j3 63:422be898443a 607 // send the packet
IanBenzMaxim 74:23be10c32fa3 608 result = WriteCOM(sendlen, sendpacket);
IanBenzMaxim 74:23be10c32fa3 609 if (result == OneWireMaster::Success)
j3 63:422be898443a 610 {
j3 63:422be898443a 611 // read back the 1 byte response from setting time limit
IanBenzMaxim 74:23be10c32fa3 612 result = ReadCOM(1, readbuffer);
IanBenzMaxim 74:23be10c32fa3 613 if (result == OneWireMaster::Success)
j3 63:422be898443a 614 {
j3 63:422be898443a 615 // check response byte
IanBenzMaxim 74:23be10c32fa3 616 if ((readbuffer[0] & 0x81) == 0)
j3 63:422be898443a 617 {
IanBenzMaxim 75:8b627804927c 618 _ULevel = newLevel;
j3 63:422be898443a 619 }
j3 63:422be898443a 620 else
j3 63:422be898443a 621 {
j3 63:422be898443a 622 result = OneWireMaster::OperationFailure;
j3 63:422be898443a 623 }
j3 63:422be898443a 624 }
j3 63:422be898443a 625 }
j3 63:422be898443a 626 }
j3 63:422be898443a 627
j3 63:422be898443a 628 // if lost communication with DS2480B then reset
IanBenzMaxim 74:23be10c32fa3 629 if (result != OneWireMaster::Success)
j3 63:422be898443a 630 {
j3 63:422be898443a 631 DS2480B_Detect();
j3 63:422be898443a 632 }
IanBenzMaxim 74:23be10c32fa3 633
j3 63:422be898443a 634 }
j3 63:422be898443a 635
j3 63:422be898443a 636 return result;
j3 63:422be898443a 637 }
j3 63:422be898443a 638
j3 63:422be898443a 639
j3 63:422be898443a 640 //*********************************************************************
IanBenzMaxim 73:2cecc1372acc 641 OneWireMaster::CmdResult DS2480B::DS2480B_Detect(void)
j3 63:422be898443a 642 {
j3 63:422be898443a 643 OneWireMaster::CmdResult result;
j3 63:422be898443a 644
IanBenzMaxim 74:23be10c32fa3 645 uint8_t sendpacket[10], readbuffer[10];
IanBenzMaxim 74:23be10c32fa3 646 uint8_t sendlen = 0;
j3 63:422be898443a 647
j3 63:422be898443a 648 // reset modes
j3 63:422be898443a 649 _UMode = MODSEL_COMMAND;
IanBenzMaxim 75:8b627804927c 650 _UBaud = Bps9600;
j3 63:422be898443a 651 _USpeed = SPEEDSEL_FLEX;
j3 63:422be898443a 652
j3 63:422be898443a 653 // set the baud rate to 9600
j3 63:422be898443a 654 SetBaudCOM(_UBaud);
j3 63:422be898443a 655
j3 63:422be898443a 656 // send a break to reset the DS2480B
j3 63:422be898443a 657 BreakCOM();
j3 63:422be898443a 658
j3 63:422be898443a 659 // delay to let line settle
j3 63:422be898443a 660 wait_ms(2);
IanBenzMaxim 74:23be10c32fa3 661
j3 63:422be898443a 662 FlushCOM();
j3 63:422be898443a 663
j3 63:422be898443a 664 // send the timing byte
j3 63:422be898443a 665 sendpacket[0] = 0xC1;
IanBenzMaxim 74:23be10c32fa3 666 result = WriteCOM(1, sendpacket);
IanBenzMaxim 74:23be10c32fa3 667 if (result == OneWireMaster::Success)
j3 63:422be898443a 668 {
j3 63:422be898443a 669 // delay to let line settle
j3 63:422be898443a 670 wait_ms(2);
j3 63:422be898443a 671
j3 63:422be898443a 672 // set the FLEX configuration parameters
j3 63:422be898443a 673 // default PDSRC = 1.37Vus
j3 63:422be898443a 674 sendpacket[sendlen++] = CMD_CONFIG | PARMSEL_SLEW | PARMSET_Slew1p37Vus;
j3 63:422be898443a 675 // default W1LT = 10us
j3 63:422be898443a 676 sendpacket[sendlen++] = CMD_CONFIG | PARMSEL_WRITE1LOW | PARMSET_Write10us;
j3 63:422be898443a 677 // default DSO/WORT = 8us
j3 63:422be898443a 678 sendpacket[sendlen++] = CMD_CONFIG | PARMSEL_SAMPLEOFFSET | PARMSET_SampOff8us;
j3 63:422be898443a 679
j3 63:422be898443a 680 // construct the command to read the baud rate (to test command block)
j3 63:422be898443a 681 sendpacket[sendlen++] = CMD_CONFIG | PARMSEL_PARMREAD | (PARMSEL_BAUDRATE >> 3);
j3 63:422be898443a 682
j3 63:422be898443a 683 // also do 1 bit operation (to test 1-Wire block)
j3 63:422be898443a 684 sendpacket[sendlen++] = CMD_COMM | FUNCTSEL_BIT | _UBaud | BITPOL_ONE;
IanBenzMaxim 74:23be10c32fa3 685
j3 63:422be898443a 686 FlushCOM();
j3 63:422be898443a 687
j3 63:422be898443a 688 // send the packet
IanBenzMaxim 74:23be10c32fa3 689 result = WriteCOM(sendlen, sendpacket);
IanBenzMaxim 74:23be10c32fa3 690 if (result == OneWireMaster::Success)
j3 63:422be898443a 691 {
j3 63:422be898443a 692 // read back the response
IanBenzMaxim 74:23be10c32fa3 693 result = ReadCOM(5, readbuffer);
IanBenzMaxim 74:23be10c32fa3 694 if (result == OneWireMaster::Success)
j3 63:422be898443a 695 {
j3 63:422be898443a 696 // look at the baud rate and bit operation
j3 63:422be898443a 697 // to see if the response makes sense
IanBenzMaxim 74:23be10c32fa3 698 if (((readbuffer[3] & 0xF1) == 0x00) && ((readbuffer[3] & 0x0E) == _UBaud) && ((readbuffer[4] & 0xF0) == 0x90) && ((readbuffer[4] & 0x0C) == _UBaud))
j3 63:422be898443a 699 {
j3 63:422be898443a 700 result = OneWireMaster::Success;
j3 63:422be898443a 701 }
j3 63:422be898443a 702 else
j3 63:422be898443a 703 {
j3 63:422be898443a 704 result = OneWireMaster::OperationFailure;
j3 63:422be898443a 705 }
j3 63:422be898443a 706 }
j3 63:422be898443a 707 }
j3 63:422be898443a 708 }
IanBenzMaxim 74:23be10c32fa3 709
j3 63:422be898443a 710 return result;
j3 63:422be898443a 711 }
j3 63:422be898443a 712
j3 63:422be898443a 713
j3 63:422be898443a 714 //*********************************************************************
IanBenzMaxim 75:8b627804927c 715 OneWireMaster::CmdResult DS2480B::DS2480B_ChangeBaud(Baud newBaud)
j3 63:422be898443a 716 {
j3 65:a28ac52ca127 717 OneWireMaster::CmdResult result = OneWireMaster::Success;
j3 65:a28ac52ca127 718
IanBenzMaxim 74:23be10c32fa3 719 uint8_t readbuffer[5], sendpacket[5], sendpacket2[5];
IanBenzMaxim 74:23be10c32fa3 720 uint8_t sendlen = 0, sendlen2 = 0;
j3 65:a28ac52ca127 721
j3 65:a28ac52ca127 722 //see if diffenent then current baud rate
IanBenzMaxim 75:8b627804927c 723 if (_UBaud != newBaud)
j3 65:a28ac52ca127 724 {
j3 65:a28ac52ca127 725 // build the command packet
j3 65:a28ac52ca127 726 // check for correct mode
IanBenzMaxim 74:23be10c32fa3 727 if (_UMode != MODSEL_COMMAND)
j3 65:a28ac52ca127 728 {
j3 65:a28ac52ca127 729 _UMode = MODSEL_COMMAND;
j3 65:a28ac52ca127 730 sendpacket[sendlen++] = MODE_COMMAND;
j3 65:a28ac52ca127 731 }
j3 65:a28ac52ca127 732 // build the command
IanBenzMaxim 75:8b627804927c 733 sendpacket[sendlen++] = CMD_CONFIG | PARMSEL_BAUDRATE | newBaud;
j3 63:422be898443a 734
j3 65:a28ac52ca127 735 // flush the buffers
j3 65:a28ac52ca127 736 FlushCOM();
j3 65:a28ac52ca127 737
j3 65:a28ac52ca127 738 // send the packet
IanBenzMaxim 74:23be10c32fa3 739 result = WriteCOM(sendlen, sendpacket);
IanBenzMaxim 74:23be10c32fa3 740 if (result == OneWireMaster::Success)
j3 65:a28ac52ca127 741 {
j3 65:a28ac52ca127 742 // make sure buffer is flushed
j3 65:a28ac52ca127 743 wait_ms(5);
j3 63:422be898443a 744
j3 65:a28ac52ca127 745 // change our baud rate
IanBenzMaxim 75:8b627804927c 746 SetBaudCOM(newBaud);
IanBenzMaxim 75:8b627804927c 747 _UBaud = newBaud;
j3 65:a28ac52ca127 748
j3 65:a28ac52ca127 749 // wait for things to settle
j3 65:a28ac52ca127 750 wait_ms(5);
j3 65:a28ac52ca127 751
j3 65:a28ac52ca127 752 // build a command packet to read back baud rate
j3 65:a28ac52ca127 753 sendpacket2[sendlen2++] = CMD_CONFIG | PARMSEL_PARMREAD | (PARMSEL_BAUDRATE >> 3);
j3 65:a28ac52ca127 754
j3 65:a28ac52ca127 755 // flush the buffers
j3 65:a28ac52ca127 756 FlushCOM();
j3 63:422be898443a 757
j3 65:a28ac52ca127 758 // send the packet
IanBenzMaxim 74:23be10c32fa3 759 result = WriteCOM(sendlen2, sendpacket2);
IanBenzMaxim 74:23be10c32fa3 760 if (result == OneWireMaster::Success)
j3 63:422be898443a 761 {
j3 65:a28ac52ca127 762 // read back the 1 byte response
IanBenzMaxim 74:23be10c32fa3 763 result = ReadCOM(1, readbuffer);
IanBenzMaxim 74:23be10c32fa3 764 if (result == OneWireMaster::Success)
j3 65:a28ac52ca127 765 {
j3 65:a28ac52ca127 766 // verify correct baud
IanBenzMaxim 74:23be10c32fa3 767 if ((readbuffer[0] & 0x0E) == (sendpacket[sendlen - 1] & 0x0E))
j3 65:a28ac52ca127 768 {
j3 65:a28ac52ca127 769 result = OneWireMaster::Success;
j3 65:a28ac52ca127 770 }
j3 65:a28ac52ca127 771 else
j3 65:a28ac52ca127 772 {
IanBenzMaxim 74:23be10c32fa3 773 result = OneWireMaster::OperationFailure;
j3 65:a28ac52ca127 774 }
j3 65:a28ac52ca127 775 }
j3 65:a28ac52ca127 776 else
j3 65:a28ac52ca127 777 {
j3 65:a28ac52ca127 778 result = OneWireMaster::CommunicationReadError;
j3 65:a28ac52ca127 779 }
j3 63:422be898443a 780 }
j3 65:a28ac52ca127 781 else
j3 65:a28ac52ca127 782 {
j3 65:a28ac52ca127 783 result = OneWireMaster::CommunicationWriteError;
j3 65:a28ac52ca127 784 }
j3 65:a28ac52ca127 785 }
j3 65:a28ac52ca127 786 else
j3 65:a28ac52ca127 787 {
j3 65:a28ac52ca127 788 result = OneWireMaster::CommunicationWriteError;
j3 65:a28ac52ca127 789 }
j3 65:a28ac52ca127 790 }
IanBenzMaxim 74:23be10c32fa3 791
j3 65:a28ac52ca127 792 // if lost communication with DS2480B then reset
IanBenzMaxim 74:23be10c32fa3 793 if (result != OneWireMaster::Success)
j3 65:a28ac52ca127 794 {
j3 65:a28ac52ca127 795 DS2480B_Detect();
j3 65:a28ac52ca127 796 }
IanBenzMaxim 74:23be10c32fa3 797
j3 65:a28ac52ca127 798 return result;
j3 63:422be898443a 799 }
j3 63:422be898443a 800
j3 63:422be898443a 801
j3 63:422be898443a 802 //*********************************************************************
IanBenzMaxim 73:2cecc1372acc 803 OneWireMaster::CmdResult DS2480B::WriteCOM(uint32_t outlen, uint8_t *outbuf)
j3 63:422be898443a 804 {
j3 23:e8e403d61359 805 OneWireMaster::CmdResult result = OneWireMaster::OperationFailure;
IanBenzMaxim 74:23be10c32fa3 806
IanBenzMaxim 73:2cecc1372acc 807 mbed::Timer t;
j3 63:422be898443a 808 uint32_t t_val;
j3 63:422be898443a 809 uint32_t timeout;
j3 63:422be898443a 810 uint8_t idx = 0;
IanBenzMaxim 74:23be10c32fa3 811
j3 63:422be898443a 812 //calculate timeout, the time needed to tx 1 byte
j3 65:a28ac52ca127 813 //double for 115200 due to timer object inaccuracies
IanBenzMaxim 74:23be10c32fa3 814 switch (_UBaud)
j3 63:422be898443a 815 {
IanBenzMaxim 75:8b627804927c 816 case Bps115200:
IanBenzMaxim 74:23be10c32fa3 817 timeout = ((1000000 / 115200) * 20);
IanBenzMaxim 74:23be10c32fa3 818 break;
IanBenzMaxim 74:23be10c32fa3 819
IanBenzMaxim 75:8b627804927c 820 case Bps57600:
IanBenzMaxim 74:23be10c32fa3 821 timeout = ((1000000 / 57600) * 10);
IanBenzMaxim 74:23be10c32fa3 822 break;
IanBenzMaxim 74:23be10c32fa3 823
IanBenzMaxim 75:8b627804927c 824 case Bps19200:
IanBenzMaxim 74:23be10c32fa3 825 timeout = ((1000000 / 19200) * 10);
IanBenzMaxim 74:23be10c32fa3 826 break;
IanBenzMaxim 74:23be10c32fa3 827
IanBenzMaxim 75:8b627804927c 828 case Bps9600:
IanBenzMaxim 74:23be10c32fa3 829 default:
IanBenzMaxim 74:23be10c32fa3 830 timeout = ((1000000 / 9600) * 10);
IanBenzMaxim 74:23be10c32fa3 831 break;
j3 63:422be898443a 832 }
IanBenzMaxim 74:23be10c32fa3 833
j3 63:422be898443a 834 t.start();
j3 63:422be898443a 835 do
j3 63:422be898443a 836 {
j3 63:422be898443a 837 t.reset();
j3 63:422be898443a 838 do
j3 63:422be898443a 839 {
j3 63:422be898443a 840 t_val = t.read_us();
IanBenzMaxim 74:23be10c32fa3 841 } while (!_p_serial->writeable() && (t_val < timeout));
IanBenzMaxim 74:23be10c32fa3 842
IanBenzMaxim 74:23be10c32fa3 843 if (t_val < timeout)
j3 63:422be898443a 844 {
j3 63:422be898443a 845 _p_serial->putc(outbuf[idx++]);
j3 63:422be898443a 846 result = OneWireMaster::Success;
j3 63:422be898443a 847 }
j3 63:422be898443a 848 else
j3 63:422be898443a 849 {
j3 63:422be898443a 850 result = OneWireMaster::TimeoutError;
j3 63:422be898443a 851 }
IanBenzMaxim 74:23be10c32fa3 852 } while ((idx < outlen) && (result == OneWireMaster::Success));
IanBenzMaxim 74:23be10c32fa3 853
j3 17:b646b1e3970b 854 return result;
j3 63:422be898443a 855 }
j3 63:422be898443a 856
j3 63:422be898443a 857
j3 63:422be898443a 858 //*********************************************************************
IanBenzMaxim 73:2cecc1372acc 859 OneWireMaster::CmdResult DS2480B::ReadCOM(uint32_t inlen, uint8_t *inbuf)
j3 63:422be898443a 860 {
j3 63:422be898443a 861 OneWireMaster::CmdResult result;
IanBenzMaxim 73:2cecc1372acc 862 mbed::Timer t;
IanBenzMaxim 74:23be10c32fa3 863 uint32_t num_bytes_read = 0;
j3 63:422be898443a 864 uint32_t timeout;
j3 65:a28ac52ca127 865 uint32_t micro_seconds;
IanBenzMaxim 74:23be10c32fa3 866
j3 65:a28ac52ca127 867 //calculate timeout, 10x the time needed to recieve inlen bytes
j3 65:a28ac52ca127 868 //double for 115200 due to timer object inaccuracies
IanBenzMaxim 74:23be10c32fa3 869 switch (_UBaud)
j3 63:422be898443a 870 {
IanBenzMaxim 75:8b627804927c 871 case Bps115200:
IanBenzMaxim 74:23be10c32fa3 872 timeout = ((1000000 / 115200) * 200) * inlen;
IanBenzMaxim 74:23be10c32fa3 873 break;
IanBenzMaxim 74:23be10c32fa3 874
IanBenzMaxim 75:8b627804927c 875 case Bps57600:
IanBenzMaxim 74:23be10c32fa3 876 timeout = ((1000000 / 57600) * 100) * inlen;
IanBenzMaxim 74:23be10c32fa3 877 break;
IanBenzMaxim 74:23be10c32fa3 878
IanBenzMaxim 75:8b627804927c 879 case Bps19200:
IanBenzMaxim 74:23be10c32fa3 880 timeout = ((1000000 / 19200) * 100) * inlen;
IanBenzMaxim 74:23be10c32fa3 881 break;
IanBenzMaxim 74:23be10c32fa3 882
IanBenzMaxim 75:8b627804927c 883 case Bps9600:
IanBenzMaxim 74:23be10c32fa3 884 default:
IanBenzMaxim 74:23be10c32fa3 885 timeout = ((1000000 / 9600) * 100) * inlen;
IanBenzMaxim 74:23be10c32fa3 886 break;
j3 63:422be898443a 887 }
IanBenzMaxim 74:23be10c32fa3 888
IanBenzMaxim 74:23be10c32fa3 889 if (rx_buffer.wrap_error)
j3 63:422be898443a 890 {
j3 63:422be898443a 891 //reset rx buffer, error, and return failure
j3 63:422be898443a 892 rx_buffer.w_idx = 0;
j3 63:422be898443a 893 rx_buffer.r_idx = 0;
j3 65:a28ac52ca127 894 rx_buffer.rx_bytes_available = 0;
j3 63:422be898443a 895 rx_buffer.wrap_error = false;
IanBenzMaxim 74:23be10c32fa3 896
j3 63:422be898443a 897 result = OneWireMaster::OperationFailure;
j3 63:422be898443a 898 }
j3 63:422be898443a 899 else
j3 63:422be898443a 900 {
j3 65:a28ac52ca127 901 t.start();
j3 65:a28ac52ca127 902 t.reset();
j3 65:a28ac52ca127 903 do
j3 63:422be898443a 904 {
j3 63:422be898443a 905 do
j3 63:422be898443a 906 {
j3 65:a28ac52ca127 907 micro_seconds = t.read_us();
IanBenzMaxim 74:23be10c32fa3 908 } while (!rx_buffer.rx_bytes_available && (micro_seconds < timeout));
IanBenzMaxim 74:23be10c32fa3 909
IanBenzMaxim 74:23be10c32fa3 910 if (rx_buffer.rx_bytes_available)
j3 63:422be898443a 911 {
j3 65:a28ac52ca127 912 inbuf[num_bytes_read++] = rx_buffer.buff[rx_buffer.r_idx++];
j3 65:a28ac52ca127 913 rx_buffer.rx_bytes_available--;
IanBenzMaxim 74:23be10c32fa3 914 }
IanBenzMaxim 74:23be10c32fa3 915 } while ((num_bytes_read < inlen) && (micro_seconds < timeout) && !rx_buffer.wrap_error);
j3 65:a28ac52ca127 916 t.stop();
IanBenzMaxim 74:23be10c32fa3 917
IanBenzMaxim 74:23be10c32fa3 918 if (num_bytes_read == inlen)
j3 65:a28ac52ca127 919 {
j3 65:a28ac52ca127 920 result = OneWireMaster::Success;
j3 65:a28ac52ca127 921 }
IanBenzMaxim 74:23be10c32fa3 922 else if (micro_seconds > timeout)
j3 65:a28ac52ca127 923 {
j3 65:a28ac52ca127 924 result = OneWireMaster::TimeoutError;
j3 63:422be898443a 925 }
j3 63:422be898443a 926 else
j3 63:422be898443a 927 {
j3 65:a28ac52ca127 928 //reset rx buffer, error, and return failure
j3 65:a28ac52ca127 929 rx_buffer.w_idx = 0;
j3 65:a28ac52ca127 930 rx_buffer.r_idx = 0;
j3 65:a28ac52ca127 931 rx_buffer.rx_bytes_available = 0;
j3 65:a28ac52ca127 932 rx_buffer.wrap_error = false;
IanBenzMaxim 74:23be10c32fa3 933
j3 65:a28ac52ca127 934 result = OneWireMaster::CommunicationReadError;
j3 63:422be898443a 935 }
j3 63:422be898443a 936 }
IanBenzMaxim 74:23be10c32fa3 937
j3 63:422be898443a 938 return result;
j3 63:422be898443a 939 }
j3 63:422be898443a 940
j3 63:422be898443a 941
j3 63:422be898443a 942 //*********************************************************************
IanBenzMaxim 73:2cecc1372acc 943 void DS2480B::BreakCOM(void)
j3 63:422be898443a 944 {
IanBenzMaxim 74:23be10c32fa3 945 while (!_p_serial->writeable());
j3 63:422be898443a 946 //for some reason send_break wouldn't work unless first sending char
j3 63:422be898443a 947 _p_serial->putc(0);
j3 63:422be898443a 948 _p_serial->send_break();
j3 63:422be898443a 949 }
j3 63:422be898443a 950
j3 63:422be898443a 951
IanBenzMaxim 73:2cecc1372acc 952 void DS2480B::FlushCOM(void)
j3 63:422be898443a 953 {
j3 63:422be898443a 954 //reset soft rx_buffer
j3 63:422be898443a 955 rx_buffer.w_idx = 0;
j3 63:422be898443a 956 rx_buffer.r_idx = 0;
j3 63:422be898443a 957 rx_buffer.wrap_error = false;
IanBenzMaxim 74:23be10c32fa3 958
j3 63:422be898443a 959 //make sure hardware rx buffer is empty
IanBenzMaxim 74:23be10c32fa3 960 while (_p_serial->readable())
j3 63:422be898443a 961 {
j3 63:422be898443a 962 _p_serial->getc();
j3 63:422be898443a 963 }
IanBenzMaxim 74:23be10c32fa3 964
j3 65:a28ac52ca127 965 /*Not sure how to flush tx buffer without sending data out on the bus.
j3 65:a28ac52ca127 966 from the example in AN192, the data shouldn't be sent, just aborted
j3 65:a28ac52ca127 967 and the buffer cleaned out, http://pdfserv.maximintegrated.com/en/an/AN192.pdf
j3 65:a28ac52ca127 968 Below is what was used in AN192 example code using windows drivers
j3 65:a28ac52ca127 969 */
j3 65:a28ac52ca127 970
j3 65:a28ac52ca127 971 //PurgeComm(ComID, PURGE_TXABORT | PURGE_RXABORT | PURGE_TXCLEAR | PURGE_RXCLEAR );
j3 63:422be898443a 972 }
j3 63:422be898443a 973
j3 63:422be898443a 974
j3 63:422be898443a 975 //*********************************************************************
IanBenzMaxim 73:2cecc1372acc 976 void DS2480B::SetBaudCOM(uint8_t new_baud)
j3 63:422be898443a 977 {
IanBenzMaxim 74:23be10c32fa3 978 switch (new_baud)
j3 63:422be898443a 979 {
IanBenzMaxim 75:8b627804927c 980 case Bps115200:
IanBenzMaxim 74:23be10c32fa3 981 _p_serial->baud(115200);
IanBenzMaxim 74:23be10c32fa3 982 break;
IanBenzMaxim 74:23be10c32fa3 983
IanBenzMaxim 75:8b627804927c 984 case Bps57600:
IanBenzMaxim 74:23be10c32fa3 985 _p_serial->baud(57600);
IanBenzMaxim 74:23be10c32fa3 986 break;
IanBenzMaxim 74:23be10c32fa3 987
IanBenzMaxim 75:8b627804927c 988 case Bps19200:
IanBenzMaxim 74:23be10c32fa3 989 _p_serial->baud(19200);
IanBenzMaxim 74:23be10c32fa3 990 break;
IanBenzMaxim 74:23be10c32fa3 991
IanBenzMaxim 75:8b627804927c 992 case Bps9600:
IanBenzMaxim 74:23be10c32fa3 993 default:
IanBenzMaxim 74:23be10c32fa3 994 _p_serial->baud(9600);
IanBenzMaxim 74:23be10c32fa3 995 break;
j3 63:422be898443a 996 }
j3 63:422be898443a 997 }
j3 63:422be898443a 998
j3 63:422be898443a 999
j3 63:422be898443a 1000 //*********************************************************************
IanBenzMaxim 73:2cecc1372acc 1001 int32_t DS2480B::bitacc(uint32_t op, uint32_t state, uint32_t loc, uint8_t *buf)
j3 63:422be898443a 1002 {
IanBenzMaxim 74:23be10c32fa3 1003 int nbyt, nbit;
j3 63:422be898443a 1004
j3 65:a28ac52ca127 1005 nbyt = (loc / 8);
j3 65:a28ac52ca127 1006 nbit = loc - (nbyt * 8);
j3 63:422be898443a 1007
IanBenzMaxim 74:23be10c32fa3 1008 if (op == WRITE_FUNCTION)
j3 65:a28ac52ca127 1009 {
j3 65:a28ac52ca127 1010 if (state)
j3 65:a28ac52ca127 1011 {
j3 65:a28ac52ca127 1012 buf[nbyt] |= (0x01 << nbit);
j3 65:a28ac52ca127 1013 }
j3 65:a28ac52ca127 1014 else
j3 65:a28ac52ca127 1015 {
j3 65:a28ac52ca127 1016 buf[nbyt] &= ~(0x01 << nbit);
j3 65:a28ac52ca127 1017 }
j3 63:422be898443a 1018
j3 65:a28ac52ca127 1019 return 1;
IanBenzMaxim 74:23be10c32fa3 1020 }
j3 65:a28ac52ca127 1021 else
j3 65:a28ac52ca127 1022 {
j3 65:a28ac52ca127 1023 return ((buf[nbyt] >> nbit) & 0x01);
j3 65:a28ac52ca127 1024 }
IanBenzMaxim 71:562f5c702094 1025 }