Aleksandrs Gumenuks / MaximInterface_Extended

Dependents:   mbed_DS28EC20_GPIO

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers I2CMaster.hpp Source File

I2CMaster.hpp

00001 /*******************************************************************************
00002 * Copyright (C) 2017 Maxim Integrated Products, Inc., All Rights Reserved.
00003 *
00004 * Permission is hereby granted, free of charge, to any person obtaining a
00005 * copy of this software and associated documentation files (the "Software"),
00006 * to deal in the Software without restriction, including without limitation
00007 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
00008 * and/or sell copies of the Software, and to permit persons to whom the
00009 * Software is furnished to do so, subject to the following conditions:
00010 *
00011 * The above copyright notice and this permission notice shall be included
00012 * in all copies or substantial portions of the Software.
00013 *
00014 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
00015 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
00016 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
00017 * IN NO EVENT SHALL MAXIM INTEGRATED BE LIABLE FOR ANY CLAIM, DAMAGES
00018 * OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
00019 * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
00020 * OTHER DEALINGS IN THE SOFTWARE.
00021 *
00022 * Except as contained in this notice, the name of Maxim Integrated
00023 * Products, Inc. shall not be used except as stated in the Maxim Integrated
00024 * Products, Inc. Branding Policy.
00025 *
00026 * The mere transfer of this software does not imply any licenses
00027 * of trade secrets, proprietary technology, copyrights, patents,
00028 * trademarks, maskwork rights, or any other form of intellectual
00029 * property whatsoever. Maxim Integrated Products, Inc. retains all
00030 * ownership rights.
00031 *******************************************************************************/
00032 
00033 #ifndef MaximInterface_I2CMaster
00034 #define MaximInterface_I2CMaster
00035 
00036 #include <stdint.h>
00037 #include <MaximInterface/Utilities/Export.h>
00038 #include <MaximInterface/Utilities/span.hpp>
00039 #include <MaximInterface/Utilities/system_error.hpp>
00040 
00041 namespace MaximInterface {
00042 
00043 /// I2C master interface.
00044 class I2CMaster {
00045 public:
00046   enum ErrorValue  {
00047     NackError = 1 ///< Transaction stopped due to a NACK from the slave device.
00048   };
00049 
00050   enum AckStatus { Nack, Ack };
00051 
00052   virtual ~I2CMaster() {}
00053 
00054   /// @brief Send start condition and address on the bus.
00055   /// @param address Address with R/W bit.
00056   virtual error_code start(uint_least8_t address) = 0;
00057   
00058   /// Send stop condition on the bus.
00059   virtual error_code stop() = 0;
00060   
00061   /// Write data byte to the bus.
00062   virtual error_code writeByte(uint_least8_t data) = 0;
00063   
00064   /// Write data block to the bus.
00065   MaximInterface_EXPORT virtual error_code
00066   writeBlock(span<const uint_least8_t> data);
00067   
00068   /// @brief
00069   /// Perform a complete write transaction on the bus with optional stop
00070   /// condition.
00071   /// @param address Address in 8-bit format.
00072   /// @param data Data to write to the bus.
00073   /// @param sendStop
00074   /// True to send a stop condition or false to set up a repeated start.
00075   error_code writePacket(uint_least8_t address, span<const uint_least8_t> data,
00076                          bool sendStop = true) {
00077     return writePacketImpl(address, data, sendStop);
00078   }
00079   
00080   /// @brief Read data byte from the bus.
00081   /// @param status Determines whether an ACK or NACK is sent after reading.
00082   /// @param[out] data Data read from the bus if successful.
00083   virtual error_code readByte(AckStatus status, uint_least8_t & data) = 0;
00084   
00085   /// @brief Read data block from the bus.
00086   /// @param status Determines whether an ACK or NACK is sent after reading.
00087   /// @param[out] data Data read from the bus if successful.
00088   MaximInterface_EXPORT virtual error_code
00089   readBlock(AckStatus status, span<uint_least8_t> data);
00090   
00091   /// @brief
00092   /// Perform a complete read transaction on the bus with optional stop
00093   /// condition.
00094   /// @param address Address in 8-bit format.
00095   /// @param[out] data Data read from the bus if successful.
00096   /// @param sendStop
00097   /// True to send a stop condition or false to set up a repeated start.
00098   error_code readPacket(uint_least8_t address, span<uint_least8_t> data,
00099                         bool sendStop = true) {
00100     return readPacketImpl(address, data, sendStop);
00101   }
00102 
00103   MaximInterface_EXPORT static const error_category & errorCategory();
00104 
00105 protected:
00106   MaximInterface_EXPORT virtual error_code
00107   writePacketImpl(uint_least8_t address, span<const uint_least8_t> data,
00108                   bool sendStop);
00109  
00110   MaximInterface_EXPORT virtual error_code
00111   readPacketImpl(uint_least8_t address, span<uint_least8_t> data,
00112                  bool sendStop);
00113 };
00114 
00115 inline error_code make_error_code(I2CMaster::ErrorValue  e) {
00116   return error_code(e, I2CMaster::errorCategory());
00117 }
00118 
00119 inline error_condition make_error_condition(I2CMaster::ErrorValue  e) {
00120   return error_condition(e, I2CMaster::errorCategory());
00121 }
00122 
00123 } // namespace MaximInterface
00124 
00125 #endif