mbed library sources that still uses the stm standard peripheral library
Fork of mbed-src by
cpp/I2C.h@0:fd0d7bdfcdc2, 2012-11-20 (annotated)
- Committer:
- mbed_official
- Date:
- Tue Nov 20 17:24:08 2012 +0000
- Revision:
- 0:fd0d7bdfcdc2
- Child:
- 2:143cac498751
mbed sources
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
mbed_official | 0:fd0d7bdfcdc2 | 1 | /* mbed Microcontroller Library |
mbed_official | 0:fd0d7bdfcdc2 | 2 | * Copyright (c) 2006-2012 ARM Limited |
mbed_official | 0:fd0d7bdfcdc2 | 3 | * |
mbed_official | 0:fd0d7bdfcdc2 | 4 | * Permission is hereby granted, free of charge, to any person obtaining a copy |
mbed_official | 0:fd0d7bdfcdc2 | 5 | * of this software and associated documentation files (the "Software"), to deal |
mbed_official | 0:fd0d7bdfcdc2 | 6 | * in the Software without restriction, including without limitation the rights |
mbed_official | 0:fd0d7bdfcdc2 | 7 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
mbed_official | 0:fd0d7bdfcdc2 | 8 | * copies of the Software, and to permit persons to whom the Software is |
mbed_official | 0:fd0d7bdfcdc2 | 9 | * furnished to do so, subject to the following conditions: |
mbed_official | 0:fd0d7bdfcdc2 | 10 | * |
mbed_official | 0:fd0d7bdfcdc2 | 11 | * The above copyright notice and this permission notice shall be included in |
mbed_official | 0:fd0d7bdfcdc2 | 12 | * all copies or substantial portions of the Software. |
mbed_official | 0:fd0d7bdfcdc2 | 13 | * |
mbed_official | 0:fd0d7bdfcdc2 | 14 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
mbed_official | 0:fd0d7bdfcdc2 | 15 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
mbed_official | 0:fd0d7bdfcdc2 | 16 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
mbed_official | 0:fd0d7bdfcdc2 | 17 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
mbed_official | 0:fd0d7bdfcdc2 | 18 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
mbed_official | 0:fd0d7bdfcdc2 | 19 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE |
mbed_official | 0:fd0d7bdfcdc2 | 20 | * SOFTWARE. |
mbed_official | 0:fd0d7bdfcdc2 | 21 | */ |
mbed_official | 0:fd0d7bdfcdc2 | 22 | #ifndef MBED_I2C_H |
mbed_official | 0:fd0d7bdfcdc2 | 23 | #define MBED_I2C_H |
mbed_official | 0:fd0d7bdfcdc2 | 24 | |
mbed_official | 0:fd0d7bdfcdc2 | 25 | #include "platform.h" |
mbed_official | 0:fd0d7bdfcdc2 | 26 | |
mbed_official | 0:fd0d7bdfcdc2 | 27 | #if DEVICE_I2C |
mbed_official | 0:fd0d7bdfcdc2 | 28 | |
mbed_official | 0:fd0d7bdfcdc2 | 29 | #include "i2c_api.h" |
mbed_official | 0:fd0d7bdfcdc2 | 30 | |
mbed_official | 0:fd0d7bdfcdc2 | 31 | namespace mbed { |
mbed_official | 0:fd0d7bdfcdc2 | 32 | |
mbed_official | 0:fd0d7bdfcdc2 | 33 | /** An I2C Master, used for communicating with I2C slave devices |
mbed_official | 0:fd0d7bdfcdc2 | 34 | * |
mbed_official | 0:fd0d7bdfcdc2 | 35 | * Example: |
mbed_official | 0:fd0d7bdfcdc2 | 36 | * @code |
mbed_official | 0:fd0d7bdfcdc2 | 37 | * // Read from I2C slave at address 0x62 |
mbed_official | 0:fd0d7bdfcdc2 | 38 | * |
mbed_official | 0:fd0d7bdfcdc2 | 39 | * #include "mbed.h" |
mbed_official | 0:fd0d7bdfcdc2 | 40 | * |
mbed_official | 0:fd0d7bdfcdc2 | 41 | * I2C i2c(p28, p27); |
mbed_official | 0:fd0d7bdfcdc2 | 42 | * |
mbed_official | 0:fd0d7bdfcdc2 | 43 | * int main() { |
mbed_official | 0:fd0d7bdfcdc2 | 44 | * int address = 0x62; |
mbed_official | 0:fd0d7bdfcdc2 | 45 | * char data[2]; |
mbed_official | 0:fd0d7bdfcdc2 | 46 | * i2c.read(address, data, 2); |
mbed_official | 0:fd0d7bdfcdc2 | 47 | * } |
mbed_official | 0:fd0d7bdfcdc2 | 48 | * @endcode |
mbed_official | 0:fd0d7bdfcdc2 | 49 | */ |
mbed_official | 0:fd0d7bdfcdc2 | 50 | class I2C { |
mbed_official | 0:fd0d7bdfcdc2 | 51 | |
mbed_official | 0:fd0d7bdfcdc2 | 52 | public: |
mbed_official | 0:fd0d7bdfcdc2 | 53 | enum RxStatus { |
mbed_official | 0:fd0d7bdfcdc2 | 54 | NoData, |
mbed_official | 0:fd0d7bdfcdc2 | 55 | MasterGeneralCall, |
mbed_official | 0:fd0d7bdfcdc2 | 56 | MasterWrite, |
mbed_official | 0:fd0d7bdfcdc2 | 57 | MasterRead |
mbed_official | 0:fd0d7bdfcdc2 | 58 | }; |
mbed_official | 0:fd0d7bdfcdc2 | 59 | |
mbed_official | 0:fd0d7bdfcdc2 | 60 | enum Acknowledge { |
mbed_official | 0:fd0d7bdfcdc2 | 61 | NoACK = 0, |
mbed_official | 0:fd0d7bdfcdc2 | 62 | ACK = 1 |
mbed_official | 0:fd0d7bdfcdc2 | 63 | }; |
mbed_official | 0:fd0d7bdfcdc2 | 64 | |
mbed_official | 0:fd0d7bdfcdc2 | 65 | /** Create an I2C Master interface, connected to the specified pins |
mbed_official | 0:fd0d7bdfcdc2 | 66 | * |
mbed_official | 0:fd0d7bdfcdc2 | 67 | * @param sda I2C data line pin |
mbed_official | 0:fd0d7bdfcdc2 | 68 | * @param scl I2C clock line pin |
mbed_official | 0:fd0d7bdfcdc2 | 69 | */ |
mbed_official | 0:fd0d7bdfcdc2 | 70 | I2C(PinName sda, PinName scl); |
mbed_official | 0:fd0d7bdfcdc2 | 71 | |
mbed_official | 0:fd0d7bdfcdc2 | 72 | /** Set the frequency of the I2C interface |
mbed_official | 0:fd0d7bdfcdc2 | 73 | * |
mbed_official | 0:fd0d7bdfcdc2 | 74 | * @param hz The bus frequency in hertz |
mbed_official | 0:fd0d7bdfcdc2 | 75 | */ |
mbed_official | 0:fd0d7bdfcdc2 | 76 | void frequency(int hz); |
mbed_official | 0:fd0d7bdfcdc2 | 77 | |
mbed_official | 0:fd0d7bdfcdc2 | 78 | /** Read from an I2C slave |
mbed_official | 0:fd0d7bdfcdc2 | 79 | * |
mbed_official | 0:fd0d7bdfcdc2 | 80 | * Performs a complete read transaction. The bottom bit of |
mbed_official | 0:fd0d7bdfcdc2 | 81 | * the address is forced to 1 to indicate a read. |
mbed_official | 0:fd0d7bdfcdc2 | 82 | * |
mbed_official | 0:fd0d7bdfcdc2 | 83 | * @param address 8-bit I2C slave address [ addr | 1 ] |
mbed_official | 0:fd0d7bdfcdc2 | 84 | * @param data Pointer to the byte-array to read data in to |
mbed_official | 0:fd0d7bdfcdc2 | 85 | * @param length Number of bytes to read |
mbed_official | 0:fd0d7bdfcdc2 | 86 | * @param repeated Repeated start, true - don't send stop at end |
mbed_official | 0:fd0d7bdfcdc2 | 87 | * |
mbed_official | 0:fd0d7bdfcdc2 | 88 | * @returns |
mbed_official | 0:fd0d7bdfcdc2 | 89 | * 0 on success (ack), |
mbed_official | 0:fd0d7bdfcdc2 | 90 | * non-0 on failure (nack) |
mbed_official | 0:fd0d7bdfcdc2 | 91 | */ |
mbed_official | 0:fd0d7bdfcdc2 | 92 | int read(int address, char *data, int length, bool repeated = false); |
mbed_official | 0:fd0d7bdfcdc2 | 93 | |
mbed_official | 0:fd0d7bdfcdc2 | 94 | /** Read a single byte from the I2C bus |
mbed_official | 0:fd0d7bdfcdc2 | 95 | * |
mbed_official | 0:fd0d7bdfcdc2 | 96 | * @param ack indicates if the byte is to be acknowledged (1 = acknowledge) |
mbed_official | 0:fd0d7bdfcdc2 | 97 | * |
mbed_official | 0:fd0d7bdfcdc2 | 98 | * @returns |
mbed_official | 0:fd0d7bdfcdc2 | 99 | * the byte read |
mbed_official | 0:fd0d7bdfcdc2 | 100 | */ |
mbed_official | 0:fd0d7bdfcdc2 | 101 | int read(int ack); |
mbed_official | 0:fd0d7bdfcdc2 | 102 | |
mbed_official | 0:fd0d7bdfcdc2 | 103 | /** Write to an I2C slave |
mbed_official | 0:fd0d7bdfcdc2 | 104 | * |
mbed_official | 0:fd0d7bdfcdc2 | 105 | * Performs a complete write transaction. The bottom bit of |
mbed_official | 0:fd0d7bdfcdc2 | 106 | * the address is forced to 0 to indicate a write. |
mbed_official | 0:fd0d7bdfcdc2 | 107 | * |
mbed_official | 0:fd0d7bdfcdc2 | 108 | * @param address 8-bit I2C slave address [ addr | 0 ] |
mbed_official | 0:fd0d7bdfcdc2 | 109 | * @param data Pointer to the byte-array data to send |
mbed_official | 0:fd0d7bdfcdc2 | 110 | * @param length Number of bytes to send |
mbed_official | 0:fd0d7bdfcdc2 | 111 | * @param repeated Repeated start, true - do not send stop at end |
mbed_official | 0:fd0d7bdfcdc2 | 112 | * |
mbed_official | 0:fd0d7bdfcdc2 | 113 | * @returns |
mbed_official | 0:fd0d7bdfcdc2 | 114 | * 0 on success (ack), |
mbed_official | 0:fd0d7bdfcdc2 | 115 | * non-0 on failure (nack) |
mbed_official | 0:fd0d7bdfcdc2 | 116 | */ |
mbed_official | 0:fd0d7bdfcdc2 | 117 | int write(int address, const char *data, int length, bool repeated = false); |
mbed_official | 0:fd0d7bdfcdc2 | 118 | |
mbed_official | 0:fd0d7bdfcdc2 | 119 | /** Write single byte out on the I2C bus |
mbed_official | 0:fd0d7bdfcdc2 | 120 | * |
mbed_official | 0:fd0d7bdfcdc2 | 121 | * @param data data to write out on bus |
mbed_official | 0:fd0d7bdfcdc2 | 122 | * |
mbed_official | 0:fd0d7bdfcdc2 | 123 | * @returns |
mbed_official | 0:fd0d7bdfcdc2 | 124 | * '1' if an ACK was received, |
mbed_official | 0:fd0d7bdfcdc2 | 125 | * '0' otherwise |
mbed_official | 0:fd0d7bdfcdc2 | 126 | */ |
mbed_official | 0:fd0d7bdfcdc2 | 127 | int write(int data); |
mbed_official | 0:fd0d7bdfcdc2 | 128 | |
mbed_official | 0:fd0d7bdfcdc2 | 129 | /** Creates a start condition on the I2C bus |
mbed_official | 0:fd0d7bdfcdc2 | 130 | */ |
mbed_official | 0:fd0d7bdfcdc2 | 131 | |
mbed_official | 0:fd0d7bdfcdc2 | 132 | void start(void); |
mbed_official | 0:fd0d7bdfcdc2 | 133 | |
mbed_official | 0:fd0d7bdfcdc2 | 134 | /** Creates a stop condition on the I2C bus |
mbed_official | 0:fd0d7bdfcdc2 | 135 | */ |
mbed_official | 0:fd0d7bdfcdc2 | 136 | void stop(void); |
mbed_official | 0:fd0d7bdfcdc2 | 137 | |
mbed_official | 0:fd0d7bdfcdc2 | 138 | protected: |
mbed_official | 0:fd0d7bdfcdc2 | 139 | void aquire(); |
mbed_official | 0:fd0d7bdfcdc2 | 140 | |
mbed_official | 0:fd0d7bdfcdc2 | 141 | i2c_t _i2c; |
mbed_official | 0:fd0d7bdfcdc2 | 142 | static I2C *_owner; |
mbed_official | 0:fd0d7bdfcdc2 | 143 | int _hz; |
mbed_official | 0:fd0d7bdfcdc2 | 144 | }; |
mbed_official | 0:fd0d7bdfcdc2 | 145 | |
mbed_official | 0:fd0d7bdfcdc2 | 146 | } // namespace mbed |
mbed_official | 0:fd0d7bdfcdc2 | 147 | |
mbed_official | 0:fd0d7bdfcdc2 | 148 | #endif |
mbed_official | 0:fd0d7bdfcdc2 | 149 | |
mbed_official | 0:fd0d7bdfcdc2 | 150 | #endif |