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
--- a/MaximInterfaceCore/RomCommands.cpp	Mon Jul 22 11:44:07 2019 -0500
+++ b/MaximInterfaceCore/RomCommands.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"),
@@ -34,16 +34,6 @@
 
 namespace MaximInterfaceCore {
 
-enum RomCmd {
-  ReadRomCmd = 0x33,
-  MatchRomCmd = 0x55,
-  SearchRomCmd = 0xF0,
-  SkipRomCmd = 0xCC,
-  ResumeRomCmd = 0xA5,
-  OverdriveSkipRomCmd = 0x3C,
-  OverdriveMatchRomCmd = 0x69
-};
-
 void skipCurrentFamily(SearchRomState & searchState) {
   // Set the last discrepancy to last family discrepancy.
   searchState.lastDiscrepancy = searchState.lastFamilyDiscrepancy;
@@ -55,110 +45,112 @@
   }
 }
 
-error_code verifyRom(OneWireMaster & master, RomId::const_span romId) {
+Result<void> verifyRom(OneWireMaster & master, RomId::const_span romId) {
   SearchRomState searchState(romId);
-  error_code result = searchRom(master, searchState);
-  if (result) {
+  Result<void> result = searchRom(master, searchState);
+  if (!result) {
     return result;
   }
   // Check if same device found.
   if (!equal(romId, make_span(searchState.romId))) {
-    result = make_error_code(OneWireMaster::NoSlaveError);
+    result = OneWireMaster::NoSlaveError;
   }
   return result;
 }
 
-error_code readRom(OneWireMaster & master, RomId::span romId) {
-  error_code result = master.reset();
-  if (result) {
+Result<RomId::array> readRom(OneWireMaster & master) {
+  Result<void> result = master.reset();
+  if (!result) {
+    return result.error();
+  }
+  result = master.writeByte(0x33);
+  if (!result) {
+    return result.error();
+  }
+  RomId::array romId;
+  result = master.readBlock(romId);
+  if (!result) {
+    return result.error();
+  }
+  if (!valid(romId)) {
+    return OneWireMaster::NoSlaveError;
+  }
+  return romId;
+}
+
+Result<void> skipRom(OneWireMaster & master) {
+  Result<void> result = master.reset();
+  if (!result) {
     return result;
   }
-  result = master.writeByte(ReadRomCmd);
-  if (result) {
-    return result;
-  }
-  result = master.readBlock(romId);
-  if (result) {
-    return result;
-  }
-  if (!valid(romId)) {
-    result = make_error_code(OneWireMaster::NoSlaveError);
-  }
+  result = master.writeByte(0xCC);
   return result;
 }
 
-error_code skipRom(OneWireMaster & master) {
-  error_code result = master.reset();
-  if (result) {
+Result<void> matchRom(OneWireMaster & master, RomId::const_span romId) {
+  Result<void> result = master.reset();
+  if (!result) {
     return result;
   }
-  result = master.writeByte(SkipRomCmd);
-  return result;
-}
-
-error_code matchRom(OneWireMaster & master, RomId::const_span romId) {
-  error_code result = master.reset();
-  if (result) {
-    return result;
-  }
-  result = master.writeByte(MatchRomCmd);
-  if (result) {
+  result = master.writeByte(0x55);
+  if (!result) {
     return result;
   }
   result = master.writeBlock(romId);
   return result;
 }
 
-error_code overdriveSkipRom(OneWireMaster & master) {
-  error_code result = master.reset();
-  if (result) {
+Result<void> overdriveSkipRom(OneWireMaster & master) {
+  Result<void> result = master.reset();
+  if (!result) {
     return result;
   }
-  result = master.writeByte(OverdriveSkipRomCmd);
-  if (result) {
+  result = master.writeByte(0x3C);
+  if (!result) {
     return result;
   }
   result = master.setSpeed(OneWireMaster::OverdriveSpeed);
   return result;
 }
 
-error_code overdriveMatchRom(OneWireMaster & master, RomId::const_span romId) {
-  error_code result = master.reset();
-  if (result) {
+Result<void> overdriveMatchRom(OneWireMaster & master,
+                               RomId::const_span romId) {
+  Result<void> result = master.reset();
+  if (!result) {
     return result;
   }
-  result = master.writeByte(OverdriveMatchRomCmd);
-  if (result) {
+  result = master.writeByte(0x69);
+  if (!result) {
     return result;
   }
   result = master.setSpeed(OneWireMaster::OverdriveSpeed);
-  if (result) {
+  if (!result) {
     return result;
   }
   result = master.writeBlock(romId);
   return result;
 }
 
-error_code resumeRom(OneWireMaster & master) {
-  error_code result = master.reset();
-  if (result) {
+Result<void> resumeRom(OneWireMaster & master) {
+  Result<void> result = master.reset();
+  if (!result) {
     return result;
   }
-  result = master.writeByte(ResumeRomCmd);
+  result = master.writeByte(0xA5);
   return result;
 }
 
-error_code searchRom(OneWireMaster & master, SearchRomState & searchState) {
+Result<void> searchRom(OneWireMaster & master, SearchRomState & searchState) {
   if (searchState.lastDevice) {
     searchState = SearchRomState();
   }
 
-  error_code result = master.reset();
-  if (result) {
+  Result<void> result = master.reset();
+  if (!result) {
     return result;
   }
-  result = master.writeByte(SearchRomCmd);
-  if (result) {
+  result = master.writeByte(0xF0);
+  if (!result) {
     return result;
   }
 
@@ -178,12 +170,10 @@
           (searchState.romId[idByteNumber] & idBitMask) == idBitMask;
     }
 
-    result = master.triplet(tripletData);
-    if (result) {
-      return result;
-    }
+    MaximInterfaceCore_TRY_VALUE(tripletData,
+                                 master.triplet(tripletData.writeBit));
     if (tripletData.readBit && tripletData.readBitComplement) {
-      return make_error_code(OneWireMaster::NoSlaveError);
+      return OneWireMaster::NoSlaveError;
     }
 
     if (tripletData.writeBit) {
@@ -202,7 +192,7 @@
     }
     searchState = newSearchState;
   } else {
-    result = make_error_code(OneWireMaster::NoSlaveError);
+    result = OneWireMaster::NoSlaveError;
   }
   return result;
 }