Device interface library for multiple platforms including Mbed.

Dependents:   DeepCover Embedded Security in IoT MaximInterface MAXREFDES155#

Maxim Interface is a library framework focused on providing flexible and expressive hardware interfaces. Both communication interfaces such as I2C and 1-Wire and device interfaces such as DS18B20 are supported. Modern C++ concepts are used extensively while keeping compatibility with C++98/C++03 and requiring no external dependencies. The embedded-friendly design does not depend on exceptions or RTTI.

The full version of the project is hosted on GitLab: https://gitlab.com/iabenz/MaximInterface

Committer:
IanBenzMaxim
Date:
Fri May 29 16:19:22 2020 -0500
Revision:
12:7eb41621ba22
Parent:
8:5ea891c7d1a1
Updated to version 2.2.

Who changed what in which revision?

UserRevisionLine numberNew 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_DS18B20_hpp
IanBenzMaxim 8:5ea891c7d1a1 34 #define MaximInterfaceDevices_DS18B20_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 DS18B20 Programmable Resolution 1-Wire Digital Thermometer
IanBenzMaxim 7:9cd16581b578 44 /// @details The DS18B20 digital thermometer provides 9-bit to 12-bit
IanBenzMaxim 7:9cd16581b578 45 /// Celsius temperature measurements and has an alarm function with
IanBenzMaxim 7:9cd16581b578 46 /// nonvolatile user-programmable upper and lower trigger points. The
IanBenzMaxim 7:9cd16581b578 47 /// DS18B20 communicates over a 1-Wire bus that by definition requires
IanBenzMaxim 7:9cd16581b578 48 /// only one data line (and ground) for communication with a central
IanBenzMaxim 7:9cd16581b578 49 /// microprocessor. In addition, the DS18B20 can derive power directly
IanBenzMaxim 7:9cd16581b578 50 /// from the data line ("parasite power"), eliminating the need for an
IanBenzMaxim 7:9cd16581b578 51 /// external power supply.
IanBenzMaxim 7:9cd16581b578 52 class DS18B20 {
IanBenzMaxim 7:9cd16581b578 53 public:
IanBenzMaxim 7:9cd16581b578 54 enum ErrorValue { CrcError = 1, DataError };
IanBenzMaxim 7:9cd16581b578 55
IanBenzMaxim 7:9cd16581b578 56 static const uint_least8_t nineBitResolution = 0x1F;
IanBenzMaxim 7:9cd16581b578 57 static const uint_least8_t tenBitResolution = 0x3F;
IanBenzMaxim 7:9cd16581b578 58 static const uint_least8_t elevenBitResolution = 0x5F;
IanBenzMaxim 7:9cd16581b578 59 static const uint_least8_t twelveBitResolution = 0x7F;
IanBenzMaxim 7:9cd16581b578 60
IanBenzMaxim 7:9cd16581b578 61 /// Holds the contents of the device scratchpad.
IanBenzMaxim 8:5ea891c7d1a1 62 typedef Core::array<uint_least8_t, 8> Scratchpad;
IanBenzMaxim 7:9cd16581b578 63
IanBenzMaxim 7:9cd16581b578 64 DS18B20(Core::Sleep & sleep, Core::OneWireMaster & master,
IanBenzMaxim 7:9cd16581b578 65 const Core::SelectRom & selectRom)
IanBenzMaxim 7:9cd16581b578 66 : selectRom(selectRom), master(&master), sleep(&sleep), resolution(0) {}
IanBenzMaxim 7:9cd16581b578 67
IanBenzMaxim 7:9cd16581b578 68 void setSleep(Core::Sleep & sleep) { this->sleep = &sleep; }
IanBenzMaxim 7:9cd16581b578 69
IanBenzMaxim 7:9cd16581b578 70 void setMaster(Core::OneWireMaster & master) { this->master = &master; }
IanBenzMaxim 7:9cd16581b578 71
IanBenzMaxim 7:9cd16581b578 72 void setSelectRom(const Core::SelectRom & selectRom) {
IanBenzMaxim 7:9cd16581b578 73 this->selectRom = selectRom;
IanBenzMaxim 7:9cd16581b578 74 }
IanBenzMaxim 7:9cd16581b578 75
IanBenzMaxim 7:9cd16581b578 76 /// Initializes the device for first time use.
IanBenzMaxim 8:5ea891c7d1a1 77 MaximInterfaceDevices_EXPORT Core::Result<void> initialize() const;
IanBenzMaxim 7:9cd16581b578 78
IanBenzMaxim 7:9cd16581b578 79 /// @brief Write Scratchpad Command
IanBenzMaxim 7:9cd16581b578 80 /// @details If the result of a temperature measurement is higher
IanBenzMaxim 7:9cd16581b578 81 /// than TH or lower than TL, an alarm flag inside the device is
IanBenzMaxim 7:9cd16581b578 82 /// set. This flag is updated with every temperature measurement.
IanBenzMaxim 7:9cd16581b578 83 /// As long as the alarm flag is set, the DS1920 will respond to
IanBenzMaxim 7:9cd16581b578 84 /// the alarm search command.
IanBenzMaxim 7:9cd16581b578 85 /// @param[in] th 8-bit upper temperature threshold, MSB indicates sign.
IanBenzMaxim 7:9cd16581b578 86 /// @param[in] tl 8-bit lower temperature threshold, LSB indicates sign.
IanBenzMaxim 7:9cd16581b578 87 /// @param[in] res Resolution of the DS18B20.
IanBenzMaxim 8:5ea891c7d1a1 88 MaximInterfaceDevices_EXPORT Core::Result<void>
IanBenzMaxim 7:9cd16581b578 89 writeScratchpad(uint_least8_t th, uint_least8_t tl, uint_least8_t res);
IanBenzMaxim 7:9cd16581b578 90
IanBenzMaxim 7:9cd16581b578 91 /// @brief Read Scratchpad Command
IanBenzMaxim 8:5ea891c7d1a1 92 /// @returns scratchpad Contents of scratchpad.
IanBenzMaxim 8:5ea891c7d1a1 93 MaximInterfaceDevices_EXPORT Core::Result<Scratchpad> readScratchpad() const;
IanBenzMaxim 7:9cd16581b578 94
IanBenzMaxim 7:9cd16581b578 95 /// @brief Copy Scratchpad Command
IanBenzMaxim 7:9cd16581b578 96 /// @details This command copies from the scratchpad into the
IanBenzMaxim 7:9cd16581b578 97 /// EEPROM of the DS18B20, storing the temperature trigger bytes
IanBenzMaxim 7:9cd16581b578 98 /// and resolution in nonvolatile memory.
IanBenzMaxim 8:5ea891c7d1a1 99 MaximInterfaceDevices_EXPORT Core::Result<void> copyScratchpad();
IanBenzMaxim 7:9cd16581b578 100
IanBenzMaxim 7:9cd16581b578 101 /// @brief Read Power Supply command
IanBenzMaxim 7:9cd16581b578 102 /// @details This command determines if the DS18B20 is parasite
IanBenzMaxim 7:9cd16581b578 103 /// powered or has a local supply
IanBenzMaxim 8:5ea891c7d1a1 104 /// @returns
IanBenzMaxim 7:9cd16581b578 105 /// True if the device is powered by a local power supply, or false if the
IanBenzMaxim 7:9cd16581b578 106 /// device is parasitically powered.
IanBenzMaxim 8:5ea891c7d1a1 107 MaximInterfaceDevices_EXPORT Core::Result<bool> readPowerSupply() const;
IanBenzMaxim 7:9cd16581b578 108
IanBenzMaxim 7:9cd16581b578 109 /// @brief Convert Temperature Command
IanBenzMaxim 7:9cd16581b578 110 /// @details This command begins a temperature conversion.
IanBenzMaxim 8:5ea891c7d1a1 111 MaximInterfaceDevices_EXPORT Core::Result<void> convertTemperature();
IanBenzMaxim 7:9cd16581b578 112
IanBenzMaxim 7:9cd16581b578 113 /// @brief Recall Command
IanBenzMaxim 7:9cd16581b578 114 /// @details This command recalls the temperature trigger values
IanBenzMaxim 7:9cd16581b578 115 /// and resolution stored in EEPROM to the scratchpad.
IanBenzMaxim 8:5ea891c7d1a1 116 MaximInterfaceDevices_EXPORT Core::Result<void> recallEeprom();
IanBenzMaxim 7:9cd16581b578 117
IanBenzMaxim 7:9cd16581b578 118 MaximInterfaceDevices_EXPORT static const Core::error_category &
IanBenzMaxim 7:9cd16581b578 119 errorCategory();
IanBenzMaxim 7:9cd16581b578 120
IanBenzMaxim 7:9cd16581b578 121 private:
IanBenzMaxim 7:9cd16581b578 122 Core::SelectRom selectRom;
IanBenzMaxim 7:9cd16581b578 123 Core::OneWireMaster * master;
IanBenzMaxim 7:9cd16581b578 124 const Core::Sleep * sleep;
IanBenzMaxim 8:5ea891c7d1a1 125 mutable uint_least8_t resolution;
IanBenzMaxim 7:9cd16581b578 126 };
IanBenzMaxim 7:9cd16581b578 127
IanBenzMaxim 7:9cd16581b578 128 /// @brief Reads the current temperature as an integer value with decimal.
IanBenzMaxim 7:9cd16581b578 129 /// @param ds18b20 Device to read.
IanBenzMaxim 8:5ea891c7d1a1 130 /// @returns Temperature in degrees Celsius multiplied by 16.
IanBenzMaxim 8:5ea891c7d1a1 131 MaximInterfaceDevices_EXPORT Core::Result<int>
IanBenzMaxim 8:5ea891c7d1a1 132 readTemperature(DS18B20 & ds18b20);
IanBenzMaxim 8:5ea891c7d1a1 133
IanBenzMaxim 8:5ea891c7d1a1 134 } // namespace MaximInterfaceDevices
IanBenzMaxim 8:5ea891c7d1a1 135 namespace MaximInterfaceCore {
IanBenzMaxim 8:5ea891c7d1a1 136
IanBenzMaxim 8:5ea891c7d1a1 137 template <>
IanBenzMaxim 8:5ea891c7d1a1 138 struct is_error_code_enum<MaximInterfaceDevices::DS18B20::ErrorValue>
IanBenzMaxim 8:5ea891c7d1a1 139 : true_type {};
IanBenzMaxim 8:5ea891c7d1a1 140
IanBenzMaxim 8:5ea891c7d1a1 141 } // namespace MaximInterfaceCore
IanBenzMaxim 8:5ea891c7d1a1 142 namespace MaximInterfaceDevices {
IanBenzMaxim 7:9cd16581b578 143
IanBenzMaxim 7:9cd16581b578 144 inline Core::error_code make_error_code(DS18B20::ErrorValue e) {
IanBenzMaxim 7:9cd16581b578 145 return Core::error_code(e, DS18B20::errorCategory());
IanBenzMaxim 7:9cd16581b578 146 }
IanBenzMaxim 7:9cd16581b578 147
IanBenzMaxim 7:9cd16581b578 148 } // namespace MaximInterfaceDevices
IanBenzMaxim 7:9cd16581b578 149
IanBenzMaxim 7:9cd16581b578 150 #endif