Aleksandrs Gumenuks / MaximInterface_Extended

Dependents:   mbed_DS28EC20_GPIO

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers DS28EC20.hpp Source File

DS28EC20.hpp

00001 /*******************************************************************************
00002 * Copyright (C) 2017 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 MaximInterface_DS28EC20
00034 #define MaximInterface_DS28EC20
00035 
00036 #include "MaximInterface/Links/SelectRom.hpp"
00037 #include "MaximInterface/Links/Sleep.hpp"
00038 #include "MaximInterface/Utilities/Export.h"
00039 
00040 namespace MaximInterface {
00041 
00042 ///
00043 /// DS28EC20 20480-bit 1-Wire EEPROM
00044 ///
00045 /// @details
00046 /// The  DS28EC20  is  a  20480-bit,  1-Wire (R)   EEPROM
00047 /// organized as 80 memory pages of 256 bits each. An
00048 /// additional  page  is  set  aside  for  control  functions.
00049 /// Data is written to a 32-byte scratchpad, verified, and
00050 /// then  copied  to  the  EEPROM  memory.  As  a  special
00051 /// feature,  blocks of eight memory pages can  be  write
00052 /// protected  or  put  in  EPROM-Emulation  mode,  where
00053 /// bits can only be changed from a 1 to a 0 state. The
00054 /// DS28EC20 communicates over the single-conductor
00055 /// 1-Wire bus. The communication follows the standard
00056 /// 1-Wire protocol. Each device has its own unalterable
00057 /// and  unique  64-bit  ROM  registration  number.  The
00058 /// registration number is used to address the device in
00059 /// a multidrop 1-Wire net environment.
00060 ///
00061 
00062 
00063 class DS28EC20
00064 {
00065     public:
00066         typedef array<uint_least8_t, 32u> Scratchpad;
00067         typedef uint_least16_t Address;
00068 
00069         enum ErrorValue
00070         {
00071             CrcError = 1,
00072             OperationFailure
00073         };
00074 
00075     public:
00076         static const uint_least8_t familyCode = 0x43u;
00077 
00078         static const size_t pageSizeBytes = 32u;
00079 
00080         /// Number of memory pages on the device
00081         /// 2560 bytes / 32 bytes/page = 80 pages
00082         static const size_t memoryPages = 80u;
00083 
00084         DS28EC20(const Sleep & sleep, OneWireMaster & master, const SelectRom & selectRom, RomId::const_span romId)
00085                 : selectRom(selectRom), romId(&romId), master(&master), sleep(&sleep)
00086         {
00087         }
00088 
00089         void setSleep(const Sleep & sleep)
00090         {
00091             this->sleep = &sleep;
00092         }
00093         void setMaster(OneWireMaster & master)
00094         {
00095             this->master = &master;
00096         }
00097         void setSelectRom(const SelectRom & selectRom)
00098         {
00099             this->selectRom = selectRom;
00100         }
00101         void setRomId(RomId::const_span & romId)
00102         {
00103             this->romId = &romId;
00104         }
00105 
00106         /// Reads block of data from EEPROM memory.
00107         /// @param[in] beginAddress EEPROM memory address to start reading from.
00108         /// @param[out] data EEPROM data read from the device.
00109         /// @param[in] dataLen Length of data parameter and number of bytes to read.
00110         MaximInterface_EXPORT
00111         error_code readMemory(Address beginAddress, uint_least8_t * data, size_t dataLen) const;
00112 
00113         /// Reads block of data from EEPROM memory and checks the CRC at the end.
00114         /// @param[in] beginAddress EEPROM memory address to start reading from.
00115         /// @param[out] data EEPROM data read from the device.
00116         /// @param[in] dataLen Length of data parameter and number of bytes to read.
00117         MaximInterface_EXPORT
00118         error_code readMemoryExt(Address beginAddress, uint_least8_t * data, size_t dataLen) const;
00119 
00120         /// Writes 8 bytes to the scratchpad.
00121         /// @param[in] targetAddress EEPROM memory address that this data.
00122         /// will be copied to.  Must be on row boundary.
00123         /// @param[in] data Data to write to scratchpad.
00124         MaximInterface_EXPORT
00125         error_code writeScratchpad(Address targetAddress, const Scratchpad & data);
00126 
00127         /// Reads contents of scratchpad.
00128         /// @param[out] data Data read from scratchpad.
00129         /// @param[out] esByte E/S byte read before scratchpad data.
00130         MaximInterface_EXPORT
00131         error_code readScratchpad(Scratchpad & data, uint_least8_t & esByte);
00132 
00133         /// Copies contents of scratchpad to EEPROM.
00134         /// @param[in] targetAddress EEPROM memory address that scratchpad
00135         /// will be copied to. Must be on row boundary.
00136         /// @param[in] esByte E/S byte from preceding Read Scratchpad command.
00137         MaximInterface_EXPORT
00138         error_code copyScratchpad(Address targetAddress, uint_least8_t esByte);
00139 
00140         MaximInterface_EXPORT
00141         static const error_category & errorCategory();
00142 
00143     private:
00144         SelectRom selectRom;
00145         RomId::const_span * romId;
00146         OneWireMaster * master;
00147         const Sleep * sleep;
00148 };
00149 
00150 /// Writes data to EEPROM using Write Scratchpad, Read Scratchpad,
00151 /// and Copy Scratchpad commands.
00152 /// @param[in] targetAddress EEPROM memory address to start writing at.
00153 /// @param[in] data Data to write to EEPROM.
00154 MaximInterface_EXPORT error_code writeMemory(DS28EC20 & device, DS28EC20::Address targetAddress, const DS28EC20::Scratchpad & data);
00155 
00156 /// Writes data to EEPROM using Write Scratchpad, Read Scratchpad,
00157 /// and Copy Scratchpad commands.
00158 /// @param[in] targetAddress EEPROM memory address to start writing at.
00159 /// @param[in] data Data to write to EEPROM.
00160 /// @param[in] data Length of data to be written.
00161 MaximInterface_EXPORT error_code writeMemory(DS28EC20 & device, DS28EC20::Address targetAddress, const uint_least8_t * dataIn, size_t dataLen);
00162 
00163 inline error_code make_error_code(DS28EC20::ErrorValue e)
00164 {
00165     return error_code(e, DS28EC20::errorCategory());
00166 }
00167 
00168 } // namespace MaximInterface
00169 
00170 #endif