Device interface library for multiple platforms including Mbed.

Dependents:   DeepCover Embedded Security in IoT MaximInterface MAXREFDES155#

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers DS2431.hpp Source File

DS2431.hpp

00001 /*******************************************************************************
00002 * Copyright (C) Maxim Integrated Products, Inc., All Rights Reserved.
00003 *
00004 * Permission is hereby granted, free of charge, to any person obtaining a
00005 * copy of this software and associated documentation files (the "Software"),
00006 * to deal in the Software without restriction, including without limitation
00007 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
00008 * and/or sell copies of the Software, and to permit persons to whom the
00009 * Software is furnished to do so, subject to the following conditions:
00010 *
00011 * The above copyright notice and this permission notice shall be included
00012 * in all copies or substantial portions of the Software.
00013 *
00014 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
00015 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
00016 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
00017 * IN NO EVENT SHALL MAXIM INTEGRATED BE LIABLE FOR ANY CLAIM, DAMAGES
00018 * OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
00019 * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
00020 * OTHER DEALINGS IN THE SOFTWARE.
00021 *
00022 * Except as contained in this notice, the name of Maxim Integrated
00023 * Products, Inc. shall not be used except as stated in the Maxim Integrated
00024 * Products, Inc. Branding Policy.
00025 *
00026 * The mere transfer of this software does not imply any licenses
00027 * of trade secrets, proprietary technology, copyrights, patents,
00028 * trademarks, maskwork rights, or any other form of intellectual
00029 * property whatsoever. Maxim Integrated Products, Inc. retains all
00030 * ownership rights.
00031 *******************************************************************************/
00032 
00033 #ifndef MaximInterfaceDevices_DS2431_hpp
00034 #define MaximInterfaceDevices_DS2431_hpp
00035 
00036 #include <utility>
00037 #include <MaximInterfaceCore/array_span.hpp>
00038 #include <MaximInterfaceCore/SelectRom.hpp>
00039 #include <MaximInterfaceCore/Sleep.hpp>
00040 #include "Config.hpp"
00041 
00042 namespace MaximInterfaceDevices {
00043 
00044 /// @brief DS2431 1024-bit 1-Wire EEPROM
00045 /// @details The DS2431 is a 1024-bit, 1-Wire® EEPROM chip organized
00046 /// as four memory pages of 256 bits each. Data is written to an 8-byte
00047 /// scratchpad, verified, and then copied to the EEPROM memory. As a
00048 /// special feature, the four memory pages can individually be write
00049 /// protected or put in EPROM-emulation mode, where bits can only be
00050 /// changed from a 1 to a 0 state. The DS2431 communicates over the
00051 /// single-conductor 1-Wire bus. The communication follows the standard
00052 /// 1-Wire protocol. Each device has its own unalterable and unique
00053 /// 64-bit ROM registration number that is factory lasered into the chip.
00054 /// The registration number is used to address the device in a multidrop,
00055 /// 1-Wire net environment.
00056 class DS2431 {
00057 public:
00058   enum ErrorValue { CrcError = 1, OperationFailure };
00059 
00060   typedef Core::array_span<uint_least8_t, 8> Scratchpad;
00061 
00062   DS2431(Core::Sleep & sleep, Core::OneWireMaster & master,
00063          const Core::SelectRom & selectRom)
00064       : selectRom(selectRom), master(&master), sleep(&sleep) {}
00065 
00066   void setSleep(Core::Sleep & sleep) { this->sleep = &sleep; }
00067 
00068   void setMaster(Core::OneWireMaster & master) { this->master = &master; }
00069 
00070   void setSelectRom(const Core::SelectRom & selectRom) {
00071     this->selectRom = selectRom;
00072   }
00073 
00074   /// @brief Reads block of data from EEPROM memory.
00075   /// @param[in] beginAddress EEPROM memory address to start reading from.
00076   /// @param[out] data EEPROM data read from the device.
00077   MaximInterfaceDevices_EXPORT Core::Result<void>
00078   readMemory(uint_least8_t beginAddress, Core::span<uint_least8_t> data) const;
00079 
00080   /// @brief Writes 8 bytes to the scratchpad.
00081   /// @param[in] targetAddress
00082   /// EEPROM memory address that this data will be copied to.
00083   /// Must be on row boundary.
00084   /// @param[in] data Data to write to scratchpad.
00085   MaximInterfaceDevices_EXPORT Core::Result<void>
00086   writeScratchpad(uint_least8_t targetAddress, Scratchpad::const_span data);
00087 
00088   /// @brief Reads contents of scratchpad.
00089   /// @returns E/S byte and scratchpad data.
00090   MaximInterfaceDevices_EXPORT
00091       Core::Result<std::pair<uint_least8_t, Scratchpad::array> >
00092       readScratchpad() const;
00093 
00094   /// @brief Copies contents of scratchpad to EEPROM.
00095   /// @param[in] targetAddress EEPROM memory address that scratchpad
00096   /// will be copied to. Must be on row boundary.
00097   /// @param[in] esByte E/S byte from preceding Read Scratchpad command.
00098   MaximInterfaceDevices_EXPORT Core::Result<void>
00099   copyScratchpad(uint_least8_t targetAddress, uint_least8_t esByte);
00100 
00101   MaximInterfaceDevices_EXPORT static const Core::error_category &
00102   errorCategory();
00103 
00104 private:
00105   Core::SelectRom selectRom;
00106   Core::OneWireMaster * master;
00107   const Core::Sleep * sleep;
00108 };
00109 
00110 /// @brief
00111 /// Writes data to EEPROM using Write Scratchpad, Read Scratchpad,
00112 /// and Copy Scratchpad commands.
00113 /// @param device Device to write.
00114 /// @param[in] targetAddress EEPROM memory address to start writing at.
00115 /// @param[in] data Data to write to EEPROM.
00116 MaximInterfaceDevices_EXPORT Core::Result<void>
00117 writeMemory(DS2431 & device, uint_least8_t targetAddress,
00118             DS2431::Scratchpad::const_span data);
00119 
00120 } // namespace MaximInterfaceDevices
00121 namespace MaximInterfaceCore {
00122 
00123 template <>
00124 struct is_error_code_enum<MaximInterfaceDevices::DS2431::ErrorValue>
00125     : true_type {};
00126 
00127 } // namespace MaximInterfaceCore
00128 namespace MaximInterfaceDevices {
00129 
00130 inline Core::error_code make_error_code(DS2431::ErrorValue e) {
00131   return Core::error_code(e, DS2431::errorCategory());
00132 }
00133 
00134 } // namespace MaximInterfaceDevices
00135 
00136 #endif