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:
11:3f3bf6bf5e6c
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 7:9cd16581b578 33 #include <MaximInterfaceCore/Error.hpp>
IanBenzMaxim 7:9cd16581b578 34 #include <MaximInterfaceCore/OneWireMaster.hpp>
IanBenzMaxim 7:9cd16581b578 35 #include "DS2413.hpp"
IanBenzMaxim 7:9cd16581b578 36
IanBenzMaxim 8:5ea891c7d1a1 37 #define TRY MaximInterfaceCore_TRY
IanBenzMaxim 8:5ea891c7d1a1 38 #define TRY_VALUE MaximInterfaceCore_TRY_VALUE
IanBenzMaxim 8:5ea891c7d1a1 39
IanBenzMaxim 7:9cd16581b578 40 namespace MaximInterfaceDevices {
IanBenzMaxim 7:9cd16581b578 41
IanBenzMaxim 7:9cd16581b578 42 using namespace Core;
IanBenzMaxim 7:9cd16581b578 43
IanBenzMaxim 8:5ea891c7d1a1 44 Result<DS2413::Status> DS2413::readStatus() const {
IanBenzMaxim 8:5ea891c7d1a1 45 return Result<DS2413::Status>(pioAccessRead());
IanBenzMaxim 7:9cd16581b578 46 }
IanBenzMaxim 7:9cd16581b578 47
IanBenzMaxim 8:5ea891c7d1a1 48 Result<void> DS2413::writeOutputState(bool pioAState, bool pioBState) {
IanBenzMaxim 7:9cd16581b578 49 uint_least8_t val = 0xFC;
IanBenzMaxim 7:9cd16581b578 50 if (pioAState) {
IanBenzMaxim 7:9cd16581b578 51 val |= 0x1;
IanBenzMaxim 7:9cd16581b578 52 }
IanBenzMaxim 7:9cd16581b578 53 if (pioBState) {
IanBenzMaxim 7:9cd16581b578 54 val |= 0x2;
IanBenzMaxim 7:9cd16581b578 55 }
IanBenzMaxim 7:9cd16581b578 56 return pioAccessWrite(val);
IanBenzMaxim 7:9cd16581b578 57 }
IanBenzMaxim 7:9cd16581b578 58
IanBenzMaxim 8:5ea891c7d1a1 59 Result<uint_least8_t> DS2413::pioAccessRead() const {
IanBenzMaxim 8:5ea891c7d1a1 60 TRY(selectRom(*master));
IanBenzMaxim 8:5ea891c7d1a1 61 TRY(master->writeByte(0xF5));
IanBenzMaxim 8:5ea891c7d1a1 62 uint_least8_t result;
IanBenzMaxim 8:5ea891c7d1a1 63 TRY_VALUE(result, master->readByte());
IanBenzMaxim 8:5ea891c7d1a1 64 return (result == ((result ^ 0xF0) >> 4)) ? makeResult(result)
IanBenzMaxim 8:5ea891c7d1a1 65 : CommunicationError;
IanBenzMaxim 7:9cd16581b578 66 }
IanBenzMaxim 7:9cd16581b578 67
IanBenzMaxim 8:5ea891c7d1a1 68 Result<void> DS2413::pioAccessWrite(uint_least8_t val) {
IanBenzMaxim 8:5ea891c7d1a1 69 TRY(selectRom(*master));
IanBenzMaxim 8:5ea891c7d1a1 70 const uint_least8_t block[] = {0x5A, val,
IanBenzMaxim 8:5ea891c7d1a1 71 static_cast<uint_least8_t>(val ^ 0xFF)};
IanBenzMaxim 8:5ea891c7d1a1 72 TRY(master->writeBlock(block));
IanBenzMaxim 8:5ea891c7d1a1 73 TRY_VALUE(val, master->readByte());
IanBenzMaxim 8:5ea891c7d1a1 74 return (val == 0xAA) ? makeResult(none) : CommunicationError;
IanBenzMaxim 7:9cd16581b578 75 }
IanBenzMaxim 7:9cd16581b578 76
IanBenzMaxim 7:9cd16581b578 77 const error_category & DS2413::errorCategory() {
IanBenzMaxim 7:9cd16581b578 78 static class : public error_category {
IanBenzMaxim 7:9cd16581b578 79 public:
IanBenzMaxim 11:3f3bf6bf5e6c 80 virtual const char * name() const { return "MaximInterfaceDevices.DS2413"; }
IanBenzMaxim 7:9cd16581b578 81
IanBenzMaxim 7:9cd16581b578 82 virtual std::string message(int condition) const {
IanBenzMaxim 7:9cd16581b578 83 switch (condition) {
IanBenzMaxim 7:9cd16581b578 84 case CommunicationError:
IanBenzMaxim 7:9cd16581b578 85 return "Communication Error";
IanBenzMaxim 7:9cd16581b578 86 }
IanBenzMaxim 7:9cd16581b578 87 return defaultErrorMessage(condition);
IanBenzMaxim 7:9cd16581b578 88 }
IanBenzMaxim 7:9cd16581b578 89 } instance;
IanBenzMaxim 7:9cd16581b578 90 return instance;
IanBenzMaxim 7:9cd16581b578 91 }
IanBenzMaxim 7:9cd16581b578 92
IanBenzMaxim 8:5ea891c7d1a1 93 Result<void> writePioAOutputState(DS2413 & ds2413, bool pioAState) {
IanBenzMaxim 7:9cd16581b578 94 DS2413::Status status;
IanBenzMaxim 8:5ea891c7d1a1 95 TRY_VALUE(status, ds2413.readStatus());
IanBenzMaxim 8:5ea891c7d1a1 96 if (pioAState != status[DS2413::PioAOutputState]) {
IanBenzMaxim 8:5ea891c7d1a1 97 TRY(ds2413.writeOutputState(pioAState, status[DS2413::PioBOutputState]));
IanBenzMaxim 7:9cd16581b578 98 }
IanBenzMaxim 8:5ea891c7d1a1 99 return none;
IanBenzMaxim 7:9cd16581b578 100 }
IanBenzMaxim 7:9cd16581b578 101
IanBenzMaxim 8:5ea891c7d1a1 102 Result<void> writePioBOutputState(DS2413 & ds2413, bool pioBState) {
IanBenzMaxim 7:9cd16581b578 103 DS2413::Status status;
IanBenzMaxim 8:5ea891c7d1a1 104 TRY_VALUE(status, ds2413.readStatus());
IanBenzMaxim 8:5ea891c7d1a1 105 if (pioBState != status[DS2413::PioBOutputState]) {
IanBenzMaxim 8:5ea891c7d1a1 106 TRY(ds2413.writeOutputState(status[DS2413::PioAOutputState], pioBState));
IanBenzMaxim 7:9cd16581b578 107 }
IanBenzMaxim 8:5ea891c7d1a1 108 return none;
IanBenzMaxim 7:9cd16581b578 109 }
IanBenzMaxim 7:9cd16581b578 110
IanBenzMaxim 7:9cd16581b578 111 } // namespace MaximInterfaceDevices