Knight KE
/
Game_Master
游戏王对战板,目前code还是空的
mbed-os/drivers/I2C.h@0:9b3d4731edbb, 2018-06-21 (annotated)
- Committer:
- WFKnight
- Date:
- Thu Jun 21 13:51:43 2018 +0000
- Revision:
- 0:9b3d4731edbb
UART, RTOS, LED
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
WFKnight | 0:9b3d4731edbb | 1 | /* mbed Microcontroller Library |
WFKnight | 0:9b3d4731edbb | 2 | * Copyright (c) 2006-2015 ARM Limited |
WFKnight | 0:9b3d4731edbb | 3 | * |
WFKnight | 0:9b3d4731edbb | 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
WFKnight | 0:9b3d4731edbb | 5 | * you may not use this file except in compliance with the License. |
WFKnight | 0:9b3d4731edbb | 6 | * You may obtain a copy of the License at |
WFKnight | 0:9b3d4731edbb | 7 | * |
WFKnight | 0:9b3d4731edbb | 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
WFKnight | 0:9b3d4731edbb | 9 | * |
WFKnight | 0:9b3d4731edbb | 10 | * Unless required by applicable law or agreed to in writing, software |
WFKnight | 0:9b3d4731edbb | 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
WFKnight | 0:9b3d4731edbb | 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
WFKnight | 0:9b3d4731edbb | 13 | * See the License for the specific language governing permissions and |
WFKnight | 0:9b3d4731edbb | 14 | * limitations under the License. |
WFKnight | 0:9b3d4731edbb | 15 | */ |
WFKnight | 0:9b3d4731edbb | 16 | #ifndef MBED_I2C_H |
WFKnight | 0:9b3d4731edbb | 17 | #define MBED_I2C_H |
WFKnight | 0:9b3d4731edbb | 18 | |
WFKnight | 0:9b3d4731edbb | 19 | #include "platform/platform.h" |
WFKnight | 0:9b3d4731edbb | 20 | |
WFKnight | 0:9b3d4731edbb | 21 | #if defined (DEVICE_I2C) || defined(DOXYGEN_ONLY) |
WFKnight | 0:9b3d4731edbb | 22 | |
WFKnight | 0:9b3d4731edbb | 23 | #include "hal/i2c_api.h" |
WFKnight | 0:9b3d4731edbb | 24 | #include "platform/SingletonPtr.h" |
WFKnight | 0:9b3d4731edbb | 25 | #include "platform/PlatformMutex.h" |
WFKnight | 0:9b3d4731edbb | 26 | #include "platform/NonCopyable.h" |
WFKnight | 0:9b3d4731edbb | 27 | |
WFKnight | 0:9b3d4731edbb | 28 | #if DEVICE_I2C_ASYNCH |
WFKnight | 0:9b3d4731edbb | 29 | #include "platform/CThunk.h" |
WFKnight | 0:9b3d4731edbb | 30 | #include "hal/dma_api.h" |
WFKnight | 0:9b3d4731edbb | 31 | #include "platform/FunctionPointer.h" |
WFKnight | 0:9b3d4731edbb | 32 | #endif |
WFKnight | 0:9b3d4731edbb | 33 | |
WFKnight | 0:9b3d4731edbb | 34 | namespace mbed { |
WFKnight | 0:9b3d4731edbb | 35 | /** \addtogroup drivers */ |
WFKnight | 0:9b3d4731edbb | 36 | |
WFKnight | 0:9b3d4731edbb | 37 | /** An I2C Master, used for communicating with I2C slave devices |
WFKnight | 0:9b3d4731edbb | 38 | * |
WFKnight | 0:9b3d4731edbb | 39 | * @note Synchronization level: Thread safe |
WFKnight | 0:9b3d4731edbb | 40 | * |
WFKnight | 0:9b3d4731edbb | 41 | * Example: |
WFKnight | 0:9b3d4731edbb | 42 | * @code |
WFKnight | 0:9b3d4731edbb | 43 | * // Read from I2C slave at address 0x62 |
WFKnight | 0:9b3d4731edbb | 44 | * |
WFKnight | 0:9b3d4731edbb | 45 | * #include "mbed.h" |
WFKnight | 0:9b3d4731edbb | 46 | * |
WFKnight | 0:9b3d4731edbb | 47 | * I2C i2c(p28, p27); |
WFKnight | 0:9b3d4731edbb | 48 | * |
WFKnight | 0:9b3d4731edbb | 49 | * int main() { |
WFKnight | 0:9b3d4731edbb | 50 | * int address = 0x62; |
WFKnight | 0:9b3d4731edbb | 51 | * char data[2]; |
WFKnight | 0:9b3d4731edbb | 52 | * i2c.read(address, data, 2); |
WFKnight | 0:9b3d4731edbb | 53 | * } |
WFKnight | 0:9b3d4731edbb | 54 | * @endcode |
WFKnight | 0:9b3d4731edbb | 55 | * @ingroup drivers |
WFKnight | 0:9b3d4731edbb | 56 | */ |
WFKnight | 0:9b3d4731edbb | 57 | class I2C : private NonCopyable<I2C> { |
WFKnight | 0:9b3d4731edbb | 58 | |
WFKnight | 0:9b3d4731edbb | 59 | public: |
WFKnight | 0:9b3d4731edbb | 60 | enum RxStatus { |
WFKnight | 0:9b3d4731edbb | 61 | NoData, |
WFKnight | 0:9b3d4731edbb | 62 | MasterGeneralCall, |
WFKnight | 0:9b3d4731edbb | 63 | MasterWrite, |
WFKnight | 0:9b3d4731edbb | 64 | MasterRead |
WFKnight | 0:9b3d4731edbb | 65 | }; |
WFKnight | 0:9b3d4731edbb | 66 | |
WFKnight | 0:9b3d4731edbb | 67 | enum Acknowledge { |
WFKnight | 0:9b3d4731edbb | 68 | NoACK = 0, |
WFKnight | 0:9b3d4731edbb | 69 | ACK = 1 |
WFKnight | 0:9b3d4731edbb | 70 | }; |
WFKnight | 0:9b3d4731edbb | 71 | |
WFKnight | 0:9b3d4731edbb | 72 | /** Create an I2C Master interface, connected to the specified pins |
WFKnight | 0:9b3d4731edbb | 73 | * |
WFKnight | 0:9b3d4731edbb | 74 | * @param sda I2C data line pin |
WFKnight | 0:9b3d4731edbb | 75 | * @param scl I2C clock line pin |
WFKnight | 0:9b3d4731edbb | 76 | */ |
WFKnight | 0:9b3d4731edbb | 77 | I2C(PinName sda, PinName scl); |
WFKnight | 0:9b3d4731edbb | 78 | |
WFKnight | 0:9b3d4731edbb | 79 | /** Set the frequency of the I2C interface |
WFKnight | 0:9b3d4731edbb | 80 | * |
WFKnight | 0:9b3d4731edbb | 81 | * @param hz The bus frequency in hertz |
WFKnight | 0:9b3d4731edbb | 82 | */ |
WFKnight | 0:9b3d4731edbb | 83 | void frequency(int hz); |
WFKnight | 0:9b3d4731edbb | 84 | |
WFKnight | 0:9b3d4731edbb | 85 | /** Read from an I2C slave |
WFKnight | 0:9b3d4731edbb | 86 | * |
WFKnight | 0:9b3d4731edbb | 87 | * Performs a complete read transaction. The bottom bit of |
WFKnight | 0:9b3d4731edbb | 88 | * the address is forced to 1 to indicate a read. |
WFKnight | 0:9b3d4731edbb | 89 | * |
WFKnight | 0:9b3d4731edbb | 90 | * @param address 8-bit I2C slave address [ addr | 1 ] |
WFKnight | 0:9b3d4731edbb | 91 | * @param data Pointer to the byte-array to read data in to |
WFKnight | 0:9b3d4731edbb | 92 | * @param length Number of bytes to read |
WFKnight | 0:9b3d4731edbb | 93 | * @param repeated Repeated start, true - don't send stop at end |
WFKnight | 0:9b3d4731edbb | 94 | * |
WFKnight | 0:9b3d4731edbb | 95 | * @returns |
WFKnight | 0:9b3d4731edbb | 96 | * 0 on success (ack), |
WFKnight | 0:9b3d4731edbb | 97 | * non-0 on failure (nack) |
WFKnight | 0:9b3d4731edbb | 98 | */ |
WFKnight | 0:9b3d4731edbb | 99 | int read(int address, char *data, int length, bool repeated = false); |
WFKnight | 0:9b3d4731edbb | 100 | |
WFKnight | 0:9b3d4731edbb | 101 | /** Read a single byte from the I2C bus |
WFKnight | 0:9b3d4731edbb | 102 | * |
WFKnight | 0:9b3d4731edbb | 103 | * @param ack indicates if the byte is to be acknowledged (1 = acknowledge) |
WFKnight | 0:9b3d4731edbb | 104 | * |
WFKnight | 0:9b3d4731edbb | 105 | * @returns |
WFKnight | 0:9b3d4731edbb | 106 | * the byte read |
WFKnight | 0:9b3d4731edbb | 107 | */ |
WFKnight | 0:9b3d4731edbb | 108 | int read(int ack); |
WFKnight | 0:9b3d4731edbb | 109 | |
WFKnight | 0:9b3d4731edbb | 110 | /** Write to an I2C slave |
WFKnight | 0:9b3d4731edbb | 111 | * |
WFKnight | 0:9b3d4731edbb | 112 | * Performs a complete write transaction. The bottom bit of |
WFKnight | 0:9b3d4731edbb | 113 | * the address is forced to 0 to indicate a write. |
WFKnight | 0:9b3d4731edbb | 114 | * |
WFKnight | 0:9b3d4731edbb | 115 | * @param address 8-bit I2C slave address [ addr | 0 ] |
WFKnight | 0:9b3d4731edbb | 116 | * @param data Pointer to the byte-array data to send |
WFKnight | 0:9b3d4731edbb | 117 | * @param length Number of bytes to send |
WFKnight | 0:9b3d4731edbb | 118 | * @param repeated Repeated start, true - do not send stop at end |
WFKnight | 0:9b3d4731edbb | 119 | * |
WFKnight | 0:9b3d4731edbb | 120 | * @returns |
WFKnight | 0:9b3d4731edbb | 121 | * 0 on success (ack), |
WFKnight | 0:9b3d4731edbb | 122 | * non-0 on failure (nack) |
WFKnight | 0:9b3d4731edbb | 123 | */ |
WFKnight | 0:9b3d4731edbb | 124 | int write(int address, const char *data, int length, bool repeated = false); |
WFKnight | 0:9b3d4731edbb | 125 | |
WFKnight | 0:9b3d4731edbb | 126 | /** Write single byte out on the I2C bus |
WFKnight | 0:9b3d4731edbb | 127 | * |
WFKnight | 0:9b3d4731edbb | 128 | * @param data data to write out on bus |
WFKnight | 0:9b3d4731edbb | 129 | * |
WFKnight | 0:9b3d4731edbb | 130 | * @returns |
WFKnight | 0:9b3d4731edbb | 131 | * '0' - NAK was received |
WFKnight | 0:9b3d4731edbb | 132 | * '1' - ACK was received, |
WFKnight | 0:9b3d4731edbb | 133 | * '2' - timeout |
WFKnight | 0:9b3d4731edbb | 134 | */ |
WFKnight | 0:9b3d4731edbb | 135 | int write(int data); |
WFKnight | 0:9b3d4731edbb | 136 | |
WFKnight | 0:9b3d4731edbb | 137 | /** Creates a start condition on the I2C bus |
WFKnight | 0:9b3d4731edbb | 138 | */ |
WFKnight | 0:9b3d4731edbb | 139 | |
WFKnight | 0:9b3d4731edbb | 140 | void start(void); |
WFKnight | 0:9b3d4731edbb | 141 | |
WFKnight | 0:9b3d4731edbb | 142 | /** Creates a stop condition on the I2C bus |
WFKnight | 0:9b3d4731edbb | 143 | */ |
WFKnight | 0:9b3d4731edbb | 144 | void stop(void); |
WFKnight | 0:9b3d4731edbb | 145 | |
WFKnight | 0:9b3d4731edbb | 146 | /** Acquire exclusive access to this I2C bus |
WFKnight | 0:9b3d4731edbb | 147 | */ |
WFKnight | 0:9b3d4731edbb | 148 | virtual void lock(void); |
WFKnight | 0:9b3d4731edbb | 149 | |
WFKnight | 0:9b3d4731edbb | 150 | /** Release exclusive access to this I2C bus |
WFKnight | 0:9b3d4731edbb | 151 | */ |
WFKnight | 0:9b3d4731edbb | 152 | virtual void unlock(void); |
WFKnight | 0:9b3d4731edbb | 153 | |
WFKnight | 0:9b3d4731edbb | 154 | virtual ~I2C() { |
WFKnight | 0:9b3d4731edbb | 155 | // Do nothing |
WFKnight | 0:9b3d4731edbb | 156 | } |
WFKnight | 0:9b3d4731edbb | 157 | |
WFKnight | 0:9b3d4731edbb | 158 | #if DEVICE_I2C_ASYNCH |
WFKnight | 0:9b3d4731edbb | 159 | |
WFKnight | 0:9b3d4731edbb | 160 | /** Start non-blocking I2C transfer. |
WFKnight | 0:9b3d4731edbb | 161 | * |
WFKnight | 0:9b3d4731edbb | 162 | * This function locks the deep sleep until any event has occurred |
WFKnight | 0:9b3d4731edbb | 163 | * |
WFKnight | 0:9b3d4731edbb | 164 | * @param address 8/10 bit I2C slave address |
WFKnight | 0:9b3d4731edbb | 165 | * @param tx_buffer The TX buffer with data to be transfered |
WFKnight | 0:9b3d4731edbb | 166 | * @param tx_length The length of TX buffer in bytes |
WFKnight | 0:9b3d4731edbb | 167 | * @param rx_buffer The RX buffer which is used for received data |
WFKnight | 0:9b3d4731edbb | 168 | * @param rx_length The length of RX buffer in bytes |
WFKnight | 0:9b3d4731edbb | 169 | * @param event The logical OR of events to modify |
WFKnight | 0:9b3d4731edbb | 170 | * @param callback The event callback function |
WFKnight | 0:9b3d4731edbb | 171 | * @param repeated Repeated start, true - do not send stop at end |
WFKnight | 0:9b3d4731edbb | 172 | * @return Zero if the transfer has started, or -1 if I2C peripheral is busy |
WFKnight | 0:9b3d4731edbb | 173 | */ |
WFKnight | 0:9b3d4731edbb | 174 | int transfer(int address, const char *tx_buffer, int tx_length, char *rx_buffer, int rx_length, const event_callback_t& callback, int event = I2C_EVENT_TRANSFER_COMPLETE, bool repeated = false); |
WFKnight | 0:9b3d4731edbb | 175 | |
WFKnight | 0:9b3d4731edbb | 176 | /** Abort the on-going I2C transfer |
WFKnight | 0:9b3d4731edbb | 177 | */ |
WFKnight | 0:9b3d4731edbb | 178 | void abort_transfer(); |
WFKnight | 0:9b3d4731edbb | 179 | |
WFKnight | 0:9b3d4731edbb | 180 | protected: |
WFKnight | 0:9b3d4731edbb | 181 | /** Lock deep sleep only if it is not yet locked */ |
WFKnight | 0:9b3d4731edbb | 182 | void lock_deep_sleep(); |
WFKnight | 0:9b3d4731edbb | 183 | |
WFKnight | 0:9b3d4731edbb | 184 | /** Unlock deep sleep only if it has been locked */ |
WFKnight | 0:9b3d4731edbb | 185 | void unlock_deep_sleep(); |
WFKnight | 0:9b3d4731edbb | 186 | |
WFKnight | 0:9b3d4731edbb | 187 | void irq_handler_asynch(void); |
WFKnight | 0:9b3d4731edbb | 188 | event_callback_t _callback; |
WFKnight | 0:9b3d4731edbb | 189 | CThunk<I2C> _irq; |
WFKnight | 0:9b3d4731edbb | 190 | DMAUsage _usage; |
WFKnight | 0:9b3d4731edbb | 191 | bool _deep_sleep_locked; |
WFKnight | 0:9b3d4731edbb | 192 | #endif |
WFKnight | 0:9b3d4731edbb | 193 | |
WFKnight | 0:9b3d4731edbb | 194 | protected: |
WFKnight | 0:9b3d4731edbb | 195 | void aquire(); |
WFKnight | 0:9b3d4731edbb | 196 | |
WFKnight | 0:9b3d4731edbb | 197 | i2c_t _i2c; |
WFKnight | 0:9b3d4731edbb | 198 | static I2C *_owner; |
WFKnight | 0:9b3d4731edbb | 199 | int _hz; |
WFKnight | 0:9b3d4731edbb | 200 | static SingletonPtr<PlatformMutex> _mutex; |
WFKnight | 0:9b3d4731edbb | 201 | }; |
WFKnight | 0:9b3d4731edbb | 202 | |
WFKnight | 0:9b3d4731edbb | 203 | } // namespace mbed |
WFKnight | 0:9b3d4731edbb | 204 | |
WFKnight | 0:9b3d4731edbb | 205 | #endif |
WFKnight | 0:9b3d4731edbb | 206 | |
WFKnight | 0:9b3d4731edbb | 207 | #endif |