mbed SDK library sources

Fork of mbed-src by mbed official

Development branch of the mbed library sources. This library is kept in synch with the latest changes from the mbed SDK and it is not guaranteed to work.

If you are looking for a stable and tested release, please import one of the official mbed library releases:

Import librarymbed

The official Mbed 2 C/C++ SDK provides the software platform and libraries to build your applications.

Committer:
emilmont
Date:
Mon Jun 10 16:03:00 2013 +0100
Revision:
9:0ce32e54c9a7
Parent:
cpp/I2C.h@2:143cac498751
Child:
10:3bc89ef62ce7
Refactoring of the mbed SDK:
- Provide a well defined HAL and API
- Keep separated the HAL implementations for the different targets

Who changed what in which revision?

UserRevisionLine numberNew contents of line
mbed_official 0:fd0d7bdfcdc2 1 /* mbed Microcontroller Library
emilmont 2:143cac498751 2 * Copyright (c) 2006-2013 ARM Limited
mbed_official 0:fd0d7bdfcdc2 3 *
emilmont 2:143cac498751 4 * Licensed under the Apache License, Version 2.0 (the "License");
emilmont 2:143cac498751 5 * you may not use this file except in compliance with the License.
emilmont 2:143cac498751 6 * You may obtain a copy of the License at
mbed_official 0:fd0d7bdfcdc2 7 *
emilmont 2:143cac498751 8 * http://www.apache.org/licenses/LICENSE-2.0
mbed_official 0:fd0d7bdfcdc2 9 *
emilmont 2:143cac498751 10 * Unless required by applicable law or agreed to in writing, software
emilmont 2:143cac498751 11 * distributed under the License is distributed on an "AS IS" BASIS,
emilmont 2:143cac498751 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
emilmont 2:143cac498751 13 * See the License for the specific language governing permissions and
emilmont 2:143cac498751 14 * limitations under the License.
mbed_official 0:fd0d7bdfcdc2 15 */
mbed_official 0:fd0d7bdfcdc2 16 #ifndef MBED_I2C_H
mbed_official 0:fd0d7bdfcdc2 17 #define MBED_I2C_H
mbed_official 0:fd0d7bdfcdc2 18
mbed_official 0:fd0d7bdfcdc2 19 #include "platform.h"
mbed_official 0:fd0d7bdfcdc2 20
mbed_official 0:fd0d7bdfcdc2 21 #if DEVICE_I2C
mbed_official 0:fd0d7bdfcdc2 22
mbed_official 0:fd0d7bdfcdc2 23 #include "i2c_api.h"
mbed_official 0:fd0d7bdfcdc2 24
mbed_official 0:fd0d7bdfcdc2 25 namespace mbed {
mbed_official 0:fd0d7bdfcdc2 26
mbed_official 0:fd0d7bdfcdc2 27 /** An I2C Master, used for communicating with I2C slave devices
mbed_official 0:fd0d7bdfcdc2 28 *
mbed_official 0:fd0d7bdfcdc2 29 * Example:
mbed_official 0:fd0d7bdfcdc2 30 * @code
mbed_official 0:fd0d7bdfcdc2 31 * // Read from I2C slave at address 0x62
mbed_official 0:fd0d7bdfcdc2 32 *
mbed_official 0:fd0d7bdfcdc2 33 * #include "mbed.h"
mbed_official 0:fd0d7bdfcdc2 34 *
mbed_official 0:fd0d7bdfcdc2 35 * I2C i2c(p28, p27);
mbed_official 0:fd0d7bdfcdc2 36 *
mbed_official 0:fd0d7bdfcdc2 37 * int main() {
mbed_official 0:fd0d7bdfcdc2 38 * int address = 0x62;
mbed_official 0:fd0d7bdfcdc2 39 * char data[2];
mbed_official 0:fd0d7bdfcdc2 40 * i2c.read(address, data, 2);
mbed_official 0:fd0d7bdfcdc2 41 * }
mbed_official 0:fd0d7bdfcdc2 42 * @endcode
mbed_official 0:fd0d7bdfcdc2 43 */
mbed_official 0:fd0d7bdfcdc2 44 class I2C {
mbed_official 0:fd0d7bdfcdc2 45
mbed_official 0:fd0d7bdfcdc2 46 public:
mbed_official 0:fd0d7bdfcdc2 47 enum RxStatus {
mbed_official 0:fd0d7bdfcdc2 48 NoData,
mbed_official 0:fd0d7bdfcdc2 49 MasterGeneralCall,
mbed_official 0:fd0d7bdfcdc2 50 MasterWrite,
mbed_official 0:fd0d7bdfcdc2 51 MasterRead
mbed_official 0:fd0d7bdfcdc2 52 };
mbed_official 0:fd0d7bdfcdc2 53
mbed_official 0:fd0d7bdfcdc2 54 enum Acknowledge {
mbed_official 0:fd0d7bdfcdc2 55 NoACK = 0,
mbed_official 0:fd0d7bdfcdc2 56 ACK = 1
mbed_official 0:fd0d7bdfcdc2 57 };
mbed_official 0:fd0d7bdfcdc2 58
mbed_official 0:fd0d7bdfcdc2 59 /** Create an I2C Master interface, connected to the specified pins
mbed_official 0:fd0d7bdfcdc2 60 *
mbed_official 0:fd0d7bdfcdc2 61 * @param sda I2C data line pin
mbed_official 0:fd0d7bdfcdc2 62 * @param scl I2C clock line pin
mbed_official 0:fd0d7bdfcdc2 63 */
mbed_official 0:fd0d7bdfcdc2 64 I2C(PinName sda, PinName scl);
mbed_official 0:fd0d7bdfcdc2 65
mbed_official 0:fd0d7bdfcdc2 66 /** Set the frequency of the I2C interface
mbed_official 0:fd0d7bdfcdc2 67 *
mbed_official 0:fd0d7bdfcdc2 68 * @param hz The bus frequency in hertz
mbed_official 0:fd0d7bdfcdc2 69 */
mbed_official 0:fd0d7bdfcdc2 70 void frequency(int hz);
mbed_official 0:fd0d7bdfcdc2 71
mbed_official 0:fd0d7bdfcdc2 72 /** Read from an I2C slave
mbed_official 0:fd0d7bdfcdc2 73 *
mbed_official 0:fd0d7bdfcdc2 74 * Performs a complete read transaction. The bottom bit of
mbed_official 0:fd0d7bdfcdc2 75 * the address is forced to 1 to indicate a read.
mbed_official 0:fd0d7bdfcdc2 76 *
mbed_official 0:fd0d7bdfcdc2 77 * @param address 8-bit I2C slave address [ addr | 1 ]
emilmont 2:143cac498751 78 * @param data Pointer to the byte-array to read data in to
mbed_official 0:fd0d7bdfcdc2 79 * @param length Number of bytes to read
mbed_official 0:fd0d7bdfcdc2 80 * @param repeated Repeated start, true - don't send stop at end
mbed_official 0:fd0d7bdfcdc2 81 *
mbed_official 0:fd0d7bdfcdc2 82 * @returns
mbed_official 0:fd0d7bdfcdc2 83 * 0 on success (ack),
mbed_official 0:fd0d7bdfcdc2 84 * non-0 on failure (nack)
emilmont 2:143cac498751 85 */
mbed_official 0:fd0d7bdfcdc2 86 int read(int address, char *data, int length, bool repeated = false);
mbed_official 0:fd0d7bdfcdc2 87
mbed_official 0:fd0d7bdfcdc2 88 /** Read a single byte from the I2C bus
mbed_official 0:fd0d7bdfcdc2 89 *
mbed_official 0:fd0d7bdfcdc2 90 * @param ack indicates if the byte is to be acknowledged (1 = acknowledge)
mbed_official 0:fd0d7bdfcdc2 91 *
mbed_official 0:fd0d7bdfcdc2 92 * @returns
mbed_official 0:fd0d7bdfcdc2 93 * the byte read
mbed_official 0:fd0d7bdfcdc2 94 */
mbed_official 0:fd0d7bdfcdc2 95 int read(int ack);
mbed_official 0:fd0d7bdfcdc2 96
mbed_official 0:fd0d7bdfcdc2 97 /** Write to an I2C slave
mbed_official 0:fd0d7bdfcdc2 98 *
mbed_official 0:fd0d7bdfcdc2 99 * Performs a complete write transaction. The bottom bit of
mbed_official 0:fd0d7bdfcdc2 100 * the address is forced to 0 to indicate a write.
mbed_official 0:fd0d7bdfcdc2 101 *
mbed_official 0:fd0d7bdfcdc2 102 * @param address 8-bit I2C slave address [ addr | 0 ]
emilmont 2:143cac498751 103 * @param data Pointer to the byte-array data to send
mbed_official 0:fd0d7bdfcdc2 104 * @param length Number of bytes to send
mbed_official 0:fd0d7bdfcdc2 105 * @param repeated Repeated start, true - do not send stop at end
mbed_official 0:fd0d7bdfcdc2 106 *
mbed_official 0:fd0d7bdfcdc2 107 * @returns
mbed_official 0:fd0d7bdfcdc2 108 * 0 on success (ack),
mbed_official 0:fd0d7bdfcdc2 109 * non-0 on failure (nack)
emilmont 2:143cac498751 110 */
mbed_official 0:fd0d7bdfcdc2 111 int write(int address, const char *data, int length, bool repeated = false);
mbed_official 0:fd0d7bdfcdc2 112
mbed_official 0:fd0d7bdfcdc2 113 /** Write single byte out on the I2C bus
mbed_official 0:fd0d7bdfcdc2 114 *
mbed_official 0:fd0d7bdfcdc2 115 * @param data data to write out on bus
mbed_official 0:fd0d7bdfcdc2 116 *
mbed_official 0:fd0d7bdfcdc2 117 * @returns
mbed_official 0:fd0d7bdfcdc2 118 * '1' if an ACK was received,
mbed_official 0:fd0d7bdfcdc2 119 * '0' otherwise
mbed_official 0:fd0d7bdfcdc2 120 */
mbed_official 0:fd0d7bdfcdc2 121 int write(int data);
mbed_official 0:fd0d7bdfcdc2 122
mbed_official 0:fd0d7bdfcdc2 123 /** Creates a start condition on the I2C bus
mbed_official 0:fd0d7bdfcdc2 124 */
mbed_official 0:fd0d7bdfcdc2 125
mbed_official 0:fd0d7bdfcdc2 126 void start(void);
mbed_official 0:fd0d7bdfcdc2 127
mbed_official 0:fd0d7bdfcdc2 128 /** Creates a stop condition on the I2C bus
mbed_official 0:fd0d7bdfcdc2 129 */
mbed_official 0:fd0d7bdfcdc2 130 void stop(void);
mbed_official 0:fd0d7bdfcdc2 131
mbed_official 0:fd0d7bdfcdc2 132 protected:
mbed_official 0:fd0d7bdfcdc2 133 void aquire();
emilmont 2:143cac498751 134
mbed_official 0:fd0d7bdfcdc2 135 i2c_t _i2c;
mbed_official 0:fd0d7bdfcdc2 136 static I2C *_owner;
mbed_official 0:fd0d7bdfcdc2 137 int _hz;
mbed_official 0:fd0d7bdfcdc2 138 };
mbed_official 0:fd0d7bdfcdc2 139
mbed_official 0:fd0d7bdfcdc2 140 } // namespace mbed
mbed_official 0:fd0d7bdfcdc2 141
mbed_official 0:fd0d7bdfcdc2 142 #endif
mbed_official 0:fd0d7bdfcdc2 143
mbed_official 0:fd0d7bdfcdc2 144 #endif