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