Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Fork of mbed by
I2C.h
00001 /* mbed Microcontroller Library - I2C 00002 * Copyright (c) 2007-2011 ARM Limited. All rights reserved. 00003 */ 00004 00005 #ifndef MBED_I2C_H 00006 #define MBED_I2C_H 00007 00008 #include "device.h" 00009 00010 #if DEVICE_I2C 00011 00012 #include "platform.h" 00013 #include "PinNames.h" 00014 #include "PeripheralNames.h" 00015 #include "Base.h" 00016 00017 namespace mbed { 00018 00019 /** An I2C Master, used for communicating with I2C slave devices 00020 * 00021 * Example: 00022 * @code 00023 * // Read from I2C slave at address 0x62 00024 * 00025 * #include "mbed.h" 00026 * 00027 * I2C i2c(p28, p27); 00028 * 00029 * int main() { 00030 * int address = 0x62; 00031 * char data[2]; 00032 * i2c.read(address, data, 2); 00033 * } 00034 * @endcode 00035 */ 00036 class I2C : public Base { 00037 00038 public: 00039 00040 enum RxStatus { 00041 NoData 00042 , MasterGeneralCall 00043 , MasterWrite 00044 , MasterRead 00045 }; 00046 00047 enum Acknowledge { 00048 NoACK = 0 00049 , ACK = 1 00050 }; 00051 00052 /** Create an I2C Master interface, connected to the specified pins 00053 * 00054 * @param sda I2C data line pin 00055 * @param scl I2C clock line pin 00056 */ 00057 I2C(PinName sda, PinName scl, const char *name = NULL); 00058 00059 /** Set the frequency of the I2C interface 00060 * 00061 * @param hz The bus frequency in hertz 00062 */ 00063 void frequency(int hz); 00064 00065 /** Read from an I2C slave 00066 * 00067 * Performs a complete read transaction. The bottom bit of 00068 * the address is forced to 1 to indicate a read. 00069 * 00070 * @param address 8-bit I2C slave address [ addr | 1 ] 00071 * @param data Pointer to the byte-array to read data in to 00072 * @param length Number of bytes to read 00073 * @param repeated Repeated start, true - don't send stop at end 00074 * 00075 * @returns 00076 * 0 on success (ack), 00077 * non-0 on failure (nack) 00078 */ 00079 int read(int address, char *data, int length, bool repeated = false); 00080 00081 /** Read a single byte from the I2C bus 00082 * 00083 * @param ack indicates if the byte is to be acknowledged (1 = acknowledge) 00084 * 00085 * @returns 00086 * the byte read 00087 */ 00088 int read(int ack); 00089 00090 /** Write to an I2C slave 00091 * 00092 * Performs a complete write transaction. The bottom bit of 00093 * the address is forced to 0 to indicate a write. 00094 * 00095 * @param address 8-bit I2C slave address [ addr | 0 ] 00096 * @param data Pointer to the byte-array data to send 00097 * @param length Number of bytes to send 00098 * @param repeated Repeated start, true - do not send stop at end 00099 * 00100 * @returns 00101 * 0 on success (ack), 00102 * non-0 on failure (nack) 00103 */ 00104 int write(int address, const char *data, int length, bool repeated = false); 00105 00106 /** Write single byte out on the I2C bus 00107 * 00108 * @param data data to write out on bus 00109 * 00110 * @returns 00111 * '1' if an ACK was received, 00112 * '0' otherwise 00113 */ 00114 int write(int data); 00115 00116 /** Creates a start condition on the I2C bus 00117 */ 00118 void start(void); 00119 00120 /** Creates a stop condition on the I2C bus 00121 */ 00122 void stop(void); 00123 00124 protected: 00125 00126 void aquire(); 00127 00128 I2CName _i2c; 00129 static I2C *_owner; 00130 int _hz; 00131 00132 }; 00133 00134 } // namespace mbed 00135 00136 #endif 00137 00138 #endif 00139
Generated on Tue Jul 12 2022 20:12:25 by
