RTOS enabled i2c-driver based on the official i2c-C-api.
Fork of mbed-RtosI2cDriver by
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 * ... no! instead it returns number of bytes read minus one ... weird, guess its a bug in the official lib 00064 */ 00065 int read(char *data, int length) { 00066 return m_drv.readSlave(data, length); 00067 } 00068 00069 /** Read a single byte from an I2C master. 00070 * 00071 * @returns 00072 * the byte read 00073 */ 00074 int read(void) { 00075 return m_drv.readSlave(); 00076 } 00077 00078 /** Write to an I2C master. 00079 * 00080 * @param data pointer to the byte array to be transmitted 00081 * @param length the number of bytes to transmite 00082 * 00083 * @returns 00084 * 0 on success, 00085 * non-0 otherwise 00086 */ 00087 int write(const char *data, int length) { 00088 return m_drv.writeSlave(data, length); 00089 } 00090 00091 /** Write a single byte to an I2C master. 00092 * 00093 * @data the byte to write 00094 * 00095 * @returns 00096 * '1' if an ACK was received, 00097 * '0' otherwise 00098 */ 00099 int write(int data) { 00100 return m_drv.writeSlave(data); 00101 } 00102 00103 /** Sets the I2C slave address. 00104 * 00105 * @param address The address to set for the slave (ignoring the least 00106 * signifcant bit). If set to 0, the slave will only respond to the 00107 * general call address. 00108 */ 00109 void address(int address) { 00110 m_drv.addressSlave(address); 00111 } 00112 00113 00114 /** Reset the I2C slave back into the known ready receiving state. 00115 */ 00116 void stop(void) { 00117 m_drv.stopSlave(); 00118 } 00119 00120 00121 /// Wait until the interface becomes available. 00122 /// 00123 /// Useful if you want to run a sequence of command without interrution by another thread. 00124 /// There's no need to call this function for running single request, because all driver functions 00125 /// will lock the device for exclusive access automatically. 00126 void lock() { 00127 m_drv.lock(); 00128 } 00129 00130 /// Unlock the interface that has previously been locked by the same thread. 00131 void unlock() { 00132 m_drv.unlock(); 00133 } 00134 00135 }; 00136 } 00137 00138 #endif
Generated on Wed Jul 13 2022 17:20:05 by 1.7.2