mbed library sources. Supersedes mbed-src.
Dependents: Nucleo_Hello_Encoder BLE_iBeaconScan AM1805_DEMO DISCO-F429ZI_ExportTemplate1 ... more
I2CSlave.h
00001 /* mbed Microcontroller Library 00002 * Copyright (c) 2006-2013 ARM Limited 00003 * SPDX-License-Identifier: Apache-2.0 00004 * 00005 * Licensed under the Apache License, Version 2.0 (the "License"); 00006 * you may not use this file except in compliance with the License. 00007 * You may obtain a copy of the License at 00008 * 00009 * http://www.apache.org/licenses/LICENSE-2.0 00010 * 00011 * Unless required by applicable law or agreed to in writing, software 00012 * distributed under the License is distributed on an "AS IS" BASIS, 00013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 00014 * See the License for the specific language governing permissions and 00015 * limitations under the License. 00016 */ 00017 #ifndef MBED_I2C_SLAVE_H 00018 #define MBED_I2C_SLAVE_H 00019 00020 #include "platform/platform.h" 00021 00022 #if DEVICE_I2CSLAVE || defined(DOXYGEN_ONLY) 00023 00024 #include "hal/i2c_api.h" 00025 00026 namespace mbed { 00027 /** \addtogroup drivers */ 00028 00029 /** An I2C Slave, used for communicating with an I2C Master device. 00030 * 00031 * @note Synchronization level: Not protected 00032 * 00033 * Example Simple I2C responder: 00034 * @code 00035 * #include <mbed.h> 00036 * 00037 * const int SLAVE_ADDRESS = 0xA0; 00038 * const char message[] = "Slave!"; 00039 * 00040 * I2CSlave slave(I2C_SDA, I2C_SCL); 00041 * 00042 * int main() { 00043 * slave.address(SLAVE_ADDRESS); 00044 * while (1) { 00045 * int operation = slave.receive(); 00046 * switch (operation) { 00047 * case I2CSlave::ReadAddressed: 00048 * int status = slave.write(message, sizeof(message)); 00049 * if (status == 0) { 00050 * printf("Written message: %s\n", message); 00051 * } else { 00052 * printf("Failed to write message.\n"); 00053 * } 00054 * break; 00055 * case I2CSlave::WriteGeneral: 00056 * int byte_read = slave.read(); 00057 * printf("Read General: %c (%d)\n", byte_read, byte_read); 00058 * break; 00059 * case I2CSlave::WriteAddressed: 00060 * int byte_read = slave.read(); 00061 * printf("Read Addressed: %c (%d)\n", byte_read, byte_read); 00062 * break; 00063 * } 00064 * } 00065 * } 00066 * @endcode 00067 * @ingroup drivers 00068 */ 00069 class I2CSlave { 00070 00071 public: 00072 enum RxStatus { 00073 NoData = 0, 00074 ReadAddressed = 1, 00075 WriteGeneral = 2, 00076 WriteAddressed = 3 00077 }; 00078 00079 /** Create an I2C Slave interface, connected to the specified pins. 00080 * 00081 * @param sda I2C data line pin. 00082 * @param scl I2C clock line pin. 00083 */ 00084 I2CSlave(PinName sda, PinName scl); 00085 00086 /** Set the frequency of the I2C interface. 00087 * 00088 * @param hz The bus frequency in Hertz. 00089 */ 00090 void frequency(int hz); 00091 00092 /** Check if this I2C Slave has been addressed. 00093 * 00094 * @return A status indicating if the device has been addressed and how. 00095 * @retval NoData The slave has not been addressed. 00096 * @retval ReadAddressed The master has requested a read from this slave. 00097 * @retval WriteAddressed The master is writing to this slave. 00098 * @retval WriteGeneral The master is writing to all slave. 00099 */ 00100 int receive(void); 00101 00102 /** Read specified number of bytes from an I2C master. 00103 * 00104 * @param data Pointer to the buffer to read data into. 00105 * @param length Number of bytes to read. 00106 * 00107 * @return Result of the operation. 00108 * @retval 0 If the number of bytes read is equal to length requested. 00109 * @retval nonzero On error or if the number of bytes read is less than requested. 00110 */ 00111 int read(char *data, int length); 00112 00113 /** Read a single byte from an I2C master. 00114 * 00115 * @return The byte read. 00116 */ 00117 int read(void); 00118 00119 /** Write to an I2C master. 00120 * 00121 * @param data Pointer to the buffer containing the data to be sent. 00122 * @param length Number of bytes to send. 00123 * 00124 * @return 00125 * @retval 0 If written all bytes successfully. 00126 * @retval nonzero On error or if the number of bytes written is less than requested. 00127 */ 00128 int write(const char *data, int length); 00129 00130 /** Write a single byte to an I2C master. 00131 * 00132 * @param data Value to write. 00133 * 00134 * @return Result of the operation. 00135 * @retval 0 If a NACK is received. 00136 * @retval 1 If an ACK is received. 00137 * @retval 2 On timeout. 00138 */ 00139 int write(int data); 00140 00141 /** Set the I2C slave address. 00142 * 00143 * @param address The address to set for the slave (least significant bit is ignored). 00144 * 00145 * @note If address is set to 0, the slave will only respond to the 00146 * general call address. 00147 */ 00148 void address(int address); 00149 00150 /** Reset the I2C slave back into the known ready receiving state. 00151 */ 00152 void stop(void); 00153 00154 #if !defined(DOXYGEN_ONLY) 00155 00156 protected: 00157 /* Internal i2c object identifying the resources */ 00158 i2c_t _i2c; 00159 00160 #endif //!defined(DOXYGEN_ONLY) 00161 }; 00162 00163 } // namespace mbed 00164 00165 #endif 00166 00167 #endif
Generated on Tue Jul 12 2022 20:41:15 by 1.7.2