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