Preliminary main mbed library for nexpaq development

Committer:
nexpaq
Date:
Fri Nov 04 20:54:50 2016 +0000
Revision:
1:d96dbedaebdb
Parent:
0:6c56fb4bc5f0
Removed extra directories for other platforms

Who changed what in which revision?

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