mbed library for NZ32-SC151

Committer:
modtronix-com
Date:
Fri Aug 19 15:46:42 2016 +1000
Revision:
17:639ed60ce759
Parent:
1:71204b8406f2
Added tag v1.1 for changeset 076cbe3e55be

Who changed what in which revision?

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