5.2.1 - Updated I2C files

Dependents:   mbed-TFT-example-NCS36510 mbed-Accelerometer-example-NCS36510 mbed-Accelerometer-example-NCS36510

Committer:
jacobjohnson
Date:
Mon Feb 27 17:45:05 2017 +0000
Revision:
1:f30bdcd2b33b
Parent:
0:098463de4c5d
changed the inputscale from 1 to 7 in analogin_api.c.  This will need to be changed later, and accessed from the main level, but for now this allows the  adc to read a value from 0 to 3.7V, instead of just up to 1V.;

Who changed what in which revision?

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