Maxim Integrated / Mbed 2 deprecated MAXREFDES132_OneWire_Demo

Dependencies:   OneWire Terminal mbed

Committer:
j3
Date:
Tue Aug 23 04:35:43 2016 +0000
Revision:
7:81d4e19b7b50
Parent:
5:e016b530bef5
Child:
8:7af592c694b2
Fixed DS2480B instantiation after update

Who changed what in which revision?

UserRevisionLine numberNew contents of line
j3 0:11c9d61c8876 1 /**********************************************************************
j3 0:11c9d61c8876 2 * Copyright (C) 2016 Maxim Integrated Products, Inc., All Rights Reserved.
j3 0:11c9d61c8876 3 *
j3 0:11c9d61c8876 4 * Permission is hereby granted, free of charge, to any person obtaining a
j3 0:11c9d61c8876 5 * copy of this software and associated documentation files (the "Software"),
j3 0:11c9d61c8876 6 * to deal in the Software without restriction, including without limitation
j3 0:11c9d61c8876 7 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
j3 0:11c9d61c8876 8 * and/or sell copies of the Software, and to permit persons to whom the
j3 0:11c9d61c8876 9 * Software is furnished to do so, subject to the following conditions:
j3 0:11c9d61c8876 10 *
j3 0:11c9d61c8876 11 * The above copyright notice and this permission notice shall be included
j3 0:11c9d61c8876 12 * in all copies or substantial portions of the Software.
j3 0:11c9d61c8876 13 *
j3 0:11c9d61c8876 14 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
j3 0:11c9d61c8876 15 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
j3 0:11c9d61c8876 16 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
j3 0:11c9d61c8876 17 * IN NO EVENT SHALL MAXIM INTEGRATED BE LIABLE FOR ANY CLAIM, DAMAGES
j3 0:11c9d61c8876 18 * OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
j3 0:11c9d61c8876 19 * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
j3 0:11c9d61c8876 20 * OTHER DEALINGS IN THE SOFTWARE.
j3 0:11c9d61c8876 21 *
j3 0:11c9d61c8876 22 * Except as contained in this notice, the name of Maxim Integrated
j3 0:11c9d61c8876 23 * Products, Inc. shall not be used except as stated in the Maxim Integrated
j3 0:11c9d61c8876 24 * Products, Inc. Branding Policy.
j3 0:11c9d61c8876 25 *
j3 0:11c9d61c8876 26 * The mere transfer of this software does not imply any licenses
j3 0:11c9d61c8876 27 * of trade secrets, proprietary technology, copyrights, patents,
j3 0:11c9d61c8876 28 * trademarks, maskwork rights, or any other form of intellectual
j3 0:11c9d61c8876 29 * property whatsoever. Maxim Integrated Products, Inc. retains all
j3 0:11c9d61c8876 30 * ownership rights.
j3 0:11c9d61c8876 31 **********************************************************************/
j3 0:11c9d61c8876 32
j3 0:11c9d61c8876 33
j3 0:11c9d61c8876 34 #include "mbed.h"
j3 4:9f799f2e157a 35 #include "OneWire.h"
j3 0:11c9d61c8876 36 #include "Terminal.h"
j3 0:11c9d61c8876 37
j3 0:11c9d61c8876 38 using namespace OneWire;
j3 1:c9d52433407f 39 using namespace RomCommands;
j3 0:11c9d61c8876 40
j3 0:11c9d61c8876 41 //Uncomment the master for your setup
j3 0:11c9d61c8876 42
j3 7:81d4e19b7b50 43 //#define I2C_TO_OW_MASTER
j3 7:81d4e19b7b50 44 #define SERIAL_TO_OW_MASTER
j3 5:e016b530bef5 45 //#define BIT_BANG_MASTER
j3 0:11c9d61c8876 46
j3 0:11c9d61c8876 47 void print_rom_id(Terminal & term, const RomId & romId);
j3 0:11c9d61c8876 48 void DS1920Menu(Terminal & term, const RomId & romId, MultidropRomIterator & selector);
j3 0:11c9d61c8876 49
j3 0:11c9d61c8876 50 int main(void)
j3 0:11c9d61c8876 51 {
j3 0:11c9d61c8876 52 Terminal term(USBTX, USBRX);
j3 0:11c9d61c8876 53 char user_entry = 'y';
j3 0:11c9d61c8876 54
j3 0:11c9d61c8876 55 #if defined(I2C_TO_OW_MASTER)
j3 0:11c9d61c8876 56 //Instantiates a 1-wire master using the given I2C bus SDA/SCL pins
j3 0:11c9d61c8876 57 //Requires a DS248X series of masters on I2C bus
j3 0:11c9d61c8876 58 //https://para.maximintegrated.com/en/results.mvp?fam=1wire&tree=master
j3 0:11c9d61c8876 59
j3 1:c9d52433407f 60 I2C i2cBus(D14, D15);
j3 5:e016b530bef5 61 i2cBus.frequency(400000);
j3 2:6d410b7d0fd0 62 DS2484 owm(i2cBus);
j3 0:11c9d61c8876 63 term.printf("\nStarting I2C to 1-wire Demo\n");
j3 0:11c9d61c8876 64
j3 0:11c9d61c8876 65 #elif defined(SERIAL_TO_OW_MASTER)
j3 0:11c9d61c8876 66 //Instantiates a 1-wire master using the given UART tx/rx pins
j3 0:11c9d61c8876 67 //Requires a DS2480B serial to 1-wire master
j3 0:11c9d61c8876 68 //https://www.maximintegrated.com/en/products/interface/controllers-expanders/DS2480B.html
j3 0:11c9d61c8876 69
j3 7:81d4e19b7b50 70 DS2480B owm(D1, D0);
j3 0:11c9d61c8876 71 term.printf("\nStarting Serial to 1-wire Demo\n");
j3 0:11c9d61c8876 72
j3 0:11c9d61c8876 73 #elif defined(BIT_BANG_MASTER)
j3 0:11c9d61c8876 74 //Instantiates a 1-wire master on the given GPIO pin
j3 0:11c9d61c8876 75 //Requires MAX32600MBED platform
j3 0:11c9d61c8876 76 //https://www.maximintegrated.com/en/products/digital/microcontrollers/MAX32600MBED.html
j3 0:11c9d61c8876 77
j3 0:11c9d61c8876 78 OwGpio owm(D2);
j3 0:11c9d61c8876 79 term.printf("\nStarting Bit-Bang 1-wire Demo\n");
j3 0:11c9d61c8876 80
j3 0:11c9d61c8876 81 #else
j3 0:11c9d61c8876 82 #error One Wire Master Not Defined
j3 0:11c9d61c8876 83 #endif
j3 0:11c9d61c8876 84
j3 0:11c9d61c8876 85 OneWireMaster::CmdResult result = owm.OWInitMaster();
j3 0:11c9d61c8876 86
j3 0:11c9d61c8876 87 if(result == OneWireMaster::Success)
j3 0:11c9d61c8876 88 {
j3 0:11c9d61c8876 89 //Rom Iterator for slave devices.
j3 0:11c9d61c8876 90 //Encapsulates owm, ROM cntl fxs, and RomId of device into one object
j3 0:11c9d61c8876 91 //that slave member fxs use to implement ROM cntl fxs so that you don't have too.
j3 0:11c9d61c8876 92 //You just use the features of the slave device
j3 0:11c9d61c8876 93 MultidropRomIterator selector(owm);
j3 0:11c9d61c8876 94
j3 0:11c9d61c8876 95 //Search state var for main loop
j3 1:c9d52433407f 96 SearchState searchState;
j3 0:11c9d61c8876 97
j3 0:11c9d61c8876 98 while(user_entry != 'n')
j3 0:11c9d61c8876 99 {
j3 0:11c9d61c8876 100 result = owm.OWReset();
j3 0:11c9d61c8876 101 if(result == OneWireMaster::Success)
j3 0:11c9d61c8876 102 {
j3 0:11c9d61c8876 103 term.printf("\nOWReset success, starting search\n");
j3 0:11c9d61c8876 104
j3 1:c9d52433407f 105 result = OWFirst(owm, searchState);
j3 0:11c9d61c8876 106 if(result == OneWireMaster::Success)
j3 0:11c9d61c8876 107 {
j3 0:11c9d61c8876 108 do
j3 0:11c9d61c8876 109 {
j3 0:11c9d61c8876 110 //print current devices rom id
j3 0:11c9d61c8876 111 print_rom_id(term, searchState.romId);
j3 0:11c9d61c8876 112
j3 0:11c9d61c8876 113
j3 0:11c9d61c8876 114 //This could be a switch statement based on familyCodes
j3 0:11c9d61c8876 115 //if your app has more than one device on the bus
j3 0:11c9d61c8876 116 if(searchState.romId.familyCode() == 0x10)
j3 0:11c9d61c8876 117 {
j3 0:11c9d61c8876 118 DS1920Menu(term, searchState.romId, selector);
j3 0:11c9d61c8876 119 }
j3 0:11c9d61c8876 120
j3 0:11c9d61c8876 121 //find the next device if any
j3 1:c9d52433407f 122 result = OWNext(owm, searchState);
j3 0:11c9d61c8876 123 }
j3 0:11c9d61c8876 124 while(result == OneWireMaster::Success);
j3 0:11c9d61c8876 125 }
j3 0:11c9d61c8876 126 else
j3 0:11c9d61c8876 127 {
j3 0:11c9d61c8876 128 term.printf("\nSearch failed\n");
j3 0:11c9d61c8876 129 term.printf("\nError code = %d\n", result);
j3 0:11c9d61c8876 130 }
j3 0:11c9d61c8876 131 }
j3 0:11c9d61c8876 132 else
j3 0:11c9d61c8876 133 {
j3 0:11c9d61c8876 134 term.printf("\nFailed to find any 1-wire devices on bus\n");
j3 0:11c9d61c8876 135 term.printf("\nError code = %d\n", result);
j3 0:11c9d61c8876 136 }
j3 0:11c9d61c8876 137
j3 0:11c9d61c8876 138 user_entry = term.get_char("\nEnter 'y' to continue, or 'n' to quit: ", 'n', 'y');
j3 0:11c9d61c8876 139 term.cls();
j3 0:11c9d61c8876 140 term.home();
j3 0:11c9d61c8876 141 }
j3 0:11c9d61c8876 142 }
j3 0:11c9d61c8876 143 else
j3 0:11c9d61c8876 144 {
j3 0:11c9d61c8876 145 term.printf("\nFailed to initialize the 1-wire Master\n");
j3 0:11c9d61c8876 146 term.printf("\nError code = %d\n", result);
j3 0:11c9d61c8876 147 }
j3 0:11c9d61c8876 148
j3 0:11c9d61c8876 149 term.printf("\nEnding Program\n");
j3 0:11c9d61c8876 150
j3 0:11c9d61c8876 151 return 0;
j3 0:11c9d61c8876 152 }
j3 0:11c9d61c8876 153 //***** end main ******************************************************
j3 0:11c9d61c8876 154
j3 0:11c9d61c8876 155
j3 0:11c9d61c8876 156 //*********************************************************************
j3 0:11c9d61c8876 157 void print_rom_id(Terminal & term, const RomId & romId)
j3 0:11c9d61c8876 158 {
j3 0:11c9d61c8876 159 //print the rom number
j3 0:11c9d61c8876 160 term.printf("\n");
j3 0:11c9d61c8876 161 int8_t idx = (RomId::byteLen - 1);
j3 0:11c9d61c8876 162 do
j3 0:11c9d61c8876 163 {
j3 0:11c9d61c8876 164 if(romId[idx] < 16)
j3 0:11c9d61c8876 165 {
j3 0:11c9d61c8876 166 term.printf("0x0%x ", romId[idx--]);
j3 0:11c9d61c8876 167 }
j3 0:11c9d61c8876 168 else
j3 0:11c9d61c8876 169 {
j3 0:11c9d61c8876 170 term.printf("0x%2x ", romId[idx--]);
j3 0:11c9d61c8876 171 }
j3 0:11c9d61c8876 172 }
j3 0:11c9d61c8876 173 while(idx >= 0);
j3 0:11c9d61c8876 174 term.printf("\n");
j3 0:11c9d61c8876 175 }
j3 0:11c9d61c8876 176
j3 0:11c9d61c8876 177
j3 0:11c9d61c8876 178 //*********************************************************************
j3 0:11c9d61c8876 179 void DS1920Menu(Terminal & term, const RomId & romId, MultidropRomIterator & selector)
j3 0:11c9d61c8876 180 {
j3 0:11c9d61c8876 181 char userEntry = '0';
j3 0:11c9d61c8876 182 uint8_t th, tl, idx;
j3 0:11c9d61c8876 183 uint8_t scratchPadBuff[8];
j3 0:11c9d61c8876 184 float temperature;
j3 0:11c9d61c8876 185
j3 0:11c9d61c8876 186 DS1920 tempIbutton(selector);
j3 0:11c9d61c8876 187 DS1920::CmdResult result = DS1920::OpFailure;
j3 1:c9d52433407f 188 tempIbutton.setRomId(romId);
j3 0:11c9d61c8876 189
j3 0:11c9d61c8876 190 while(userEntry != '7')
j3 0:11c9d61c8876 191 {
j3 0:11c9d61c8876 192 term.printf("\nDS1920 Menu Options\n");
j3 0:11c9d61c8876 193 term.printf("\n%t1. Write ScratchPad");
j3 0:11c9d61c8876 194 term.printf("\n%t2. Read Scratchpad");
j3 0:11c9d61c8876 195 term.printf("\n%t3. Copy Scratchpad");
j3 0:11c9d61c8876 196 term.printf("\n%t4. Convert Temperature");
j3 0:11c9d61c8876 197 term.printf("\n%t5. Recall EEPROM");
j3 0:11c9d61c8876 198 term.printf("\n%t6. Clear Screen");
j3 0:11c9d61c8876 199 term.printf("\n%t7. Quit");
j3 0:11c9d61c8876 200
j3 0:11c9d61c8876 201 userEntry = term.get_char("\nPlease select an option above: ", '1', '7');
j3 0:11c9d61c8876 202
j3 0:11c9d61c8876 203 switch(userEntry)
j3 0:11c9d61c8876 204 {
j3 0:11c9d61c8876 205 case '1':
j3 0:11c9d61c8876 206 th = term.get_int32("\nPlease enter upper temperature threshold, integer values only: ", 0, 100);
j3 0:11c9d61c8876 207 tl = term.get_int32("\nPlease enter lower temperature threshold, integer values only: ", -55, 0);
j3 0:11c9d61c8876 208
j3 0:11c9d61c8876 209 result = tempIbutton.writeScratchPad(th, tl);
j3 0:11c9d61c8876 210 if(result == DS1920::Success)
j3 0:11c9d61c8876 211 {
j3 0:11c9d61c8876 212 term.printf("\nWrite Scratchpad Success\n");
j3 0:11c9d61c8876 213 }
j3 0:11c9d61c8876 214 else
j3 0:11c9d61c8876 215 {
j3 0:11c9d61c8876 216 term.printf("\nWrite Scratchpad Fail\n");
j3 0:11c9d61c8876 217 }
j3 0:11c9d61c8876 218
j3 0:11c9d61c8876 219 break;
j3 0:11c9d61c8876 220
j3 0:11c9d61c8876 221 case '2':
j3 0:11c9d61c8876 222
j3 0:11c9d61c8876 223 result = tempIbutton.readScratchPad(scratchPadBuff);
j3 0:11c9d61c8876 224 if(result == DS1920::Success)
j3 0:11c9d61c8876 225 {
j3 0:11c9d61c8876 226 term.printf("\nRead Scratchpad Success\n");
j3 0:11c9d61c8876 227 term.printf("\nScratchpad = ");
j3 0:11c9d61c8876 228 for(idx = 0; idx < 8; idx++)
j3 0:11c9d61c8876 229 {
j3 0:11c9d61c8876 230 if(scratchPadBuff[idx] < 16)
j3 0:11c9d61c8876 231 {
j3 0:11c9d61c8876 232 term.printf("0x0%x ", scratchPadBuff[idx]);
j3 0:11c9d61c8876 233 }
j3 0:11c9d61c8876 234 else
j3 0:11c9d61c8876 235 {
j3 0:11c9d61c8876 236 term.printf("0x%2x ", scratchPadBuff[idx]);
j3 0:11c9d61c8876 237 }
j3 0:11c9d61c8876 238 }
j3 0:11c9d61c8876 239 term.printf("\n");
j3 0:11c9d61c8876 240
j3 0:11c9d61c8876 241 }
j3 0:11c9d61c8876 242 else
j3 0:11c9d61c8876 243 {
j3 0:11c9d61c8876 244 term.printf("\nRead Scratchpad Fail\n");
j3 0:11c9d61c8876 245 }
j3 0:11c9d61c8876 246
j3 0:11c9d61c8876 247 break;
j3 0:11c9d61c8876 248
j3 0:11c9d61c8876 249 case '3':
j3 0:11c9d61c8876 250
j3 0:11c9d61c8876 251 result = tempIbutton.copyScratchPad();
j3 0:11c9d61c8876 252 if(result == DS1920::Success)
j3 0:11c9d61c8876 253 {
j3 0:11c9d61c8876 254 term.printf("\nCopy Scratchpad Success\n");
j3 0:11c9d61c8876 255 }
j3 0:11c9d61c8876 256 else
j3 0:11c9d61c8876 257 {
j3 0:11c9d61c8876 258 term.printf("\nCopy Scratchpad Fail\n");
j3 0:11c9d61c8876 259 }
j3 0:11c9d61c8876 260
j3 0:11c9d61c8876 261 break;
j3 0:11c9d61c8876 262
j3 0:11c9d61c8876 263 case '4':
j3 0:11c9d61c8876 264
j3 0:11c9d61c8876 265 result = tempIbutton.convertTemperature(temperature);
j3 0:11c9d61c8876 266 if(result == DS1920::Success)
j3 0:11c9d61c8876 267 {
j3 0:11c9d61c8876 268 term.printf("\nConvert Temperature Success\n");
j3 0:11c9d61c8876 269 term.printf("\nTemperature = %.1f", temperature);
j3 0:11c9d61c8876 270 }
j3 0:11c9d61c8876 271 else
j3 0:11c9d61c8876 272 {
j3 0:11c9d61c8876 273 term.printf("\nConvert Temperature Fail\n");
j3 0:11c9d61c8876 274 }
j3 0:11c9d61c8876 275
j3 0:11c9d61c8876 276 break;
j3 0:11c9d61c8876 277
j3 0:11c9d61c8876 278 case '5':
j3 0:11c9d61c8876 279
j3 0:11c9d61c8876 280 result = tempIbutton.recallEEPROM();
j3 0:11c9d61c8876 281 if(result == DS1920::Success)
j3 0:11c9d61c8876 282 {
j3 0:11c9d61c8876 283 term.printf("\nRecall EEPROM Success\n");
j3 0:11c9d61c8876 284 }
j3 0:11c9d61c8876 285 else
j3 0:11c9d61c8876 286 {
j3 0:11c9d61c8876 287 term.printf("\nRecall EEPROM Fail\n");
j3 0:11c9d61c8876 288 }
j3 0:11c9d61c8876 289
j3 0:11c9d61c8876 290 break;
j3 0:11c9d61c8876 291
j3 0:11c9d61c8876 292 case '6':
j3 0:11c9d61c8876 293 term.cls();
j3 0:11c9d61c8876 294 term.home();
j3 0:11c9d61c8876 295 break;
j3 0:11c9d61c8876 296
j3 0:11c9d61c8876 297 case '7':
j3 0:11c9d61c8876 298 term.printf("\nLeaving DS1920 Menu Options\n");
j3 0:11c9d61c8876 299 break;
j3 0:11c9d61c8876 300
j3 0:11c9d61c8876 301 default:
j3 0:11c9d61c8876 302 mbed_die();
j3 0:11c9d61c8876 303 break;
j3 0:11c9d61c8876 304 }
j3 0:11c9d61c8876 305 }
j3 0:11c9d61c8876 306 }