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 mbed-src by
I2CSlave.h
00001 /* mbed Microcontroller Library 00002 * Copyright (c) 2006-2013 ARM Limited 00003 * 00004 * Licensed under the Apache License, Version 2.0 (the "License"); 00005 * you may not use this file except in compliance with the License. 00006 * You may obtain a copy of the License at 00007 * 00008 * http://www.apache.org/licenses/LICENSE-2.0 00009 * 00010 * Unless required by applicable law or agreed to in writing, software 00011 * distributed under the License is distributed on an "AS IS" BASIS, 00012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 00013 * See the License for the specific language governing permissions and 00014 * limitations under the License. 00015 */ 00016 #ifndef MBED_I2C_SLAVE_H 00017 #define MBED_I2C_SLAVE_H 00018 00019 #include "platform.h" 00020 00021 #if DEVICE_I2CSLAVE 00022 00023 #include "i2c_api.h" 00024 00025 namespace mbed { 00026 00027 /** An I2C Slave, used for communicating with an I2C Master device 00028 * 00029 * Example: 00030 * @code 00031 * // Simple I2C responder 00032 * #include <mbed.h> 00033 * 00034 * I2CSlave slave(p9, p10); 00035 * 00036 * int main() { 00037 * char buf[10]; 00038 * char msg[] = "Slave!"; 00039 * 00040 * slave.address(0xA0); 00041 * while (1) { 00042 * int i = slave.receive(); 00043 * switch (i) { 00044 * case I2CSlave::ReadAddressed: 00045 * slave.write(msg, strlen(msg) + 1); // Includes null char 00046 * break; 00047 * case I2CSlave::WriteGeneral: 00048 * slave.read(buf, 10); 00049 * printf("Read G: %s\n", buf); 00050 * break; 00051 * case I2CSlave::WriteAddressed: 00052 * slave.read(buf, 10); 00053 * printf("Read A: %s\n", buf); 00054 * break; 00055 * } 00056 * for(int i = 0; i < 10; i++) buf[i] = 0; // Clear buffer 00057 * } 00058 * } 00059 * @endcode 00060 */ 00061 class I2CSlave { 00062 00063 public: 00064 enum RxStatus { 00065 NoData = 0, 00066 ReadAddressed = 1, 00067 WriteGeneral = 2, 00068 WriteAddressed = 3 00069 }; 00070 00071 /** Create an I2C Slave interface, connected to the specified pins. 00072 * 00073 * @param sda I2C data line pin 00074 * @param scl I2C clock line pin 00075 */ 00076 I2CSlave(PinName sda, PinName scl); 00077 00078 /** Set the frequency of the I2C interface 00079 * 00080 * @param hz The bus frequency in hertz 00081 */ 00082 void frequency(int hz); 00083 00084 /** Checks to see if this I2C Slave has been addressed. 00085 * 00086 * @returns 00087 * A status indicating if the device has been addressed, and how 00088 * - NoData - the slave has not been addressed 00089 * - ReadAddressed - the master has requested a read from this slave 00090 * - WriteAddressed - the master is writing to this slave 00091 * - WriteGeneral - the master is writing to all slave 00092 */ 00093 int receive(void); 00094 00095 /** Read from an I2C master. 00096 * 00097 * @param data pointer to the byte array to read data in to 00098 * @param length maximum number of bytes to read 00099 * 00100 * @returns 00101 * 0 on success, 00102 * non-0 otherwise 00103 */ 00104 int read(char *data, int length); 00105 00106 /** Read a single byte from an I2C master. 00107 * 00108 * @returns 00109 * the byte read 00110 */ 00111 int read(void); 00112 00113 /** Write to an I2C master. 00114 * 00115 * @param data pointer to the byte array to be transmitted 00116 * @param length the number of bytes to transmite 00117 * 00118 * @returns 00119 * 0 on success, 00120 * non-0 otherwise 00121 */ 00122 int write(const char *data, int length); 00123 00124 /** Write a single byte to an I2C master. 00125 * 00126 * @data the byte to write 00127 * 00128 * @returns 00129 * '1' if an ACK was received, 00130 * '0' otherwise 00131 */ 00132 int write(int data); 00133 00134 /** Sets the I2C slave address. 00135 * 00136 * @param address The address to set for the slave (ignoring the least 00137 * signifcant bit). If set to 0, the slave will only respond to the 00138 * general call address. 00139 */ 00140 void address(int address); 00141 00142 /** Reset the I2C slave back into the known ready receiving state. 00143 */ 00144 void stop(void); 00145 00146 protected: 00147 i2c_t _i2c; 00148 }; 00149 00150 } // namespace mbed 00151 00152 #endif 00153 00154 #endif
Generated on Tue Jul 12 2022 21:21:33 by
