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 May 13 07:48:35 2016 -0500
Revision:
74:23be10c32fa3
Parent:
73:2cecc1372acc
Child:
75:8b627804927c
Assimilated indentation and braces.

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
IanBenzMaxim 73:2cecc1372acc 33 #include "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 73:2cecc1372acc 186 using OneWire::Masters::DS2480B;
IanBenzMaxim 73:2cecc1372acc 187 using OneWire::Masters::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(PinName tx, PinName rx)
IanBenzMaxim 74:23be10c32fa3 198 :_p_serial(new mbed::Serial(tx, rx)), _serial_owner(true)
j3 3:644fc630f958 199 {
j3 3:644fc630f958 200 }
j3 3:644fc630f958 201
j3 3:644fc630f958 202
j3 3:644fc630f958 203 //*********************************************************************
IanBenzMaxim 73:2cecc1372acc 204 DS2480B::~DS2480B()
j3 3:644fc630f958 205 {
IanBenzMaxim 74:23be10c32fa3 206 if (_serial_owner)
j3 3:644fc630f958 207 {
j3 3:644fc630f958 208 delete _p_serial;
j3 3:644fc630f958 209 }
j3 3:644fc630f958 210 }
j3 3:644fc630f958 211
j3 3:644fc630f958 212
j3 3:644fc630f958 213 //*********************************************************************
IanBenzMaxim 73:2cecc1372acc 214 void DS2480B::rx_callback(void)
j3 63:422be898443a 215 {
IanBenzMaxim 74:23be10c32fa3 216 while (_p_serial->readable())
j3 65:a28ac52ca127 217 {
j3 65:a28ac52ca127 218 rx_buffer.buff[rx_buffer.w_idx++] = _p_serial->getc();
j3 65:a28ac52ca127 219 rx_buffer.rx_bytes_available++;
j3 65:a28ac52ca127 220 }
IanBenzMaxim 74:23be10c32fa3 221
IanBenzMaxim 74:23be10c32fa3 222 if (rx_buffer.w_idx == rx_buffer.r_idx)
j3 63:422be898443a 223 {
j3 63:422be898443a 224 rx_buffer.wrap_error = true;
j3 63:422be898443a 225 }
j3 63:422be898443a 226 }
j3 63:422be898443a 227
j3 63:422be898443a 228
j3 63:422be898443a 229 //*********************************************************************
IanBenzMaxim 73:2cecc1372acc 230 OneWireMaster::CmdResult DS2480B::OWInitMaster(void)
j3 14:7b2886a50321 231 {
IanBenzMaxim 73:2cecc1372acc 232 _p_serial->attach(this, &DS2480B::rx_callback, mbed::Serial::RxIrq);
IanBenzMaxim 74:23be10c32fa3 233
j3 63:422be898443a 234 rx_buffer.w_idx = 0;
j3 63:422be898443a 235 rx_buffer.r_idx = 0;
j3 65:a28ac52ca127 236 rx_buffer.rx_bytes_available = 0;
j3 63:422be898443a 237 rx_buffer.wrap_error = false;
IanBenzMaxim 74:23be10c32fa3 238
j3 63:422be898443a 239 _ULevel = OneWireMaster::LEVEL_NORMAL;
j3 63:422be898443a 240 _UBaud = BPS_9600;
j3 63:422be898443a 241 _UMode = MODSEL_COMMAND;
j3 63:422be898443a 242 _USpeed = OneWireMaster::SPEED_STANDARD;
IanBenzMaxim 74:23be10c32fa3 243
j3 63:422be898443a 244 return DS2480B_Detect();
j3 14:7b2886a50321 245 }
j3 14:7b2886a50321 246
j3 14:7b2886a50321 247
j3 14:7b2886a50321 248 //*********************************************************************
IanBenzMaxim 73:2cecc1372acc 249 OneWireMaster::CmdResult DS2480B::OWReset(void)
j3 3:644fc630f958 250 {
j3 63:422be898443a 251 OneWireMaster::CmdResult result;
j3 63:422be898443a 252
IanBenzMaxim 74:23be10c32fa3 253 uint8_t readbuffer[10], sendpacket[10];
IanBenzMaxim 74:23be10c32fa3 254 uint8_t sendlen = 0;
j3 63:422be898443a 255
j3 63:422be898443a 256 // make sure normal level
j3 63:422be898443a 257 result = OWSetLevel(OneWireMaster::LEVEL_NORMAL);
IanBenzMaxim 74:23be10c32fa3 258 if (result == OneWireMaster::Success)
j3 63:422be898443a 259 {
j3 63:422be898443a 260 // check for correct mode
IanBenzMaxim 74:23be10c32fa3 261 if (_UMode != MODSEL_COMMAND)
j3 63:422be898443a 262 {
j3 63:422be898443a 263 _UMode = MODSEL_COMMAND;
j3 63:422be898443a 264 sendpacket[sendlen++] = MODE_COMMAND;
j3 63:422be898443a 265 }
j3 63:422be898443a 266
j3 63:422be898443a 267 // construct the command
j3 63:422be898443a 268 sendpacket[sendlen++] = (uint8_t)(CMD_COMM | FUNCTSEL_RESET | _USpeed);
IanBenzMaxim 74:23be10c32fa3 269
j3 63:422be898443a 270 FlushCOM();
j3 63:422be898443a 271
j3 63:422be898443a 272 // send the packet
IanBenzMaxim 74:23be10c32fa3 273 result = WriteCOM(sendlen, sendpacket);
IanBenzMaxim 74:23be10c32fa3 274 if (result == OneWireMaster::Success)
j3 63:422be898443a 275 {
j3 63:422be898443a 276 // read back the 1 byte response
IanBenzMaxim 74:23be10c32fa3 277 result = ReadCOM(1, readbuffer);
IanBenzMaxim 74:23be10c32fa3 278 if (result == OneWireMaster::Success)
j3 63:422be898443a 279 {
j3 63:422be898443a 280 // make sure this byte looks like a reset byte
IanBenzMaxim 74:23be10c32fa3 281 if (((readbuffer[0] & RB_RESET_MASK) == RB_PRESENCE) || ((readbuffer[0] & RB_RESET_MASK) == RB_ALARMPRESENCE))
j3 63:422be898443a 282 {
j3 63:422be898443a 283 result = OneWireMaster::Success;
j3 63:422be898443a 284 }
j3 63:422be898443a 285 else
j3 63:422be898443a 286 {
j3 63:422be898443a 287 result = OneWireMaster::OperationFailure;
j3 63:422be898443a 288 }
j3 63:422be898443a 289 }
j3 63:422be898443a 290 }
IanBenzMaxim 74:23be10c32fa3 291
IanBenzMaxim 74:23be10c32fa3 292 if (result != OneWireMaster::Success)
j3 63:422be898443a 293 {
j3 63:422be898443a 294 // an error occured so re-sync with DS2480B
j3 63:422be898443a 295 DS2480B_Detect();
j3 63:422be898443a 296 }
j3 63:422be898443a 297 }
j3 63:422be898443a 298
j3 17:b646b1e3970b 299 return result;
j3 3:644fc630f958 300 }
j3 3:644fc630f958 301
j3 3:644fc630f958 302
j3 63:422be898443a 303 //*********************************************************************
IanBenzMaxim 73:2cecc1372acc 304 OneWireMaster::CmdResult DS2480B::OWTouchBitSetLevel(uint8_t & sendrecvbit, OWLevel after_level)
j3 3:644fc630f958 305 {
j3 23:e8e403d61359 306 OneWireMaster::CmdResult result = OneWireMaster::OperationFailure;
j3 63:422be898443a 307
IanBenzMaxim 74:23be10c32fa3 308 uint8_t readbuffer[10], sendpacket[10];
IanBenzMaxim 74:23be10c32fa3 309 uint8_t sendlen = 0;
j3 63:422be898443a 310
j3 63:422be898443a 311 // make sure normal level
j3 63:422be898443a 312 OWSetLevel(OneWireMaster::LEVEL_NORMAL);
j3 63:422be898443a 313
j3 63:422be898443a 314 // check for correct mode
IanBenzMaxim 74:23be10c32fa3 315 if (_UMode != MODSEL_COMMAND)
j3 63:422be898443a 316 {
j3 63:422be898443a 317 _UMode = MODSEL_COMMAND;
j3 63:422be898443a 318 sendpacket[sendlen++] = MODE_COMMAND;
j3 63:422be898443a 319 }
j3 63:422be898443a 320
j3 63:422be898443a 321 // construct the command
j3 63:422be898443a 322 sendpacket[sendlen] = (sendrecvbit != 0) ? BITPOL_ONE : BITPOL_ZERO;
j3 63:422be898443a 323 sendpacket[sendlen++] |= CMD_COMM | FUNCTSEL_BIT | _USpeed;
j3 63:422be898443a 324
j3 63:422be898443a 325 // flush the buffers
j3 63:422be898443a 326 FlushCOM();
j3 63:422be898443a 327
j3 63:422be898443a 328 // send the packet
IanBenzMaxim 74:23be10c32fa3 329 result = WriteCOM(sendlen, sendpacket);
IanBenzMaxim 74:23be10c32fa3 330 if (result == OneWireMaster::Success)
j3 63:422be898443a 331 {
j3 63:422be898443a 332 // read back the response
IanBenzMaxim 74:23be10c32fa3 333 result = ReadCOM(1, readbuffer);
IanBenzMaxim 74:23be10c32fa3 334 if (result == OneWireMaster::Success)
j3 63:422be898443a 335 {
j3 63:422be898443a 336 // interpret the response
j3 63:422be898443a 337 if (((readbuffer[0] & 0xE0) == 0x80) && ((readbuffer[0] & RB_BIT_MASK) == RB_BIT_ONE))
j3 63:422be898443a 338 {
j3 63:422be898443a 339 sendrecvbit = 1;
j3 63:422be898443a 340 result = OneWireMaster::Success;
j3 63:422be898443a 341 }
j3 63:422be898443a 342 else
j3 63:422be898443a 343 {
j3 63:422be898443a 344 sendrecvbit = 0;
j3 63:422be898443a 345 result = OneWireMaster::Success;
j3 63:422be898443a 346 }
j3 63:422be898443a 347 }
j3 63:422be898443a 348 else
j3 63:422be898443a 349 {
j3 63:422be898443a 350 result = OneWireMaster::CommunicationReadError;
j3 63:422be898443a 351 }
j3 63:422be898443a 352 }
j3 63:422be898443a 353 else
j3 63:422be898443a 354 {
j3 63:422be898443a 355 result = OneWireMaster::CommunicationWriteError;
j3 63:422be898443a 356 }
j3 63:422be898443a 357
j3 63:422be898443a 358 // an error occured so re-sync with DS2480B
IanBenzMaxim 74:23be10c32fa3 359 if (result != OneWireMaster::Success)
j3 63:422be898443a 360 {
j3 63:422be898443a 361 DS2480B_Detect();
j3 63:422be898443a 362 }
j3 63:422be898443a 363 else
j3 63:422be898443a 364 {
IanBenzMaxim 74:23be10c32fa3 365 result = OWSetLevel(after_level);
j3 63:422be898443a 366 }
j3 63:422be898443a 367
j3 17:b646b1e3970b 368 return result;
j3 3:644fc630f958 369 }
j3 3:644fc630f958 370
j3 3:644fc630f958 371
j3 3:644fc630f958 372 //*********************************************************************
IanBenzMaxim 73:2cecc1372acc 373 OneWireMaster::CmdResult DS2480B::OWWriteByteSetLevel(uint8_t sendbyte, OWLevel after_level)
IanBenzMaxim 26:a361e3f42ba5 374 {
IanBenzMaxim 26:a361e3f42ba5 375 OneWireMaster::CmdResult result = OneWireMaster::OperationFailure;
j3 63:422be898443a 376
IanBenzMaxim 74:23be10c32fa3 377 uint8_t readbuffer[10], sendpacket[10];
IanBenzMaxim 74:23be10c32fa3 378 uint8_t sendlen = 0;
j3 63:422be898443a 379
j3 63:422be898443a 380 // make sure normal level
j3 63:422be898443a 381 OWSetLevel(OneWireMaster::LEVEL_NORMAL);
j3 63:422be898443a 382
j3 63:422be898443a 383 // check for correct mode
IanBenzMaxim 74:23be10c32fa3 384 if (_UMode != MODSEL_DATA)
j3 63:422be898443a 385 {
j3 63:422be898443a 386 _UMode = MODSEL_DATA;
j3 63:422be898443a 387 sendpacket[sendlen++] = MODE_DATA;
j3 63:422be898443a 388 }
j3 63:422be898443a 389
j3 63:422be898443a 390 // add the byte to send
j3 63:422be898443a 391 sendpacket[sendlen++] = sendbyte;
j3 63:422be898443a 392
j3 63:422be898443a 393 // check for duplication of data that looks like COMMAND mode
IanBenzMaxim 74:23be10c32fa3 394 if (sendbyte == MODE_COMMAND)
j3 63:422be898443a 395 {
j3 63:422be898443a 396 sendpacket[sendlen++] = sendbyte;
j3 63:422be898443a 397 }
j3 63:422be898443a 398
j3 63:422be898443a 399 // flush the buffers
j3 63:422be898443a 400 FlushCOM();
j3 63:422be898443a 401
j3 63:422be898443a 402 // send the packet
IanBenzMaxim 74:23be10c32fa3 403 result = WriteCOM(sendlen, sendpacket);
IanBenzMaxim 74:23be10c32fa3 404 if (result == OneWireMaster::Success)
j3 63:422be898443a 405 {
j3 63:422be898443a 406 // read back the 1 byte response
IanBenzMaxim 74:23be10c32fa3 407 result = ReadCOM(1, readbuffer);
IanBenzMaxim 74:23be10c32fa3 408 if ((result == OneWireMaster::Success) && (readbuffer[0] == sendbyte))
j3 63:422be898443a 409 {
j3 63:422be898443a 410 result = OneWireMaster::Success;
j3 63:422be898443a 411 }
j3 63:422be898443a 412 else
j3 63:422be898443a 413 {
j3 63:422be898443a 414 result = OneWireMaster::CommunicationReadError;
j3 63:422be898443a 415 }
j3 63:422be898443a 416 }
j3 63:422be898443a 417 else
j3 63:422be898443a 418 {
j3 63:422be898443a 419 result = OneWireMaster::CommunicationWriteError;
j3 63:422be898443a 420 }
j3 63:422be898443a 421
j3 63:422be898443a 422 // an error occured so re-sync with DS2480B
IanBenzMaxim 74:23be10c32fa3 423 if (result != OneWireMaster::Success)
j3 63:422be898443a 424 {
j3 63:422be898443a 425 DS2480B_Detect();
j3 63:422be898443a 426 }
j3 63:422be898443a 427 else
j3 63:422be898443a 428 {
IanBenzMaxim 74:23be10c32fa3 429 result = OWSetLevel(after_level);
j3 63:422be898443a 430 }
j3 63:422be898443a 431
IanBenzMaxim 26:a361e3f42ba5 432 return result;
IanBenzMaxim 26:a361e3f42ba5 433 }
IanBenzMaxim 26:a361e3f42ba5 434
IanBenzMaxim 26:a361e3f42ba5 435
IanBenzMaxim 26:a361e3f42ba5 436 //*********************************************************************
IanBenzMaxim 73:2cecc1372acc 437 OneWireMaster::CmdResult DS2480B::OWReadByteSetLevel(uint8_t & recvbyte, OWLevel after_level)
j3 3:644fc630f958 438 {
j3 23:e8e403d61359 439 OneWireMaster::CmdResult result = OneWireMaster::OperationFailure;
j3 63:422be898443a 440
IanBenzMaxim 74:23be10c32fa3 441 uint8_t readbuffer[10], sendpacket[10];
IanBenzMaxim 74:23be10c32fa3 442 uint8_t sendlen = 0;
j3 63:422be898443a 443
j3 63:422be898443a 444 // make sure normal level
j3 63:422be898443a 445 OWSetLevel(OneWireMaster::LEVEL_NORMAL);
j3 63:422be898443a 446
j3 63:422be898443a 447 // check for correct mode
IanBenzMaxim 74:23be10c32fa3 448 if (_UMode != MODSEL_DATA)
j3 63:422be898443a 449 {
j3 63:422be898443a 450 _UMode = MODSEL_DATA;
j3 63:422be898443a 451 sendpacket[sendlen++] = MODE_DATA;
j3 63:422be898443a 452 }
j3 63:422be898443a 453
j3 63:422be898443a 454 // add the byte to send
j3 63:422be898443a 455 sendpacket[sendlen++] = 0xFF;
j3 63:422be898443a 456
j3 63:422be898443a 457 // flush the buffers
j3 63:422be898443a 458 FlushCOM();
j3 63:422be898443a 459
j3 63:422be898443a 460 // send the packet
IanBenzMaxim 74:23be10c32fa3 461 result = WriteCOM(sendlen, sendpacket);
IanBenzMaxim 74:23be10c32fa3 462 if (result == OneWireMaster::Success)
j3 63:422be898443a 463 {
j3 63:422be898443a 464 // read back the 1 byte response
IanBenzMaxim 74:23be10c32fa3 465 result = ReadCOM(1, readbuffer);
IanBenzMaxim 74:23be10c32fa3 466 if (result == OneWireMaster::Success)
j3 63:422be898443a 467 {
j3 63:422be898443a 468 recvbyte = readbuffer[0];
j3 63:422be898443a 469 result = OneWireMaster::Success;
j3 63:422be898443a 470 }
j3 63:422be898443a 471 else
j3 63:422be898443a 472 {
j3 63:422be898443a 473 result = OneWireMaster::CommunicationReadError;
j3 63:422be898443a 474 }
j3 63:422be898443a 475 }
j3 63:422be898443a 476 else
j3 63:422be898443a 477 {
j3 63:422be898443a 478 result = OneWireMaster::CommunicationWriteError;
j3 63:422be898443a 479 }
j3 63:422be898443a 480
j3 63:422be898443a 481 // an error occured so re-sync with DS2480B
IanBenzMaxim 74:23be10c32fa3 482 if (result != OneWireMaster::Success)
j3 63:422be898443a 483 {
j3 63:422be898443a 484 DS2480B_Detect();
j3 63:422be898443a 485 }
j3 63:422be898443a 486 else
j3 63:422be898443a 487 {
IanBenzMaxim 74:23be10c32fa3 488 result = OWSetLevel(after_level);
j3 63:422be898443a 489 }
j3 63:422be898443a 490
j3 17:b646b1e3970b 491 return result;
j3 3:644fc630f958 492 }
j3 3:644fc630f958 493
j3 3:644fc630f958 494
j3 3:644fc630f958 495 //*********************************************************************
IanBenzMaxim 73:2cecc1372acc 496 OneWireMaster::CmdResult DS2480B::OWSetSpeed(OWSpeed new_speed)
j3 17:b646b1e3970b 497 {
j3 23:e8e403d61359 498 OneWireMaster::CmdResult result = OneWireMaster::OperationFailure;
IanBenzMaxim 74:23be10c32fa3 499
j3 17:b646b1e3970b 500 //TODO
IanBenzMaxim 74:23be10c32fa3 501
j3 17:b646b1e3970b 502 return result;
j3 3:644fc630f958 503 }
j3 3:644fc630f958 504
j3 3:644fc630f958 505
j3 3:644fc630f958 506 //*********************************************************************
IanBenzMaxim 73:2cecc1372acc 507 OneWireMaster::CmdResult DS2480B::OWSetLevel(OWLevel new_level)
j3 3:644fc630f958 508 {
j3 63:422be898443a 509 OneWireMaster::CmdResult result = OneWireMaster::Success;
j3 63:422be898443a 510
IanBenzMaxim 74:23be10c32fa3 511 uint8_t sendpacket[10], readbuffer[10];
IanBenzMaxim 74:23be10c32fa3 512 uint8_t sendlen = 0;
j3 63:422be898443a 513
j3 63:422be898443a 514 // check if need to change level
IanBenzMaxim 74:23be10c32fa3 515 if (new_level != _ULevel)
j3 63:422be898443a 516 {
j3 63:422be898443a 517 // check for correct mode
IanBenzMaxim 74:23be10c32fa3 518 if (_UMode != MODSEL_COMMAND)
j3 63:422be898443a 519 {
j3 63:422be898443a 520 _UMode = MODSEL_COMMAND;
j3 63:422be898443a 521 sendpacket[sendlen++] = MODE_COMMAND;
j3 63:422be898443a 522 }
IanBenzMaxim 74:23be10c32fa3 523
j3 63:422be898443a 524 // check if just putting back to normal
IanBenzMaxim 74:23be10c32fa3 525 if (new_level == OneWireMaster::LEVEL_NORMAL)
j3 63:422be898443a 526 {
j3 63:422be898443a 527 // stop pulse command
j3 63:422be898443a 528 sendpacket[sendlen++] = MODE_STOP_PULSE;
j3 63:422be898443a 529
j3 63:422be898443a 530 // add the command to begin the pulse WITHOUT prime
j3 63:422be898443a 531 sendpacket[sendlen++] = CMD_COMM | FUNCTSEL_CHMOD | SPEEDSEL_PULSE | BITPOL_5V | PRIME5V_FALSE;
j3 63:422be898443a 532
j3 63:422be898443a 533 // stop pulse command
j3 63:422be898443a 534 sendpacket[sendlen++] = MODE_STOP_PULSE;
IanBenzMaxim 74:23be10c32fa3 535
j3 63:422be898443a 536 FlushCOM();
j3 63:422be898443a 537
j3 63:422be898443a 538 // send the packet
IanBenzMaxim 74:23be10c32fa3 539 result = WriteCOM(sendlen, sendpacket);
IanBenzMaxim 74:23be10c32fa3 540 if (result == OneWireMaster::Success)
j3 63:422be898443a 541 {
j3 63:422be898443a 542 // read back the 1 byte response
IanBenzMaxim 74:23be10c32fa3 543 result = ReadCOM(2, readbuffer);
IanBenzMaxim 74:23be10c32fa3 544 if (result == OneWireMaster::Success)
j3 63:422be898443a 545 {
j3 63:422be898443a 546 // check response byte
IanBenzMaxim 74:23be10c32fa3 547 if (((readbuffer[0] & 0xE0) == 0xE0) && ((readbuffer[1] & 0xE0) == 0xE0))
j3 63:422be898443a 548 {
j3 63:422be898443a 549 _ULevel = OneWireMaster::LEVEL_NORMAL;
j3 63:422be898443a 550 }
j3 63:422be898443a 551 else
j3 63:422be898443a 552 {
j3 63:422be898443a 553 result = OneWireMaster::OperationFailure;
j3 63:422be898443a 554 }
j3 63:422be898443a 555 }
j3 63:422be898443a 556 }
j3 63:422be898443a 557 }
j3 63:422be898443a 558 // set new level
IanBenzMaxim 74:23be10c32fa3 559 else
j3 63:422be898443a 560 {
j3 63:422be898443a 561 // set the SPUD time value
j3 63:422be898443a 562 sendpacket[sendlen++] = CMD_CONFIG | PARMSEL_5VPULSE | PARMSET_infinite;
j3 63:422be898443a 563 // add the command to begin the pulse
j3 63:422be898443a 564 sendpacket[sendlen++] = CMD_COMM | FUNCTSEL_CHMOD | SPEEDSEL_PULSE | BITPOL_5V;
IanBenzMaxim 74:23be10c32fa3 565
j3 63:422be898443a 566 FlushCOM();
j3 63:422be898443a 567
j3 63:422be898443a 568 // send the packet
IanBenzMaxim 74:23be10c32fa3 569 result = WriteCOM(sendlen, sendpacket);
IanBenzMaxim 74:23be10c32fa3 570 if (result == OneWireMaster::Success)
j3 63:422be898443a 571 {
j3 63:422be898443a 572 // read back the 1 byte response from setting time limit
IanBenzMaxim 74:23be10c32fa3 573 result = ReadCOM(1, readbuffer);
IanBenzMaxim 74:23be10c32fa3 574 if (result == OneWireMaster::Success)
j3 63:422be898443a 575 {
j3 63:422be898443a 576 // check response byte
IanBenzMaxim 74:23be10c32fa3 577 if ((readbuffer[0] & 0x81) == 0)
j3 63:422be898443a 578 {
j3 63:422be898443a 579 _ULevel = new_level;
j3 63:422be898443a 580 }
j3 63:422be898443a 581 else
j3 63:422be898443a 582 {
j3 63:422be898443a 583 result = OneWireMaster::OperationFailure;
j3 63:422be898443a 584 }
j3 63:422be898443a 585 }
j3 63:422be898443a 586 }
j3 63:422be898443a 587 }
j3 63:422be898443a 588
j3 63:422be898443a 589 // if lost communication with DS2480B then reset
IanBenzMaxim 74:23be10c32fa3 590 if (result != OneWireMaster::Success)
j3 63:422be898443a 591 {
j3 63:422be898443a 592 DS2480B_Detect();
j3 63:422be898443a 593 }
IanBenzMaxim 74:23be10c32fa3 594
j3 63:422be898443a 595 }
j3 63:422be898443a 596
j3 63:422be898443a 597 return result;
j3 63:422be898443a 598 }
j3 63:422be898443a 599
j3 63:422be898443a 600
j3 63:422be898443a 601 //*********************************************************************
IanBenzMaxim 73:2cecc1372acc 602 OneWireMaster::CmdResult DS2480B::DS2480B_Detect(void)
j3 63:422be898443a 603 {
j3 63:422be898443a 604 OneWireMaster::CmdResult result;
j3 63:422be898443a 605
IanBenzMaxim 74:23be10c32fa3 606 uint8_t sendpacket[10], readbuffer[10];
IanBenzMaxim 74:23be10c32fa3 607 uint8_t sendlen = 0;
j3 63:422be898443a 608
j3 63:422be898443a 609 // reset modes
j3 63:422be898443a 610 _UMode = MODSEL_COMMAND;
j3 63:422be898443a 611 _UBaud = BPS_9600;
j3 63:422be898443a 612 _USpeed = SPEEDSEL_FLEX;
j3 63:422be898443a 613
j3 63:422be898443a 614 // set the baud rate to 9600
j3 63:422be898443a 615 SetBaudCOM(_UBaud);
j3 63:422be898443a 616
j3 63:422be898443a 617 // send a break to reset the DS2480B
j3 63:422be898443a 618 BreakCOM();
j3 63:422be898443a 619
j3 63:422be898443a 620 // delay to let line settle
j3 63:422be898443a 621 wait_ms(2);
IanBenzMaxim 74:23be10c32fa3 622
j3 63:422be898443a 623 FlushCOM();
j3 63:422be898443a 624
j3 63:422be898443a 625 // send the timing byte
j3 63:422be898443a 626 sendpacket[0] = 0xC1;
IanBenzMaxim 74:23be10c32fa3 627 result = WriteCOM(1, sendpacket);
IanBenzMaxim 74:23be10c32fa3 628 if (result == OneWireMaster::Success)
j3 63:422be898443a 629 {
j3 63:422be898443a 630 // delay to let line settle
j3 63:422be898443a 631 wait_ms(2);
j3 63:422be898443a 632
j3 63:422be898443a 633 // set the FLEX configuration parameters
j3 63:422be898443a 634 // default PDSRC = 1.37Vus
j3 63:422be898443a 635 sendpacket[sendlen++] = CMD_CONFIG | PARMSEL_SLEW | PARMSET_Slew1p37Vus;
j3 63:422be898443a 636 // default W1LT = 10us
j3 63:422be898443a 637 sendpacket[sendlen++] = CMD_CONFIG | PARMSEL_WRITE1LOW | PARMSET_Write10us;
j3 63:422be898443a 638 // default DSO/WORT = 8us
j3 63:422be898443a 639 sendpacket[sendlen++] = CMD_CONFIG | PARMSEL_SAMPLEOFFSET | PARMSET_SampOff8us;
j3 63:422be898443a 640
j3 63:422be898443a 641 // construct the command to read the baud rate (to test command block)
j3 63:422be898443a 642 sendpacket[sendlen++] = CMD_CONFIG | PARMSEL_PARMREAD | (PARMSEL_BAUDRATE >> 3);
j3 63:422be898443a 643
j3 63:422be898443a 644 // also do 1 bit operation (to test 1-Wire block)
j3 63:422be898443a 645 sendpacket[sendlen++] = CMD_COMM | FUNCTSEL_BIT | _UBaud | BITPOL_ONE;
IanBenzMaxim 74:23be10c32fa3 646
j3 63:422be898443a 647 FlushCOM();
j3 63:422be898443a 648
j3 63:422be898443a 649 // send the packet
IanBenzMaxim 74:23be10c32fa3 650 result = WriteCOM(sendlen, sendpacket);
IanBenzMaxim 74:23be10c32fa3 651 if (result == OneWireMaster::Success)
j3 63:422be898443a 652 {
j3 63:422be898443a 653 // read back the response
IanBenzMaxim 74:23be10c32fa3 654 result = ReadCOM(5, readbuffer);
IanBenzMaxim 74:23be10c32fa3 655 if (result == OneWireMaster::Success)
j3 63:422be898443a 656 {
j3 63:422be898443a 657 // look at the baud rate and bit operation
j3 63:422be898443a 658 // to see if the response makes sense
IanBenzMaxim 74:23be10c32fa3 659 if (((readbuffer[3] & 0xF1) == 0x00) && ((readbuffer[3] & 0x0E) == _UBaud) && ((readbuffer[4] & 0xF0) == 0x90) && ((readbuffer[4] & 0x0C) == _UBaud))
j3 63:422be898443a 660 {
j3 63:422be898443a 661 result = OneWireMaster::Success;
j3 63:422be898443a 662 }
j3 63:422be898443a 663 else
j3 63:422be898443a 664 {
j3 63:422be898443a 665 result = OneWireMaster::OperationFailure;
j3 63:422be898443a 666 }
j3 63:422be898443a 667 }
j3 63:422be898443a 668 }
j3 63:422be898443a 669 }
IanBenzMaxim 74:23be10c32fa3 670
j3 63:422be898443a 671 return result;
j3 63:422be898443a 672 }
j3 63:422be898443a 673
j3 63:422be898443a 674
j3 63:422be898443a 675 //*********************************************************************
IanBenzMaxim 73:2cecc1372acc 676 OneWireMaster::CmdResult DS2480B::DS2480B_ChangeBaud(DS2480B_BPS newbaud)
j3 63:422be898443a 677 {
j3 65:a28ac52ca127 678 OneWireMaster::CmdResult result = OneWireMaster::Success;
j3 65:a28ac52ca127 679
IanBenzMaxim 74:23be10c32fa3 680 uint8_t readbuffer[5], sendpacket[5], sendpacket2[5];
IanBenzMaxim 74:23be10c32fa3 681 uint8_t sendlen = 0, sendlen2 = 0;
j3 65:a28ac52ca127 682
j3 65:a28ac52ca127 683 //see if diffenent then current baud rate
IanBenzMaxim 74:23be10c32fa3 684 if (_UBaud != newbaud)
j3 65:a28ac52ca127 685 {
j3 65:a28ac52ca127 686 // build the command packet
j3 65:a28ac52ca127 687 // check for correct mode
IanBenzMaxim 74:23be10c32fa3 688 if (_UMode != MODSEL_COMMAND)
j3 65:a28ac52ca127 689 {
j3 65:a28ac52ca127 690 _UMode = MODSEL_COMMAND;
j3 65:a28ac52ca127 691 sendpacket[sendlen++] = MODE_COMMAND;
j3 65:a28ac52ca127 692 }
j3 65:a28ac52ca127 693 // build the command
j3 65:a28ac52ca127 694 sendpacket[sendlen++] = CMD_CONFIG | PARMSEL_BAUDRATE | newbaud;
j3 63:422be898443a 695
j3 65:a28ac52ca127 696 // flush the buffers
j3 65:a28ac52ca127 697 FlushCOM();
j3 65:a28ac52ca127 698
j3 65:a28ac52ca127 699 // send the packet
IanBenzMaxim 74:23be10c32fa3 700 result = WriteCOM(sendlen, sendpacket);
IanBenzMaxim 74:23be10c32fa3 701 if (result == OneWireMaster::Success)
j3 65:a28ac52ca127 702 {
j3 65:a28ac52ca127 703 // make sure buffer is flushed
j3 65:a28ac52ca127 704 wait_ms(5);
j3 63:422be898443a 705
j3 65:a28ac52ca127 706 // change our baud rate
j3 65:a28ac52ca127 707 SetBaudCOM(newbaud);
j3 65:a28ac52ca127 708 _UBaud = newbaud;
j3 65:a28ac52ca127 709
j3 65:a28ac52ca127 710 // wait for things to settle
j3 65:a28ac52ca127 711 wait_ms(5);
j3 65:a28ac52ca127 712
j3 65:a28ac52ca127 713 // build a command packet to read back baud rate
j3 65:a28ac52ca127 714 sendpacket2[sendlen2++] = CMD_CONFIG | PARMSEL_PARMREAD | (PARMSEL_BAUDRATE >> 3);
j3 65:a28ac52ca127 715
j3 65:a28ac52ca127 716 // flush the buffers
j3 65:a28ac52ca127 717 FlushCOM();
j3 63:422be898443a 718
j3 65:a28ac52ca127 719 // send the packet
IanBenzMaxim 74:23be10c32fa3 720 result = WriteCOM(sendlen2, sendpacket2);
IanBenzMaxim 74:23be10c32fa3 721 if (result == OneWireMaster::Success)
j3 63:422be898443a 722 {
j3 65:a28ac52ca127 723 // read back the 1 byte response
IanBenzMaxim 74:23be10c32fa3 724 result = ReadCOM(1, readbuffer);
IanBenzMaxim 74:23be10c32fa3 725 if (result == OneWireMaster::Success)
j3 65:a28ac52ca127 726 {
j3 65:a28ac52ca127 727 // verify correct baud
IanBenzMaxim 74:23be10c32fa3 728 if ((readbuffer[0] & 0x0E) == (sendpacket[sendlen - 1] & 0x0E))
j3 65:a28ac52ca127 729 {
j3 65:a28ac52ca127 730 result = OneWireMaster::Success;
j3 65:a28ac52ca127 731 }
j3 65:a28ac52ca127 732 else
j3 65:a28ac52ca127 733 {
IanBenzMaxim 74:23be10c32fa3 734 result = OneWireMaster::OperationFailure;
j3 65:a28ac52ca127 735 }
j3 65:a28ac52ca127 736 }
j3 65:a28ac52ca127 737 else
j3 65:a28ac52ca127 738 {
j3 65:a28ac52ca127 739 result = OneWireMaster::CommunicationReadError;
j3 65:a28ac52ca127 740 }
j3 63:422be898443a 741 }
j3 65:a28ac52ca127 742 else
j3 65:a28ac52ca127 743 {
j3 65:a28ac52ca127 744 result = OneWireMaster::CommunicationWriteError;
j3 65:a28ac52ca127 745 }
j3 65:a28ac52ca127 746 }
j3 65:a28ac52ca127 747 else
j3 65:a28ac52ca127 748 {
j3 65:a28ac52ca127 749 result = OneWireMaster::CommunicationWriteError;
j3 65:a28ac52ca127 750 }
j3 65:a28ac52ca127 751 }
IanBenzMaxim 74:23be10c32fa3 752
j3 65:a28ac52ca127 753 // if lost communication with DS2480B then reset
IanBenzMaxim 74:23be10c32fa3 754 if (result != OneWireMaster::Success)
j3 65:a28ac52ca127 755 {
j3 65:a28ac52ca127 756 DS2480B_Detect();
j3 65:a28ac52ca127 757 }
IanBenzMaxim 74:23be10c32fa3 758
j3 65:a28ac52ca127 759 return result;
j3 63:422be898443a 760 }
j3 63:422be898443a 761
j3 63:422be898443a 762
j3 63:422be898443a 763 //*********************************************************************
IanBenzMaxim 73:2cecc1372acc 764 OneWireMaster::CmdResult DS2480B::WriteCOM(uint32_t outlen, uint8_t *outbuf)
j3 63:422be898443a 765 {
j3 23:e8e403d61359 766 OneWireMaster::CmdResult result = OneWireMaster::OperationFailure;
IanBenzMaxim 74:23be10c32fa3 767
IanBenzMaxim 73:2cecc1372acc 768 mbed::Timer t;
j3 63:422be898443a 769 uint32_t t_val;
j3 63:422be898443a 770 uint32_t timeout;
j3 63:422be898443a 771 uint8_t idx = 0;
IanBenzMaxim 74:23be10c32fa3 772
j3 63:422be898443a 773 //calculate timeout, the time needed to tx 1 byte
j3 65:a28ac52ca127 774 //double for 115200 due to timer object inaccuracies
IanBenzMaxim 74:23be10c32fa3 775 switch (_UBaud)
j3 63:422be898443a 776 {
IanBenzMaxim 74:23be10c32fa3 777 case BPS_115200:
IanBenzMaxim 74:23be10c32fa3 778 timeout = ((1000000 / 115200) * 20);
IanBenzMaxim 74:23be10c32fa3 779 break;
IanBenzMaxim 74:23be10c32fa3 780
IanBenzMaxim 74:23be10c32fa3 781 case BPS_57600:
IanBenzMaxim 74:23be10c32fa3 782 timeout = ((1000000 / 57600) * 10);
IanBenzMaxim 74:23be10c32fa3 783 break;
IanBenzMaxim 74:23be10c32fa3 784
IanBenzMaxim 74:23be10c32fa3 785 case BPS_19200:
IanBenzMaxim 74:23be10c32fa3 786 timeout = ((1000000 / 19200) * 10);
IanBenzMaxim 74:23be10c32fa3 787 break;
IanBenzMaxim 74:23be10c32fa3 788
IanBenzMaxim 74:23be10c32fa3 789 case BPS_9600:
IanBenzMaxim 74:23be10c32fa3 790 default:
IanBenzMaxim 74:23be10c32fa3 791 timeout = ((1000000 / 9600) * 10);
IanBenzMaxim 74:23be10c32fa3 792 break;
j3 63:422be898443a 793 }
IanBenzMaxim 74:23be10c32fa3 794
j3 63:422be898443a 795 t.start();
j3 63:422be898443a 796 do
j3 63:422be898443a 797 {
j3 63:422be898443a 798 t.reset();
j3 63:422be898443a 799 do
j3 63:422be898443a 800 {
j3 63:422be898443a 801 t_val = t.read_us();
IanBenzMaxim 74:23be10c32fa3 802 } while (!_p_serial->writeable() && (t_val < timeout));
IanBenzMaxim 74:23be10c32fa3 803
IanBenzMaxim 74:23be10c32fa3 804 if (t_val < timeout)
j3 63:422be898443a 805 {
j3 63:422be898443a 806 _p_serial->putc(outbuf[idx++]);
j3 63:422be898443a 807 result = OneWireMaster::Success;
j3 63:422be898443a 808 }
j3 63:422be898443a 809 else
j3 63:422be898443a 810 {
j3 63:422be898443a 811 result = OneWireMaster::TimeoutError;
j3 63:422be898443a 812 }
IanBenzMaxim 74:23be10c32fa3 813 } while ((idx < outlen) && (result == OneWireMaster::Success));
IanBenzMaxim 74:23be10c32fa3 814
j3 17:b646b1e3970b 815 return result;
j3 63:422be898443a 816 }
j3 63:422be898443a 817
j3 63:422be898443a 818
j3 63:422be898443a 819 //*********************************************************************
IanBenzMaxim 73:2cecc1372acc 820 OneWireMaster::CmdResult DS2480B::ReadCOM(uint32_t inlen, uint8_t *inbuf)
j3 63:422be898443a 821 {
j3 63:422be898443a 822 OneWireMaster::CmdResult result;
IanBenzMaxim 73:2cecc1372acc 823 mbed::Timer t;
IanBenzMaxim 74:23be10c32fa3 824 uint32_t num_bytes_read = 0;
j3 63:422be898443a 825 uint32_t timeout;
j3 65:a28ac52ca127 826 uint32_t micro_seconds;
IanBenzMaxim 74:23be10c32fa3 827
j3 65:a28ac52ca127 828 //calculate timeout, 10x the time needed to recieve inlen bytes
j3 65:a28ac52ca127 829 //double for 115200 due to timer object inaccuracies
IanBenzMaxim 74:23be10c32fa3 830 switch (_UBaud)
j3 63:422be898443a 831 {
IanBenzMaxim 74:23be10c32fa3 832 case BPS_115200:
IanBenzMaxim 74:23be10c32fa3 833 timeout = ((1000000 / 115200) * 200) * inlen;
IanBenzMaxim 74:23be10c32fa3 834 break;
IanBenzMaxim 74:23be10c32fa3 835
IanBenzMaxim 74:23be10c32fa3 836 case BPS_57600:
IanBenzMaxim 74:23be10c32fa3 837 timeout = ((1000000 / 57600) * 100) * inlen;
IanBenzMaxim 74:23be10c32fa3 838 break;
IanBenzMaxim 74:23be10c32fa3 839
IanBenzMaxim 74:23be10c32fa3 840 case BPS_19200:
IanBenzMaxim 74:23be10c32fa3 841 timeout = ((1000000 / 19200) * 100) * inlen;
IanBenzMaxim 74:23be10c32fa3 842 break;
IanBenzMaxim 74:23be10c32fa3 843
IanBenzMaxim 74:23be10c32fa3 844 case BPS_9600:
IanBenzMaxim 74:23be10c32fa3 845 default:
IanBenzMaxim 74:23be10c32fa3 846 timeout = ((1000000 / 9600) * 100) * inlen;
IanBenzMaxim 74:23be10c32fa3 847 break;
j3 63:422be898443a 848 }
IanBenzMaxim 74:23be10c32fa3 849
IanBenzMaxim 74:23be10c32fa3 850 if (rx_buffer.wrap_error)
j3 63:422be898443a 851 {
j3 63:422be898443a 852 //reset rx buffer, error, and return failure
j3 63:422be898443a 853 rx_buffer.w_idx = 0;
j3 63:422be898443a 854 rx_buffer.r_idx = 0;
j3 65:a28ac52ca127 855 rx_buffer.rx_bytes_available = 0;
j3 63:422be898443a 856 rx_buffer.wrap_error = false;
IanBenzMaxim 74:23be10c32fa3 857
j3 63:422be898443a 858 result = OneWireMaster::OperationFailure;
j3 63:422be898443a 859 }
j3 63:422be898443a 860 else
j3 63:422be898443a 861 {
j3 65:a28ac52ca127 862 t.start();
j3 65:a28ac52ca127 863 t.reset();
j3 65:a28ac52ca127 864 do
j3 63:422be898443a 865 {
j3 63:422be898443a 866 do
j3 63:422be898443a 867 {
j3 65:a28ac52ca127 868 micro_seconds = t.read_us();
IanBenzMaxim 74:23be10c32fa3 869 } while (!rx_buffer.rx_bytes_available && (micro_seconds < timeout));
IanBenzMaxim 74:23be10c32fa3 870
IanBenzMaxim 74:23be10c32fa3 871 if (rx_buffer.rx_bytes_available)
j3 63:422be898443a 872 {
j3 65:a28ac52ca127 873 inbuf[num_bytes_read++] = rx_buffer.buff[rx_buffer.r_idx++];
j3 65:a28ac52ca127 874 rx_buffer.rx_bytes_available--;
IanBenzMaxim 74:23be10c32fa3 875 }
IanBenzMaxim 74:23be10c32fa3 876 } while ((num_bytes_read < inlen) && (micro_seconds < timeout) && !rx_buffer.wrap_error);
j3 65:a28ac52ca127 877 t.stop();
IanBenzMaxim 74:23be10c32fa3 878
IanBenzMaxim 74:23be10c32fa3 879 if (num_bytes_read == inlen)
j3 65:a28ac52ca127 880 {
j3 65:a28ac52ca127 881 result = OneWireMaster::Success;
j3 65:a28ac52ca127 882 }
IanBenzMaxim 74:23be10c32fa3 883 else if (micro_seconds > timeout)
j3 65:a28ac52ca127 884 {
j3 65:a28ac52ca127 885 result = OneWireMaster::TimeoutError;
j3 63:422be898443a 886 }
j3 63:422be898443a 887 else
j3 63:422be898443a 888 {
j3 65:a28ac52ca127 889 //reset rx buffer, error, and return failure
j3 65:a28ac52ca127 890 rx_buffer.w_idx = 0;
j3 65:a28ac52ca127 891 rx_buffer.r_idx = 0;
j3 65:a28ac52ca127 892 rx_buffer.rx_bytes_available = 0;
j3 65:a28ac52ca127 893 rx_buffer.wrap_error = false;
IanBenzMaxim 74:23be10c32fa3 894
j3 65:a28ac52ca127 895 result = OneWireMaster::CommunicationReadError;
j3 63:422be898443a 896 }
j3 63:422be898443a 897 }
IanBenzMaxim 74:23be10c32fa3 898
j3 63:422be898443a 899 return result;
j3 63:422be898443a 900 }
j3 63:422be898443a 901
j3 63:422be898443a 902
j3 63:422be898443a 903 //*********************************************************************
IanBenzMaxim 73:2cecc1372acc 904 void DS2480B::BreakCOM(void)
j3 63:422be898443a 905 {
IanBenzMaxim 74:23be10c32fa3 906 while (!_p_serial->writeable());
j3 63:422be898443a 907 //for some reason send_break wouldn't work unless first sending char
j3 63:422be898443a 908 _p_serial->putc(0);
j3 63:422be898443a 909 _p_serial->send_break();
j3 63:422be898443a 910 }
j3 63:422be898443a 911
j3 63:422be898443a 912
IanBenzMaxim 73:2cecc1372acc 913 void DS2480B::FlushCOM(void)
j3 63:422be898443a 914 {
j3 63:422be898443a 915 //reset soft rx_buffer
j3 63:422be898443a 916 rx_buffer.w_idx = 0;
j3 63:422be898443a 917 rx_buffer.r_idx = 0;
j3 63:422be898443a 918 rx_buffer.wrap_error = false;
IanBenzMaxim 74:23be10c32fa3 919
j3 63:422be898443a 920 //make sure hardware rx buffer is empty
IanBenzMaxim 74:23be10c32fa3 921 while (_p_serial->readable())
j3 63:422be898443a 922 {
j3 63:422be898443a 923 _p_serial->getc();
j3 63:422be898443a 924 }
IanBenzMaxim 74:23be10c32fa3 925
j3 65:a28ac52ca127 926 /*Not sure how to flush tx buffer without sending data out on the bus.
j3 65:a28ac52ca127 927 from the example in AN192, the data shouldn't be sent, just aborted
j3 65:a28ac52ca127 928 and the buffer cleaned out, http://pdfserv.maximintegrated.com/en/an/AN192.pdf
j3 65:a28ac52ca127 929 Below is what was used in AN192 example code using windows drivers
j3 65:a28ac52ca127 930 */
j3 65:a28ac52ca127 931
j3 65:a28ac52ca127 932 //PurgeComm(ComID, PURGE_TXABORT | PURGE_RXABORT | PURGE_TXCLEAR | PURGE_RXCLEAR );
j3 63:422be898443a 933 }
j3 63:422be898443a 934
j3 63:422be898443a 935
j3 63:422be898443a 936 //*********************************************************************
IanBenzMaxim 73:2cecc1372acc 937 void DS2480B::SetBaudCOM(uint8_t new_baud)
j3 63:422be898443a 938 {
IanBenzMaxim 74:23be10c32fa3 939 switch (new_baud)
j3 63:422be898443a 940 {
IanBenzMaxim 74:23be10c32fa3 941 case BPS_115200:
IanBenzMaxim 74:23be10c32fa3 942 _p_serial->baud(115200);
IanBenzMaxim 74:23be10c32fa3 943 break;
IanBenzMaxim 74:23be10c32fa3 944
IanBenzMaxim 74:23be10c32fa3 945 case BPS_57600:
IanBenzMaxim 74:23be10c32fa3 946 _p_serial->baud(57600);
IanBenzMaxim 74:23be10c32fa3 947 break;
IanBenzMaxim 74:23be10c32fa3 948
IanBenzMaxim 74:23be10c32fa3 949 case BPS_19200:
IanBenzMaxim 74:23be10c32fa3 950 _p_serial->baud(19200);
IanBenzMaxim 74:23be10c32fa3 951 break;
IanBenzMaxim 74:23be10c32fa3 952
IanBenzMaxim 74:23be10c32fa3 953 case BPS_9600:
IanBenzMaxim 74:23be10c32fa3 954 default:
IanBenzMaxim 74:23be10c32fa3 955 _p_serial->baud(9600);
IanBenzMaxim 74:23be10c32fa3 956 break;
j3 63:422be898443a 957 }
j3 63:422be898443a 958 }
j3 63:422be898443a 959
j3 63:422be898443a 960
j3 63:422be898443a 961 //*********************************************************************
IanBenzMaxim 73:2cecc1372acc 962 int32_t DS2480B::bitacc(uint32_t op, uint32_t state, uint32_t loc, uint8_t *buf)
j3 63:422be898443a 963 {
IanBenzMaxim 74:23be10c32fa3 964 int nbyt, nbit;
j3 63:422be898443a 965
j3 65:a28ac52ca127 966 nbyt = (loc / 8);
j3 65:a28ac52ca127 967 nbit = loc - (nbyt * 8);
j3 63:422be898443a 968
IanBenzMaxim 74:23be10c32fa3 969 if (op == WRITE_FUNCTION)
j3 65:a28ac52ca127 970 {
j3 65:a28ac52ca127 971 if (state)
j3 65:a28ac52ca127 972 {
j3 65:a28ac52ca127 973 buf[nbyt] |= (0x01 << nbit);
j3 65:a28ac52ca127 974 }
j3 65:a28ac52ca127 975 else
j3 65:a28ac52ca127 976 {
j3 65:a28ac52ca127 977 buf[nbyt] &= ~(0x01 << nbit);
j3 65:a28ac52ca127 978 }
j3 63:422be898443a 979
j3 65:a28ac52ca127 980 return 1;
IanBenzMaxim 74:23be10c32fa3 981 }
j3 65:a28ac52ca127 982 else
j3 65:a28ac52ca127 983 {
j3 65:a28ac52ca127 984 return ((buf[nbyt] >> nbit) & 0x01);
j3 65:a28ac52ca127 985 }
IanBenzMaxim 71:562f5c702094 986 }