Kenji Arai / mbed-os_TYBLE16

Dependents:   TYBLE16_simple_data_logger TYBLE16_MP3_Air

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers I2CSlave.h Source File

I2CSlave.h

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