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