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/MaximInterfaceDevices/DS28E17.hpp	Mon Jul 22 11:44:07 2019 -0500
+++ b/MaximInterfaceDevices/DS28E17.hpp	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"),
@@ -30,8 +30,8 @@
 * ownership rights.
 *******************************************************************************/
 
-#ifndef MaximInterfaceDevices_DS28E17
-#define MaximInterfaceDevices_DS28E17
+#ifndef MaximInterfaceDevices_DS28E17_hpp
+#define MaximInterfaceDevices_DS28E17_hpp
 
 #include <stdint.h>
 #include <MaximInterfaceCore/SelectRom.hpp>
@@ -55,13 +55,13 @@
 /// provides 1-Wire access for a single I2C interface.
 class DS28E17 {
 public:
+  // Values from 1-255 represent the Write Status byte.
   enum ErrorValue {
-    TimeoutError = 1,
+    TimeoutError = 256,
     OutOfRangeError,
     InvalidCrc16Error,
     AddressNackError,
-    InvalidStartError,
-    WriteNackError
+    InvalidStartError
   };
 
   enum I2CSpeed { Speed100kHz, Speed400kHz, Speed900kHz };
@@ -81,14 +81,12 @@
   /// I2C slave address. The least significant bit of the I2C
   /// address is automatically cleared by the command.
   /// @param[in] data I2C data to write with length 1-255.
-  /// @param[out] wr_status
-  /// Indicates which write byte NACK’d. A value of 00h indicates all bytes
-  /// were acknowledged by the slave. A non-zero value indicates the byte number
-  /// that NACK’d. May be set to NULL.
-  MaximInterfaceDevices_EXPORT Core::error_code
+  /// @returns
+  /// Values from 1-255 in the DS28E17 category represent the Write Status
+  /// indicating which write byte did not acknowledge.
+  MaximInterfaceDevices_EXPORT Core::Result<void>
   writeDataWithStop(uint_least8_t I2C_addr,
-                    Core::span<const uint_least8_t> data,
-                    uint_least8_t * wr_status = NULL);
+                    Core::span<const uint_least8_t> data);
 
   /// @brief Write Data No Stop command.
   /// @details Output on I2C: S, Address + Write, Write Data [1-255]
@@ -96,35 +94,29 @@
   /// I2C slave address. The least significant bit of the I2C address
   /// is automatically cleared by the command.
   /// @param[in] data I2C data to write with length 1-255.
-  /// @param[out] wr_status
-  /// Indicates which write byte NACK’d. A value of 00h indicates all bytes
-  /// were acknowledged by the slave. A non-zero value indicates the byte number
-  /// that NACK’d. May be set to NULL.
-  MaximInterfaceDevices_EXPORT Core::error_code
-  writeDataNoStop(uint_least8_t I2C_addr, Core::span<const uint_least8_t> data,
-                  uint_least8_t * wr_status = NULL);
+  /// @returns
+  /// Values from 1-255 in the DS28E17 category represent the Write Status
+  /// indicating which write byte did not acknowledge.
+  MaximInterfaceDevices_EXPORT Core::Result<void>
+  writeDataNoStop(uint_least8_t I2C_addr, Core::span<const uint_least8_t> data);
 
   /// @brief Write Data Only command.
   /// @details Output on I2C: Write Data [1-255]
   /// @param[in] data I2C data to write with length 1-255.
-  /// @param[out] wr_status
-  /// Indicates which write byte NACK’d. A value of 00h indicates all bytes
-  /// were acknowledged by the slave. A non-zero value indicates the byte number
-  /// that NACK’d. May be set to NULL.
-  MaximInterfaceDevices_EXPORT Core::error_code
-  writeDataOnly(Core::span<const uint_least8_t> data,
-                uint_least8_t * wr_status = NULL);
+  /// @returns
+  /// Values from 1-255 in the DS28E17 category represent the Write Status
+  /// indicating which write byte did not acknowledge.
+  MaximInterfaceDevices_EXPORT Core::Result<void>
+  writeDataOnly(Core::span<const uint_least8_t> data);
 
   /// @brief Write Data Only With Stop command.
   /// @details Output on I2C: Write Data [1-255], P
   /// @param[in] data I2C data to write with length 1-255.
-  /// @param[out] wr_status
-  /// Indicates which write byte NACK’d. A value of 00h indicates all bytes
-  /// were acknowledged by the slave. A non-zero value indicates the byte number
-  /// that NACK’d. May be set to NULL.
-  MaximInterfaceDevices_EXPORT Core::error_code
-  writeDataOnlyWithStop(Core::span<const uint_least8_t> data,
-                        uint_least8_t * wr_status = NULL);
+  /// @returns
+  /// Values from 1-255 in the DS28E17 category represent the Write Status
+  /// indicating which write byte did not acknowledge.
+  MaximInterfaceDevices_EXPORT Core::Result<void>
+  writeDataOnlyWithStop(Core::span<const uint_least8_t> data);
 
   /// @brief Write, Read Data With Stop command.
   /// @details Output on I2C:
@@ -135,13 +127,13 @@
   /// is automatically cleared and set by the command.
   /// @param[in] write_data I2C data to write with length 1-255.
   /// @param[out] read_data I2C data that was read with length 1-255.
-  /// @param[out] wr_status
-  /// Indicates which write byte NACK’d. A value of 00h indicates all bytes
-  /// were acknowledged by the slave. A non-zero value indicates the byte number
-  /// that NACK’d. May be set to NULL.
-  MaximInterfaceDevices_EXPORT Core::error_code writeReadDataWithStop(
-      uint_least8_t I2C_addr, Core::span<const uint_least8_t> write_data,
-      Core::span<uint_least8_t> read_data, uint_least8_t * wr_status = NULL);
+  /// @returns
+  /// Values from 1-255 in the DS28E17 category represent the Write Status
+  /// indicating which write byte did not acknowledge.
+  MaximInterfaceDevices_EXPORT Core::Result<void>
+  writeReadDataWithStop(uint_least8_t I2C_addr,
+                        Core::span<const uint_least8_t> write_data,
+                        Core::span<uint_least8_t> read_data);
 
   /// @brief Read Data With Stop command.
   /// @details Output on I2C:
@@ -150,28 +142,29 @@
   /// I2C slave address. The least significant bit of the I2C address
   /// is automatically set by the command.
   /// @param[out] data I2C data that was read with length 1-255.
-  MaximInterfaceDevices_EXPORT Core::error_code
+  MaximInterfaceDevices_EXPORT Core::Result<void>
   readDataWithStop(uint_least8_t I2C_addr, Core::span<uint_least8_t> data);
 
   /// Write to Configuration Register of DS28E17.
-  MaximInterfaceDevices_EXPORT Core::error_code writeConfigReg(I2CSpeed speed);
+  MaximInterfaceDevices_EXPORT Core::Result<void>
+  writeConfigReg(I2CSpeed speed);
 
   /// @brief Read the Configuration Register of DS28E17.
-  /// @param[out] speed Speed read from configuration register.
-  MaximInterfaceDevices_EXPORT Core::error_code readConfigReg(I2CSpeed & speed);
+  /// @returns speed Speed read from configuration register.
+  MaximInterfaceDevices_EXPORT Core::Result<I2CSpeed> readConfigReg() const;
 
   /// @brief Put the device into a low current mode.
   /// @details All 1-Wire communication is ignored until woken up. Immediately
   /// after the command, the device monitors the WAKEUP input pin and exits
   /// sleep mode on a rising edge.
-  MaximInterfaceDevices_EXPORT Core::error_code enableSleepMode();
+  MaximInterfaceDevices_EXPORT Core::Result<void> enableSleepMode();
 
   /// @brief Read the Device Revision of DS28E17.
   /// @details The upper nibble is the major revision,
   /// and the lower nibble is the minor revision.
-  /// @param[out] rev Device Revision.
-  MaximInterfaceDevices_EXPORT Core::error_code
-  readDeviceRevision(uint_least8_t & rev);
+  /// @returns Device Revision.
+  MaximInterfaceDevices_EXPORT Core::Result<uint_least8_t>
+  readDeviceRevision() const;
 
   MaximInterfaceDevices_EXPORT static const Core::error_category &
   errorCategory();
@@ -190,15 +183,24 @@
     ReadDeviceRevisionCmd = 0xC3
   };
 
-  Core::error_code sendPacket(Command command, const uint_least8_t * I2C_addr,
-                              Core::span<const uint_least8_t> write_data,
-                              Core::span<uint_least8_t> read_data,
-                              uint_least8_t * wr_status);
+  Core::Result<void> sendPacket(Command command, const uint_least8_t * I2C_addr,
+                                Core::span<const uint_least8_t> write_data,
+                                Core::span<uint_least8_t> read_data);
 
   Core::SelectRom selectRom;
   Core::OneWireMaster * master;
 };
 
+} // namespace MaximInterfaceDevices
+namespace MaximInterfaceCore {
+
+template <>
+struct is_error_code_enum<MaximInterfaceDevices::DS28E17::ErrorValue>
+    : true_type {};
+
+} // namespace MaximInterfaceCore
+namespace MaximInterfaceDevices {
+
 inline Core::error_code make_error_code(DS28E17::ErrorValue e) {
   return Core::error_code(e, DS28E17::errorCategory());
 }