Aleksandrs Gumenuks / MaximInterface_Extended

Dependents:   mbed_DS28EC20_GPIO

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers DS1920.hpp Source File

DS1920.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_DS1920
00034 #define MaximInterface_DS1920
00035 
00036 #include <MaximInterface/Links/SelectRom.hpp>
00037 #include <MaximInterface/Links/Sleep.hpp>
00038 #include <MaximInterface/Utilities/array_span.hpp>
00039 #include <MaximInterface/Utilities/Export.h>
00040 
00041 namespace MaximInterface {
00042 
00043 /// @brief DS1920 1-Wire Temperature iButton
00044 /// @details The iButton® temperature logger (DS1920) provides
00045 /// direct-to-digital 9-bit temperature readings over a range of
00046 /// -55°C to +100°C in 0.5° increments. The iButton communicates with
00047 /// a processor using the 1-Wire® protocol through a hardware port
00048 /// interface. The port interface provides both the physical link and
00049 /// handles the communication protocols that enable the processor to
00050 /// access iButton resources with simple commands. Two bytes of
00051 /// EEPROM can be used either to set alarm triggers or for storing
00052 /// user data.
00053 class DS1920 {
00054 public:
00055   enum ErrorValue { CrcError = 1, DataError };
00056 
00057   /// Holds the contents of the device scratchpad.
00058   typedef array_span<uint_least8_t, 8> Scratchpad;
00059 
00060   DS1920(Sleep & sleep, OneWireMaster & master, const SelectRom & selectRom)
00061       : selectRom(selectRom), master(&master), sleep(&sleep) {}
00062 
00063   void setSleep(Sleep & sleep) { this->sleep = &sleep; }
00064   
00065   void setMaster(OneWireMaster & master) { this->master = &master; }
00066   
00067   void setSelectRom(const SelectRom & selectRom) {
00068     this->selectRom = selectRom;
00069   }
00070 
00071   /// @brief Write Scratchpad Command
00072   /// @details If the result of a temperature measurement is higher
00073   /// than TH or lower than TL, an alarm flag inside the device is
00074   /// set. This flag is updated with every temperature measurement.
00075   /// As long as the alarm flag is set, the DS1920 will respond to
00076   /// the alarm search command.
00077   /// @param[in] th 8-bit upper temperature threshold, MSB indicates sign.
00078   /// @param[in] tl 8-bit lower temperature threshold, MSB indicates sign.
00079   MaximInterface_EXPORT error_code writeScratchpad(uint_least8_t th,
00080                                                    uint_least8_t tl);
00081 
00082   /// @brief Read Scratchpad Command
00083   /// @param[out] scratchpad Contents of scratchpad.
00084   MaximInterface_EXPORT error_code readScratchpad(Scratchpad::span scratchpad);
00085 
00086   /// @brief Copy Scratchpad Command
00087   /// @details This command copies from the scratchpad into the
00088   /// EEPROM of the DS1920, storing the temperature trigger bytes
00089   /// in nonvolatile memory.
00090   MaximInterface_EXPORT error_code copyScratchpad();
00091 
00092   /// @brief Convert Temperature Command
00093   /// @details This command begins a temperature conversion.
00094   MaximInterface_EXPORT error_code convertTemperature();
00095 
00096   /// @brief Recall Command
00097   /// @details This command recalls the temperature trigger values
00098   /// stored in EEPROM to the scratchpad.
00099   MaximInterface_EXPORT error_code recallEeprom();
00100 
00101   MaximInterface_EXPORT static const error_category & errorCategory();
00102 
00103 private:
00104   SelectRom selectRom;
00105   OneWireMaster * master;
00106   const Sleep * sleep;
00107 };
00108 
00109 /// @brief Reads the current temperature as an integer value.
00110 /// @param ds1920 Device to read.
00111 /// @param[out] temperature Temperature in degrees Celsius multiplied by 2.
00112 MaximInterface_EXPORT error_code readTemperature(DS1920 & ds1920,
00113                                                  int & temperature);
00114 
00115 inline error_code make_error_code(DS1920::ErrorValue e) {
00116   return error_code(e, DS1920::errorCategory());
00117 }
00118 
00119 } // namespace MaximInterface
00120 
00121 #endif