Mouse code for the MacroRat

Dependencies:   ITG3200 QEI

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