Modified I2C api to work with NRF's dynamic pin asignment.

Dependents:   Seed_Barometer_Sensor_custom_I2C_lib

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers I2CX.h Source File

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