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.
I2CSlaveX.h
00001 #ifndef MBED_I2CSLAVEX_H 00002 #define MBED_I2CSLAVEX_H 00003 00004 #include "mbed.h" 00005 00006 namespace mbed { 00007 00008 /** An I2C Slave, used for communicating with an I2C Master device with support for Interrupts and multiple slave addresses 00009 * 00010 * Warning: Currently only somewhat tested with LPC1347. This should be concidered a proof of concept. Ideally this would 00011 * be merged into the original I2CSlave library. 00012 * 00013 * Example: 00014 * @code 00015 * // Simple Simulated I2C Read-Only EEPROM 00016 * #include <mbed.h> 00017 * #include "I2CSlaveX.h" 00018 * 00019 * I2CSlaveX slave(p9, p10); 00020 * 00021 * uint8_t eeprom_data[16] = {0); 00022 * uint8_t eeprom_addr = 0; 00023 * 00024 * void receive_handler() { 00025 * char addr; 00026 * int i = slave.receive(&addr); 00027 * switch (i) { 00028 * case I2CSlave::ReadAddressed: 00029 * slave.write(&eeprom_data[eeprom_addr], 1); 00030 * printf("Write @ %02X [%02X] : %02X\n", addr, eeprom_addr, eeprom_data[eeprom_addr]); 00031 * break; 00032 * case I2CSlave::WriteAddressed: 00033 * slave.read(&eeprom_addr, 1); 00034 * printf("Read @ %02X [%02X]\n", addr, eeprom_addr); 00035 * break; 00036 * } 00037 * } 00038 * 00039 * int main() { 00040 * slave.address(0xA0, 0); 00041 * slave.address(0x30, 1); 00042 * slave.attach(&receive_handler); 00043 * 00044 * while(1) { 00045 * printf("Nothing to do really..."); 00046 * eeprom_data[0]++; 00047 * wait(1); 00048 * } 00049 * } 00050 * @endcode 00051 */ 00052 class I2CSlaveX : public I2CSlave 00053 { 00054 public: 00055 /** Create an I2C Slave interface, connected to the specified pins. 00056 * 00057 * @param sda I2C data line pin 00058 * @param scl I2C clock line pin 00059 */ 00060 I2CSlaveX(PinName sda, PinName scl); 00061 00062 /** Checks to see if this I2C Slave has been addressed. 00063 * 00064 * @returns 00065 * A status indicating if the device has been addressed, and how 00066 * - NoData - the slave has not been addressed 00067 * - ReadAddressed - the master has requested a read from this slave 00068 * - WriteAddressed - the master is writing to this slave 00069 * - WriteGeneral - the master is writing to all slave 00070 */ 00071 int receive(char *data = NULL); 00072 00073 /** Read from an I2C master. 00074 * 00075 * @param data pointer to the byte array to read data in to 00076 * @param length maximum number of bytes to read 00077 * 00078 * @returns 00079 * 0 on success, 00080 * non-0 otherwise 00081 */ 00082 int read(char *data, int length); 00083 00084 /** Write to an I2C master. 00085 * 00086 * @param data pointer to the byte array to be transmitted 00087 * @param length the number of bytes to transmite 00088 * 00089 * @returns 00090 * 0 on success, 00091 * non-0 otherwise 00092 */ 00093 int write(const char *data, int length); 00094 00095 /** Sets the I2C slave address. 00096 * 00097 * @param address The address to set for the slave (ignoring the least 00098 * signifcant bit). If set to 0, the slave will only respond to the 00099 * general call address. 00100 */ 00101 void address(int address, int idx = 0); 00102 00103 /** Attach a function to call when state changed event occured on the I2C peripheral 00104 * 00105 * @param fptr A pointer to a void function, or 0 to set as none 00106 */ 00107 void attach(void (*fptr)(void)); 00108 00109 /** Attach a member function to call when state changed event occured on the I2C peripheral 00110 * 00111 * @param tptr pointer to the object to call the member function on 00112 * @param mptr pointer to the member function to be called 00113 */ 00114 template<typename T> 00115 void attach(T* tptr, void (T::*mptr)(void)) { 00116 _receive.attach(tptr, mptr); 00117 enable_irq(); 00118 } 00119 00120 /** Enable IRQ. This method depends on hw implementation, might enable one 00121 * port interrupts. For further information, check gpio_irq_enable(). 00122 */ 00123 void enable_irq(); 00124 00125 /** Disable IRQ. This method depends on hw implementation, might disable one 00126 * port interrupts. For further information, check gpio_irq_disable(). 00127 */ 00128 void disable_irq(); 00129 00130 static void _irq_handler(uint32_t id, uint8_t addr, uint8_t state); 00131 00132 protected: 00133 FunctionPointer _receive; 00134 00135 }; 00136 00137 } 00138 00139 #endif
Generated on Tue Jul 12 2022 21:31:39 by
1.7.2