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.
Dependencies: nRF51_Vdd TextLCD BME280
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/platform.h" 00020 00021 #if defined (DEVICE_I2CSLAVE) || defined(DOXYGEN_ONLY) 00022 00023 #include "hal/i2c_api.h" 00024 00025 namespace mbed { 00026 /** \addtogroup drivers */ 00027 00028 /** An I2C Slave, used for communicating with an I2C Master device. 00029 * 00030 * @note Synchronization level: Not protected 00031 * 00032 * Example Simple I2C responder: 00033 * @code 00034 * #include <mbed.h> 00035 * 00036 * const int SLAVE_ADDRESS = 0xA0; 00037 * const char message[] = "Slave!"; 00038 * 00039 * I2CSlave slave(I2C_SDA, I2C_SCL); 00040 * 00041 * int main() { 00042 * slave.address(SLAVE_ADDRESS); 00043 * while (1) { 00044 * int operation = slave.receive(); 00045 * switch (operation) { 00046 * case I2CSlave::ReadAddressed: 00047 * int status = slave.write(message, sizeof(message)); 00048 * if (status == 0) { 00049 * printf("Written message: %s\n", message); 00050 * } else { 00051 * printf("Failed to write message.\n"); 00052 * } 00053 * break; 00054 * case I2CSlave::WriteGeneral: 00055 * int byte_read = slave.read(); 00056 * printf("Read General: %c (%d)\n", byte_read, byte_read); 00057 * break; 00058 * case I2CSlave::WriteAddressed: 00059 * int byte_read = slave.read(); 00060 * printf("Read Addressed: %c (%d)\n", byte_read, byte_read); 00061 * break; 00062 * } 00063 * } 00064 * } 00065 * @endcode 00066 * @ingroup drivers 00067 */ 00068 class I2CSlave { 00069 00070 public: 00071 enum RxStatus { 00072 NoData = 0, 00073 ReadAddressed = 1, 00074 WriteGeneral = 2, 00075 WriteAddressed = 3 00076 }; 00077 00078 /** Create an I2C Slave interface, connected to the specified pins. 00079 * 00080 * @param sda I2C data line pin. 00081 * @param scl I2C clock line pin. 00082 */ 00083 I2CSlave(PinName sda, PinName scl); 00084 00085 /** Set the frequency of the I2C interface. 00086 * 00087 * @param hz The bus frequency in Hertz. 00088 */ 00089 void frequency(int hz); 00090 00091 /** Check if this I2C Slave has been addressed. 00092 * 00093 * @return A status indicating if the device has been addressed and how. 00094 * @retval NoData The slave has not been addressed. 00095 * @retval ReadAddressed The master has requested a read from this slave. 00096 * @retval WriteAddressed The master is writing to this slave. 00097 * @retval WriteGeneral The master is writing to all slave. 00098 */ 00099 int receive(void); 00100 00101 /** Read specified number of bytes from an I2C master. 00102 * 00103 * @param data Pointer to the buffer to read data into. 00104 * @param length Number of bytes to read. 00105 * 00106 * @return Result of the operation. 00107 * @retval 0 If the number of bytes read is equal to length requested. 00108 * @retval nonzero On error or if the number of bytes read is less than requested. 00109 */ 00110 int read(char *data, int length); 00111 00112 /** Read a single byte from an I2C master. 00113 * 00114 * @return The byte read. 00115 */ 00116 int read(void); 00117 00118 /** Write to an I2C master. 00119 * 00120 * @param data Pointer to the buffer containing the data to be sent. 00121 * @param length Number of bytes to send. 00122 * 00123 * @return 00124 * @retval 0 If written all bytes successfully. 00125 * @retval nonzero On error or if the number of bytes written is less than requested. 00126 */ 00127 int write(const char *data, int length); 00128 00129 /** Write a single byte to an I2C master. 00130 * 00131 * @param data Value to write. 00132 * 00133 * @return Result of the operation. 00134 * @retval 0 If a NACK is received. 00135 * @retval 1 If an ACK is received. 00136 * @retval 2 On timeout. 00137 */ 00138 int write(int data); 00139 00140 /** Set the I2C slave address. 00141 * 00142 * @param address The address to set for the slave (least significant bit is ignored). 00143 * 00144 * @note If address is set to 0, the slave will only respond to the 00145 * general call address. 00146 */ 00147 void address(int address); 00148 00149 /** Reset the I2C slave back into the known ready receiving state. 00150 */ 00151 void stop(void); 00152 00153 #if !defined(DOXYGEN_ONLY) 00154 00155 protected: 00156 /* Internal i2c object identifying the resources */ 00157 i2c_t _i2c; 00158 00159 #endif //!defined(DOXYGEN_ONLY) 00160 }; 00161 00162 } // namespace mbed 00163 00164 #endif 00165 00166 #endif
Generated on Tue Jul 12 2022 15:15:47 by
