Please use mbed-src instead of this library.mbed-src supports GR-PEACH rev.C. mbed-srcライブラリをご利用ください。mbed-srcはGR-PEACH rev.Cに対応しています。

Fork of mbed-src_GR-PEACH_rev_c by GR-PEACH_producer_meeting

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers I2C.h Source File

I2C.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_H
00017 #define MBED_I2C_H
00018 
00019 #include "platform.h"
00020 
00021 #if DEVICE_I2C
00022 
00023 #include "i2c_api.h"
00024 
00025 namespace mbed {
00026 
00027 /** An I2C Master, used for communicating with I2C slave devices
00028  *
00029  * Example:
00030  * @code
00031  * // Read from I2C slave at address 0x62
00032  *
00033  * #include "mbed.h"
00034  *
00035  * I2C i2c(p28, p27);
00036  *
00037  * int main() {
00038  *     int address = 0x62;
00039  *     char data[2];
00040  *     i2c.read(address, data, 2);
00041  * }
00042  * @endcode
00043  */
00044 class I2C {
00045 
00046 public:
00047     enum RxStatus {
00048         NoData,
00049         MasterGeneralCall,
00050         MasterWrite,
00051         MasterRead
00052     };
00053 
00054     enum Acknowledge {
00055         NoACK = 0,
00056         ACK   = 1
00057     };
00058 
00059     /** Create an I2C Master interface, connected to the specified pins
00060      *
00061      *  @param sda I2C data line pin
00062      *  @param scl I2C clock line pin
00063      */
00064     I2C(PinName sda, PinName scl);
00065 
00066     /** Set the frequency of the I2C interface
00067      *
00068      *  @param hz The bus frequency in hertz
00069      */
00070     void frequency(int hz);
00071 
00072     /** Read from an I2C slave
00073      *
00074      * Performs a complete read transaction. The bottom bit of
00075      * the address is forced to 1 to indicate a read.
00076      *
00077      *  @param address 8-bit I2C slave address [ addr | 1 ]
00078      *  @param data Pointer to the byte-array to read data in to
00079      *  @param length Number of bytes to read
00080      *  @param repeated Repeated start, true - don't send stop at end
00081      *
00082      *  @returns
00083      *       0 on success (ack),
00084      *   non-0 on failure (nack)
00085      */
00086     int read(int address, char *data, int length, bool repeated = false);
00087 
00088     /** Read a single byte from the I2C bus
00089      *
00090      *  @param ack indicates if the byte is to be acknowledged (1 = acknowledge)
00091      *
00092      *  @returns
00093      *    the byte read
00094      */
00095     int read(int ack);
00096 
00097     /** Write to an I2C slave
00098      *
00099      * Performs a complete write transaction. The bottom bit of
00100      * the address is forced to 0 to indicate a write.
00101      *
00102      *  @param address 8-bit I2C slave address [ addr | 0 ]
00103      *  @param data Pointer to the byte-array data to send
00104      *  @param length Number of bytes to send
00105      *  @param repeated Repeated start, true - do not send stop at end
00106      *
00107      *  @returns
00108      *       0 on success (ack),
00109      *   non-0 on failure (nack)
00110      */
00111     int write(int address, const char *data, int length, bool repeated = false);
00112 
00113     /** Write single byte out on the I2C bus
00114      *
00115      *  @param data data to write out on bus
00116      *
00117      *  @returns
00118      *    '1' if an ACK was received,
00119      *    '0' otherwise
00120      */
00121     int write(int data);
00122 
00123     /** Creates a start condition on the I2C bus
00124      */
00125 
00126     void start(void);
00127 
00128     /** Creates a stop condition on the I2C bus
00129      */
00130     void stop(void);
00131 
00132 protected:
00133     void aquire();
00134 
00135     i2c_t _i2c;
00136     static I2C  *_owner;
00137     int         _hz;
00138 };
00139 
00140 } // namespace mbed
00141 
00142 #endif
00143 
00144 #endif