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