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.
I2CSlave.h
00001 /* mbed Microcontroller Library 00002 * Copyright (c) 2006-2013 ARM Limited 00003 * 00004 * Permission is hereby granted, free of charge, to any person obtaining a copy 00005 * of this software and associated documentation files (the "Software"), to deal 00006 * in the Software without restriction, including without limitation the rights 00007 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 00008 * copies of the Software, and to permit persons to whom the Software is 00009 * furnished to do so, subject to the following conditions: 00010 * 00011 * The above copyright notice and this permission notice shall be included in 00012 * all copies or substantial portions of the Software. 00013 * 00014 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 00015 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 00016 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 00017 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 00018 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 00019 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 00020 * SOFTWARE. 00021 */ 00022 #ifndef MBED_I2C_SLAVE_H 00023 #define MBED_I2C_SLAVE_H 00024 00025 #include "platform.h" 00026 00027 #if DEVICE_I2CSLAVE 00028 00029 #include "i2c_api.h" 00030 00031 namespace mbed { 00032 00033 /** An I2C Slave, used for communicating with an I2C Master device 00034 * 00035 * Example: 00036 * @code 00037 * // Simple I2C responder 00038 * #include <mbed.h> 00039 * 00040 * I2CSlave slave(p9, p10); 00041 * 00042 * int main() { 00043 * char buf[10]; 00044 * char msg[] = "Slave!"; 00045 * 00046 * slave.address(0xA0); 00047 * while (1) { 00048 * int i = slave.receive(); 00049 * switch (i) { 00050 * case I2CSlave::ReadAddressed: 00051 * slave.write(msg, strlen(msg) + 1); // Includes null char 00052 * break; 00053 * case I2CSlave::WriteGeneral: 00054 * slave.read(buf, 10); 00055 * printf("Read G: %s\n", buf); 00056 * break; 00057 * case I2CSlave::WriteAddressed: 00058 * slave.read(buf, 10); 00059 * printf("Read A: %s\n", buf); 00060 * break; 00061 * } 00062 * for(int i = 0; i < 10; i++) buf[i] = 0; // Clear buffer 00063 * } 00064 * } 00065 * @endcode 00066 */ 00067 class I2CSlave { 00068 00069 public: 00070 enum RxStatus { 00071 NoData = 0, 00072 ReadAddressed = 1, 00073 WriteGeneral = 2, 00074 WriteAddressed = 3 00075 }; 00076 00077 /** Create an I2C Slave interface, connected to the specified pins. 00078 * 00079 * @param sda I2C data line pin 00080 * @param scl I2C clock line pin 00081 */ 00082 I2CSlave(PinName sda, PinName scl); 00083 00084 /** Set the frequency of the I2C interface 00085 * 00086 * @param hz The bus frequency in hertz 00087 */ 00088 void frequency(int hz); 00089 00090 /** Checks to see if this I2C Slave has been addressed. 00091 * 00092 * @returns 00093 * A status indicating if the device has been addressed, and how 00094 * - NoData - the slave has not been addressed 00095 * - ReadAddressed - the master has requested a read from this slave 00096 * - WriteAddressed - the master is writing to this slave 00097 * - WriteGeneral - the master is writing to all slave 00098 */ 00099 int receive(void); 00100 00101 /** Read from an I2C master. 00102 * 00103 * @param data pointer to the byte array to read data in to 00104 * @param length maximum number of bytes to read 00105 * 00106 * @returns 00107 * 0 on success, 00108 * non-0 otherwise 00109 */ 00110 int read(char *data, int length); 00111 00112 /** Read a single byte from an I2C master. 00113 * 00114 * @returns 00115 * the byte read 00116 */ 00117 int read(void); 00118 00119 /** Write to an I2C master. 00120 * 00121 * @param data pointer to the byte array to be transmitted 00122 * @param length the number of bytes to transmite 00123 * 00124 * @returns 00125 * 0 on success, 00126 * non-0 otherwise 00127 */ 00128 int write(const char *data, int length); 00129 00130 /** Write a single byte to an I2C master. 00131 * 00132 * @data the byte to write 00133 * 00134 * @returns 00135 * '1' if an ACK was received, 00136 * '0' otherwise 00137 */ 00138 int write(int data); 00139 00140 /** Sets the I2C slave address. 00141 * 00142 * @param address The address to set for the slave (ignoring the least 00143 * signifcant bit). If set to 0, the slave will only respond to the 00144 * general call address. 00145 */ 00146 void address(int address); 00147 00148 /** Reset the I2C slave back into the known ready receiving state. 00149 */ 00150 void stop(void); 00151 00152 protected: 00153 i2c_t _i2c; 00154 }; 00155 00156 } // namespace mbed 00157 00158 #endif 00159 00160 #endif 00161
Generated on Wed Jul 13 2022 02:59:11 by
