1-Wire® library for mbed. Complete 1-Wire library that supports our silicon masters along with a bit-bang master on the MAX32600MBED platform with one common interface for mbed. Slave support has also been included and more slaves will be added as time permits.

Dependents:   MAXREFDES131_Qt_Demo MAX32630FTHR_iButton_uSD_Logger MAX32630FTHR_DS18B20_uSD_Logger MAXREFDES130_131_Demo ... more

Superseded by MaximInterface.

Committer:
IanBenzMaxim
Date:
Wed Mar 30 16:50:29 2016 -0500
Revision:
32:bce180b544ed
Parent:
26:a361e3f42ba5
Child:
69:f915c4c59a69
1. Move the implementation of OWSearch() into OneWireMaster since it is a fairly complex algorithm that shouldn?t be implemented over and over. It will also match all other ROM function that are implemented there.
2. Create a new member function, OWTriplet(), in OneWireMaster to handle the virtual section of OWSearch(). Create a default implementation of OWTriplet() that uses OWReadByte() and OWWriteByte(). Masters only need to implement this function if they have a search accelerator of some sort.
3. Create type SearchState that will encapsulate all persistent data used by the search ROM functions. This will also make it easy to not have the search state part of the permanent OneWireMaster class data.
4. Rename OWSpeed() to OWSetSpeed() and OWLevel() to OWSetLevel() for naming consistency.

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 5:ce108eeb878d 103
j3 5:ce108eeb878d 104 static const size_t POLL_LIMIT = 200;
j3 1:91e52f8ab8bf 105
j3 17:b646b1e3970b 106 static const int I2C_WRITE_OK = 0;
j3 17:b646b1e3970b 107
j3 17:b646b1e3970b 108 static const int I2C_READ_OK = 0;
j3 17:b646b1e3970b 109
j3 3:644fc630f958 110
j3 1:91e52f8ab8bf 111 /**********************************************************//**
j3 1:91e52f8ab8bf 112 * @brief Ds248x constructor
j3 1:91e52f8ab8bf 113 *
j3 1:91e52f8ab8bf 114 * @details allows user to use existing I2C object
j3 1:91e52f8ab8bf 115 *
j3 1:91e52f8ab8bf 116 * On Entry:
j3 1:91e52f8ab8bf 117 * @param[in] p_i2c_bus - pointer to existing I2C object
j3 1:91e52f8ab8bf 118 *
j3 1:91e52f8ab8bf 119 * On Exit:
j3 1:91e52f8ab8bf 120 * @return
j3 1:91e52f8ab8bf 121 **************************************************************/
j3 5:ce108eeb878d 122 Ds248x(I2C &i2c_bus, DS248X_I2C_ADRS adrs);
j3 1:91e52f8ab8bf 123
j3 1:91e52f8ab8bf 124
j3 1:91e52f8ab8bf 125 /**********************************************************//**
j3 1:91e52f8ab8bf 126 * @brief Ds248x constructor
j3 1:91e52f8ab8bf 127 *
j3 3:644fc630f958 128 * @details Object instantiates a new I2C object with no
j3 3:644fc630f958 129 * public access
j3 1:91e52f8ab8bf 130 *
j3 1:91e52f8ab8bf 131 * On Entry:
j3 1:91e52f8ab8bf 132 * @param[in] sda - sda pin of I2C bus
j3 1:91e52f8ab8bf 133 * @param[in] scl - scl pin of I2C bus
j3 1:91e52f8ab8bf 134 *
j3 1:91e52f8ab8bf 135 * On Exit:
j3 1:91e52f8ab8bf 136 * @return
j3 1:91e52f8ab8bf 137 **************************************************************/
j3 5:ce108eeb878d 138 Ds248x(PinName sda, PinName scl, DS248X_I2C_ADRS adrs);
j3 1:91e52f8ab8bf 139
j3 1:91e52f8ab8bf 140
j3 1:91e52f8ab8bf 141 /**********************************************************//**
j3 1:91e52f8ab8bf 142 * @brief Ds248x destructor
j3 1:91e52f8ab8bf 143 *
j3 1:91e52f8ab8bf 144 * @details deletes I2C object if owner
j3 1:91e52f8ab8bf 145 *
j3 1:91e52f8ab8bf 146 * On Entry:
j3 1:91e52f8ab8bf 147 *
j3 1:91e52f8ab8bf 148 * On Exit:
j3 1:91e52f8ab8bf 149 * @return
j3 1:91e52f8ab8bf 150 **************************************************************/
j3 5:ce108eeb878d 151 virtual ~Ds248x();
j3 1:91e52f8ab8bf 152
j3 1:91e52f8ab8bf 153
j3 1:91e52f8ab8bf 154 /**********************************************************//**
j3 1:91e52f8ab8bf 155 * @brief detect()
j3 1:91e52f8ab8bf 156 *
j3 1:91e52f8ab8bf 157 * @details Detect routine that performs a device reset
j3 1:91e52f8ab8bf 158 * followed by writing the configuration byte to default
j3 1:91e52f8ab8bf 159 * values:
j3 1:91e52f8ab8bf 160 * 1-Wire speed (c1WS) = standard (0)
j3 1:91e52f8ab8bf 161 * Strong pull-up (cSPU) = off (0)
j3 1:91e52f8ab8bf 162 * Presence pulse masking (cPPM) = off (0)
j3 1:91e52f8ab8bf 163 * Active pull-up (cAPU) = on (CONFIG_APU = 0x01)
j3 1:91e52f8ab8bf 164 *
j3 1:91e52f8ab8bf 165 * On Entry:
j3 1:91e52f8ab8bf 166 *
j3 1:91e52f8ab8bf 167 * On Exit:
j3 1:91e52f8ab8bf 168 * @return TRUE if device was detected and written
j3 1:91e52f8ab8bf 169 * FALSE device not detected or failure to write
j3 1:91e52f8ab8bf 170 * configuration byte
j3 1:91e52f8ab8bf 171 **************************************************************/
j3 23:e8e403d61359 172 OneWireMaster::CmdResult detect(void);
j3 1:91e52f8ab8bf 173
j3 1:91e52f8ab8bf 174
j3 1:91e52f8ab8bf 175 /**********************************************************//**
j3 1:91e52f8ab8bf 176 * @brief reset()
j3 1:91e52f8ab8bf 177 *
j3 1:91e52f8ab8bf 178 * @details Perform a device reset on the Ds248x
j3 1:91e52f8ab8bf 179 *
j3 1:91e52f8ab8bf 180 * On Entry:
j3 1:91e52f8ab8bf 181 *
j3 1:91e52f8ab8bf 182 * On Exit:
j3 1:91e52f8ab8bf 183 * @return TRUE if device was reset
j3 1:91e52f8ab8bf 184 * FALSE device not detected or failure to perform reset
j3 1:91e52f8ab8bf 185 **************************************************************/
j3 23:e8e403d61359 186 OneWireMaster::CmdResult reset(void);
j3 1:91e52f8ab8bf 187
j3 1:91e52f8ab8bf 188
j3 1:91e52f8ab8bf 189 /**********************************************************//**
j3 1:91e52f8ab8bf 190 * @brief write_config()
j3 1:91e52f8ab8bf 191 *
j3 1:91e52f8ab8bf 192 * @details Write the configuration register in the Ds248x. The
j3 1:91e52f8ab8bf 193 * configuration options are provided in the lower nibble
j3 1:91e52f8ab8bf 194 * of the provided config byte. The uppper nibble is
j3 1:91e52f8ab8bf 195 * bitwise inverted when written to the Ds248x.
j3 1:91e52f8ab8bf 196 *
j3 1:91e52f8ab8bf 197 * On Entry:
j3 1:91e52f8ab8bf 198 * @param[in] config - lower nib of configuration register
j3 1:91e52f8ab8bf 199 *
j3 1:91e52f8ab8bf 200 * On Exit:
j3 1:91e52f8ab8bf 201 * @return TRUE: config written and response correct
j3 1:91e52f8ab8bf 202 * FALSE: response incorrect
j3 1:91e52f8ab8bf 203 **************************************************************/
j3 23:e8e403d61359 204 OneWireMaster::CmdResult write_config(uint8_t config);
j3 2:02d228c25fd4 205
j3 2:02d228c25fd4 206
j3 2:02d228c25fd4 207 /**********************************************************//**
j3 2:02d228c25fd4 208 * @brief channel_select()
j3 2:02d228c25fd4 209 *
j3 2:02d228c25fd4 210 * @details Select the 1-Wire channel on a DS2482-800.
j3 2:02d228c25fd4 211 * Min channel = 1
j3 2:02d228c25fd4 212 *
j3 2:02d228c25fd4 213 * On Entry:
j3 2:02d228c25fd4 214 * @param[in] channel - desired channel of the DS2482
j3 2:02d228c25fd4 215 *
j3 2:02d228c25fd4 216 * On Exit:
j3 2:02d228c25fd4 217 * @return TRUE if channel selected
j3 2:02d228c25fd4 218 * FALSE device not detected or failure to perform select
j3 2:02d228c25fd4 219 **************************************************************/
j3 23:e8e403d61359 220 OneWireMaster::CmdResult channel_select(uint8_t channel);
j3 1:91e52f8ab8bf 221
j3 1:91e52f8ab8bf 222
j3 1:91e52f8ab8bf 223 /**********************************************************//**
j3 1:91e52f8ab8bf 224 * @brief adjust_timing()
j3 1:91e52f8ab8bf 225 *
j3 2:02d228c25fd4 226 * @details adjustable timming available in DS2484 only
j3 1:91e52f8ab8bf 227 *
j3 1:91e52f8ab8bf 228 * On Entry:
j3 1:91e52f8ab8bf 229 * @param[in] param - 1 of 8 adjustable parameters
j3 1:91e52f8ab8bf 230 * @param[in] val - new value for parameter, see datasheet
j3 1:91e52f8ab8bf 231 * for codes
j3 1:91e52f8ab8bf 232 *
j3 1:91e52f8ab8bf 233 * On Exit:
j3 1:91e52f8ab8bf 234 * @return TRUE: parameter successfully adjusted
j3 1:91e52f8ab8bf 235 * FALSE: failed to adjust parameter
j3 1:91e52f8ab8bf 236 **************************************************************/
j3 23:e8e403d61359 237 OneWireMaster::CmdResult adjust_timing(uint8_t param, uint8_t val);
j3 1:91e52f8ab8bf 238
j3 1:91e52f8ab8bf 239
j3 1:91e52f8ab8bf 240 /**********************************************************//**
j3 1:91e52f8ab8bf 241 * @brief search_triplet()
j3 1:91e52f8ab8bf 242 *
j3 1:91e52f8ab8bf 243 * @details Use the Ds248x help command '1-Wire triplet' to perform
j3 1:91e52f8ab8bf 244 * one bit of a 1-Wire search. This command does two read
j3 1:91e52f8ab8bf 245 * bits and one write bit. The write bit is either the
j3 1:91e52f8ab8bf 246 * default direction (all device have same bit) or in case
j3 1:91e52f8ab8bf 247 * of a discrepancy, the 'search_direction' parameter is
j3 1:91e52f8ab8bf 248 * used.
j3 1:91e52f8ab8bf 249 *
j3 1:91e52f8ab8bf 250 * On Entry:
j3 1:91e52f8ab8bf 251 * @param[in] search_direction
j3 1:91e52f8ab8bf 252 *
j3 1:91e52f8ab8bf 253 * On Exit:
j3 1:91e52f8ab8bf 254 * @return The Ds248x status byte result from the triplet command
j3 1:91e52f8ab8bf 255 **************************************************************/
IanBenzMaxim 32:bce180b544ed 256 virtual OneWireMaster::CmdResult OWTriplet(SearchDirection & search_direction, std::uint8_t & sbr, std::uint8_t & tsb);
j3 1:91e52f8ab8bf 257
j3 5:ce108eeb878d 258
j3 23:e8e403d61359 259 //Part of OneWireMaster that should be implemented for each master
j3 23:e8e403d61359 260 //See OneWireMaster.h for documentation
j3 15:f6cb0d906fb6 261
j3 23:e8e403d61359 262 virtual OneWireMaster::CmdResult OWInitMaster(void);
j3 14:7b2886a50321 263
j3 23:e8e403d61359 264 virtual OneWireMaster::CmdResult OWReset(void);
j3 1:91e52f8ab8bf 265
IanBenzMaxim 32:bce180b544ed 266 virtual OneWireMaster::CmdResult OWTouchBit(uint8_t & sendrecvbit, OWLevel after_level);
IanBenzMaxim 26:a361e3f42ba5 267
IanBenzMaxim 32:bce180b544ed 268 virtual OneWireMaster::CmdResult OWReadByte(uint8_t & recvbyte, OWLevel after_level);
IanBenzMaxim 26:a361e3f42ba5 269
IanBenzMaxim 32:bce180b544ed 270 virtual OneWireMaster::CmdResult OWWriteByte(uint8_t sendbyte, OWLevel after_level);
j3 17:b646b1e3970b 271
j3 23:e8e403d61359 272 virtual OneWireMaster::CmdResult OWWriteBlock(const uint8_t *tran_buf, uint8_t tran_len);
j3 1:91e52f8ab8bf 273
j3 23:e8e403d61359 274 virtual OneWireMaster::CmdResult OWReadBlock(uint8_t *rx_buf, uint8_t rx_len);
j3 3:644fc630f958 275
IanBenzMaxim 32:bce180b544ed 276 virtual OneWireMaster::CmdResult OWSetSpeed(OWSpeed new_speed);
j3 3:644fc630f958 277
IanBenzMaxim 32:bce180b544ed 278 virtual OneWireMaster::CmdResult OWSetLevel(OWLevel new_level);
j3 2:02d228c25fd4 279
IanBenzMaxim 26:a361e3f42ba5 280 OneWireMaster::CmdResult ConfigureSPU(bool spu_enable);
IanBenzMaxim 21:00c94aeb533e 281
j3 1:91e52f8ab8bf 282 private:
j3 1:91e52f8ab8bf 283
j3 6:1faafa0b3cd7 284 //private fx for initializing _w_adrs and _r_adrs
j3 6:1faafa0b3cd7 285 void set_i2c_adrs(DS248X_I2C_ADRS adrs);
j3 5:ce108eeb878d 286
j3 1:91e52f8ab8bf 287 I2C *_p_i2c_bus;
j3 1:91e52f8ab8bf 288 uint8_t _w_adrs, _r_adrs;
j3 5:ce108eeb878d 289 bool _i2c_owner;
j3 2:02d228c25fd4 290
j3 2:02d228c25fd4 291 // ds248x state
j3 2:02d228c25fd4 292 uint8_t _short_detected;
j3 15:f6cb0d906fb6 293 uint8_t _c1WS, _cSPU, _cPDN, _cAPU;
j3 1:91e52f8ab8bf 294 };
j3 1:91e52f8ab8bf 295
j3 1:91e52f8ab8bf 296 #endif /*DS248X_H*/