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 SoftI2C by
SoftI2C.h
00001 #include "mbed.h" 00002 00003 #ifndef SOFTI2C_H 00004 #define SOFTI2C_H 00005 00006 /** 00007 * A software I2C class in case you run out of hardware I2C pins or cannot use 00008 * those for another reason. 00009 * 00010 * The class is a drop-in replacement of the classic mbed I2C class: include 00011 * this file in your program, replace I2C with SoftI2C when creating the object 00012 * and it should work properly without other modifications. 00013 */ 00014 class SoftI2C { 00015 public: 00016 /** 00017 * Create an software I2C Master interface, connected to the specified pins 00018 * 00019 * @param sda I2C data pin 00020 * @param scl I2C clock pin 00021 */ 00022 SoftI2C(PinName sda, PinName scl); 00023 00024 /** 00025 * Set the frequency of the I2C interface. 00026 * 00027 * Note that the delay of the DigitalInOuts is not compensated: the real 00028 * frequency will be lower than the one set. However since I2C is a 00029 * synchronous protocol this shouldn't affect functionality 00030 * 00031 * @param hz The bus frequency in hertz 00032 */ 00033 void frequency(int hz); 00034 00035 /** 00036 * Read from an I2C slave. 00037 * 00038 * Performs a complete read transaction. The bottom bit of the address is forced to 1 to indicate a read. 00039 * 00040 * @param address 8-bit I2C slave address [ addr | 1 ] 00041 * @param Pointer to the byte-array data to read to 00042 * @param length Number of bytes to read 00043 * @param repeated Repeated start, true - do not send stop at end 00044 * @return 0 on success (ack), non-0 on failure (nack) 00045 */ 00046 int read(int address, char *data, int length, bool repeated=false); 00047 00048 /** 00049 * Read a single byte from the I2C bus 00050 * 00051 * @param ack indicates if the byte is to be acknowledged (1 = acknowledge) 00052 * @return the byte read 00053 */ 00054 int read(int ack); 00055 00056 /** 00057 * Write to an I2C slave. 00058 * 00059 * Performs a complete write transaction. The bottom bit of the address is forced to 0 to indicate a write. 00060 * 00061 * @param address 8-bit I2C slave address [ addr | 0 ] 00062 * @param Pointer to the byte-array data to send 00063 * @param length Number of bytes to send 00064 * @param repeated Repeated start, true - do not send stop at end 00065 * @return 0 on success (ack), non-0 on failure (nack) 00066 */ 00067 int write(int address, const char *data, int length, bool repeated=false); 00068 00069 /** 00070 * Write single byte out on the I2C bus 00071 * 00072 * @param data data to write on the bus 00073 * @return '1' if an ACK is received, '0' otherwise 00074 */ 00075 int write(int data); 00076 00077 /** 00078 * Create a (re-)start condition on the I2C bus 00079 */ 00080 void start(void); 00081 00082 /** 00083 * Create a stop condition on the I2C bus 00084 */ 00085 void stop(void); 00086 00087 protected: 00088 DigitalInOut _sda; 00089 DigitalInOut _scl; 00090 int delay_us; 00091 bool active; 00092 }; 00093 00094 00095 #endif
Generated on Fri Jul 15 2022 09:54:37 by
1.7.2
