Implementation of 1-Wire with added Alarm Search Functionality

Dependents:   Max32630_One_Wire_Interface

Revision:
75:8b627804927c
Parent:
74:23be10c32fa3
Child:
76:84e6c4994e29
--- a/Masters/OneWireMaster.h	Fri May 13 07:48:35 2016 -0500
+++ b/Masters/OneWireMaster.h	Fri May 13 14:52:50 2016 -0500
@@ -35,7 +35,6 @@
 
 #include <stdint.h>
 #include <stddef.h>
-
 #include "RomId.h"
 
 namespace OneWire
@@ -49,23 +48,22 @@
             /// Speed of the 1-Wire bus
             enum OWSpeed
             {
-                SPEED_STANDARD = 0x00,
-                SPEED_OVERDRIVE = 0x01
+                StandardSpeed = 0x00,
+                OverdriveSpeed = 0x01
             };
 
             /// Level of the 1-Wire bus
             enum OWLevel
             {
-                LEVEL_NORMAL = 0x00,
-                LEVEL_STRONG = 0x02,
-                LEVEL_PROGRAM = 0x04,
+                NormalLevel = 0x00,
+                StrongLevel = 0x02
             };
 
             /// Search direction for the Triplet
             enum SearchDirection
             {
-                DIRECTION_WRITE_ZERO = 0,
-                DIRECTION_WRITE_ONE = 1
+                WriteZero = 0,
+                WriteOne = 1
             };
 
             /// Result of all 1-Wire commands
@@ -99,10 +97,10 @@
             };
 
             /// Perform a CRC16 calculation.
-            /// @param CRC16 Beginning state of the CRC generator.
+            /// @param crc16 Beginning state of the CRC generator.
             /// @param data Data to pass though the CRC generator.
             /// @returns The calculated CRC16.
-            static uint16_t calculateCRC16(uint16_t CRC16, uint16_t data);
+            static uint16_t calculateCrc16(uint16_t crc16, uint16_t data);
 
             /// Perform a CRC16 calculation with variable length data.
             /// @param[in] data Data array to pass through the CRC generator.
@@ -110,151 +108,52 @@
             /// @param data_len Length of the data array to process.
             /// @param crc Beginning state of the CRC generator.
             /// @returns The calculated CRC16.
-            static uint16_t calculateCRC16(const uint8_t * data, size_t data_offset, size_t data_len, uint16_t crc = 0);
+            static uint16_t calculateCrc16(const uint8_t * data, size_t dataOffset, size_t dataLen, uint16_t crc = 0);
 
             /// Allow freeing through a base class pointer.
             virtual ~OneWireMaster() { }
 
-            /**********************************************************//**
-            * @brief OWInitMaster()
-            *
-            * @details Initiializes particular master being instaniated.
-            *  Added to interface to provide a common 'init' function between
-            *  all masters
-            *
-            * On Entry:
-            *
-            * On Exit:
-            *
-            **************************************************************/
-            virtual CmdResult OWInitMaster(void) = 0;
+            /// Initialize a master for use.
+            virtual CmdResult OWInitMaster() = 0;
+
+            /// Reset all of the devices on the 1-Wire bus and check for a presence pulse.
+            /// @returns OperationFailure if reset was performed but no presence pulse was detected.
+            virtual CmdResult OWReset() = 0;
 
-            /**********************************************************//**
-            * @brief OWReset()
-            *
-            * @details Reset all of the devices on the 1-Wire Net and return
-            *          the result.
-            *
-            * On Entry:
-            *
-            * On Exit:
-            *
-            * @returns OperationFailure if reset was performed but no
-            *          presence pulse was detected.
-            **************************************************************/
-            virtual CmdResult OWReset(void) = 0;
+            /// Send and receive one bit of communication and set a new level on the 1-Wire bus.
+            /// @param[in,out] sendRecvBit Buffer containing the bit to send on 1-Wire bus in lsb.
+            ///                            Read data from 1-Wire bus will be returned in lsb.
+            /// @param afterLevel Level to set the 1-Wire bus to after communication.
+            virtual CmdResult OWTouchBitSetLevel(uint8_t & sendRecvBit, OWLevel afterLevel) = 0;
+
+            /// Send one byte of communication and set a new level on the 1-Wire bus.
+            /// @param sendByte Byte to send on the 1-Wire bus.
+            /// @param afterLevel Level to set the 1-Wire bus to after communication.
+            virtual CmdResult OWWriteByteSetLevel(uint8_t sendByte, OWLevel afterLevel) = 0;
 
-            /**********************************************************//**
-            * @brief OWTouchBit()
-            *
-            * @details Send 1 bit of communication to the 1-Wire Net and return
-            *          the result 1 bit read from the 1-Wire Net.  The
-            *          parameter 'sendbit' least significant bit is used and
-            *          the least significant bit of the result is the return
-            *          bit.
-            *
-            * On Entry:
-            *     @param[in] 'sendbit' - the least significant bit is the bit to send
-            *
-            * On Exit:
-            *
-            **************************************************************/
-            virtual CmdResult OWTouchBitSetLevel(uint8_t & sendrecvbit, OWLevel after_level) = 0;
+            /// Receive one byte of communication and set a new level on the 1-Wire bus.
+            /// @param recvByte Buffer to receive the data from the 1-Wire bus.
+            /// @param afterLevel Level to set the 1-Wire bus to after communication.
+            virtual CmdResult OWReadByteSetLevel(uint8_t & recvByte, OWLevel afterLevel) = 0;
+
+            /// Send a block of communication on the 1-Wire bus.
+            /// @param[in] sendBuf Buffer to send on the 1-Wire bus.
+            /// @param sendLen Length of the buffer to send.
+            virtual CmdResult OWWriteBlock(const uint8_t *sendBuf, uint8_t sendLen);
 
-            /**********************************************************//**
-            * @brief OWWRiteByte()
-            *
-            * @details Send 8 bits of communication to the 1-Wire Net and
-            *          verify that the 8 bits read from the 1-Wire Net is the
-            *          same (write operation).The parameter 'sendbyte' least
-            *          significant 8 bits are used.
-            *
-            * On Entry:
-            *     @param[in] 'sendbyte' - 8 bits to send (least significant byte)
-            *
-            * On Exit:
-            *
-            **************************************************************/
-            virtual CmdResult OWWriteByteSetLevel(uint8_t sendbyte, OWLevel after_level) = 0;
+            /// Receive a block of communication on the 1-Wire bus.
+            /// @param[out] recvBuf Buffer to receive the data from the 1-Wire bus.
+            /// @param recvLen Length of the buffer to receive.
+            virtual CmdResult OWReadBlock(uint8_t *recvBuf, uint8_t recvLen);
+
+            /// Set the 1-Wire bus communication speed.
+            virtual CmdResult OWSetSpeed(OWSpeed newSpeed) = 0;
+
+            /// Set the 1-Wire bus level.
+            virtual CmdResult OWSetLevel(OWLevel newLevel) = 0;
 
             /**********************************************************//**
-            * @brief OWReadByte()
-            *
-            * @details Send 8 bits of read communication to the 1-Wire Net
-            *          and return the result 8 bits read from the 1-Wire Net.
-            *
-            * On Entry:
-            *
-            * On Exit:
-            *
-            **************************************************************/
-            virtual CmdResult OWReadByteSetLevel(uint8_t & recvbyte, OWLevel after_level) = 0;
-
-            /**********************************************************//**
-            * @brief OWWriteBlock()
-            *
-            * @details complements OWBlock, writes 'tran_len' bytes from
-            * 'tran_buf' to 1-wire Net.
-            *
-            * On Entry:
-            *     @param[in] tran_buf - pointer to data to write
-            *     @param[in] tran_len - number of bytes to write
-            *
-            * On Exit:
-            *
-            **************************************************************/
-            virtual CmdResult OWWriteBlock(const uint8_t *tran_buf, uint8_t tran_len);
-
-            /**********************************************************//**
-            * @brief OWReadBlock()
-            *
-            * @details complements OWBlock, reads 'recv_len' bytes from
-            * 1-wire Net and puts them in 'recv_buf'.
-            *
-            * On Entry:
-            *     @param[in] recv_buf - pointer to receive buffer
-            *     @param[in] recv_len - number of bytes to read
-            *
-            * On Exit:
-            *
-            **************************************************************/
-            virtual CmdResult OWReadBlock(uint8_t *rx_buf, uint8_t rx_len);
-
-            /**********************************************************//**
-            * @brief OWSetSpeed()
-            *
-            * @details Set the 1-Wire Net communication speed.
-            *
-            * On Entry:
-            *     @param[in] 'new_speed' - new speed defined as
-            *                              MODE_STANDARD   0x00
-            *                              MODE_OVERDRIVE  0x01
-            *
-            * On Exit:
-            *
-            **************************************************************/
-            virtual CmdResult OWSetSpeed(OWSpeed new_speed) = 0;
-
-            /**********************************************************//**
-            * @brief OWSetLevel()
-            *
-            * @details Set the 1-Wire Net line level pull-up to normal. The
-            *          ds2484 does only allows enabling strong pull-up on a
-            *          bit or byte event. Consequently this function only
-            *          allows the MODE_STANDARD argument. To enable strong
-            *          pull-up use OWWriteBytePower or OWReadBitPower.
-            *
-            * On Entry:
-            *     @param[in] 'new_level' - new level defined as
-            *                              MODE_STANDARD     0x00
-            *
-            * On Exit:
-            *
-            **************************************************************/
-            virtual CmdResult OWSetLevel(OWLevel new_level) = 0;
-
-            /**********************************************************//**
-            * @brief OWTriplet()
+            * @brief 1-Wire Triplet operation.
             *
             * @details Perform one bit of a 1-Wire search. This command
             * does two read bits and one write bit. The write bit is either
@@ -268,171 +167,77 @@
             * @param[out] sbr Bit result of first read operation.
             * @param[out] tsb Bit result of second read operation.
             **************************************************************/
-            virtual CmdResult OWTriplet(SearchDirection & search_direction, uint8_t & sbr, uint8_t & tsb);
+            virtual CmdResult OWTriplet(SearchDirection & searchDirection, uint8_t & sbr, uint8_t & tsb);
 
-            /// OWWriteBit()
-            CmdResult OWWriteBitSetLevel(uint8_t sendbit, OWLevel after_level) { return OWTouchBitSetLevel(sendbit, after_level); }
+            /// Send one bit of communication and set a new level on the 1-Wire bus.
+            /// @param sendBit Buffer containing the bit to send on 1-Wire bus in lsb.
+            /// @param afterLevel Level to set the 1-Wire bus to after communication.
+            CmdResult OWWriteBitSetLevel(uint8_t sendBit, OWLevel afterLevel) { return OWTouchBitSetLevel(sendBit, afterLevel); }
 
-            /// OWReadBit()
-            CmdResult OWReadBitSetLevel(uint8_t & recvbit, OWLevel after_level) { recvbit = 0x01; return OWTouchBitSetLevel(recvbit, after_level); }
+            /// Receive one bit of communication and set a new level on the 1-Wire bus.
+            /// @param[out] sendRecvBit Read data from 1-Wire bus will be returned in lsb.
+            /// @param afterLevel Level to set the 1-Wire bus to after communication.
+            CmdResult OWReadBitSetLevel(uint8_t & recvBit, OWLevel afterLevel) { recvBit = 0x01; return OWTouchBitSetLevel(recvBit, afterLevel); }
 
             // Alternate forms of read and write functions
-            CmdResult OWWriteBit(uint8_t sendbit) { return OWWriteBitSetLevel(sendbit, LEVEL_NORMAL); }
-            CmdResult OWReadBit(uint8_t & recvbit) { return OWReadBitSetLevel(recvbit, LEVEL_NORMAL); }
-            CmdResult OWWriteBitPower(uint8_t sendbit) { return OWWriteBitSetLevel(sendbit, LEVEL_STRONG); }
-            CmdResult OWReadBitPower(uint8_t & recvbit) { return OWReadBitSetLevel(recvbit, LEVEL_STRONG); }
-            CmdResult OWWriteByte(uint8_t sendbyte) { return OWWriteByteSetLevel(sendbyte, LEVEL_NORMAL); }
-            CmdResult OWReadByte(uint8_t & recvbyte) { return OWReadByteSetLevel(recvbyte, LEVEL_NORMAL); }
-            CmdResult OWWriteBytePower(uint8_t sendbyte) { return OWWriteByteSetLevel(sendbyte, LEVEL_STRONG); }
-            CmdResult OWReadBytePower(uint8_t & recvbyte) { return OWReadByteSetLevel(recvbyte, LEVEL_STRONG); }
+            CmdResult OWWriteBit(uint8_t sendBit) { return OWWriteBitSetLevel(sendBit, NormalLevel); }
+            CmdResult OWReadBit(uint8_t & recvBit) { return OWReadBitSetLevel(recvBit, NormalLevel); }
+            CmdResult OWWriteBitPower(uint8_t sendBit) { return OWWriteBitSetLevel(sendBit, StrongLevel); }
+            CmdResult OWReadBitPower(uint8_t & recvBit) { return OWReadBitSetLevel(recvBit, StrongLevel); }
+            CmdResult OWWriteByte(uint8_t sendByte) { return OWWriteByteSetLevel(sendByte, NormalLevel); }
+            CmdResult OWReadByte(uint8_t & recvByte) { return OWReadByteSetLevel(recvByte, NormalLevel); }
+            CmdResult OWWriteBytePower(uint8_t sendByte) { return OWWriteByteSetLevel(sendByte, StrongLevel); }
+            CmdResult OWReadBytePower(uint8_t & recvByte) { return OWReadByteSetLevel(recvByte, StrongLevel); }
 
-            /**********************************************************//**
-            * @brief OWFirst()
-            *
-            * @details Find the 'first' devices on the 1-Wire network
-            *
-            * On Entry:
-            *
-            * On Exit:
-            *
-            **************************************************************/
+            /// Find the 'first' devices on the 1-Wire network.
             CmdResult OWFirst(SearchState & searchState);
 
-            /**********************************************************//**
-            * @brief OWNext()
-            *
-            * @details Find the 'next' devices on the 1-Wire network
-            *
-            * On Entry:
-            *
-            * On Exit:
-            *
-            **************************************************************/
+            /// Find the 'next' devices on the 1-Wire network.
             CmdResult OWNext(SearchState & searchState);
 
-            /**********************************************************//**
-            * @brief OWVerify()
-            *
-            * @details Verify the device with the ROM number in ROM_NO buffer
-            *        is present.
-            *
-            * On Entry:
-            *
-            * On Exit:
-            *
-            **************************************************************/
+            /// Verify that the device with the specified ROM ID is present.
             CmdResult OWVerify(const RomId & romId);
 
-            /**********************************************************//**
-            * @brief OWTargetSetup()
-            *
-            * @details Setup the search to find the device type 'family_code'
-            *        on the next call to OWNext() if it is present.
-            *
-            * On Entry:
-            *     @param[in] family_code - family code of device
-            *
-            * On Exit:
-            *
-            **************************************************************/
+            /// Setup the search to find the device type 'family_code'
+            /// on the next call to OWNext() if it is present.
             void OWTargetSetup(SearchState & searchState);
 
-            /**********************************************************//**
-            * @brief OWFamilySkipSetup()
-            *
-            * @details Setup the search to skip the current device type on the
-            *          next call to OWNext().
-            *
-            * On Entry:
-            *
-            * On Exit:
-            *
-            **************************************************************/
+            /// Setup the search to skip the current device type on the
+            /// next call to OWNext().
             void OWFamilySkipSetup(SearchState & searchState);
 
+            /// Use Read ROM command to read ROM ID from device on bus.
+            /// @note Only use this command with a single drop bus, data
+            ///       collisions will occur if more than 1 device on bus.
+            /// @param[out] romId ROM ID read from device.
+            CmdResult OWReadRom(RomId & romId);
+
+            /// Issue Skip ROM command on bus.
+            /// @note Only use this command with a single drop bus, data
+            ///       collisions will occur if more than 1 device on bus.
+            CmdResult OWSkipRom();
+
+            /// Use the Match ROM command to select the device by its known ID.
+            /// @param[in] romId ROM ID of device to select.
+            CmdResult OWMatchRom(const RomId & romId);
+
+            /// Issue Overdrive Skip ROM command on bus.
+            /// @details This command causes all devices supporting Overdrive
+            ///          mode to switch to Overdrive timing.
+            /// @note Only use this command with a single drop bus, data
+            ///       collisions will occur if more than 1 device on bus.
+            CmdResult OWOverdriveSkipRom();
+
+            
+            CmdResult OWOverdriveMatchRom(const RomId & romId);
+
+            /// Perform a Resume ROM command on bus.
+            /// @details Resumes communication with the last device selected
+            ///          though a Match ROM or Search ROM operation.
+            CmdResult OWResume();
+
             /**********************************************************//**
-            * @brief OWReadROM()
-            *
-            * @details Only use this command with a single drop bus, data
-            * collisions will occur if more than 1 device on bus.
-            * Issues READ_ROM command, slave device will respond with ROM ID.
-            *
-            * On Entry:
-            *
-            * On Exit:
-            *
-            **************************************************************/
-            CmdResult OWReadROM(RomId & romId);
-
-            /**********************************************************//**
-            * @brief OWSkipROM()
-            *
-            * @details Issue SKIP_ROM command on 1-wire bus
-            *
-            * On Entry:
-            *
-            * On Exit:
-            *
-            **************************************************************/
-            CmdResult OWSkipROM(void);
-
-            /**********************************************************//**
-            * @brief OWMatchROM()
-            *
-            * @details Issues MATCH_ROM command on 1-wire bus and then sends
-            * the rom id in _rom_number
-            *
-            * On Entry:
-            *
-            * On Exit:
-            *
-            **************************************************************/
-            CmdResult OWMatchROM(const RomId & romId);
-
-            /**********************************************************//**
-            * @brief OWOverdriveSkipROM()
-            *
-            * @details Issues OVERDRIVE_SKIP rom command.  DS248X OW speed
-            * is set to SPEED_OVERDRIVE
-            *
-            * On Entry:
-            *
-            * On Exit:
-            *
-            **************************************************************/
-            CmdResult OWOverdriveSkipROM(void);
-
-            /**********************************************************//**
-            * @brief OWOverdriveMatchROM()
-            *
-            * @details Issues OVERDRIVE_MATCH rom command.  DS248X OW speed
-            * is set to SPEED_OVERDRIVE
-            *
-            * On Entry:
-            *
-            * On Exit:
-            *
-            **************************************************************/
-            CmdResult OWOverdriveMatchROM(const RomId & romId);
-
-            /**********************************************************//**
-            * @brief OWResume()
-            *
-            * @details Issues RESUME command, very usefull.  Is like skip
-            * on a multidrop bus, however you must first select the device
-            * you want to interface with using a MATCH, or SEARCH.  The
-            * device stays selected until another device is selected,
-            * see slave datasheet for detailed explanation.
-            *
-            * On Entry:
-            *
-            * On Exit:
-            *
-            **************************************************************/
-            CmdResult OWResume(void);
-
-            /**********************************************************//**
-            * @brief OWSearch()
+            * @brief Enumerate all devices on the 1-Wire bus.
             *
             * @details The 'OWSearch' function does a general search.  This
             *        function continues from the previous search state. The
@@ -443,11 +248,6 @@
             *        command 0xF0. Using the find alarm command 0xEC will
             *        limit the search to only 1-Wire devices that are in an
             *        'alarm' state.
-            *
-            * On Entry:
-            *
-            * On Exit:
-            *
             **************************************************************/
             CmdResult OWSearch(SearchState & searchState);
         };