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:
j3
Date:
Wed Apr 20 21:47:42 2016 +0000
Revision:
65:a28ac52ca127
Parent:
63:422be898443a
Child:
67:76776130aec9
Improved timing in ReadCOM fx for DS2480B

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