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