Extended MaximInterface

Dependents:   mbed_DS28EC20_GPIO

Committer:
IanBenzMaxim
Date:
Thu Jan 11 13:50:39 2018 -0600
Revision:
3:f818ea5172ed
Parent:
0:f77ad7f72d04
Child:
6:a8c83a2e6fa4
Updated to version 1.1.

Who changed what in which revision?

UserRevisionLine numberNew contents of line
IanBenzMaxim 0:f77ad7f72d04 1 /*******************************************************************************
IanBenzMaxim 0:f77ad7f72d04 2 * Copyright (C) 2017 Maxim Integrated Products, Inc., All Rights Reserved.
IanBenzMaxim 0:f77ad7f72d04 3 *
IanBenzMaxim 0:f77ad7f72d04 4 * Permission is hereby granted, free of charge, to any person obtaining a
IanBenzMaxim 0:f77ad7f72d04 5 * copy of this software and associated documentation files (the "Software"),
IanBenzMaxim 0:f77ad7f72d04 6 * to deal in the Software without restriction, including without limitation
IanBenzMaxim 0:f77ad7f72d04 7 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
IanBenzMaxim 0:f77ad7f72d04 8 * and/or sell copies of the Software, and to permit persons to whom the
IanBenzMaxim 0:f77ad7f72d04 9 * Software is furnished to do so, subject to the following conditions:
IanBenzMaxim 0:f77ad7f72d04 10 *
IanBenzMaxim 0:f77ad7f72d04 11 * The above copyright notice and this permission notice shall be included
IanBenzMaxim 0:f77ad7f72d04 12 * in all copies or substantial portions of the Software.
IanBenzMaxim 0:f77ad7f72d04 13 *
IanBenzMaxim 0:f77ad7f72d04 14 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
IanBenzMaxim 0:f77ad7f72d04 15 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
IanBenzMaxim 0:f77ad7f72d04 16 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
IanBenzMaxim 0:f77ad7f72d04 17 * IN NO EVENT SHALL MAXIM INTEGRATED BE LIABLE FOR ANY CLAIM, DAMAGES
IanBenzMaxim 0:f77ad7f72d04 18 * OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
IanBenzMaxim 0:f77ad7f72d04 19 * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
IanBenzMaxim 0:f77ad7f72d04 20 * OTHER DEALINGS IN THE SOFTWARE.
IanBenzMaxim 0:f77ad7f72d04 21 *
IanBenzMaxim 0:f77ad7f72d04 22 * Except as contained in this notice, the name of Maxim Integrated
IanBenzMaxim 0:f77ad7f72d04 23 * Products, Inc. shall not be used except as stated in the Maxim Integrated
IanBenzMaxim 0:f77ad7f72d04 24 * Products, Inc. Branding Policy.
IanBenzMaxim 0:f77ad7f72d04 25 *
IanBenzMaxim 0:f77ad7f72d04 26 * The mere transfer of this software does not imply any licenses
IanBenzMaxim 0:f77ad7f72d04 27 * of trade secrets, proprietary technology, copyrights, patents,
IanBenzMaxim 0:f77ad7f72d04 28 * trademarks, maskwork rights, or any other form of intellectual
IanBenzMaxim 0:f77ad7f72d04 29 * property whatsoever. Maxim Integrated Products, Inc. retains all
IanBenzMaxim 0:f77ad7f72d04 30 * ownership rights.
IanBenzMaxim 0:f77ad7f72d04 31 *******************************************************************************/
IanBenzMaxim 0:f77ad7f72d04 32
IanBenzMaxim 0:f77ad7f72d04 33 #ifndef MaximInterface_I2CMaster
IanBenzMaxim 0:f77ad7f72d04 34 #define MaximInterface_I2CMaster
IanBenzMaxim 0:f77ad7f72d04 35
IanBenzMaxim 0:f77ad7f72d04 36 #include <stddef.h>
IanBenzMaxim 0:f77ad7f72d04 37 #include <stdint.h>
IanBenzMaxim 0:f77ad7f72d04 38 #include <MaximInterface/Utilities/Export.h>
IanBenzMaxim 0:f77ad7f72d04 39 #include <MaximInterface/Utilities/system_error.hpp>
IanBenzMaxim 0:f77ad7f72d04 40
IanBenzMaxim 0:f77ad7f72d04 41 namespace MaximInterface {
IanBenzMaxim 0:f77ad7f72d04 42
IanBenzMaxim 3:f818ea5172ed 43 /// I2C master interface.
IanBenzMaxim 0:f77ad7f72d04 44 class I2CMaster {
IanBenzMaxim 0:f77ad7f72d04 45 public:
IanBenzMaxim 0:f77ad7f72d04 46 enum ErrorValue {
IanBenzMaxim 0:f77ad7f72d04 47 NackError = 1 ///< Transaction stopped due to a NACK from the slave device.
IanBenzMaxim 0:f77ad7f72d04 48 };
IanBenzMaxim 0:f77ad7f72d04 49
IanBenzMaxim 3:f818ea5172ed 50 enum AckStatus { Nack, Ack };
IanBenzMaxim 0:f77ad7f72d04 51
IanBenzMaxim 0:f77ad7f72d04 52 virtual ~I2CMaster() {}
IanBenzMaxim 0:f77ad7f72d04 53
IanBenzMaxim 3:f818ea5172ed 54 /// Send start condition and address on the bus.
IanBenzMaxim 3:f818ea5172ed 55 /// @param address Address with R/W bit.
IanBenzMaxim 0:f77ad7f72d04 56 virtual error_code start(uint_least8_t address) = 0;
IanBenzMaxim 3:f818ea5172ed 57
IanBenzMaxim 3:f818ea5172ed 58 /// Send stop condition on the bus.
IanBenzMaxim 0:f77ad7f72d04 59 virtual error_code stop() = 0;
IanBenzMaxim 3:f818ea5172ed 60
IanBenzMaxim 3:f818ea5172ed 61 /// Write data byte to the bus.
IanBenzMaxim 0:f77ad7f72d04 62 virtual error_code writeByte(uint_least8_t data) = 0;
IanBenzMaxim 3:f818ea5172ed 63
IanBenzMaxim 3:f818ea5172ed 64 /// Write data block to the bus.
IanBenzMaxim 0:f77ad7f72d04 65 MaximInterface_EXPORT virtual error_code
IanBenzMaxim 0:f77ad7f72d04 66 writeBlock(const uint_least8_t * data, size_t dataLen);
IanBenzMaxim 3:f818ea5172ed 67
IanBenzMaxim 3:f818ea5172ed 68 /// Perform a complete write transaction on the bus with optional stop
IanBenzMaxim 3:f818ea5172ed 69 /// condition.
IanBenzMaxim 3:f818ea5172ed 70 /// @param address Address in 8-bit format.
IanBenzMaxim 3:f818ea5172ed 71 /// @param sendStop
IanBenzMaxim 3:f818ea5172ed 72 /// True to send a stop condition or false to set up a repeated start.
IanBenzMaxim 0:f77ad7f72d04 73 error_code writePacket(uint_least8_t address, const uint_least8_t * data,
IanBenzMaxim 3:f818ea5172ed 74 size_t dataLen, bool sendStop = true) {
IanBenzMaxim 3:f818ea5172ed 75 return writePacketImpl(address, data, dataLen, sendStop);
IanBenzMaxim 0:f77ad7f72d04 76 }
IanBenzMaxim 3:f818ea5172ed 77
IanBenzMaxim 3:f818ea5172ed 78 /// Read data byte from the bus.
IanBenzMaxim 3:f818ea5172ed 79 /// @param status Determines whether an ACK or NACK is sent after reading.
IanBenzMaxim 3:f818ea5172ed 80 /// @param[out] data Data read from the bus if successful.
IanBenzMaxim 0:f77ad7f72d04 81 virtual error_code readByte(AckStatus status, uint_least8_t & data) = 0;
IanBenzMaxim 3:f818ea5172ed 82
IanBenzMaxim 3:f818ea5172ed 83 /// Read data block from the bus.
IanBenzMaxim 3:f818ea5172ed 84 /// @param status Determines whether an ACK or NACK is sent after reading.
IanBenzMaxim 3:f818ea5172ed 85 /// @param[out] data Data read from the bus if successful.
IanBenzMaxim 3:f818ea5172ed 86 /// @param dataLen Number of bytes to read and length of data.
IanBenzMaxim 0:f77ad7f72d04 87 MaximInterface_EXPORT virtual error_code
IanBenzMaxim 0:f77ad7f72d04 88 readBlock(AckStatus status, uint_least8_t * data, size_t dataLen);
IanBenzMaxim 3:f818ea5172ed 89
IanBenzMaxim 3:f818ea5172ed 90 /// Perform a complete read transaction on the bus with optional stop
IanBenzMaxim 3:f818ea5172ed 91 /// condition.
IanBenzMaxim 3:f818ea5172ed 92 /// @param address Address in 8-bit format.
IanBenzMaxim 3:f818ea5172ed 93 /// @param sendStop
IanBenzMaxim 3:f818ea5172ed 94 /// True to send a stop condition or false to set up a repeated start.
IanBenzMaxim 0:f77ad7f72d04 95 error_code readPacket(uint_least8_t address, uint_least8_t * data,
IanBenzMaxim 3:f818ea5172ed 96 size_t dataLen, bool sendStop = true) {
IanBenzMaxim 3:f818ea5172ed 97 return readPacketImpl(address, data, dataLen, sendStop);
IanBenzMaxim 0:f77ad7f72d04 98 }
IanBenzMaxim 0:f77ad7f72d04 99
IanBenzMaxim 0:f77ad7f72d04 100 MaximInterface_EXPORT static const error_category & errorCategory();
IanBenzMaxim 3:f818ea5172ed 101
IanBenzMaxim 3:f818ea5172ed 102 protected:
IanBenzMaxim 3:f818ea5172ed 103 MaximInterface_EXPORT virtual error_code
IanBenzMaxim 3:f818ea5172ed 104 writePacketImpl(uint_least8_t address, const uint_least8_t * data,
IanBenzMaxim 3:f818ea5172ed 105 size_t dataLen, bool sendStop);
IanBenzMaxim 3:f818ea5172ed 106
IanBenzMaxim 3:f818ea5172ed 107 MaximInterface_EXPORT virtual error_code readPacketImpl(uint_least8_t address,
IanBenzMaxim 3:f818ea5172ed 108 uint_least8_t * data,
IanBenzMaxim 3:f818ea5172ed 109 size_t dataLen,
IanBenzMaxim 3:f818ea5172ed 110 bool sendStop);
IanBenzMaxim 0:f77ad7f72d04 111 };
IanBenzMaxim 0:f77ad7f72d04 112
IanBenzMaxim 0:f77ad7f72d04 113 inline error_code make_error_code(I2CMaster::ErrorValue e) {
IanBenzMaxim 0:f77ad7f72d04 114 return error_code(e, I2CMaster::errorCategory());
IanBenzMaxim 0:f77ad7f72d04 115 }
IanBenzMaxim 0:f77ad7f72d04 116 inline error_condition make_error_condition(I2CMaster::ErrorValue e) {
IanBenzMaxim 0:f77ad7f72d04 117 return error_condition(e, I2CMaster::errorCategory());
IanBenzMaxim 0:f77ad7f72d04 118 }
IanBenzMaxim 0:f77ad7f72d04 119
IanBenzMaxim 0:f77ad7f72d04 120 } // namespace MaximInterface
IanBenzMaxim 0:f77ad7f72d04 121
IanBenzMaxim 0:f77ad7f72d04 122 #endif