Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Dependents: DeepCover Embedded Security in IoT MaximInterface MAXREFDES155#
MaximInterfaceDevices/DS1920.hpp@8:5ea891c7d1a1, 2019-09-16 (annotated)
- Committer:
- IanBenzMaxim
- Date:
- Mon Sep 16 11:13:37 2019 -0500
- Revision:
- 8:5ea891c7d1a1
- Parent:
- 7:9cd16581b578
Updated to version 2.0.
Who changed what in which revision?
| User | Revision | Line number | New contents of line | 
|---|---|---|---|
| IanBenzMaxim | 7:9cd16581b578 | 1 | /******************************************************************************* | 
| IanBenzMaxim | 8:5ea891c7d1a1 | 2 | * Copyright (C) Maxim Integrated Products, Inc., All Rights Reserved. | 
| IanBenzMaxim | 7:9cd16581b578 | 3 | * | 
| IanBenzMaxim | 7:9cd16581b578 | 4 | * Permission is hereby granted, free of charge, to any person obtaining a | 
| IanBenzMaxim | 7:9cd16581b578 | 5 | * copy of this software and associated documentation files (the "Software"), | 
| IanBenzMaxim | 7:9cd16581b578 | 6 | * to deal in the Software without restriction, including without limitation | 
| IanBenzMaxim | 7:9cd16581b578 | 7 | * the rights to use, copy, modify, merge, publish, distribute, sublicense, | 
| IanBenzMaxim | 7:9cd16581b578 | 8 | * and/or sell copies of the Software, and to permit persons to whom the | 
| IanBenzMaxim | 7:9cd16581b578 | 9 | * Software is furnished to do so, subject to the following conditions: | 
| IanBenzMaxim | 7:9cd16581b578 | 10 | * | 
| IanBenzMaxim | 7:9cd16581b578 | 11 | * The above copyright notice and this permission notice shall be included | 
| IanBenzMaxim | 7:9cd16581b578 | 12 | * in all copies or substantial portions of the Software. | 
| IanBenzMaxim | 7:9cd16581b578 | 13 | * | 
| IanBenzMaxim | 7:9cd16581b578 | 14 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS | 
| IanBenzMaxim | 7:9cd16581b578 | 15 | * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF | 
| IanBenzMaxim | 7:9cd16581b578 | 16 | * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. | 
| IanBenzMaxim | 7:9cd16581b578 | 17 | * IN NO EVENT SHALL MAXIM INTEGRATED BE LIABLE FOR ANY CLAIM, DAMAGES | 
| IanBenzMaxim | 7:9cd16581b578 | 18 | * OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, | 
| IanBenzMaxim | 7:9cd16581b578 | 19 | * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR | 
| IanBenzMaxim | 7:9cd16581b578 | 20 | * OTHER DEALINGS IN THE SOFTWARE. | 
| IanBenzMaxim | 7:9cd16581b578 | 21 | * | 
| IanBenzMaxim | 7:9cd16581b578 | 22 | * Except as contained in this notice, the name of Maxim Integrated | 
| IanBenzMaxim | 7:9cd16581b578 | 23 | * Products, Inc. shall not be used except as stated in the Maxim Integrated | 
| IanBenzMaxim | 7:9cd16581b578 | 24 | * Products, Inc. Branding Policy. | 
| IanBenzMaxim | 7:9cd16581b578 | 25 | * | 
| IanBenzMaxim | 7:9cd16581b578 | 26 | * The mere transfer of this software does not imply any licenses | 
| IanBenzMaxim | 7:9cd16581b578 | 27 | * of trade secrets, proprietary technology, copyrights, patents, | 
| IanBenzMaxim | 7:9cd16581b578 | 28 | * trademarks, maskwork rights, or any other form of intellectual | 
| IanBenzMaxim | 7:9cd16581b578 | 29 | * property whatsoever. Maxim Integrated Products, Inc. retains all | 
| IanBenzMaxim | 7:9cd16581b578 | 30 | * ownership rights. | 
| IanBenzMaxim | 7:9cd16581b578 | 31 | *******************************************************************************/ | 
| IanBenzMaxim | 7:9cd16581b578 | 32 | |
| IanBenzMaxim | 8:5ea891c7d1a1 | 33 | #ifndef MaximInterfaceDevices_DS1920_hpp | 
| IanBenzMaxim | 8:5ea891c7d1a1 | 34 | #define MaximInterfaceDevices_DS1920_hpp | 
| IanBenzMaxim | 7:9cd16581b578 | 35 | |
| IanBenzMaxim | 8:5ea891c7d1a1 | 36 | #include <MaximInterfaceCore/array.hpp> | 
| IanBenzMaxim | 7:9cd16581b578 | 37 | #include <MaximInterfaceCore/SelectRom.hpp> | 
| IanBenzMaxim | 7:9cd16581b578 | 38 | #include <MaximInterfaceCore/Sleep.hpp> | 
| IanBenzMaxim | 7:9cd16581b578 | 39 | #include "Config.hpp" | 
| IanBenzMaxim | 7:9cd16581b578 | 40 | |
| IanBenzMaxim | 7:9cd16581b578 | 41 | namespace MaximInterfaceDevices { | 
| IanBenzMaxim | 7:9cd16581b578 | 42 | |
| IanBenzMaxim | 7:9cd16581b578 | 43 | /// @brief DS1920 1-Wire Temperature iButton | 
| IanBenzMaxim | 7:9cd16581b578 | 44 | /// @details The iButton® temperature logger (DS1920) provides | 
| IanBenzMaxim | 7:9cd16581b578 | 45 | /// direct-to-digital 9-bit temperature readings over a range of | 
| IanBenzMaxim | 7:9cd16581b578 | 46 | /// -55°C to +100°C in 0.5° increments. The iButton communicates with | 
| IanBenzMaxim | 7:9cd16581b578 | 47 | /// a processor using the 1-Wire® protocol through a hardware port | 
| IanBenzMaxim | 7:9cd16581b578 | 48 | /// interface. The port interface provides both the physical link and | 
| IanBenzMaxim | 7:9cd16581b578 | 49 | /// handles the communication protocols that enable the processor to | 
| IanBenzMaxim | 7:9cd16581b578 | 50 | /// access iButton resources with simple commands. Two bytes of | 
| IanBenzMaxim | 7:9cd16581b578 | 51 | /// EEPROM can be used either to set alarm triggers or for storing | 
| IanBenzMaxim | 7:9cd16581b578 | 52 | /// user data. | 
| IanBenzMaxim | 7:9cd16581b578 | 53 | class DS1920 { | 
| IanBenzMaxim | 7:9cd16581b578 | 54 | public: | 
| IanBenzMaxim | 7:9cd16581b578 | 55 | enum ErrorValue { CrcError = 1, DataError }; | 
| IanBenzMaxim | 7:9cd16581b578 | 56 | |
| IanBenzMaxim | 7:9cd16581b578 | 57 | /// Holds the contents of the device scratchpad. | 
| IanBenzMaxim | 8:5ea891c7d1a1 | 58 | typedef Core::array<uint_least8_t, 8> Scratchpad; | 
| IanBenzMaxim | 7:9cd16581b578 | 59 | |
| IanBenzMaxim | 7:9cd16581b578 | 60 | DS1920(Core::Sleep & sleep, Core::OneWireMaster & master, | 
| IanBenzMaxim | 7:9cd16581b578 | 61 | const Core::SelectRom & selectRom) | 
| IanBenzMaxim | 7:9cd16581b578 | 62 | : selectRom(selectRom), master(&master), sleep(&sleep) {} | 
| IanBenzMaxim | 7:9cd16581b578 | 63 | |
| IanBenzMaxim | 7:9cd16581b578 | 64 | void setSleep(Core::Sleep & sleep) { this->sleep = &sleep; } | 
| IanBenzMaxim | 7:9cd16581b578 | 65 | |
| IanBenzMaxim | 7:9cd16581b578 | 66 | void setMaster(Core::OneWireMaster & master) { this->master = &master; } | 
| IanBenzMaxim | 7:9cd16581b578 | 67 | |
| IanBenzMaxim | 7:9cd16581b578 | 68 | void setSelectRom(const Core::SelectRom & selectRom) { | 
| IanBenzMaxim | 7:9cd16581b578 | 69 | this->selectRom = selectRom; | 
| IanBenzMaxim | 7:9cd16581b578 | 70 | } | 
| IanBenzMaxim | 7:9cd16581b578 | 71 | |
| IanBenzMaxim | 7:9cd16581b578 | 72 | /// @brief Write Scratchpad Command | 
| IanBenzMaxim | 7:9cd16581b578 | 73 | /// @details If the result of a temperature measurement is higher | 
| IanBenzMaxim | 7:9cd16581b578 | 74 | /// than TH or lower than TL, an alarm flag inside the device is | 
| IanBenzMaxim | 7:9cd16581b578 | 75 | /// set. This flag is updated with every temperature measurement. | 
| IanBenzMaxim | 7:9cd16581b578 | 76 | /// As long as the alarm flag is set, the DS1920 will respond to | 
| IanBenzMaxim | 7:9cd16581b578 | 77 | /// the alarm search command. | 
| IanBenzMaxim | 7:9cd16581b578 | 78 | /// @param[in] th 8-bit upper temperature threshold, MSB indicates sign. | 
| IanBenzMaxim | 7:9cd16581b578 | 79 | /// @param[in] tl 8-bit lower temperature threshold, MSB indicates sign. | 
| IanBenzMaxim | 8:5ea891c7d1a1 | 80 | MaximInterfaceDevices_EXPORT Core::Result<void> | 
| IanBenzMaxim | 7:9cd16581b578 | 81 | writeScratchpad(uint_least8_t th, uint_least8_t tl); | 
| IanBenzMaxim | 7:9cd16581b578 | 82 | |
| IanBenzMaxim | 7:9cd16581b578 | 83 | /// @brief Read Scratchpad Command | 
| IanBenzMaxim | 8:5ea891c7d1a1 | 84 | /// @returns Contents of scratchpad. | 
| IanBenzMaxim | 8:5ea891c7d1a1 | 85 | MaximInterfaceDevices_EXPORT Core::Result<Scratchpad> readScratchpad() const; | 
| IanBenzMaxim | 7:9cd16581b578 | 86 | |
| IanBenzMaxim | 7:9cd16581b578 | 87 | /// @brief Copy Scratchpad Command | 
| IanBenzMaxim | 7:9cd16581b578 | 88 | /// @details This command copies from the scratchpad into the | 
| IanBenzMaxim | 7:9cd16581b578 | 89 | /// EEPROM of the DS1920, storing the temperature trigger bytes | 
| IanBenzMaxim | 7:9cd16581b578 | 90 | /// in nonvolatile memory. | 
| IanBenzMaxim | 8:5ea891c7d1a1 | 91 | MaximInterfaceDevices_EXPORT Core::Result<void> copyScratchpad(); | 
| IanBenzMaxim | 7:9cd16581b578 | 92 | |
| IanBenzMaxim | 7:9cd16581b578 | 93 | /// @brief Convert Temperature Command | 
| IanBenzMaxim | 7:9cd16581b578 | 94 | /// @details This command begins a temperature conversion. | 
| IanBenzMaxim | 8:5ea891c7d1a1 | 95 | MaximInterfaceDevices_EXPORT Core::Result<void> convertTemperature(); | 
| IanBenzMaxim | 7:9cd16581b578 | 96 | |
| IanBenzMaxim | 7:9cd16581b578 | 97 | /// @brief Recall Command | 
| IanBenzMaxim | 7:9cd16581b578 | 98 | /// @details This command recalls the temperature trigger values | 
| IanBenzMaxim | 7:9cd16581b578 | 99 | /// stored in EEPROM to the scratchpad. | 
| IanBenzMaxim | 8:5ea891c7d1a1 | 100 | MaximInterfaceDevices_EXPORT Core::Result<void> recallEeprom(); | 
| IanBenzMaxim | 7:9cd16581b578 | 101 | |
| IanBenzMaxim | 7:9cd16581b578 | 102 | MaximInterfaceDevices_EXPORT static const Core::error_category & | 
| IanBenzMaxim | 7:9cd16581b578 | 103 | errorCategory(); | 
| IanBenzMaxim | 7:9cd16581b578 | 104 | |
| IanBenzMaxim | 7:9cd16581b578 | 105 | private: | 
| IanBenzMaxim | 7:9cd16581b578 | 106 | Core::SelectRom selectRom; | 
| IanBenzMaxim | 7:9cd16581b578 | 107 | Core::OneWireMaster * master; | 
| IanBenzMaxim | 7:9cd16581b578 | 108 | const Core::Sleep * sleep; | 
| IanBenzMaxim | 7:9cd16581b578 | 109 | }; | 
| IanBenzMaxim | 7:9cd16581b578 | 110 | |
| IanBenzMaxim | 7:9cd16581b578 | 111 | /// @brief Reads the current temperature as an integer value. | 
| IanBenzMaxim | 7:9cd16581b578 | 112 | /// @param ds1920 Device to read. | 
| IanBenzMaxim | 8:5ea891c7d1a1 | 113 | /// @returns Temperature in degrees Celsius multiplied by 2. | 
| IanBenzMaxim | 8:5ea891c7d1a1 | 114 | MaximInterfaceDevices_EXPORT Core::Result<int> readTemperature(DS1920 & ds1920); | 
| IanBenzMaxim | 8:5ea891c7d1a1 | 115 | |
| IanBenzMaxim | 8:5ea891c7d1a1 | 116 | } // namespace MaximInterfaceDevices | 
| IanBenzMaxim | 8:5ea891c7d1a1 | 117 | namespace MaximInterfaceCore { | 
| IanBenzMaxim | 8:5ea891c7d1a1 | 118 | |
| IanBenzMaxim | 8:5ea891c7d1a1 | 119 | template <> | 
| IanBenzMaxim | 8:5ea891c7d1a1 | 120 | struct is_error_code_enum<MaximInterfaceDevices::DS1920::ErrorValue> | 
| IanBenzMaxim | 8:5ea891c7d1a1 | 121 | : true_type {}; | 
| IanBenzMaxim | 8:5ea891c7d1a1 | 122 | |
| IanBenzMaxim | 8:5ea891c7d1a1 | 123 | } // namespace MaximInterfaceCore | 
| IanBenzMaxim | 8:5ea891c7d1a1 | 124 | namespace MaximInterfaceDevices { | 
| IanBenzMaxim | 7:9cd16581b578 | 125 | |
| IanBenzMaxim | 7:9cd16581b578 | 126 | inline Core::error_code make_error_code(DS1920::ErrorValue e) { | 
| IanBenzMaxim | 7:9cd16581b578 | 127 | return Core::error_code(e, DS1920::errorCategory()); | 
| IanBenzMaxim | 7:9cd16581b578 | 128 | } | 
| IanBenzMaxim | 7:9cd16581b578 | 129 | |
| IanBenzMaxim | 7:9cd16581b578 | 130 | } // namespace MaximInterfaceDevices | 
| IanBenzMaxim | 7:9cd16581b578 | 131 | |
| IanBenzMaxim | 7:9cd16581b578 | 132 | #endif |