Describes predefine macros for mbed online compiler (armcc)

Committer:
MACRUM
Date:
Thu Mar 16 21:58:09 2017 +0900
Revision:
6:40e873bbc5f7
Add licence header info

Who changed what in which revision?

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