This fork captures the mbed lib v125 for ease of integration into older projects.

Fork of mbed-dev by mbed official

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-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  * @Note Synchronization level: Not protected
00030  *
00031  * Example:
00032  * @code
00033  * // Simple I2C responder
00034  * #include <mbed.h>
00035  *
00036  * I2CSlave slave(p9, p10);
00037  *
00038  * int main() {
00039  *     char buf[10];
00040  *     char msg[] = "Slave!";
00041  *
00042  *     slave.address(0xA0);
00043  *     while (1) {
00044  *         int i = slave.receive();
00045  *         switch (i) {
00046  *             case I2CSlave::ReadAddressed:
00047  *                 slave.write(msg, strlen(msg) + 1); // Includes null char
00048  *                 break;
00049  *             case I2CSlave::WriteGeneral:
00050  *                 slave.read(buf, 10);
00051  *                 printf("Read G: %s\n", buf);
00052  *                 break;
00053  *             case I2CSlave::WriteAddressed:
00054  *                 slave.read(buf, 10);
00055  *                 printf("Read A: %s\n", buf);
00056  *                 break;
00057  *         }
00058  *         for(int i = 0; i < 10; i++) buf[i] = 0;    // Clear buffer
00059  *     }
00060  * }
00061  * @endcode
00062  */
00063 class I2CSlave {
00064 
00065 public:
00066     enum RxStatus {
00067         NoData         = 0,
00068         ReadAddressed  = 1,
00069         WriteGeneral   = 2,
00070         WriteAddressed = 3
00071     };
00072 
00073     /** Create an I2C Slave interface, connected to the specified pins.
00074      *
00075      *  @param sda I2C data line pin
00076      *  @param scl I2C clock line pin
00077      */
00078     I2CSlave(PinName sda, PinName scl);
00079 
00080     /** Set the frequency of the I2C interface
00081      *
00082      *  @param hz The bus frequency in hertz
00083      */
00084     void frequency(int hz);
00085 
00086     /** Checks to see if this I2C Slave has been addressed.
00087      *
00088      *  @returns
00089      *  A status indicating if the device has been addressed, and how
00090      *  - NoData            - the slave has not been addressed
00091      *  - ReadAddressed     - the master has requested a read from this slave
00092      *  - WriteAddressed    - the master is writing to this slave
00093      *  - WriteGeneral      - the master is writing to all slave
00094      */
00095     int receive(void);
00096 
00097     /** Read from an I2C master.
00098      *
00099      *  @param data pointer to the byte array to read data in to
00100      *  @param length maximum number of bytes to read
00101      *
00102      *  @returns
00103      *       0 on success,
00104      *   non-0 otherwise
00105      */
00106     int read(char *data, int length);
00107 
00108     /** Read a single byte from an I2C master.
00109      *
00110      *  @returns
00111      *    the byte read
00112      */
00113     int read(void);
00114 
00115     /** Write to an I2C master.
00116      *
00117      *  @param data pointer to the byte array to be transmitted
00118      *  @param length the number of bytes to transmite
00119      *
00120      *  @returns
00121      *       0 on success,
00122      *   non-0 otherwise
00123      */
00124     int write(const char *data, int length);
00125 
00126     /** Write a single byte to an I2C master.
00127      *
00128      *  @data the byte to write
00129      *
00130      *  @returns
00131      *    '1' if an ACK was received,
00132      *    '0' otherwise
00133      */
00134     int write(int data);
00135 
00136     /** Sets the I2C slave address.
00137      *
00138      *  @param address The address to set for the slave (ignoring the least
00139      *  signifcant bit). If set to 0, the slave will only respond to the
00140      *  general call address.
00141      */
00142     void address(int address);
00143 
00144     /** Reset the I2C slave back into the known ready receiving state.
00145      */
00146     void stop(void);
00147 
00148 protected:
00149     i2c_t _i2c;
00150 };
00151 
00152 } // namespace mbed
00153 
00154 #endif
00155 
00156 #endif