Implementation of 1-Wire with added Alarm Search Functionality

Dependents:   Max32630_One_Wire_Interface

Committer:
IanBenzMaxim
Date:
Wed Mar 23 15:25:40 2016 -0500
Revision:
26:a361e3f42ba5
Parent:
24:8942d8478d68
Child:
32:bce180b544ed
Rework the OneWireMaster virtual interface for simplicity.

Who changed what in which revision?

UserRevisionLine numberNew contents of line
j3 1:91e52f8ab8bf 1 /******************************************************************//**
j3 1:91e52f8ab8bf 2 * Copyright (C) 2016 Maxim Integrated Products, Inc., All Rights Reserved.
j3 1:91e52f8ab8bf 3 *
j3 1:91e52f8ab8bf 4 * Permission is hereby granted, free of charge, to any person obtaining a
j3 1:91e52f8ab8bf 5 * copy of this software and associated documentation files (the "Software"),
j3 1:91e52f8ab8bf 6 * to deal in the Software without restriction, including without limitation
j3 1:91e52f8ab8bf 7 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
j3 1:91e52f8ab8bf 8 * and/or sell copies of the Software, and to permit persons to whom the
j3 1:91e52f8ab8bf 9 * Software is furnished to do so, subject to the following conditions:
j3 1:91e52f8ab8bf 10 *
j3 1:91e52f8ab8bf 11 * The above copyright notice and this permission notice shall be included
j3 1:91e52f8ab8bf 12 * in all copies or substantial portions of the Software.
j3 1:91e52f8ab8bf 13 *
j3 1:91e52f8ab8bf 14 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
j3 1:91e52f8ab8bf 15 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
j3 1:91e52f8ab8bf 16 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
j3 1:91e52f8ab8bf 17 * IN NO EVENT SHALL MAXIM INTEGRATED BE LIABLE FOR ANY CLAIM, DAMAGES
j3 1:91e52f8ab8bf 18 * OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
j3 1:91e52f8ab8bf 19 * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
j3 1:91e52f8ab8bf 20 * OTHER DEALINGS IN THE SOFTWARE.
j3 1:91e52f8ab8bf 21 *
j3 1:91e52f8ab8bf 22 * Except as contained in this notice, the name of Maxim Integrated
j3 1:91e52f8ab8bf 23 * Products, Inc. shall not be used except as stated in the Maxim Integrated
j3 1:91e52f8ab8bf 24 * Products, Inc. Branding Policy.
j3 1:91e52f8ab8bf 25 *
j3 1:91e52f8ab8bf 26 * The mere transfer of this software does not imply any licenses
j3 1:91e52f8ab8bf 27 * of trade secrets, proprietary technology, copyrights, patents,
j3 1:91e52f8ab8bf 28 * trademarks, maskwork rights, or any other form of intellectual
j3 1:91e52f8ab8bf 29 * property whatsoever. Maxim Integrated Products, Inc. retains all
j3 1:91e52f8ab8bf 30 * ownership rights.
j3 1:91e52f8ab8bf 31 **********************************************************************/
j3 1:91e52f8ab8bf 32
j3 1:91e52f8ab8bf 33
j3 1:91e52f8ab8bf 34 #ifndef DS248X_H
j3 1:91e52f8ab8bf 35 #define DS248X_H
j3 1:91e52f8ab8bf 36
j3 1:91e52f8ab8bf 37
j3 1:91e52f8ab8bf 38 #include "mbed.h"
IanBenzMaxim 24:8942d8478d68 39 #include "OneWire_Masters/OneWireMaster.h"
j3 1:91e52f8ab8bf 40
j3 1:91e52f8ab8bf 41
j3 15:f6cb0d906fb6 42 class Ds248x: public OneWireMaster
j3 1:91e52f8ab8bf 43 {
j3 1:91e52f8ab8bf 44 public:
j3 1:91e52f8ab8bf 45
j3 5:ce108eeb878d 46 enum DS248X_I2C_ADRS
j3 1:91e52f8ab8bf 47 {
j3 1:91e52f8ab8bf 48 DS248X_I2C_ADRS0 = 0x18,
j3 1:91e52f8ab8bf 49 DS248X_I2C_ADRS1,
j3 1:91e52f8ab8bf 50 DS248X_I2C_ADRS2,
j3 1:91e52f8ab8bf 51 DS248X_I2C_ADRS3,
j3 1:91e52f8ab8bf 52 DS248X_I2C_ADRS4,
j3 1:91e52f8ab8bf 53 DS248X_I2C_ADRS5,
j3 1:91e52f8ab8bf 54 DS248X_I2C_ADRS6,
j3 5:ce108eeb878d 55 DS248X_I2C_ADRS7
j3 5:ce108eeb878d 56 };
j3 5:ce108eeb878d 57
j3 5:ce108eeb878d 58 enum DS248X_CMDS
j3 5:ce108eeb878d 59 {
j3 5:ce108eeb878d 60 CMD_DRST = 0xF0,
j3 5:ce108eeb878d 61 CMD_WCFG = 0xD2,
j3 5:ce108eeb878d 62 CMD_A1WP = 0xC3, //DS2484 only
j3 5:ce108eeb878d 63 CMD_CHSL = 0xC3, //DS2482-800 only
j3 5:ce108eeb878d 64 CMD_SRP = 0xE1,
j3 5:ce108eeb878d 65 CMD_1WRS = 0xB4,
j3 5:ce108eeb878d 66 CMD_1WWB = 0xA5,
j3 5:ce108eeb878d 67 CMD_1WRB = 0x96,
j3 5:ce108eeb878d 68 CMD_1WSB = 0x87,
j3 5:ce108eeb878d 69 CMD_1WT = 0x78
j3 5:ce108eeb878d 70 };
j3 5:ce108eeb878d 71
j3 5:ce108eeb878d 72 enum DS248X_CONFIG_BITS
j3 5:ce108eeb878d 73 {
j3 5:ce108eeb878d 74 CONFIG_APU = 0x01,
j3 5:ce108eeb878d 75 CONFIG_PDN = 0x02,
j3 5:ce108eeb878d 76 CONFIG_SPU = 0x04,
j3 5:ce108eeb878d 77 CONFIG_1WS = 0x08
j3 5:ce108eeb878d 78 };
j3 5:ce108eeb878d 79
j3 5:ce108eeb878d 80 enum DS248X_STATUS_BITS
j3 5:ce108eeb878d 81 {
j3 5:ce108eeb878d 82 STATUS_1WB = 0x01,
j3 5:ce108eeb878d 83 STATUS_PPD = 0x02,
j3 5:ce108eeb878d 84 STATUS_SD = 0x04,
j3 5:ce108eeb878d 85 STATUS_LL = 0x08,
j3 5:ce108eeb878d 86 STATUS_RST = 0x10,
j3 5:ce108eeb878d 87 STATUS_SBR = 0x20,
j3 5:ce108eeb878d 88 STATUS_TSB = 0x40,
j3 5:ce108eeb878d 89 STATUS_DIR = 0x80
j3 5:ce108eeb878d 90 };
j3 5:ce108eeb878d 91
j3 5:ce108eeb878d 92 enum DS2484_ADJST_PARAMS
j3 5:ce108eeb878d 93 {
j3 5:ce108eeb878d 94 TRSTL = 0,
j3 5:ce108eeb878d 95 TRSTL_OD,
j3 5:ce108eeb878d 96 TMSP,
j3 5:ce108eeb878d 97 TMSP_OD,
j3 5:ce108eeb878d 98 TW0L,
j3 5:ce108eeb878d 99 TW0L_OD,
j3 5:ce108eeb878d 100 TREC0, //OD NA
j3 5:ce108eeb878d 101 RWPU = 8 //OD NA, see DS2484 datasheet page 13
j3 5:ce108eeb878d 102 };
j3 17:b646b1e3970b 103
j3 17:b646b1e3970b 104 enum SEARCH_DIRECTION
j3 17:b646b1e3970b 105 {
j3 17:b646b1e3970b 106 DIRECTION_WRITE_ZERO = 0,
j3 17:b646b1e3970b 107 DIRECTION_WRITE_ONE = 1
j3 17:b646b1e3970b 108 };
j3 5:ce108eeb878d 109
j3 5:ce108eeb878d 110 static const size_t POLL_LIMIT = 200;
j3 1:91e52f8ab8bf 111
j3 17:b646b1e3970b 112 static const int I2C_WRITE_OK = 0;
j3 17:b646b1e3970b 113
j3 17:b646b1e3970b 114 static const int I2C_READ_OK = 0;
j3 17:b646b1e3970b 115
j3 3:644fc630f958 116
j3 1:91e52f8ab8bf 117 /**********************************************************//**
j3 1:91e52f8ab8bf 118 * @brief Ds248x constructor
j3 1:91e52f8ab8bf 119 *
j3 1:91e52f8ab8bf 120 * @details allows user to use existing I2C object
j3 1:91e52f8ab8bf 121 *
j3 1:91e52f8ab8bf 122 * On Entry:
j3 1:91e52f8ab8bf 123 * @param[in] p_i2c_bus - pointer to existing I2C object
j3 1:91e52f8ab8bf 124 *
j3 1:91e52f8ab8bf 125 * On Exit:
j3 1:91e52f8ab8bf 126 * @return
j3 1:91e52f8ab8bf 127 **************************************************************/
j3 5:ce108eeb878d 128 Ds248x(I2C &i2c_bus, DS248X_I2C_ADRS adrs);
j3 1:91e52f8ab8bf 129
j3 1:91e52f8ab8bf 130
j3 1:91e52f8ab8bf 131 /**********************************************************//**
j3 1:91e52f8ab8bf 132 * @brief Ds248x constructor
j3 1:91e52f8ab8bf 133 *
j3 3:644fc630f958 134 * @details Object instantiates a new I2C object with no
j3 3:644fc630f958 135 * public access
j3 1:91e52f8ab8bf 136 *
j3 1:91e52f8ab8bf 137 * On Entry:
j3 1:91e52f8ab8bf 138 * @param[in] sda - sda pin of I2C bus
j3 1:91e52f8ab8bf 139 * @param[in] scl - scl pin of I2C bus
j3 1:91e52f8ab8bf 140 *
j3 1:91e52f8ab8bf 141 * On Exit:
j3 1:91e52f8ab8bf 142 * @return
j3 1:91e52f8ab8bf 143 **************************************************************/
j3 5:ce108eeb878d 144 Ds248x(PinName sda, PinName scl, DS248X_I2C_ADRS adrs);
j3 1:91e52f8ab8bf 145
j3 1:91e52f8ab8bf 146
j3 1:91e52f8ab8bf 147 /**********************************************************//**
j3 1:91e52f8ab8bf 148 * @brief Ds248x destructor
j3 1:91e52f8ab8bf 149 *
j3 1:91e52f8ab8bf 150 * @details deletes I2C object if owner
j3 1:91e52f8ab8bf 151 *
j3 1:91e52f8ab8bf 152 * On Entry:
j3 1:91e52f8ab8bf 153 *
j3 1:91e52f8ab8bf 154 * On Exit:
j3 1:91e52f8ab8bf 155 * @return
j3 1:91e52f8ab8bf 156 **************************************************************/
j3 5:ce108eeb878d 157 virtual ~Ds248x();
j3 1:91e52f8ab8bf 158
j3 1:91e52f8ab8bf 159
j3 1:91e52f8ab8bf 160 /**********************************************************//**
j3 1:91e52f8ab8bf 161 * @brief detect()
j3 1:91e52f8ab8bf 162 *
j3 1:91e52f8ab8bf 163 * @details Detect routine that performs a device reset
j3 1:91e52f8ab8bf 164 * followed by writing the configuration byte to default
j3 1:91e52f8ab8bf 165 * values:
j3 1:91e52f8ab8bf 166 * 1-Wire speed (c1WS) = standard (0)
j3 1:91e52f8ab8bf 167 * Strong pull-up (cSPU) = off (0)
j3 1:91e52f8ab8bf 168 * Presence pulse masking (cPPM) = off (0)
j3 1:91e52f8ab8bf 169 * Active pull-up (cAPU) = on (CONFIG_APU = 0x01)
j3 1:91e52f8ab8bf 170 *
j3 1:91e52f8ab8bf 171 * On Entry:
j3 1:91e52f8ab8bf 172 *
j3 1:91e52f8ab8bf 173 * On Exit:
j3 1:91e52f8ab8bf 174 * @return TRUE if device was detected and written
j3 1:91e52f8ab8bf 175 * FALSE device not detected or failure to write
j3 1:91e52f8ab8bf 176 * configuration byte
j3 1:91e52f8ab8bf 177 **************************************************************/
j3 23:e8e403d61359 178 OneWireMaster::CmdResult detect(void);
j3 1:91e52f8ab8bf 179
j3 1:91e52f8ab8bf 180
j3 1:91e52f8ab8bf 181 /**********************************************************//**
j3 1:91e52f8ab8bf 182 * @brief reset()
j3 1:91e52f8ab8bf 183 *
j3 1:91e52f8ab8bf 184 * @details Perform a device reset on the Ds248x
j3 1:91e52f8ab8bf 185 *
j3 1:91e52f8ab8bf 186 * On Entry:
j3 1:91e52f8ab8bf 187 *
j3 1:91e52f8ab8bf 188 * On Exit:
j3 1:91e52f8ab8bf 189 * @return TRUE if device was reset
j3 1:91e52f8ab8bf 190 * FALSE device not detected or failure to perform reset
j3 1:91e52f8ab8bf 191 **************************************************************/
j3 23:e8e403d61359 192 OneWireMaster::CmdResult reset(void);
j3 1:91e52f8ab8bf 193
j3 1:91e52f8ab8bf 194
j3 1:91e52f8ab8bf 195 /**********************************************************//**
j3 1:91e52f8ab8bf 196 * @brief write_config()
j3 1:91e52f8ab8bf 197 *
j3 1:91e52f8ab8bf 198 * @details Write the configuration register in the Ds248x. The
j3 1:91e52f8ab8bf 199 * configuration options are provided in the lower nibble
j3 1:91e52f8ab8bf 200 * of the provided config byte. The uppper nibble is
j3 1:91e52f8ab8bf 201 * bitwise inverted when written to the Ds248x.
j3 1:91e52f8ab8bf 202 *
j3 1:91e52f8ab8bf 203 * On Entry:
j3 1:91e52f8ab8bf 204 * @param[in] config - lower nib of configuration register
j3 1:91e52f8ab8bf 205 *
j3 1:91e52f8ab8bf 206 * On Exit:
j3 1:91e52f8ab8bf 207 * @return TRUE: config written and response correct
j3 1:91e52f8ab8bf 208 * FALSE: response incorrect
j3 1:91e52f8ab8bf 209 **************************************************************/
j3 23:e8e403d61359 210 OneWireMaster::CmdResult write_config(uint8_t config);
j3 2:02d228c25fd4 211
j3 2:02d228c25fd4 212
j3 2:02d228c25fd4 213 /**********************************************************//**
j3 2:02d228c25fd4 214 * @brief channel_select()
j3 2:02d228c25fd4 215 *
j3 2:02d228c25fd4 216 * @details Select the 1-Wire channel on a DS2482-800.
j3 2:02d228c25fd4 217 * Min channel = 1
j3 2:02d228c25fd4 218 *
j3 2:02d228c25fd4 219 * On Entry:
j3 2:02d228c25fd4 220 * @param[in] channel - desired channel of the DS2482
j3 2:02d228c25fd4 221 *
j3 2:02d228c25fd4 222 * On Exit:
j3 2:02d228c25fd4 223 * @return TRUE if channel selected
j3 2:02d228c25fd4 224 * FALSE device not detected or failure to perform select
j3 2:02d228c25fd4 225 **************************************************************/
j3 23:e8e403d61359 226 OneWireMaster::CmdResult channel_select(uint8_t channel);
j3 1:91e52f8ab8bf 227
j3 1:91e52f8ab8bf 228
j3 1:91e52f8ab8bf 229 /**********************************************************//**
j3 1:91e52f8ab8bf 230 * @brief adjust_timing()
j3 1:91e52f8ab8bf 231 *
j3 2:02d228c25fd4 232 * @details adjustable timming available in DS2484 only
j3 1:91e52f8ab8bf 233 *
j3 1:91e52f8ab8bf 234 * On Entry:
j3 1:91e52f8ab8bf 235 * @param[in] param - 1 of 8 adjustable parameters
j3 1:91e52f8ab8bf 236 * @param[in] val - new value for parameter, see datasheet
j3 1:91e52f8ab8bf 237 * for codes
j3 1:91e52f8ab8bf 238 *
j3 1:91e52f8ab8bf 239 * On Exit:
j3 1:91e52f8ab8bf 240 * @return TRUE: parameter successfully adjusted
j3 1:91e52f8ab8bf 241 * FALSE: failed to adjust parameter
j3 1:91e52f8ab8bf 242 **************************************************************/
j3 23:e8e403d61359 243 OneWireMaster::CmdResult adjust_timing(uint8_t param, uint8_t val);
j3 1:91e52f8ab8bf 244
j3 1:91e52f8ab8bf 245
j3 1:91e52f8ab8bf 246 /**********************************************************//**
j3 1:91e52f8ab8bf 247 * @brief search_triplet()
j3 1:91e52f8ab8bf 248 *
j3 1:91e52f8ab8bf 249 * @details Use the Ds248x help command '1-Wire triplet' to perform
j3 1:91e52f8ab8bf 250 * one bit of a 1-Wire search. This command does two read
j3 1:91e52f8ab8bf 251 * bits and one write bit. The write bit is either the
j3 1:91e52f8ab8bf 252 * default direction (all device have same bit) or in case
j3 1:91e52f8ab8bf 253 * of a discrepancy, the 'search_direction' parameter is
j3 1:91e52f8ab8bf 254 * used.
j3 1:91e52f8ab8bf 255 *
j3 1:91e52f8ab8bf 256 * On Entry:
j3 1:91e52f8ab8bf 257 * @param[in] search_direction
j3 1:91e52f8ab8bf 258 *
j3 1:91e52f8ab8bf 259 * On Exit:
j3 1:91e52f8ab8bf 260 * @return The Ds248x status byte result from the triplet command
j3 1:91e52f8ab8bf 261 **************************************************************/
j3 23:e8e403d61359 262 OneWireMaster::CmdResult search_triplet(uint8_t search_direction, uint8_t & status);
j3 1:91e52f8ab8bf 263
j3 5:ce108eeb878d 264
j3 23:e8e403d61359 265 //Part of OneWireMaster that should be implemented for each master
j3 23:e8e403d61359 266 //See OneWireMaster.h for documentation
j3 15:f6cb0d906fb6 267
j3 23:e8e403d61359 268 virtual OneWireMaster::CmdResult OWInitMaster(void);
j3 14:7b2886a50321 269
j3 23:e8e403d61359 270 virtual OneWireMaster::CmdResult OWReset(void);
j3 1:91e52f8ab8bf 271
IanBenzMaxim 26:a361e3f42ba5 272 virtual OneWireMaster::CmdResult OWTouchBit(uint8_t & sendrecvbit, OW_LEVEL after_level);
IanBenzMaxim 26:a361e3f42ba5 273
IanBenzMaxim 26:a361e3f42ba5 274 virtual OneWireMaster::CmdResult OWReadByte(uint8_t & recvbyte, OW_LEVEL after_level);
IanBenzMaxim 26:a361e3f42ba5 275
IanBenzMaxim 26:a361e3f42ba5 276 virtual OneWireMaster::CmdResult OWWriteByte(uint8_t sendbyte, OW_LEVEL after_level);
j3 17:b646b1e3970b 277
j3 23:e8e403d61359 278 virtual OneWireMaster::CmdResult OWWriteBlock(const uint8_t *tran_buf, uint8_t tran_len);
j3 1:91e52f8ab8bf 279
j3 23:e8e403d61359 280 virtual OneWireMaster::CmdResult OWReadBlock(uint8_t *rx_buf, uint8_t rx_len);
j3 17:b646b1e3970b 281
j3 23:e8e403d61359 282 virtual OneWireMaster::CmdResult OWSearch(RomId & romId);
j3 3:644fc630f958 283
j3 23:e8e403d61359 284 virtual OneWireMaster::CmdResult OWSpeed(OW_SPEED new_speed);
j3 3:644fc630f958 285
j3 23:e8e403d61359 286 virtual OneWireMaster::CmdResult OWLevel(OW_LEVEL new_level);
j3 2:02d228c25fd4 287
IanBenzMaxim 26:a361e3f42ba5 288 OneWireMaster::CmdResult ConfigureSPU(bool spu_enable);
IanBenzMaxim 21:00c94aeb533e 289
j3 1:91e52f8ab8bf 290 private:
j3 1:91e52f8ab8bf 291
j3 6:1faafa0b3cd7 292 //private fx for initializing _w_adrs and _r_adrs
j3 6:1faafa0b3cd7 293 void set_i2c_adrs(DS248X_I2C_ADRS adrs);
j3 5:ce108eeb878d 294
j3 1:91e52f8ab8bf 295 I2C *_p_i2c_bus;
j3 1:91e52f8ab8bf 296 uint8_t _w_adrs, _r_adrs;
j3 5:ce108eeb878d 297 bool _i2c_owner;
j3 2:02d228c25fd4 298
j3 2:02d228c25fd4 299 // ds248x state
j3 2:02d228c25fd4 300 uint8_t _short_detected;
j3 15:f6cb0d906fb6 301 uint8_t _c1WS, _cSPU, _cPDN, _cAPU;
j3 1:91e52f8ab8bf 302 };
j3 1:91e52f8ab8bf 303
j3 1:91e52f8ab8bf 304 #endif /*DS248X_H*/