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