Modified version of the official mbed lib providing a RTOS enabled i2c-driver based on the official i2c-C-api.
Dependencies: mbed-rtos mbed-src
I2CSlaveRtos.h
00001 #ifndef I2CSLAVERTOS_H 00002 #define I2CSLAVERTOS_H 00003 00004 #include "I2CDriver.h" 00005 00006 namespace mbed 00007 { 00008 00009 /// I2C slave interface to the RTOS-I2CDriver. 00010 /// The interface is compatible to the original mbed I2C class. 00011 class I2CSlaveRtos 00012 { 00013 I2CDriver m_drv; 00014 00015 public: 00016 /// Status returned by the receiveSlave() function 00017 enum RxStatus { 00018 NoData = 0, 00019 ReadAddressed = 1, 00020 WriteGeneral = 2, 00021 WriteAddressed = 3 00022 }; 00023 00024 /** Create an I2C Slave interface, connected to the specified pins. 00025 * 00026 * @param sda I2C data line pin 00027 * @param scl I2C clock line pin 00028 * 00029 * @note Has to be created in a thread context, i.e. within the main or some other function. A global delaration does not work 00030 */ 00031 I2CSlaveRtos(PinName sda, PinName scl, int freq=100000, int address=42) 00032 :m_drv(sda,scl,100000,address) {} 00033 00034 /** Set the frequency of the I2C interface 00035 * 00036 * @param hz The bus frequency in hertz 00037 */ 00038 void frequency(int hz) { 00039 m_drv.frequency(hz); 00040 } 00041 00042 /** Checks to see if this I2C Slave has been addressed. 00043 * 00044 * @returns 00045 * A status indicating if the device has been addressed, and how 00046 * - NoData - the slave has not been addressed 00047 * - ReadAddressed - the master has requested a read from this slave 00048 * - WriteAddressed - the master is writing to this slave 00049 * - WriteGeneral - the master is writing to all slave 00050 */ 00051 int receive(uint32_t timeout_ms=osWaitForever) { 00052 return m_drv.receiveSlave(timeout_ms); 00053 } 00054 00055 /** Read from an I2C master. 00056 * 00057 * @param data pointer to the byte array to read data in to 00058 * @param length maximum number of bytes to read 00059 * 00060 * @returns 00061 * 0 on success, 00062 * non-0 otherwise 00063 */ 00064 int read(char *data, int length) { 00065 return m_drv.readSlave(data, length); 00066 } 00067 00068 /** Read a single byte from an I2C master. 00069 * 00070 * @returns 00071 * the byte read 00072 */ 00073 int read(void) { 00074 return m_drv.readSlave(); 00075 } 00076 00077 /** Write to an I2C master. 00078 * 00079 * @param data pointer to the byte array to be transmitted 00080 * @param length the number of bytes to transmite 00081 * 00082 * @returns 00083 * 0 on success, 00084 * non-0 otherwise 00085 */ 00086 int write(const char *data, int length) { 00087 return m_drv.writeSlave(data, length); 00088 } 00089 00090 /** Write a single byte to an I2C master. 00091 * 00092 * @data the byte to write 00093 * 00094 * @returns 00095 * '1' if an ACK was received, 00096 * '0' otherwise 00097 */ 00098 int write(int data) { 00099 return m_drv.writeSlave(data); 00100 } 00101 00102 /** Sets the I2C slave address. 00103 * 00104 * @param address The address to set for the slave (ignoring the least 00105 * signifcant bit). If set to 0, the slave will only respond to the 00106 * general call address. 00107 */ 00108 void address(int address) { 00109 m_drv.addressSlave(address); 00110 } 00111 00112 00113 /** Reset the I2C slave back into the known ready receiving state. 00114 */ 00115 void stop(void){ 00116 m_drv.stopSlave(); 00117 } 00118 }; 00119 } 00120 00121 #endif
Generated on Sun Jul 17 2022 01:45:03 by 1.7.2