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:
Fri May 13 14:52:50 2016 -0500
Revision:
75:8b627804927c
Parent:
74:23be10c32fa3
Child:
76:84e6c4994e29
Code cleanup.

Who changed what in which revision?

UserRevisionLine numberNew contents of line
j3 5:ce108eeb878d 1 /******************************************************************//**
j3 5:ce108eeb878d 2 * Copyright (C) 2016 Maxim Integrated Products, Inc., All Rights Reserved.
j3 5:ce108eeb878d 3 *
j3 5:ce108eeb878d 4 * Permission is hereby granted, free of charge, to any person obtaining a
j3 5:ce108eeb878d 5 * copy of this software and associated documentation files (the "Software"),
j3 5:ce108eeb878d 6 * to deal in the Software without restriction, including without limitation
j3 5:ce108eeb878d 7 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
j3 5:ce108eeb878d 8 * and/or sell copies of the Software, and to permit persons to whom the
j3 5:ce108eeb878d 9 * Software is furnished to do so, subject to the following conditions:
j3 5:ce108eeb878d 10 *
j3 5:ce108eeb878d 11 * The above copyright notice and this permission notice shall be included
j3 5:ce108eeb878d 12 * in all copies or substantial portions of the Software.
j3 5:ce108eeb878d 13 *
j3 5:ce108eeb878d 14 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
j3 5:ce108eeb878d 15 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
j3 5:ce108eeb878d 16 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
j3 5:ce108eeb878d 17 * IN NO EVENT SHALL MAXIM INTEGRATED BE LIABLE FOR ANY CLAIM, DAMAGES
j3 5:ce108eeb878d 18 * OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
j3 5:ce108eeb878d 19 * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
j3 5:ce108eeb878d 20 * OTHER DEALINGS IN THE SOFTWARE.
j3 5:ce108eeb878d 21 *
j3 5:ce108eeb878d 22 * Except as contained in this notice, the name of Maxim Integrated
j3 5:ce108eeb878d 23 * Products, Inc. shall not be used except as stated in the Maxim Integrated
j3 5:ce108eeb878d 24 * Products, Inc. Branding Policy.
j3 5:ce108eeb878d 25 *
j3 5:ce108eeb878d 26 * The mere transfer of this software does not imply any licenses
j3 5:ce108eeb878d 27 * of trade secrets, proprietary technology, copyrights, patents,
j3 5:ce108eeb878d 28 * trademarks, maskwork rights, or any other form of intellectual
j3 5:ce108eeb878d 29 * property whatsoever. Maxim Integrated Products, Inc. retains all
j3 5:ce108eeb878d 30 * ownership rights.
j3 5:ce108eeb878d 31 **********************************************************************/
j3 5:ce108eeb878d 32
IanBenzMaxim 73:2cecc1372acc 33 #ifndef OneWire_Masters_OneWireMaster
IanBenzMaxim 73:2cecc1372acc 34 #define OneWire_Masters_OneWireMaster
j3 5:ce108eeb878d 35
IanBenzMaxim 73:2cecc1372acc 36 #include <stdint.h>
IanBenzMaxim 73:2cecc1372acc 37 #include <stddef.h>
IanBenzMaxim 73:2cecc1372acc 38 #include "RomId.h"
IanBenzMaxim 73:2cecc1372acc 39
IanBenzMaxim 73:2cecc1372acc 40 namespace OneWire
IanBenzMaxim 73:2cecc1372acc 41 {
IanBenzMaxim 73:2cecc1372acc 42 namespace Masters
IanBenzMaxim 32:bce180b544ed 43 {
IanBenzMaxim 73:2cecc1372acc 44 /// Base class for all 1-Wire Masters.
IanBenzMaxim 73:2cecc1372acc 45 class OneWireMaster
IanBenzMaxim 74:23be10c32fa3 46 {
IanBenzMaxim 73:2cecc1372acc 47 public:
IanBenzMaxim 73:2cecc1372acc 48 /// Speed of the 1-Wire bus
IanBenzMaxim 73:2cecc1372acc 49 enum OWSpeed
IanBenzMaxim 73:2cecc1372acc 50 {
IanBenzMaxim 75:8b627804927c 51 StandardSpeed = 0x00,
IanBenzMaxim 75:8b627804927c 52 OverdriveSpeed = 0x01
IanBenzMaxim 73:2cecc1372acc 53 };
IanBenzMaxim 74:23be10c32fa3 54
IanBenzMaxim 73:2cecc1372acc 55 /// Level of the 1-Wire bus
IanBenzMaxim 73:2cecc1372acc 56 enum OWLevel
IanBenzMaxim 73:2cecc1372acc 57 {
IanBenzMaxim 75:8b627804927c 58 NormalLevel = 0x00,
IanBenzMaxim 75:8b627804927c 59 StrongLevel = 0x02
IanBenzMaxim 73:2cecc1372acc 60 };
IanBenzMaxim 74:23be10c32fa3 61
IanBenzMaxim 73:2cecc1372acc 62 /// Search direction for the Triplet
IanBenzMaxim 73:2cecc1372acc 63 enum SearchDirection
IanBenzMaxim 73:2cecc1372acc 64 {
IanBenzMaxim 75:8b627804927c 65 WriteZero = 0,
IanBenzMaxim 75:8b627804927c 66 WriteOne = 1
IanBenzMaxim 73:2cecc1372acc 67 };
IanBenzMaxim 74:23be10c32fa3 68
IanBenzMaxim 73:2cecc1372acc 69 /// Result of all 1-Wire commands
IanBenzMaxim 73:2cecc1372acc 70 enum CmdResult
IanBenzMaxim 73:2cecc1372acc 71 {
IanBenzMaxim 73:2cecc1372acc 72 Success,
IanBenzMaxim 73:2cecc1372acc 73 CommunicationWriteError,
IanBenzMaxim 73:2cecc1372acc 74 CommunicationReadError,
IanBenzMaxim 73:2cecc1372acc 75 TimeoutError,
IanBenzMaxim 73:2cecc1372acc 76 OperationFailure
IanBenzMaxim 73:2cecc1372acc 77 };
IanBenzMaxim 74:23be10c32fa3 78
IanBenzMaxim 73:2cecc1372acc 79 /// State used by all ROM ID search functions.
IanBenzMaxim 73:2cecc1372acc 80 struct SearchState
IanBenzMaxim 73:2cecc1372acc 81 {
IanBenzMaxim 73:2cecc1372acc 82 RomId romId;
IanBenzMaxim 73:2cecc1372acc 83 uint8_t last_discrepancy;
IanBenzMaxim 73:2cecc1372acc 84 uint8_t last_family_discrepancy;
IanBenzMaxim 73:2cecc1372acc 85 bool last_device_flag;
IanBenzMaxim 74:23be10c32fa3 86
IanBenzMaxim 73:2cecc1372acc 87 /// Reset to the search state to start at the beginning.
IanBenzMaxim 73:2cecc1372acc 88 void reset()
IanBenzMaxim 73:2cecc1372acc 89 {
IanBenzMaxim 73:2cecc1372acc 90 last_discrepancy = 0;
IanBenzMaxim 73:2cecc1372acc 91 last_device_flag = false;
IanBenzMaxim 73:2cecc1372acc 92 last_family_discrepancy = 0;
IanBenzMaxim 73:2cecc1372acc 93 romId.reset();
IanBenzMaxim 73:2cecc1372acc 94 }
IanBenzMaxim 74:23be10c32fa3 95
IanBenzMaxim 73:2cecc1372acc 96 SearchState() { reset(); }
IanBenzMaxim 73:2cecc1372acc 97 };
IanBenzMaxim 74:23be10c32fa3 98
IanBenzMaxim 73:2cecc1372acc 99 /// Perform a CRC16 calculation.
IanBenzMaxim 75:8b627804927c 100 /// @param crc16 Beginning state of the CRC generator.
IanBenzMaxim 73:2cecc1372acc 101 /// @param data Data to pass though the CRC generator.
IanBenzMaxim 73:2cecc1372acc 102 /// @returns The calculated CRC16.
IanBenzMaxim 75:8b627804927c 103 static uint16_t calculateCrc16(uint16_t crc16, uint16_t data);
IanBenzMaxim 74:23be10c32fa3 104
IanBenzMaxim 73:2cecc1372acc 105 /// Perform a CRC16 calculation with variable length data.
IanBenzMaxim 73:2cecc1372acc 106 /// @param[in] data Data array to pass through the CRC generator.
IanBenzMaxim 73:2cecc1372acc 107 /// @param data_offset Offset of the data array to begin processing.
IanBenzMaxim 73:2cecc1372acc 108 /// @param data_len Length of the data array to process.
IanBenzMaxim 73:2cecc1372acc 109 /// @param crc Beginning state of the CRC generator.
IanBenzMaxim 73:2cecc1372acc 110 /// @returns The calculated CRC16.
IanBenzMaxim 75:8b627804927c 111 static uint16_t calculateCrc16(const uint8_t * data, size_t dataOffset, size_t dataLen, uint16_t crc = 0);
IanBenzMaxim 74:23be10c32fa3 112
IanBenzMaxim 73:2cecc1372acc 113 /// Allow freeing through a base class pointer.
IanBenzMaxim 73:2cecc1372acc 114 virtual ~OneWireMaster() { }
IanBenzMaxim 74:23be10c32fa3 115
IanBenzMaxim 75:8b627804927c 116 /// Initialize a master for use.
IanBenzMaxim 75:8b627804927c 117 virtual CmdResult OWInitMaster() = 0;
IanBenzMaxim 75:8b627804927c 118
IanBenzMaxim 75:8b627804927c 119 /// Reset all of the devices on the 1-Wire bus and check for a presence pulse.
IanBenzMaxim 75:8b627804927c 120 /// @returns OperationFailure if reset was performed but no presence pulse was detected.
IanBenzMaxim 75:8b627804927c 121 virtual CmdResult OWReset() = 0;
IanBenzMaxim 74:23be10c32fa3 122
IanBenzMaxim 75:8b627804927c 123 /// Send and receive one bit of communication and set a new level on the 1-Wire bus.
IanBenzMaxim 75:8b627804927c 124 /// @param[in,out] sendRecvBit Buffer containing the bit to send on 1-Wire bus in lsb.
IanBenzMaxim 75:8b627804927c 125 /// Read data from 1-Wire bus will be returned in lsb.
IanBenzMaxim 75:8b627804927c 126 /// @param afterLevel Level to set the 1-Wire bus to after communication.
IanBenzMaxim 75:8b627804927c 127 virtual CmdResult OWTouchBitSetLevel(uint8_t & sendRecvBit, OWLevel afterLevel) = 0;
IanBenzMaxim 75:8b627804927c 128
IanBenzMaxim 75:8b627804927c 129 /// Send one byte of communication and set a new level on the 1-Wire bus.
IanBenzMaxim 75:8b627804927c 130 /// @param sendByte Byte to send on the 1-Wire bus.
IanBenzMaxim 75:8b627804927c 131 /// @param afterLevel Level to set the 1-Wire bus to after communication.
IanBenzMaxim 75:8b627804927c 132 virtual CmdResult OWWriteByteSetLevel(uint8_t sendByte, OWLevel afterLevel) = 0;
IanBenzMaxim 74:23be10c32fa3 133
IanBenzMaxim 75:8b627804927c 134 /// Receive one byte of communication and set a new level on the 1-Wire bus.
IanBenzMaxim 75:8b627804927c 135 /// @param recvByte Buffer to receive the data from the 1-Wire bus.
IanBenzMaxim 75:8b627804927c 136 /// @param afterLevel Level to set the 1-Wire bus to after communication.
IanBenzMaxim 75:8b627804927c 137 virtual CmdResult OWReadByteSetLevel(uint8_t & recvByte, OWLevel afterLevel) = 0;
IanBenzMaxim 75:8b627804927c 138
IanBenzMaxim 75:8b627804927c 139 /// Send a block of communication on the 1-Wire bus.
IanBenzMaxim 75:8b627804927c 140 /// @param[in] sendBuf Buffer to send on the 1-Wire bus.
IanBenzMaxim 75:8b627804927c 141 /// @param sendLen Length of the buffer to send.
IanBenzMaxim 75:8b627804927c 142 virtual CmdResult OWWriteBlock(const uint8_t *sendBuf, uint8_t sendLen);
IanBenzMaxim 74:23be10c32fa3 143
IanBenzMaxim 75:8b627804927c 144 /// Receive a block of communication on the 1-Wire bus.
IanBenzMaxim 75:8b627804927c 145 /// @param[out] recvBuf Buffer to receive the data from the 1-Wire bus.
IanBenzMaxim 75:8b627804927c 146 /// @param recvLen Length of the buffer to receive.
IanBenzMaxim 75:8b627804927c 147 virtual CmdResult OWReadBlock(uint8_t *recvBuf, uint8_t recvLen);
IanBenzMaxim 75:8b627804927c 148
IanBenzMaxim 75:8b627804927c 149 /// Set the 1-Wire bus communication speed.
IanBenzMaxim 75:8b627804927c 150 virtual CmdResult OWSetSpeed(OWSpeed newSpeed) = 0;
IanBenzMaxim 75:8b627804927c 151
IanBenzMaxim 75:8b627804927c 152 /// Set the 1-Wire bus level.
IanBenzMaxim 75:8b627804927c 153 virtual CmdResult OWSetLevel(OWLevel newLevel) = 0;
IanBenzMaxim 74:23be10c32fa3 154
IanBenzMaxim 73:2cecc1372acc 155 /**********************************************************//**
IanBenzMaxim 75:8b627804927c 156 * @brief 1-Wire Triplet operation.
IanBenzMaxim 74:23be10c32fa3 157 *
IanBenzMaxim 73:2cecc1372acc 158 * @details Perform one bit of a 1-Wire search. This command
IanBenzMaxim 73:2cecc1372acc 159 * does two read bits and one write bit. The write bit is either
IanBenzMaxim 73:2cecc1372acc 160 * the default direction (all device have same bit) or in case
IanBenzMaxim 73:2cecc1372acc 161 * of a discrepancy, the 'search_direction' parameter is used.
IanBenzMaxim 74:23be10c32fa3 162 *
IanBenzMaxim 73:2cecc1372acc 163 * @param[in,out] search_direction
IanBenzMaxim 73:2cecc1372acc 164 * Input with desired direction in case both read bits are zero.
IanBenzMaxim 73:2cecc1372acc 165 * Output with direction taken based on read bits.
IanBenzMaxim 74:23be10c32fa3 166 *
IanBenzMaxim 73:2cecc1372acc 167 * @param[out] sbr Bit result of first read operation.
IanBenzMaxim 73:2cecc1372acc 168 * @param[out] tsb Bit result of second read operation.
IanBenzMaxim 73:2cecc1372acc 169 **************************************************************/
IanBenzMaxim 75:8b627804927c 170 virtual CmdResult OWTriplet(SearchDirection & searchDirection, uint8_t & sbr, uint8_t & tsb);
IanBenzMaxim 74:23be10c32fa3 171
IanBenzMaxim 75:8b627804927c 172 /// Send one bit of communication and set a new level on the 1-Wire bus.
IanBenzMaxim 75:8b627804927c 173 /// @param sendBit Buffer containing the bit to send on 1-Wire bus in lsb.
IanBenzMaxim 75:8b627804927c 174 /// @param afterLevel Level to set the 1-Wire bus to after communication.
IanBenzMaxim 75:8b627804927c 175 CmdResult OWWriteBitSetLevel(uint8_t sendBit, OWLevel afterLevel) { return OWTouchBitSetLevel(sendBit, afterLevel); }
IanBenzMaxim 74:23be10c32fa3 176
IanBenzMaxim 75:8b627804927c 177 /// Receive one bit of communication and set a new level on the 1-Wire bus.
IanBenzMaxim 75:8b627804927c 178 /// @param[out] sendRecvBit Read data from 1-Wire bus will be returned in lsb.
IanBenzMaxim 75:8b627804927c 179 /// @param afterLevel Level to set the 1-Wire bus to after communication.
IanBenzMaxim 75:8b627804927c 180 CmdResult OWReadBitSetLevel(uint8_t & recvBit, OWLevel afterLevel) { recvBit = 0x01; return OWTouchBitSetLevel(recvBit, afterLevel); }
IanBenzMaxim 74:23be10c32fa3 181
IanBenzMaxim 73:2cecc1372acc 182 // Alternate forms of read and write functions
IanBenzMaxim 75:8b627804927c 183 CmdResult OWWriteBit(uint8_t sendBit) { return OWWriteBitSetLevel(sendBit, NormalLevel); }
IanBenzMaxim 75:8b627804927c 184 CmdResult OWReadBit(uint8_t & recvBit) { return OWReadBitSetLevel(recvBit, NormalLevel); }
IanBenzMaxim 75:8b627804927c 185 CmdResult OWWriteBitPower(uint8_t sendBit) { return OWWriteBitSetLevel(sendBit, StrongLevel); }
IanBenzMaxim 75:8b627804927c 186 CmdResult OWReadBitPower(uint8_t & recvBit) { return OWReadBitSetLevel(recvBit, StrongLevel); }
IanBenzMaxim 75:8b627804927c 187 CmdResult OWWriteByte(uint8_t sendByte) { return OWWriteByteSetLevel(sendByte, NormalLevel); }
IanBenzMaxim 75:8b627804927c 188 CmdResult OWReadByte(uint8_t & recvByte) { return OWReadByteSetLevel(recvByte, NormalLevel); }
IanBenzMaxim 75:8b627804927c 189 CmdResult OWWriteBytePower(uint8_t sendByte) { return OWWriteByteSetLevel(sendByte, StrongLevel); }
IanBenzMaxim 75:8b627804927c 190 CmdResult OWReadBytePower(uint8_t & recvByte) { return OWReadByteSetLevel(recvByte, StrongLevel); }
IanBenzMaxim 74:23be10c32fa3 191
IanBenzMaxim 75:8b627804927c 192 /// Find the 'first' devices on the 1-Wire network.
IanBenzMaxim 73:2cecc1372acc 193 CmdResult OWFirst(SearchState & searchState);
IanBenzMaxim 74:23be10c32fa3 194
IanBenzMaxim 75:8b627804927c 195 /// Find the 'next' devices on the 1-Wire network.
IanBenzMaxim 73:2cecc1372acc 196 CmdResult OWNext(SearchState & searchState);
IanBenzMaxim 74:23be10c32fa3 197
IanBenzMaxim 75:8b627804927c 198 /// Verify that the device with the specified ROM ID is present.
IanBenzMaxim 73:2cecc1372acc 199 CmdResult OWVerify(const RomId & romId);
IanBenzMaxim 74:23be10c32fa3 200
IanBenzMaxim 75:8b627804927c 201 /// Setup the search to find the device type 'family_code'
IanBenzMaxim 75:8b627804927c 202 /// on the next call to OWNext() if it is present.
IanBenzMaxim 73:2cecc1372acc 203 void OWTargetSetup(SearchState & searchState);
IanBenzMaxim 74:23be10c32fa3 204
IanBenzMaxim 75:8b627804927c 205 /// Setup the search to skip the current device type on the
IanBenzMaxim 75:8b627804927c 206 /// next call to OWNext().
IanBenzMaxim 73:2cecc1372acc 207 void OWFamilySkipSetup(SearchState & searchState);
IanBenzMaxim 74:23be10c32fa3 208
IanBenzMaxim 75:8b627804927c 209 /// Use Read ROM command to read ROM ID from device on bus.
IanBenzMaxim 75:8b627804927c 210 /// @note Only use this command with a single drop bus, data
IanBenzMaxim 75:8b627804927c 211 /// collisions will occur if more than 1 device on bus.
IanBenzMaxim 75:8b627804927c 212 /// @param[out] romId ROM ID read from device.
IanBenzMaxim 75:8b627804927c 213 CmdResult OWReadRom(RomId & romId);
IanBenzMaxim 75:8b627804927c 214
IanBenzMaxim 75:8b627804927c 215 /// Issue Skip ROM command on bus.
IanBenzMaxim 75:8b627804927c 216 /// @note Only use this command with a single drop bus, data
IanBenzMaxim 75:8b627804927c 217 /// collisions will occur if more than 1 device on bus.
IanBenzMaxim 75:8b627804927c 218 CmdResult OWSkipRom();
IanBenzMaxim 75:8b627804927c 219
IanBenzMaxim 75:8b627804927c 220 /// Use the Match ROM command to select the device by its known ID.
IanBenzMaxim 75:8b627804927c 221 /// @param[in] romId ROM ID of device to select.
IanBenzMaxim 75:8b627804927c 222 CmdResult OWMatchRom(const RomId & romId);
IanBenzMaxim 75:8b627804927c 223
IanBenzMaxim 75:8b627804927c 224 /// Issue Overdrive Skip ROM command on bus.
IanBenzMaxim 75:8b627804927c 225 /// @details This command causes all devices supporting Overdrive
IanBenzMaxim 75:8b627804927c 226 /// mode to switch to Overdrive timing.
IanBenzMaxim 75:8b627804927c 227 /// @note Only use this command with a single drop bus, data
IanBenzMaxim 75:8b627804927c 228 /// collisions will occur if more than 1 device on bus.
IanBenzMaxim 75:8b627804927c 229 CmdResult OWOverdriveSkipRom();
IanBenzMaxim 75:8b627804927c 230
IanBenzMaxim 75:8b627804927c 231
IanBenzMaxim 75:8b627804927c 232 CmdResult OWOverdriveMatchRom(const RomId & romId);
IanBenzMaxim 75:8b627804927c 233
IanBenzMaxim 75:8b627804927c 234 /// Perform a Resume ROM command on bus.
IanBenzMaxim 75:8b627804927c 235 /// @details Resumes communication with the last device selected
IanBenzMaxim 75:8b627804927c 236 /// though a Match ROM or Search ROM operation.
IanBenzMaxim 75:8b627804927c 237 CmdResult OWResume();
IanBenzMaxim 75:8b627804927c 238
IanBenzMaxim 73:2cecc1372acc 239 /**********************************************************//**
IanBenzMaxim 75:8b627804927c 240 * @brief Enumerate all devices on the 1-Wire bus.
IanBenzMaxim 74:23be10c32fa3 241 *
IanBenzMaxim 74:23be10c32fa3 242 * @details The 'OWSearch' function does a general search. This
IanBenzMaxim 74:23be10c32fa3 243 * function continues from the previous search state. The
IanBenzMaxim 74:23be10c32fa3 244 * search state can be reset by using the 'OWFirst'
IanBenzMaxim 74:23be10c32fa3 245 * function. This function contains one parameter
IanBenzMaxim 74:23be10c32fa3 246 * 'alarm_only'. When 'alarm_only' is TRUE (1) the find
IanBenzMaxim 74:23be10c32fa3 247 * alarm command 0xEC is sent instead of the normal search
IanBenzMaxim 74:23be10c32fa3 248 * command 0xF0. Using the find alarm command 0xEC will
IanBenzMaxim 74:23be10c32fa3 249 * limit the search to only 1-Wire devices that are in an
IanBenzMaxim 73:2cecc1372acc 250 * 'alarm' state.
IanBenzMaxim 73:2cecc1372acc 251 **************************************************************/
IanBenzMaxim 73:2cecc1372acc 252 CmdResult OWSearch(SearchState & searchState);
IanBenzMaxim 73:2cecc1372acc 253 };
IanBenzMaxim 73:2cecc1372acc 254 }
IanBenzMaxim 73:2cecc1372acc 255 }
j3 15:f6cb0d906fb6 256
IanBenzMaxim 74:23be10c32fa3 257 #endif