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

Revision:
8:5ea891c7d1a1
Parent:
7:9cd16581b578
Child:
11:3f3bf6bf5e6c
--- a/MaximInterfaceDevices/DS2480B.cpp	Mon Jul 22 11:44:07 2019 -0500
+++ b/MaximInterfaceDevices/DS2480B.cpp	Mon Sep 16 11:13:37 2019 -0500
@@ -1,5 +1,5 @@
 /*******************************************************************************
-* Copyright (C) 2017 Maxim Integrated Products, Inc., All Rights Reserved.
+* Copyright (C) Maxim Integrated Products, Inc., All Rights Reserved.
 *
 * Permission is hereby granted, free of charge, to any person obtaining a
 * copy of this software and associated documentation files (the "Software"),
@@ -33,6 +33,9 @@
 #include <MaximInterfaceCore/Error.hpp>
 #include "DS2480B.hpp"
 
+#define TRY MaximInterfaceCore_TRY
+#define TRY_VALUE MaximInterfaceCore_TRY_VALUE
+
 // Mode Commands
 #define MODE_DATA 0xE1
 #define MODE_COMMAND 0xE3
@@ -173,23 +176,23 @@
 
 using namespace Core;
 
-error_code DS2480B::initialize() {
+Result<void> DS2480B::initialize() {
   level = NormalLevel;
   mode = MODSEL_COMMAND;
   speed = SPEEDSEL_STD;
 
   // Send a break to reset the DS2480B.
   // Switch to lower baud rate to ensure break is longer than 2 ms.
-  error_code result = uart->setBaudRate(4800);
-  if (result) {
+  Result<void> result = uart->setBaudRate(4800);
+  if (!result) {
     return result;
   }
   result = uart->sendBreak();
-  if (result) {
+  if (!result) {
     return result;
   }
   result = uart->setBaudRate(9600);
-  if (result) {
+  if (!result) {
     return result;
   }
 
@@ -198,62 +201,55 @@
 
   // Flush the read buffer.
   result = uart->clearReadBuffer();
-  if (result) {
+  if (!result) {
     return result;
   }
 
   // Send the timing byte.
   result = uart->writeByte(CMD_COMM | FUNCTSEL_RESET | SPEEDSEL_STD);
-  if (result) {
+  if (!result) {
     return result;
   }
 
   // Change the DS2480 baud rate.
   result = uart->writeByte(CMD_CONFIG | PARMSEL_BAUDRATE | PARMSET_115200);
-  if (result) {
+  if (!result) {
     return result;
   }
 
   // Change our baud rate.
   result = uart->setBaudRate(115200);
-  if (result) {
+  if (!result) {
     return result;
   }
 
+  // Verify response.
   uint_least8_t response;
-
-  // Verify response.
-  result = uart->readByte(response);
-  if (result) {
-    return result;
-  }
+  TRY_VALUE(response, uart->readByte());
   if ((response & (PARMSEL_MASK | PARMSET_MASK)) !=
       (PARMSEL_BAUDRATE | PARMSET_115200)) {
-    return make_error_code(HardwareError);
+    return HardwareError;
   }
 
   // Set the SPUD time value.
   result = uart->writeByte(CMD_CONFIG | PARMSEL_5VPULSE | PARMSET_infinite);
-  if (result) {
+  if (!result) {
     return result;
   }
 
   // Verify response.
-  result = uart->readByte(response);
-  if (result) {
-    return result;
-  }
+  TRY_VALUE(response, uart->readByte());
   if ((response & (PARMSEL_MASK | PARMSET_MASK)) !=
       (PARMSEL_5VPULSE | PARMSET_infinite)) {
-    return make_error_code(HardwareError);
+    return HardwareError;
   }
 
   return result;
 }
 
-error_code DS2480B::reset() {
+Result<void> DS2480B::reset() {
   if (level != NormalLevel) {
-    return make_error_code(InvalidLevelError);
+    return InvalidLevelError;
   }
 
   uint_least8_t packet[2];
@@ -269,29 +265,25 @@
   packet[packetLen++] = CMD_COMM | FUNCTSEL_RESET | speed;
 
   // Send the packet.
-  error_code result = uart->writeBlock(make_span(packet, packetLen));
-  if (result) {
-    return result;
-  }
+  TRY(uart->writeBlock(make_span(packet, packetLen)));
 
   // Read back the response.
-  result = uart->readByte(packet[0]);
-  if (result) {
-    return result;
-  }
+  TRY_VALUE(packet[0], uart->readByte());
 
   // Make sure this byte looks like a reset byte.
   if ((packet[0] & RB_RESET_MASK) == RB_1WIRESHORT) {
-    result = make_error_code(ShortDetectedError);
-  } else if ((packet[0] & RB_RESET_MASK) == RB_NOPRESENCE) {
-    result = make_error_code(NoSlaveError);
+    return ShortDetectedError;
   }
-  return result;
+  if ((packet[0] & RB_RESET_MASK) == RB_NOPRESENCE) {
+    return NoSlaveError;
+  }
+
+  return none;
 }
 
-error_code DS2480B::touchBitSetLevel(bool & sendRecvBit, Level afterLevel) {
+Result<bool> DS2480B::touchBitSetLevel(bool sendBit, Level afterLevel) {
   if (level != NormalLevel) {
-    return make_error_code(InvalidLevelError);
+    return InvalidLevelError;
   }
 
   uint_least8_t packet[3];
@@ -304,8 +296,8 @@
   }
 
   // Construct the command.
-  packet[packetLen++] = CMD_COMM | FUNCTSEL_BIT |
-                        (sendRecvBit ? BITPOL_ONE : BITPOL_ZERO) | speed;
+  packet[packetLen++] =
+      CMD_COMM | FUNCTSEL_BIT | (sendBit ? BITPOL_ONE : BITPOL_ZERO) | speed;
   switch (afterLevel) {
   case NormalLevel:
     break;
@@ -317,35 +309,27 @@
     break;
 
   default:
-    return make_error_code(InvalidLevelError);
+    return InvalidLevelError;
   }
 
   // Send the packet.
-  error_code result = uart->writeBlock(make_span(packet, packetLen));
-  if (result) {
-    return result;
-  }
+  TRY(uart->writeBlock(make_span(packet, packetLen)));
 
   // Read back the response.
-  result = uart->readByte(packet[0]);
-  if (result) {
-    return result;
-  }
+  TRY_VALUE(packet[0], uart->readByte());
 
   // Interpret the response.
-  if ((packet[0] & 0xE0) == 0x80) {
-    sendRecvBit = ((packet[0] & RB_BIT_MASK) == RB_BIT_ONE);
-    level = afterLevel;
-  } else {
-    result = make_error_code(HardwareError);
+  if ((packet[0] & 0xE0) != 0x80) {
+    return HardwareError;
   }
-  return result;
+  level = afterLevel;
+  return (packet[0] & RB_BIT_MASK) == RB_BIT_ONE;
 }
 
-error_code DS2480B::writeByteSetLevel(uint_least8_t sendByte,
-                                      Level afterLevel) {
+Result<void> DS2480B::writeByteSetLevel(uint_least8_t sendByte,
+                                        Level afterLevel) {
   if (level != NormalLevel) {
-    return make_error_code(InvalidLevelError);
+    return InvalidLevelError;
   }
 
   switch (afterLevel) {
@@ -356,7 +340,7 @@
     return OneWireMaster::writeByteSetLevel(sendByte, afterLevel);
 
   default:
-    return make_error_code(InvalidLevelError);
+    return InvalidLevelError;
   }
 
   uint_least8_t packet[3];
@@ -377,28 +361,22 @@
   }
 
   // Send the packet.
-  error_code result = uart->writeBlock(make_span(packet, packetLen));
-  if (result) {
-    return result;
-  }
+  TRY(uart->writeBlock(make_span(packet, packetLen)));
 
   // Read back the response.
-  result = uart->readByte(packet[0]);
-  if (result) {
-    return result;
-  }
+  TRY_VALUE(packet[0], uart->readByte());
 
   // Interpret the response.
   if (packet[0] != sendByte) {
-    result = make_error_code(HardwareError);
+    return HardwareError;
   }
-  return result;
+
+  return none;
 }
 
-error_code DS2480B::readByteSetLevel(uint_least8_t & recvByte,
-                                     Level afterLevel) {
+Result<uint_least8_t> DS2480B::readByteSetLevel(Level afterLevel) {
   if (level != NormalLevel) {
-    return make_error_code(InvalidLevelError);
+    return InvalidLevelError;
   }
 
   switch (afterLevel) {
@@ -406,10 +384,10 @@
     break;
 
   case StrongLevel:
-    return OneWireMaster::readByteSetLevel(recvByte, afterLevel);
+    return OneWireMaster::readByteSetLevel(afterLevel);
 
   default:
-    return make_error_code(InvalidLevelError);
+    return InvalidLevelError;
   }
 
   uint_least8_t packet[2];
@@ -425,17 +403,16 @@
   packet[packetLen++] = 0xFF;
 
   // Send the packet.
-  error_code result = uart->writeBlock(make_span(packet, packetLen));
-  if (result) {
-    return result;
+  const Result<void> result = uart->writeBlock(make_span(packet, packetLen));
+  if (!result) {
+    return result.error();
   }
 
   // Read back the response.
-  result = uart->readByte(recvByte);
-  return result;
+  return uart->readByte();
 }
 
-error_code DS2480B::setSpeed(Speed newSpeed) {
+Result<void> DS2480B::setSpeed(Speed newSpeed) {
   uint_least8_t newSpeedByte;
   switch (newSpeed) {
   case OverdriveSpeed:
@@ -447,10 +424,10 @@
     break;
 
   default:
-    return make_error_code(InvalidSpeedError);
+    return InvalidSpeedError;
   }
   if (speed == newSpeedByte) {
-    return error_code();
+    return none;
   }
   speed = newSpeedByte;
 
@@ -470,9 +447,9 @@
   return uart->writeBlock(make_span(packet, packetLen));
 }
 
-error_code DS2480B::setLevel(Level newLevel) {
+Result<void> DS2480B::setLevel(Level newLevel) {
   if (level == newLevel) {
-    return error_code();
+    return none;
   }
 
   uint_least8_t packet[2];
@@ -497,33 +474,27 @@
     break;
 
   default:
-    return make_error_code(InvalidLevelError);
+    return InvalidLevelError;
   }
 
   // Send the packet.
-  error_code result = uart->writeBlock(make_span(packet, packetLen));
-  if (result) {
-    return result;
-  }
+  TRY(uart->writeBlock(make_span(packet, packetLen)));
 
   if (newLevel == NormalLevel) {
     // Read back the response.
-    result = uart->readByte(packet[0]);
-    if (result) {
-      return result;
-    }
+    TRY_VALUE(packet[0], uart->readByte());
 
     // Interpret the response.
     if ((packet[0] & 0xE0) != 0xE0) {
-      return make_error_code(HardwareError);
+      return HardwareError;
     }
   }
 
   level = newLevel;
-  return result;
+  return none;
 }
 
-error_code DS2480B::sendCommand(uint_least8_t command) {
+Result<void> DS2480B::sendCommand(uint_least8_t command) {
   uint_least8_t packet[2];
   int packetLen = 0;
 
@@ -549,10 +520,8 @@
       switch (condition) {
       case HardwareError:
         return "Hardware Error";
-
-      default:
-        return defaultErrorMessage(condition);
       }
+      return defaultErrorMessage(condition);
     }
   } instance;
   return instance;